forge
Explore
Log in

Don't you dare go Hollow

Rescale a stream's initial_padding into its own time_base before subtracting it from duration

Browse files at this commit

28b9982d8625245fe12fb0889eccf7d86d0add77Ahmet Kaan GümüşParent: dc51bff
  • fix
    • rescale a stream's initial_padding into its own time_base before subtracting it from duration
Show full message
fix(ec-probe): rescale a stream's initial_padding into its own time_base before subtracting it from duration

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.rsmodified+37 -2
@@ -347,8 +347,25 @@ impl Reader {
347347 self.streams()
348348 .iter()
349349 .filter_map(|s| {
350- s.duration
351- .map(|d| Timestamp::new(d - i64::from(s.initial_padding), s.time_base))
350+ s.duration.map(|d| {
351+ // `initial_padding` is a sample count, not a `time_base`
352+ // tick count — the two only coincide when the stream's
353+ // time base happens to be `1/sample_rate`, which a
354+ // container's own duration field usually is not.
355+ let padding_ticks = s
356+ .params
357+ .audio()
358+ .filter(|_| s.initial_padding > 0)
359+ .map(|a| {
360+ TimeBase::from_rate(a.sample_rate).rescale(
361+ i64::from(s.initial_padding),
362+ s.time_base,
363+ Rounding::Down,
364+ )
365+ })
366+ .unwrap_or(0);
367+ Timestamp::new(d - padding_ticks, s.time_base)
368+ })
352369 })
353370 .max_by(|a, b| {
354371 a.as_secs_f64()
@@ -719,6 +736,24 @@ mod tests {
719736 assert_eq!(sniff(b"not a media file at all"), None);
720737 }
721738
739+ /// `initial_padding` is a *sample* count while a stream's `duration` is in
740+ /// `time_base` ticks -- for a Matroska file whose `TimestampScale` is not
741+ /// `1/sample_rate` (the common case: ticks are milliseconds), subtracting
742+ /// one from the other unconverted lopped a third off an AAC track's
743+ /// stated length (3.021 s of `CodecDelay`-bearing audio reported as 1.997
744+ /// s: `1024` samples read straight off as `1024` one-millisecond ticks).
745+ #[test]
746+ fn duration_converts_initial_padding_out_of_sample_units() {
747+ let path = Path::new(env!("CARGO_MANIFEST_DIR"))
748+ .join("../../fixtures/audio/aac-mka-stereo-48000.mka");
749+ if !path.exists() {
750+ return; // fixtures are generated, not checked in
751+ }
752+ let reader = Reader::open(&path).unwrap();
753+ let d = reader.duration().expect("a stated duration").as_secs_f64();
754+ assert!((d - 3.0).abs() < 0.1, "duration {d}, want ~3.0s");
755+ }
756+
722757 /// An `.aac` or `.mp3` that opens with a tag is still an `.aac` or `.mp3`.
723758 #[test]
724759 fn an_id3_tag_does_not_hide_the_format() {