From 28b9982d8625245fe12fb0889eccf7d86d0add77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 01:28:51 +0300 Subject: [PATCH] fix(ec-probe): rescale a stream's initial_padding into its own time_base before subtracting it from duration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reader::duration() subtracted `initial_padding` (a sample count) straight off `duration` (time_base ticks). The two units only coincide when the time base is 1/sample_rate; a Matroska file's TimestampScale usually is not (commonly milliseconds), so a stream's CodecDelay-derived padding — 1024 samples for AAC — was read as 1024 whole ticks and lopped a third off the stated length (aac-mka-stereo-48000.mka: 3.021s -> 1.997s). Root cause: crates/ec-probe/src/lib.rs Reader::duration() (StreamInfo's own doc comment at crates/ec-core/src/registry.rs:245-249 already says initial_padding is samples, not time_base ticks). Fixed by rescaling initial_padding through TimeBase::from_rate(sample_rate) into the stream's time_base before subtracting. Lacing/BlockGroup handling in ec-matroska was already correct — not the bug. --- crates/ec-probe/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/ec-probe/src/lib.rs b/crates/ec-probe/src/lib.rs index 04bee1e8..e5dd0b9b 100644 --- a/crates/ec-probe/src/lib.rs +++ b/crates/ec-probe/src/lib.rs @@ -347,8 +347,25 @@ impl Reader { self.streams() .iter() .filter_map(|s| { - s.duration - .map(|d| Timestamp::new(d - i64::from(s.initial_padding), s.time_base)) + s.duration.map(|d| { + // `initial_padding` is a sample count, not a `time_base` + // tick count — the two only coincide when the stream's + // time base happens to be `1/sample_rate`, which a + // container's own duration field usually is not. + let padding_ticks = s + .params + .audio() + .filter(|_| s.initial_padding > 0) + .map(|a| { + TimeBase::from_rate(a.sample_rate).rescale( + i64::from(s.initial_padding), + s.time_base, + Rounding::Down, + ) + }) + .unwrap_or(0); + Timestamp::new(d - padding_ticks, s.time_base) + }) }) .max_by(|a, b| { a.as_secs_f64() @@ -719,6 +736,24 @@ mod tests { assert_eq!(sniff(b"not a media file at all"), None); } + /// `initial_padding` is a *sample* count while a stream's `duration` is in + /// `time_base` ticks -- for a Matroska file whose `TimestampScale` is not + /// `1/sample_rate` (the common case: ticks are milliseconds), subtracting + /// one from the other unconverted lopped a third off an AAC track's + /// stated length (3.021 s of `CodecDelay`-bearing audio reported as 1.997 + /// s: `1024` samples read straight off as `1024` one-millisecond ticks). + #[test] + fn duration_converts_initial_padding_out_of_sample_units() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../fixtures/audio/aac-mka-stereo-48000.mka"); + if !path.exists() { + return; // fixtures are generated, not checked in + } + let reader = Reader::open(&path).unwrap(); + let d = reader.duration().expect("a stated duration").as_secs_f64(); + assert!((d - 3.0).abs() < 0.1, "duration {d}, want ~3.0s"); + } + /// An `.aac` or `.mp3` that opens with a tag is still an `.aac` or `.mp3`. #[test] fn an_id3_tag_does_not_hide_the_format() {