diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 9ee1d95b..e20e4a89 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -4519,12 +4519,45 @@ fn decode_inter_block( // reads) against aomdec's own `RefCntBuffer` bookkeeping on // `fixtures/av1blend-r1-mismatch.obu`, starting at decode-order // frame 2. + // + // r2 (fresh fixture, seed 45, same recipe -- the original r1 + // fixture is gitignored and could not be regenerated byte-exact): + // static review of `stream.rs`'s `ref_slots`/`refresh_frame_flags`/ + // `cdf_slots` bookkeeping and the `show_existing_frame` refresh + // arm (spec 7.21: only refreshes on a shown KEY frame, which the + // parser already enforces at `frame.rs:791/815` -- `refresh_frame_flags` + // is forced 0 on every non-key `show_existing_frame`) turned up + // nothing wrong; `other_refs`/`ref_planes` slot indexing for + // `BWDREF_FRAME` is spec-correct too. A live unmasked run + // isolated a *different, smaller* shape than r1's: decode-order + // frame 3 (a `ref1 == BWDREF_FRAME`, zero-MV, simple-average + // compound block) is the first to diverge from ffmpeg (worst + // delta 1, luma only, clustered at 32-pixel block edges) even + // though BOTH its compound inputs -- the key frame and + // decode-order frame 2's own picture (`ref1 == ALTREF_FRAME`, + // itself proven bit-exact: it is later re-shown via + // `show_existing_frame` and matches ffmpeg exactly) -- are + // independently pixel-exact, and the blend arithmetic is the + // same proven-exact formula. That rules out "wrong slot"/"stale + // picture" for this fixture: the inputs are right, so the defect + // is in decode-order frame 3's *own* per-block decode (entropy + // desync from compound-CDF adaptation carried across a slot, or + // the compound mode/MV read itself) -- not in `ref_slots`/ + // `refresh_frame_flags`. Not isolated further within budget. + // Upgrade: bisect decode-order frame 3's tile decode against a + // synced `EC_TOK`/`EC_PART` trace from the instrumented aomdec + // build (`/tmp/libaom-src/build/decoder-debug/aomdec`) on + // `fixtures/av1blend-r1-mismatch.obu` (seed 45 now, not seed 49) + // -- the pre-deblock `EC_AV1_PREFILT_DUMP` frame-index alignment + // between this crate and that aomdec build is NOT 1:1 on this + // fixture (23 dumps here vs 24 there), so calibrate that first. return Err(unsupported( "a COMPOUND_REFERENCE block using the plain/distance-weighted \ average blend (comp_group_idx == 0) -- proven non-pixel-exact \ - against a real aomenc stream (lane-av1blend r1, see \ - av1blend-r1-mismatch.obu: a reference-slot bug, not an MC/blend \ - arithmetic bug -- see the comment above)", + against a real aomenc stream (lane-av1blend r1/r2, see \ + av1blend-r1-mismatch.obu and the comment above: a per-block \ + decode defect surfacing only on chained BWDREF compound, not \ + an MC/blend arithmetic bug)", )); #[allow(unreachable_code)] let (fwd_offset, bck_offset, compound_idx) = if !skip_mode && enable_jnt_comp { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index b37551e1..855aef38 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -74,6 +74,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { let mut cdf_slots: [Option; NUM_REF_FRAMES] = std::array::from_fn(|_| None); let mut pos = 0usize; + let mut decode_idx = 0u32; while pos < data.len() { let obu = parser.parse_obu(&data[pos..])?; let obu_offset = pos; @@ -294,6 +295,19 @@ pub fn decode_stream(data: &[u8]) -> Result> { let ref_order_hints: [u32; 7] = std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]); + if std::env::var("EC_AV1_COMPOUND_DEBUG").is_ok() { + eprintln!( + "FRAME decode_idx={decode_idx} type={:?} show={} order_hint={} refresh_frame_flags={:#04x} ref_frame_idx={:?} order_hint_slots={:?}", + header.frame_type, + header.show_frame, + header.order_hint, + header.refresh_frame_flags, + header.ref_frame_idx, + order_hint_slots, + ); + } + decode_idx += 1; + let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( tile_bytes, @@ -3245,6 +3259,91 @@ mod tests { } } + /// lane-av1blend r2 scratch: sweep every seed of the compound_references + /// recipe (with the plain-average blend UNMASKED locally) looking for a + /// pixel mismatch, dumping the first one found to `EC_AV1_GATE_DUMP`. + /// Remove with the rest of the r1/r2 scaffolding. + #[test] + #[ignore] + fn scratch_sweep_compound_mismatch() { + if !have_ffmpeg() || !have_aomenc() { + eprintln!("SKIP: missing ffmpeg/aomenc"); + return; + } + let (width, height, frame_count) = (64usize, 64usize, 24usize); + for seed in 42..82u32 { + let duration = frame_count as f64 / 25.0; + let source = + format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-f", + "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!(y4m.status.success()); + let args: Vec<&str> = vec![ + "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", + "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", + "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", + "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", + "--enable-interintra-comp=0", "--enable-onesided-comp=0", + "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", + "--enable-rect-partitions=0", "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", "--enable-filter-intra=0", + "--enable-smooth-intra=0", "--enable-paeth-intra=0", + "--enable-directional-intra=0", "--enable-angle-delta=0", + "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", + "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", + "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", + "--obu", "-o", "-", "-", + ]; + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!(out.status.success()); + let stream = out.stdout; + let frames = match decode_stream(&stream) { + Err(_) => continue, + Ok(frames) => frames, + }; + if frames.len() != frame_count { + continue; + } + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if mismatched { + eprintln!("MISMATCH seed {seed}"); + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!("wrote {path}"); + } + return; + } else { + eprintln!("seed {seed}: bit-exact"); + } + } + eprintln!("no mismatch found in sweep"); + } + /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8 /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame