Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to limit minimum bitrate #264

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/FLAC++/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
28 changes: 28 additions & 0 deletions include/FLAC/stream_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/flac/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,7 @@ FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t optio
FLAC__stream_encoder_set_apodization(e->encoder, apodizations);
FLAC__stream_encoder_set_total_samples_estimate(e->encoder, e->total_samples_to_encode);
FLAC__stream_encoder_set_metadata(e->encoder, (num_metadata > 0)? metadata : 0, num_metadata);
FLAC__stream_encoder_set_limit_min_bitrate(e->encoder, options.limit_min_bitrate);

FLAC__stream_encoder_disable_constant_subframes(e->encoder, options.debug.disable_constant_subframes);
FLAC__stream_encoder_disable_fixed_subframes(e->encoder, options.debug.disable_fixed_subframes);
Expand Down
1 change: 1 addition & 0 deletions src/flac/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ typedef struct {
FLAC__bool ignore_chunk_sizes;
FLAC__bool sector_align;
FLAC__bool error_on_compression_fail;
FLAC__bool limit_min_bitrate;

FLAC__StreamMetadata *vorbis_comment;
FLAC__StreamMetadata *pictures[64];
Expand Down
13 changes: 13 additions & 0 deletions src/flac/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ static struct share__option long_options_[] = {
{ "sign" , share__required_argument, 0, 0 },
{ "input-size" , share__required_argument, 0, 0 },
{ "error-on-compression-fail" , share__no_argument, 0, 0 },
{ "limit-min-bitrate" , share__no_argument, 0, 0 },

/*
* analysis options
Expand Down Expand Up @@ -268,6 +269,7 @@ static struct {
FLAC__bool cued_seekpoints;
FLAC__bool channel_map_none; /* --channel-map=none specified, eventually will expand to take actual channel map */
FLAC__bool error_on_compression_fail;
FLAC__bool limit_min_bitrate;

uint32_t num_files;
char **filenames;
Expand Down Expand Up @@ -597,6 +599,7 @@ FLAC__bool init_options(void)
option_values.cued_seekpoints = true;
option_values.channel_map_none = false;
option_values.error_on_compression_fail = false;
option_values.limit_min_bitrate = false;

option_values.num_files = 0;
option_values.filenames = 0;
Expand Down Expand Up @@ -825,6 +828,9 @@ int parse_option(int short_option, const char *long_option, const char *option_a
else if(0 == strcmp(long_option, "residual-text")) {
option_values.aopts.do_residual_text = true;
}
else if(0 == strcmp(long_option, "limit-min-bitrate")) {
option_values.limit_min_bitrate = true;
}
/*
* negatives
*/
Expand Down Expand Up @@ -1289,6 +1295,7 @@ void show_help(void)
printf(" -p, --qlp-coeff-precision-search Exhaustively search LP coeff quantization\n");
printf(" -q, --qlp-coeff-precision=# Specify precision in bits\n");
printf(" -r, --rice-partition-order=[#,]# Set [min,]max residual partition order\n");
printf(" --limit-min-bitrate Limit minimum bitrate (for streaming)\n");
printf("format options:\n");
printf(" --force-raw-format Treat input or output as raw samples\n");
printf(" --force-aiff-format Force decoding to AIFF format\n");
Expand Down Expand Up @@ -1618,6 +1625,11 @@ void show_explain(void)
printf(" (# is 0 to 15 inclusive; min defaults to 0;\n");
printf(" the default is -r 0; above 4 does not\n");
printf(" usually help much)\n");
printf(" --limit-min-bitrate Limit minimum bitrate by not allowing frames\n");
printf(" consisting of only constant subframes. This\n");
printf(" ensures a bitrate of at least 1 bit/sample,\n");
printf(" for example 48kbit/s for 48kHz input. This is\n");
printf(" mostly beneficial for internet streaming.\n");
printf("format options:\n");
printf(" --force-raw-format Force input (when encoding) or output (when\n");
printf(" decoding) to be treated as raw samples\n");
Expand Down Expand Up @@ -1926,6 +1938,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
encode_options.debug.disable_verbatim_subframes = option_values.debug.disable_verbatim_subframes;
encode_options.debug.do_md5 = option_values.debug.do_md5;
encode_options.error_on_compression_fail = option_values.error_on_compression_fail;
encode_options.limit_min_bitrate = option_values.limit_min_bitrate;

/* if infilename and outfilename point to the same file, we need to write to a temporary file */
if(encode_infile != stdin && grabbag__file_are_same(infilename, outfilename)) {
Expand Down
12 changes: 12 additions & 0 deletions src/libFLAC++/stream_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
18 changes: 10 additions & 8 deletions src/libFLAC/fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/libFLAC/fixed_intrin_sse2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/libFLAC/fixed_intrin_ssse3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/libFLAC/include/protected/stream_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 30 additions & 1 deletion src/libFLAC/stream_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions src/test_libFLAC++/encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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++)
Expand Down
Loading