@@ -534,6 +534,9 @@ FLAC__bool DecoderSession_process(DecoderSession *d)
534534 print_error_with_state (d , "ERROR during decoding" );
535535 return false;
536536 }
537+ else if (d -> until_specification -> value .samples > 0 && !d -> aborting_due_to_until ) {
538+ flac__utils_printf (stderr , 1 , "%s: WARNING, input file ended before --until value was reached\n" , d -> inbasefilename );
539+ }
537540
538541 /* write padding bytes for alignment if necessary */
539542 if (!d -> analysis_mode && !d -> test_only && d -> format != FORMAT_RAW ) {
@@ -664,17 +667,22 @@ FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec,
664667 }
665668
666669 /* in any other case the total samples in the input must be known */
667- if (total_samples_in_input == 0 ) {
670+ /* if(total_samples_in_input == 0) {
668671 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0\n", inbasefilename);
669672 return false;
670- }
673+ }*/
671674
672675 FLAC__ASSERT (spec -> value_is_samples );
673676
674677 /* convert relative specifications to absolute */
675678 if (spec -> is_relative ) {
676679 if (spec -> value .samples <= 0 )
677- spec -> value .samples += (FLAC__int64 )total_samples_in_input ;
680+ if (total_samples_in_input == 0 ) {
681+ flac__utils_printf (stderr , 1 , "%s: ERROR, cannot use --until relative to end when FLAC metadata has total sample count of 0\n" , inbasefilename );
682+ return false;
683+ }
684+ else
685+ spec -> value .samples += (FLAC__int64 )total_samples_in_input ;
678686 else
679687 spec -> value .samples += skip ;
680688 spec -> is_relative = false;
@@ -689,7 +697,7 @@ FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec,
689697 flac__utils_printf (stderr , 1 , "%s: ERROR, --until value is before --skip point\n" , inbasefilename );
690698 return false;
691699 }
692- if ((FLAC__uint64 )spec -> value .samples > total_samples_in_input ) {
700+ if ((FLAC__uint64 )spec -> value .samples > total_samples_in_input && total_samples_in_input != 0 ) {
693701 flac__utils_printf (stderr , 1 , "%s: ERROR, --until value is after end of input\n" , inbasefilename );
694702 return false;
695703 }
@@ -1600,8 +1608,8 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
16001608 decoder_session -> abort_flag = true;
16011609 return ;
16021610 }
1603- FLAC__ASSERT (decoder_session -> skip_specification -> value .samples >= 0 );
1604- skip = (FLAC__uint64 )decoder_session -> skip_specification -> value .samples ;
1611+ FLAC__ASSERT (decoder_session -> skip_specification -> value .samples >= 0 );
1612+ skip = (FLAC__uint64 )decoder_session -> skip_specification -> value .samples ;
16051613 }
16061614 else
16071615 skip = 0 ;
@@ -1612,13 +1620,17 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
16121620 decoder_session -> abort_flag = true;
16131621 return ;
16141622 }
1615- else if (metadata -> data .stream_info .total_samples == 0 && skip > 0 ) {
1623+ /* else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
16161624 flac__utils_printf(stderr, 1, "%s: ERROR, can't --skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
16171625 decoder_session->abort_flag = true;
16181626 return;
1619- }
1627+ }*/
16201628 FLAC__ASSERT (skip == 0 || 0 == decoder_session -> cue_specification );
1621- decoder_session -> total_samples = metadata -> data .stream_info .total_samples - skip ;
1629+
1630+ if (metadata -> data .stream_info .total_samples > 0 )
1631+ decoder_session -> total_samples = metadata -> data .stream_info .total_samples - skip ;
1632+ else
1633+ decoder_session -> total_samples = metadata -> data .stream_info .total_samples ;
16221634
16231635 /* note that we use metadata->data.stream_info.total_samples instead of decoder_session->total_samples */
16241636 if (decoder_session -> stream_counter < 0 ) {
@@ -1632,7 +1644,7 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
16321644 else
16331645 until = 0 ;
16341646
1635- if (until > 0 ) {
1647+ if (until > 0 && decoder_session -> total_samples > 0 ) {
16361648 FLAC__ASSERT (decoder_session -> total_samples != 0 );
16371649 FLAC__ASSERT (0 == decoder_session -> cue_specification );
16381650 decoder_session -> total_samples -= (metadata -> data .stream_info .total_samples - until );
0 commit comments