From 4df2de2f2047f65a809e16beed40e01a1c5e3bdb Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Mon, 10 Mar 2025 15:00:35 +0000 Subject: [PATCH] LibMedia/Audio: Use duration in the container if stream doesn't have one --- Libraries/LibMedia/Audio/FFmpegLoader.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Libraries/LibMedia/Audio/FFmpegLoader.cpp b/Libraries/LibMedia/Audio/FFmpegLoader.cpp index 587a662f72e..8e687d084e5 100644 --- a/Libraries/LibMedia/Audio/FFmpegLoader.cpp +++ b/Libraries/LibMedia/Audio/FFmpegLoader.cpp @@ -90,9 +90,20 @@ ErrorOr FFmpegLoaderPlugin::initialize() // This is an initial estimate of the total number of samples in the stream. // During decoding, we might need to increase the number as more frames come in. - double duration_in_seconds = static_cast(m_audio_stream->duration) * time_base(); - if (duration_in_seconds < 0) - return Error::from_string_literal("Negative stream duration"); + auto duration_in_seconds = TRY([this] -> ErrorOr { + if (m_audio_stream->duration >= 0) { + auto time_base = av_q2d(m_audio_stream->time_base); + return static_cast(m_audio_stream->duration) * time_base; + } + + // If the stream doesn't specify the duration, fallback to what the container says the duration is. + // If the container doesn't know the duration, then we're out of luck. Return an error. + if (m_format_context->duration < 0) + return Error::from_string_literal("Negative stream duration"); + + return static_cast(m_format_context->duration) / AV_TIME_BASE; + }()); + m_total_samples = AK::round_to(sample_rate() * duration_in_seconds); // Allocate packet (logical chunk of data) and frame (video / audio frame) buffers