LCOV - code coverage report
Current view: top level - lib_dec - lib_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ d40ed931793b6a50d91fe879dc4ec795971aff53 Lines: 1231 1888 65.2 %
Date: 2025-09-18 03:57:22 Functions: 60 75 80.0 %

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

Generated by: LCOV version 1.14