LCOV - code coverage report
Current view: top level - lib_enc - lib_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 695 913 76.1 %
Date: 2025-06-27 02:59:36 Functions: 36 40 90.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 <math.h>
      34             : #include <assert.h>
      35             : #include <stdio.h>
      36             : #include <string.h>
      37             : #include "options.h"
      38             : #ifdef DEBUGGING
      39             : #include "debug.h"
      40             : #endif
      41             : #include "lib_enc.h"
      42             : #include "ivas_prot_fx.h"
      43             : #include "prot_fx.h"
      44             : #include "prot_fx_enc.h"
      45             : #include "wmc_auto.h"
      46             : #include "ivas_rom_enc.h"
      47             : 
      48             : /*---------------------------------------------------------------------*
      49             :  * Local struct
      50             :  *---------------------------------------------------------------------*/
      51             : 
      52             : struct IVAS_ENC
      53             : {
      54             :     Encoder_Struct *st_ivas;
      55             :     ENC_CORE_HANDLE hCoreCoder;
      56             :     bool isConfigured;
      57             : #ifdef DEBUGGING
      58             :     bool cmd_stereo;
      59             : #endif
      60             :     bool switchingActive; /* flag for configuration changes during encoding - currently only used with mono */
      61             :     Word16 Opt_RF_ON_loc;
      62             :     Word16 rf_fec_offset_loc;
      63             :     bool ismMetadataProvided[MAX_NUM_OBJECTS];
      64             :     bool maxBandwidthUser;              /* Was a specific max bandwith selected by the user? */
      65             :     IVAS_ENC_BANDWIDTH newBandwidthApi; /* maximum encoded bandwidth, as set on API level */
      66             :     bool extMetadataApi;                /* External metadata requested, to be checked against current bit rate */
      67             : };
      68             : 
      69             : /*---------------------------------------------------------------------*
      70             :  * Local functions
      71             :  *---------------------------------------------------------------------*/
      72             : 
      73             : // static ivas_error configureEncoder_fx( IVAS_ENC_HANDLE hIvasEnc, const Word32 inputFs, const Word32 initBitrate, const IVAS_ENC_BANDWIDTH initBandwidth, const IVAS_ENC_DTX_CONFIG dtxConfig, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
      74             : static ivas_error setBandwidth_fx( IVAS_ENC_HANDLE hIvasEnc, const IVAS_ENC_BANDWIDTH maxBandwidth );
      75             : static ivas_error setChannelAwareConfig_fx( IVAS_ENC_HANDLE hIvasEnc, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
      76             : static ivas_error sanitizeBandwidth_fx( const IVAS_ENC_HANDLE hIvasEnc );
      77             : static ivas_error sanitizeBitrateISM_fx( const ENCODER_CONFIG_HANDLE hEncoderConfig, const bool extMetadataApi );
      78             : static Word16 getInputBufferSize_fx( const Encoder_Struct *st_ivas );
      79             : static ivas_error configureEncoder( IVAS_ENC_HANDLE hIvasEnc, const Word32 inputFs, const Word32 initBitrate, const IVAS_ENC_BANDWIDTH initBandwidth, const IVAS_ENC_DTX_CONFIG dtxConfig, const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig );
      80             : static ivas_error setBitrate( IVAS_ENC_HANDLE hIvasEnc, const Word32 totalBitrate );
      81             : static ivas_error doCommonConfigureChecks( IVAS_ENC_HANDLE hIvasEnc );
      82             : static ivas_error doCommonSetterChecks( IVAS_ENC_HANDLE hIvasEnc );
      83             : static void init_encoder_config( ENCODER_CONFIG_HANDLE hEncoderConfig );
      84             : static void resetIsmMetadataProvidedFlags( IVAS_ENC_HANDLE hIvasEnc );
      85             : static ivas_error bandwidthApiToInternal( const IVAS_ENC_BANDWIDTH maxBandwidth, Word16 *internalMaxBandwidth );
      86             : static ivas_error fecIndicatorApiToInternal( const IVAS_ENC_FEC_INDICATOR fecIndicator, Word16 *fecIndicatorInternal );
      87             : #ifdef DEBUGGING
      88             : static ivas_error forcedModeApiToInternal( IVAS_ENC_FORCED_MODE forcedMode, int16_t *forcedModeInternal );
      89             : #endif
      90             : 
      91             : 
      92             : /*---------------------------------------------------------------------*
      93             :  * IVAS_ENC_Open()
      94             :  *
      95             :  * Open IVAS encoder
      96             :  *---------------------------------------------------------------------*/
      97             : 
      98           0 : ivas_error IVAS_ENC_Open(
      99             :     IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to an encoder handle to be opened */
     100             : )
     101             : {
     102             :     Encoder_Struct *st_ivas;
     103             : 
     104           0 :     if ( phIvasEnc == NULL )
     105             :     {
     106           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     107             :     }
     108             : 
     109             :     /*-----------------------------------------------------------------*
     110             :      * Allocate and initialize IVAS application encoder handle
     111             :      *-----------------------------------------------------------------*/
     112             : 
     113           0 :     if ( ( *phIvasEnc = (IVAS_ENC_HANDLE) malloc( sizeof( struct IVAS_ENC ) ) ) == NULL )
     114             :     {
     115           0 :         return IVAS_ERR_FAILED_ALLOC;
     116             :     }
     117             : 
     118           0 :     ( *phIvasEnc )->hCoreCoder = NULL;
     119           0 :     ( *phIvasEnc )->isConfigured = false;
     120             : #ifdef DEBUGGING
     121             :     ( *phIvasEnc )->cmd_stereo = false;
     122             : #endif
     123           0 :     ( *phIvasEnc )->switchingActive = false;
     124           0 :     ( *phIvasEnc )->maxBandwidthUser = false;
     125           0 :     resetIsmMetadataProvidedFlags( *phIvasEnc );
     126             : 
     127             :     /*-----------------------------------------------------------------*
     128             :      * Allocate IVAS-codec encoder state
     129             :      *-----------------------------------------------------------------*/
     130             : 
     131           0 :     if ( ( ( *phIvasEnc )->st_ivas = (Encoder_Struct *) malloc( sizeof( Encoder_Struct ) ) ) == NULL )
     132             :     {
     133           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS encoder structure" );
     134             :     }
     135             : 
     136           0 :     if ( ( ( *phIvasEnc )->st_ivas->hEncoderConfig = (ENCODER_CONFIG_HANDLE) malloc( sizeof( ENCODER_CONFIG ) ) ) == NULL )
     137             :     {
     138           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Encoder config structure" );
     139             :     }
     140             : 
     141             :     /*-----------------------------------------------------------------*
     142             :      * Initialize IVAS-codec encoder state
     143             :      *-----------------------------------------------------------------*/
     144             : 
     145           0 :     st_ivas = ( *phIvasEnc )->st_ivas;
     146             : 
     147             :     /* initialize encoder Config. handle */
     148           0 :     init_encoder_config( st_ivas->hEncoderConfig );
     149             : 
     150             :     /* initialize pointers to handles to NULL */
     151           0 :     ivas_initialize_handles_enc_fx( st_ivas );
     152             : 
     153           0 :     st_ivas->ind_list = NULL;
     154           0 :     st_ivas->ind_list_metadata = NULL;
     155             : 
     156             :     /* set high-level parameters */
     157           0 :     st_ivas->mc_mode = MC_MODE_NONE;
     158           0 :     st_ivas->ism_mode = ISM_MODE_NONE;
     159           0 :     st_ivas->sba_analysis_order = 0;
     160           0 :     move16();
     161             : 
     162           0 :     return IVAS_ERR_OK;
     163             : }
     164             : 
     165         624 : ivas_error IVAS_ENC_Open_fx(
     166             :     IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to an encoder handle to be opened */
     167             : )
     168             : {
     169             :     Encoder_Struct *st_ivas;
     170             : 
     171         624 :     IF( phIvasEnc == NULL )
     172             :     {
     173           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     174             :     }
     175             : 
     176             :     /*-----------------------------------------------------------------*
     177             :      * Allocate and initialize IVAS application encoder handle
     178             :      *-----------------------------------------------------------------*/
     179             : 
     180         624 :     IF( ( *phIvasEnc = (IVAS_ENC_HANDLE) malloc( sizeof( struct IVAS_ENC ) ) ) == NULL )
     181             :     {
     182           0 :         return IVAS_ERR_FAILED_ALLOC;
     183             :     }
     184             : 
     185         624 :     ( *phIvasEnc )->hCoreCoder = NULL;
     186         624 :     ( *phIvasEnc )->isConfigured = false;
     187             : #ifdef DEBUGGING
     188             :     ( *phIvasEnc )->cmd_stereo = false;
     189             : #endif
     190         624 :     ( *phIvasEnc )->switchingActive = false;
     191         624 :     ( *phIvasEnc )->maxBandwidthUser = false;
     192         624 :     resetIsmMetadataProvidedFlags( *phIvasEnc );
     193             : 
     194             :     /*-----------------------------------------------------------------*
     195             :      * Allocate IVAS-codec encoder state
     196             :      *-----------------------------------------------------------------*/
     197             : 
     198         624 :     IF( ( ( *phIvasEnc )->st_ivas = (Encoder_Struct *) malloc( sizeof( Encoder_Struct ) ) ) == NULL )
     199             :     {
     200           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS encoder structure" );
     201             :     }
     202             : 
     203         624 :     IF( ( ( *phIvasEnc )->st_ivas->hEncoderConfig = (ENCODER_CONFIG_HANDLE) malloc( sizeof( ENCODER_CONFIG ) ) ) == NULL )
     204             :     {
     205           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Encoder config structure" );
     206             :     }
     207             : 
     208             :     /*-----------------------------------------------------------------*
     209             :      * Initialize IVAS-codec encoder state
     210             :      *-----------------------------------------------------------------*/
     211             : 
     212         624 :     st_ivas = ( *phIvasEnc )->st_ivas;
     213             : 
     214             :     /* initialize encoder Config. handle */
     215         624 :     init_encoder_config( st_ivas->hEncoderConfig );
     216             : 
     217             :     /* initialize pointers to handles to NULL */
     218         624 :     ivas_initialize_handles_enc_fx( st_ivas );
     219             : 
     220         624 :     st_ivas->ind_list = NULL;
     221         624 :     st_ivas->ind_list_metadata = NULL;
     222             : 
     223             :     /* set high-level parameters */
     224         624 :     st_ivas->mc_mode = MC_MODE_NONE;
     225         624 :     st_ivas->ism_mode = ISM_MODE_NONE;
     226         624 :     st_ivas->sba_analysis_order = 0;
     227         624 :     move16();
     228             : 
     229         624 :     return IVAS_ERR_OK;
     230             : }
     231             : /*---------------------------------------------------------------------*
     232             :  * IVAS_ENC_Close()
     233             :  *
     234             :  *
     235             :  *---------------------------------------------------------------------*/
     236             : 
     237         624 : void IVAS_ENC_Close(
     238             :     IVAS_ENC_HANDLE *phIvasEnc /* i/o: pointer to IVAS encoder handle */
     239             : )
     240             : {
     241             :     /* Free all memory */
     242         624 :     test();
     243         624 :     IF( phIvasEnc == NULL || *phIvasEnc == NULL )
     244             :     {
     245           0 :         return;
     246             :     }
     247             : 
     248         624 :     IF( ( *phIvasEnc )->isConfigured )
     249             :     {
     250         624 :         ivas_destroy_enc_fx( ( *phIvasEnc )->st_ivas );
     251             :     }
     252             :     ELSE
     253             :     {
     254           0 :         IF( ( *phIvasEnc )->st_ivas->hEncoderConfig )
     255             :         {
     256           0 :             free( ( *phIvasEnc )->st_ivas->hEncoderConfig );
     257           0 :             ( *phIvasEnc )->st_ivas->hEncoderConfig = NULL;
     258             :         }
     259           0 :         free( ( *phIvasEnc )->st_ivas );
     260             :     }
     261             : 
     262         624 :     ( *phIvasEnc )->st_ivas = NULL;
     263             : 
     264         624 :     free( *phIvasEnc );
     265             : 
     266         624 :     *phIvasEnc = NULL;
     267         624 :     phIvasEnc = NULL;
     268             : 
     269         624 :     return;
     270             : }
     271             : 
     272             : 
     273             : /*---------------------------------------------------------------------*
     274             :  * IVAS_ENC_ConfigureForMono()
     275             :  *
     276             :  * Configure and initialize the mono encoder.
     277             :  *---------------------------------------------------------------------*/
     278             : 
     279           3 : ivas_error IVAS_ENC_ConfigureForMono(
     280             :     IVAS_ENC_HANDLE hIvasEnc,                     /* i/o: IVAS encoder handle                                                                                 */
     281             :     const Word32 inputFs,                         /* i  : input sampling frequency                                                                            */
     282             :     const Word32 bitrate,                         /* i  : requested bitrate of the output bitstream                                                           */
     283             :     const bool max_bwidth_user,                   /* i  : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false)   */
     284             :     const IVAS_ENC_BANDWIDTH maxBandwidth,        /* i  : bandwidth limitation                                                                                */
     285             :     const IVAS_ENC_DTX_CONFIG dtxConfig,          /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     286             :     const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig, /* i  : configuration of channel-aware mode, can by set to default by using IVAS_ENC_GetDefaultChannelAwareConfig() */
     287             :     const bool downmixFromStereo,                 /* i  : if true, the encoder accepts a stereo input and internally downmixes it to mono before encoding     */
     288             :     const bool is_binaural                        /* i  : if true, the input is binaural audio */
     289             : )
     290             : {
     291             :     ivas_error error;
     292             : 
     293           3 :     error = IVAS_ERR_OK;
     294             : 
     295           3 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     296             :     {
     297           0 :         return error;
     298             :     }
     299             : 
     300           3 :     hIvasEnc->st_ivas->hEncoderConfig->ivas_format = MONO_FORMAT;
     301           3 :     hIvasEnc->st_ivas->hEncoderConfig->is_binaural = (Word16) is_binaural;
     302           3 :     move16();
     303           3 :     move16();
     304             : 
     305           3 :     if ( downmixFromStereo )
     306             :     {
     307           2 :         hIvasEnc->st_ivas->hEncoderConfig->stereo_dmx_evs = 1;
     308           2 :         move16();
     309             :     }
     310           3 :     hIvasEnc->maxBandwidthUser = max_bwidth_user;
     311             : 
     312           3 :     error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, caConfig );
     313             : 
     314           3 :     return error;
     315             : }
     316             : 
     317             : 
     318             : /*---------------------------------------------------------------------*
     319             :  * IVAS_ENC_ConfigureForStereo()
     320             :  *
     321             :  * Configure and initialize the stereo encoder.
     322             :  *---------------------------------------------------------------------*/
     323             : 
     324          68 : ivas_error IVAS_ENC_ConfigureForStereo(
     325             :     IVAS_ENC_HANDLE hIvasEnc,              /* i/o: IVAS encoder handle                                                                                 */
     326             :     const Word32 inputFs,                  /* i  : input sampling frequency                                                                            */
     327             :     const Word32 bitrate,                  /* i  : requested bitrate of the output bitstream                                                           */
     328             :     const bool max_bwidth_user,            /* i  : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false)   */
     329             :     const IVAS_ENC_BANDWIDTH maxBandwidth, /* i  : bandwidth limitation                                                                                */
     330             :     const IVAS_ENC_DTX_CONFIG dtxConfig,   /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     331             :     const bool is_binaural                 /* i  : flag indicating if input is binaural audio */
     332             : #ifdef DEBUGGING
     333             :     ,
     334             :     const IVAS_ENC_STEREO_MODE stereoMode /* i  : forces a specific stereo coding mode                                                                */
     335             : #endif
     336             : )
     337             : {
     338             :     Encoder_Struct *st_ivas;
     339             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     340             : 
     341             :     ivas_error error;
     342             : 
     343          68 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     344             :     {
     345           0 :         return error;
     346             :     }
     347             : 
     348          68 :     st_ivas = hIvasEnc->st_ivas;
     349          68 :     hEncoderConfig = st_ivas->hEncoderConfig;
     350          68 :     hEncoderConfig->nchan_inp = 2;
     351          68 :     move16();
     352          68 :     hEncoderConfig->ivas_format = STEREO_FORMAT;
     353          68 :     move16();
     354          68 :     hEncoderConfig->is_binaural = (Word16) is_binaural;
     355          68 :     move16();
     356             : 
     357             : #ifdef DEBUGGING
     358             :     switch ( stereoMode )
     359             :     {
     360             :         case IVAS_ENC_STEREO_MODE_UNIFIED:
     361             :             hEncoderConfig->stereo_mode_cmdl = 1; /* set unified stereo by default */
     362             :             hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     363             :             hIvasEnc->cmd_stereo = true;
     364             :             break;
     365             :         case IVAS_ENC_STEREO_MODE_DFT:
     366             :             hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     367             :             hEncoderConfig->stereo_mode_cmdl = IVAS_CPE_DFT;
     368             :             break;
     369             :         case IVAS_ENC_STEREO_MODE_TD:
     370             :             hEncoderConfig->stereo_mode_cmdl = IVAS_CPE_TD;
     371             :             hEncoderConfig->element_mode_init = IVAS_CPE_TD;
     372             :             break;
     373             :         case IVAS_ENC_STEREO_MODE_MDCT_DECISION:
     374             :             hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     375             :             hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
     376             :             break;
     377             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
     378             :         case IVAS_ENC_STEREO_MODE_MDCT_FORCE_LR:
     379             :             hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     380             :             hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_FORCE_LR;
     381             :             break;
     382             :         case IVAS_ENC_STEREO_MODE_MDCT_FORCE_MS:
     383             :             hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     384             :             hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_FORCE_MS;
     385             :             break;
     386             : #endif
     387             :         default:
     388             :             return IVAS_ERR_INVALID_STEREO_MODE;
     389             :             break;
     390             :     }
     391             : #endif /* DEBUGGING */
     392             : 
     393          68 :     hIvasEnc->maxBandwidthUser = max_bwidth_user;
     394             : 
     395          68 :     error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     396             : 
     397          68 :     return error;
     398             : }
     399             : 
     400             : 
     401             : /*---------------------------------------------------------------------*
     402             :  * IVAS_ENC_ConfigureForMASAObjects()
     403             :  *
     404             :  * Configure and initialize the combined MASA and ISM encoder.
     405             :  *---------------------------------------------------------------------*/
     406          44 : ivas_error IVAS_ENC_ConfigureForMASAObjects(
     407             :     IVAS_ENC_HANDLE hIvasEnc,              /* i/o: IVAS encoder handle                                                                                 */
     408             :     const Word32 inputFs,                  /* i  : input sampling frequency                                                                            */
     409             :     const Word32 bitrate,                  /* i  : requested bitrate of the ouput bitstream                                                            */
     410             :     const IVAS_ENC_BANDWIDTH maxBandwidth, /* i  : bandwidth limitation                                                                                */
     411             :     const IVAS_ENC_DTX_CONFIG dtxConfig,   /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     412             :     const UWord16 numObjects,              /* i  : number of objects to be encoded                                                                     */
     413             :     const Word16 masaVariant               /* i  : index specifying the number of MASA transport channels                                              */
     414             : )
     415             : {
     416             :     Encoder_Struct *st_ivas;
     417             :     ivas_error error;
     418             : 
     419          44 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     420             :     {
     421           0 :         return error;
     422             :     }
     423             : 
     424          44 :     IF( GT_32( numObjects, MAX_NUM_OBJECTS ) )
     425             :     {
     426           0 :         return IVAS_ERR_TOO_MANY_INPUTS;
     427             :     }
     428          44 :     st_ivas = hIvasEnc->st_ivas;
     429          44 :     SWITCH( masaVariant )
     430             :     {
     431          32 :         case IVAS_ENC_MASA_2CH:
     432          32 :             st_ivas->hEncoderConfig->nchan_inp = add( CPE_CHANNELS, numObjects );
     433          32 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
     434          32 :             move16();
     435          32 :             move16();
     436          32 :             BREAK;
     437          12 :         case IVAS_ENC_MASA_1CH:
     438          12 :             st_ivas->hEncoderConfig->nchan_inp = add( 1, numObjects );
     439          12 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
     440          12 :             move16();
     441          12 :             move16();
     442          12 :             BREAK;
     443           0 :         default:
     444           0 :             return IVAS_ERR_INVALID_MASA_CONFIG;
     445             :     }
     446             : 
     447          44 :     st_ivas = hIvasEnc->st_ivas;
     448             : 
     449             :     /* Currently this is true but it is already shown in descriptive metadata that there can be inequality for this. */
     450          44 :     st_ivas->nchan_transport = sub( st_ivas->hEncoderConfig->nchan_inp, numObjects );
     451          44 :     st_ivas->hEncoderConfig->ivas_format = MASA_ISM_FORMAT;
     452          44 :     st_ivas->hEncoderConfig->nchan_ism = numObjects;
     453          44 :     move16();
     454          44 :     move16();
     455          44 :     move16();
     456             : 
     457          44 :     return configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     458             : }
     459             : 
     460             : 
     461             : /*---------------------------------------------------------------------*
     462             :  * IVAS_ENC_ConfigureForObjects()
     463             :  *
     464             :  * Configure and initialize the ISM encoder.
     465             :  *---------------------------------------------------------------------*/
     466             : 
     467          74 : ivas_error IVAS_ENC_ConfigureForObjects(
     468             :     IVAS_ENC_HANDLE hIvasEnc,              /* i/o: IVAS encoder handle                                                                                 */
     469             :     const Word32 inputFs,                  /* i  : input sampling frequency                                                                            */
     470             :     const Word32 bitrate,                  /* i  : requested bitrate of the output bitstream                                                           */
     471             :     const bool max_bwidth_user,            /* i  : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false)   */
     472             :     const IVAS_ENC_BANDWIDTH maxBandwidth, /* i  : bandwidth limitation                                                                                */
     473             :     const IVAS_ENC_DTX_CONFIG dtxConfig,   /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     474             :     const UWord16 numObjects,              /* i  : number of objects to be encoded                                                                     */
     475             :     const bool ism_extended_metadata       /* i  : Extended metadata used (true/false), where extended metadata includes radius and orientation        */
     476             : )
     477             : {
     478             :     Encoder_Struct *st_ivas;
     479             :     ivas_error error;
     480             : 
     481          74 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     482             :     {
     483           0 :         return error;
     484             :     }
     485             : 
     486          74 :     IF( GT_32( numObjects, MAX_NUM_OBJECTS ) )
     487             :     {
     488           0 :         return IVAS_ERR_TOO_MANY_INPUTS;
     489             :     }
     490             : 
     491          74 :     st_ivas = hIvasEnc->st_ivas;
     492          74 :     st_ivas->hEncoderConfig->ivas_format = ISM_FORMAT;
     493          74 :     move16();
     494          74 :     st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE;
     495          74 :     move16();
     496          74 :     st_ivas->hEncoderConfig->nchan_inp = numObjects;
     497          74 :     move16();
     498          74 :     st_ivas->hEncoderConfig->nchan_ism = numObjects;
     499          74 :     move16();
     500          74 :     st_ivas->hEncoderConfig->ism_extended_metadata_flag = ism_extended_metadata;
     501          74 :     move16();
     502          74 :     hIvasEnc->extMetadataApi = ( ism_extended_metadata == 1 );
     503          74 :     hIvasEnc->maxBandwidthUser = max_bwidth_user;
     504             : 
     505          74 :     error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     506             : 
     507          74 :     return error;
     508             : }
     509             : 
     510             : 
     511             : /*---------------------------------------------------------------------*
     512             :  * IVAS_ENC_FeedObjectMetadata()
     513             :  *
     514             :  *
     515             :  *---------------------------------------------------------------------*/
     516             : 
     517      427276 : ivas_error IVAS_ENC_FeedObjectMetadata(
     518             :     IVAS_ENC_HANDLE hIvasEnc,        /* i/o: IVAS encoder handle                                                                                 */
     519             :     const UWord16 ismIndex,          /* i  : object index                                                                                        */
     520             :     const IVAS_ISM_METADATA metadata /* i  : object metadata handle for current frame                                                            */
     521             : )
     522             : {
     523             :     ivas_error error;
     524             : 
     525      427276 :     error = IVAS_ERR_OK;
     526             : 
     527      427276 :     IF( !hIvasEnc->isConfigured )
     528             :     {
     529           0 :         return IVAS_ERR_NOT_CONFIGURED;
     530             :     }
     531             : 
     532      427276 :     test();
     533      427276 :     test();
     534      427276 :     IF( NE_16( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, ISM_FORMAT ) && NE_16( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) && NE_16( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
     535             :     {
     536           0 :         return IVAS_ERR_METADATA_NOT_EXPECTED;
     537             :     }
     538             : 
     539      427276 :     IF( GT_32( ismIndex, hIvasEnc->st_ivas->hEncoderConfig->nchan_inp ) )
     540             :     {
     541           0 :         return IVAS_ERR_INVALID_INDEX;
     542             :     }
     543      427276 :     Word32 azimuth_fx = float_to_fix( metadata.azimuth, Q22 );     /* Q22 */
     544      427276 :     Word32 elevation_fx = float_to_fix( metadata.elevation, Q22 ); /* Q22 */
     545      427276 :     Word16 radius_fx = float_to_fix16( metadata.radius, Q9 );      /* Q9 */
     546      427276 :     Word32 yaw_fx = float_to_fix( metadata.yaw, Q22 );             /* Q22 */
     547      427276 :     Word32 pitch_fx = float_to_fix( metadata.pitch, Q22 );         /* Q22 */
     548      427276 :     error = ivas_set_ism_metadata_fx( hIvasEnc->st_ivas->hIsmMetaData[ismIndex], azimuth_fx, elevation_fx, radius_fx, yaw_fx, pitch_fx, metadata.non_diegetic_flag );
     549             : 
     550      427276 :     IF( error != IVAS_ERR_OK )
     551             :     {
     552           0 :         return error;
     553             :     }
     554             : 
     555      427276 :     hIvasEnc->ismMetadataProvided[ismIndex] = true;
     556             : 
     557      427276 :     return error;
     558             : }
     559             : 
     560             : 
     561             : /*---------------------------------------------------------------------*
     562             :  * IVAS_ENC_ConfigureForAmbisonics()
     563             :  *
     564             :  * Configure and initialize the SBA encoder.
     565             :  *---------------------------------------------------------------------*/
     566             : 
     567         244 : ivas_error IVAS_ENC_ConfigureForAmbisonics(
     568             :     IVAS_ENC_HANDLE hIvasEnc,              /* i/o: IVAS encoder handle                                                                                 */
     569             :     const Word32 inputFs,                  /* i  : input sampling frequency                                                                            */
     570             :     const Word32 bitrate,                  /* i  : requested bitrate of the output bitstream                                                           */
     571             :     const bool max_bwidth_user,            /* i  : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false)   */
     572             :     const IVAS_ENC_BANDWIDTH maxBandwidth, /* i  : bandwidth limitation                                                                                */
     573             :     const IVAS_ENC_DTX_CONFIG dtxConfig,   /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     574             :     const IVAS_ENC_SBA_ORDER order,        /* i  : order of the Ambisonics input                                                                       */
     575             :     const bool isPlanar,                   /* i  : if true, input is treated as planar Ambisonics                                                      */
     576             :     const bool Opt_PCA_ON                  /* i  : PCA option flag                                                                                     */
     577             : )
     578             : {
     579             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     580             :     ivas_error error;
     581             : 
     582         244 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     583             :     {
     584           0 :         return error;
     585             :     }
     586             : 
     587         244 :     hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
     588             : 
     589         244 :     hEncoderConfig->ivas_format = SBA_FORMAT;
     590         244 :     hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
     591         244 :     hEncoderConfig->sba_planar = isPlanar;
     592         244 :     hEncoderConfig->sba_order = order;
     593         244 :     move16();
     594         244 :     move16();
     595         244 :     move16();
     596             : 
     597             :     /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
     598         244 :     hEncoderConfig->nchan_inp = ivas_sba_get_nchan_fx( hEncoderConfig->sba_order, 0 ); /*planar input arg. deliberately set to zero since input always in ACN/SN3D*/
     599         244 :     move16();
     600             : 
     601             : 
     602         244 :     hEncoderConfig->Opt_PCA_ON = (Word16) Opt_PCA_ON;
     603         244 :     move16();
     604             : 
     605         244 :     hIvasEnc->maxBandwidthUser = max_bwidth_user;
     606             : 
     607         244 :     error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     608             : 
     609             : 
     610         244 :     return error;
     611             : }
     612             : /*---------------------------------------------------------------------*
     613             :  * IVAS_ENC_ConfigureForSBAObjects()
     614             :  *
     615             :  * Configure and initialize the combined SBA and ISM encoder.
     616             :  *---------------------------------------------------------------------*/
     617             : 
     618          35 : ivas_error IVAS_ENC_ConfigureForSBAObjects(
     619             :     IVAS_ENC_HANDLE hIvasEnc,              /* i/o: IVAS encoder handle                                                                                 */
     620             :     const Word32 inputFs,                  /* i  : input sampling frequency                                                                            */
     621             :     const Word32 bitrate,                  /* i  : requested bitrate of the ouput bitstream                                                            */
     622             :     const IVAS_ENC_BANDWIDTH maxBandwidth, /* i  : bandwidth limitation                                                                                */
     623             :     const IVAS_ENC_DTX_CONFIG dtxConfig,   /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     624             :     const UWord16 numObjects,              /* i  : number of objects to be encoded                                                                     */
     625             :     const IVAS_ENC_SBA_ORDER order,        /* i  : order of the Ambisonics input                                                                       */
     626             :     const bool isPlanar,                   /* i  : if true, input is treated as planar Ambisonics                                                      */
     627             :     const bool Opt_PCA_ON                  /* i  : PCA option flag                                                                                     */
     628             : )
     629             : {
     630             :     Encoder_Struct *st_ivas;
     631             :     ivas_error error;
     632             : 
     633          35 :     IF( NE_16( ( error = doCommonConfigureChecks( hIvasEnc ) ), IVAS_ERR_OK ) )
     634             :     {
     635           0 :         return error;
     636             :     }
     637             : 
     638          35 :     IF( GT_32( numObjects, MAX_NUM_OBJECTS ) )
     639             :     {
     640           0 :         return IVAS_ERR_TOO_MANY_INPUTS;
     641             :     }
     642          35 :     st_ivas = hIvasEnc->st_ivas;
     643             : 
     644          35 :     st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE; /* Just needs to be something not mono, will be set later */
     645          35 :     move16();
     646          35 :     st_ivas->hEncoderConfig->sba_planar = isPlanar;
     647          35 :     move16();
     648          35 :     st_ivas->hEncoderConfig->sba_order = order;
     649          35 :     move16();
     650             : 
     651             :     /* Input in ACN/SN3D in all cases (3D and planar): get number of channels */
     652             :     /*Input file will always contain all channels for a given order irrespective of planar flag*/
     653          35 :     st_ivas->hEncoderConfig->nchan_inp = add( ivas_sba_get_nchan_fx( st_ivas->hEncoderConfig->sba_order, 0 ), numObjects );
     654          35 :     move16();
     655          35 :     st_ivas->hEncoderConfig->Opt_PCA_ON = extract_l( Opt_PCA_ON );
     656          35 :     move16();
     657             : 
     658             :     /* Currently this is true but it is already shown in descriptive metadata that there can be inequality for this. */
     659          35 :     st_ivas->nchan_transport = sub( st_ivas->hEncoderConfig->nchan_inp, numObjects );
     660          35 :     move16();
     661          35 :     st_ivas->hEncoderConfig->ivas_format = SBA_ISM_FORMAT;
     662          35 :     move16();
     663          35 :     st_ivas->hEncoderConfig->nchan_ism = numObjects;
     664          35 :     move16();
     665             : 
     666          35 :     return configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     667             : }
     668             : 
     669             : 
     670             : /*---------------------------------------------------------------------*
     671             :  * IVAS_ENC_ConfigureForMasa()
     672             :  *
     673             :  * Configure and initialize the MASA encoder.
     674             :  *---------------------------------------------------------------------*/
     675          75 : ivas_error IVAS_ENC_ConfigureForMasa(
     676             :     IVAS_ENC_HANDLE hIvasEnc,               /* i/o: IVAS encoder handle                                                                                 */
     677             :     const Word32 inputFs,                   /* i  : input sampling frequency                                                                            */
     678             :     const Word32 bitrate,                   /* i  : requested bitrate of the output bitstream                                                           */
     679             :     const bool max_bwidth_user,             /* i  : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false)   */
     680             :     const IVAS_ENC_BANDWIDTH maxBandwidth,  /* i  : bandwidth limitation                                                                                */
     681             :     const IVAS_ENC_DTX_CONFIG dtxConfig,    /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     682             :     const IVAS_ENC_MASA_VARIANT masaVariant /* i  : type of MASA input (either 1 or 2 channels)                                                         */
     683             : )
     684             : {
     685             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     686             :     ivas_error error;
     687             : 
     688          75 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     689             :     {
     690           0 :         return error;
     691             :     }
     692             : 
     693          75 :     hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
     694             : 
     695          75 :     hEncoderConfig->ivas_format = MASA_FORMAT;
     696          75 :     move32();
     697             : 
     698          75 :     SWITCH( masaVariant )
     699             :     {
     700          31 :         case IVAS_ENC_MASA_1CH:
     701          31 :             hEncoderConfig->nchan_inp = 1;
     702          31 :             hEncoderConfig->element_mode_init = IVAS_SCE;
     703          31 :             move16();
     704          31 :             move16();
     705          31 :             BREAK;
     706          44 :         case IVAS_ENC_MASA_2CH:
     707          44 :             hEncoderConfig->nchan_inp = 2;
     708             : #ifdef DEBUGGING
     709             :             hEncoderConfig->stereo_mode_cmdl = 1; /* set unified stereo by default */
     710             : #endif
     711          44 :             hEncoderConfig->element_mode_init = IVAS_CPE_DFT; /* initialization only, might be changed later based on element_brate */
     712          44 :             move16();
     713          44 :             move16();
     714          44 :             BREAK;
     715           0 :         default:
     716           0 :             return IVAS_ERR_INVALID_MASA_CONFIG;
     717             :     }
     718             : 
     719          75 :     hIvasEnc->maxBandwidthUser = max_bwidth_user;
     720             : 
     721          75 :     error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     722             : 
     723          75 :     return error;
     724             : }
     725             : 
     726             : 
     727             : /*---------------------------------------------------------------------*
     728             :  * IVAS_ENC_FeedMasaMetadata()
     729             :  *
     730             :  *
     731             :  *---------------------------------------------------------------------*/
     732             : 
     733       39626 : ivas_error IVAS_ENC_FeedMasaMetadata(
     734             :     IVAS_ENC_HANDLE hIvasEnc,               /* i/o: IVAS encoder handle                                                                                 */
     735             :     IVAS_MASA_METADATA_HANDLE hMasaMetadata /* i  : MASA metadata for current frame                                                                     */
     736             : )
     737             : {
     738       39626 :     IF( !hIvasEnc->isConfigured )
     739             :     {
     740           0 :         return IVAS_ERR_NOT_CONFIGURED;
     741             :     }
     742             : 
     743       39626 :     test();
     744       39626 :     IF( NE_32( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, MASA_FORMAT ) && NE_32( hIvasEnc->st_ivas->hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
     745             :     {
     746           0 :         return IVAS_ERR_METADATA_NOT_EXPECTED;
     747             :     }
     748             : 
     749       39626 :     hIvasEnc->st_ivas->hMasa->masaMetadata = *hMasaMetadata;
     750             : 
     751       39626 :     return IVAS_ERR_OK;
     752             : }
     753             : 
     754             : 
     755             : /*---------------------------------------------------------------------*
     756             :  * IVAS_ENC_ConfigureForMultichannel()
     757             :  *
     758             :  * Configure and initialize the MC encoder.
     759             :  *---------------------------------------------------------------------*/
     760             : 
     761          81 : ivas_error IVAS_ENC_ConfigureForMultichannel(
     762             :     IVAS_ENC_HANDLE hIvasEnc,              /* i/o: IVAS encoder handle                                                                                 */
     763             :     const Word32 inputFs,                  /* i  : input sampling frequency                                                                            */
     764             :     const Word32 bitrate,                  /* i  : requested bitrate of the output bitstream                                                           */
     765             :     const bool max_bwidth_user,            /* i  : shows if bandwidth limitation was set by the user (true) or if default bandwidth was used (false)   */
     766             :     const IVAS_ENC_BANDWIDTH maxBandwidth, /* i  : bandwidth limitation                                                                                */
     767             :     const IVAS_ENC_DTX_CONFIG dtxConfig,   /* i  : configuration of DTX, can by set to default by using IVAS_ENC_GetDefaultDtxConfig()                 */
     768             :     const IVAS_ENC_MC_LAYOUT mcLayout      /* i  : inpu MC layput                                                                                      */
     769             : )
     770             : {
     771             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     772             :     ivas_error error;
     773             : 
     774          81 :     IF( ( error = doCommonConfigureChecks( hIvasEnc ) ) != IVAS_ERR_OK )
     775             :     {
     776           0 :         return error;
     777             :     }
     778             : 
     779          81 :     hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
     780             : 
     781          81 :     hEncoderConfig->ivas_format = MC_FORMAT;
     782          81 :     move16();
     783          81 :     hEncoderConfig->element_mode_init = IVAS_CPE_MDCT; /*just for initialization*/
     784          81 :     move16();
     785             : 
     786          81 :     SWITCH( mcLayout )
     787             :     {
     788          42 :         case IVAS_ENC_MC_5_1:
     789          42 :             hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1;
     790          42 :             BREAK;
     791           4 :         case IVAS_ENC_MC_7_1:
     792           4 :             hEncoderConfig->mc_input_setup = MC_LS_SETUP_7_1;
     793           4 :             BREAK;
     794           6 :         case IVAS_ENC_MC_5_1_2:
     795           6 :             hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1_2;
     796           6 :             BREAK;
     797           6 :         case IVAS_ENC_MC_5_1_4:
     798           6 :             hEncoderConfig->mc_input_setup = MC_LS_SETUP_5_1_4;
     799           6 :             BREAK;
     800          23 :         case IVAS_ENC_MC_7_1_4:
     801          23 :             hEncoderConfig->mc_input_setup = MC_LS_SETUP_7_1_4;
     802          23 :             BREAK;
     803           0 :         default:
     804           0 :             return IVAS_ERR_INVALID_MC_LAYOUT;
     805             :             BREAK;
     806             :     }
     807             : 
     808          81 :     hEncoderConfig->nchan_inp = ivas_mc_ls_setup_get_num_channels_fx( hEncoderConfig->mc_input_setup );
     809          81 :     move16();
     810             : 
     811          81 :     hIvasEnc->maxBandwidthUser = max_bwidth_user;
     812             : 
     813          81 :     error = configureEncoder( hIvasEnc, inputFs, bitrate, maxBandwidth, dtxConfig, IVAS_ENC_GetDefaultChannelAwareConfig() );
     814             : 
     815          81 :     return error;
     816             : }
     817             : 
     818             : 
     819             : /*---------------------------------------------------------------------*
     820             :  * configureEncoder()
     821             :  *
     822             :  * Configure the IVAS encoder.
     823             :  *---------------------------------------------------------------------*/
     824             : 
     825         624 : static ivas_error configureEncoder(
     826             :     IVAS_ENC_HANDLE hIvasEnc,
     827             :     const Word32 inputFs,
     828             :     const Word32 initBitrate,
     829             :     const IVAS_ENC_BANDWIDTH initBandwidth,
     830             :     const IVAS_ENC_DTX_CONFIG dtxConfig,
     831             :     const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
     832             : {
     833             :     Encoder_Struct *st_ivas;
     834             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     835             :     ivas_error error;
     836             :     Word32 cpe_brate;
     837             : 
     838         624 :     error = IVAS_ERR_OK;
     839             : 
     840         624 :     st_ivas = hIvasEnc->st_ivas;
     841         624 :     hEncoderConfig = st_ivas->hEncoderConfig;
     842             : 
     843             :     /*-----------------------------------------------------------------*
     844             :      * Bandwidth limitation
     845             :      *-----------------------------------------------------------------*/
     846             : 
     847         624 :     IF( ( error = setBandwidth_fx( hIvasEnc, initBandwidth ) ) != IVAS_ERR_OK )
     848             :     {
     849           0 :         return error;
     850             :     }
     851             : 
     852             :     /*-----------------------------------------------------------------*
     853             :      * DTX/CNG
     854             :      *-----------------------------------------------------------------*/
     855             : 
     856         624 :     IF( dtxConfig.enabled )
     857             :     {
     858         126 :         hEncoderConfig->Opt_DTX_ON = 1;
     859         126 :         move16();
     860             : 
     861         126 :         IF( dtxConfig.variable_SID_rate )
     862             :         {
     863           0 :             hEncoderConfig->var_SID_rate_flag = 1;
     864           0 :             hEncoderConfig->interval_SID = 0;
     865           0 :             move16();
     866           0 :             move16();
     867             :         }
     868             :         ELSE
     869             :         {
     870         126 :             hEncoderConfig->var_SID_rate_flag = 0;
     871         126 :             move16();
     872             : 
     873         126 :             test();
     874         126 :             IF( GE_16( dtxConfig.SID_interval, 3 ) && LE_16( dtxConfig.SID_interval, 100 ) )
     875             :             {
     876         126 :                 hEncoderConfig->interval_SID = dtxConfig.SID_interval;
     877         126 :                 move16();
     878             :             }
     879             :             ELSE
     880             :             {
     881           0 :                 return IVAS_ERR_INVALID_DTX_UPDATE_RATE;
     882             :             }
     883             :         }
     884             :     }
     885             :     ELSE
     886             :     {
     887         498 :         hEncoderConfig->Opt_DTX_ON = 0;
     888         498 :         move16();
     889             :     }
     890             : 
     891             :     /*-----------------------------------------------------------------*
     892             :      * Bitrate
     893             :      *-----------------------------------------------------------------*/
     894             : 
     895         624 :     hEncoderConfig->ivas_total_brate = initBitrate;
     896         624 :     move32();
     897             : 
     898             :     /* SC-VBR at 5.90 kbps */
     899         624 :     IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_5k90 ) )
     900             :     {
     901           0 :         hEncoderConfig->ivas_total_brate = ACELP_7k20;
     902           0 :         hEncoderConfig->Opt_SC_VBR = 1;
     903           0 :         hEncoderConfig->last_Opt_SC_VBR = hEncoderConfig->Opt_SC_VBR;
     904           0 :         move32();
     905           0 :         move16();
     906           0 :         move16();
     907             : 
     908           0 :         if ( ( hEncoderConfig->max_bwidth != NB ) )
     909             :         {
     910           0 :             hEncoderConfig->max_bwidth = WB;
     911           0 :             move16();
     912             :         }
     913             :     }
     914             : 
     915             :     /* check if the entered bitrate is supported */
     916         624 :     test();
     917         624 :     IF( NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) ) /* IVAS */
     918             :     {
     919         621 :         IF( !is_IVAS_bitrate_fx( hEncoderConfig->ivas_total_brate ) )
     920             :         {
     921           0 :             IF( hEncoderConfig->Opt_SC_VBR )
     922             :             {
     923           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", ACELP_5k90 );
     924             :             }
     925             :             ELSE
     926             :             {
     927           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS [bps]: %d", hEncoderConfig->ivas_total_brate );
     928             :             }
     929             :         }
     930             : 
     931         621 :         IF( EQ_16( hEncoderConfig->ivas_format, STEREO_FORMAT ) )
     932             :         {
     933             : #ifdef DEBUGGING
     934             :             if ( hIvasEnc->cmd_stereo )
     935             : #endif
     936             :             {
     937          68 :                 hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     938             : #ifdef DEBUGGING
     939             :                 hEncoderConfig->stereo_mode_cmdl = 1;
     940             : #endif
     941             : 
     942          68 :                 if ( GE_32( hEncoderConfig->ivas_total_brate, MIN_BRATE_MDCT_STEREO ) )
     943             :                 {
     944          18 :                     hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     945          18 :                     move16();
     946             : #ifdef DEBUGGING
     947             :                     hEncoderConfig->stereo_mode_cmdl = 0;
     948             : #endif
     949             :                 }
     950             :             }
     951             : 
     952          68 :             test();
     953          68 :             test();
     954          68 :             IF( ( EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_TD ) || EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_DFT ) ) && GT_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
     955             :             {
     956           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for TD/DFT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
     957             :             }
     958             : 
     959          68 :             test();
     960          68 :             IF( EQ_16( hEncoderConfig->element_mode_init, IVAS_CPE_MDCT ) && LT_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
     961             :             {
     962           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for MDCT Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
     963             :             }
     964             : 
     965          68 :             IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_256k ) )
     966             :             {
     967           0 :                 return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for Stereo specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
     968             :             }
     969             :         }
     970         553 :         ELSE IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
     971             :         {
     972          74 :             IF( ( error = sanitizeBitrateISM_fx( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
     973             :             {
     974           0 :                 return error;
     975             :             }
     976             :         }
     977         479 :         ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) )
     978             :         {
     979             :             /* nothing */
     980             :         }
     981         235 :         ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_FORMAT ) )
     982             :         {
     983             :             /* adapt element_mode according to the bitrate */
     984          75 :             test();
     985          75 :             IF( EQ_16( hEncoderConfig->nchan_inp, 2 ) && NE_16( hEncoderConfig->element_mode_init, IVAS_SCE ) )
     986             :             {
     987          44 :                 IF( GE_32( hEncoderConfig->ivas_total_brate, IVAS_48k ) )
     988             :                 {
     989          22 :                     hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     990          22 :                     move16();
     991             :                 }
     992          22 :                 ELSE IF( LT_32( hEncoderConfig->ivas_total_brate, MASA_STEREO_MIN_BITRATE ) )
     993             :                 {
     994          14 :                     hEncoderConfig->element_mode_init = IVAS_CPE_DFT;
     995          14 :                     move16();
     996             :                 }
     997             :             }
     998             :         }
     999         160 :         ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
    1000             :         {
    1001          44 :             st_ivas->ism_mode = ivas_omasa_ism_mode_select_fx( st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
    1002          44 :             move32();
    1003             : 
    1004          44 :             cpe_brate = calculate_cpe_brate_MASA_ISM_fx( st_ivas->ism_mode, st_ivas->hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism );
    1005             : 
    1006             :             /*adapt element_mode according to the bit-rate*/
    1007          44 :             IF( NE_16( hEncoderConfig->element_mode_init, IVAS_SCE ) )
    1008             :             {
    1009          44 :                 if ( GE_32( cpe_brate, IVAS_48k ) )
    1010             :                 {
    1011          17 :                     hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
    1012          17 :                     move16();
    1013             :                 }
    1014             :             }
    1015             :         }
    1016         116 :         ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
    1017             :         {
    1018          35 :             st_ivas->ism_mode = ISM_MODE_NONE;
    1019          35 :             move16();
    1020             :         }
    1021             :     }
    1022             :     ELSE /* EVS mono */
    1023             :     {
    1024           3 :         hEncoderConfig->ivas_format = MONO_FORMAT;
    1025           3 :         move16();
    1026           3 :         hEncoderConfig->element_mode_init = EVS_MONO;
    1027           3 :         move16();
    1028             : 
    1029           3 :         IF( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
    1030             :         {
    1031           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
    1032             :         }
    1033             : 
    1034           3 :         IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
    1035             :         {
    1036           2 :             hEncoderConfig->nchan_inp = 2;
    1037           2 :             move16();
    1038             :         }
    1039             :     }
    1040             : 
    1041             :     /*-----------------------------------------------------------------*
    1042             :      * Input sampling frequency
    1043             :      *-----------------------------------------------------------------*/
    1044         624 :     test();
    1045         624 :     test();
    1046         624 :     test();
    1047         624 :     IF( NE_32( inputFs, 8000 ) && NE_32( inputFs, 16000 ) && NE_32( inputFs, 32000 ) && NE_32( inputFs, 48000 ) )
    1048             :     {
    1049           0 :         return IVAS_ERR_INVALID_SAMPLING_RATE;
    1050             :     }
    1051             : 
    1052         624 :     hEncoderConfig->input_Fs = inputFs;
    1053         624 :     move32();
    1054             : 
    1055             :     /*-----------------------------------------------------------------*
    1056             :      * Channel-aware mode
    1057             :      *-----------------------------------------------------------------*/
    1058             : 
    1059         624 :     IF( ( error = setChannelAwareConfig_fx( hIvasEnc, caConfig ) ) != IVAS_ERR_OK )
    1060             :     {
    1061           0 :         return error;
    1062             :     }
    1063             : 
    1064             :     /*-----------------------------------------------------------------*
    1065             :      * Set codec mode
    1066             :      *-----------------------------------------------------------------*/
    1067             : 
    1068         624 :     st_ivas->codec_mode = MODE1; /* Note: in IVAS, set MODE1 */
    1069         624 :     move16();
    1070             : 
    1071         624 :     IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1072             :     {
    1073           3 :         IF( hEncoderConfig->Opt_AMR_WB )
    1074             :         {
    1075           0 :             st_ivas->codec_mode = MODE1;
    1076           0 :             move16();
    1077             :         }
    1078             :         ELSE
    1079             :         {
    1080           3 :             st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
    1081           3 :             move16();
    1082             :         }
    1083             :     }
    1084             : 
    1085         624 :     test();
    1086         624 :     IF( EQ_32( hEncoderConfig->ivas_total_brate, IVAS_13k2 ) && EQ_16( hEncoderConfig->Opt_RF_ON, 1 ) )
    1087             :     {
    1088           0 :         st_ivas->codec_mode = MODE2;
    1089           0 :         move16();
    1090             :     }
    1091             : 
    1092         624 :     st_ivas->last_codec_mode = st_ivas->codec_mode;
    1093         624 :     move16();
    1094             : 
    1095             :     /*-----------------------------------------------------------------*
    1096             :      * Sanity checks
    1097             :      *-----------------------------------------------------------------*/
    1098             : 
    1099         624 :     assert( hEncoderConfig->ivas_format != UNDEFINED_FORMAT && "\n IVAS format undefined" );
    1100             : 
    1101         624 :     test();
    1102         624 :     test();
    1103         624 :     IF( ( NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) || hEncoderConfig->stereo_dmx_evs ) && EQ_32( hEncoderConfig->input_Fs, 8000 ) )
    1104             :     {
    1105           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "8kHz input sampling rate is not supported in IVAS." );
    1106             :     }
    1107             : 
    1108         624 :     test();
    1109         624 :     test();
    1110         624 :     test();
    1111         624 :     test();
    1112         624 :     test();
    1113         624 :     test();
    1114         624 :     IF( hEncoderConfig->Opt_DTX_ON && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) &&
    1115             :         ( ( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) && GT_16( ivas_get_sba_num_TCs_fx( hEncoderConfig->ivas_total_brate, 1 ), 2 ) ) ||
    1116             :           EQ_16( hEncoderConfig->ivas_format, MC_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) ) )
    1117             :     {
    1118           0 :         return IVAS_ERROR( IVAS_ERR_DTX_NOT_SUPPORTED, "DTX is not supported in this IVAS format and element mode." );
    1119             :     }
    1120             : 
    1121         624 :     test();
    1122         624 :     test();
    1123         624 :     test();
    1124         624 :     IF( hEncoderConfig->Opt_PCA_ON && !( ( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) || EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) ) && EQ_32( hEncoderConfig->ivas_total_brate, PCA_BRATE ) && EQ_16( hEncoderConfig->sba_order, SBA_FOA_ORDER ) ) )
    1125             :     {
    1126           0 :         return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "PCA supported at SBA FOA 256 kbps only." );
    1127             :     }
    1128             : 
    1129         624 :     IF( ( error = sanitizeBandwidth_fx( hIvasEnc ) ) != IVAS_ERR_OK )
    1130             :     {
    1131           0 :         return error;
    1132             :     }
    1133             : 
    1134         624 :     test();
    1135         624 :     test();
    1136         624 :     test();
    1137         624 :     IF( hEncoderConfig->is_binaural && !( ( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) && hEncoderConfig->stereo_dmx_evs ) || EQ_16( hEncoderConfig->ivas_format, STEREO_FORMAT ) ) )
    1138             :     {
    1139           0 :         return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "'-binaural' option is supported only with '-stereo' or '-stereo_dmx_evs'" );
    1140             :     }
    1141             : 
    1142             :     /*-----------------------------------------------------------------*
    1143             :      * Finalize initialization
    1144             :      *-----------------------------------------------------------------*/
    1145             : 
    1146         624 :     IF( ( error = ivas_init_encoder_fx( st_ivas ) ) != IVAS_ERR_OK )
    1147             :     {
    1148           0 :         return error;
    1149             :     }
    1150             : 
    1151         624 :     IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1152             :     {
    1153           3 :         hIvasEnc->hCoreCoder = st_ivas->hSCE[0]->hCoreCoder[0]; /* Note: this is needed for switching in EVS mono */
    1154             :     }
    1155             :     ELSE
    1156             :     {
    1157         621 :         hIvasEnc->hCoreCoder = NULL;
    1158             :     }
    1159             : 
    1160         624 :     hIvasEnc->Opt_RF_ON_loc = hEncoderConfig->Opt_RF_ON;
    1161         624 :     hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
    1162         624 :     move16();
    1163         624 :     move16();
    1164             : 
    1165         624 :     hIvasEnc->isConfigured = true;
    1166             : 
    1167         624 :     return error;
    1168             : }
    1169             : /*---------------------------------------------------------------------*
    1170             :  * IVAS_ENC_GetDelay()
    1171             :  *
    1172             :  *
    1173             :  *---------------------------------------------------------------------*/
    1174             : 
    1175         624 : ivas_error IVAS_ENC_GetDelay(
    1176             :     const IVAS_ENC_HANDLE hIvasEnc, /* i  : IVAS encoder handle  */
    1177             :     Word16 *delay                   /* o  : encoder delay        Q0*/
    1178             : )
    1179             : {
    1180             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1181             : 
    1182         624 :     hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
    1183             : 
    1184         624 :     IF( !hIvasEnc->isConfigured )
    1185             :     {
    1186           0 :         return IVAS_ERR_NOT_CONFIGURED;
    1187             :     }
    1188             : 
    1189         624 :     IF( delay == NULL )
    1190             :     {
    1191           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1192             :     }
    1193             : 
    1194         624 :     *delay = NS2SA_FX2( hEncoderConfig->input_Fs, get_delay_fx( ENC, hEncoderConfig->input_Fs, hEncoderConfig->ivas_format, NULL ) ); /*Q0*/
    1195         624 :     move16();
    1196             : 
    1197         624 :     *delay = imult1616( *delay, hEncoderConfig->nchan_inp ); /*Q0*/
    1198         624 :     move16();
    1199             : 
    1200         624 :     return IVAS_ERR_OK;
    1201             : }
    1202             : 
    1203             : 
    1204             : /*---------------------------------------------------------------------*
    1205             :  * getInputBufferSize()
    1206             :  *
    1207             :  *
    1208             :  *---------------------------------------------------------------------*/
    1209      425012 : static Word16 getInputBufferSize_fx(
    1210             :     const Encoder_Struct *st_ivas /* i  : IVAS encoder handle */
    1211             : )
    1212             : {
    1213      425012 :     return extract_l( Mpy_32_32( imult3216( st_ivas->hEncoderConfig->input_Fs, st_ivas->hEncoderConfig->nchan_inp ), ONE_BY_FRAMES_PER_SEC_Q31 ) );
    1214             : }
    1215             : 
    1216             : /*---------------------------------------------------------------------*
    1217             :  * IVAS_ENC_GetNumInChannels()
    1218             :  *
    1219             :  *
    1220             :  *---------------------------------------------------------------------*/
    1221         624 : ivas_error IVAS_ENC_GetNumInChannels(
    1222             :     const IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle                                                                                 */
    1223             :     Word16 *numInChannels           /* o  : total number of samples expected in the input buffer for current encoder configuration              */
    1224             : )
    1225             : {
    1226         624 :     test();
    1227         624 :     IF( hIvasEnc == NULL || numInChannels == NULL )
    1228             :     {
    1229           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1230             :     }
    1231             : 
    1232         624 :     IF( !hIvasEnc->isConfigured )
    1233             :     {
    1234           0 :         return IVAS_ERR_NOT_CONFIGURED;
    1235             :     }
    1236             : 
    1237         624 :     *numInChannels = hIvasEnc->st_ivas->hEncoderConfig->nchan_inp;
    1238         624 :     move16();
    1239             : 
    1240         624 :     return IVAS_ERR_OK;
    1241             : }
    1242             : 
    1243             : 
    1244             : /*---------------------------------------------------------------------*
    1245             :  * IVAS_ENC_GetInputBufferSize()
    1246             :  *
    1247             :  *
    1248             :  *---------------------------------------------------------------------*/
    1249         624 : ivas_error IVAS_ENC_GetInputBufferSize(
    1250             :     const IVAS_ENC_HANDLE hIvasEnc, /* i  : IVAS encoder handle                                                                    */
    1251             :     Word16 *inputBufferSize         /* o  : total number of samples expected in the input buffer for current encoder configuration */
    1252             : )
    1253             : {
    1254         624 :     IF( !hIvasEnc->isConfigured )
    1255             :     {
    1256           0 :         return IVAS_ERR_NOT_CONFIGURED;
    1257             :     }
    1258             : 
    1259         624 :     IF( inputBufferSize == NULL )
    1260             :     {
    1261           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1262             :     }
    1263             : 
    1264         624 :     *inputBufferSize = getInputBufferSize_fx( hIvasEnc->st_ivas );
    1265         624 :     move16();
    1266             : 
    1267         624 :     return IVAS_ERR_OK;
    1268             : }
    1269             : 
    1270             : 
    1271             : /*---------------------------------------------------------------------*
    1272             :  * IVAS_ENC_EncodeFrameToSerial()
    1273             :  *
    1274             :  * Main function to encode one frame to a serial bitstream
    1275             :  *---------------------------------------------------------------------*/
    1276      424388 : ivas_error IVAS_ENC_EncodeFrameToSerial(
    1277             :     IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle                                                                                                     */
    1278             :     Word16 *inputBuffer,      /* i  : PCM input, Q0                                                                                                           Q0*/
    1279             :     Word16 inputBufferSize,   /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()                            Q0*/
    1280             :     UWord16 *outputBitStream, /* o  : pointer to serial output bitstream. The array must already be allocated and be of size at least IVAS_MAX_BITS_PER_FRAME Q0*/
    1281             :     UWord16 *numOutBits       /* o  : number of bits written to output bitstream. Each bit is stored as a single uint16_t value                               Q0*/
    1282             : #ifdef DBG_BITSTREAM_ANALYSIS
    1283             :     ,
    1284             :     int32_t frame
    1285             : #endif
    1286             : )
    1287             : {
    1288             :     Encoder_Struct *st_ivas;
    1289             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1290             :     Word16 i;
    1291             :     Word16 n, ch;
    1292             :     ivas_error error;
    1293             : 
    1294      424388 :     error = IVAS_ERR_OK;
    1295      424388 :     move32();
    1296             : 
    1297      424388 :     IF( !hIvasEnc->isConfigured )
    1298             :     {
    1299           0 :         return IVAS_ERR_NOT_CONFIGURED;
    1300             :     }
    1301             : 
    1302      424388 :     st_ivas = hIvasEnc->st_ivas;
    1303      424388 :     hEncoderConfig = st_ivas->hEncoderConfig;
    1304      424388 :     ENC_CORE_HANDLE hCoreCoder = hIvasEnc->hCoreCoder;
    1305             : 
    1306      424388 :     IF( NE_16( inputBufferSize, getInputBufferSize_fx( st_ivas ) ) )
    1307             :     {
    1308           0 :         return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
    1309             :     }
    1310             : 
    1311      424388 :     IF( NE_32( ( error = sanitizeBandwidth_fx( hIvasEnc ) ), IVAS_ERR_OK ) )
    1312             :     {
    1313           0 :         return error;
    1314             :     }
    1315             : 
    1316      424388 :     IF( EQ_32( hEncoderConfig->ivas_format, ISM_FORMAT ) )
    1317             :     {
    1318      407452 :         FOR( i = 0; i < hEncoderConfig->nchan_inp; ++i )
    1319             :         {
    1320      303626 :             IF( !hIvasEnc->ismMetadataProvided[i] )
    1321             :             {
    1322        6000 :                 ivas_ism_reset_metadata_API( hIvasEnc->st_ivas->hIsmMetaData[i] );
    1323             :             }
    1324             :         }
    1325      103826 :         resetIsmMetadataProvidedFlags( hIvasEnc );
    1326             :     }
    1327             : 
    1328      424388 :     test();
    1329      424388 :     test();
    1330      424388 :     test();
    1331      424388 :     test();
    1332      424388 :     IF( ( hEncoderConfig->Opt_RF_ON && ( NE_32( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) || EQ_32( hEncoderConfig->input_Fs, 8000 ) || hEncoderConfig->max_bwidth == NB ) ) || hEncoderConfig->rf_fec_offset == 0 )
    1333             :     {
    1334      424388 :         test();
    1335      424388 :         IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) && EQ_32( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1336             :         {
    1337        1050 :             st_ivas->codec_mode = MODE1;
    1338        1050 :             move16();
    1339             : 
    1340        1050 :             reset_rf_indices_fx( hCoreCoder );
    1341             :         }
    1342      424388 :         hEncoderConfig->Opt_RF_ON = 0;
    1343      424388 :         move16();
    1344      424388 :         hEncoderConfig->rf_fec_offset = 0;
    1345      424388 :         move16();
    1346      424388 :         hIvasEnc->switchingActive = true;
    1347      424388 :         move16();
    1348             :     }
    1349             : 
    1350      424388 :     test();
    1351      424388 :     test();
    1352      424388 :     test();
    1353      424388 :     test();
    1354      424388 :     IF( hIvasEnc->Opt_RF_ON_loc && hIvasEnc->rf_fec_offset_loc != 0 && L_sub( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) == 0 && L_sub( hEncoderConfig->input_Fs, 8000 ) != 0 && hEncoderConfig->max_bwidth != NB )
    1355             :     {
    1356           0 :         st_ivas->codec_mode = MODE2;
    1357           0 :         move16();
    1358           0 :         test();
    1359           0 :         IF( hEncoderConfig->Opt_RF_ON == 0 && EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1360             :         {
    1361           0 :             reset_rf_indices_fx( hCoreCoder );
    1362             :         }
    1363           0 :         hEncoderConfig->Opt_RF_ON = 1;
    1364           0 :         hEncoderConfig->rf_fec_offset = hIvasEnc->rf_fec_offset_loc;
    1365           0 :         hIvasEnc->switchingActive = true;
    1366           0 :         move16();
    1367           0 :         move16();
    1368           0 :         move16();
    1369             :     }
    1370             : 
    1371             :     /* in case of 8kHz sampling rate or when in "max_band NB" mode, limit the total bitrate to 24.40 kbps */
    1372      424388 :     test();
    1373      424388 :     test();
    1374      424388 :     IF( ( EQ_32( hEncoderConfig->input_Fs, 8000 ) || ( hEncoderConfig->max_bwidth == NB ) ) && GT_32( hEncoderConfig->ivas_total_brate, ACELP_24k40 ) )
    1375             :     {
    1376           0 :         hEncoderConfig->ivas_total_brate = ACELP_24k40;
    1377           0 :         st_ivas->codec_mode = MODE2;
    1378           0 :         hIvasEnc->switchingActive = true;
    1379           0 :         move32();
    1380           0 :         move16();
    1381           0 :         move16();
    1382             :     }
    1383             : 
    1384             :     /*-----------------------------------------------------------------*
    1385             :      * Re-allocate and re-initialize buffer of indices if IVAS total bitrate has changed
    1386             :      *-----------------------------------------------------------------*/
    1387             : 
    1388      424388 :     IF( NE_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->last_ivas_total_brate ) )
    1389             :     {
    1390             :         /* de-allocate old buffer of indices */
    1391        6724 :         free( st_ivas->ind_list );
    1392             : 
    1393             :         /* set the maximum allowed number of indices in the list */
    1394        6724 :         st_ivas->ivas_max_num_indices = get_ivas_max_num_indices_fx( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
    1395        6724 :         move16();
    1396             : 
    1397             :         /* allocate new buffer of indices */
    1398        6724 :         IF( ( st_ivas->ind_list = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices * sizeof( Indice ) ) ) == NULL )
    1399             :         {
    1400           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
    1401             :         }
    1402             : 
    1403             :         /* reset the list of indices */
    1404     5239804 :         FOR( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
    1405             :         {
    1406     5233080 :             st_ivas->ind_list[i].nb_bits = -1;
    1407     5233080 :             move16();
    1408             :         }
    1409             : #ifdef DBG_BITSTREAM_ANALYSIS
    1410             :         for ( i = 0; i < st_ivas->ivas_max_num_indices; i++ )
    1411             :         {
    1412             :             memset( st_ivas->ind_list[i].function_name, 'A', 100 * sizeof( char ) );
    1413             :         }
    1414             : #endif
    1415             :         /* de-allocate old buffer of metadata indices */
    1416        6724 :         IF( st_ivas->ind_list_metadata != NULL )
    1417             :         {
    1418        6724 :             free( st_ivas->ind_list_metadata );
    1419             :         }
    1420             : 
    1421             :         /* set the maximum allowed number of metadata indices in the list */
    1422        6724 :         st_ivas->ivas_max_num_indices_metadata = get_ivas_max_num_indices_metadata_fx( hEncoderConfig->ivas_format, hEncoderConfig->ivas_total_brate );
    1423        6724 :         move16();
    1424             : 
    1425        6724 :         IF( st_ivas->ivas_max_num_indices_metadata > 0 )
    1426             :         {
    1427             :             /* allocate new buffer of metadata indices */
    1428        6724 :             IF( ( st_ivas->ind_list_metadata = (INDICE_HANDLE) malloc( st_ivas->ivas_max_num_indices_metadata * sizeof( Indice ) ) ) == NULL )
    1429             :             {
    1430           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of metadata indices!\n" ) );
    1431             :             }
    1432             : 
    1433             :             /* reset the list of metadata indices */
    1434     2326189 :             FOR( i = 0; i < st_ivas->ivas_max_num_indices_metadata; i++ )
    1435             :             {
    1436     2319465 :                 st_ivas->ind_list_metadata[i].nb_bits = -1;
    1437     2319465 :                 move16();
    1438             :             }
    1439             :         }
    1440             :         ELSE
    1441             :         {
    1442           0 :             st_ivas->ind_list_metadata = NULL;
    1443             :         }
    1444             : 
    1445             :         /* set pointers to the new buffers of indices in each element */
    1446       13092 :         FOR( n = 0; n < st_ivas->nSCE; n++ )
    1447             :         {
    1448        6368 :             st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ind_list = st_ivas->ind_list;
    1449        6368 :             st_ivas->hSCE[n]->hCoreCoder[0]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
    1450             : 
    1451        6368 :             if ( st_ivas->hSCE[n]->hMetaData != NULL )
    1452             :             {
    1453        3627 :                 st_ivas->hSCE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
    1454        3627 :                 st_ivas->hSCE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    1455             :             }
    1456             :         }
    1457             : 
    1458       12884 :         FOR( n = 0; n < st_ivas->nCPE; n++ )
    1459             :         {
    1460       18480 :             FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
    1461             :             {
    1462       12320 :                 st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ind_list = st_ivas->ind_list;
    1463       12320 :                 st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
    1464             :             }
    1465             : 
    1466        6160 :             IF( st_ivas->hCPE[n]->hMetaData != NULL )
    1467             :             {
    1468        4310 :                 st_ivas->hCPE[n]->hMetaData->ind_list = st_ivas->ind_list_metadata;
    1469        4310 :                 st_ivas->hCPE[n]->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata;
    1470             :             }
    1471             :         }
    1472             :     }
    1473             : 
    1474      424388 :     test();
    1475      424388 :     IF( hIvasEnc->switchingActive && EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1476             :     {
    1477        3100 :         copy_encoder_config_fx( st_ivas, hCoreCoder, 0 );
    1478        3100 :         hEncoderConfig->last_ivas_total_brate = hEncoderConfig->ivas_total_brate;
    1479        3100 :         move32();
    1480             :     }
    1481             : 
    1482             :     /* run the main encoding routine */
    1483      424388 :     IF( EQ_32( hEncoderConfig->ivas_format, MONO_FORMAT ) ) /* EVS mono */
    1484             :     {
    1485        3100 :         hCoreCoder->total_brate = hEncoderConfig->ivas_total_brate; /* needed in case of bitrate switching */
    1486        3100 :         move32();
    1487             : 
    1488        3100 :         IF( EQ_16( hEncoderConfig->stereo_dmx_evs, 1 ) )
    1489             :         {
    1490        2100 :             inputBufferSize = shr( inputBufferSize, 1 );
    1491        2100 :             stereo_dmx_evs_enc_fx( st_ivas->hStereoDmxEVS, hEncoderConfig->input_Fs, inputBuffer, inputBufferSize, hEncoderConfig->is_binaural );
    1492             :         }
    1493             : 
    1494        3100 :         IF( hEncoderConfig->Opt_AMR_WB )
    1495             :         {
    1496           0 :             amr_wb_enc_fx( hCoreCoder, inputBuffer, inputBufferSize );
    1497             :         }
    1498             :         ELSE
    1499             :         {
    1500             : #ifdef DEBUG_MODE_INFO
    1501             :             dbgwrite( inputBuffer, sizeof( int16_t ), inputBufferSize, 1, strcat( fname( debug_dir, "ivas_input_dmx", 0, 1, ENC ), ".pcm" ) );
    1502             : #endif
    1503        3100 :             hCoreCoder->input_frame_fx = inputBufferSize;
    1504        3100 :             move32();
    1505        3100 :             IF( NE_32( ( error = evs_enc_fx( hCoreCoder, inputBuffer, hCoreCoder->mem_hp20_in_fx, inputBufferSize ) ), IVAS_ERR_OK ) )
    1506             :             {
    1507           0 :                 return error;
    1508             :             }
    1509             :         }
    1510             :     }
    1511             :     ELSE /* IVAS */
    1512             :         {
    1513      421288 :             IF( NE_32( ( error = ivas_enc_fx( st_ivas, inputBuffer, inputBufferSize ) ), IVAS_ERR_OK ) ){
    1514           0 :                 return error;
    1515             : }
    1516             : }
    1517             : 
    1518             : /* write indices into bitstream buffer */
    1519             : {
    1520      424388 :     write_indices_ivas_fx( st_ivas, outputBitStream, numOutBits
    1521             : #ifdef DBG_BITSTREAM_ANALYSIS
    1522             :                            ,
    1523             :                            frame
    1524             : #endif
    1525             :     );
    1526             : }
    1527             : /* Reset switching flag before next call - can be set to "true" by some setters */
    1528      424388 : hIvasEnc->switchingActive = false;
    1529      424388 : move16();
    1530             : 
    1531      424388 : return error;
    1532             : }
    1533             : 
    1534             : 
    1535             : /*---------------------------------------------------------------------*
    1536             :  * IVAS_ENC_SetBandwidth()
    1537             :  *
    1538             :  *
    1539             :  *---------------------------------------------------------------------*/
    1540             : 
    1541       18150 : ivas_error IVAS_ENC_SetBandwidth(
    1542             :     IVAS_ENC_HANDLE hIvasEnc,             /* i/o: IVAS encoder handle               */
    1543             :     const IVAS_ENC_BANDWIDTH maxBandwidth /* i  : bandwidth limitation to be used   */
    1544             : )
    1545             : {
    1546             :     ivas_error error;
    1547             : 
    1548             :     /* Do additional checks for user-facing function */
    1549       18150 :     IF( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
    1550             :     {
    1551           0 :         return error;
    1552             :     }
    1553             : 
    1554             :     /* Use internal function to set bandiwdth */
    1555       18150 :     return setBandwidth_fx( hIvasEnc, maxBandwidth );
    1556             : }
    1557             : 
    1558             : 
    1559             : /*---------------------------------------------------------------------*
    1560             :  * IVAS_ENC_SetBitrate()
    1561             :  *
    1562             :  *
    1563             :  *---------------------------------------------------------------------*/
    1564             : 
    1565       56138 : ivas_error IVAS_ENC_SetBitrate(
    1566             :     IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle                        */
    1567             :     const Word32 totalBitrate /* i  : requested bitrate of the output bitstream  */
    1568             : )
    1569             : {
    1570             :     ivas_error error;
    1571             : 
    1572             :     /* Do additional checks for user-facing function */
    1573       56138 :     IF( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
    1574             :     {
    1575           0 :         return error;
    1576             :     }
    1577             : 
    1578             :     /* Use internal function to set bitrate */
    1579       56138 :     return setBitrate( hIvasEnc, totalBitrate );
    1580             : }
    1581             : 
    1582             : 
    1583             : /*---------------------------------------------------------------------*
    1584             :  * IVAS_ENC_SetChannelAwareConfig()
    1585             :  *
    1586             :  * Sets the configuration of channel-aware mode. Since CA mode is only
    1587             :  * supported at 13.2 kbps, this function has no effect at other bitrates.
    1588             :  *---------------------------------------------------------------------*/
    1589             : 
    1590           0 : ivas_error IVAS_ENC_SetChannelAwareConfig(
    1591             :     IVAS_ENC_HANDLE hIvasEnc,                    /* i/o: IVAS encoder handle                                                                                 */
    1592             :     const IVAS_ENC_CHANNEL_AWARE_CONFIG rfConfig /* i  : configuration of channel-aware mode                                                                 */
    1593             : )
    1594             : {
    1595             :     ivas_error error;
    1596             : 
    1597             :     /* Do additional checks for user-facing function */
    1598           0 :     IF( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
    1599             :     {
    1600           0 :         return error;
    1601             :     }
    1602             : 
    1603             :     /* Use internal function to set CA config */
    1604           0 :     return setChannelAwareConfig_fx( hIvasEnc, rfConfig );
    1605             : }
    1606             : 
    1607             : #ifdef DEBUGGING
    1608             : /*---------------------------------------------------------------------*
    1609             :  * IVAS_ENC_SetForcedMode()
    1610             :  *
    1611             :  *
    1612             :  *---------------------------------------------------------------------*/
    1613             : 
    1614             : ivas_error IVAS_ENC_SetForcedMode(
    1615             :     IVAS_ENC_HANDLE hIvasEnc,             /* i/o: IVAS encoder handle   */
    1616             :     const IVAS_ENC_FORCED_MODE forcedMode /* i  : forced coding mode    */
    1617             : #ifdef DEBUG_FORCE_DIR
    1618             :     ,
    1619             :     const char *forcedModeDir /* i  : directory containing external binary files for modes/parameters enforcement */
    1620             : #endif
    1621             : )
    1622             : {
    1623             :     int16_t newForced;
    1624             :     ivas_error error;
    1625             : 
    1626             :     /* Do additional checks for user-facing function */
    1627             :     if ( ( error = doCommonSetterChecks( hIvasEnc ) ) != IVAS_ERR_OK )
    1628             :     {
    1629             :         return error;
    1630             :     }
    1631             : 
    1632             : #ifdef DEBUG_FORCE_DIR
    1633             :     hIvasEnc->st_ivas->hEncoderConfig->force_dir[0] = '\0';
    1634             :     if ( forcedMode < IVAS_ENC_FORCE_FILE )
    1635             :     {
    1636             :         if ( ( error = forcedModeApiToInternal( forcedMode, &newForced ) ) != IVAS_ERR_OK )
    1637             :         {
    1638             :             return error;
    1639             :         }
    1640             : 
    1641             :         if ( hIvasEnc->st_ivas->hEncoderConfig->force != newForced )
    1642             :         {
    1643             :             hIvasEnc->st_ivas->hEncoderConfig->force = newForced;
    1644             :             hIvasEnc->switchingActive = true;
    1645             :         }
    1646             :     }
    1647             :     else if ( forcedMode == IVAS_ENC_FORCE_DIR )
    1648             :     {
    1649             :         strcpy( hIvasEnc->st_ivas->hEncoderConfig->force_dir, forcedModeDir );
    1650             :         hIvasEnc->st_ivas->hEncoderConfig->force = IVAS_ENC_FORCE_UNFORCED;
    1651             :     }
    1652             : #else
    1653             :     if ( ( error = forcedModeApiToInternal( forcedMode, &newForced ) ) != IVAS_ERR_OK )
    1654             :     {
    1655             :         return error;
    1656             :     }
    1657             : 
    1658             :     if ( hIvasEnc->st_ivas->hEncoderConfig->force != newForced )
    1659             :     {
    1660             :         hIvasEnc->st_ivas->hEncoderConfig->force = newForced;
    1661             :         hIvasEnc->switchingActive = true;
    1662             :     }
    1663             : #endif
    1664             : 
    1665             :     return IVAS_ERR_OK;
    1666             : }
    1667             : #endif /* DEBUGGING */
    1668             : 
    1669             : /*---------------------------------------------------------------------*
    1670             :  * IVAS_ENC_GetDefaultBandwidth()
    1671             :  *
    1672             :  *
    1673             :  *---------------------------------------------------------------------*/
    1674             : 
    1675         448 : IVAS_ENC_BANDWIDTH IVAS_ENC_GetDefaultBandwidth( const bool isEVS )
    1676             : {
    1677         448 :     IVAS_ENC_BANDWIDTH bwidth = IVAS_ENC_BANDWIDTH_FB;
    1678             : 
    1679         448 :     if ( isEVS )
    1680             :     {
    1681           3 :         bwidth = IVAS_ENC_BANDWIDTH_SWB;
    1682             :     }
    1683             : 
    1684         448 :     return bwidth;
    1685             : }
    1686             : 
    1687             : 
    1688             : /*---------------------------------------------------------------------*
    1689             :  * IVAS_ENC_GetDefaultDtxConfig()
    1690             :  *
    1691             :  *
    1692             :  *---------------------------------------------------------------------*/
    1693             : 
    1694         624 : IVAS_ENC_DTX_CONFIG IVAS_ENC_GetDefaultDtxConfig( void )
    1695             : {
    1696             :     IVAS_ENC_DTX_CONFIG defaultDtxConfig;
    1697         624 :     defaultDtxConfig.enabled = false;
    1698         624 :     defaultDtxConfig.SID_interval = 0;
    1699         624 :     move16();
    1700         624 :     defaultDtxConfig.variable_SID_rate = false;
    1701             : 
    1702         624 :     return defaultDtxConfig;
    1703             : }
    1704             : 
    1705             : 
    1706             : /*---------------------------------------------------------------------*
    1707             :  * IVAS_ENC_GetDefaultChannelAwareConfig()
    1708             :  *
    1709             :  *
    1710             :  *---------------------------------------------------------------------*/
    1711             : 
    1712        1245 : IVAS_ENC_CHANNEL_AWARE_CONFIG IVAS_ENC_GetDefaultChannelAwareConfig( void )
    1713             : {
    1714             :     IVAS_ENC_CHANNEL_AWARE_CONFIG defaultCaConfig;
    1715        1245 :     defaultCaConfig.channelAwareModeEnabled = 0;
    1716        1245 :     move16();
    1717        1245 :     defaultCaConfig.fec_indicator = IVAS_ENC_FEC_HI;
    1718        1245 :     move16();
    1719        1245 :     defaultCaConfig.fec_offset = 0;
    1720        1245 :     move16();
    1721             : 
    1722        1245 :     return defaultCaConfig;
    1723             : }
    1724             : 
    1725             : 
    1726             : /*---------------------------------------------------------------------*
    1727             :  * IVAS_ENC_GetErrorMessage()
    1728             :  *
    1729             :  *
    1730             :  *---------------------------------------------------------------------*/
    1731             : 
    1732           0 : const char *IVAS_ENC_GetErrorMessage(
    1733             :     ivas_error error /* i  : encoder error code enum  */
    1734             : )
    1735             : {
    1736           0 :     return ivas_error_to_string( error );
    1737             : }
    1738             : 
    1739             : 
    1740             : /*---------------------------------------------------------------------*
    1741             :  * Local functions
    1742             :  *---------------------------------------------------------------------*/
    1743             : 
    1744         624 : static ivas_error printConfigInfo_enc(
    1745             :     IVAS_ENC_HANDLE hIvasEnc,
    1746             :     const Word16 channelAwareModeEnabled )
    1747             : {
    1748             :     Encoder_Struct *st_ivas;
    1749             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1750             :     Word16 newBandwidthApi;
    1751             :     ivas_error error;
    1752             : 
    1753         624 :     st_ivas = hIvasEnc->st_ivas;
    1754         624 :     hEncoderConfig = st_ivas->hEncoderConfig;
    1755             : 
    1756             :     /*-----------------------------------------------------------------*
    1757             :      * Print input signal sampling frequency
    1758             :      *-----------------------------------------------------------------*/
    1759             : 
    1760         624 :     fprintf( stdout, "Input sampling rate:    %d Hz\n", hEncoderConfig->input_Fs );
    1761             : 
    1762             :     /*-----------------------------------------------------------------*
    1763             :      * Print bitrate
    1764             :      *-----------------------------------------------------------------*/
    1765             : 
    1766         624 :     IF( st_ivas->hEncoderConfig->Opt_SC_VBR )
    1767             :     {
    1768           0 :         fprintf( stdout, "Average bitrate:        %.2f kbps\n", (float) ACELP_5k90 / 1000 );
    1769             :     }
    1770             :     ELSE
    1771             :     {
    1772         624 :         fprintf( stdout, "Bitrate:                %.2f kbps\n", (float) hEncoderConfig->ivas_total_brate / 1000 );
    1773             :     }
    1774             : 
    1775             :     /*-----------------------------------------------------------------*
    1776             :      * Print IVAS format
    1777             :      *-----------------------------------------------------------------*/
    1778             : 
    1779         624 :     IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1780             :     {
    1781           3 :         IF( hEncoderConfig->stereo_dmx_evs )
    1782             :         {
    1783           2 :             fprintf( stdout, "IVAS format:            stereo downmix to bit-exact EVS mono\n" );
    1784             :         }
    1785             :         ELSE
    1786             :         {
    1787           1 :             fprintf( stdout, "IVAS format:            bit-exact EVS mono\n" );
    1788             :         }
    1789             :     }
    1790         621 :     ELSE IF( hEncoderConfig->ivas_format == STEREO_FORMAT )
    1791             :     {
    1792             : #ifdef DEBUGGING
    1793             :         if ( hEncoderConfig->stereo_mode_cmdl == 1 )
    1794             :         {
    1795             :             fprintf( stdout, "IVAS format:            stereo - Unified stereo\n" );
    1796             :         }
    1797             :         else if ( hEncoderConfig->element_mode_init == IVAS_CPE_DFT )
    1798             :         {
    1799             :             fprintf( stdout, "IVAS format:            stereo - DFT stereo\n" );
    1800             :         }
    1801             :         else if ( hEncoderConfig->element_mode_init == IVAS_CPE_TD )
    1802             :         {
    1803             :             fprintf( stdout, "IVAS format:            stereo - TD stereo\n" );
    1804             :         }
    1805             :         else if ( hEncoderConfig->element_mode_init == IVAS_CPE_MDCT )
    1806             :         {
    1807             :             fprintf( stdout, "IVAS format:            stereo - MDCT stereo\n" );
    1808             :         }
    1809             : #else
    1810          68 :         if ( hEncoderConfig->element_mode_init != IVAS_CPE_MDCT )
    1811             :         {
    1812          50 :             fprintf( stdout, "IVAS format:            stereo - Unified stereo\n" );
    1813             :         }
    1814             :         else
    1815             :         {
    1816          18 :             fprintf( stdout, "IVAS format:            stereo - MDCT stereo\n" );
    1817             :         }
    1818             : #endif
    1819             :     }
    1820         553 :     ELSE IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
    1821             :     {
    1822          74 :         test();
    1823          74 :         IF( LE_32( hEncoderConfig->ivas_total_brate, ACELP_32k ) && GT_16( hEncoderConfig->nchan_inp, 2 ) )
    1824             :         {
    1825          17 :             fprintf( stdout, "IVAS format:            Param-ISM (%i streams)\n", hEncoderConfig->nchan_inp );
    1826             :         }
    1827             :         ELSE
    1828             :         {
    1829          57 :             fprintf( stdout, "IVAS format:            ISM (%i streams)\n", hEncoderConfig->nchan_inp );
    1830             :         }
    1831             :     }
    1832         479 :     ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_FORMAT ) )
    1833             :     {
    1834         244 :         fprintf( stdout, "IVAS format:            Scene Based Audio, Ambisonic order %i %s ", hEncoderConfig->sba_order, hEncoderConfig->sba_planar ? "(Planar)" : "" );
    1835         244 :         IF( hEncoderConfig->Opt_PCA_ON )
    1836             :         {
    1837           4 :             fprintf( stdout, "- PCA configured with signal adaptive decision " );
    1838             :         }
    1839             : 
    1840         244 :         fprintf( stdout, "\n" );
    1841             :     }
    1842         235 :     ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_FORMAT ) )
    1843             :     {
    1844          75 :         fprintf( stdout, "IVAS format:            MASA format\n" );
    1845             :     }
    1846         160 :     ELSE IF( EQ_16( hEncoderConfig->ivas_format, MC_FORMAT ) )
    1847             :     {
    1848          81 :         IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1 )
    1849             :         {
    1850          42 :             fprintf( stdout, "IVAS mode:              Multi-Channel 5.1 \n" );
    1851             :         }
    1852          39 :         ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1 )
    1853             :         {
    1854           4 :             fprintf( stdout, "IVAS mode:              Multi-Channel 7.1 \n" );
    1855             :         }
    1856          35 :         ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_2 )
    1857             :         {
    1858           6 :             fprintf( stdout, "IVAS mode:              Multi-Channel 5.1+2 \n" );
    1859             :         }
    1860          29 :         ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_5_1_4 )
    1861             :         {
    1862           6 :             fprintf( stdout, "IVAS mode:              Multi-Channel 5.1+4\n" );
    1863             :         }
    1864          23 :         ELSE IF( hEncoderConfig->mc_input_setup == MC_LS_SETUP_7_1_4 )
    1865             :         {
    1866          23 :             fprintf( stdout, "IVAS mode:              Multi-Channel 7.1+4\n" );
    1867             :         }
    1868             :     }
    1869          79 :     ELSE IF( EQ_16( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
    1870             :     {
    1871          35 :         fprintf( stdout, "IVAS format:            combined ISM and SBA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
    1872             :     }
    1873          44 :     ELSE IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
    1874             :     {
    1875          44 :         fprintf( stdout, "IVAS format:            combined ISM and MASA (%i ISM stream(s))\n", hEncoderConfig->nchan_ism );
    1876             :     }
    1877             : 
    1878         624 :     IF( hEncoderConfig->is_binaural )
    1879             :     {
    1880           0 :         fprintf( stdout, "Optional indication:    binaural audio\n" );
    1881             :     }
    1882             : 
    1883             :     /*-----------------------------------------------------------------*
    1884             :      * Print CNG update interval, if DTX is activated
    1885             :      *-----------------------------------------------------------------*/
    1886             : 
    1887         624 :     IF( hEncoderConfig->Opt_DTX_ON )
    1888             :     {
    1889         126 :         IF( hEncoderConfig->var_SID_rate_flag )
    1890             :         {
    1891           0 :             fprintf( stdout, "DTX:                    ON, variable CNG update interval\n" );
    1892             :         }
    1893             :         ELSE
    1894             :         {
    1895         126 :             fprintf( stdout, "DTX:                    ON, CNG update interval = %d frames\n", hEncoderConfig->interval_SID );
    1896             :         }
    1897             :     }
    1898             : 
    1899             :     /*-----------------------------------------------------------------*
    1900             :      * Print potential limitation of audio bandwidth
    1901             :      *-----------------------------------------------------------------*/
    1902             : 
    1903         624 :     IF( ( error = bandwidthApiToInternal( hIvasEnc->newBandwidthApi, &newBandwidthApi ) ) != IVAS_ERR_OK )
    1904             :     {
    1905           0 :         return error;
    1906             :     }
    1907             : 
    1908         624 :     test();
    1909         624 :     IF( st_ivas->hEncoderConfig->Opt_SC_VBR && !hEncoderConfig->Opt_DTX_ON )
    1910             :     {
    1911           0 :         return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "\nError: SC-VBR 5900 bps not supported without DTX\n\n" );
    1912             :     }
    1913             : 
    1914         624 :     IF( EQ_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    1915             :     {
    1916           3 :         IF( NE_16( newBandwidthApi, hEncoderConfig->max_bwidth ) )
    1917             :         {
    1918           0 :             IF( EQ_16( newBandwidthApi, FB ) )
    1919             :             {
    1920           0 :                 fprintf( stdout, "\nFB coding not supported below %.2f kbps. ", ACELP_16k40 / 1000.f );
    1921           0 :                 IF( EQ_16( hEncoderConfig->max_bwidth, WB ) )
    1922             :                 {
    1923           0 :                     fprintf( stdout, "Switching to WB.\n" );
    1924             :                 }
    1925             :                 ELSE
    1926             :                 {
    1927           0 :                     fprintf( stdout, "Switching to SWB.\n" );
    1928             :                 }
    1929             :             }
    1930           0 :             ELSE IF( EQ_16( newBandwidthApi, SWB ) )
    1931             :             {
    1932           0 :                 fprintf( stdout, "\nSWB coding not supported below %.2f kbps. Switching to WB.\n", ACELP_9k60 / 1000.f );
    1933             :             }
    1934             :         }
    1935             : 
    1936             :         /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
    1937           3 :         test();
    1938           3 :         test();
    1939           3 :         IF( ( ( newBandwidthApi == NB ) || EQ_32( hEncoderConfig->input_Fs, 8000 ) ) && GT_32( hEncoderConfig->ivas_total_brate, ACELP_24k40 ) )
    1940             :         {
    1941           0 :             fprintf( stdout, "\nError: Unsupported mode NB %d bps, NB mode supports rates 5900-24400 bps\n\n", hEncoderConfig->ivas_total_brate );
    1942           0 :             return IVAS_ERR_INVALID_BITRATE;
    1943             :         }
    1944             :     }
    1945             :     ELSE
    1946             :     {
    1947         621 :         IF( NE_16( newBandwidthApi, hEncoderConfig->max_bwidth ) )
    1948             :         {
    1949         104 :             IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
    1950             :             {
    1951          23 :                 fprintf( stdout, "\nFB coding not supported below %.2f kbps for %i objects. Switching to SWB.\n", hEncoderConfig->nchan_ism * MIN_BRATE_FB_ISM / 1000.f, hEncoderConfig->nchan_ism );
    1952             :             }
    1953             :             ELSE
    1954             :             {
    1955          81 :                 fprintf( stdout, "\nFB coding not supported below %.2f kbps. Switching to SWB.\n", MIN_BRATE_FB_STEREO / 1000.f );
    1956             :             }
    1957             :         }
    1958             :     }
    1959             : 
    1960             :     /*-----------------------------------------------------------------*
    1961             :      * Print Channel-aware limitation
    1962             :      *-----------------------------------------------------------------*/
    1963             : 
    1964         624 :     IF( channelAwareModeEnabled )
    1965             :     {
    1966           0 :         IF( hEncoderConfig->Opt_RF_ON == 0 )
    1967             :         {
    1968           0 :             fprintf( stdout, "\nChannel-aware mode is supported at 13.2 kbps 32/48 kHz only. Switching to normal mode.\n" );
    1969             :         }
    1970             :     }
    1971             : 
    1972         624 :     return IVAS_ERR_OK;
    1973             : }
    1974             : 
    1975             : 
    1976             : /*---------------------------------------------------------------------*
    1977             :  * setBitrate()
    1978             :  *
    1979             :  *
    1980             :  *---------------------------------------------------------------------*/
    1981             : 
    1982       56138 : static ivas_error setBitrate(
    1983             :     IVAS_ENC_HANDLE hIvasEnc,
    1984             :     const Word32 totalBitrate )
    1985             : {
    1986             :     Encoder_Struct *st_ivas;
    1987             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1988             :     ivas_error error;
    1989             : 
    1990       56138 :     st_ivas = hIvasEnc->st_ivas;
    1991       56138 :     hEncoderConfig = st_ivas->hEncoderConfig;
    1992             : 
    1993       56138 :     hEncoderConfig->ivas_total_brate = totalBitrate;
    1994       56138 :     hIvasEnc->switchingActive = true;
    1995       56138 :     move32();
    1996             : 
    1997             :     /* channel-aware mode is supported only at 13.20 kbps */
    1998       56138 :     test();
    1999       56138 :     IF( hEncoderConfig->Opt_RF_ON && NE_32( hEncoderConfig->ivas_total_brate, ACELP_13k20 ) )
    2000             :     {
    2001           0 :         assert( 0 && "\nChannel-aware mode is supported only at 13.20 kbps\n" );
    2002             :         hEncoderConfig->Opt_RF_ON = 0;
    2003             :         move16();
    2004             :     }
    2005             : 
    2006       56138 :     IF( EQ_32( hEncoderConfig->ivas_total_brate, ACELP_5k90 ) )
    2007             :     {
    2008           0 :         st_ivas->hEncoderConfig->Opt_SC_VBR = 1;
    2009           0 :         hEncoderConfig->ivas_total_brate = ACELP_7k20;
    2010           0 :         move16();
    2011           0 :         move32();
    2012             :     }
    2013             :     ELSE
    2014             :     {
    2015       56138 :         st_ivas->hEncoderConfig->Opt_SC_VBR = 0;
    2016       56138 :         move16();
    2017             :     }
    2018             : 
    2019             :     /* check if the entered bitrate is supported */
    2020       56138 :     IF( hEncoderConfig->element_mode_init > EVS_MONO )
    2021             :     {
    2022       56138 :         IF( !is_IVAS_bitrate_fx( hEncoderConfig->ivas_total_brate ) )
    2023             :         {
    2024           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2025             :         }
    2026             :     }
    2027             :     ELSE
    2028             :     {
    2029           0 :         IF( !is_EVS_bitrate( hEncoderConfig->ivas_total_brate, &hEncoderConfig->Opt_AMR_WB ) )
    2030             :         {
    2031           0 :             return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Incorrect bitrate specification in EVS mono: %d", hEncoderConfig->ivas_total_brate );
    2032             :         }
    2033             : 
    2034             :         /* in case of 8kHz signal, limit the total bitrate to 24.40 kbps */
    2035           0 :         test();
    2036           0 :         IF( EQ_32( hEncoderConfig->input_Fs, 8000 ) && GT_32( hEncoderConfig->ivas_total_brate, ACELP_24k40 ) )
    2037             :         {
    2038           0 :             hEncoderConfig->ivas_total_brate = ACELP_24k40;
    2039           0 :             move32();
    2040             :         }
    2041             :     }
    2042             : 
    2043       56138 :     IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
    2044             :     {
    2045       10086 :         IF( ( error = sanitizeBitrateISM_fx( hEncoderConfig, hIvasEnc->extMetadataApi ) ) != IVAS_ERR_OK )
    2046             :         {
    2047           0 :             return error;
    2048             :         }
    2049             :     }
    2050             : 
    2051       56138 :     st_ivas->codec_mode = MODE1;
    2052       56138 :     move16();
    2053             : 
    2054       56138 :     IF( hEncoderConfig->element_mode_init == EVS_MONO )
    2055             :     {
    2056           0 :         IF( hEncoderConfig->Opt_AMR_WB )
    2057             :         {
    2058           0 :             st_ivas->codec_mode = MODE1;
    2059           0 :             move16();
    2060             :         }
    2061             :         ELSE
    2062             :         {
    2063           0 :             st_ivas->codec_mode = get_codec_mode( hEncoderConfig->ivas_total_brate );
    2064           0 :             move16();
    2065             :         }
    2066             :     }
    2067             : 
    2068       56138 :     return IVAS_ERR_OK;
    2069             : }
    2070             : 
    2071             : 
    2072             : /*---------------------------------------------------------------------*
    2073             :  * setChannelAwareConfig()
    2074             :  *
    2075             :  *
    2076             :  *---------------------------------------------------------------------*/
    2077             : 
    2078         624 : static ivas_error setChannelAwareConfig_fx(
    2079             :     IVAS_ENC_HANDLE hIvasEnc,
    2080             :     const IVAS_ENC_CHANNEL_AWARE_CONFIG caConfig )
    2081             : {
    2082             :     Word16 newFecIndicator;
    2083             :     ivas_error error;
    2084             :     Encoder_Struct *st_ivas;
    2085             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    2086             : 
    2087         624 :     st_ivas = hIvasEnc->st_ivas;
    2088         624 :     hEncoderConfig = st_ivas->hEncoderConfig;
    2089             : 
    2090             :     /* channel-aware mode is supported only at 13.20 kbps and with WB or SWB bandwidth */
    2091         624 :     test();
    2092         624 :     test();
    2093         624 :     test();
    2094         624 :     IF( ( caConfig.channelAwareModeEnabled && NE_32( st_ivas->hEncoderConfig->ivas_total_brate, ACELP_13k20 ) ) || ( hEncoderConfig->Opt_RF_ON && EQ_32( hEncoderConfig->input_Fs, 8000 ) ) )
    2095             :     {
    2096           0 :         hEncoderConfig->Opt_RF_ON = 0;
    2097           0 :         move16();
    2098           0 :         hEncoderConfig->rf_fec_offset = 0;
    2099           0 :         move16();
    2100           0 :         return IVAS_ERR_OK;
    2101             :     }
    2102             : 
    2103         624 :     IF( caConfig.channelAwareModeEnabled )
    2104             :     {
    2105           0 :         hEncoderConfig->Opt_RF_ON = 1;
    2106           0 :         move16();
    2107             : 
    2108             :         /* Convert FEC indicator from API type */
    2109           0 :         IF( ( error = fecIndicatorApiToInternal( caConfig.fec_indicator, &newFecIndicator ) ) != IVAS_ERR_OK )
    2110             :         {
    2111           0 :             return error;
    2112             :         }
    2113             : 
    2114             :         /* Set new values only if they differ from current values */
    2115           0 :         test();
    2116           0 :         IF( ( NE_16( newFecIndicator, hEncoderConfig->rf_fec_indicator ) || NE_16( caConfig.fec_offset, hEncoderConfig->rf_fec_offset ) ) )
    2117             :         {
    2118           0 :             hEncoderConfig->rf_fec_indicator = newFecIndicator;
    2119           0 :             move16();
    2120             : 
    2121             :             /* Check if new FEC offset has a valid value */
    2122           0 :             test();
    2123           0 :             test();
    2124           0 :             test();
    2125           0 :             test();
    2126           0 :             IF( EQ_16( caConfig.fec_offset, 0 ) || EQ_16( caConfig.fec_offset, 2 ) || EQ_16( caConfig.fec_offset, 3 ) || EQ_16( caConfig.fec_offset, 5 ) || EQ_16( caConfig.fec_offset, 7 ) )
    2127             :             {
    2128           0 :                 hEncoderConfig->rf_fec_offset = caConfig.fec_offset;
    2129           0 :                 move16();
    2130             :             }
    2131             :             ELSE
    2132             :             {
    2133           0 :                 return IVAS_ERR_INVALID_FEC_OFFSET;
    2134             :             }
    2135             : 
    2136           0 :             hIvasEnc->switchingActive = true;
    2137             :         }
    2138             : 
    2139             :         /* Save a copy of FEC offset value - needed during encoding */
    2140           0 :         hIvasEnc->rf_fec_offset_loc = hEncoderConfig->rf_fec_offset;
    2141           0 :         move16();
    2142             :     }
    2143             :     ELSE
    2144             :     {
    2145         624 :         hEncoderConfig->Opt_RF_ON = 0;
    2146         624 :         move16();
    2147             :     }
    2148             : 
    2149         624 :     return IVAS_ERR_OK;
    2150             : }
    2151             : /*---------------------------------------------------------------------*
    2152             :  * doCommonConfigureChecks()
    2153             :  *
    2154             :  *
    2155             :  *---------------------------------------------------------------------*/
    2156             : 
    2157         624 : static ivas_error doCommonConfigureChecks(
    2158             :     IVAS_ENC_HANDLE hIvasEnc )
    2159             : {
    2160         624 :     test();
    2161         624 :     IF( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
    2162             :     {
    2163           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2164             :     }
    2165             : 
    2166         624 :     IF( hIvasEnc->isConfigured )
    2167             :     {
    2168           0 :         return IVAS_ERR_RECONFIGURE_NOT_SUPPORTED;
    2169             :     }
    2170             : 
    2171         624 :     return IVAS_ERR_OK;
    2172             : }
    2173             : 
    2174             : 
    2175             : /*---------------------------------------------------------------------*
    2176             :  * doCommonSetterChecks()
    2177             :  *
    2178             :  *
    2179             :  *---------------------------------------------------------------------*/
    2180             : 
    2181       74288 : static ivas_error doCommonSetterChecks(
    2182             :     IVAS_ENC_HANDLE hIvasEnc )
    2183             : {
    2184       74288 :     test();
    2185       74288 :     IF( hIvasEnc == NULL || hIvasEnc->st_ivas == NULL )
    2186             :     {
    2187           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2188             :     }
    2189             : 
    2190             :     /* Currently settings can be changed only after configuration step */
    2191       74288 :     IF( !hIvasEnc->isConfigured )
    2192             :     {
    2193           0 :         return IVAS_ERR_NOT_CONFIGURED;
    2194             :     }
    2195             : 
    2196       74288 :     return IVAS_ERR_OK;
    2197             : }
    2198             : 
    2199             : 
    2200             : /*---------------------------------------------------------------------*
    2201             :  * sanitizeBandwidth()
    2202             :  *
    2203             :  *
    2204             :  *---------------------------------------------------------------------*/
    2205             : 
    2206      425012 : static ivas_error sanitizeBandwidth_fx(
    2207             :     const IVAS_ENC_HANDLE hIvasEnc )
    2208             : {
    2209             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    2210             :     Word16 max_bwidth_tmp;
    2211             : 
    2212      425012 :     hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
    2213             : 
    2214      425012 :     max_bwidth_tmp = hIvasEnc->newBandwidthApi;
    2215             : 
    2216             :     /* Prevent st_ivas->max_bwidth from being higher than Fs/2 */
    2217      425012 :     test();
    2218      425012 :     test();
    2219      425012 :     test();
    2220      425012 :     IF( EQ_32( hEncoderConfig->input_Fs, 8000 ) && ( max_bwidth_tmp > NB ) )
    2221             :     {
    2222           0 :         max_bwidth_tmp = NB;
    2223           0 :         move16();
    2224             :     }
    2225      425012 :     ELSE IF( EQ_32( hEncoderConfig->input_Fs, 16000 ) && GT_16( max_bwidth_tmp, WB ) )
    2226             :     {
    2227           0 :         max_bwidth_tmp = WB;
    2228           0 :         move16();
    2229             :     }
    2230      425012 :     ELSE IF( EQ_32( hEncoderConfig->input_Fs, 32000 ) && GT_16( max_bwidth_tmp, SWB ) )
    2231             :     {
    2232        1351 :         max_bwidth_tmp = SWB;
    2233        1351 :         move16();
    2234             :     }
    2235             : 
    2236             :     /* NB coding not supported in IVAS. Switching to WB. */
    2237      425012 :     test();
    2238      425012 :     test();
    2239      425012 :     IF( ( max_bwidth_tmp == NB ) && NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    2240             :     {
    2241         301 :         IF( GE_32( hEncoderConfig->input_Fs, 16000 ) )
    2242             :         {
    2243         301 :             max_bwidth_tmp = WB;
    2244         301 :             move16();
    2245             :         }
    2246             :         ELSE
    2247             :         {
    2248           0 :             return IVAS_ERR_INVALID_BITRATE;
    2249             :         }
    2250             :     }
    2251             : 
    2252      425012 :     IF( NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    2253             :     {
    2254      421909 :         Word32 quo = 0, rem;
    2255      421909 :         move32();
    2256      421909 :         IF( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) )
    2257             :         {
    2258      103900 :             iDiv_and_mod_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->nchan_ism, &quo, &rem, 0 );
    2259             :         }
    2260             : 
    2261      421909 :         test();
    2262      421909 :         test();
    2263      421909 :         test();
    2264      421909 :         test();
    2265      421909 :         IF( EQ_16( max_bwidth_tmp, FB ) && ( ( NE_16( hEncoderConfig->ivas_format, ISM_FORMAT ) && LT_32( hEncoderConfig->ivas_total_brate, MIN_BRATE_FB_STEREO ) ) ||
    2266             :                                              ( EQ_16( hEncoderConfig->ivas_format, ISM_FORMAT ) && LT_32( quo, MIN_BRATE_FB_ISM ) ) ) )
    2267             :         {
    2268       60842 :             max_bwidth_tmp = SWB;
    2269       60842 :             move16();
    2270             :         }
    2271             :     }
    2272             : 
    2273      425012 :     IF( NE_16( hEncoderConfig->max_bwidth, max_bwidth_tmp ) )
    2274             :     {
    2275        4978 :         hEncoderConfig->max_bwidth = max_bwidth_tmp;
    2276        4978 :         move16();
    2277        4978 :         hIvasEnc->switchingActive = true;
    2278             :     }
    2279             : 
    2280      425012 :     return IVAS_ERR_OK;
    2281             : }
    2282             : /*---------------------------------------------------------------------*
    2283             :  * sanitizeBitrateISM_fx()
    2284             :  *
    2285             :  *
    2286             :  *---------------------------------------------------------------------*/
    2287             : 
    2288       10160 : static ivas_error sanitizeBitrateISM_fx(
    2289             :     const ENCODER_CONFIG_HANDLE hEncoderConfig,
    2290             :     const bool extMetadataApi )
    2291             : {
    2292       10160 :     test();
    2293       10160 :     IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_128k ) && EQ_16( hEncoderConfig->nchan_inp, 1 ) )
    2294             :     {
    2295           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 1 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2296             :     }
    2297             : 
    2298       10160 :     test();
    2299       10160 :     IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_256k ) && EQ_16( hEncoderConfig->nchan_inp, 2 ) )
    2300             :     {
    2301           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2302             :     }
    2303             : 
    2304       10160 :     test();
    2305       10160 :     IF( GT_32( hEncoderConfig->ivas_total_brate, IVAS_384k ) && EQ_16( hEncoderConfig->nchan_inp, 3 ) )
    2306             :     {
    2307           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2308             :     }
    2309             : 
    2310       10160 :     test();
    2311       10160 :     IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_16k4 ) && EQ_16( hEncoderConfig->nchan_inp, 2 ) )
    2312             :     {
    2313           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 2 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2314             :     }
    2315             : 
    2316       10160 :     test();
    2317       10160 :     IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) && EQ_16( hEncoderConfig->nchan_inp, 3 ) )
    2318             :     {
    2319           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 3 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2320             :     }
    2321             : 
    2322       10160 :     test();
    2323       10160 :     IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) && EQ_16( hEncoderConfig->nchan_inp, 4 ) )
    2324             :     {
    2325           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 4 ISM specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    2326             :     }
    2327             : 
    2328       10160 :     IF( extMetadataApi )
    2329             :     {
    2330        1007 :         hEncoderConfig->ism_extended_metadata_flag = (Word16) GE_32( hEncoderConfig->ivas_total_brate, ISM_EXTENDED_METADATA_BRATE );
    2331        1007 :         move16();
    2332             :     }
    2333             :     ELSE
    2334             :     {
    2335        9153 :         hEncoderConfig->ism_extended_metadata_flag = 0;
    2336        9153 :         move16();
    2337             :     }
    2338             : 
    2339       10160 :     return IVAS_ERR_OK;
    2340             : }
    2341             : /*---------------------------------------------------------------------*
    2342             :  * setBandwidth_fx()
    2343             :  *
    2344             :  *
    2345             :  *---------------------------------------------------------------------*/
    2346             : 
    2347       18774 : static ivas_error setBandwidth_fx(
    2348             :     IVAS_ENC_HANDLE hIvasEnc,
    2349             :     const IVAS_ENC_BANDWIDTH maxBandwidth )
    2350             : {
    2351             :     ivas_error error;
    2352             :     Word16 newBandwidth;
    2353             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    2354             : 
    2355       18774 :     hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;
    2356             : 
    2357             :     /* Convert bandwidth from API type */
    2358       18774 :     IF( ( error = bandwidthApiToInternal( maxBandwidth, &newBandwidth ) ) != IVAS_ERR_OK )
    2359             :     {
    2360           0 :         return error;
    2361             :     }
    2362             : 
    2363       18774 :     hIvasEnc->newBandwidthApi = newBandwidth;
    2364             : 
    2365             :     /* NB coding not supported in IVAS. Switching to WB. */
    2366       18774 :     test();
    2367       18774 :     test();
    2368       18774 :     IF( ( newBandwidth == NB ) && NE_16( hEncoderConfig->ivas_format, UNDEFINED_FORMAT ) && NE_16( hEncoderConfig->ivas_format, MONO_FORMAT ) )
    2369             :     {
    2370         301 :         newBandwidth = WB;
    2371         301 :         move16();
    2372             :     }
    2373             : 
    2374       18774 :     IF( hEncoderConfig->max_bwidth != newBandwidth )
    2375             :     {
    2376        3401 :         hEncoderConfig->max_bwidth = newBandwidth;
    2377        3401 :         hIvasEnc->switchingActive = true;
    2378        3401 :         move16();
    2379             :     }
    2380             : 
    2381       18774 :     return IVAS_ERR_OK;
    2382             : }
    2383             : 
    2384             : /*---------------------------------------------------------------------*
    2385             :  * resetIsmMetadataProvidedFlags()
    2386             :  *
    2387             :  *
    2388             :  *---------------------------------------------------------------------*/
    2389             : 
    2390      104450 : static void resetIsmMetadataProvidedFlags(
    2391             :     IVAS_ENC_HANDLE hIvasEnc )
    2392             : {
    2393             :     Word16 i;
    2394             : 
    2395      522250 :     FOR( i = 0; i < MAX_NUM_OBJECTS; ++i )
    2396             :     {
    2397      417800 :         hIvasEnc->ismMetadataProvided[i] = false;
    2398             :     }
    2399             : 
    2400      104450 :     return;
    2401             : }
    2402             : 
    2403             : 
    2404             : /*---------------------------------------------------------------------*
    2405             :  * bandwidthApiToInternal()
    2406             :  *
    2407             :  *
    2408             :  *---------------------------------------------------------------------*/
    2409             : 
    2410       19398 : static ivas_error bandwidthApiToInternal(
    2411             :     const IVAS_ENC_BANDWIDTH maxBandwidth,
    2412             :     Word16 *internalMaxBandwidth )
    2413             : {
    2414       19398 :     SWITCH( maxBandwidth )
    2415             :     {
    2416         302 :         case IVAS_ENC_BANDWIDTH_NB:
    2417         302 :             *internalMaxBandwidth = NB;
    2418         302 :             BREAK;
    2419        6316 :         case IVAS_ENC_BANDWIDTH_WB:
    2420        6316 :             *internalMaxBandwidth = WB;
    2421        6316 :             BREAK;
    2422        6306 :         case IVAS_ENC_BANDWIDTH_SWB:
    2423        6306 :             *internalMaxBandwidth = SWB;
    2424        6306 :             BREAK;
    2425        6474 :         case IVAS_ENC_BANDWIDTH_FB:
    2426        6474 :             *internalMaxBandwidth = FB;
    2427        6474 :             BREAK;
    2428           0 :         case IVAS_ENC_BANDWIDTH_UNDEFINED:
    2429             :         default:
    2430           0 :             return IVAS_ERR_INVALID_BANDWIDTH;
    2431             :     }
    2432             : 
    2433       19398 :     return IVAS_ERR_OK;
    2434             : }
    2435             : 
    2436             : 
    2437             : /*---------------------------------------------------------------------*
    2438             :  * fecIndicatorApiToInternal()
    2439             :  *
    2440             :  *
    2441             :  *---------------------------------------------------------------------*/
    2442             : 
    2443           0 : static ivas_error fecIndicatorApiToInternal(
    2444             :     const IVAS_ENC_FEC_INDICATOR fecIndicator,
    2445             :     Word16 *fecIndicatorInternal )
    2446             : {
    2447           0 :     SWITCH( fecIndicator )
    2448             :     {
    2449           0 :         case IVAS_ENC_FEC_LO:
    2450           0 :             *fecIndicatorInternal = 0;
    2451           0 :             BREAK;
    2452           0 :         case IVAS_ENC_FEC_HI:
    2453           0 :             *fecIndicatorInternal = 1;
    2454           0 :             BREAK;
    2455           0 :         default:
    2456           0 :             return IVAS_ERR_INTERNAL;
    2457             :     }
    2458             : 
    2459           0 :     return IVAS_ERR_OK;
    2460             : }
    2461             : 
    2462             : #ifdef DEBUGGING
    2463             : /*---------------------------------------------------------------------*
    2464             :  * forcedModeApiToInternal()
    2465             :  *
    2466             :  *
    2467             :  *---------------------------------------------------------------------*/
    2468             : 
    2469             : static ivas_error forcedModeApiToInternal(
    2470             :     IVAS_ENC_FORCED_MODE forcedMode,
    2471             :     int16_t *forcedModeInternal )
    2472             : {
    2473             :     switch ( forcedMode )
    2474             :     {
    2475             :         case IVAS_ENC_FORCE_SPEECH:
    2476             :             *forcedModeInternal = FORCE_SPEECH;
    2477             :             break;
    2478             :         case IVAS_ENC_FORCE_MUSIC:
    2479             :             *forcedModeInternal = FORCE_MUSIC;
    2480             :             break;
    2481             :         case IVAS_ENC_FORCE_ACELP:
    2482             :             *forcedModeInternal = FORCE_ACELP;
    2483             :             break;
    2484             :         case IVAS_ENC_FORCE_GSC:
    2485             :             *forcedModeInternal = FORCE_GSC;
    2486             :             break;
    2487             : #ifdef SUPPORT_FORCE_TCX10_TCX20
    2488             :         case IVAS_ENC_FORCE_TCX10:
    2489             :             *forcedModeInternal = FORCE_TCX10;
    2490             :             break;
    2491             :         case IVAS_ENC_FORCE_TCX20:
    2492             :             *forcedModeInternal = FORCE_TCX20;
    2493             :             break;
    2494             : #else
    2495             :         case IVAS_ENC_FORCE_TCX:
    2496             :             *forcedModeInternal = FORCE_TCX;
    2497             :             break;
    2498             : #endif
    2499             :         case IVAS_ENC_FORCE_HQ:
    2500             :             *forcedModeInternal = FORCE_HQ;
    2501             :             break;
    2502             :         case IVAS_ENC_FORCE_UNFORCED:
    2503             :             *forcedModeInternal = -1;
    2504             :             break;
    2505             :         default:
    2506             :             return IVAS_ERR_INVALID_FORCE_MODE;
    2507             :             break;
    2508             :     }
    2509             : 
    2510             :     return IVAS_ERR_OK;
    2511             : }
    2512             : #endif
    2513             : 
    2514             : /*---------------------------------------------------------------------*
    2515             :  * IVAS_ENC_PrintConfig()
    2516             :  *
    2517             :  *
    2518             :  *---------------------------------------------------------------------*/
    2519             : 
    2520         624 : ivas_error IVAS_ENC_PrintConfig(
    2521             :     const IVAS_ENC_HANDLE hIvasEnc,      /* i  : IVAS encoder handle             */
    2522             :     const Word16 channelAwareModeEnabled /* i  : channel-aware mode enabled flag */
    2523             : )
    2524             : {
    2525         624 :     return printConfigInfo_enc( hIvasEnc, channelAwareModeEnabled );
    2526             : }
    2527             : 
    2528             : 
    2529             : /*---------------------------------------------------------------------*
    2530             :  * IVAS_ENC_PrintDisclaimer()
    2531             :  *
    2532             :  * Print IVAS disclaimer to console
    2533             :  *---------------------------------------------------------------------*/
    2534             : 
    2535         624 : void IVAS_ENC_PrintDisclaimer( void )
    2536             : {
    2537         624 :     print_disclaimer( stderr );
    2538             : 
    2539         624 :     return;
    2540             : }
    2541             : 
    2542             : 
    2543             : /*---------------------------------------------------------------------*
    2544             :  * init_encoder_config()
    2545             :  *
    2546             :  * Initialize Encoder Config. handle
    2547             :  *---------------------------------------------------------------------*/
    2548             : 
    2549         624 : static void init_encoder_config(
    2550             :     ENCODER_CONFIG_HANDLE hEncoderConfig /* o  : configuration structure */
    2551             : )
    2552             : {
    2553         624 :     hEncoderConfig->ivas_total_brate = ACELP_12k65;
    2554         624 :     move32();
    2555         624 :     hEncoderConfig->max_bwidth = SWB;
    2556         624 :     move16();
    2557         624 :     hEncoderConfig->input_Fs = 16000;
    2558         624 :     move32();
    2559         624 :     hEncoderConfig->nchan_inp = 1;
    2560         624 :     move16();
    2561         624 :     hEncoderConfig->element_mode_init = EVS_MONO;
    2562         624 :     move16();
    2563         624 :     hEncoderConfig->ivas_format = UNDEFINED_FORMAT;
    2564         624 :     move16();
    2565         624 :     hEncoderConfig->is_binaural = 0;
    2566         624 :     move16();
    2567         624 :     hEncoderConfig->Opt_SC_VBR = 0;
    2568         624 :     move16();
    2569         624 :     hEncoderConfig->last_Opt_SC_VBR = 0;
    2570         624 :     move16();
    2571         624 :     hEncoderConfig->Opt_AMR_WB = 0;
    2572         624 :     move16();
    2573         624 :     hEncoderConfig->Opt_DTX_ON = 0;
    2574         624 :     move16();
    2575         624 :     hEncoderConfig->Opt_RF_ON = 0;
    2576         624 :     move16();
    2577         624 :     hEncoderConfig->rf_fec_offset = 0;
    2578         624 :     move16();
    2579         624 :     hEncoderConfig->rf_fec_indicator = 1;
    2580         624 :     move16();
    2581         624 :     hEncoderConfig->interval_SID = FIXED_SID_RATE;
    2582         624 :     move16();
    2583         624 :     hEncoderConfig->var_SID_rate_flag = 1;
    2584         624 :     move16();
    2585         624 :     hEncoderConfig->mc_input_setup = MC_LS_SETUP_INVALID;
    2586         624 :     move16();
    2587         624 :     hEncoderConfig->stereo_dmx_evs = 0;
    2588         624 :     move16();
    2589         624 :     hEncoderConfig->nchan_ism = 0;
    2590         624 :     move16();
    2591         624 :     hEncoderConfig->sba_order = 0;
    2592         624 :     move16();
    2593         624 :     hEncoderConfig->sba_planar = 0;
    2594         624 :     move16();
    2595         624 :     hEncoderConfig->ism_extended_metadata_flag = 0;
    2596         624 :     move16();
    2597             : #ifdef DEBUGGING
    2598             :     hEncoderConfig->stereo_mode_cmdl = 0;
    2599             :     hEncoderConfig->force = -1;
    2600             :     hEncoderConfig->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
    2601             : #endif
    2602         624 :     hEncoderConfig->Opt_PCA_ON = 0;
    2603         624 :     move16();
    2604             : 
    2605         624 :     return;
    2606             : }

Generated by: LCOV version 1.14