LCOV - code coverage report
Current view: top level - lib_dec - lib_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 1193 1848 64.6 %
Date: 2026-02-12 08:06:16 Functions: 71 94 75.5 %

          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        1939 : 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        1939 :     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        1939 :     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        1939 :     hIvasDec = *phIvasDec;
     163        1939 :     hIvasDec->hVoIP = NULL;
     164        1939 :     hIvasDec->hTimeScaler = NULL;
     165        1939 :     hIvasDec->tsm_scale = 100;
     166        1939 :     hIvasDec->tsm_max_scaling = 0;
     167        1939 :     hIvasDec->tsm_quality = 1.0f;
     168        1939 :     hIvasDec->timeScalingDone = 0;
     169        1939 :     hIvasDec->needNewFrame = false;
     170        1939 :     hIvasDec->nTransportChannelsOld = 0;
     171        1939 :     hIvasDec->nSamplesAvailableNext = 0;
     172        1939 :     hIvasDec->nSamplesFrame = 0;
     173        1939 :     hIvasDec->hasBeenFedFrame = false;
     174        1939 :     hIvasDec->hasBeenFedFirstGoodFrame = false;
     175        1939 :     hIvasDec->hasDecodedFirstGoodFrame = false;
     176        1939 :     hIvasDec->isInitialized = false;
     177        1939 :     hIvasDec->updateOrientation = false;
     178        1939 :     hIvasDec->flushbuffer = NULL;
     179        1939 :     hIvasDec->pcmType = IVAS_DEC_PCM_INVALID;
     180        1939 :     hIvasDec->nSamplesFlushed = 0;
     181        1939 :     hIvasDec->hasBeenPreparedRendering = false;
     182             : 
     183        1939 :     hIvasDec->mode = mode;
     184             : 
     185        1939 :     hIvasDec->bitstreamformat = G192;
     186             : #ifdef DEBUGGING
     187             :     hIvasDec->Opt_VOIP = 0;
     188             : #endif
     189        1939 :     hIvasDec->amrwb_rfc4867_flag = -1;
     190        1939 :     hIvasDec->prev_ft_speech = 1; /* RXDTX handler previous frametype flag for G.192 format AMRWB SID_FIRST detection */
     191        1939 :     hIvasDec->CNG = 0;            /* RXDTX handler CNG = 1, no CNG = 0*/
     192             : 
     193             :     /*-----------------------------------------------------------------*
     194             :      * Initialize IVAS-codec decoder state
     195             :      *-----------------------------------------------------------------*/
     196             : 
     197        1939 :     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        1939 :     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        1939 :     st_ivas = hIvasDec->st_ivas;
     212             : 
     213             :     /* initialize Decoder Config. handle */
     214        1939 :     init_decoder_config( hIvasDec->st_ivas->hDecoderConfig );
     215             : 
     216             :     /* initialize pointers to handles to NULL */
     217        1939 :     ivas_initialize_handles_dec( st_ivas );
     218             : 
     219        1939 :     st_ivas->restartNeeded = 0;
     220             : 
     221             :     /* set high-level parameters */
     222             : 
     223        1939 :     st_ivas->codec_mode = 0; /* unknown before first frame */
     224        1939 :     st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
     225        1939 :     st_ivas->intern_config = IVAS_AUDIO_CONFIG_INVALID;
     226        1939 :     st_ivas->writeFECoffset = 0;
     227        1939 :     st_ivas->sba_analysis_order = 0; /* not really used in EVS mode, but initialize here to fix MSAN complaint */
     228             : 
     229        1939 :     if ( mode == IVAS_DEC_MODE_EVS )
     230             :     {
     231          64 :         st_ivas->element_mode_init = EVS_MONO;
     232          64 :         st_ivas->ivas_format = MONO_FORMAT;
     233          64 :         hIvasDec->hasDecodedFirstGoodFrame = true; /* Functionality to suppress output for initial lost frames is disabled in EVS operation */
     234             : 
     235          64 :         return IVAS_ERR_OK;
     236             :     }
     237        1875 :     else if ( mode == IVAS_DEC_MODE_IVAS )
     238             :     {
     239        1875 :         st_ivas->element_mode_init = -1;
     240        1875 :         st_ivas->ivas_format = UNDEFINED_FORMAT;
     241        1875 :         st_ivas->renderer_type = RENDERER_DISABLE;
     242        1875 :         st_ivas->ini_frame = 0;
     243        1875 :         st_ivas->ini_active_frame = 0;
     244             : 
     245        1875 :         st_ivas->ism_mode = ISM_MODE_NONE;
     246        1875 :         st_ivas->mc_mode = MC_MODE_NONE;
     247             : 
     248        1875 :         st_ivas->sba_order = 0;
     249        1875 :         st_ivas->sba_planar = 0;
     250             : 
     251        1875 :         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           0 : 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           0 :     splitRendBits->bits_read = 0;
     272           0 :     splitRendBits->bits_written = 0;
     273           0 :     splitRendBits->buf_len = ISAR_MAX_SPLIT_REND_BITS_BUFFER_SIZE_IN_BYTES;
     274           0 :     splitRendBits->codec = ISAR_SPLIT_REND_CODEC_DEFAULT;
     275           0 :     splitRendBits->pose_correction = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
     276           0 :     splitRendBits->codec_frame_size_ms = 0;
     277           0 :     splitRendBits->isar_frame_size_ms = 0;
     278           0 :     splitRendBits->lc3plus_highres = 0;
     279             : 
     280           0 :     ISAR_PRE_REND_GetMultiBinPoseData( hSplitBinConfig, &hSplitBinRend->splitrend.multiBinPoseData, ( hCombinedOrientationData != NULL ) ? hCombinedOrientationData->sr_pose_pred_axis : DEFAULT_AXIS );
     281             : 
     282           0 :     if ( hCombinedOrientationData != NULL )
     283             :     {
     284           0 :         isar_set_split_rend_ht_setup( &hSplitBinRend->splitrend, hCombinedOrientationData->Quaternions, hCombinedOrientationData->Rmat );
     285             :     }
     286             : 
     287           0 :     return IVAS_ERR_OK;
     288             : }
     289             : 
     290             : /*---------------------------------------------------------------------*
     291             :  * init_decoder_config()
     292             :  *
     293             :  * Initialize Decoder Config. handle
     294             :  *---------------------------------------------------------------------*/
     295             : 
     296        1939 : static void init_decoder_config(
     297             :     DECODER_CONFIG_HANDLE hDecoderConfig /* i/o: configuration structure */
     298             : )
     299             : {
     300        1939 :     hDecoderConfig->Opt_AMR_WB = 0;
     301        1939 :     hDecoderConfig->output_Fs = -1;
     302        1939 :     hDecoderConfig->nchan_out = 1;
     303        1939 :     hDecoderConfig->output_config = IVAS_AUDIO_CONFIG_INVALID;
     304        1939 :     hDecoderConfig->Opt_LsCustom = 0;
     305        1939 :     hDecoderConfig->Opt_HRTF_binary = 0;
     306        1939 :     hDecoderConfig->Opt_Headrotation = 0;
     307        1939 :     hDecoderConfig->Opt_RendConfigCustom = 0;
     308        1939 :     hDecoderConfig->room_size = IVAS_ROOM_SIZE_AUTO;
     309        1939 :     hDecoderConfig->orientation_tracking = IVAS_HEAD_ORIENT_TRK_NONE;
     310        1939 :     hDecoderConfig->Opt_non_diegetic_pan = 0;
     311        1939 :     hDecoderConfig->non_diegetic_pan_gain = 0;
     312        1939 :     hDecoderConfig->Opt_tsm = 0;
     313        1939 :     hDecoderConfig->Opt_delay_comp = 0;
     314        1939 :     hDecoderConfig->Opt_ExternalOrientation = 0;
     315        1939 :     hDecoderConfig->Opt_dpid_on = 0;
     316        1939 :     hDecoderConfig->Opt_aeid_on = 0;
     317        1939 :     hDecoderConfig->Opt_ObjEdit_on = 0;
     318             : 
     319        1939 :     return;
     320             : }
     321             : 
     322             : 
     323             : /*---------------------------------------------------------------------*
     324             :  * IVAS_DEC_Close( )
     325             :  *
     326             :  * Deallocate IVAS decoder memory handles
     327             :  *---------------------------------------------------------------------*/
     328             : 
     329        1939 : void IVAS_DEC_Close(
     330             :     IVAS_DEC_HANDLE *phIvasDec /* i/o: pointer to IVAS decoder handle   */
     331             : )
     332             : {
     333             :     /* Free all memory */
     334        1939 :     if ( phIvasDec == NULL || *phIvasDec == NULL )
     335             :     {
     336           0 :         return;
     337             :     }
     338             : 
     339        1939 :     if ( ( *phIvasDec )->hVoIP )
     340             :     {
     341         111 :         ivas_destroy_handle_VoIP( ( *phIvasDec )->hVoIP );
     342         111 :         ( *phIvasDec )->hVoIP = NULL;
     343             :     }
     344             : 
     345        1939 :     if ( ( *phIvasDec )->st_ivas )
     346             :     {
     347             :         /* destroy Split binaural renderer (ISAR) handle */
     348        1939 :         ivas_destroy_handle_isar( &( *phIvasDec )->st_ivas->hSplitBinRend );
     349             : 
     350             :         /* destroy IVAS decoder handles */
     351        1939 :         ivas_destroy_dec( ( *phIvasDec )->st_ivas );
     352        1939 :         ( *phIvasDec )->st_ivas = NULL;
     353             :     }
     354             : 
     355        1939 :     apa_exit( &( *phIvasDec )->hTimeScaler );
     356             : 
     357        1939 :     if ( ( *phIvasDec )->flushbuffer != NULL )
     358             :     {
     359         111 :         free( ( *phIvasDec )->flushbuffer );
     360             :     }
     361             : 
     362        1939 :     free( *phIvasDec );
     363        1939 :     *phIvasDec = NULL;
     364        1939 :     phIvasDec = NULL;
     365             : 
     366        1939 :     return;
     367             : }
     368             : 
     369             : 
     370             : /*---------------------------------------------------------------------*
     371             :  * mapIvasFormat( )
     372             :  *
     373             :  *
     374             :  *---------------------------------------------------------------------*/
     375             : 
     376         150 : static IVAS_DEC_BS_FORMAT mapIvasFormat(
     377             :     const IVAS_FORMAT ivas_format )
     378             : {
     379         150 :     switch ( ivas_format )
     380             :     {
     381           0 :         case MONO_FORMAT:
     382           0 :             return IVAS_DEC_BS_MONO;
     383           3 :         case STEREO_FORMAT:
     384           3 :             return IVAS_DEC_BS_STEREO;
     385          45 :         case ISM_FORMAT:
     386          45 :             return IVAS_DEC_BS_OBJ;
     387          12 :         case MC_FORMAT:
     388          12 :             return IVAS_DEC_BS_MC;
     389          18 :         case SBA_FORMAT:
     390          18 :             return IVAS_DEC_BS_SBA;
     391           9 :         case SBA_ISM_FORMAT:
     392           9 :             return IVAS_DEC_BS_SBA_ISM;
     393          51 :         case MASA_FORMAT:
     394          51 :             return IVAS_DEC_BS_MASA;
     395          12 :         case MASA_ISM_FORMAT:
     396          12 :             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         111 : static ivas_error create_flush_buffer(
     412             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle            */
     413             : )
     414             : {
     415         111 :     hIvasDec->flushbuffer = (void *) malloc( CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( int16_t ) );
     416         111 :     if ( hIvasDec->flushbuffer == NULL )
     417             :     {
     418           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate JBM flush buffer" );
     419             :     }
     420             : 
     421         111 :     hIvasDec->pcmType = IVAS_DEC_PCM_INT16;
     422         111 :     set_s( (int16_t *) hIvasDec->flushbuffer, 0, CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
     423             : 
     424         111 :     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        1939 : 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        1939 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     460             :     {
     461           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     462             :     }
     463             : 
     464        1939 :     if ( sampleRate != 8000 && sampleRate != 16000 && sampleRate != 32000 && sampleRate != 48000 )
     465             :     {
     466           0 :         return IVAS_ERR_WRONG_PARAMS;
     467             :     }
     468             : 
     469        1939 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS &&
     470          64 :          ( outputConfig == IVAS_AUDIO_CONFIG_INVALID ||
     471          64 :            outputConfig == IVAS_AUDIO_CONFIG_ISM1 ||
     472          64 :            outputConfig == IVAS_AUDIO_CONFIG_ISM2 ||
     473          64 :            outputConfig == IVAS_AUDIO_CONFIG_ISM3 ||
     474          64 :            outputConfig == IVAS_AUDIO_CONFIG_ISM4 ||
     475          64 :            outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
     476          64 :            outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
     477          64 :            outputConfig == IVAS_AUDIO_CONFIG_MASA1 ||
     478             :            outputConfig == IVAS_AUDIO_CONFIG_MASA2 ) )
     479             :     {
     480           0 :         return IVAS_ERR_WRONG_MODE;
     481             :     }
     482             : 
     483        1939 :     st_ivas = hIvasDec->st_ivas;
     484             : 
     485        1939 :     hDecoderConfig = st_ivas->hDecoderConfig;
     486             : 
     487        1939 :     hDecoderConfig->output_config = outputConfig;
     488        1939 :     if ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_INVALID )
     489             :     {
     490           0 :         return IVAS_ERR_WRONG_PARAMS;
     491             :     }
     492             : 
     493        1939 :     hDecoderConfig->output_Fs = sampleRate;
     494             : 
     495        1939 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     496             :     {
     497          64 :         st_ivas->element_mode_init = EVS_MONO;
     498          64 :         hDecoderConfig->nchan_out = 1;
     499             :     }
     500             : 
     501        1939 :     if ( outputConfig != IVAS_AUDIO_CONFIG_EXTERNAL && outputConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
     502             :     {
     503        1780 :         hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
     504             :     }
     505             : 
     506        1939 :     hDecoderConfig->Opt_LsCustom = (int16_t) customLsOutputEnabled;
     507        1939 :     hDecoderConfig->Opt_Headrotation = (int16_t) enableHeadRotation;
     508        1939 :     hDecoderConfig->orientation_tracking = orientation_tracking;
     509        1939 :     hDecoderConfig->Opt_HRTF_binary = (int16_t) hrtfReaderEnabled;
     510        1939 :     hDecoderConfig->Opt_RendConfigCustom = (int16_t) renderConfigEnabled;
     511        1939 :     hDecoderConfig->room_size = roomSize;
     512        1939 :     hDecoderConfig->Opt_non_diegetic_pan = (int16_t) non_diegetic_pan_enabled;
     513        1939 :     hDecoderConfig->non_diegetic_pan_gain = non_diegetic_pan_gain;
     514        1939 :     hDecoderConfig->Opt_delay_comp = (int16_t) delayCompensationEnabled;
     515        1939 :     hDecoderConfig->Opt_ExternalOrientation = enableExternalOrientation;
     516        1939 :     hDecoderConfig->Opt_dpid_on = (int16_t) dpidEnabled;
     517        1939 :     hDecoderConfig->Opt_aeid_on = acousticEnvironmentId != 65535 ? TRUE : FALSE;
     518        1939 :     hDecoderConfig->Opt_ObjEdit_on = (int16_t) objEditEnabled;
     519             : 
     520        1939 :     if ( outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     521             :     {
     522           0 :         hDecoderConfig->Opt_Headrotation = 1;
     523             :     }
     524             : 
     525        1939 :     if ( render_num_subframes == IVAS_RENDER_NUM_SUBFR_UNKNOWN )
     526             :     {
     527           0 :         return IVAS_ERR_WRONG_PARAMS;
     528             :     }
     529             : 
     530        1939 :     if ( outputConfig == IVAS_AUDIO_CONFIG_EXTERNAL )
     531             :     {
     532         150 :         hDecoderConfig->render_num_subframes = IVAS_RENDER_NUM_SUBFR_20MS;
     533             :     }
     534             :     else
     535             :     {
     536        1789 :         hDecoderConfig->render_num_subframes = render_num_subframes;
     537             :     }
     538             : 
     539             :     /* Set decoder parameters to initial values */
     540        1939 :     if ( ( error = ivas_init_decoder_front( st_ivas ) ) != IVAS_ERR_OK )
     541             :     {
     542           0 :         return error;
     543             :     }
     544             : 
     545             :     /* create ISAR handle */
     546        1939 :     if ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     547             :     {
     548           0 :         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        1939 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     555             :     {
     556          64 :         hIvasDec->st_ivas->ivas_format = MONO_FORMAT;
     557             :     }
     558             : 
     559        1939 :     hIvasDec->nSamplesFrame = (uint16_t) ( hDecoderConfig->output_Fs / FRAMES_PER_SEC );
     560             : 
     561        1939 :     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           0 : ivas_error IVAS_DEC_EnableSplitRendering(
     572             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle    */
     573             : )
     574             : {
     575             :     DECODER_CONFIG_HANDLE hDecoderConfig;
     576             : 
     577           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     578             :     {
     579           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     580             :     }
     581             : 
     582           0 :     hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
     583             : 
     584           0 :     hDecoderConfig->Opt_Headrotation = 1;
     585           0 :     hDecoderConfig->render_num_subframes = IVAS_RENDER_NUM_SUBFR_20MS;
     586             : 
     587           0 :     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         744 : static int16_t get_render_frame_size_ms(
     599             :     const IVAS_RENDER_NUM_SUBFR render_num_subframes )
     600             : {
     601         744 :     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           0 : 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           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hDecoderConfig == NULL )
     617             :     {
     618           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     619             :     }
     620             : 
     621           0 :     hIvasDec->st_ivas->hDecoderConfig->render_num_subframes = render_num_subframes;
     622             : 
     623           0 :     if ( hIvasDec->st_ivas->hExtOrientationData != NULL )
     624             :     {
     625           0 :         hIvasDec->st_ivas->hExtOrientationData->num_subframes = (int16_t) render_num_subframes;
     626             :     }
     627             : 
     628           0 :     if ( hIvasDec->st_ivas->hCombinedOrientationData != NULL )
     629             :     {
     630           0 :         hIvasDec->st_ivas->hCombinedOrientationData->num_subframes = (int16_t) render_num_subframes;
     631             :     }
     632             : 
     633           0 :     return IVAS_ERR_OK;
     634             : }
     635             : 
     636             : 
     637             : /*---------------------------------------------------------------------*
     638             :  * IVAS_DEC_GetRenderNumSubfr( )
     639             :  *
     640             :  * Get number of rendering subframes
     641             :  *---------------------------------------------------------------------*/
     642             : 
     643    33667414 : 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    33667414 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_num_subframes == NULL )
     649             :     {
     650           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     651             :     }
     652             : 
     653    33667414 :     *render_num_subframes = hIvasDec->st_ivas->hDecoderConfig->render_num_subframes;
     654             : 
     655    33667414 :     return IVAS_ERR_OK;
     656             : }
     657             : 
     658             : 
     659             : /*---------------------------------------------------------------------*
     660             :  * get_render_frame_size_samples( )
     661             :  *
     662             :  *
     663             :  *---------------------------------------------------------------------*/
     664             : 
     665        1939 : static int16_t get_render_frame_size_samples(
     666             :     const DECODER_CONFIG_HANDLE hDecoderConfig /* i  : configuration structure */
     667             : )
     668             : {
     669        1939 :     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        1939 : 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        1939 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
     685             :     {
     686           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     687             :     }
     688             : 
     689        1939 :     *render_framesize = get_render_frame_size_samples( hIvasDec->st_ivas->hDecoderConfig );
     690             : 
     691        1939 :     return IVAS_ERR_OK;
     692             : }
     693             : 
     694             : 
     695             : /*---------------------------------------------------------------------*
     696             :  * IVAS_DEC_GetRenderFramesizeMs( )
     697             :  *
     698             :  * Get render framesize in milliseconds
     699             :  *---------------------------------------------------------------------*/
     700             : 
     701         111 : 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         111 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize_ms == NULL )
     707             :     {
     708           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     709             :     }
     710             : 
     711         111 :     *render_framesize_ms = get_render_frame_size_ms( hIvasDec->st_ivas->hDecoderConfig->render_num_subframes );
     712             : 
     713         111 :     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        1939 : 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        1939 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || update_frequency == NULL )
     729             :     {
     730           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     731             :     }
     732             : 
     733        1939 :     *update_frequency = (int16_t) ( IVAS_MAX_PARAM_SPATIAL_SUBFRAMES / hIvasDec->st_ivas->hDecoderConfig->render_num_subframes );
     734             : 
     735        1939 :     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         111 : 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         111 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     757             :     {
     758           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     759             :     }
     760             : 
     761         111 :     hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
     762             : 
     763             : #ifdef DEBUGGING
     764             :     hIvasDec->Opt_VOIP = 1;
     765             : #endif
     766         111 :     hDecoderConfig->Opt_tsm = 1;
     767             : 
     768         111 :     if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
     769             :     {
     770          99 :         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         111 :     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         111 :     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         111 :     hIvasDec->hVoIP->lastDecodedWasActive = 0;
     790         111 :     hIvasDec->hVoIP->hCurrentDataUnit = NULL;
     791         111 :     hIvasDec->hVoIP->nSamplesRendered20ms = 0;
     792             : 
     793             : #define WMC_TOOL_SKIP
     794             :     /* Bitstream conversion is not counted towards complexity and memory usage */
     795         111 :     hIvasDec->hVoIP->bs_conversion_buf = malloc( sizeof( uint16_t ) * ( MAX_BITS_PER_FRAME + 4 * 8 ) );
     796             : #undef WMC_TOOL_SKIP
     797             : 
     798         111 :     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         111 :     if ( ( error = JB4_Create( &hIvasDec->hVoIP->hJBM ) != IVAS_ERR_OK ) != IVAS_ERR_OK )
     805             :     {
     806           0 :         return error;
     807             :     }
     808             : 
     809         111 :     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         111 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     816             :     {
     817           3 :         JB4_TMP_SetEvsCompatFlag( hIvasDec->hVoIP->hJBM );
     818             :     }
     819             : #endif
     820             : 
     821             :     /* init flush buffer (needed for binaural outputs) */
     822         111 :     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         111 :     return IVAS_ERR_OK;
     829             : }
     830             : 
     831             : 
     832             : /*---------------------------------------------------------------------*
     833             :  * IVAS_DEC_FeedFrame_Serial( )
     834             :  *
     835             :  *
     836             :  *---------------------------------------------------------------------*/
     837             : 
     838    15136709 : 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    15136709 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     848             :     {
     849           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     850             :     }
     851             : 
     852    15136709 :     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        2179 :         if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     857             :         {
     858          64 :             hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = ACELP_8k00;
     859          64 :             hIvasDec->st_ivas->transport_config = IVAS_AUDIO_CONFIG_MONO;
     860             : 
     861          64 :             if ( ( error = ivas_init_decoder( hIvasDec->st_ivas ) ) != IVAS_ERR_OK )
     862             :             {
     863           0 :                 return error;
     864             :             }
     865             : 
     866          64 :             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          64 :             hIvasDec->isInitialized = true;
     875             :         }
     876             :     }
     877             : 
     878    15136709 :     if ( !bfi ) /* TODO(mcjbm): Is this ok for bfi == 2 (partial frame)? Is there enough info to fully configure decoder? */
     879             :     {
     880    14394108 :         hIvasDec->hasBeenFedFirstGoodFrame = true;
     881             :     }
     882             : 
     883             :     /* Update redundant frame information in EVS (pre- read indices) */
     884    15136709 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS && hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
     885             :     {
     886        3268 :         DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
     887        3268 :         st->bit_stream = serial;
     888             : 
     889        3268 :         if ( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame || st->prev_use_partial_copy )
     890             :         {
     891           0 :             st->next_coder_type = hIvasDec->hVoIP->hCurrentDataUnit->nextCoderType;
     892             :         }
     893             :         else
     894             :         {
     895        3268 :             st->next_coder_type = INACTIVE;
     896             :         }
     897             : 
     898        3268 :         if ( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame == 1 && bfi != 1 )
     899             :         {
     900           0 :             bfi = 2;
     901             :         }
     902             :     }
     903             : 
     904    15136709 :     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    15136709 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS &&
     911      149120 :          hIvasDec->hVoIP != NULL &&
     912        3292 :          hIvasDec->hVoIP->hCurrentDataUnit != NULL &&
     913        3268 :          hIvasDec->hVoIP->hCurrentDataUnit->partial_frame != 0 )
     914             :     {
     915           0 :         DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
     916           0 :         st->codec_mode = MODE2;
     917           0 :         st->use_partial_copy = 1;
     918             :     }
     919             : 
     920    15136709 :     hIvasDec->needNewFrame = false;
     921    15136709 :     hIvasDec->hasBeenFedFrame = true;
     922    15136709 :     hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
     923             : 
     924    15136709 :     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    28499556 : 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    28499556 :     switch ( renderer_type )
     942             :     {
     943      928623 :         case RENDERER_BINAURAL_OBJECTS_TD:
     944      928623 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_TDREND;
     945      928623 :             break;
     946      805311 :         case RENDERER_BINAURAL_MIXER_CONV:
     947             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
     948      805311 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_CREND;
     949      805311 :             break;
     950      757104 :         case RENDERER_BINAURAL_FASTCONV:
     951      757104 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
     952      757104 :             break;
     953      327816 :         case RENDERER_BINAURAL_FASTCONV_ROOM:
     954      327816 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
     955      327816 :             break;
     956     2351178 :         case RENDERER_BINAURAL_PARAMETRIC:
     957             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
     958     2351178 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_PARAMBIN;
     959     2351178 :             break;
     960    23329524 :         default:
     961    23329524 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_NONE;
     962    23329524 :             break;
     963             :     }
     964             : 
     965    28499556 :     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    15081994 : 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    15081994 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     994             :     {
     995           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     996             :     }
     997             : 
     998    15081994 :     st_ivas = hIvasDec->st_ivas;
     999    15081994 :     ism_mode_old = st_ivas->ism_mode;
    1000    15081994 :     mc_mode_old = st_ivas->mc_mode;
    1001    15081994 :     nchan_transport_old = st_ivas->nchan_transport;
    1002    15081994 :     intern_config_old = st_ivas->intern_config;
    1003    15081994 :     transport_config_old = st_ivas->transport_config;
    1004    15081994 :     renderer_type_old = st_ivas->renderer_type;
    1005    15081994 :     renderer_type_sec_old = ivas_renderer_secondary_select( st_ivas );
    1006             : 
    1007    15081994 :     output_config = st_ivas->hDecoderConfig->output_config;
    1008             : 
    1009    15081994 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    1010             :     {
    1011      148909 :         return IVAS_ERR_OK;
    1012             :     }
    1013             : 
    1014    14933085 :     if ( st_ivas->bfi == 0 )
    1015             :     {
    1016    14249778 :         if ( ( error = ivas_dec_get_format( st_ivas ) ) != IVAS_ERR_OK )
    1017             :         {
    1018           0 :             return error;
    1019             :         }
    1020             : 
    1021    14249778 :         if ( st_ivas->restartNeeded == 1 )
    1022             :         {
    1023           0 :             return IVAS_ERR_OK;
    1024             :         }
    1025             : 
    1026             :         /* Select binaural renderer */
    1027    14249778 :         ivas_renderer_select( st_ivas );
    1028    14249778 :         *binaural_renderer = renderer_type_to_mode( st_ivas->renderer_type );
    1029             : 
    1030             :         /* Select secondary binaural renderer (used in combined formats) */
    1031    14249778 :         renderer_type_sec_new = ivas_renderer_secondary_select( st_ivas );
    1032    14249778 :         *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    14249778 :         *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_INVALID;
    1036    14249778 :         if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV )
    1037             :         {
    1038     1084920 :             if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1039             :             {
    1040             :                 /* SHD HRIRs */
    1041      764811 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
    1042             : 
    1043      764811 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    1044             :                 {
    1045             :                     /* BRIRs */
    1046      264537 :                     *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1047             :                 }
    1048             :             }
    1049      320109 :             else if ( st_ivas->ivas_format == MC_FORMAT )
    1050             :             {
    1051             :                 /* HRIRs */
    1052      320109 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1053             : 
    1054      320109 :                 if ( ( st_ivas->hDecoderConfig->Opt_Headrotation ) &&
    1055      213288 :                      !( st_ivas->mc_mode == MC_MODE_PARAMUPMIX && output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) )
    1056             :                 {
    1057             :                     /* SHD HRIRs for low complexity rotation */
    1058      213288 :                     *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
    1059             :                 }
    1060             :             }
    1061             :         }
    1062    13164858 :         else if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND )
    1063             :         {
    1064      805311 :             if ( st_ivas->ivas_format == ISM_FORMAT )
    1065             :             {
    1066             :                 /* BRIRs */
    1067      186270 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1068             :             }
    1069      619041 :             else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT )
    1070             :             {
    1071             :                 /* BRIRs */
    1072      619041 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1073             : 
    1074      619041 :                 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       31191 :                     *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    14249778 :         if ( st_ivas->ini_active_frame > 0 && st_ivas->hDecoderConfig->Opt_tsm &&
    1084      755739 :              ( ( renderer_type_old != st_ivas->renderer_type ) ||
    1085             :                ( renderer_type_sec_old != renderer_type_sec_new ) ) )
    1086             :         {
    1087       18639 :             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       18639 :             st_ivas->nchan_transport = nchan_transport_old;
    1090             : 
    1091       18639 :             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       18639 :             if ( tc_granularity_new < st_ivas->hTcBuffer->n_samples_granularity )
    1098             :             {
    1099        6363 :                 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    14933085 :     st_ivas->ism_mode = ism_mode_old;
    1108    14933085 :     st_ivas->mc_mode = mc_mode_old;
    1109    14933085 :     st_ivas->nchan_transport = nchan_transport_old;
    1110    14933085 :     st_ivas->intern_config = intern_config_old;
    1111    14933085 :     st_ivas->transport_config = transport_config_old;
    1112    14933085 :     st_ivas->renderer_type = renderer_type_old;
    1113             : 
    1114    14933085 :     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    15136685 : 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    15136685 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1137             :     {
    1138           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1139             :     }
    1140             : 
    1141    15136685 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame && !hIvasDec->isInitialized ) /* note: 'isInitialized' is related to EVS decoder */
    1142             :     {
    1143         240 :         return IVAS_ERR_OK;
    1144             :     }
    1145             : 
    1146    15136445 :     st_ivas = hIvasDec->st_ivas;
    1147             : 
    1148    15136445 :     isInitialized_voip = hIvasDec->hTimeScaler != NULL;
    1149             : 
    1150    15136445 :     if ( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame ) /* wait for the first good frame */
    1151             :     {
    1152             :         /*-----------------------------------------------------------------*
    1153             :          * Setup all decoder parts (IVAS decoder, ISAR)
    1154             :          *-----------------------------------------------------------------*/
    1155             : 
    1156    15136445 :         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    15136445 :         if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    1166             :         {
    1167      149096 :             if ( ( error = evs_dec_main( st_ivas ) ) != IVAS_ERR_OK )
    1168             :             {
    1169           0 :                 return error;
    1170             :             }
    1171             :         }
    1172    14987349 :         else if ( hIvasDec->mode == IVAS_DEC_MODE_IVAS )
    1173             :         {
    1174    14987349 :             if ( ( error = ivas_dec( st_ivas ) ) != IVAS_ERR_OK )
    1175             :             {
    1176           0 :                 return error;
    1177             :             }
    1178             : 
    1179    14987349 :             hIvasDec->isInitialized = true; /* Initialization done in ivas_dec() */
    1180             :         }
    1181             : 
    1182    15136445 :         if ( hIvasDec->hasBeenFedFirstGoodFrame )
    1183             :         {
    1184    15136430 :             hIvasDec->hasDecodedFirstGoodFrame = true;
    1185             :         }
    1186             : 
    1187             :         /*-----------------------------------------------------------------*
    1188             :          * JBM
    1189             :          *-----------------------------------------------------------------*/
    1190             : 
    1191    15136445 :         if ( st_ivas->hDecoderConfig->Opt_tsm )
    1192             :         {
    1193      813619 :             if ( nTransportChannels != hIvasDec->nTransportChannelsOld )
    1194             :             {
    1195       62808 :                 if ( ( error = apa_setup( hIvasDec, isInitialized_voip, nTransportChannels ) ) != IVAS_ERR_OK )
    1196             :                 {
    1197           0 :                     return error;
    1198             :                 }
    1199             :             }
    1200             : 
    1201      813619 :             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      813619 :             ivas_buffer_deinterleaved_to_interleaved( st_ivas->p_output_f, st_ivas->hTcBuffer->tc_buffer, nTransportChannels, hIvasDec->nSamplesFrame );
    1208             : 
    1209             :             /* time-scale modification */
    1210      813619 :             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      813619 :             assert( nTimeScalerOutSamples <= APA_BUF );
    1216      813619 :             nSamplesTcsScaled = nTimeScalerOutSamples / nTransportChannels;
    1217      813619 :             hIvasDec->timeScalingDone = 1;
    1218             : 
    1219             :             /* convert interleaved time-scaled TC audio channels buffer to deinterleaved one */
    1220      813619 :             ivas_buffer_interleaved_to_deinterleaved( st_ivas->hTcBuffer->tc_buffer, st_ivas->p_output_f, nTransportChannels, nSamplesTcsScaled );
    1221             :         }
    1222             :         else
    1223             :         {
    1224    14322826 :             nSamplesTcsScaled = hIvasDec->nSamplesFrame;
    1225             :         }
    1226             : 
    1227             :         /*-----------------------------------------------------------------*
    1228             :          * Feed decoded transport channels samples to the renderer
    1229             :          *-----------------------------------------------------------------*/
    1230             : 
    1231    15136445 :         ivas_dec_feed_tc_to_renderer( st_ivas, nSamplesTcsScaled, &nResidualSamples );
    1232             : 
    1233    15136445 :         if ( st_ivas->hDecoderConfig->Opt_tsm )
    1234             :         {
    1235             :             /* feed residual samples to TSM for the next call */
    1236      813619 :             if ( apa_set_renderer_residual_samples( hIvasDec->hTimeScaler, (uint16_t) nResidualSamples ) != 0 )
    1237             :             {
    1238           0 :                 return IVAS_ERR_UNKNOWN;
    1239             :             }
    1240             :         }
    1241             : 
    1242    15136445 :         hIvasDec->hasBeenFedFrame = false;
    1243             :     }
    1244             : 
    1245    15136445 :     hIvasDec->hasBeenPreparedRendering = false;
    1246             : 
    1247             :     /*-----------------------------------------------------------------*
    1248             :      * Set editable metadata
    1249             :      *-----------------------------------------------------------------*/
    1250             : 
    1251    15136445 :     if ( st_ivas->hIsmMetaData[0] )
    1252             :     {
    1253     3079980 :         if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1254             :         {
    1255     3000600 :             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     2447541 :                 ISM_METADATA_HANDLE *hIsmMetaData = st_ivas->hIsmMetaData;
    1259     9628620 :                 for ( obj = 0; obj < st_ivas->nchan_ism; obj++ )
    1260             :                 {
    1261     7181079 :                     hIsmMetaData[obj]->edited_azimuth = hIsmMetaData[obj]->azimuth;
    1262     7181079 :                     hIsmMetaData[obj]->edited_elevation = hIsmMetaData[obj]->elevation;
    1263     7181079 :                     hIsmMetaData[obj]->edited_yaw = hIsmMetaData[obj]->yaw;
    1264     7181079 :                     hIsmMetaData[obj]->edited_pitch = hIsmMetaData[obj]->pitch;
    1265     7181079 :                     hIsmMetaData[obj]->edited_radius = hIsmMetaData[obj]->radius;
    1266     7181079 :                     hIsmMetaData[obj]->edited_gain = 1.0f;
    1267             :                 }
    1268             : 
    1269     2447541 :                 if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1270             :                 {
    1271      536226 :                     st_ivas->hSbaIsmData->gain_bed = 1.0f;
    1272             :                 }
    1273             :             }
    1274             :         }
    1275             :     }
    1276             : 
    1277    15136445 :     if ( st_ivas->hParamIsmDec != NULL )
    1278             :     {
    1279      368349 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    1280             :         {
    1281      368349 :             int16_t obj = 0;
    1282      368349 :             PARAM_ISM_DEC_HANDLE hParamIsmDec = st_ivas->hParamIsmDec;
    1283     1725816 :             for ( obj = 0; obj < st_ivas->nchan_ism; obj++ )
    1284             :             {
    1285     1357467 :                 hParamIsmDec->edited_azimuth_values[obj] = hParamIsmDec->azimuth_values[obj];
    1286     1357467 :                 hParamIsmDec->edited_elevation_values[obj] = hParamIsmDec->elevation_values[obj];
    1287             :             }
    1288             :         }
    1289             :     }
    1290             : 
    1291    15136445 :     return IVAS_ERR_OK;
    1292             : }
    1293             : 
    1294             : 
    1295             : /*---------------------------------------------------------------------*
    1296             :  * IVAS_DEC_GetEditableParameters( )
    1297             :  *
    1298             :  * Get editable metadata parameters
    1299             :  *---------------------------------------------------------------------*/
    1300             : 
    1301      299016 : 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      299016 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasEditableParameters == NULL )
    1311             :     {
    1312           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1313             :     }
    1314             : 
    1315      299016 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    1316             :     {
    1317           3 :         hIvasEditableParameters->num_obj = 0;
    1318             : 
    1319           3 :         return IVAS_ERR_OK;
    1320             :     }
    1321             : 
    1322      299013 :     st_ivas = hIvasDec->st_ivas;
    1323      299013 :     ism_mode = st_ivas->ism_mode;
    1324             : 
    1325      299013 :     if ( !( st_ivas->ivas_format == ISM_FORMAT ||
    1326      143838 :             st_ivas->ivas_format == SBA_ISM_FORMAT ||
    1327       72039 :             st_ivas->ivas_format == MASA_ISM_FORMAT ||
    1328        6000 :             ( 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      299013 :     hIvasEditableParameters->gain_bed = 1.0f;
    1334      299013 :     hIvasEditableParameters->num_obj = st_ivas->nchan_ism;
    1335      299013 :     if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1336             :     {
    1337      226974 :         if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
    1338             :         {
    1339      620847 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1340             :             {
    1341      484560 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hIsmMetaData[obj]->azimuth;
    1342      484560 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hIsmMetaData[obj]->elevation;
    1343      484560 :                 hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
    1344      484560 :                 hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
    1345      484560 :                 hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
    1346      484560 :                 hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
    1347      484560 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
    1348             :             }
    1349             : 
    1350      136287 :             if ( ism_mode == ISM_SBA_MODE_DISC )
    1351             :             {
    1352       58749 :                 hIvasEditableParameters->gain_bed = 1.0f;
    1353             :             }
    1354             :         }
    1355       90687 :         else if ( ism_mode == ISM_MODE_PARAM )
    1356             :         {
    1357      362418 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1358             :             {
    1359      284781 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hParamIsmDec->azimuth_values[obj];
    1360      284781 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hParamIsmDec->elevation_values[obj];
    1361      284781 :                 hIvasEditableParameters->ism_metadata[obj].yaw = 0.0f;
    1362      284781 :                 hIvasEditableParameters->ism_metadata[obj].pitch = 0.0f;
    1363      284781 :                 hIvasEditableParameters->ism_metadata[obj].radius = 0.0f;
    1364      284781 :                 hIvasEditableParameters->ism_metadata[obj].gain = 1.0f;
    1365      284781 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = 0;
    1366             :             }
    1367             :         }
    1368       13050 :         else if ( ism_mode == ISM_MODE_NONE )
    1369             :         {
    1370       13050 :             hIvasEditableParameters->num_obj = 0;
    1371             :         }
    1372             : #ifdef DEBUGGING
    1373             :         else
    1374             :         {
    1375             :             assert( 0 && "This should never happen!" );
    1376             :         }
    1377             : #endif
    1378             :     }
    1379       72039 :     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       72039 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1383             :         {
    1384      162159 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1385             :             {
    1386      120120 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hIsmMetaData[obj]->azimuth;
    1387      120120 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hIsmMetaData[obj]->elevation;
    1388      120120 :                 hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
    1389      120120 :                 hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
    1390      120120 :                 hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
    1391             : 
    1392             :                 /* reset the otherwise unused "gain" field for the object */
    1393      120120 :                 st_ivas->hIsmMetaData[obj]->edited_gain = 1.0f;
    1394      120120 :                 hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
    1395      120120 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
    1396             :             }
    1397             :         }
    1398       30000 :         else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1399             :         {
    1400             :             /* Handle MONO output */
    1401       24000 :             if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
    1402             :             {
    1403       24000 :                 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      108000 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1411             :             {
    1412       84000 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hMasaIsmData->azimuth_ism[obj][dirac_read_idx];
    1413       84000 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx];
    1414             : 
    1415       84000 :                 hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
    1416       84000 :                 hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
    1417       84000 :                 hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
    1418             : 
    1419             :                 /* reset the otherwise unused "gain" field for the object */
    1420       84000 :                 st_ivas->hIsmMetaData[obj]->edited_gain = 1.0f;
    1421       84000 :                 hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
    1422       84000 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
    1423             :             }
    1424             :         }
    1425        6000 :         else if ( ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || ism_mode == ISM_MODE_NONE )
    1426             :         {
    1427        6000 :             hIvasEditableParameters->num_obj = 0;
    1428             :         }
    1429             : #ifdef DEBUGGING
    1430             :         else
    1431             :         {
    1432             :             assert( 0 && "This should never happen!" );
    1433             :         }
    1434             : #endif
    1435             :     }
    1436             : 
    1437      299013 :     return IVAS_ERR_OK;
    1438             : }
    1439             : 
    1440             : 
    1441             : /*---------------------------------------------------------------------*
    1442             :  * IVAS_DEC_SetEditableParameters( )
    1443             :  *
    1444             :  * Set editable metadata parameters
    1445             :  *---------------------------------------------------------------------*/
    1446             : 
    1447      299016 : 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      299016 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1457             :     {
    1458           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1459             :     }
    1460             : 
    1461      299016 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    1462             :     {
    1463           3 :         return IVAS_ERR_OK;
    1464             :     }
    1465             : 
    1466      299013 :     st_ivas = hIvasDec->st_ivas;
    1467      299013 :     ism_mode = st_ivas->ism_mode;
    1468             : 
    1469      299013 :     if ( !( st_ivas->ivas_format == ISM_FORMAT ||
    1470      143838 :             st_ivas->ivas_format == SBA_ISM_FORMAT ||
    1471       72039 :             st_ivas->ivas_format == MASA_ISM_FORMAT ||
    1472        6000 :             ( 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      299013 :     if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1478             :     {
    1479      226974 :         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      620847 :             for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
    1485             :             {
    1486      484560 :                 st_ivas->hIsmMetaData[obj]->edited_azimuth = hIvasEditableParameters.ism_metadata[obj].azimuth;
    1487      484560 :                 st_ivas->hIsmMetaData[obj]->edited_elevation = hIvasEditableParameters.ism_metadata[obj].elevation;
    1488      484560 :                 st_ivas->hIsmMetaData[obj]->edited_radius = hIvasEditableParameters.ism_metadata[obj].radius;
    1489      484560 :                 st_ivas->hIsmMetaData[obj]->edited_yaw = hIvasEditableParameters.ism_metadata[obj].yaw;
    1490      484560 :                 st_ivas->hIsmMetaData[obj]->edited_pitch = hIvasEditableParameters.ism_metadata[obj].pitch;
    1491      484560 :                 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      484560 :                     st_ivas->hIsmMetaData[obj]->edited_gain = hIvasEditableParameters.ism_metadata[obj].gain;
    1498             :                 }
    1499      484560 :                 st_ivas->hIsmMetaData[obj]->non_diegetic_flag = hIvasEditableParameters.ism_metadata[obj].non_diegetic_flag;
    1500             :             }
    1501             : 
    1502      136287 :             if ( ism_mode == ISM_SBA_MODE_DISC )
    1503             :             {
    1504       58749 :                 if ( hIvasEditableParameters.gain_bed > EDIT_GAIN_MAX )
    1505             :                 {
    1506           0 :                     st_ivas->hSbaIsmData->gain_bed = EDIT_GAIN_MAX;
    1507             :                 }
    1508             :                 else
    1509             :                 {
    1510       58749 :                     st_ivas->hSbaIsmData->gain_bed = hIvasEditableParameters.gain_bed;
    1511             :                 }
    1512             :             }
    1513             :         }
    1514       90687 :         else if ( ism_mode == ISM_MODE_PARAM )
    1515             :         {
    1516             : #ifdef DEBUGGING
    1517             :             assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
    1518             : #endif
    1519      362418 :             for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
    1520             :             {
    1521      284781 :                 st_ivas->hParamIsmDec->edited_azimuth_values[obj] = hIvasEditableParameters.ism_metadata[obj].azimuth;
    1522      284781 :                 st_ivas->hParamIsmDec->edited_elevation_values[obj] = hIvasEditableParameters.ism_metadata[obj].elevation;
    1523             : 
    1524      284781 :                 if ( st_ivas->hMasaIsmData != NULL )
    1525             :                 {
    1526             :                     /* Limit gain edit to a range of +12dB to -24dB with parametric ISM mode */
    1527      207480 :                     if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1528             :                     {
    1529           0 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MAX;
    1530             :                     }
    1531      207480 :                     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      207480 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = hIvasEditableParameters.ism_metadata[obj].gain;
    1538             :                     }
    1539             : 
    1540             :                     /* Detect direction editing in Param-ISM mode */
    1541      207480 :                     if ( fabsf( st_ivas->hParamIsmDec->azimuth_values[obj] - hIvasEditableParameters.ism_metadata[obj].azimuth ) > OMASA_AZI_EDIT_THR ||
    1542        1152 :                          fabsf( st_ivas->hParamIsmDec->elevation_values[obj] - hIvasEditableParameters.ism_metadata[obj].elevation ) > OMASA_ELE_EDIT_THR )
    1543             :                     {
    1544      207366 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
    1545             :                     }
    1546             :                     else
    1547             :                     {
    1548         114 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
    1549             :                     }
    1550             : 
    1551             :                     /* Detect gain editing in Param-ISM mode */
    1552      207480 :                     if ( fabsf( 1.0f - hIvasEditableParameters.ism_metadata[obj].gain ) > OMASA_GAIN_EDIT_THR )
    1553             :                     {
    1554      196794 :                         st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 1u;
    1555             :                     }
    1556             :                     else
    1557             :                     {
    1558       10686 :                         st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 0u;
    1559             :                     }
    1560             :                 }
    1561             :             }
    1562             : 
    1563       77637 :             if ( st_ivas->hMasaIsmData != NULL )
    1564             :             {
    1565             :                 /* MASA is not present with the ISM format */
    1566       51870 :                 st_ivas->hMasaIsmData->masa_gain_is_edited = 0u;
    1567             :             }
    1568             :         }
    1569       13050 :         else if ( ism_mode == ISM_MODE_NONE )
    1570             :         {
    1571       13050 :             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       72039 :     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      270159 :         for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
    1592             :         {
    1593      204120 :             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      204120 :                 if ( hIvasEditableParameters.ism_metadata[obj].azimuth > 0.0f )
    1599             :                 {
    1600      138243 :                     new_azi = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].azimuth + 0.5f );
    1601             :                 }
    1602             :                 else
    1603             :                 {
    1604       65877 :                     new_azi = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].azimuth - 0.5f );
    1605             :                 }
    1606             : 
    1607      204120 :                 if ( hIvasEditableParameters.ism_metadata[obj].elevation > 0.0f )
    1608             :                 {
    1609       67524 :                     new_ele = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].elevation + 0.5f );
    1610             :                 }
    1611             :                 else
    1612             :                 {
    1613      136596 :                     new_ele = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].elevation - 0.5f );
    1614             :                 }
    1615             : 
    1616      204120 :                 if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1617             :                 {
    1618             :                     /* Handle MONO output */
    1619       84000 :                     if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
    1620             :                     {
    1621       84000 :                         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       84000 :                     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       84000 :                     threshold_azi = 360.0f / (float) no_phi_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 1][id_th];
    1635       84000 :                     threshold_ele = delta_theta_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 3];
    1636             : 
    1637       84000 :                     if ( ( (float) abs( new_azi - st_ivas->hMasaIsmData->azimuth_ism[obj][dirac_read_idx] ) > threshold_azi ) ||
    1638        7554 :                          ( (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       80466 :                         st_ivas->hMasaIsmData->azimuth_ism_edited[obj] = new_azi;
    1642       80466 :                         st_ivas->hMasaIsmData->elevation_ism_edited[obj] = new_ele;
    1643             : 
    1644       80466 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
    1645             :                     }
    1646             :                     else
    1647             :                     {
    1648        3534 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
    1649             :                     }
    1650             :                 }
    1651             :                 else
    1652             :                 {
    1653             :                     /* detect editing in ISM_MASA_MODE_DISC mode */
    1654      120120 :                     if ( fabsf( st_ivas->hIsmMetaData[obj]->azimuth - hIvasEditableParameters.ism_metadata[obj].azimuth ) > OMASA_AZI_EDIT_THR ||
    1655         702 :                          fabsf( st_ivas->hIsmMetaData[obj]->elevation - hIvasEditableParameters.ism_metadata[obj].elevation ) > OMASA_ELE_EDIT_THR )
    1656             :                     {
    1657      120102 :                         st_ivas->hMasaIsmData->azimuth_ism_edited[obj] = new_azi;
    1658      120102 :                         st_ivas->hMasaIsmData->elevation_ism_edited[obj] = new_ele;
    1659             : 
    1660      120102 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
    1661             :                     }
    1662             :                     else
    1663             :                     {
    1664          18 :                         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      204120 :                 if ( fabsf( st_ivas->hIsmMetaData[obj]->edited_gain - hIvasEditableParameters.ism_metadata[obj].gain ) > OMASA_GAIN_EDIT_THR )
    1670             :                 {
    1671      188604 :                     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      188604 :                     if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1675             :                     {
    1676           0 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MAX;
    1677             :                     }
    1678      188604 :                     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      188604 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = hIvasEditableParameters.ism_metadata[obj].gain;
    1685             :                     }
    1686             :                 }
    1687             :                 else
    1688             :                 {
    1689       15516 :                     st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 0u;
    1690             :                 }
    1691             :             }
    1692             : 
    1693             :             /* Copy edited values to hIsmMetaData struct */
    1694      204120 :             if ( st_ivas->hIsmMetaData[obj] != NULL )
    1695             :             {
    1696      204120 :                 st_ivas->hIsmMetaData[obj]->edited_azimuth = hIvasEditableParameters.ism_metadata[obj].azimuth;
    1697      204120 :                 st_ivas->hIsmMetaData[obj]->edited_elevation = hIvasEditableParameters.ism_metadata[obj].elevation;
    1698             : 
    1699      204120 :                 if ( ism_mode == ISM_MASA_MODE_DISC )
    1700             :                 {
    1701      120120 :                     st_ivas->hIsmMetaData[obj]->edited_yaw = hIvasEditableParameters.ism_metadata[obj].yaw;
    1702      120120 :                     st_ivas->hIsmMetaData[obj]->edited_pitch = hIvasEditableParameters.ism_metadata[obj].pitch;
    1703      120120 :                     st_ivas->hIsmMetaData[obj]->edited_radius = hIvasEditableParameters.ism_metadata[obj].radius;
    1704             :                 }
    1705             : 
    1706      204120 :                 if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1707             :                 {
    1708           0 :                     st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MAX;
    1709             :                 }
    1710      204120 :                 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      204120 :                     st_ivas->hIsmMetaData[obj]->edited_gain = hIvasEditableParameters.ism_metadata[obj].gain;
    1717             :                 }
    1718             : 
    1719      204120 :                 st_ivas->hIsmMetaData[obj]->non_diegetic_flag = hIvasEditableParameters.ism_metadata[obj].non_diegetic_flag;
    1720             :             }
    1721             :         }
    1722             : 
    1723       66039 :         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       63459 :             if ( hIvasEditableParameters.gain_bed > EDIT_GAIN_MAX )
    1727             :             {
    1728           0 :                 st_ivas->hMasaIsmData->gain_masa_edited = EDIT_GAIN_MAX;
    1729             :             }
    1730       63459 :             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       63459 :                 st_ivas->hMasaIsmData->gain_masa_edited = hIvasEditableParameters.gain_bed;
    1737             :             }
    1738       63459 :             st_ivas->hMasaIsmData->masa_gain_is_edited = 1u;
    1739             :         }
    1740             :         else
    1741             :         {
    1742        2580 :             st_ivas->hMasaIsmData->masa_gain_is_edited = 0u;
    1743             :         }
    1744             :     }
    1745             : 
    1746      299013 :     return IVAS_ERR_OK;
    1747             : }
    1748             : 
    1749             : 
    1750             : /*---------------------------------------------------------------------*
    1751             :  * IVAS_DEC_PrepareRenderer( )
    1752             :  *
    1753             :  * prepare IVAS renderer
    1754             :  *---------------------------------------------------------------------*/
    1755             : 
    1756    15136685 : ivas_error IVAS_DEC_PrepareRenderer(
    1757             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle    */
    1758             : )
    1759             : {
    1760    15136685 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1761             :     {
    1762           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1763             :     }
    1764             : 
    1765    15136685 :     if ( hIvasDec->hasBeenFedFirstGoodFrame || hIvasDec->isInitialized ) /* note: 'isInitialized' is related to EVS decoder */
    1766             :     {
    1767    15136445 :         ivas_dec_prepare_renderer( hIvasDec->st_ivas );
    1768             :     }
    1769             : 
    1770    15136685 :     hIvasDec->hasBeenPreparedRendering = true;
    1771             : 
    1772    15136685 :     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    34162467 : 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    34162467 :     nSamplesRendered = 0;
    1797    34162467 :     nOutChannels = 0;
    1798    34162467 :     nSamplesRendered_loop = 0;
    1799             : 
    1800    34162467 :     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    34162467 :     if ( hIvasDec->hasBeenPreparedRendering == false )
    1807             :     {
    1808           0 :         return IVAS_ERR_UNKNOWN;
    1809             :     }
    1810             : 
    1811    34162467 :     st_ivas = hIvasDec->st_ivas;
    1812             : 
    1813    34162467 :     if ( hIvasDec->updateOrientation )
    1814             :     {
    1815             :         /*----------------------------------------------------------------*
    1816             :          * Combine orientations
    1817             :          *----------------------------------------------------------------*/
    1818             : 
    1819     4622919 :         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     4622919 :         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           0 :             isar_set_split_rend_ht_setup( &st_ivas->hSplitBinRend->splitrend, st_ivas->hCombinedOrientationData->Quaternions, st_ivas->hCombinedOrientationData->Rmat );
    1831             :         }
    1832             : 
    1833     4622919 :         hIvasDec->updateOrientation = false;
    1834             :     }
    1835             : 
    1836    34162467 :     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    34162467 :     if ( !hIvasDec->isInitialized && st_ivas->bfi )
    1847             :     {
    1848         544 :         hIvasDec->hasBeenFedFrame = false;
    1849         544 :         set_s( pcmBuf, 0, st_ivas->hDecoderConfig->nchan_out * nSamplesAsked );
    1850         544 :         nSamplesRendered = nSamplesAsked;
    1851         544 :         hIvasDec->nSamplesAvailableNext -= nSamplesAsked;
    1852             :     }
    1853             :     else
    1854             :     {
    1855    34161923 :         nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
    1856    34161923 :         hIvasDec->hasBeenFedFrame = false;
    1857             : 
    1858             :         /* check for possible flushed samples from a rate switch */
    1859    34161923 :         if ( hIvasDec->nSamplesFlushed > 0 )
    1860             :         {
    1861             : #ifdef DEBUGGING
    1862             :             assert( hIvasDec->pcmType == pcmType );
    1863             : #endif
    1864             :             /* note: offset (rendered samples) is always 0 */
    1865        4368 :             if ( pcmType == IVAS_DEC_PCM_INT16 )
    1866             :             {
    1867        4368 :                 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        4368 :             nSamplesRendered = hIvasDec->nSamplesFlushed;
    1880        4368 :             hIvasDec->nSamplesFlushed = 0;
    1881             :         }
    1882             : 
    1883             :         /* render IVAS frames directly to the output buffer */
    1884    34161923 :         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    34161923 :         nSamplesRendered += nSamplesRendered_loop;
    1890             :     }
    1891             : 
    1892    34162467 :     if ( hIvasDec->nSamplesAvailableNext == 0 )
    1893             :     {
    1894    15136613 :         *needNewFrame = true;
    1895    15136613 :         hIvasDec->needNewFrame = true;
    1896             :     }
    1897             :     else
    1898             :     {
    1899    19025854 :         *needNewFrame = false;
    1900             :     }
    1901             : 
    1902    34162467 :     *nOutSamples = nSamplesRendered;
    1903             : 
    1904    34162467 :     return IVAS_ERR_OK;
    1905             : }
    1906             : 
    1907             : 
    1908             : /*---------------------------------------------------------------------*
    1909             :  * isar_get_frame_size( )
    1910             :  *
    1911             :  *
    1912             :  *---------------------------------------------------------------------*/
    1913             : 
    1914           0 : 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           0 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1922             : 
    1923           0 :     if ( st_ivas->hDecoderConfig->render_num_subframes != IVAS_RENDER_NUM_SUBFR_20MS &&
    1924           0 :          ( 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           0 :         nSamplesPerChannel = (int16_t) ( output_Fs / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES );
    1928           0 :         nSamplesPerChannel *= (int16_t) st_ivas->hDecoderConfig->render_num_subframes;
    1929             :     }
    1930             :     else
    1931             :     {
    1932           0 :         nSamplesPerChannel = (int16_t) ( output_Fs / FRAMES_PER_SEC );
    1933             :     }
    1934             : 
    1935           0 :     return nSamplesPerChannel;
    1936             : }
    1937             : 
    1938             : 
    1939             : /*---------------------------------------------------------------------*
    1940             :  * isar_render_poses( )
    1941             :  *
    1942             :  *
    1943             :  *---------------------------------------------------------------------*/
    1944             : 
    1945           0 : 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           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1958             :     {
    1959           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1960             :     }
    1961             : 
    1962           0 :     *needNewFrame = false;
    1963             : 
    1964           0 :     st_ivas = hIvasDec->st_ivas;
    1965             : 
    1966           0 :     numPoses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    1967             : 
    1968             :     /* init flush buffer for rate switch if not already initizalized */
    1969           0 :     if ( hIvasDec->flushbuffer == NULL )
    1970             :     {
    1971           0 :         hIvasDec->flushbuffer = (void *) malloc( numPoses * BINAURAL_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( float ) );
    1972           0 :         if ( hIvasDec->flushbuffer == NULL )
    1973             :         {
    1974           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate flush buffer" );
    1975             :         }
    1976           0 :         hIvasDec->pcmType = IVAS_DEC_PCM_FLOAT;
    1977           0 :         set_zero( (float *) hIvasDec->flushbuffer, numPoses * BINAURAL_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
    1978             :     }
    1979             : 
    1980             :     /* render */
    1981           0 :     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           0 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    1987             :     {
    1988           0 :         return IVAS_ERR_OK;
    1989             :     }
    1990             : 
    1991           0 :     if ( !ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type ) )
    1992             :     {
    1993           0 :         ivas_TD_RINGBUF_PushInterleaved( st_ivas->hSplitBinRend->hMultiBinTdData, pcmBuf, *nOutSamples );
    1994             :     }
    1995             : 
    1996           0 :     return error;
    1997             : }
    1998             : 
    1999             : 
    2000             : /*---------------------------------------------------------------------*
    2001             :  * isar_generate_metadata_and_bitstream( )
    2002             :  *
    2003             :  *
    2004             :  *---------------------------------------------------------------------*/
    2005             : 
    2006           0 : 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           0 :     hSplitBinRend = st_ivas->hSplitBinRend;
    2025             : 
    2026           0 :     max_band = (int16_t) ( ( BINAURAL_MAXBANDS * st_ivas->hDecoderConfig->output_Fs ) / 48000 );
    2027           0 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    2028           0 :     cldfb_in_flag = ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type );
    2029             : 
    2030           0 :     if ( cldfb_in_flag )
    2031             :     {
    2032           0 :         n_samples_in_cldfb_slot = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
    2033           0 :         assert( nSamples % n_samples_in_cldfb_slot == 0 );
    2034           0 :         num_cldfb_slots = nSamples / n_samples_in_cldfb_slot;
    2035             : 
    2036           0 :         num_poses = hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    2037             : 
    2038           0 :         for ( i = 0; i < BINAURAL_CHANNELS * num_poses; ++i )
    2039             :         {
    2040           0 :             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           0 :                 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           0 :                 ivas_CLDFB_RINGBUF_Pop( hSplitBinRend->hMultiBinCldfbData[i], NULL, NULL, CLDFB_NO_CHANNELS_MAX );
    2051             :             }
    2052             :         }
    2053             :     }
    2054             :     else
    2055             :     {
    2056           0 :         ivas_TD_RINGBUF_PopChannels( st_ivas->hSplitBinRend->hMultiBinTdData, p_head_pose_buf, nSamples );
    2057             :     }
    2058             : 
    2059             : 
    2060           0 :     if ( st_ivas->hBinRendererTd != NULL )
    2061             :     {
    2062           0 :         ro_md_flag = 1;
    2063             :     }
    2064             :     else
    2065             :     {
    2066           0 :         ro_md_flag = 0;
    2067             :     }
    2068             : 
    2069           0 :     if ( st_ivas->hHeadTrackData != NULL )
    2070             :     {
    2071           0 :         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           0 :     if ( ( error = ISAR_PRE_REND_MultiBinToSplitBinaural( &hSplitBinRend->splitrend,
    2082             :                                                           Quaternion,
    2083           0 :                                                           st_ivas->hRenderConfig->split_rend_config.splitRendBitRate,
    2084           0 :                                                           st_ivas->hRenderConfig->split_rend_config.codec,
    2085           0 :                                                           st_ivas->hRenderConfig->split_rend_config.isar_frame_size_ms,
    2086           0 :                                                           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           0 :     return IVAS_ERR_OK;
    2096             : }
    2097             : 
    2098             : 
    2099             : /*---------------------------------------------------------------------*
    2100             :  * IVAS_DEC_GetSplitBinauralBitstream( )
    2101             :  *
    2102             :  * Get split-rendering bitstream
    2103             :  *---------------------------------------------------------------------*/
    2104             : 
    2105           0 : 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           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2122             :     {
    2123           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2124             :     }
    2125             : 
    2126           0 :     st_ivas = hIvasDec->st_ivas;
    2127             : 
    2128           0 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
    2129             :     {
    2130           0 :         return IVAS_ERR_WRONG_PARAMS;
    2131             :     }
    2132             : 
    2133           0 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    2134           0 :     numSamplesPerChannelToOutput = isar_get_frame_size( st_ivas );
    2135             : 
    2136           0 :     if ( ( error = isar_render_poses( hIvasDec, numSamplesPerChannelToOutput, nOutSamples, needNewFrame ) ) != IVAS_ERR_OK )
    2137             :     {
    2138           0 :         return error;
    2139             :     }
    2140             : 
    2141           0 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    2142             :     {
    2143           0 :         return IVAS_ERR_OK;
    2144             :     }
    2145             : 
    2146           0 :     for ( i = 0; i < BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES; ++i )
    2147             :     {
    2148           0 :         p_head_pose_buf[i] = head_pose_buf[i];
    2149             :     }
    2150             : 
    2151           0 :     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           0 :     if ( pcm_out_flag )
    2158             :     {
    2159             : #ifndef DISABLE_LIMITER
    2160           0 :         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           0 :             ivas_syn_output( p_head_pose_buf, numSamplesPerChannelToOutput, st_ivas->hDecoderConfig->nchan_out, (int16_t *) pcmBuf_out );
    2167             :     }
    2168             : 
    2169           0 :     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    15136445 : 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    15136445 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    2188             :     {
    2189      149096 :         *nTransportChannels = 1;
    2190             :     }
    2191             :     else
    2192             :     {
    2193             :         Decoder_Struct *st_ivas;
    2194             : 
    2195    14987349 :         st_ivas = hIvasDec->st_ivas;
    2196             : 
    2197             :         /* Setup IVAS split rendering */
    2198    14987349 :         if ( splitRendBits != NULL )
    2199             :         {
    2200           0 :             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    14987349 :         if ( st_ivas->bfi == 0 )
    2215             :         {
    2216    14259252 :             if ( ( error = ivas_dec_setup( st_ivas ) ) != IVAS_ERR_OK )
    2217             :             {
    2218           0 :                 return error;
    2219             :             }
    2220             :         }
    2221             : 
    2222    14987349 :         *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    14987349 :         if ( st_ivas->ini_frame == 0 && splitRendBits != NULL )
    2231             :         {
    2232           0 :             if ( ( error = ivas_dec_init_split_rend( st_ivas ) ) != IVAS_ERR_OK )
    2233             :             {
    2234           0 :                 return error;
    2235             :             }
    2236             :         }
    2237             : 
    2238    14987349 :         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           0 :             if ( ( error = ivas_dec_reconfig_split_rend( st_ivas ) ) != IVAS_ERR_OK )
    2241             :             {
    2242           0 :                 return error;
    2243             :             }
    2244             :         }
    2245             :     }
    2246             : 
    2247    15136445 :     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      514821 : 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      514821 :     is_masa_ism = 0;
    2264             : 
    2265      514821 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2266             :     {
    2267           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2268             :     }
    2269             : 
    2270      514821 :     if ( hIvasDec->st_ivas->hMasa != NULL )
    2271             :     {
    2272       72030 :         if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
    2273             :         {
    2274       72030 :             is_masa_ism = 1;
    2275             :         }
    2276             :     }
    2277             : 
    2278      514821 :     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      514821 :         *numObjects = hIvasDec->st_ivas->nchan_ism;
    2281             :     }
    2282             :     else
    2283             :     {
    2284           0 :         *numObjects = 0;
    2285             :     }
    2286             : 
    2287      514821 :     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         150 : 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         150 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2304             :     {
    2305           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2306             :     }
    2307             : 
    2308         150 :     if ( hIvasDec->hasDecodedFirstGoodFrame )
    2309             :     {
    2310         150 :         *format = mapIvasFormat( hIvasDec->st_ivas->ivas_format );
    2311             :     }
    2312             :     else
    2313             :     {
    2314           0 :         *format = IVAS_DEC_BS_UNKOWN;
    2315             :     }
    2316             : 
    2317         150 :     if ( *format == IVAS_DEC_BS_MASA && hIvasDec->st_ivas->hMasa != NULL )
    2318             :     {
    2319          51 :         if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
    2320             :         {
    2321           0 :             *format = IVAS_DEC_BS_MASA_ISM;
    2322             :         }
    2323             :     }
    2324             : 
    2325         150 :     return IVAS_ERR_OK;
    2326             : }
    2327             : 
    2328             : 
    2329             : /*---------------------------------------------------------------------*
    2330             :  * getOutputBufferSize()
    2331             :  *
    2332             :  * Get size of output buffer in samples
    2333             :  *---------------------------------------------------------------------*/
    2334             : 
    2335        3878 : static int16_t getOutputBufferSize(
    2336             :     const Decoder_Struct *st_ivas /* i  : IVAS decoder handle */
    2337             : )
    2338             : {
    2339        3878 :     if ( st_ivas->hDecoderConfig == NULL )
    2340             :     {
    2341           0 :         return -1;
    2342             :     }
    2343             : 
    2344        3878 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    2345             :     {
    2346         300 :         return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * ( MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS ) / FRAMES_PER_SEC );
    2347             :     }
    2348        3578 :     else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2349             :     {
    2350          18 :         if ( st_ivas->hLsSetupCustom == NULL )
    2351             :         {
    2352           0 :             return -1;
    2353             :         }
    2354             : 
    2355          18 :         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        3560 :         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        3878 : 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        3878 :     if ( outputBufferSize == NULL || hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2376             :     {
    2377           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2378             :     }
    2379             : 
    2380        3878 :     *outputBufferSize = getOutputBufferSize( hIvasDec->st_ivas );
    2381             : 
    2382        3878 :     if ( *outputBufferSize == -1 )
    2383             :     {
    2384           0 :         return IVAS_ERR_INVALID_OUTPUT_BUFFER_SIZE;
    2385             :     }
    2386             :     else
    2387             :     {
    2388        3878 :         return IVAS_ERR_OK;
    2389             :     }
    2390             : }
    2391             : 
    2392             : 
    2393             : /*---------------------------------------------------------------------*
    2394             :  * IVAS_DEC_GetNumOutputChannels( )
    2395             :  *
    2396             :  * Returns number of output channels
    2397             :  *---------------------------------------------------------------------*/
    2398             : 
    2399        1939 : 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        1939 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2405             :     {
    2406           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2407             :     }
    2408             : 
    2409        1939 :     if ( hIvasDec->hasDecodedFirstGoodFrame )
    2410             :     {
    2411        1939 :         *numOutputChannels = hIvasDec->st_ivas->hDecoderConfig->nchan_out;
    2412             :     }
    2413             :     else
    2414             :     {
    2415           0 :         *numOutputChannels = 0;
    2416             :     }
    2417             : 
    2418        1939 :     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     1512402 : 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     1512402 :     is_masa_ism = 0;
    2439             : 
    2440     1512402 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2441             :     {
    2442           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2443             :     }
    2444             : 
    2445     1512402 :     st_ivas = hIvasDec->st_ivas;
    2446     1512402 :     if ( hIvasDec->st_ivas->hMasa != NULL )
    2447             :     {
    2448      234168 :         if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
    2449             :         {
    2450      234168 :             is_masa_ism = 1;
    2451             :         }
    2452             :     }
    2453     1512402 :     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     1512402 :     if ( objectIdx >= st_ivas->nchan_ism )
    2459             :     {
    2460           0 :         return IVAS_ERR_INVALID_INDEX;
    2461             :     }
    2462             : 
    2463     1512402 :     hIsmMeta = st_ivas->hIsmMetaData[objectIdx];
    2464             : 
    2465     1512402 :     if ( hIsmMeta == NULL || zero_flag || ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) )
    2466             :     {
    2467       70326 :         metadata->azimuth = 0.f;
    2468       70326 :         metadata->elevation = 0.f;
    2469       70326 :         metadata->radius = 1.f;
    2470       70326 :         metadata->spread = 0.f;
    2471       70326 :         metadata->gainFactor = 1.f;
    2472       70326 :         metadata->yaw = 0.f;
    2473       70326 :         metadata->pitch = 0.f;
    2474       70326 :         metadata->non_diegetic_flag = 0;
    2475             :     }
    2476             :     else
    2477             :     {
    2478     1442076 :         if ( st_ivas->ism_mode == ISM_MODE_DISC )
    2479             :         {
    2480     1022643 :             metadata->azimuth = hIsmMeta->edited_azimuth;
    2481     1022643 :             metadata->elevation = hIsmMeta->edited_elevation;
    2482     1022643 :             metadata->radius = hIsmMeta->edited_radius;
    2483             : 
    2484     1022643 :             metadata->yaw = hIsmMeta->edited_yaw;
    2485     1022643 :             metadata->pitch = hIsmMeta->edited_pitch;
    2486     1022643 :             metadata->spread = 0.f;
    2487             : 
    2488     1022643 :             metadata->gainFactor = hIsmMeta->edited_gain;
    2489     1022643 :             metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
    2490             :         }
    2491      419433 :         else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    2492             :         {
    2493       38496 :             metadata->azimuth = st_ivas->hParamIsmDec->edited_azimuth_values[objectIdx];
    2494       38496 :             metadata->elevation = st_ivas->hParamIsmDec->edited_elevation_values[objectIdx];
    2495       38496 :             metadata->radius = hIsmMeta->radius;
    2496       38496 :             metadata->yaw = hIsmMeta->yaw;
    2497       38496 :             metadata->pitch = hIsmMeta->pitch;
    2498       38496 :             metadata->spread = 0.f;
    2499       38496 :             metadata->gainFactor = 1.f;
    2500       38496 :             metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
    2501             :         }
    2502      380937 :         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      362937 :             metadata->azimuth = st_ivas->hIsmMetaData[objectIdx]->edited_azimuth;
    2505      362937 :             metadata->elevation = st_ivas->hIsmMetaData[objectIdx]->edited_elevation;
    2506      362937 :             metadata->radius = st_ivas->hIsmMetaData[objectIdx]->edited_radius;
    2507      362937 :             metadata->yaw = st_ivas->hIsmMetaData[objectIdx]->edited_yaw;
    2508      362937 :             metadata->pitch = st_ivas->hIsmMetaData[objectIdx]->edited_pitch;
    2509      362937 :             metadata->spread = 0.f;
    2510      362937 :             metadata->gainFactor = st_ivas->hIsmMetaData[objectIdx]->edited_gain;
    2511      362937 :             metadata->non_diegetic_flag = st_ivas->hIsmMetaData[objectIdx]->non_diegetic_flag;
    2512             :         }
    2513             :         else
    2514             :         {
    2515       18000 :             metadata->azimuth = hIsmMeta->azimuth;
    2516       18000 :             metadata->elevation = hIsmMeta->elevation;
    2517       18000 :             metadata->radius = hIsmMeta->radius;
    2518       18000 :             metadata->yaw = hIsmMeta->yaw;
    2519       18000 :             metadata->pitch = hIsmMeta->pitch;
    2520       18000 :             metadata->spread = 0.f;
    2521       18000 :             metadata->gainFactor = 1.f;
    2522       18000 :             metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
    2523             :         }
    2524             :     }
    2525             : 
    2526     1512402 :     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      470634 : 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      470634 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2543             :     {
    2544           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2545             :     }
    2546             : 
    2547      470634 :     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      470634 :     if ( getFromJbmBuffer )
    2553             :     {
    2554       61332 :         ivas_jbm_masa_sf_to_sf_map( hIvasDec->st_ivas );
    2555             :     }
    2556             : 
    2557      470634 :     *hMasaExtOutMeta = hIvasDec->st_ivas->hMasa->data.extOutMeta;
    2558             : 
    2559      470634 :     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     7925777 : 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     7925777 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2581             :     {
    2582           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2583             :     }
    2584             : 
    2585     7925777 :     hHeadTrackData = hIvasDec->st_ivas->hHeadTrackData;
    2586             : 
    2587     7925777 :     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     7925777 :     if ( orientation.w == -3.0f )
    2595             :     {
    2596           0 :         Euler2Quat( deg2rad( orientation.x ), deg2rad( orientation.y ), deg2rad( orientation.z ), &orientation );
    2597             :     }
    2598             : 
    2599     7925777 :     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     7925777 :     hHeadTrackData->Pos[subframe_idx].x = Pos.x;
    2605     7925777 :     hHeadTrackData->Pos[subframe_idx].y = Pos.y;
    2606     7925777 :     hHeadTrackData->Pos[subframe_idx].z = Pos.z;
    2607             : 
    2608     7925777 :     hHeadTrackData->sr_pose_pred_axis = rot_axis;
    2609     7925777 :     hIvasDec->updateOrientation = true;
    2610             : 
    2611     7925777 :     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       95724 : 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       95724 :     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       95724 :     pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
    2668             : 
    2669       95724 :     hIvasDec->updateOrientation = true;
    2670             : 
    2671       95724 :     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     2974657 : 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     2974657 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2694             :     {
    2695           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2696             :     }
    2697             : 
    2698     2974657 :     hExternalOrientationData = hIvasDec->st_ivas->hExtOrientationData;
    2699             : 
    2700     2974657 :     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     2974657 :     QuaternionInverse( orientation, &hExternalOrientationData->Quaternions[subframe_idx] );
    2707             : 
    2708     2974657 :     hExternalOrientationData->enableHeadRotation[subframe_idx] = enableHeadRotation;
    2709     2974657 :     hExternalOrientationData->enableExternalOrientation[subframe_idx] = enableExternalOrientation;
    2710     2974657 :     hExternalOrientationData->enableRotationInterpolation[subframe_idx] = enableRotationInterpolation;
    2711     2974657 :     hExternalOrientationData->numFramesToTargetOrientation[subframe_idx] = numFramesToTargetOrientation;
    2712             : 
    2713     2974657 :     hIvasDec->updateOrientation = true;
    2714             : 
    2715     2974657 :     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           9 : 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           9 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2735             :     {
    2736           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2737             :     }
    2738             : 
    2739           9 :     hLsSetupCustom = hIvasDec->st_ivas->hLsSetupCustom;
    2740             : 
    2741           9 :     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           9 :     hLsSetupCustom->num_spk = hLsCustomData.num_spk;
    2750             : 
    2751           9 :     mvr2r( hLsCustomData.azimuth, hLsSetupCustom->ls_azimuth, hLsCustomData.num_spk );
    2752           9 :     mvr2r( hLsCustomData.elevation, hLsSetupCustom->ls_elevation, hLsCustomData.num_spk );
    2753             : 
    2754             :     /* Set planar flag */
    2755           9 :     is_planar = 1;
    2756         153 :     for ( i = 0; i < hLsCustomData.num_spk; i++ )
    2757             :     {
    2758         144 :         if ( is_planar && hLsSetupCustom->ls_elevation[i] != 0.0f )
    2759             :         {
    2760           9 :             is_planar = 0;
    2761             :         }
    2762             :     }
    2763           9 :     hLsSetupCustom->is_planar_setup = is_planar;
    2764             : 
    2765             :     /* Loudspeaker LFE */
    2766           9 :     hLsSetupCustom->num_lfe = hLsCustomData.num_lfe;
    2767           9 :     mvs2s( hLsCustomData.lfe_idx, hLsSetupCustom->lfe_idx, hLsCustomData.num_lfe );
    2768             : 
    2769           9 :     return IVAS_ERR_OK;
    2770             : }
    2771             : 
    2772             : 
    2773             : /*---------------------------------------------------------------------*
    2774             :  * IVAS_DEC_GetHrtfTDrendHandle( )
    2775             :  *
    2776             :  * Get TD binaural renderer handle
    2777             :  *---------------------------------------------------------------------*/
    2778             : 
    2779        3561 : 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        3561 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfTD == NULL )
    2785             :     {
    2786           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2787             :     }
    2788             : 
    2789        3561 :     *hHrtfTD = &hIvasDec->st_ivas->hHrtfTD;
    2790             : 
    2791        3561 :     return IVAS_ERR_OK;
    2792             : }
    2793             : 
    2794             : 
    2795             : /*---------------------------------------------------------------------*
    2796             :  * IVAS_DEC_GetHrtfCRendHandle( )
    2797             :  *
    2798             :  * Get Crend binaural renderer handle
    2799             :  *---------------------------------------------------------------------*/
    2800             : 
    2801        1407 : 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        1407 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfCrend == NULL )
    2807             :     {
    2808           0 :         return IVAS_ERR_WRONG_PARAMS;
    2809             :     }
    2810             : 
    2811        1407 :     *hHrtfCrend = &hIvasDec->st_ivas->hHrtfCrend;
    2812             : 
    2813        1407 :     return IVAS_ERR_OK;
    2814             : }
    2815             : 
    2816             : 
    2817             : /*---------------------------------------------------------------------*
    2818             :  * IVAS_DEC_GetHrtfFastConvHandle( )
    2819             :  *
    2820             :  * Get FastConv binaural renderer handle
    2821             :  *---------------------------------------------------------------------*/
    2822             : 
    2823        2880 : 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        2880 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfFastConv == NULL )
    2829             :     {
    2830           0 :         return IVAS_ERR_WRONG_PARAMS;
    2831             :     }
    2832             : 
    2833        2880 :     *hHrtfFastConv = &hIvasDec->st_ivas->hHrtfFastConv;
    2834             : 
    2835        2880 :     return IVAS_ERR_OK;
    2836             : }
    2837             : 
    2838             : 
    2839             : /*---------------------------------------------------------------------*
    2840             :  * IVAS_DEC_GetHrtfParamBinHandle( )
    2841             :  *
    2842             :  * Get Parametric binaural renderer handle
    2843             :  *---------------------------------------------------------------------*/
    2844             : 
    2845        8148 : 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        8148 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfParambin == NULL )
    2851             :     {
    2852           0 :         return IVAS_ERR_WRONG_PARAMS;
    2853             :     }
    2854             : 
    2855        8148 :     *hHrtfParambin = &hIvasDec->st_ivas->hHrtfParambin;
    2856             : 
    2857        8148 :     return IVAS_ERR_OK;
    2858             : }
    2859             : 
    2860             : /*---------------------------------------------------------------------*
    2861             :  * IVAS_DEC_GetHrtfStatisticsHandle( )
    2862             :  *
    2863             :  * Get HRTF statistics (room effect) binaural renderer handle
    2864             :  *---------------------------------------------------------------------*/
    2865             : 
    2866           9 : 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           9 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfStatistics == NULL )
    2872             :     {
    2873           0 :         return IVAS_ERR_WRONG_PARAMS;
    2874             :     }
    2875             : 
    2876           9 :     *hHrtfStatistics = &hIvasDec->st_ivas->hHrtfStatistics;
    2877             : 
    2878           9 :     return IVAS_ERR_OK;
    2879             : }
    2880             : 
    2881             : 
    2882             : /*---------------------------------------------------------------------*
    2883             :  * IVAS_DEC_HRTF_binary_open( )
    2884             :  *
    2885             :  * Allocate HRTF binary handles
    2886             :  *---------------------------------------------------------------------*/
    2887             : 
    2888       26184 : 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       26184 :     st_ivas = hIvasDec->st_ivas;
    2897             : 
    2898       26184 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2899             :     {
    2900           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2901             :     }
    2902             : 
    2903       26184 :     if ( st_ivas->hDecoderConfig->Opt_HRTF_binary )
    2904             :     {
    2905             :         /* TD binaural renderer */
    2906       26184 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_TDREND || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfTD == NULL )
    2907             :         {
    2908        3561 :             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       26184 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfCrend == NULL )
    2916             :         {
    2917        1407 :             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       26184 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfFastConv == NULL )
    2925             :         {
    2926        2688 :             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       26184 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_PARAMBIN || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfParambin == NULL )
    2934             :         {
    2935        3651 :             if ( ( error = ivas_HRTF_parambin_binary_open( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
    2936             :             {
    2937           0 :                 return error;
    2938             :             }
    2939             :         }
    2940             : 
    2941       26184 :         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           9 :             if ( ( error = ivas_HRTF_statistics_binary_open( &st_ivas->hHrtfStatistics ) ) != IVAS_ERR_OK )
    2944             :             {
    2945           0 :                 return error;
    2946             :             }
    2947             :         }
    2948             :     }
    2949             : 
    2950       26184 :     return IVAS_ERR_OK;
    2951             : }
    2952             : 
    2953             : 
    2954             : /*---------------------------------------------------------------------*
    2955             :  * IVAS_DEC_HRTF_binary_close( )
    2956             :  *
    2957             :  * Deallocate HRTF binary handles
    2958             :  *---------------------------------------------------------------------*/
    2959             : 
    2960       13092 : 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       13092 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2968             :     {
    2969           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2970             :     }
    2971             : 
    2972       13092 :     st_ivas = hIvasDec->st_ivas;
    2973             : 
    2974       13092 :     if ( st_ivas->hDecoderConfig->Opt_HRTF_binary && st_ivas->ini_frame > 0 )
    2975             :     {
    2976       13020 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_TDREND || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    2977             :         {
    2978       12381 :             ivas_HRTF_td_binary_close( &st_ivas->hHrtfTD );
    2979             :         }
    2980             : 
    2981       13020 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_CREND || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    2982             :         {
    2983             :             /* CRend binaural renderer handle */
    2984       11628 :             ivas_HRTF_CRend_binary_close( &st_ivas->hHrtfCrend );
    2985             :         }
    2986             : 
    2987       13020 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    2988             :         {
    2989             :             /* Fastconv HRTF filters */
    2990       10161 :             ivas_HRTF_fastconv_binary_close( &st_ivas->hHrtfFastConv );
    2991             :         }
    2992             : 
    2993       13020 :         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        4890 :             ivas_HRTF_parambin_binary_close( &st_ivas->hHrtfParambin );
    2998             :         }
    2999             :     }
    3000             : 
    3001       13092 :     return IVAS_ERR_OK;
    3002             : }
    3003             : 
    3004             : 
    3005             : /*---------------------------------------------------------------------*
    3006             :  * IVAS_DEC_AddAcousticEnvironment( )
    3007             :  *
    3008             :  * Adds acoustic environment configuration
    3009             :  *---------------------------------------------------------------------*/
    3010             : 
    3011         129 : 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         129 :     IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pAE = NULL;
    3019             : 
    3020         129 :     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         129 :     st_ivas = hIvasDec->st_ivas;
    3026             : 
    3027             :     /* Check if already there */
    3028         192 :     for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
    3029             :     {
    3030          63 :         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         129 :     if ( pAE == NULL )
    3039             :     {
    3040         129 :         IVAS_ROOM_ACOUSTICS_CONFIG_DATA *ppAE = malloc( ( st_ivas->acousticEnvironmentsCount + 1 ) * sizeof( IVAS_ROOM_ACOUSTICS_CONFIG_DATA ) );
    3041             : 
    3042         129 :         if ( ppAE == NULL )
    3043             :         {
    3044           0 :             return IVAS_ERR_FAILED_ALLOC;
    3045             :         }
    3046             : 
    3047         192 :         for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
    3048             :         {
    3049          63 :             pAE = &ppAE[n];
    3050          63 :             pAE->aeID = st_ivas->pAcousticEnvironments[n].aeID;
    3051          63 :             pAE->nBands = st_ivas->pAcousticEnvironments[n].nBands;
    3052          63 :             pAE->acousticPreDelay = st_ivas->pAcousticEnvironments[n].acousticPreDelay;
    3053          63 :             pAE->inputPreDelay = st_ivas->pAcousticEnvironments[n].inputPreDelay;
    3054             : 
    3055          63 :             mvr2r( st_ivas->pAcousticEnvironments[n].pFc_input, pAE->pFc_input, CLDFB_NO_CHANNELS_MAX );
    3056          63 :             mvr2r( st_ivas->pAcousticEnvironments[n].pAcoustic_rt60, pAE->pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3057          63 :             mvr2r( st_ivas->pAcousticEnvironments[n].pAcoustic_dsr, pAE->pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3058             : 
    3059          63 :             pAE->use_er = st_ivas->pAcousticEnvironments[n].use_er;
    3060             : 
    3061          63 :             if ( pAE->use_er == 1 )
    3062             :             {
    3063          21 :                 pAE->lowComplexity = st_ivas->pAcousticEnvironments[n].lowComplexity;
    3064          21 :                 pAE->dimensions.x = st_ivas->pAcousticEnvironments[n].dimensions.x;
    3065          21 :                 pAE->dimensions.y = st_ivas->pAcousticEnvironments[n].dimensions.y;
    3066          21 :                 pAE->dimensions.z = st_ivas->pAcousticEnvironments[n].dimensions.z;
    3067          21 :                 pAE->ListenerOrigin.x = st_ivas->pAcousticEnvironments[n].ListenerOrigin.x;
    3068          21 :                 pAE->ListenerOrigin.y = st_ivas->pAcousticEnvironments[n].ListenerOrigin.y;
    3069          21 :                 pAE->ListenerOrigin.z = st_ivas->pAcousticEnvironments[n].ListenerOrigin.z;
    3070             : 
    3071          21 :                 mvr2r( st_ivas->pAcousticEnvironments[n].AbsCoeff, pAE->AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3072             :             }
    3073             :         }
    3074             : 
    3075         129 :         free( st_ivas->pAcousticEnvironments );
    3076         129 :         st_ivas->pAcousticEnvironments = ppAE;
    3077         129 :         n = st_ivas->acousticEnvironmentsCount++;
    3078         129 :         pAE = &st_ivas->pAcousticEnvironments[n];
    3079             :     }
    3080             : 
    3081         129 :     pAE->aeID = roomAcousticsConfig.aeID;
    3082         129 :     pAE->nBands = roomAcousticsConfig.nBands;
    3083         129 :     pAE->acousticPreDelay = roomAcousticsConfig.acousticPreDelay;
    3084         129 :     pAE->inputPreDelay = roomAcousticsConfig.inputPreDelay;
    3085             : 
    3086         129 :     mvr2r( roomAcousticsConfig.pFc_input, pAE->pFc_input, CLDFB_NO_CHANNELS_MAX );
    3087         129 :     mvr2r( roomAcousticsConfig.pAcoustic_rt60, pAE->pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3088         129 :     mvr2r( roomAcousticsConfig.pAcoustic_dsr, pAE->pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3089             : 
    3090         129 :     pAE->use_er = roomAcousticsConfig.use_er;
    3091             : 
    3092         129 :     if ( pAE->use_er == 1 )
    3093             :     {
    3094          54 :         pAE->lowComplexity = roomAcousticsConfig.lowComplexity;
    3095          54 :         pAE->dimensions.x = roomAcousticsConfig.dimensions.x;
    3096          54 :         pAE->dimensions.y = roomAcousticsConfig.dimensions.y;
    3097          54 :         pAE->dimensions.z = roomAcousticsConfig.dimensions.z;
    3098          54 :         pAE->ListenerOrigin.x = roomAcousticsConfig.ListenerOrigin.x;
    3099          54 :         pAE->ListenerOrigin.y = roomAcousticsConfig.ListenerOrigin.y;
    3100          54 :         pAE->ListenerOrigin.z = roomAcousticsConfig.ListenerOrigin.z;
    3101             : 
    3102          54 :         mvr2r( roomAcousticsConfig.AbsCoeff, pAE->AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3103             :     }
    3104             : 
    3105         129 :     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          87 : 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          87 :     uint16_t found = 0;
    3123             :     Decoder_Struct *st_ivas;
    3124             : 
    3125          87 :     if ( hIvasDec == NULL || pAcEnv == NULL )
    3126             :     {
    3127           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3128             :     }
    3129             : 
    3130          87 :     st_ivas = hIvasDec->st_ivas;
    3131             : 
    3132             :     /* In case of default AE ID, select the first one available */
    3133          87 :     if ( aeID == IVAS_DEFAULT_AEID && st_ivas->acousticEnvironmentsCount > 0 )
    3134             :     {
    3135          75 :         aeID = st_ivas->pAcousticEnvironments[0].aeID;
    3136             :     }
    3137             : 
    3138         216 :     for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
    3139             :     {
    3140         129 :         IVAS_ROOM_ACOUSTICS_CONFIG_DATA ae = st_ivas->pAcousticEnvironments[n];
    3141             : 
    3142         129 :         if ( aeID == ae.aeID )
    3143             :         {
    3144          87 :             found = 1;
    3145          87 :             pAcEnv->aeID = aeID;
    3146          87 :             pAcEnv->nBands = ae.nBands;
    3147          87 :             pAcEnv->inputPreDelay = ae.inputPreDelay;
    3148        2637 :             for ( m = 0; m < pAcEnv->nBands; m++ )
    3149             :             {
    3150        2550 :                 pAcEnv->pFc_input[m] = ae.pFc_input[m];
    3151        2550 :                 pAcEnv->pAcoustic_rt60[m] = ae.pAcoustic_rt60[m];
    3152        2550 :                 pAcEnv->pAcoustic_dsr[m] = ae.pAcoustic_dsr[m];
    3153             :             }
    3154             : 
    3155             :             /* If ER are allocated then propagate parameters */
    3156          87 :             pAcEnv->use_er = ae.use_er;
    3157          87 :             if ( ae.use_er != 0 )
    3158             :             {
    3159          21 :                 pAcEnv->lowComplexity = ae.lowComplexity;
    3160             : 
    3161          21 :                 pAcEnv->dimensions.x = ae.dimensions.x;
    3162          21 :                 pAcEnv->dimensions.y = ae.dimensions.y;
    3163          21 :                 pAcEnv->dimensions.z = ae.dimensions.z;
    3164             : 
    3165          21 :                 pAcEnv->ListenerOrigin.x = ae.ListenerOrigin.x;
    3166          21 :                 pAcEnv->ListenerOrigin.y = ae.ListenerOrigin.y;
    3167          21 :                 pAcEnv->ListenerOrigin.z = ae.ListenerOrigin.z;
    3168             : 
    3169         147 :                 for ( m = 0; m < IVAS_ROOM_ABS_COEFF; m++ )
    3170             :                 {
    3171         126 :                     pAcEnv->AbsCoeff[m] = ae.AbsCoeff[m];
    3172             :                 }
    3173             :             }
    3174             :         }
    3175             :     }
    3176             : 
    3177          87 :     return found ? IVAS_ERR_OK : IVAS_ERR_ACOUSTIC_ENVIRONMENT_MISSING;
    3178             : }
    3179             : 
    3180             : 
    3181             : /*---------------------------------------------------------------------*
    3182             :  * copyRendererConfigStruct( )
    3183             :  *
    3184             :  *
    3185             :  *---------------------------------------------------------------------*/
    3186             : 
    3187         192 : static ivas_error copyRendererConfigStruct(
    3188             :     RENDER_CONFIG_HANDLE hRCin,
    3189             :     IVAS_RENDER_CONFIG_HANDLE hRCout )
    3190             : {
    3191         192 :     if ( hRCin == NULL || hRCout == NULL )
    3192             :     {
    3193           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3194             :     }
    3195             : 
    3196         192 :     hRCout->roomAcoustics.aeID = hRCin->roomAcoustics.aeID;
    3197         192 :     hRCout->roomAcoustics.nBands = hRCin->roomAcoustics.nBands;
    3198         192 :     hRCout->roomAcoustics.acousticPreDelay = hRCin->roomAcoustics.acousticPreDelay;
    3199         192 :     hRCout->roomAcoustics.inputPreDelay = hRCin->roomAcoustics.inputPreDelay;
    3200             : 
    3201         192 :     mvr2r( hRCin->roomAcoustics.pFc_input, hRCout->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
    3202         192 :     mvr2r( hRCin->roomAcoustics.pAcoustic_rt60, hRCout->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3203         192 :     mvr2r( hRCin->roomAcoustics.pAcoustic_dsr, hRCout->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3204         192 :     mvr2r( hRCin->directivity, hRCout->directivity, 3 * MAX_NUM_OBJECTS );
    3205         192 :     mvr2r( hRCin->distAtt, hRCout->distAtt, 3 );
    3206             : 
    3207         192 :     hRCout->split_rend_config = hRCin->split_rend_config;
    3208             : 
    3209         192 :     hRCout->roomAcoustics.use_er = hRCin->roomAcoustics.use_er;
    3210         192 :     hRCout->roomAcoustics.lowComplexity = hRCin->roomAcoustics.lowComplexity;
    3211             : 
    3212         192 :     return IVAS_ERR_OK;
    3213             : }
    3214             : 
    3215             : 
    3216             : /*---------------------------------------------------------------------*
    3217             :  * IVAS_DEC_GetRenderConfig( )
    3218             :  *
    3219             :  * Return renderer configuration parameters handle
    3220             :  *---------------------------------------------------------------------*/
    3221             : 
    3222         192 : 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         192 :     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         192 :     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         360 : 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         360 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL )
    3276             :     {
    3277           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3278             :     }
    3279             : 
    3280         360 :     hRenderConfig = hIvasDec->st_ivas->hRenderConfig;
    3281         360 :     st_ivas = hIvasDec->st_ivas;
    3282             : 
    3283         360 :     hRenderConfig->roomAcoustics.aeID = renderConfig.roomAcoustics.aeID;
    3284         360 :     hRenderConfig->roomAcoustics.nBands = renderConfig.roomAcoustics.nBands;
    3285         360 :     hRenderConfig->roomAcoustics.acousticPreDelay = renderConfig.roomAcoustics.acousticPreDelay;
    3286         360 :     hRenderConfig->roomAcoustics.inputPreDelay = renderConfig.roomAcoustics.inputPreDelay;
    3287             : 
    3288         360 :     hRenderConfig->roomAcoustics.use_er = 0;
    3289         360 :     if ( renderConfig.roomAcoustics.use_er == 1 )
    3290             :     {
    3291         198 :         hRenderConfig->roomAcoustics.use_er = renderConfig.roomAcoustics.use_er;
    3292         198 :         hRenderConfig->roomAcoustics.lowComplexity = renderConfig.roomAcoustics.lowComplexity;
    3293         198 :         hRenderConfig->roomAcoustics.dimensions = renderConfig.roomAcoustics.dimensions;
    3294         198 :         hRenderConfig->roomAcoustics.ListenerOrigin = renderConfig.roomAcoustics.ListenerOrigin;
    3295             : 
    3296         198 :         mvr2r( renderConfig.roomAcoustics.AbsCoeff, hRenderConfig->roomAcoustics.AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3297             :     }
    3298             : 
    3299         360 :     mvr2r( renderConfig.roomAcoustics.pFc_input, hRenderConfig->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
    3300         360 :     mvr2r( renderConfig.roomAcoustics.pAcoustic_rt60, hRenderConfig->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3301         360 :     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         360 :     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         360 :     if ( st_ivas->hCrendWrapper != NULL && st_ivas->hCrendWrapper->hCrend[0] != NULL && st_ivas->hCrendWrapper->hCrend[0]->hReverb != NULL )
    3316             :     {
    3317          90 :         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         360 :     if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->hReverb != NULL )
    3325             :     {
    3326          93 :         ivas_binaural_reverb_close( &( st_ivas->hDiracDecBin[0]->hReverb ) );
    3327             : 
    3328          93 :         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          93 :                                                   &( 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         360 :     if ( st_ivas->hBinRenderer != NULL && st_ivas->hBinRenderer->hReverb != NULL )
    3337             :     {
    3338          81 :         ivas_binaural_reverb_close( &( st_ivas->hBinRenderer->hReverb ) );
    3339             : 
    3340          81 :         if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hBinRenderer->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hBinRenderer->conv_band, st_ivas->hBinRenderer->timeSlots,
    3341          81 :                                                   &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
    3342             :         {
    3343           0 :             return error;
    3344             :         }
    3345             :     }
    3346             : 
    3347         360 :     mvr2r( renderConfig.directivity, hRenderConfig->directivity, 3 * MAX_NUM_OBJECTS );
    3348         360 :     mvr2r( renderConfig.distAtt, hRenderConfig->distAtt, 3 );
    3349             : 
    3350         360 :     hRenderConfig->split_rend_config = renderConfig.split_rend_config;
    3351             : 
    3352             :     /* Overwrite any pose correction settings if 0 DOF (no pose correction) was selected */
    3353         360 :     if ( hRenderConfig->split_rend_config.dof == 0 )
    3354             :     {
    3355           0 :         hRenderConfig->split_rend_config.poseCorrectionMode = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
    3356             :     }
    3357             : 
    3358         360 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, hRenderConfig ) )
    3359             :     {
    3360           0 :         if ( ( error = isar_split_rend_validate_config( &hRenderConfig->split_rend_config, ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0 ) ) != IVAS_ERR_OK )
    3361             :         {
    3362           0 :             return error;
    3363             :         }
    3364             :     }
    3365             : 
    3366         360 :     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      472573 : 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      472573 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || nSamples == NULL || timeScale == NULL )
    3503             :     {
    3504           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3505             :     }
    3506             : 
    3507      472573 :     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      472573 :     st_ivas = hIvasDec->st_ivas;
    3514      472573 :     hDecoderConfig = st_ivas->hDecoderConfig;
    3515             : 
    3516      472573 :     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      472573 :     nSamples[2] = (int16_t) roundf( (float) st_ivas->binaural_latency_ns * hDecoderConfig->output_Fs / 1000000000.f );
    3519      472573 :     nSamples[0] = nSamples[1] + nSamples[2];
    3520             : 
    3521      472573 :     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      470991 :         nSamples[1] -= NS2SA( hDecoderConfig->output_Fs, IVAS_ENC_DELAY_NS );
    3525             :     }
    3526             : 
    3527      472573 :     *timeScale = hDecoderConfig->output_Fs;
    3528             : 
    3529      472573 :     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        4251 : 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        4251 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hasDecodedFirstGoodFrame == NULL )
    3545             :     {
    3546           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3547             :     }
    3548             : 
    3549        4251 :     *hasDecodedFirstGoodFrame = hIvasDec->hasDecodedFirstGoodFrame;
    3550             : 
    3551        4251 :     return IVAS_ERR_OK;
    3552             : }
    3553             : 
    3554             : 
    3555             : /*---------------------------------------------------------------------*
    3556             :  * isSidFrame( )
    3557             :  *
    3558             :  * Check if a frame contains a SID
    3559             :  *---------------------------------------------------------------------*/
    3560             : 
    3561      759816 : static bool isSidFrame(
    3562             :     const uint16_t size )
    3563             : {
    3564      759816 :     if ( size == SID_1k75 / FRAMES_PER_SEC )
    3565             :     {
    3566           0 :         return true; /* AMR-WB SID */
    3567             :     }
    3568      759816 :     else if ( size == SID_2k40 / FRAMES_PER_SEC )
    3569             :     {
    3570           0 :         return true; /* EVS SID */
    3571             :     }
    3572      759816 :     else if ( size == IVAS_SID_5k2 / FRAMES_PER_SEC )
    3573             :     {
    3574        2067 :         return true; /* IVAS SID */
    3575             :     }
    3576             : 
    3577      757749 :     return false;
    3578             : }
    3579             : 
    3580             : 
    3581             : /*---------------------------------------------------------------------*
    3582             :  * bsCompactToSerial( )
    3583             :  *
    3584             :  * Bitstream conversion to Byte format
    3585             :  *---------------------------------------------------------------------*/
    3586             : 
    3587      758928 : 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      758928 :     uint8_t byte = 0;
    3596      758928 :     const uint8_t mask = 0x80;
    3597             : 
    3598  1698110440 :     for ( i = 0; i < num_bits; ++i )
    3599             :     {
    3600  1697351512 :         if ( i % 8 == 0 )
    3601             :         {
    3602   212168939 :             byte = compact[i / 8];
    3603             :         }
    3604             : 
    3605  1697351512 :         serial[i] = ( byte & mask ) >> 7;
    3606             : 
    3607  1697351512 :         byte <<= 1;
    3608             :     }
    3609             : 
    3610             :     /* Add 4 padding bytes required by core coder */
    3611    25044624 :     for ( i = 0; i < 4 * 8; ++i )
    3612             :     {
    3613    24285696 :         serial[num_bits + i] = 0;
    3614             :     }
    3615             : #undef WMC_TOOL_SKIP
    3616      758928 : }
    3617             : 
    3618             : 
    3619             : /*---------------------------------------------------------------------*
    3620             :  * IVAS_DEC_VoIP_FeedFrame( )
    3621             :  *
    3622             :  * Feed RTP packet into internal jitter buffer
    3623             :  *---------------------------------------------------------------------*/
    3624             : 
    3625      759816 : 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      759816 :     if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || au == NULL )
    3640             :     {
    3641           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3642             :     }
    3643             : 
    3644      759816 :     if ( auSize == 0 )
    3645             :     {
    3646           0 :         return IVAS_ERR_OK; /* ignore empty/NO_DATA frame - shouldn't be transmitted in RTP */
    3647             :     }
    3648      759816 :     if ( ( auSize + 7 ) / 8 > MAX_AU_SIZE )
    3649             :     {
    3650           0 :         return IVAS_ERR_INVALID_BITSTREAM;
    3651             :     }
    3652             : 
    3653      759816 :     partialCopyFrameType = 0;
    3654      759816 :     partialCopyOffset = 0;
    3655             : 
    3656      759816 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    3657             :     {
    3658             :         /* check if frame contains a partial copy and get its offset */
    3659        3084 :         evs_dec_previewFrame( au, auSize, &partialCopyFrameType, &partialCopyOffset );
    3660             :     }
    3661             : 
    3662             :     /* create data unit for primary copy in the frame */
    3663      759816 :     dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
    3664      759816 :     mvc2c( au, dataUnit->data, (int16_t) ( ( auSize + 7 ) / 8 ) );
    3665      759816 :     dataUnit->dataSize = auSize;
    3666      759816 :     dataUnit->duration = 20;
    3667      759816 :     dataUnit->sequenceNumber = rtpSequenceNumber;
    3668      759816 :     dataUnit->silenceIndicator = isSidFrame( dataUnit->dataSize );
    3669      759816 :     dataUnit->timeScale = 1000;
    3670      759816 :     dataUnit->rcvTime = rcvTime_ms;
    3671      759816 :     dataUnit->timeStamp = rtpTimeStamp;
    3672      759816 :     dataUnit->partial_frame = 0;
    3673      759816 :     dataUnit->partialCopyOffset = partialCopyOffset;
    3674      759816 :     dataUnit->qBit = qBit;
    3675             : 
    3676             :     /* add the frame to the JBM */
    3677      759816 :     result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
    3678      759816 :     if ( result != 0 )
    3679             :     {
    3680           0 :         return IVAS_ERR_UNKNOWN;
    3681             :     }
    3682             : 
    3683      759816 :     if ( partialCopyFrameType != RF_NO_DATA && partialCopyOffset != 0 )
    3684             :     {
    3685             :         /* create data unit for partial copy in the frame */
    3686           0 :         dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
    3687           0 :         mvc2c( au, dataUnit->data, (int16_t) ( ( auSize + 7 ) / 8 ) );
    3688           0 :         dataUnit->dataSize = auSize;
    3689           0 :         dataUnit->duration = 20;
    3690           0 :         dataUnit->sequenceNumber = rtpSequenceNumber;
    3691           0 :         dataUnit->silenceIndicator = 0; /* there are no partial copies for SID frames */
    3692           0 :         dataUnit->timeScale = 1000;
    3693           0 :         dataUnit->rcvTime = rcvTime_ms;
    3694           0 :         dataUnit->timeStamp = rtpTimeStamp - partialCopyOffset * dataUnit->duration;
    3695           0 :         dataUnit->partial_frame = 1;
    3696           0 :         dataUnit->partialCopyOffset = partialCopyOffset;
    3697           0 :         dataUnit->qBit = qBit;
    3698             : 
    3699             :         /* add the frame to the JBM */
    3700           0 :         result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
    3701           0 :         if ( result != 0 )
    3702             :         {
    3703           0 :             return IVAS_ERR_UNKNOWN;
    3704             :         }
    3705             :     }
    3706             : 
    3707      759816 :     return IVAS_ERR_OK;
    3708             : }
    3709             : 
    3710             : 
    3711             : /*---------------------------------------------------------------------*
    3712             :  * IVAS_DEC_VoIP_SetScale( )
    3713             :  *
    3714             :  * Set the TSM scale
    3715             :  *---------------------------------------------------------------------*/
    3716             : 
    3717      814435 : 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      814435 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    3724             :     {
    3725           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3726             :     }
    3727             : 
    3728      814435 :     if ( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm == 0 )
    3729             :     {
    3730           0 :         return IVAS_ERR_TSM_NOT_ENABLED;
    3731             :     }
    3732             :     else
    3733             :     {
    3734      814435 :         hIvasDec->tsm_scale = scale;
    3735      814435 :         hIvasDec->tsm_max_scaling = maxScaling;
    3736             :     }
    3737             : 
    3738      814435 :     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     3351613 : 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     3351613 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->hVoIP == NULL )
    3852             :     {
    3853           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3854             :     }
    3855             : 
    3856     3351613 :     st_ivas = hIvasDec->st_ivas;
    3857     3351613 :     hDecoderConfig = st_ivas->hDecoderConfig;
    3858     3351613 :     hVoIP = hIvasDec->hVoIP;
    3859     3351613 :     nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
    3860     3351613 :     *parametersAvailableForEditing = false;
    3861             : 
    3862     3351613 :     if ( nSamplesPerChannel == 0 )
    3863             :     {
    3864           0 :         return IVAS_ERR_WRONG_PARAMS;
    3865             :     }
    3866             : 
    3867     3351613 :     if ( ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
    3868     3351613 :            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     5631402 :     while ( *nSamplesRendered < nSamplesPerChannel )
    3876             :     {
    3877     3852336 :         if ( hIvasDec->nSamplesAvailableNext == 0 )
    3878             :         {
    3879             :             int16_t nSamplesBuffered;
    3880      814435 :             nSamplesBuffered = 0;
    3881      814435 :             if ( hIvasDec->hasBeenFedFirstGoodFrame )
    3882             :             {
    3883             :                 /* check if the TC buffer already exists, otherweise nothing is buffered anyway */
    3884      813508 :                 if ( st_ivas->hTcBuffer != NULL )
    3885             :                 {
    3886      813508 :                     nSamplesBuffered = st_ivas->hTcBuffer->n_samples_buffered - st_ivas->hTcBuffer->n_samples_rendered;
    3887      813508 :                     nSamplesBuffered += hVoIP->nSamplesRendered20ms;
    3888             :                 }
    3889             :             }
    3890             : 
    3891      814435 :             extBufferedSamples = nSamplesBuffered;
    3892      814435 :             extBufferedTime_ms = extBufferedSamples * 1000 / hDecoderConfig->output_Fs;
    3893      814435 :             dataUnit = NULL;
    3894             : 
    3895             :             /* pop one access unit from the jitter buffer */
    3896      814435 :             result = JB4_PopDataUnit( hVoIP->hJBM, systemTimestamp_ms, extBufferedTime_ms, &dataUnit, &scale, &maxScaling );
    3897      814435 :             if ( result != 0 )
    3898             :             {
    3899           0 :                 return IVAS_ERR_UNKNOWN;
    3900             :             }
    3901      814435 :             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      814435 :             if ( scale != 100U )
    3912             :             {
    3913       22357 :                 if ( hIvasDec->timeScalingDone )
    3914             :                 {
    3915         207 :                     scale = 100;
    3916             :                 }
    3917             :             }
    3918             : 
    3919             :             /* limit scale to range supported by time scaler */
    3920      814435 :             if ( scale < APA_MIN_SCALE )
    3921             :             {
    3922           0 :                 scale = APA_MIN_SCALE;
    3923             :             }
    3924      814435 :             else if ( scale > APA_MAX_SCALE )
    3925             :             {
    3926           0 :                 scale = APA_MAX_SCALE;
    3927             :             }
    3928             : 
    3929      814435 :             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      814435 :             if ( dataUnit )
    3936             :             {
    3937      758928 :                 hIvasDec->hVoIP->hCurrentDataUnit = dataUnit;
    3938             : 
    3939      758928 :                 bsCompactToSerial( dataUnit->data, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize );
    3940             : 
    3941      758928 :                 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      758928 :                 *bitstreamReadDone = true;
    3947             :             }
    3948       55507 :             else if ( hIvasDec->hasDecodedFirstGoodFrame )
    3949             :             {
    3950             :                 /* Decoder has been initialized with first good frame - do PLC */
    3951       54715 :                 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      814435 :             if ( jbmWriterFn != NULL && jbmWriter != NULL )
    3960             :             {
    3961             :                 /* write JBM trace data entry */
    3962      814435 :                 store_JbmData( hVoIP, dataUnit, systemTimestamp_ms, extBufferedSamples, hDecoderConfig->output_Fs );
    3963      814435 :                 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      814435 :             if ( dataUnit )
    3971             :             {
    3972      758928 :                 if ( dataUnit->partial_frame != 0 )
    3973             :                 {
    3974           0 :                     hVoIP->lastDecodedWasActive = 1;
    3975             :                 }
    3976             :                 else
    3977             :                 {
    3978      758928 :                     hVoIP->lastDecodedWasActive = !dataUnit->silenceIndicator;
    3979             :                 }
    3980             : 
    3981             :                 /* data unit memory is no longer used */
    3982      758928 :                 JB4_FreeDataUnit( hVoIP->hJBM, dataUnit );
    3983             :             }
    3984             : 
    3985      814435 :             if ( hIvasDec->hasBeenFedFirstGoodFrame && *bitstreamReadDone == true )
    3986             :             {
    3987             :                 /* new bitstream was feeded, return for reconfiguration */
    3988      758928 :                 return IVAS_ERR_OK;
    3989             :             }
    3990             : 
    3991       55507 :             if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    3992             :             {
    3993         816 :                 hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
    3994             :             }
    3995             :         }
    3996             : 
    3997             :         /* decode */
    3998     3093408 :         if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    3999             :         {
    4000             :             /* codec mode to use not known yet - simply output silence */
    4001             :             /* directly set output zero */
    4002        1792 :             int16_t nSamplesToZero = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
    4003        1792 :             set_pcm_buffer_to_zero( pcm_buffer_offset( pcmBuf, pcmType, ( *nSamplesRendered ) * nOutChannels ), pcmType, nSamplesToZero * nOutChannels );
    4004        1792 :             *nSamplesRendered += nSamplesToZero;
    4005        1792 :             hIvasDec->nSamplesAvailableNext -= nSamplesToZero;
    4006        1792 :             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     3091616 :             if ( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame )
    4015             :             {
    4016      813619 :                 if ( hIvasDec->nSamplesAvailableNext == 0 || hIvasDec->nSamplesAvailableNext == hIvasDec->nSamplesFrame )
    4017             :                 {
    4018      813619 :                     if ( ( error = IVAS_DEC_GetSamplesDecoder( hIvasDec, splitRendBits ) ) != IVAS_ERR_OK )
    4019             :                     {
    4020      813619 :                         return error;
    4021             :                     }
    4022             : 
    4023      813619 :                     *bitstreamReadDone = false;
    4024      813619 :                     *parametersAvailableForEditing = true;
    4025      813619 :                     return IVAS_ERR_OK;
    4026             :                 }
    4027             :             }
    4028             : 
    4029     2277997 :             nSamplesToRender = nSamplesPerChannel - *nSamplesRendered;
    4030             : 
    4031             :             /* check if we still need to prepare the renderer */
    4032     2277997 :             if ( hIvasDec->hasBeenPreparedRendering == false )
    4033             :             {
    4034      813619 :                 if ( ( error = IVAS_DEC_PrepareRenderer( hIvasDec ) ) != IVAS_ERR_OK )
    4035             :                 {
    4036           0 :                     return error;
    4037             :                 }
    4038             :             }
    4039             : 
    4040     2277997 :             if ( splitRendBits != NULL )
    4041             :             {
    4042             :                 /* Render head poses from time-scaled transport channels */
    4043           0 :                 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     2277997 :                 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     2277997 :             *nSamplesRendered += nSamplesRendered_loop;
    4058     2277997 :             update_voip_rendered20ms( hIvasDec, nSamplesRendered_loop );
    4059             :         }
    4060             :     }
    4061             : 
    4062     1779066 :     if ( hIvasDec->hasDecodedFirstGoodFrame && splitRendBits != NULL )
    4063             :     {
    4064             :         /* Analyse head poses over entire frame, generate ISAR metadata and maybe encode if split coded */
    4065           0 :         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           0 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    4072             :         {
    4073             : #ifndef DISABLE_LIMITER
    4074           0 :             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           0 :                 ivas_syn_output( p_head_pose_buf, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) pcmBuf );
    4081             :         }
    4082             :     }
    4083             : 
    4084     1779066 :     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     3351613 : 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     3351613 :     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           0 : 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           0 :     ivas_error error = IVAS_ERR_UNKNOWN;
    4153           0 :     int16_t nSamplesPerChannel = 0;
    4154             : 
    4155           0 :     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           0 :     for ( i = 0; i < BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES; ++i )
    4162             :     {
    4163           0 :         pp_head_pose_buf[i] = head_pose_buf[i];
    4164             :     }
    4165             : 
    4166           0 :     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     2279789 : static void update_voip_rendered20ms(
    4191             :     IVAS_DEC_HANDLE hIvasDec,
    4192             :     const int16_t nSamplesRendered )
    4193             : {
    4194             :     int16_t nSamplesRenderedTotal;
    4195             : 
    4196     2279789 :     nSamplesRenderedTotal = hIvasDec->hVoIP->nSamplesRendered20ms + nSamplesRendered;
    4197             : 
    4198             :     /* we have crossed a 20ms border, reset the time scaling done flag */
    4199     2279789 :     if ( nSamplesRenderedTotal >= hIvasDec->nSamplesFrame )
    4200             :     {
    4201      814218 :         hIvasDec->timeScalingDone = 0;
    4202             :     }
    4203             : 
    4204     2279789 :     hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal % hIvasDec->nSamplesFrame;
    4205             : 
    4206     2279789 :     return;
    4207             : }
    4208             : 
    4209             : 
    4210             : /*---------------------------------------------------------------------*
    4211             :  * IVAS_DEC_VoIP_Flush( )
    4212             :  *
    4213             :  * Function to flush remaining audio samples in VoIP
    4214             :  *---------------------------------------------------------------------*/
    4215             : 
    4216         111 : 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         111 :     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         111 :     hIvasDec->st_ivas->flushing = 1;
    4235             : #endif
    4236             : 
    4237         111 :     *nSamplesFlushed = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
    4238             : 
    4239         111 :     nSamplesToRender = (uint16_t) *nSamplesFlushed;
    4240             : 
    4241             :     /* render IVAS frames  */
    4242         111 :     error = IVAS_ERR_OK;
    4243         111 :     if ( nSamplesToRender > 0 && hIvasDec->st_ivas->ivas_format != MONO_FORMAT )
    4244             :     {
    4245          69 :         error = ivas_dec_render( hIvasDec->st_ivas, nSamplesToRender, &nSamplesFlushedLocal, &hIvasDec->nSamplesAvailableNext, pcm_type_API_to_internal( pcmType ), pcmBuf );
    4246             :     }
    4247             :     else
    4248             :     {
    4249          42 :         *nSamplesFlushed = 0;
    4250             :     }
    4251             : 
    4252             : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
    4253         111 :     hIvasDec->st_ivas->flushing = 0;
    4254             : #endif
    4255             : 
    4256         111 :     return error;
    4257             : }
    4258             : 
    4259             : 
    4260             : /*---------------------------------------------------------------------*
    4261             :  * IVAS_DEC_isRestartNeeded( )
    4262             :  *
    4263             :  *
    4264             :  *---------------------------------------------------------------------*/
    4265             : 
    4266      758928 : 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      758928 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    4273             :     {
    4274           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4275             :     }
    4276             : 
    4277      758928 :     *restartNeeded = hIvasDec->st_ivas->restartNeeded > 0;
    4278             : 
    4279      758928 :     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     1779177 : 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     1779177 :     if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL )
    4296             :     {
    4297           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4298             :     }
    4299             : 
    4300     1779177 :     *isEmpty = ( JB4_bufferedDataUnits( hIvasDec->hVoIP->hJBM ) == 0 ) && ( hIvasDec->nSamplesAvailableNext < nSamplesAsked );
    4301             : 
    4302     1779177 :     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         111 : static void ivas_destroy_handle_VoIP(
    4336             :     IVAS_DEC_VOIP *hVoIP /* i/o: VoIP decoder handle */
    4337             : )
    4338             : {
    4339         111 :     JB4_Destroy( &hVoIP->hJBM );
    4340             : 
    4341         111 :     if ( hVoIP->bs_conversion_buf != NULL )
    4342             :     {
    4343             : #define WMC_TOOL_SKIP
    4344             :         /* Bitstream conversion is not counted towards complexity and memory usage */
    4345         111 :         free( hVoIP->bs_conversion_buf );
    4346             : #undef WMC_TOOL_SKIP
    4347             :     }
    4348             : 
    4349         111 :     free( hVoIP );
    4350             : 
    4351         111 :     return;
    4352             : }
    4353             : 
    4354             : 
    4355             : #ifdef SUPPORT_JBM_TRACEFILE
    4356             : /*---------------------------------------------------------------------*
    4357             :  * store_JbmData()
    4358             :  *
    4359             :  * Store JBM trace data entry
    4360             :  *---------------------------------------------------------------------*/
    4361             : 
    4362      814435 : 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      814435 :     if ( hVoIP == NULL )
    4372             :     {
    4373           0 :         return;
    4374             :     }
    4375             : 
    4376      814435 :     JbmTraceData = &hVoIP->JbmTraceData;
    4377             : 
    4378      814435 :     JbmTraceData->systemTimestamp_ms = systemTimestamp_ms;
    4379      814435 :     JbmTraceData->extBufferedSamples = extBufferedSamples;
    4380      814435 :     JbmTraceData->lastDecodedWasActive = hVoIP->lastDecodedWasActive;
    4381      814435 :     JbmTraceData->output_Fs = output_Fs;
    4382      814435 :     JbmTraceData->dataUnit_flag = dataUnit != NULL;
    4383      814435 :     if ( dataUnit != NULL )
    4384             :     {
    4385      758928 :         JbmTraceData->sequenceNumber = dataUnit->sequenceNumber;
    4386      758928 :         JbmTraceData->timeStamp = dataUnit->timeStamp;
    4387      758928 :         JbmTraceData->rcvTime = dataUnit->rcvTime;
    4388      758928 :         JbmTraceData->partial_frame = dataUnit->partial_frame;
    4389      758928 :         JbmTraceData->partialCopyOffset = dataUnit->partialCopyOffset;
    4390             :     }
    4391             : 
    4392      814435 :     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        1939 : 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        1939 :     fprintf( stdout, "\n" );
    4456             : 
    4457             :     /*-----------------------------------------------------------------*
    4458             :      * Print output sampling frequency
    4459             :      *-----------------------------------------------------------------*/
    4460             : 
    4461        1939 :     fprintf( stdout, "Output sampling rate:   %d Hz\n", st_ivas->hDecoderConfig->output_Fs );
    4462             : 
    4463             :     /*-----------------------------------------------------------------*
    4464             :      * Print bitrate
    4465             :      *-----------------------------------------------------------------*/
    4466             : 
    4467        1939 :     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        1939 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    4492             :     {
    4493          64 :         if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    4494             :         {
    4495           3 :             fprintf( stdout, "Output configuration:   mono EVS bit-exact decoding to stereo\n" );
    4496           3 :             fprintf( stdout, "Non-diegetic panning:   %.2f\n", st_ivas->hDecoderConfig->non_diegetic_pan_gain * 90.f );
    4497             :         }
    4498             :         else
    4499             :         {
    4500          61 :             output_config = st_ivas->hDecoderConfig->output_config;
    4501          61 :             if ( output_config != IVAS_AUDIO_CONFIG_MONO )
    4502             :             {
    4503           0 :                 get_channel_config( output_config, &config_str[0] );
    4504           0 :                 fprintf( stdout, "Output configuration:   mono EVS bit-exact decoding rendering to %s\n", config_str );
    4505             :             }
    4506             :             else
    4507             :             {
    4508          61 :                 fprintf( stdout, "Output configuration:   mono EVS bit-exact decoding\n" );
    4509             :             }
    4510             :         }
    4511             :     }
    4512             :     else
    4513             :     {
    4514        1875 :         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        1875 :         output_config = st_ivas->hDecoderConfig->output_config;
    4559        1875 :         get_channel_config( output_config, &config_str[0] );
    4560        1875 :         fprintf( stdout, "Output configuration:   %s\n", config_str );
    4561             : 
    4562        1875 :         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         633 :             fprintf( stdout, "Render framesize:       %d ms\n", get_render_frame_size_ms( st_ivas->hDecoderConfig->render_num_subframes ) );
    4565             :         }
    4566        1875 :         if ( st_ivas->hDecoderConfig->Opt_HRTF_binary )
    4567             :         {
    4568          72 :             fprintf( stdout, "HRIR/BRIR file:         ON\n" );
    4569             :         }
    4570             : 
    4571        1875 :         if ( st_ivas->hDecoderConfig->Opt_RendConfigCustom )
    4572             :         {
    4573          96 :             fprintf( stdout, "Renderer config. file:  ON\n" );
    4574             :         }
    4575             : 
    4576        1875 :         if ( st_ivas->hDecoderConfig->Opt_Headrotation )
    4577             :         {
    4578         249 :             fprintf( stdout, "Head rotation:          ON\n" );
    4579             :         }
    4580             : 
    4581        1875 :         if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    4582             :         {
    4583          93 :             fprintf( stdout, "External orientation:   ON\n" );
    4584             :         }
    4585             : 
    4586        1875 :         if ( st_ivas->hDecoderConfig->orientation_tracking != IVAS_HEAD_ORIENT_TRK_NONE )
    4587             :         {
    4588          48 :             switch ( st_ivas->hDecoderConfig->orientation_tracking )
    4589             :             {
    4590          36 :                 case IVAS_HEAD_ORIENT_TRK_AVG:
    4591          36 :                     fprintf( stdout, "Orientation tracking:   AVG\n" );
    4592          36 :                     break;
    4593           0 :                 case IVAS_HEAD_ORIENT_TRK_REF:
    4594           0 :                     fprintf( stdout, "Orientation tracking:   REF\n" );
    4595           0 :                     break;
    4596           6 :                 case IVAS_HEAD_ORIENT_TRK_REF_VEC:
    4597           6 :                     fprintf( stdout, "Orientation tracking:   REF_VEC\n" );
    4598           6 :                     break;
    4599           6 :                 case IVAS_HEAD_ORIENT_TRK_REF_VEC_LEV:
    4600           6 :                     fprintf( stdout, "Orientation tracking:   REF_VEC_LEV\n" );
    4601           6 :                     break;
    4602           0 :                 default:
    4603           0 :                     break;
    4604             :             }
    4605        1827 :         }
    4606             : 
    4607        1875 :         if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    4608             :         {
    4609           3 :             fprintf( stdout, "Non-diegetic panning:   %.2f\n", st_ivas->hDecoderConfig->non_diegetic_pan_gain * 90.f );
    4610             :         }
    4611             : 
    4612        1875 :         if ( st_ivas->hDecoderConfig->Opt_dpid_on )
    4613             :         {
    4614           6 :             fprintf( stdout, "Directivity pattern:    ON\n" );
    4615             :         }
    4616             : 
    4617        1875 :         if ( st_ivas->hDecoderConfig->Opt_aeid_on )
    4618             :         {
    4619          12 :             fprintf( stdout, "Acoustic environment ID:ON\n" );
    4620             :         }
    4621             : 
    4622        1875 :         if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on )
    4623             :         {
    4624          39 :             fprintf( stdout, "Objects editing :       ON\n" );
    4625             :         }
    4626             :     }
    4627             : 
    4628             :     /*-----------------------------------------------------------------*
    4629             :      * Print TSM mode info
    4630             :      *-----------------------------------------------------------------*/
    4631             : 
    4632        1939 :     if ( st_ivas->hDecoderConfig->Opt_tsm )
    4633             :     {
    4634         111 :         fprintf( stdout, "TSM mode:               ON\n" );
    4635             :     }
    4636             : 
    4637        1939 :     return IVAS_ERR_OK;
    4638             : }
    4639             : 
    4640             : 
    4641             : /*---------------------------------------------------------------------*
    4642             :  * IVAS_DEC_PrintConfig( )
    4643             :  *
    4644             :  * Print decoder set-up info
    4645             :  *---------------------------------------------------------------------*/
    4646             : 
    4647        1939 : ivas_error IVAS_DEC_PrintConfig(
    4648             :     const IVAS_DEC_HANDLE hIvasDec,
    4649             :     const bool quietModeEnabled,
    4650             :     const bool voipMode )
    4651             : {
    4652        1939 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    4653             :     {
    4654           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4655             :     }
    4656             : 
    4657        1939 :     printConfigInfo_dec( hIvasDec->st_ivas, hIvasDec->bitstreamformat, voipMode, quietModeEnabled );
    4658             : 
    4659        1939 :     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        1939 : void IVAS_DEC_PrintDisclaimer( void )
    4737             : {
    4738        1939 :     print_disclaimer( stderr );
    4739             : 
    4740        1939 :     return;
    4741             : }
    4742             : 
    4743             : 
    4744             : /*---------------------------------------------------------------------*
    4745             :  * evs_dec_main( )
    4746             :  *
    4747             :  * EVS codec main decoder fucntion
    4748             :  *---------------------------------------------------------------------*/
    4749             : 
    4750      149096 : 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      149096 :     hCoreCoder = st_ivas->hSCE[0]->hCoreCoder;
    4760      149096 :     hCoreCoder[0]->total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    4761      149096 :     nOutSamples = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
    4762             : 
    4763      149096 :     mdct_switching_dec( hCoreCoder[0] );
    4764             : 
    4765      149096 :     p_output[0] = st_ivas->p_output_f[0];
    4766             : 
    4767             :     /* run the main EVS decoding routine */
    4768      149096 :     if ( hCoreCoder[0]->codec_mode == MODE1 )
    4769             :     {
    4770       86251 :         if ( hCoreCoder[0]->Opt_AMR_WB )
    4771             :         {
    4772        3255 :             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       82996 :             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       62845 :         if ( hCoreCoder[0]->bfi == 0 )
    4788             :         {
    4789       62370 :             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         475 :         else if ( hCoreCoder[0]->bfi == 2 )
    4795             :         {
    4796           0 :             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         475 :             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      149096 :     st_ivas->BER_detect = hCoreCoder[0]->BER_detect;
    4811             : 
    4812      149096 :     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        3268 :             ivas_syn_output( &p_output[0], nOutSamples, 1, pcm_buf_local );
    4820        3268 :         mvs2r( pcm_buf_local, p_output[0], nOutSamples );
    4821             :     }
    4822             : 
    4823      149096 :     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         111 : 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         111 :     switch ( input_format )
    4934             :     {
    4935         111 :         case IVAS_DEC_INPUT_FORMAT_G192:
    4936         111 :             *bitstream_format_internal = is_voip_enabled ? VOIP_G192_RTP : G192;
    4937         111 :             *sdp_hf_only = 0;
    4938         111 :             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           0 :         case IVAS_DEC_INPUT_FORMAT_RTPDUMP_HF:
    4949           0 :             assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
    4950           0 :             *bitstream_format_internal = VOIP_RTPDUMP;
    4951           0 :             *sdp_hf_only = 1;
    4952           0 :             break;
    4953           0 :         default:
    4954           0 :             return IVAS_ERR_INVALID_BITSTREAM;
    4955             :             break;
    4956             :     }
    4957             : 
    4958         111 :     return IVAS_ERR_OK;
    4959             : }
    4960             : 
    4961             : 
    4962             : /*---------------------------------------------------------------------*
    4963             :  * apa_setup()
    4964             :  *
    4965             :  * Setup APA decoder
    4966             :  *---------------------------------------------------------------------*/
    4967             : 
    4968       62808 : 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       62808 :     l_ts = (uint16_t) hIvasDec->st_ivas->hTcBuffer->n_samples_granularity;
    4976             : 
    4977       62808 :     if ( !isInitialized_voip )
    4978             :     {
    4979             :         DECODER_CONFIG_HANDLE hDecoderConfig;
    4980             :         uint16_t wss, css;
    4981             :         float startQuality;
    4982             : 
    4983         111 :         startQuality = hIvasDec->tsm_quality;
    4984         111 :         hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
    4985             : 
    4986         111 :         if ( hDecoderConfig->output_Fs == 8000 )
    4987             :         {
    4988           0 :             wss = 1;
    4989           0 :             css = 1;
    4990             :         }
    4991         111 :         else if ( hDecoderConfig->output_Fs == 16000 )
    4992             :         {
    4993           9 :             wss = 2;
    4994           9 :             css = 1;
    4995             :         }
    4996         102 :         else if ( hDecoderConfig->output_Fs == 32000 )
    4997             :         {
    4998          30 :             wss = 4;
    4999          30 :             css = 2;
    5000             :         }
    5001          72 :         else if ( hDecoderConfig->output_Fs == 48000 )
    5002             :         {
    5003          72 :             wss = 6;
    5004          72 :             css = 3;
    5005             :         }
    5006             :         else
    5007             :         {
    5008           0 :             return IVAS_ERR_INIT_ERROR;
    5009             :         }
    5010             : 
    5011         222 :         if ( apa_init( &hIvasDec->hTimeScaler, nTransportChannels ) != IVAS_ERR_OK ||
    5012         222 :              apa_set_rate( hIvasDec->hTimeScaler, hDecoderConfig->output_Fs ) != 0 ||
    5013         222 :              apa_set_complexity_options( hIvasDec->hTimeScaler, wss, css ) != 0 ||
    5014         222 :              apa_set_quality( hIvasDec->hTimeScaler, startQuality, 4, 4 ) != 0 ||
    5015         111 :              apa_set_renderer_granularity( hIvasDec->hTimeScaler, l_ts ) != 0 )
    5016             :         {
    5017           0 :             return IVAS_ERR_INIT_ERROR;
    5018             :         }
    5019             : 
    5020         111 :         if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    5021             :         {
    5022           3 :             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       62697 :         if ( apa_reconfigure( hIvasDec->hTimeScaler, nTransportChannels, l_ts ) != 0 )
    5031             :         {
    5032           0 :             return IVAS_ERR_INIT_ERROR;
    5033             :         }
    5034             :     }
    5035             : 
    5036       62808 :     hIvasDec->nTransportChannelsOld = nTransportChannels;
    5037             : 
    5038       62808 :     return IVAS_ERR_OK;
    5039             : }
    5040             : 
    5041             : 
    5042             : /*---------------------------------------------------------------------*
    5043             :  * IVAS_DEC_GetSplitRendBitstreamHeader()
    5044             :  *
    5045             :  *
    5046             :  *---------------------------------------------------------------------*/
    5047             : 
    5048           0 : 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           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5058             :     {
    5059           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5060             :     }
    5061             : 
    5062           0 :     *pCodec = hIvasDec->st_ivas->hRenderConfig->split_rend_config.codec;
    5063           0 :     *pCodec_frame_size_ms = hIvasDec->st_ivas->hRenderConfig->split_rend_config.codec_frame_size_ms;
    5064           0 :     *poseCorrection = hIvasDec->st_ivas->hRenderConfig->split_rend_config.poseCorrectionMode;
    5065           0 :     *pIsar_frame_size_ms = hIvasDec->st_ivas->hRenderConfig->split_rend_config.isar_frame_size_ms;
    5066           0 :     *pLc3plusHighRes = hIvasDec->st_ivas->hRenderConfig->split_rend_config.lc3plus_highres;
    5067             : 
    5068           0 :     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    36441712 : static void *pcm_buffer_offset(
    5137             :     void *buffer,
    5138             :     const IVAS_DEC_PCM_TYPE pcmType,
    5139             :     const int32_t offset )
    5140             : {
    5141    36441712 :     switch ( pcmType )
    5142             :     {
    5143           0 :         case IVAS_DEC_PCM_FLOAT:
    5144             :         {
    5145           0 :             float *tmpBuf = (float *) buffer;
    5146           0 :             return (void *) ( tmpBuf + offset );
    5147             :         }
    5148             :         break;
    5149    36441712 :         case IVAS_DEC_PCM_INT16:
    5150             :         {
    5151    36441712 :             int16_t *tmpBuf = (int16_t *) buffer;
    5152    36441712 :             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        1792 : 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        1792 :     error = IVAS_ERR_OK;
    5177        1792 :     switch ( pcmType )
    5178             :     {
    5179           0 :         case IVAS_DEC_PCM_FLOAT:
    5180           0 :             set_zero( (float *) buffer, nZeroSamples );
    5181           0 :             break;
    5182        1792 :         case IVAS_DEC_PCM_INT16:
    5183        1792 :             set_s( (int16_t *) buffer, 0, nZeroSamples );
    5184        1792 :             break;
    5185           0 :         default:
    5186           0 :             error = IVAS_ERR_INTERNAL;
    5187             :     }
    5188             : 
    5189        1792 :     return error;
    5190             : }
    5191             : 
    5192             : 
    5193             : /*---------------------------------------------------------------------*
    5194             :  * pcm_type_API_to_internal()
    5195             :  *
    5196             :  *
    5197             :  *---------------------------------------------------------------------*/
    5198             : 
    5199    34168355 : PCM_RESOLUTION pcm_type_API_to_internal(
    5200             :     const IVAS_DEC_PCM_TYPE pcmType )
    5201             : {
    5202             :     PCM_RESOLUTION pcm_resolution;
    5203    34168355 :     pcm_resolution = PCM_NOT_KNOW;
    5204             : 
    5205    34168355 :     switch ( pcmType )
    5206             :     {
    5207           0 :         case IVAS_DEC_PCM_FLOAT:
    5208           0 :             pcm_resolution = PCM_FLOAT32;
    5209           0 :             break;
    5210    34168355 :         case IVAS_DEC_PCM_INT16:
    5211    34168355 :             pcm_resolution = PCM_INT16;
    5212    34168355 :             break;
    5213           0 :         default:
    5214           0 :             pcm_resolution = PCM_NOT_KNOW;
    5215             :     }
    5216             : 
    5217    34168355 :     return pcm_resolution;
    5218             : }
    5219             : 
    5220             : 
    5221             : /*-------------------------------------------------------------------*
    5222             :  * ivas_create_handle_isar()
    5223             :  *
    5224             :  * Initialize IVAS decoder split-rendering handle
    5225             :  *-------------------------------------------------------------------*/
    5226             : 
    5227           0 : 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           0 :     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           0 :     isar_init_split_rend_handles( &hSplitBinRend->splitrend );
    5240             : 
    5241           0 :     hSplitBinRend->hMultiBinTdData = NULL;
    5242           0 :     for ( i = 0; i < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ++i )
    5243             :     {
    5244           0 :         hSplitBinRend->hMultiBinCldfbData[i] = NULL;
    5245             :     }
    5246           0 :     hSplitBinRend->hCldfbDataOut = NULL;
    5247           0 :     hSplitBinRend->numTdSamplesPerChannelCached = 0;
    5248             : 
    5249           0 :     *hSplitBinRend_out = hSplitBinRend;
    5250             : 
    5251           0 :     return IVAS_ERR_OK;
    5252             : }
    5253             : 
    5254             : 
    5255             : /*-------------------------------------------------------------------*
    5256             :  * ivas_destroy_handle_isar()
    5257             :  *
    5258             :  * destroy IVAS decoder split rend handle
    5259             :  *-------------------------------------------------------------------*/
    5260             : 
    5261        1939 : 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        1939 :     if ( *hSplitBinRend != NULL )
    5268             :     {
    5269           0 :         if ( ( *hSplitBinRend )->hMultiBinTdData != NULL )
    5270             :         {
    5271           0 :             ivas_TD_RINGBUF_Close( &( *hSplitBinRend )->hMultiBinTdData );
    5272             :         }
    5273           0 :         for ( i = 0; i < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ++i )
    5274             :         {
    5275           0 :             if ( ( *hSplitBinRend )->hMultiBinCldfbData[i] != NULL )
    5276             :             {
    5277           0 :                 ivas_CLDFB_RINGBUF_Close( &( *hSplitBinRend )->hMultiBinCldfbData[i] );
    5278             :             }
    5279             :         }
    5280             : 
    5281           0 :         ISAR_PRE_REND_close( &( *hSplitBinRend )->splitrend, NULL );
    5282             : 
    5283           0 :         if ( ( *hSplitBinRend )->hCldfbDataOut != NULL )
    5284             :         {
    5285           0 :             free( ( *hSplitBinRend )->hCldfbDataOut );
    5286           0 :             ( *hSplitBinRend )->hCldfbDataOut = NULL;
    5287             :         }
    5288             : 
    5289           0 :         free( ( *hSplitBinRend ) );
    5290           0 :         ( *hSplitBinRend ) = NULL;
    5291             :     }
    5292             : 
    5293        1939 :     return;
    5294             : }
    5295             : 
    5296             : 
    5297             : /*---------------------------------------------------------------------*
    5298             :  * IVAS_DEC_is_split_rendering_enabled()
    5299             :  *
    5300             :  *
    5301             :  *---------------------------------------------------------------------*/
    5302             : 
    5303             : /*! r: decoder error code */
    5304        3878 : 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        3878 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5312             :     {
    5313           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5314             :     }
    5315             : 
    5316        3878 :     st_ivas = hIvasDec->st_ivas;
    5317             : 
    5318        3878 :     *isSplitRend = is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig );
    5319             : 
    5320        3878 :     return IVAS_ERR_OK;
    5321             : }
    5322             : 
    5323             : 
    5324             : /*-------------------------------------------------------------------*
    5325             :  * ivas_dec_reconfig_split_rend()
    5326             :  *
    5327             :  * IVAS decoder split rend reconfig
    5328             :  *-------------------------------------------------------------------*/
    5329             : 
    5330           0 : 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           0 :     hSplitRendWrapper = &st_ivas->hSplitBinRend->splitrend;
    5339           0 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    5340           0 :     cldfb_in_flag = 0;
    5341             : 
    5342           0 :     if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
    5343           0 :          st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    5344           0 :          st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC ||
    5345           0 :          st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    5346             :     {
    5347           0 :         cldfb_in_flag = 1;
    5348             :     }
    5349             : 
    5350           0 :     ISAR_PRE_REND_GetMultiBinPoseData( &st_ivas->hRenderConfig->split_rend_config, &hSplitRendWrapper->multiBinPoseData, ( st_ivas->hHeadTrackData != NULL ) ? st_ivas->hHeadTrackData->sr_pose_pred_axis : DEFAULT_AXIS );
    5351             : 
    5352           0 :     isCldfbNeeded = 0;
    5353             : 
    5354           0 :     if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ivas_format == SBA_ISM_FORMAT ) ||
    5355           0 :          ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    5356             :     {
    5357           0 :         cldfb_in_flag = 0;
    5358             :     }
    5359             : 
    5360           0 :     if ( st_ivas->renderer_type != RENDERER_DISABLE )
    5361             :     {
    5362           0 :         if ( cldfb_in_flag == 0 )
    5363             :         {
    5364           0 :             isCldfbNeeded = 1;
    5365             :         }
    5366           0 :         else if ( st_ivas->hRenderConfig->split_rend_config.codec == ISAR_SPLIT_REND_CODEC_LC3PLUS && cldfb_in_flag )
    5367             :         {
    5368           0 :             isCldfbNeeded = 1;
    5369             :         }
    5370           0 :         else if ( pcm_out_flag && cldfb_in_flag )
    5371             :         {
    5372           0 :             isCldfbNeeded = 1;
    5373             :         }
    5374             :     }
    5375           0 :     else if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    5376             :     {
    5377           0 :         isCldfbNeeded = 1;
    5378             :     }
    5379             : 
    5380           0 :     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           0 :     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           0 :     if ( ( st_ivas->renderer_type != RENDERER_BINAURAL_OBJECTS_TD ) &&
    5437           0 :          ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV || st_ivas->ivas_format != SBA_ISM_FORMAT ) &&
    5438           0 :          !( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    5439             :     {
    5440           0 :         for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    5441             :         {
    5442           0 :             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           0 :     return IVAS_ERR_OK;
    5451             : }
    5452             : 
    5453             : 
    5454             : /*-------------------------------------------------------------------*
    5455             :  * ivas_dec_split_rend_cldfb_in()
    5456             :  *
    5457             :  *
    5458             :  *-------------------------------------------------------------------*/
    5459             : 
    5460           0 : static int16_t ivas_dec_split_rend_cldfb_in(
    5461             :     const RENDERER_TYPE renderer_type /* i  : renderer type     */
    5462             : )
    5463             : {
    5464           0 :     if ( renderer_type == RENDERER_BINAURAL_FASTCONV ||
    5465           0 :          renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    5466           0 :          renderer_type == RENDERER_BINAURAL_PARAMETRIC ||
    5467             :          renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    5468             :     {
    5469           0 :         return 1;
    5470             :     }
    5471             :     else
    5472             :     {
    5473           0 :         return 0;
    5474             :     }
    5475             : }
    5476             : 
    5477             : 
    5478             : /*-------------------------------------------------------------------*
    5479             :  * ivas_dec_init_split_rend()
    5480             :  *
    5481             :  * IVAS decoder split rendering initialization
    5482             :  *-------------------------------------------------------------------*/
    5483             : 
    5484           0 : 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           0 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    5494           0 :     cldfb_in_flag = 0;
    5495             : 
    5496           0 :     cldfb_in_flag = ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type );
    5497             : 
    5498           0 :     ISAR_PRE_REND_GetMultiBinPoseData( &st_ivas->hRenderConfig->split_rend_config, &st_ivas->hSplitBinRend->splitrend.multiBinPoseData, ( st_ivas->hHeadTrackData != NULL ) ? st_ivas->hHeadTrackData->sr_pose_pred_axis : DEFAULT_AXIS );
    5499             : 
    5500           0 :     num_poses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    5501           0 :     assert( num_poses <= MAX_HEAD_ROT_POSES );
    5502             : 
    5503           0 :     if ( cldfb_in_flag )
    5504             :     {
    5505           0 :         for ( i = 0; i < num_poses * BINAURAL_CHANNELS; ++i )
    5506             :         {
    5507             :             /* note: this is intra-frame heap memory */
    5508           0 :             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           0 :         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           0 :     if ( cldfb_in_flag == 1 && ( st_ivas->hSplitBinRend->splitrend.multiBinPoseData.poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE ) )
    5523             :     {
    5524           0 :         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           0 :     mixed_td_cldfb_flag = 0;
    5531           0 :     if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ivas_format == SBA_ISM_FORMAT ) ||
    5532           0 :          ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    5533             :     {
    5534           0 :         mixed_td_cldfb_flag = 1;
    5535             :     }
    5536             : 
    5537           0 :     error = ISAR_PRE_REND_open( &st_ivas->hSplitBinRend->splitrend, &st_ivas->hRenderConfig->split_rend_config, st_ivas->hDecoderConfig->output_Fs, cldfb_in_flag, pcm_out_flag, (int16_t) st_ivas->hDecoderConfig->render_num_subframes, mixed_td_cldfb_flag );
    5538             : 
    5539           0 :     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        3878 : 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        3878 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5558             :     {
    5559           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5560             :     }
    5561             : 
    5562        3878 :     st_ivas = hIvasDec->st_ivas;
    5563             : 
    5564        3878 :     *isSplitCoded = 0;
    5565             : 
    5566        3878 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
    5567        3878 :          ( 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           0 :         *isSplitCoded = 1;
    5570             :     }
    5571             : 
    5572        3878 :     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