flac: Fix padding for --replay-gain option.
authorErik de Castro Lopo <erikd@mega-nerd.com>
Fri, 27 Jun 2014 23:32:57 +0000 (09:32 +1000)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Fri, 27 Jun 2014 23:33:02 +0000 (09:33 +1000)
Patches ported from the flac 1.2.1 maintenance branch.

    http://flac.cvs.sourceforge.net/viewvc/flac/flac/src/flac/encode.c?
    r1=1.194&r2=1.194.2.1&pathrev=FLAC_RELEASE_1_2_1_MAINTENANCE_BRANCH

    http://flac.cvs.sourceforge.net/viewvc/flac/flac/src/flac/main.c?
    r1=1.169&r2=1.169.2.1&pathrev=FLAC_RELEASE_1_2_1_MAINTENANCE_BRANCH

Patch-from: lvqcl <lvqcl.mail@gmail.com>

src/flac/encode.c
src/flac/main.c

index 10357b4..dc7cbe0 100644 (file)
@@ -1987,6 +1987,8 @@ FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t optio
                                p = options.padding;
                        if(p < 0)
                                p = e->total_samples_to_encode / sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8;
+                       if(p > 0)
+                               p += (e->replay_gain ? GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED : 0);
                        if(options.padding != 0) {
                                if(p > 0 && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
                                        flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
@@ -2043,7 +2045,7 @@ FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t optio
                if(options.padding != 0) {
                        padding.is_last = false; /* the encoder will set this for us */
                        padding.type = FLAC__METADATA_TYPE_PADDING;
-                       padding.length = (unsigned)(options.padding>0? options.padding : (e->total_samples_to_encode / sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8));
+                       padding.length = (unsigned)(options.padding>0? options.padding : (e->total_samples_to_encode / sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8)) + (e->replay_gain ? GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED : 0);
                        static_metadata_append(&static_metadata, &padding, /*needs_delete=*/false);
                }
                metadata = static_metadata.metadata;
index e5d50b5..03f648e 100644 (file)
@@ -437,20 +437,11 @@ int do_it(void)
                                return usage_error("ERROR: --replay-gain can only be done with mono/stereo input\n");
                        if(option_values.format_sample_rate >= 0 && !grabbag__replaygain_is_valid_sample_frequency(option_values.format_sample_rate))
                                return usage_error("ERROR: invalid sample rate used with --replay-gain\n");
-                       /*
-                        * We want to reserve padding space for the ReplayGain
-                        * tags that we will set later, to avoid rewriting the
-                        * whole file.
-                        */
                        if(
                                (option_values.padding >= 0 && option_values.padding < (int)GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED) ||
                                (option_values.padding < 0 && FLAC_ENCODE__DEFAULT_PADDING < (int)GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED)
                        ) {
                                flac__utils_printf(stderr, 1, "NOTE: --replay-gain may leave a small PADDING block even with --no-padding\n");
-                               option_values.padding = GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED;
-                       }
-                       else {
-                               option_values.padding += GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED;
                        }
                }
                if(option_values.num_files > 1 && option_values.cmdline_forced_outfilename) {