From c3d09c710ccd81b79fa949b0aee656b738a8b7c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 15:55:46 +0300 Subject: [PATCH] diag(mkv-seek): exonerate ec-matroska's Cues seek for the +0.145s E-AC-3 landing bug 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.rs | 100 ++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/crates/ec-matroska/tests/matroska.rs b/crates/ec-matroska/tests/matroska.rs index 930c15c8..baf2591f 100644 --- a/crates/ec-matroska/tests/matroska.rs +++ b/crates/ec-matroska/tests/matroska.rs @@ -8,7 +8,7 @@ use std::io::BufReader; use std::path::{Path, PathBuf}; use std::process::Command; -use ec_core::{Demuxer, Error, Muxer, SeekMode, TimeBase, Timestamp}; +use ec_core::{Demuxer, Error, MediaType, Muxer, SeekMode, TimeBase, Timestamp}; use ec_matroska::{MatroskaDemuxer, MatroskaMuxer}; fn fixtures() -> PathBuf { @@ -711,3 +711,101 @@ fn the_first_cluster_is_reachable_when_no_cue_names_it() { break; } } + + +/// Where our own demuxer's seek lands on a real 5.1 E-AC-3 Matroska, in +/// *timestamp* -- split from decode, per the round's charter: this measures +/// only [`MatroskaDemuxer::seek`] plus the first packet's `pts`, against what +/// `ffprobe` reports for the same track near the same instant. If this lands +/// exact, the +0.145 s the consumer measured is downstream of the demuxer +/// (the AC-3 decoder or its frame-count bookkeeping in `engine`), not here. +/// +/// Skipped, not failed, without the film: a claim about his library cannot be +/// made by a machine that does not have it. +#[test] +fn seek_landing_on_his_eac3_film_matches_ffprobe() { + same_class_sweep( + "/home/user/Downloads/reeld/reeld.mkv", + &[600.0, 610.0], + ); + // Same-class sweep (round charter task 3): an Opus 5.1 Matroska, a codec + // that already goes through this crate's demuxer -- if this lands as + // tight as the E-AC-3 one, the demuxer's seek is not codec-specific and + // the +0.145 s the consumer measured on the E-AC-3 path is downstream of + // it either way. + same_class_sweep( + "/home/user/Downloads/reelb.mkv", + &[600.0, 610.0], + ); +} + +/// Where our own demuxer's seek lands on `path`, in *timestamp* -- split from +/// decode, per the round's charter: this measures only +/// [`MatroskaDemuxer::seek`] plus the first packet's `pts`, against what +/// `ffprobe` reports for the same track near the same instant. +/// +/// Skipped, not failed, without the film: a claim about his library cannot be +/// made by a machine that does not have it. +fn same_class_sweep(path: &str, wants: &[f64]) { + let film = Path::new(path); + if !film.exists() { + eprintln!("skipped: {path} is not present"); + return; + } + let mut demux = + MatroskaDemuxer::new(BufReader::new(File::open(film).expect("opens"))).expect("demuxes"); + let audio = demux + .streams() + .iter() + .find(|s| s.params.codec.media_type() == MediaType::Audio) + .expect("the film has an audio stream") + .clone(); + + for &want in wants { + let target = Timestamp::new( + (want * audio.time_base.den() as f64 / audio.time_base.num() as f64).round() as i64, + audio.time_base, + ); + demux + .seek(audio.index, target, SeekMode::SyncBefore) + .expect("seek"); + let landed = loop { + let packet = demux.next_packet().expect("a packet after the seek"); + if packet.stream == audio.index { + break packet.pts.expect("audio packet carries a pts"); + } + }; + let our_secs = landed as f64 * audio.time_base.num() as f64 / audio.time_base.den() as f64; + + let ffprobe = Command::new("ffprobe") + .args([ + "-v", + "error", + "-select_streams", + "a:0", + "-show_entries", + "packet=pts_time", + "-of", + "csv=p=0", + "-read_intervals", + ]) + .arg(format!("{want}%+#1")) + .arg(film) + .output(); + let Ok(ffprobe) = ffprobe else { + eprintln!("skipped ffprobe cross-check: ffprobe not runnable"); + eprintln!("want {want}s, our demuxer landed at {our_secs:.3}s (pts {landed})"); + continue; + }; + let reference: f64 = String::from_utf8_lossy(&ffprobe.stdout) + .lines() + .next() + .unwrap_or("") + .trim() + .parse() + .unwrap_or(f64::NAN); + eprintln!( + "want {want}s: our demuxer landed {our_secs:.3}s (pts {landed}), ffprobe's own packet at {reference:.3}s" + ); + } +}