From 2e22ff2e3692291df2a62363dd8f488b65164e70 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 19 Dec 2014 10:12:01 +0100 Subject: decoder/ffmpeg: use AVStream::duration Use the duration of the stream we're actually decoding - not the "global" attribute AVFormatContext::duration which may differ. --- src/decoder/plugins/FfmpegDecoderPlugin.cxx | 16 ++++++---------- src/lib/ffmpeg/Time.hxx | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx index 27bd671cc..f78e67af3 100644 --- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx +++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx @@ -583,10 +583,7 @@ FfmpegDecode(Decoder &decoder, InputStream &input, } const SignedSongTime total_time = - format_context.duration != (int64_t)AV_NOPTS_VALUE - ? SignedSongTime::FromScale(format_context.duration, - AV_TIME_BASE) - : SignedSongTime::Negative(); + FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base); decoder_initialized(decoder, audio_format, input.IsSeekable(), total_time); @@ -696,12 +693,11 @@ FfmpegScanStream(AVFormatContext &format_context, if (audio_stream < 0) return false; - if (format_context.duration != (int64_t)AV_NOPTS_VALUE) { - const auto duration = - SongTime::FromScale(format_context.duration, - AV_TIME_BASE); - tag_handler_invoke_duration(&handler, handler_ctx, duration); - } + const AVStream &stream = *format_context.streams[audio_stream]; + if (stream.duration != (int64_t)AV_NOPTS_VALUE) + tag_handler_invoke_duration(&handler, handler_ctx, + FromFfmpegTime(stream.duration, + stream.time_base)); FfmpegScanMetadata(format_context, audio_stream, handler, handler_ctx); diff --git a/src/lib/ffmpeg/Time.hxx b/src/lib/ffmpeg/Time.hxx index 0e6ba2e84..7f2146016 100644 --- a/src/lib/ffmpeg/Time.hxx +++ b/src/lib/ffmpeg/Time.hxx @@ -59,6 +59,31 @@ RatioToAVRational() return { Ratio::num, Ratio::den }; } +/** + * Convert a FFmpeg time stamp to a #SongTime. + */ +gcc_const +static inline SongTime +FromFfmpegTime(int64_t t, const AVRational time_base) +{ + assert(t != (int64_t)AV_NOPTS_VALUE); + + return SongTime::FromMS(av_rescale_q(t, time_base, + (AVRational){1, 1000})); +} + +/** + * Convert a FFmpeg time stamp to a #SignedSongTime. + */ +gcc_const +static inline SignedSongTime +FromFfmpegTimeChecked(int64_t t, const AVRational time_base) +{ + return t != (int64_t)AV_NOPTS_VALUE + ? SignedSongTime(FromFfmpegTime(t, time_base)) + : SignedSongTime::Negative(); +} + /** * Convert a #SongTime to a FFmpeg time stamp with the given base. */ -- cgit v1.2.3