Add API functions to limit minimum bitrate

Quite a lot of decoders have trouble streaming or seeking in a
file with frames that only consist of constant subframes because
of the large difference between the largest and smallest subframe.

To remedy this, this commit makes it possible to disable the use
of constant subframes for the last subframe in case all others are
constant with a new API function. This means the minimum bitrate
for a FLAC file encoded with this function used is raised to
1bit/sample (i.e. 48kbit/s for 48kHz material). This commit also
adds tests to the test suite
This commit is contained in:
Martijn van Beurden 2021-12-01 21:34:42 +01:00
parent 3fc5ba4637
commit b5f4a1535c
10 changed files with 126 additions and 29 deletions

View File

@ -147,6 +147,7 @@ namespace FLAC {
virtual bool set_total_samples_estimate(FLAC__uint64 value); ///< See FLAC__stream_encoder_set_total_samples_estimate()
virtual bool set_metadata(::FLAC__StreamMetadata **metadata, uint32_t num_blocks); ///< See FLAC__stream_encoder_set_metadata()
virtual bool set_metadata(FLAC::Metadata::Prototype **metadata, uint32_t num_blocks); ///< See FLAC__stream_encoder_set_metadata()
virtual bool set_limit_min_bitrate(bool value); ///< See FLAC__stream_encoder_set_limit_min_bitrate()
/* get_state() is not virtual since we want subclasses to be able to return their own state */
State get_state() const; ///< See FLAC__stream_encoder_get_state()
@ -169,6 +170,7 @@ namespace FLAC {
virtual uint32_t get_max_residual_partition_order() const; ///< See FLAC__stream_encoder_get_max_residual_partition_order()
virtual uint32_t get_rice_parameter_search_dist() const; ///< See FLAC__stream_encoder_get_rice_parameter_search_dist()
virtual FLAC__uint64 get_total_samples_estimate() const; ///< See FLAC__stream_encoder_get_total_samples_estimate()
virtual bool get_limit_min_bitrate() const; ///< See FLAC__stream_encoder_get_limit_min_bitrate()
virtual ::FLAC__StreamEncoderInitStatus init(); ///< See FLAC__stream_encoder_init_stream()
virtual ::FLAC__StreamEncoderInitStatus init_ogg(); ///< See FLAC__stream_encoder_init_ogg_stream()

View File

@ -1198,6 +1198,24 @@ FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__Stream
*/
FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, uint32_t num_blocks);
/** Set to \c true to make the encoder not output frames which contain
* only constant subframes. This is beneficial for streaming
* applications: very small frames can cause problems with buffering
* as bitrates can drop as low 1kbit/s for CDDA audio encoded within
* subset. The minimum bitrate for a FLAC file encoded with this
* function used is raised to 1bit/sample (i.e. 48kbit/s for 48kHz
* material).
*
* \default \c false
* \param encoder An encoder instance to set.
* \param value Flag value (see above).
* \assert
* \code encoder != NULL \endcode
* \retval FLAC__bool
* \c false if the encoder is already initialized, else \c true.
*/
FLAC_API FLAC__bool FLAC__stream_encoder_set_limit_min_bitrate(FLAC__StreamEncoder *encoder, FLAC__bool value);
/** Get the current encoder state.
*
* \param encoder An encoder instance to query.
@ -1425,6 +1443,16 @@ FLAC_API uint32_t FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC
*/
FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
/** Get the "limit_min_bitrate" flag.
*
* \param encoder An encoder instance to query.
* \assert
* \code encoder != NULL \endcode
* \retval FLAC__bool
* See FLAC__stream_encoder_set_limit_min_bitrate().
*/
FLAC_API FLAC__bool FLAC__stream_encoder_get_limit_min_bitrate(const FLAC__StreamEncoder *encoder);
/** Initialize the encoder instance to encode native FLAC streams.
*
* This flavor of initialization sets up the encoder to encode to a

View File

@ -211,6 +211,12 @@ namespace FLAC {
return ok;
}
bool Stream::set_limit_min_bitrate(bool value)
{
FLAC__ASSERT(is_valid());
return static_cast<bool>(::FLAC__stream_encoder_set_limit_min_bitrate(encoder_, value));
}
Stream::State Stream::get_state() const
{
FLAC__ASSERT(is_valid());
@ -331,6 +337,12 @@ namespace FLAC {
return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
}
bool Stream::get_limit_min_bitrate() const
{
FLAC__ASSERT(is_valid());
return static_cast<bool>(::FLAC__stream_encoder_get_limit_min_bitrate(encoder_));
}
::FLAC__StreamEncoderInitStatus Stream::init()
{
FLAC__ASSERT(is_valid());

View File

@ -235,13 +235,14 @@ uint32_t FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], uint32_t d
error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
}
if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
/* prefer lower order */
if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
order = 0;
else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
order = 1;
else if(total_error_2 < flac_min(total_error_3, total_error_4))
else if(total_error_2 <= flac_min(total_error_3, total_error_4))
order = 2;
else if(total_error_3 < total_error_4)
else if(total_error_3 <= total_error_4)
order = 3;
else
order = 4;
@ -297,13 +298,14 @@ uint32_t FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], uint3
error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
}
if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
/* prefer lower order */
if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
order = 0;
else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
order = 1;
else if(total_error_2 < flac_min(total_error_3, total_error_4))
else if(total_error_2 <= flac_min(total_error_3, total_error_4))
order = 2;
else if(total_error_3 < total_error_4)
else if(total_error_3 <= total_error_4)
order = 3;
else
order = 4;

View File

@ -158,14 +158,14 @@ uint32_t FLAC__fixed_compute_best_predictor_intrin_sse2(const FLAC__int32 data[]
}
}
/* prefer higher order */
if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
/* prefer lower order */
if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
order = 0;
else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
order = 1;
else if(total_error_2 < flac_min(total_error_3, total_error_4))
else if(total_error_2 <= flac_min(total_error_3, total_error_4))
order = 2;
else if(total_error_3 < total_error_4)
else if(total_error_3 <= total_error_4)
order = 3;
else
order = 4;
@ -309,14 +309,14 @@ uint32_t FLAC__fixed_compute_best_predictor_wide_intrin_sse2(const FLAC__int32 d
}
}
/* prefer higher order */
if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
/* prefer lower order */
if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
order = 0;
else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
order = 1;
else if(total_error_2 < flac_min(total_error_3, total_error_4))
else if(total_error_2 <= flac_min(total_error_3, total_error_4))
order = 2;
else if(total_error_3 < total_error_4)
else if(total_error_3 <= total_error_4)
order = 3;
else
order = 4;

View File

@ -143,14 +143,14 @@ uint32_t FLAC__fixed_compute_best_predictor_intrin_ssse3(const FLAC__int32 data[
}
}
/* prefer higher order */
if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
/* prefer lower order */
if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
order = 0;
else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
order = 1;
else if(total_error_2 < flac_min(total_error_3, total_error_4))
else if(total_error_2 <= flac_min(total_error_3, total_error_4))
order = 2;
else if(total_error_3 < total_error_4)
else if(total_error_3 <= total_error_4)
order = 3;
else
order = 4;
@ -282,14 +282,14 @@ uint32_t FLAC__fixed_compute_best_predictor_wide_intrin_ssse3(const FLAC__int32
}
}
/* prefer higher order */
if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
/* prefer lower order */
if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
order = 0;
else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
order = 1;
else if(total_error_2 < flac_min(total_error_3, total_error_4))
else if(total_error_2 <= flac_min(total_error_3, total_error_4))
order = 2;
else if(total_error_3 < total_error_4)
else if(total_error_3 <= total_error_4)
order = 3;
else
order = 4;

View File

@ -107,6 +107,7 @@ typedef struct FLAC__StreamEncoderProtected {
uint32_t max_residual_partition_order;
uint32_t rice_parameter_search_dist;
FLAC__uint64 total_samples_estimate;
FLAC__bool limit_min_bitrate;
FLAC__StreamMetadata **metadata;
uint32_t num_metadata_blocks;
FLAC__uint64 streaminfo_offset, seektable_offset, audio_offset;

View File

@ -1905,6 +1905,17 @@ FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encod
return true;
}
FLAC_API FLAC__bool FLAC__stream_encoder_set_limit_min_bitrate(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
FLAC__ASSERT(0 != encoder);
FLAC__ASSERT(0 != encoder->private_);
FLAC__ASSERT(0 != encoder->protected_);
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
return false;
encoder->protected_->limit_min_bitrate = value;
return true;
}
/*
* These three functions are not static, but not publicly exposed in
* include/FLAC/ either. They are used by the test suite.
@ -2135,6 +2146,14 @@ FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC
return encoder->protected_->total_samples_estimate;
}
FLAC_API FLAC__bool FLAC__stream_encoder_get_limit_min_bitrate(const FLAC__StreamEncoder *encoder)
{
FLAC__ASSERT(0 != encoder);
FLAC__ASSERT(0 != encoder->private_);
FLAC__ASSERT(0 != encoder->protected_);
return encoder->protected_->limit_min_bitrate;
}
FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], uint32_t samples)
{
uint32_t i, j = 0, k = 0, channel;
@ -2330,6 +2349,7 @@ void set_defaults_(FLAC__StreamEncoder *encoder)
encoder->protected_->max_residual_partition_order = 0;
encoder->protected_->rice_parameter_search_dist = 0;
encoder->protected_->total_samples_estimate = 0;
encoder->protected_->limit_min_bitrate = false;
encoder->protected_->metadata = 0;
encoder->protected_->num_metadata_blocks = 0;
@ -3127,7 +3147,7 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder)
{
FLAC__FrameHeader frame_header;
uint32_t channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
FLAC__bool do_independent, do_mid_side;
FLAC__bool do_independent, do_mid_side, backup_disable_constant_subframes = encoder->private_->disable_constant_subframes, all_subframes_constant = true;
/*
* Calculate the min,max Rice partition orders
@ -3204,6 +3224,12 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder)
*/
if(do_independent) {
for(channel = 0; channel < encoder->protected_->channels; channel++) {
if(encoder->protected_->limit_min_bitrate && all_subframes_constant && (channel + 1) == encoder->protected_->channels){
/* This frame contains only constant subframes at this point.
* To prevent the frame from becoming too small, make sure
* the last subframe isn't constant */
encoder->private_->disable_constant_subframes = true;
}
if(!
process_subframe_(
encoder,
@ -3220,6 +3246,8 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder)
)
)
return false;
if(encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]].type != FLAC__SUBFRAME_TYPE_CONSTANT)
all_subframes_constant = false;
}
}
@ -3365,6 +3393,7 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder)
}
encoder->private_->last_channel_assignment = frame_header.channel_assignment;
encoder->private_->disable_constant_subframes = backup_disable_constant_subframes;
return true;
}

View File

@ -322,6 +322,11 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
return die_s_("returned false", encoder);
printf("OK\n");
printf("testing set_limit_min_bitrate()... ");
if(!encoder->set_limit_min_bitrate(true))
return die_s_("returned false", encoder);
printf("OK\n");
if(layer < LAYER_FILENAME) {
printf("opening file for FLAC output... ");
file = ::flac_fopen(flacfilename(is_ogg), "w+b");
@ -497,6 +502,13 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
return false;
}
printf("OK\n");
printf("testing get_limit_min_bitrate()... ");
if(encoder->get_limit_min_bitrate() != true) {
printf("FAILED, expected true, got false\n");
return false;
}
printf("OK\n");
/* init the dummy sample buffer */
for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)

View File

@ -275,6 +275,11 @@ static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
return die_s_("returned false", encoder);
printf("OK\n");
printf("testing FLAC__stream_encoder_set_limit_min_bitrate()... ");
if(!FLAC__stream_encoder_set_limit_min_bitrate(encoder, true))
return die_s_("returned false", encoder);
printf("OK\n");
if(layer < LAYER_FILENAME) {
printf("opening file for FLAC output... ");
file = flac_fopen(flacfilename(is_ogg), "w+b");
@ -455,6 +460,12 @@ static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
return false;
}
printf("OK\n");
printf("testing FLAC__stream_encoder_get_limit_min_bitrate()... ");
if(FLAC__stream_encoder_get_limit_min_bitrate(encoder) != true) {
printf("FAILED, expected true, got false\n");
return false;
}
/* init the dummy sample buffer */
for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)