LCOV - code coverage report
Current view: top level - lib_dec - lib_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 1076 1596 67.4 %
Date: 2025-07-09 08:36:12 Functions: 67 81 82.7 %

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

Generated by: LCOV version 1.14