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 @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 1330 1642 81.0 %
Date: 2025-10-27 07:01:45 Functions: 82 88 93.2 %

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

Generated by: LCOV version 1.14