LCOV - code coverage report
Current view: top level - lib_dec - lib_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ 0c62f5312a76f89f3e6d6ab9a8d50516c9ab4059 Lines: 1427 1855 76.9 %
Date: 2025-12-17 10:49:08 Functions: 85 95 89.5 %

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

Generated by: LCOV version 1.14