Skip to content

Commit ea1a53f

Browse files
committed
fix: rescale format context duration to stream time base
The duration fallback for streams with unknown duration assigned AVFormatContext::duration, which is measured in AV_TIME_BASE units (1/1e6 s), directly to AVStream::duration, which is measured in stream time_base units. For webm/mkv streams with a 1/1000 s time base this inflated the frame index range by ~1000x, breaking seeking and the waveform/spectrogram (e.g. "Stream ends at sample frame 18534900 instead of 18535728336"). Rescale the duration with av_rescale_q before assigning it.
1 parent 968015e commit ea1a53f

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/sources/soundsourceffmpeg.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,16 @@ SoundSource::OpenResult SoundSourceFFmpeg::tryOpen(
749749

750750
if (m_pavStream->duration == AV_NOPTS_VALUE) {
751751
if (m_pavInputFormatContext->duration != AV_NOPTS_VALUE) {
752-
m_pavStream->duration = m_pavInputFormatContext->duration;
752+
// AVFormatContext::duration is measured in AV_TIME_BASE units
753+
// (1/1e6 s), whereas AVStream::duration is measured in
754+
// stream->time_base units. Rescale accordingly, otherwise the
755+
// frame index range is inflated (e.g. ~1000x for webm/mkv
756+
// streams with a 1/1000 s time base) which breaks seeking and
757+
// the waveform/spectrogram.
758+
m_pavStream->duration = av_rescale_q(
759+
m_pavInputFormatContext->duration,
760+
AV_TIME_BASE_Q,
761+
m_pavStream->time_base);
753762
kLogger.debug()
754763
<< "using format context duration instead of stream duration";
755764
} else {

0 commit comments

Comments
 (0)