diff --git a/lib_com/options.h b/lib_com/options.h index db358692a95b2ccf4c5896ce245e1dd9c5e57525..43a77742341ce0fb1c9cc36be918856812d694c4 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -152,6 +152,15 @@ #define QUANTISE_REAL_FCN_CLEAN_UP /*Clean up the ivas_quantise_real_values() function*/ + +#define FIX_I74_BW_LIMITATION_ALT /* issue 74: Propagate bitrate induced BW limitation to hEncoderConfig. Ensures BE between explicit BW limitation using "-max_band " and BW limited by bitrate; alternative fix */ +#define FIX_I74_CLEANING /* issue 74: remove redundant function call in ivas_cpe_enc() */ + + +/* NTT switches */ + +#define SPAR_SCALING_HARMONIZATION /* issue 80: Changes to harmonize scaling in spar */ + /* ################## End DEVELOPMENT switches ######################### */ /* clang-format on */ #endif diff --git a/lib_enc/ivas_cpe_enc.c b/lib_enc/ivas_cpe_enc.c index b543761491bd57c201045f9c42280183bda1feaa..2ee9f7b71f336070148e7ad996136627d7e0818f 100644 --- a/lib_enc/ivas_cpe_enc.c +++ b/lib_enc/ivas_cpe_enc.c @@ -344,11 +344,13 @@ ivas_error ivas_cpe_enc( sts[0]->total_brate = ( sts[0]->bits_frame_nominal + 10 ) * FRAMES_PER_SEC; /* add small overhead; st[0]->total_brate used in coder_type_modif() */ +#ifndef FIX_I74_CLEANING if ( ( sts[0]->last_bwidth < max_bwidth ) || ( sts[0]->last_core_brate <= SID_2k40 ) ) /* IVAS_fmToDo: TBV - BWD output is not known here yet !!! */ { /* reconfigure in case of BW switching or if last frame was a SID/NO_DATA with coarse partitioning */ hCPE->hStereoDft->nbands = stereo_dft_band_config( hCPE->hStereoDft->band_limits, hCPE->hStereoDft->hConfig->band_res, hCPE->hStereoDft->NFFT, ENC ); } +#endif /* Update DFT Stereo memories */ stereo_dft_enc_update( hCPE->hStereoDft, sts[0]->max_bwidth diff --git a/lib_enc/lib_enc.c b/lib_enc/lib_enc.c index cf2c5aa4670ce20e3b77a59ca92edbd9cdff983c..ede42ddeaf0f766bd36a137537c52ead80879b10 100644 --- a/lib_enc/lib_enc.c +++ b/lib_enc/lib_enc.c @@ -63,6 +63,9 @@ struct IVAS_ENC int16_t rf_fec_offset_loc; bool ismMetadataProvided[MAX_NUM_OBJECTS]; bool maxBandwidthUser; /* Was a specific max bandwith selected by the user? */ +#ifdef FIX_I74_BW_LIMITATION_ALT + IVAS_ENC_BANDWIDTH newBandwidthApi; /* maximum encoded bandwidth, as set on API level */ +#endif }; /*---------------------------------------------------------------------* @@ -76,7 +79,10 @@ static ivas_error setChannelAwareConfig( IVAS_ENC_HANDLE hIvasEnc, const IVAS_EN 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 ); +#ifndef FIX_I74_BW_LIMITATION_ALT static void updateBandwidthFromFs( const ENCODER_CONFIG_HANDLE hEncoderConfig ); +#endif +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 ); @@ -830,7 +836,9 @@ static ivas_error configureEncoder( hEncoderConfig->input_Fs = inputFs; +#ifndef FIX_I74_BW_LIMITATION_ALT updateBandwidthFromFs( hEncoderConfig ); +#endif /*-----------------------------------------------------------------* * Channel-aware mode @@ -896,6 +904,13 @@ static ivas_error configureEncoder( return IVAS_ERROR( IVAS_ERR_NOT_SUPPORTED_OPTION, "PCA supported at SBA FOA 256 kbps only." ); } +#ifdef FIX_I74_BW_LIMITATION_ALT + if ( ( error = sanitizeBandwidth( hIvasEnc ) ) != IVAS_ERR_OK ) + { + return error; + } +#endif + /*-----------------------------------------------------------------* * Finalize initialization *-----------------------------------------------------------------*/ @@ -1033,6 +1048,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 ) @@ -1405,7 +1427,12 @@ static ivas_error printConfigInfo_enc( { Encoder_Struct *st_ivas; ENCODER_CONFIG_HANDLE hEncoderConfig; +#ifndef FIX_I74_BW_LIMITATION_ALT char max_bwidth_string[4]; +#else + int16_t newBandwidthApi; + ivas_error error; +#endif st_ivas = hIvasEnc->st_ivas; hEncoderConfig = st_ivas->hEncoderConfig; @@ -1546,6 +1573,12 @@ static ivas_error printConfigInfo_enc( * Print potential limitation of audio bandwidth *-----------------------------------------------------------------*/ +#ifdef FIX_I74_BW_LIMITATION_ALT + if ( ( error = bandwidthApiToInternal( hIvasEnc->newBandwidthApi, &newBandwidthApi ) ) != IVAS_ERR_OK ) + { + return error; + } +#else switch ( hEncoderConfig->max_bwidth ) { case NB: @@ -1561,19 +1594,43 @@ static ivas_error printConfigInfo_enc( strncpy( max_bwidth_string, "FB\0", sizeof( max_bwidth_string ) ); break; } +#endif if ( st_ivas->hEncoderConfig->Opt_SC_VBR && !hEncoderConfig->Opt_DTX_ON ) { return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "\nError: SC-VBR 5900 bps not supported without DTX\n\n" ); } +#ifndef FIX_I74_BW_LIMITATION_ALT if ( hIvasEnc->maxBandwidthUser ) { fprintf( stdout, "\nBandwidth limited to %s.\n", max_bwidth_string ); } +#endif if ( hEncoderConfig->ivas_format == MONO_FORMAT ) { +#ifdef FIX_I74_BW_LIMITATION_ALT + if ( newBandwidthApi != hEncoderConfig->max_bwidth ) + { + if ( newBandwidthApi == FB ) + { + fprintf( stdout, "\nFB coding not supported below %.2f kbps. ", ACELP_16k40 / 1000.f ); + if ( hEncoderConfig->max_bwidth == WB ) + { + fprintf( stdout, "Switching to WB.\n" ); + } + else + { + fprintf( stdout, "Switching to SWB.\n" ); + } + } + else if ( newBandwidthApi == SWB ) + { + fprintf( stdout, "\nSWB coding not supported below %.2f kbps. Switching to WB.\n", ACELP_9k60 / 1000.f ); + } + } +#else if ( hEncoderConfig->max_bwidth == FB && hEncoderConfig->ivas_total_brate < ACELP_16k40 ) { fprintf( stdout, "\nFB coding not supported below %.2f kbps. ", ACELP_16k40 / 1000.f ); @@ -1591,9 +1648,14 @@ static ivas_error printConfigInfo_enc( { fprintf( stdout, "\nSWB coding not supported below %.2f kbps. Switching to WB.", ACELP_9k60 / 1000.f ); } +#endif /* in case of 8kHz input sampling or "-max_band NB", require the total bitrate to be below 24.40 kbps */ +#ifdef FIX_I74_BW_LIMITATION_ALT + if ( ( newBandwidthApi == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 ) +#else if ( ( hEncoderConfig->max_bwidth == NB || hEncoderConfig->input_Fs == 8000 ) && hEncoderConfig->ivas_total_brate > ACELP_24k40 ) +#endif { fprintf( stdout, "\nError: Unsupported mode NB %d bps, NB mode supports rates 5900-24400 bps\n\n", hEncoderConfig->ivas_total_brate ); return IVAS_ERR_INVALID_BITRATE; @@ -1601,7 +1663,11 @@ static ivas_error printConfigInfo_enc( } else { +#ifdef FIX_I74_BW_LIMITATION_ALT + if ( newBandwidthApi != hEncoderConfig->max_bwidth ) +#else if ( hEncoderConfig->max_bwidth == FB && hEncoderConfig->ivas_total_brate < MIN_BRATE_FB_STEREO ) +#endif { fprintf( stdout, "\nFB coding not supported below %.2f kbps. Switching to SWB.\n", MIN_BRATE_FB_STEREO / 1000.f ); } @@ -1811,7 +1877,7 @@ static ivas_error doCommonSetterChecks( return IVAS_ERR_OK; } - +#ifndef FIX_I74_BW_LIMITATION_ALT /*---------------------------------------------------------------------* * updateBandwidthFromFs() * @@ -1837,6 +1903,102 @@ static void updateBandwidthFromFs( return; } +#endif + +#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 = hIvasEnc->newBandwidthApi; + + /* 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 0 // IVAS_fmToDo: temporary disabled to keep EVS bit-exactness -> to be verified + 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; + } +#endif + + /* 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 /*---------------------------------------------------------------------* @@ -1861,6 +2023,10 @@ static ivas_error setBandwidth( return error; } +#ifdef FIX_I74_BW_LIMITATION_ALT + hIvasEnc->newBandwidthApi = newBandwidth; +#endif + /* NB coding not supported in IVAS. Switching to WB. */ if ( newBandwidth == NB && hEncoderConfig->ivas_format != UNDEFINED_FORMAT && hEncoderConfig->ivas_format != MONO_FORMAT ) { @@ -1873,12 +2039,14 @@ static ivas_error setBandwidth( hIvasEnc->switchingActive = true; } +#ifndef FIX_I74_BW_LIMITATION_ALT /* Limit bandwidth to half of sampling rate - only possible if * sampling rate has already been set via configure function */ if ( hIvasEnc->isConfigured ) { updateBandwidthFromFs( hIvasEnc->st_ivas->hEncoderConfig ); } +#endif return IVAS_ERR_OK; }