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

Generated by: LCOV version 1.14