forge
Explore
Log in

Don't you dare go Hollow

R2 bisect -- BWDREF compound divergence is per-block, not DPB slot

Browse files at this commit

70c2a11697a1ddb26415c491bfc08fd974e56557Ahmet Kaan GümüşParent: 793125d
  • fix
    • r2 bisect -- BWDREF compound divergence is per-block, not DPB slot
Show full message
fix(av1): r2 bisect -- BWDREF compound divergence is per-block, not DPB slot

r1's fixture (av1blend-r1-mismatch.obu) was gitignored and lost; 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, already correctly gated on frame_type==Key in
ec-av1-syntax's frame.rs) found nothing wrong -- the DPB slot bookkeeping
r1 suspected is spec-correct.

A fresh unmasked sweep (seed 45, same recipe) isolated a narrower shape:
decode-order frame 3 (BWDREF compound, zero MV, simple average) is the
first to diverge from ffmpeg even though both its compound inputs are
independently proven pixel-exact and the blend arithmetic is the same
formula r1 proved algebraically exact. That rules out a stale/wrong
reference picture for this fixture -- the defect is in that frame's own
per-block decode (entropy desync or the compound mode/MV read itself),
not reference-slot bookkeeping. Not isolated further within budget.

Both masks stay (comp_group_idx == 0 and == 1); instrumentation is
env-gated (EC_AV1_COMPOUND_DEBUG frame-state trace in stream.rs,
scratch_sweep_compound_mismatch to find a fresh repro seed). Full
`cargo test --release -p ec-av1 --lib`: 214 passed, 0 failed, 18 ignored.
crates/ec-av1/src/decode.rsmodified+36 -3
@@ -4519,12 +4519,45 @@ fn decode_inter_block(
4519 // reads) against aomdec's own `RefCntBuffer` bookkeeping on4519 // reads) against aomdec's own `RefCntBuffer` bookkeeping on
4520 // `fixtures/av1blend-r1-mismatch.obu`, starting at decode-order4520 // `fixtures/av1blend-r1-mismatch.obu`, starting at decode-order
4521 // frame 2.4521 // frame 2.
4522+ //
4523+ // r2 (fresh fixture, seed 45, same recipe -- the original r1
4524+ // fixture is gitignored and could not be regenerated byte-exact):
4525+ // static review of `stream.rs`'s `ref_slots`/`refresh_frame_flags`/
4526+ // `cdf_slots` bookkeeping and the `show_existing_frame` refresh
4527+ // arm (spec 7.21: only refreshes on a shown KEY frame, which the
4528+ // parser already enforces at `frame.rs:791/815` -- `refresh_frame_flags`
4529+ // is forced 0 on every non-key `show_existing_frame`) turned up
4530+ // nothing wrong; `other_refs`/`ref_planes` slot indexing for
4531+ // `BWDREF_FRAME` is spec-correct too. A live unmasked run
4532+ // isolated a *different, smaller* shape than r1's: decode-order
4533+ // frame 3 (a `ref1 == BWDREF_FRAME`, zero-MV, simple-average
4534+ // compound block) is the first to diverge from ffmpeg (worst
4535+ // delta 1, luma only, clustered at 32-pixel block edges) even
4536+ // though BOTH its compound inputs -- the key frame and
4537+ // decode-order frame 2's own picture (`ref1 == ALTREF_FRAME`,
4538+ // itself proven bit-exact: it is later re-shown via
4539+ // `show_existing_frame` and matches ffmpeg exactly) -- are
4540+ // independently pixel-exact, and the blend arithmetic is the
4541+ // same proven-exact formula. That rules out "wrong slot"/"stale
4542+ // picture" for this fixture: the inputs are right, so the defect
4543+ // is in decode-order frame 3's *own* per-block decode (entropy
4544+ // desync from compound-CDF adaptation carried across a slot, or
4545+ // the compound mode/MV read itself) -- not in `ref_slots`/
4546+ // `refresh_frame_flags`. Not isolated further within budget.
4547+ // Upgrade: bisect decode-order frame 3's tile decode against a
4548+ // synced `EC_TOK`/`EC_PART` trace from the instrumented aomdec
4549+ // build (`/tmp/libaom-src/build/decoder-debug/aomdec`) on
4550+ // `fixtures/av1blend-r1-mismatch.obu` (seed 45 now, not seed 49)
4551+ // -- the pre-deblock `EC_AV1_PREFILT_DUMP` frame-index alignment
4552+ // between this crate and that aomdec build is NOT 1:1 on this
4553+ // fixture (23 dumps here vs 24 there), so calibrate that first.
4522 return Err(unsupported(4554 return Err(unsupported(
4523 "a COMPOUND_REFERENCE block using the plain/distance-weighted \4555 "a COMPOUND_REFERENCE block using the plain/distance-weighted \
4524 average blend (comp_group_idx == 0) -- proven non-pixel-exact \4556 average blend (comp_group_idx == 0) -- proven non-pixel-exact \
4525- against a real aomenc stream (lane-av1blend r1, see \4557+ against a real aomenc stream (lane-av1blend r1/r2, see \
4526- av1blend-r1-mismatch.obu: a reference-slot bug, not an MC/blend \4558+ av1blend-r1-mismatch.obu and the comment above: a per-block \
4527- arithmetic bug -- see the comment above)",4559+ decode defect surfacing only on chained BWDREF compound, not \
4560+ an MC/blend arithmetic bug)",
4528 ));4561 ));
4529 #[allow(unreachable_code)]4562 #[allow(unreachable_code)]
4530 let (fwd_offset, bck_offset, compound_idx) = if !skip_mode && enable_jnt_comp {4563 let (fwd_offset, bck_offset, compound_idx) = if !skip_mode && enable_jnt_comp {
crates/ec-av1/src/stream.rsmodified+99 -0
@@ -74,6 +74,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
74 let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None);74 let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None);
75 75
76 let mut pos = 0usize;76 let mut pos = 0usize;
77+ let mut decode_idx = 0u32;
77 while pos < data.len() {78 while pos < data.len() {
78 let obu = parser.parse_obu(&data[pos..])?;79 let obu = parser.parse_obu(&data[pos..])?;
79 let obu_offset = pos;80 let obu_offset = pos;
@@ -294,6 +295,19 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
294 let ref_order_hints: [u32; 7] =295 let ref_order_hints: [u32; 7] =
295 std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]);296 std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]);
296 297
298+ if std::env::var("EC_AV1_COMPOUND_DEBUG").is_ok() {
299+ eprintln!(
300+ "FRAME decode_idx={decode_idx} type={:?} show={} order_hint={} refresh_frame_flags={:#04x} ref_frame_idx={:?} order_hint_slots={:?}",
301+ header.frame_type,
302+ header.show_frame,
303+ header.order_hint,
304+ header.refresh_frame_flags,
305+ header.ref_frame_idx,
306+ order_hint_slots,
307+ );
308+ }
309+ decode_idx += 1;
310+
297 let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key {311 let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key {
298 let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs(312 let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs(
299 tile_bytes,313 tile_bytes,
@@ -3245,6 +3259,91 @@ mod tests {
3245 }3259 }
3246 }3260 }
3247 3261
3262+ /// lane-av1blend r2 scratch: sweep every seed of the compound_references
3263+ /// recipe (with the plain-average blend UNMASKED locally) looking for a
3264+ /// pixel mismatch, dumping the first one found to `EC_AV1_GATE_DUMP`.
3265+ /// Remove with the rest of the r1/r2 scaffolding.
3266+ #[test]
3267+ #[ignore]
3268+ fn scratch_sweep_compound_mismatch() {
3269+ if !have_ffmpeg() || !have_aomenc() {
3270+ eprintln!("SKIP: missing ffmpeg/aomenc");
3271+ return;
3272+ }
3273+ let (width, height, frame_count) = (64usize, 64usize, 24usize);
3274+ for seed in 42..82u32 {
3275+ let duration = frame_count as f64 / 25.0;
3276+ let source =
3277+ format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25");
3278+ let y4m = Command::new("ffmpeg")
3279+ .args([
3280+ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-f",
3281+ "yuv4mpegpipe", "-",
3282+ ])
3283+ .stdin(Stdio::null())
3284+ .stdout(Stdio::piped())
3285+ .stderr(Stdio::piped())
3286+ .output()
3287+ .expect("ffmpeg failed to run");
3288+ assert!(y4m.status.success());
3289+ let args: Vec<&str> = vec![
3290+ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0",
3291+ "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1",
3292+ "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1",
3293+ "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0",
3294+ "--enable-interintra-comp=0", "--enable-onesided-comp=0",
3295+ "--enable-interintra-wedge=0", "--enable-smooth-interintra=0",
3296+ "--enable-rect-partitions=0", "--enable-ab-partitions=0",
3297+ "--enable-1to4-partitions=0", "--enable-filter-intra=0",
3298+ "--enable-smooth-intra=0", "--enable-paeth-intra=0",
3299+ "--enable-directional-intra=0", "--enable-angle-delta=0",
3300+ "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0",
3301+ "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0",
3302+ "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0",
3303+ "--obu", "-o", "-", "-",
3304+ ];
3305+ let mut child = Command::new(aomenc_path())
3306+ .args(&args)
3307+ .stdin(Stdio::piped())
3308+ .stdout(Stdio::piped())
3309+ .stderr(Stdio::piped())
3310+ .spawn()
3311+ .expect("aomenc failed to start");
3312+ child
3313+ .stdin
3314+ .take()
3315+ .expect("aomenc stdin")
3316+ .write_all(&y4m.stdout)
3317+ .expect("writing y4m to aomenc");
3318+ let out = child.wait_with_output().expect("aomenc failed to run");
3319+ assert!(out.status.success());
3320+ let stream = out.stdout;
3321+ let frames = match decode_stream(&stream) {
3322+ Err(_) => continue,
3323+ Ok(frames) => frames,
3324+ };
3325+ if frames.len() != frame_count {
3326+ continue;
3327+ }
3328+ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count);
3329+ let mismatched = frames
3330+ .iter()
3331+ .zip(&ffmpeg_frames)
3332+ .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v);
3333+ if mismatched {
3334+ eprintln!("MISMATCH seed {seed}");
3335+ if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") {
3336+ std::fs::write(&path, &stream).expect("writing pinned stream");
3337+ eprintln!("wrote {path}");
3338+ }
3339+ return;
3340+ } else {
3341+ eprintln!("seed {seed}: bit-exact");
3342+ }
3343+ }
3344+ eprintln!("no mismatch found in sweep");
3345+ }
3346+
3248 /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=83347 /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8
3249 /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec3348 /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec
3250 /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame3349 /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame