Commit 7df47684 authored by multrus's avatar multrus
Browse files

alternative patch within FIX_I74_BW_LIMITATION_ALT: sanitize the bandwidth...

alternative patch within FIX_I74_BW_LIMITATION_ALT: sanitize the bandwidth before entering the encoding function
parent 41b2da0f
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -150,6 +150,7 @@
#define DIRAC_DRCT_GAIN_TUNING                          /* issue 64: tuning of DirAC energy-compensation gains */

/*#define FIX_I74_BW_LIMITATION*/                       /* issue 74: Propagate bitrate induced BW limitation to hEncoderConfig. Ensures BE between explicit BW limitation using "-max_band <BW>" and BW limited by bitrate */
#define FIX_I74_BW_LIMITATION_ALT                       /* issue 74: Propagate bitrate induced BW limitation to hEncoderConfig. Ensures BE between explicit BW limitation using "-max_band <BW>" and BW limited by bitrate; alternative fix */
#define FIX_I74_CLEANING                                /* issue 74: remove redundant function call in ivas_cpe_enc() */

/* ################## End DEVELOPMENT switches ######################### */
+3 −0
Original line number Diff line number Diff line
@@ -989,6 +989,9 @@ typedef struct encoder_config_structure
    int32_t input_Fs;                               /* input signal sampling frequency in Hz */
    int16_t nchan_inp;                              /* number of input audio channels */
    int16_t max_bwidth;                             /* maximum encoded bandwidth */
#ifdef FIX_I74_BW_LIMITATION_ALT
    int16_t max_bwidth_api;                         /* maximum encoded bandwidth, as set on API level */
#endif
    IVAS_FORMAT ivas_format;                        /* IVAS format */

    int16_t element_mode_init;                      /* element mode used at initialization */
+106 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ 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 void updateBandwidthFromFs( const ENCODER_CONFIG_HANDLE hEncoderConfig );
static ivas_error sanitizeBandwidth( const IVAS_ENC_HANDLE hIvasEnc );
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 );
@@ -1025,6 +1026,13 @@ ivas_error IVAS_ENC_EncodeFrameToSerial(
        return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
    }

#ifdef FIX_I74_BW_LIMITATION_ALT
    if ( ( error = sanitizeBandwidth( hIvasEnc ) ) != IVAS_ERR_OK )
    {
        return error;
    }
#endif

    if ( hEncoderConfig->ivas_format == ISM_FORMAT )
    {
        for ( i = 0; i < hEncoderConfig->nchan_inp; ++i )
@@ -1844,6 +1852,100 @@ static void updateBandwidthFromFs(
}


#ifdef FIX_I74_BW_LIMITATION_ALT
/*---------------------------------------------------------------------*
 * sanitizeBandwidth()
 *
 *
 *---------------------------------------------------------------------*/
static ivas_error sanitizeBandwidth(
    const IVAS_ENC_HANDLE hIvasEnc )
{ 
    ENCODER_CONFIG_HANDLE hEncoderConfig;
    int16_t max_bwidth_tmp;

    hEncoderConfig = hIvasEnc->st_ivas->hEncoderConfig;

    max_bwidth_tmp = hEncoderConfig->max_bwidth_api;

    /* Prevent st_ivas->max_bwidth from being higher than Fs/2 */
    if ( hEncoderConfig->input_Fs == 8000 && max_bwidth_tmp > NB )
    {
        max_bwidth_tmp = NB;
    }
    else if ( hEncoderConfig->input_Fs == 16000 && max_bwidth_tmp > WB )
    {
        max_bwidth_tmp = WB;
    }
    else if ( hEncoderConfig->input_Fs == 32000 && max_bwidth_tmp > SWB )
    {
        max_bwidth_tmp = SWB;
    }

    /* NB coding not supported in IVAS. Switching to WB. */
    if ( max_bwidth_tmp == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT )
    {
        if (  hEncoderConfig->input_Fs >= 16000 )
        {
            max_bwidth_tmp = WB;
        }
        else
        {
            return IVAS_ERR_INVALID_BITRATE;
        }
    }

    if ( hEncoderConfig->ivas_format == MONO_FORMAT )
    {
        if ( max_bwidth_tmp == FB && hEncoderConfig->ivas_total_brate < ACELP_16k40 )
        {
            if ( hEncoderConfig->ivas_total_brate < ACELP_9k60 )
            {
                max_bwidth_tmp = WB;
            }
            else
            {
                max_bwidth_tmp = SWB;
            }
        }

        if ( max_bwidth_tmp == SWB && hEncoderConfig->ivas_total_brate < ACELP_9k60 )
        {
            max_bwidth_tmp = WB;
        }

        /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */
        if ( ( max_bwidth_tmp == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 )
        {
            if ( hEncoderConfig->input_Fs >= 16000 )
            {
                max_bwidth_tmp = WB;
            }
            else
            {
                return IVAS_ERR_INVALID_BITRATE;
            }
        }
    }
    else
    {
        if ( max_bwidth_tmp == FB && hEncoderConfig->ivas_total_brate < MIN_BRATE_FB_STEREO )
        {
            max_bwidth_tmp = SWB;
        }
    }

    if ( hEncoderConfig->max_bwidth != max_bwidth_tmp )
    {
        hEncoderConfig->max_bwidth = max_bwidth_tmp;
        hIvasEnc->switchingActive = true;
    }

    return IVAS_ERR_OK;
}
#endif


/*---------------------------------------------------------------------*
 * setBandwidth()
 *
@@ -1866,6 +1968,10 @@ static ivas_error setBandwidth(
        return error;
    }

#ifdef FIX_I74_BW_LIMITATION_ALT
    hEncoderConfig->max_bwidth_api = newBandwidth;
#endif

    /* NB coding not supported in IVAS. Switching to WB. */
    if ( newBandwidth == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT )
    {