Commit eb60f574 authored by korse's avatar korse
Browse files

Merge branch '115-bitrate-switching-in-ism-rev1' into 'main'

115 bitrateswitching ism rev1

See merge request !188
parents fde5c351 91669b1a
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -149,6 +149,8 @@
#define FIX_I13_TCX_TNS_ISSUE                           /* Issue 13: Fix reported artifacts. Bug in TNS with TCX5 */
#define CORECODER_BITRATE_SWITCHING                     /* Issue 133: support bitrate switching in core-coder */

#define ISM_BITRATE_SWITCHING                           /* Issue 115: Support for Bitrate Switching in ISM */




+25 −20
Original line number Diff line number Diff line
@@ -272,6 +272,10 @@ ivas_error ivas_corecoder_dec_reconfig(
     *-----------------------------------------------------------------*/

    /// VE: this could be merged with part of ivas_init_decoder()
#ifdef ISM_BITRATE_SWITCHING
    if ( st_ivas->ivas_format == SBA_FORMAT )
#endif
    {
        if ( st_ivas->sba_mode == SBA_MODE_SPAR && st_ivas->nchan_transport == 1 )
        {
            /* skip as done in init function */
@@ -300,6 +304,7 @@ ivas_error ivas_corecoder_dec_reconfig(
                }
            }
        }
    }

    /* special case, if the decoder goes from 1TC DTX to 2TC active frame (in case the bitstream started with an SBA SID frame), allocate DTX memories */
    if ( hDecoderConfig->last_ivas_total_brate <= IVAS_SID_5k2 && st_ivas->nCPE >= 1 )
@@ -317,7 +322,7 @@ ivas_error ivas_corecoder_dec_reconfig(
/*-------------------------------------------------------------------*
 * ivas_hp20_dec_reconfig()
 *
 * Allocate, initalize, and configure HP20 memory handles in case of bitrate switching
 * Allocate, initialize, and configure HP20 memory handles in case of bitrate switching
 *-------------------------------------------------------------------*/

ivas_error ivas_hp20_dec_reconfig(
+237 −0
Original line number Diff line number Diff line
@@ -1003,6 +1003,220 @@ void ivas_param_ism_params_to_masa_param_mapping(
    return;
}


#ifdef ISM_BITRATE_SWITCHING
/*-------------------------------------------------------------------------*
 * ivas_ism_bitrate_switching()
 *
 *
 *-------------------------------------------------------------------------*/

static ivas_error ivas_ism_bitrate_switching(
    Decoder_Struct *st_ivas,           /* i/o: IVAS decoder structure               */
    const int16_t nchan_transport_old, /* i  : last number of transport channels    */
    const ISM_MODE last_ism_mode,      /* i  : last ISM mode                        */
    const int16_t num_obj              /* i  : number of objects in the bitstream   */
)
{
#ifndef CORECODER_BITRATE_SWITCHING
    int16_t sce_id;
#endif
    ivas_error error;
    int32_t element_brate_tmp[MAX_NUM_OBJECTS];

    error = IVAS_ERR_OK;

    ivas_ism_config( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, num_obj, NULL, NULL, NULL, element_brate_tmp, NULL, NULL );
    st_ivas->nSCE = st_ivas->nchan_transport;

#ifdef CORECODER_BITRATE_SWITCHING
    ivas_corecoder_dec_reconfig( st_ivas, nchan_transport_old, 0, nchan_transport_old, 0 );
#else
    if ( st_ivas->nchan_transport > nchan_transport_old )
    {
        /* Initialize for new bitrate */
        for ( sce_id = 0; sce_id < nchan_transport_old; sce_id++ )
        {
            st_ivas->hSCE[sce_id]->element_brate = st_ivas->hDecoderConfig->ivas_total_brate / st_ivas->nchan_transport;
            st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
        }

        /* Initialize some memories */
        for ( sce_id = nchan_transport_old; sce_id < st_ivas->nchan_transport; sce_id++ )
        {
            if ( ( error = create_sce_dec( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
            {
                return error;
            }
        }
    }
    else
    {
        /* Initialize for new bitrate */
        for ( sce_id = 0; sce_id < st_ivas->nchan_transport; sce_id++ )
        {
            st_ivas->hSCE[sce_id]->element_brate = st_ivas->hDecoderConfig->ivas_total_brate / st_ivas->nchan_transport;
            st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
        }

        /* Destroy the core coder memory */
        for ( ; sce_id < nchan_transport_old; sce_id++ )
        {
            destroy_sce_dec( st_ivas->hSCE[sce_id] );
            st_ivas->hSCE[sce_id] = NULL;
        }
    }
#endif

#ifdef CORECODER_BITRATE_SWITCHING
    ivas_hp20_dec_reconfig( st_ivas, nchan_transport_old );
#else
    /* destroy the memory of hp20*/
    if ( st_ivas->mem_hp20_out != NULL )
    {
        for ( sce_id = 0; sce_id < nchan_transport_old; sce_id++ )
        {
            count_free( st_ivas->mem_hp20_out[sce_id] );
            st_ivas->mem_hp20_out[sce_id] = NULL;
        }
        count_free( st_ivas->mem_hp20_out );
        st_ivas->mem_hp20_out = NULL;
    }

    /* re initialize the memory of hp20 */
    /* set number of input channels used for analysis/coding */

    if ( st_ivas->nchan_transport > 0 )
    {
        if ( ( st_ivas->mem_hp20_out = (float **) count_malloc( st_ivas->nchan_transport * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
        }
    }
    else
    {
        st_ivas->mem_hp20_out = NULL;
    }

    for ( sce_id = 0; sce_id < st_ivas->nchan_transport; sce_id++ )
    {
        if ( ( st_ivas->mem_hp20_out[sce_id] = (float *) count_malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
        }

        set_f( st_ivas->mem_hp20_out[sce_id], 0.0f, L_HP20_MEM );
    }
#endif

    /* Initialize the needed renderer struct and destroy the unnecessary renderer struct */

    /* select the renderer */
    ivas_renderer_select( st_ivas );
    ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
    if ( ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC ) && ( st_ivas->ism_mode == ISM_MODE_DISC ) )
    {
        ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->hDecoderConfig->output_config );
    }

    if ( st_ivas->ism_mode != last_ism_mode )
    {
        /* EFAP handle */
        efap_free_data( &st_ivas->hEFAPdata );
    }

    if ( st_ivas->ism_mode == ISM_MODE_DISC && last_ism_mode == ISM_MODE_PARAM )
    {
        /* switching from ParamISM to DiscISM */

        /* Deallocate the ParamISM struct */
        if ( st_ivas->hDirAC != NULL )
        {
            ivas_param_ism_dec_close( st_ivas->hDirAC, st_ivas->hDecoderConfig->output_config );
            st_ivas->hDirAC = NULL;
        }

        if ( st_ivas->hOutSetup.output_config == AUDIO_CONFIG_BINAURAL )
        {
            /* close the parametric binaural renderer */
            ivas_dirac_dec_close_binaural_data( &st_ivas->hDiracDecBin );

            /* Open the TD Binaural renderer */
            ivas_td_binaural_open( st_ivas );
        }
        else
        {
            /* close the ISM renderer and reinitialize */
            if ( st_ivas->hIsmRendererData != NULL )
            {
                count_free( st_ivas->hIsmRendererData );
                st_ivas->hIsmRendererData = NULL;
            }
            ivas_ism_renderer_open( st_ivas );
        }

        if ( st_ivas->hOutSetup.output_config == AUDIO_CONFIG_BINAURAL_ROOM )
        {
            /* close the parametric binaural renderer */
            ivas_dirac_dec_close_binaural_data( &st_ivas->hDiracDecBin );

            /* Open Crend Binaural renderer */
            ivas_crend_open( st_ivas );
        }
    }

    if ( st_ivas->ism_mode == ISM_MODE_PARAM && last_ism_mode == ISM_MODE_DISC )
    {
        /* switching from Discrete ISM to ParamISM */

        /* Allocate and initialize the ParamISM struct */
        ivas_param_ism_dec_open( st_ivas );

        if ( st_ivas->hOutSetup.output_config == AUDIO_CONFIG_BINAURAL )
        {
            /* open the parametric binaural renderer */
            ivas_dirac_dec_init_binaural_data( st_ivas );

            /* Close the TD Binaural renderer */
            if ( st_ivas->hBinRendererTd != NULL )
            {
                ivas_td_binaural_close( &st_ivas->hBinRendererTd );
            }

            if ( st_ivas->hHrtfTD != NULL )
            {
                st_ivas->hHrtfTD = NULL;
            }
        }
        else
        {
            /* Close the ISM renderer */
            if ( st_ivas->hIsmRendererData != NULL )
            {
                count_free( st_ivas->hIsmRendererData );
                st_ivas->hIsmRendererData = NULL;
            }
        }

        if ( st_ivas->hOutSetup.output_config == AUDIO_CONFIG_BINAURAL_ROOM )
        {
            /* open the parametric binaural renderer */
            ivas_dirac_dec_init_binaural_data( st_ivas );

            /* close the crend binaural renderer */
            ivas_crend_close( st_ivas );

            if ( st_ivas->hHrtf != NULL )
            {
                st_ivas->hHrtf = NULL;
            }
        }
    }

    return error;
}
#endif

/*-------------------------------------------------------------------------
 * ivas_ism_dec_config()
 *
@@ -1019,6 +1233,9 @@ ivas_error ivas_ism_dec_config(
    int32_t ivas_total_brate;
    ISM_MODE last_ism_mode;
    ivas_error error;
#ifdef ISM_BITRATE_SWITCHING
    int16_t nchan_transport_old;
#endif

    error = IVAS_ERR_OK;

@@ -1026,6 +1243,14 @@ ivas_error ivas_ism_dec_config(

    /* store last frame ISM mode */
    last_ism_mode = st_ivas->ism_mode;
#ifdef ISM_BITRATE_SWITCHING
    /* Assumes that num of input objects are constant */
    nchan_transport_old = num_obj;
    if ( last_ism_mode == ISM_MODE_PARAM )
    {
        nchan_transport_old = 2;
    }
#endif

    if ( !st_ivas->bfi && ivas_total_brate != IVAS_SID_5k2 && ivas_total_brate != FRAME_NO_DATA )
    {
@@ -1044,18 +1269,30 @@ ivas_error ivas_ism_dec_config(

        if ( st_ivas->ini_active_frame != 0 )
        {
#ifdef ISM_BITRATE_SWITCHING
            /* ISM bit-rate switching */
            if ( st_ivas->hDecoderConfig->last_ivas_total_brate != IVAS_SID_5k2 && st_ivas->hDecoderConfig->last_ivas_total_brate != FRAME_NO_DATA )
            {
                if ( ( st_ivas->ism_mode != last_ism_mode ) || ( st_ivas->hDecoderConfig->ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) )
                {
                    ivas_ism_bitrate_switching( st_ivas, nchan_transport_old, last_ism_mode, num_obj );
                }
            }
#else
            /* ISM format switching */
            if ( st_ivas->ism_mode != last_ism_mode )
            {
                /*ivas_ism_dec_reconfigure( st_ivas );*/
                return IVAS_ERROR( IVAS_ERR_RECONFIGURE_NOT_SUPPORTED, "\n\n!!! Error: ISM format switching not supported yet!!!\n\n" );
            }
#endif
        }
    }
    else if ( !st_ivas->bfi && ivas_total_brate == IVAS_SID_5k2 )
    {
        st_ivas->nchan_transport = num_obj;
    }

    switch ( num_obj )
    {
        case 1:
+243 −1
Original line number Diff line number Diff line
@@ -42,6 +42,10 @@
#include "wmops.h"


#ifdef CORECODER_BITRATE_SWITCHING
static ivas_error ivas_hp20_reconfig( Encoder_Struct *st_ivas, const int16_t nchan_hp20_old );
#endif

/*-------------------------------------------------------------------------
 * Local function definitions
 *------------------------------------------------------------------------*/
@@ -415,20 +419,258 @@ ivas_error ivas_ism_enc_config(
{
    ivas_error error;
    ISM_MODE last_ism_mode;
#ifdef ISM_BITRATE_SWITCHING
#ifdef CORECODER_BITRATE_SWITCHING
    int16_t nchan_transport_old;
#else
    int16_t nSCE_old, nchan_transport_old;
    int16_t sce_id, n;
#endif
#endif

    error = IVAS_ERR_OK;

    last_ism_mode = st_ivas->ism_mode;

    /* select ISM format mode */
    st_ivas->ism_mode = ivas_ism_mode_select( st_ivas->hEncoderConfig->nchan_inp, st_ivas->hEncoderConfig->ivas_total_brate );

#ifdef ISM_BITRATE_SWITCHING
    /* ISM bit-rate switching */
    if ( ( st_ivas->ism_mode != last_ism_mode ) || ( st_ivas->hEncoderConfig->ivas_total_brate != st_ivas->hEncoderConfig->last_ivas_total_brate ) )
    {
        int32_t element_brate_tmp[MAX_NUM_OBJECTS];
#ifndef CORECODER_BITRATE_SWITCHING
        Indice *ind_list_sce, *ind_list_metadata;
#endif

#ifndef CORECODER_BITRATE_SWITCHING
        nSCE_old = st_ivas->nSCE;
#endif
        nchan_transport_old = st_ivas->nchan_transport;

        /* Reset and Initialize */
        if ( st_ivas->ism_mode == ISM_MODE_PARAM )
        {
            st_ivas->nchan_transport = 2;
        }
        else
        {
            st_ivas->nchan_transport = st_ivas->hEncoderConfig->nchan_inp;
        }

        st_ivas->nSCE = st_ivas->nchan_transport;
        st_ivas->nCPE = 0;

        ivas_ism_config( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->hEncoderConfig->nchan_inp, NULL, NULL, NULL, element_brate_tmp, NULL, NULL );

#ifdef CORECODER_BITRATE_SWITCHING
        ivas_corecoder_enc_reconfig( st_ivas, nchan_transport_old, 0, nchan_transport_old );
#else
        if ( st_ivas->nSCE > nSCE_old )
        {
            /* Reconfigure the core coders */
            for ( sce_id = 0; sce_id < nSCE_old; sce_id++ )
            {
                copy_encoder_config( st_ivas, st_ivas->hSCE[sce_id]->hCoreCoder[0], 0 );
                st_ivas->hSCE[sce_id]->element_brate = st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport;
                st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
            }

            /* Initialize the extra required memory */
            ind_list_sce = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr->ind_list;
            ind_list_metadata = st_ivas->hSCE[0]->hMetaData->ind_list;

            for ( sce_id = nSCE_old; sce_id < st_ivas->nSCE; sce_id++ )
            {
                /* Initialize the Core Coder */
                if ( ( error = create_sce_enc( st_ivas, sce_id, element_brate_tmp[sce_id] ) ) != IVAS_ERR_OK )
                {
                    return error;
                }

                /* prepare bitstream buffers */
                st_ivas->hSCE[sce_id]->hCoreCoder[0]->hBstr->ind_list = ind_list_sce + ( sce_id * MAX_NUM_INDICES );
                reset_indices_enc( st_ivas->hSCE[sce_id]->hCoreCoder[0]->hBstr, MAX_NUM_INDICES );

                st_ivas->hSCE[sce_id]->hMetaData->ind_list = ind_list_metadata + ( sce_id * MAX_BITS_METADATA );
                reset_indices_enc( st_ivas->hSCE[sce_id]->hMetaData, MAX_BITS_METADATA );
            }
        }
        else
        {
            /* Reconfigure the Core Coders */
            for ( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
            {
                copy_encoder_config( st_ivas, st_ivas->hSCE[sce_id]->hCoreCoder[0], 0 );
                st_ivas->hSCE[sce_id]->element_brate = st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport;
                st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
            }

            /* Delete the extra memory */
            for ( sce_id = st_ivas->nSCE; sce_id < nSCE_old; sce_id++ )
            {
                if ( st_ivas->hSCE[sce_id] != NULL )
                {
                    destroy_sce_enc( st_ivas->hSCE[sce_id] );
                    st_ivas->hSCE[sce_id] = NULL;
                }
            }
        }
#endif

        if ( st_ivas->ism_mode == ISM_MODE_PARAM && last_ism_mode == ISM_MODE_DISC )
        {
            /* Allocate and Initialize the memory used by ParamISM when switch from Discrete ISM */
            if ( ( error = ivas_param_ism_enc_open( st_ivas ) ) != IVAS_ERR_OK )
            {
                return error;
            }
        }

        if ( st_ivas->ism_mode == ISM_MODE_DISC && last_ism_mode == ISM_MODE_PARAM )
        {
            /* Deallocate the memory used by ParamISM when switch to Discrete ISM */
            ivas_param_ism_enc_close( st_ivas->hDirAC, st_ivas->hEncoderConfig->input_Fs );
            st_ivas->hDirAC = NULL;
        }

#ifdef CORECODER_BITRATE_SWITCHING
        ivas_hp20_reconfig( st_ivas, nchan_transport_old );
#else
        /* destroy the memory of hp20*/
        if ( st_ivas->mem_hp20_in != NULL )
        {
            for ( sce_id = 0; sce_id < nSCE_old; sce_id++ )
            {
                count_free( st_ivas->mem_hp20_in[sce_id] );
                st_ivas->mem_hp20_in[sce_id] = NULL;
            }
            count_free( st_ivas->mem_hp20_in );
            st_ivas->mem_hp20_in = NULL;
        }

        /* re initialize the memory of hp20 */
        /* set number of input channels used for analysis/coding */
        n = getNumChanAnalysis( st_ivas );

        if ( n > 0 )
        {
            if ( ( st_ivas->mem_hp20_in = (float **) count_malloc( n * sizeof( float * ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
            }
        }
        else
        {
            st_ivas->mem_hp20_in = NULL;
        }

        for ( sce_id = 0; sce_id < n; sce_id++ )
        {
            if ( ( st_ivas->mem_hp20_in[sce_id] = (float *) count_malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
            }

            set_f( st_ivas->mem_hp20_in[sce_id], 0.0f, L_HP20_MEM );
        }
#endif
    }
#else
    /* ISM format switching */
    if ( st_ivas->ism_mode != last_ism_mode )
    {
        /*ivas_ism_dec_reconfigure( st_ivas );*/
        return IVAS_ERROR( IVAS_ERR_RECONFIGURE_NOT_SUPPORTED, "Error: ISM format switching not supported yet!!!\n\n" );
    }
#endif

    return error;
}


#ifdef CORECODER_BITRATE_SWITCHING
// VE: this is the same function as at the decoder -> harmonize them to a new file ivas_corecoder_reconfig.c
/*-------------------------------------------------------------------*
 * ivas_hp20_dec_reconfig()
 *
 * Allocate, initialize, and configure HP20 memory handles in case of bitrate switching
 *-------------------------------------------------------------------*/

static ivas_error ivas_hp20_reconfig(
    Encoder_Struct *st_ivas,     /* i/o: IVAS encoder structure                     */
    const int16_t nchan_hp20_old /* i  : number of HP20 filters in previous frame   */
)
{
    int16_t i, nchan_hp20;
    float **old_mem_hp20_out;
    ivas_error error;

    error = IVAS_ERR_OK;

    /*-----------------------------------------------------------------*
     * HP20 memories
     *-----------------------------------------------------------------*/

    nchan_hp20 = getNumChanAnalysis( st_ivas );

    if ( nchan_hp20 > nchan_hp20_old )
    {
        /* save old mem_hp_20 pointer */
        old_mem_hp20_out = st_ivas->mem_hp20_in;
        st_ivas->mem_hp20_in = NULL;

        if ( ( st_ivas->mem_hp20_in = (float **) count_malloc( nchan_hp20 * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
        }

        for ( i = 0; i < nchan_hp20_old; i++ )
        {
            st_ivas->mem_hp20_in[i] = old_mem_hp20_out[i];
            old_mem_hp20_out[i] = NULL;
        }
        /* create additional hp20 memories */
        for ( ; i < nchan_hp20; i++ )
        {
            if ( ( st_ivas->mem_hp20_in[i] = (float *) count_malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
            }

            set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
        }

        count_free( old_mem_hp20_out );
        old_mem_hp20_out = NULL;
    }
    else if ( nchan_hp20 < nchan_hp20_old )
    {
        /* save old mem_hp_20 pointer */
        old_mem_hp20_out = st_ivas->mem_hp20_in;
        st_ivas->mem_hp20_in = NULL;

        if ( ( st_ivas->mem_hp20_in = (float **) count_malloc( nchan_hp20 * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
        }

        for ( i = 0; i < nchan_hp20; i++ )
        {
            st_ivas->mem_hp20_in[i] = old_mem_hp20_out[i];
            old_mem_hp20_out[i] = NULL;
        }
        /* remove superfluous hp20 memories */
        for ( ; i < nchan_hp20_old; i++ )
        {
            count_free( old_mem_hp20_out[i] );
            old_mem_hp20_out[i] = NULL;
        }

        count_free( old_mem_hp20_out );
        old_mem_hp20_out = NULL;
    }

    return error;
}
#endif
+58 −0
Original line number Diff line number Diff line
@@ -78,6 +78,9 @@ static int16_t getInputBufferSize( const Encoder_Struct *st_ivas );
static ivas_error doCommonConfigureChecks( IVAS_ENC_HANDLE hIvasEnc );
static ivas_error doCommonSetterChecks( IVAS_ENC_HANDLE hIvasEnc );
static ivas_error sanitizeBandwidth( const IVAS_ENC_HANDLE hIvasEnc );
#ifdef ISM_BITRATE_SWITCHING
static ivas_error sanitizeBitrateISM( const ENCODER_CONFIG_HANDLE hEncoderConfig );
#endif
static void init_encoder_config( ENCODER_CONFIG_HANDLE hEncoderConfig );
static void resetIsmMetadataProvidedFlags( IVAS_ENC_HANDLE hIvasEnc );
static ivas_error bandwidthApiToInternal( const IVAS_ENC_BANDWIDTH maxBandwidth, int16_t *internalMaxBandwidth );
@@ -761,6 +764,12 @@ static ivas_error configureEncoder(
        }
        else if ( hEncoderConfig->ivas_format == ISM_FORMAT )
        {
#ifdef ISM_BITRATE_SWITCHING
            if ( ( error = sanitizeBitrateISM( hEncoderConfig ) ) != IVAS_ERR_OK )
            {
                return error;
            }
#else
            if ( hEncoderConfig->ivas_total_brate > IVAS_256k )
            {
                return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for ISm specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
@@ -777,6 +786,7 @@ static ivas_error configureEncoder(
            {
                return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 4 ISm specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
            }
#endif
        }
        else if ( hEncoderConfig->ivas_format == SBA_FORMAT )
        {
@@ -1625,6 +1635,9 @@ static ivas_error setBitrate(
{
    Encoder_Struct *st_ivas;
    ENCODER_CONFIG_HANDLE hEncoderConfig;
#ifdef ISM_BITRATE_SWITCHING
    ivas_error error;
#endif

    st_ivas = hIvasEnc->st_ivas;
    hEncoderConfig = st_ivas->hEncoderConfig;
@@ -1671,6 +1684,16 @@ static ivas_error setBitrate(
        }
    }

#ifdef ISM_BITRATE_SWITCHING
    if ( hEncoderConfig->ivas_format == ISM_FORMAT )
    {
        if ( ( error = sanitizeBitrateISM( hEncoderConfig ) ) != IVAS_ERR_OK )
        {
            return error;
        }
    }
#endif

    st_ivas->codec_mode = MODE1;

    if ( hEncoderConfig->element_mode_init == EVS_MONO )
@@ -1897,6 +1920,41 @@ static ivas_error sanitizeBandwidth(
}


#ifdef ISM_BITRATE_SWITCHING
/*---------------------------------------------------------------------*
 * sanitizeBitrateISM()
 *
 *
 *---------------------------------------------------------------------*/

static ivas_error sanitizeBitrateISM(
    const ENCODER_CONFIG_HANDLE hEncoderConfig )
{
    if ( hEncoderConfig->ivas_total_brate > IVAS_256k )
    {
        return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too high bitrate for ISm specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    }

    if ( hEncoderConfig->ivas_total_brate < IVAS_16k4 && hEncoderConfig->nchan_inp == 2 )
    {
        return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 2 ISm specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    }

    if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 && hEncoderConfig->nchan_inp == 3 )
    {
        return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 3 ISm specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    }

    if ( hEncoderConfig->ivas_total_brate < IVAS_24k4 && hEncoderConfig->nchan_inp == 4 )
    {
        return IVAS_ERROR( IVAS_ERR_INVALID_BITRATE, "Too low bitrate for 4 ISm specified in IVAS: %d", hEncoderConfig->ivas_total_brate );
    }

    return IVAS_ERR_OK;
}
#endif


/*---------------------------------------------------------------------*
 * setBandwidth()
 *
Loading