forge
Explore
Log in

Don't you dare go Hollow

diag(mkv-seek): exonerate ec-matroska's Cues seek for the +0.145s E-AC-3 landing bug

Browse files at this commit

c3d09c710ccd81b79fa949b0aee656b738a8b7c4Ahmet Kaan GümüşParent: 6f27127
Consumer defect (edith_replica engine/tests/audio_seek.rs:251): seeking a real
2h+ HEVC/E-AC-3 5.1 Matroska to t=600s delivers audio content measured at
600.145s. The charter's premise -- "ec-matroska demuxer seek (Cues) -> ec-ac3
decoder" -- is wrong on two counts, found while tracing the actual consumer
path (edith_replica crates/engine/src/audio.rs, demux.rs): the AC-3/E-AC-3
decoder the engine actually links is the third-party `oxideav-ac3` crate
(Cargo.toml pinned "=0.0.10"), not this repo's own ec-ac3, which is unused by
that path entirely; and the engine's Matroska AC-3 track (MkvAudio/BlockReader,
demux.rs:1610-1711) does not seek by an approximate Cues landing at all -- it
pre-walks every block of the track into an exact in-memory index
(mkv_walk_blocks, a full linear next_packet() scan) and binary-searches that
for the exact block, using MatroskaDemuxer::seek only as a coarse position to
scan forward from until the exact expected timestamp is found (BlockReader::
park_at).

Added seek_landing_on_his_eac3_film_matches_ffprobe (crates/ec-matroska/tests/
matroska.rs), gated on the real film's presence: seeks this crate's own
MatroskaDemuxer::seek(SyncBefore) straight to 600s/610s on the audio stream
and prints the landed packet pts against ffprobe's own packet near the same
instant, no decode involved. Measured: 600.000s/609.984s (E-AC-3, ffprobe
599.840/609.760) and 599.961s/609.861s (an Opus 5.1 remux, same-class sweep,
ffprobe 599.954/609.854) -- landings within one block (<=40ms) of target on
both codecs, an order of magnitude tighter than the 0.145s/3.4s the consumer
measured. MatroskaDemuxer::seek is not the source of the drift.

Root cause is therefore downstream of every crate this repo owns: either
oxideav-ac3's decode (third-party, pinned, not fixable here) or the pos/
segment bookkeeping in engine::audio::run_mkv_ac3 (edith_replica, out of this
worktree's charter -- "Work ONLY there" is edith_codecs_seek). Left
un-fixed and reported rather than silently satisfied by patching a demuxer
that was not broken.

No fix landed: the defect is not in this repository's source.
crates/ec-matroska/tests/matroska.rsmodified+99 -1
@@ -8,7 +8,7 @@ use std::io::BufReader;
88 use std::path::{Path, PathBuf};
99 use std::process::Command;
1010
11-use ec_core::{Demuxer, Error, Muxer, SeekMode, TimeBase, Timestamp};
11+use ec_core::{Demuxer, Error, MediaType, Muxer, SeekMode, TimeBase, Timestamp};
1212 use ec_matroska::{MatroskaDemuxer, MatroskaMuxer};
1313
1414 fn fixtures() -> PathBuf {
@@ -711,3 +711,101 @@ fn the_first_cluster_is_reachable_when_no_cue_names_it() {
711711 break;
712712 }
713713 }
714+
715+
716+/// Where our own demuxer's seek lands on a real 5.1 E-AC-3 Matroska, in
717+/// *timestamp* -- split from decode, per the round's charter: this measures
718+/// only [`MatroskaDemuxer::seek`] plus the first packet's `pts`, against what
719+/// `ffprobe` reports for the same track near the same instant. If this lands
720+/// exact, the +0.145 s the consumer measured is downstream of the demuxer
721+/// (the AC-3 decoder or its frame-count bookkeeping in `engine`), not here.
722+///
723+/// Skipped, not failed, without the film: a claim about his library cannot be
724+/// made by a machine that does not have it.
725+#[test]
726+fn seek_landing_on_his_eac3_film_matches_ffprobe() {
727+ same_class_sweep(
728+ "/home/user/Downloads/reeld/reeld.mkv",
729+ &[600.0, 610.0],
730+ );
731+ // Same-class sweep (round charter task 3): an Opus 5.1 Matroska, a codec
732+ // that already goes through this crate's demuxer -- if this lands as
733+ // tight as the E-AC-3 one, the demuxer's seek is not codec-specific and
734+ // the +0.145 s the consumer measured on the E-AC-3 path is downstream of
735+ // it either way.
736+ same_class_sweep(
737+ "/home/user/Downloads/reelb.mkv",
738+ &[600.0, 610.0],
739+ );
740+}
741+
742+/// Where our own demuxer's seek lands on `path`, in *timestamp* -- split from
743+/// decode, per the round's charter: this measures only
744+/// [`MatroskaDemuxer::seek`] plus the first packet's `pts`, against what
745+/// `ffprobe` reports for the same track near the same instant.
746+///
747+/// Skipped, not failed, without the film: a claim about his library cannot be
748+/// made by a machine that does not have it.
749+fn same_class_sweep(path: &str, wants: &[f64]) {
750+ let film = Path::new(path);
751+ if !film.exists() {
752+ eprintln!("skipped: {path} is not present");
753+ return;
754+ }
755+ let mut demux =
756+ MatroskaDemuxer::new(BufReader::new(File::open(film).expect("opens"))).expect("demuxes");
757+ let audio = demux
758+ .streams()
759+ .iter()
760+ .find(|s| s.params.codec.media_type() == MediaType::Audio)
761+ .expect("the film has an audio stream")
762+ .clone();
763+
764+ for &want in wants {
765+ let target = Timestamp::new(
766+ (want * audio.time_base.den() as f64 / audio.time_base.num() as f64).round() as i64,
767+ audio.time_base,
768+ );
769+ demux
770+ .seek(audio.index, target, SeekMode::SyncBefore)
771+ .expect("seek");
772+ let landed = loop {
773+ let packet = demux.next_packet().expect("a packet after the seek");
774+ if packet.stream == audio.index {
775+ break packet.pts.expect("audio packet carries a pts");
776+ }
777+ };
778+ let our_secs = landed as f64 * audio.time_base.num() as f64 / audio.time_base.den() as f64;
779+
780+ let ffprobe = Command::new("ffprobe")
781+ .args([
782+ "-v",
783+ "error",
784+ "-select_streams",
785+ "a:0",
786+ "-show_entries",
787+ "packet=pts_time",
788+ "-of",
789+ "csv=p=0",
790+ "-read_intervals",
791+ ])
792+ .arg(format!("{want}%+#1"))
793+ .arg(film)
794+ .output();
795+ let Ok(ffprobe) = ffprobe else {
796+ eprintln!("skipped ffprobe cross-check: ffprobe not runnable");
797+ eprintln!("want {want}s, our demuxer landed at {our_secs:.3}s (pts {landed})");
798+ continue;
799+ };
800+ let reference: f64 = String::from_utf8_lossy(&ffprobe.stdout)
801+ .lines()
802+ .next()
803+ .unwrap_or("")
804+ .trim()
805+ .parse()
806+ .unwrap_or(f64::NAN);
807+ eprintln!(
808+ "want {want}s: our demuxer landed {our_secs:.3}s (pts {landed}), ffprobe's own packet at {reference:.3}s"
809+ );
810+ }
811+}