Skip to content

Commit 9547dbc

Browse files
authored
Allow --skip and --until with a total sample count of 0 (#838)
1 parent b2c46b5 commit 9547dbc

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/flac/decode.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@ FLAC__bool DecoderSession_process(DecoderSession *d)
541541
print_error_with_state(d, "ERROR during decoding");
542542
return false;
543543
}
544+
else if(d->until_specification->value.samples > 0 && !d->aborting_due_to_until) {
545+
flac__utils_printf(stderr, 1, "%s: WARNING, input file ended before --until value was reached\n", d->inbasefilename);
546+
}
544547

545548
/* write padding bytes for alignment if necessary */
546549
if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
@@ -671,17 +674,22 @@ FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec,
671674
}
672675

673676
/* in any other case the total samples in the input must be known */
674-
if(total_samples_in_input == 0) {
677+
/*if(total_samples_in_input == 0) {
675678
flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0\n", inbasefilename);
676679
return false;
677-
}
680+
}*/
678681

679682
FLAC__ASSERT(spec->value_is_samples);
680683

681684
/* convert relative specifications to absolute */
682685
if(spec->is_relative) {
683686
if(spec->value.samples <= 0)
684-
spec->value.samples += (FLAC__int64)total_samples_in_input;
687+
if (total_samples_in_input == 0) {
688+
flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until relative to end when FLAC metadata has total sample count of 0\n", inbasefilename);
689+
return false;
690+
}
691+
else
692+
spec->value.samples += (FLAC__int64)total_samples_in_input;
685693
else
686694
spec->value.samples += skip;
687695
spec->is_relative = false;
@@ -696,7 +704,7 @@ FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec,
696704
flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
697705
return false;
698706
}
699-
if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
707+
if((FLAC__uint64)spec->value.samples > total_samples_in_input && total_samples_in_input != 0) {
700708
flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of input\n", inbasefilename);
701709
return false;
702710
}
@@ -1607,8 +1615,8 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
16071615
decoder_session->abort_flag = true;
16081616
return;
16091617
}
1610-
FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
1611-
skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
1618+
FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
1619+
skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
16121620
}
16131621
else
16141622
skip = 0;
@@ -1619,13 +1627,17 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
16191627
decoder_session->abort_flag = true;
16201628
return;
16211629
}
1622-
else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
1630+
/*else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
16231631
flac__utils_printf(stderr, 1, "%s: ERROR, can't --skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
16241632
decoder_session->abort_flag = true;
16251633
return;
1626-
}
1634+
}*/
16271635
FLAC__ASSERT(skip == 0 || 0 == decoder_session->cue_specification);
1628-
decoder_session->total_samples = metadata->data.stream_info.total_samples - skip;
1636+
1637+
if(metadata->data.stream_info.total_samples > 0)
1638+
decoder_session->total_samples = metadata->data.stream_info.total_samples - skip;
1639+
else
1640+
decoder_session->total_samples = metadata->data.stream_info.total_samples;
16291641

16301642
/* note that we use metadata->data.stream_info.total_samples instead of decoder_session->total_samples */
16311643
if(decoder_session->stream_counter < 0) {
@@ -1639,7 +1651,7 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
16391651
else
16401652
until = 0;
16411653

1642-
if(until > 0) {
1654+
if(until > 0 && decoder_session->total_samples > 0) {
16431655
FLAC__ASSERT(decoder_session->total_samples != 0);
16441656
FLAC__ASSERT(0 == decoder_session->cue_specification);
16451657
decoder_session->total_samples -= (metadata->data.stream_info.total_samples - until);

0 commit comments

Comments
 (0)