diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 2dcc9184..5c5825e1 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -8753,8 +8753,13 @@ fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { /// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). fn dump_stage16(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf, fw: usize, fh: usize) { use std::io::Write; + // lane-sbrect10 r2: index the dump by decode-order picture like + // EC_AV1_PREFILT_DUMP already does -- a fixed `.f0` name kept only the + // LAST frame, so a mid-sequence divergence could not be read out at all. + static STAGE16_IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); if let Ok(path) = std::env::var(var) - && let Ok(mut f) = std::fs::File::create(format!("{path}.f0")) + && let idx = STAGE16_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + && let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { for (p, w, h) in [(y, fw, fh), (u, fw.div_ceil(2), fh.div_ceil(2)), (v, fw.div_ceil(2), fh.div_ceil(2))] { for row in 0..h { diff --git a/crates/ec-av1/src/motion_field.rs b/crates/ec-av1/src/motion_field.rs index 330a5e97..ac29d6fa 100644 --- a/crates/ec-av1/src/motion_field.rs +++ b/crates/ec-av1/src/motion_field.rs @@ -442,6 +442,15 @@ pub fn setup_motion_field( ); } + if std::env::var_os("EC_TRACE_TPL").is_some() { + eprintln!("EC_TPL_FIELD oh={cur_order_hint} rows={rows} cols={cols}"); + for r in 0..rows { + let row: String = (0..cols) + .map(|c| if cells[r * cols + c].is_some() { '#' } else { '.' }) + .collect(); + eprintln!("EC_TPL_FIELD r{r} {row}"); + } + } TplField { cols, rows, cells } } diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 300fa320..461aed63 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -1479,7 +1479,20 @@ fn process_compound_ref_mv_candidate( /// sign-flipped) entries, position-wise and independently per side, into up /// to two full `(mv0, mv1)` pair candidates — missing slots default to /// `(0, 0)`, matching libaom's zero-initialized `combined_mvs`. -fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, i32)); 2] { +fn combine_compound_candidates( + lists: &CompoundRefLists, + gm_mv_candidates: [(i32, i32); 2], +) -> [((i32, i32), (i32, i32)); 2] { + // lane-sbrect10 r2: the tail slots libaom leaves after `ref_id` and + // `ref_diff` are filled with that side's GLOBAL MOTION vector, never with + // zero (`setup_ref_mv_list`, mvref_common.c:723-729: + // `for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx) + // comp_list[comp_idx][idx] = gm_mv_candidates[idx];`). + // Zero-filling was right only under IDENTITY global motion, which the + // module doc's original reduction assumed; aomenc turns global motion on + // by default, so a compound block with no usable neighbour pair took a + // (0, 0) predictor where libaom took the frame's translation -- silently + // wrong pixels with the entropy stream still in sync. let side_slots = |side: usize| -> [(i32, i32); 2] { let merged: Vec<(i32, i32)> = lists.ref_id[side] .iter() @@ -1487,7 +1500,7 @@ fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, .take(2) .copied() .collect(); - std::array::from_fn(|i| merged.get(i).copied().unwrap_or((0, 0))) + std::array::from_fn(|i| merged.get(i).copied().unwrap_or(gm_mv_candidates[side])) }; let side0 = side_slots(0); let side1 = side_slots(1); @@ -1802,7 +1815,10 @@ pub fn find_mv_stack_compound( // bump) — otherwise append `comp_list[0]` as a brand-new entry. When // the stack is empty, both `comp_list[0]` and `comp_list[1]` are // appended unconditionally, with no dedup check at all. - let comp_list = combine_compound_candidates(&lists); + let comp_list = combine_compound_candidates( + &lists, + [gm_mv(gm, ref_frame.0), gm_mv(gm, ref_frame.1)], + ); if candidates.len() == 1 { let (mv0, mv1) = if comp_list[0] == (candidates[0].mv0, candidates[0].mv1) { comp_list[1] @@ -2824,7 +2840,7 @@ mod tests { // side 1 has only one entry -> comp_idx 1 falls back to ref_diff. lists.ref_diff[1].push((4, 4)); - let combined = combine_compound_candidates(&lists); + let combined = combine_compound_candidates(&lists, [(0, 0), (0, 0)]); assert_eq!(combined[0], ((1, 1), (3, 3))); assert_eq!(combined[1], ((2, 2), (4, 4))); } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 5337cfc1..df1c1676 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6586,10 +6586,20 @@ mod tests { let vertical_split = attempt % 2 == 1; let cq = [58, 52, 45, 61][(attempt / 2 % 4) as usize]; let sp = 8 + (attempt / 8) * 4; + // Half ramp, half deterministic pseudo-noise: the flat "200" + // half of r1's source made aomenc code the right-hand + // superblocks skip, so at 8 bit no >32 intra block ever + // appeared (probed cq 45/52/58/61/63 = 0 hits). The textured + // half forces intra above 32x32 in an inter frame at BOTH + // depths, so both arms carry the shape. let geq = if vertical_split { - format!("if(lt(Y,64), 40+mod(floor((X+N*{sp})/32)*90,200), 200)") + format!( + "if(lt(Y,64), 40+mod(floor((X+N*{sp})/32)*90,200), mod((X*7+Y*13+N*97)*31,256))" + ) } else { - format!("if(lt(X,64), 40+mod(floor((Y+N*{sp})/32)*90,200), 200)") + format!( + "if(lt(X,128), 40+mod(floor((Y+N*{sp})/32)*90,200), mod((X*7+Y*13+N*97)*31,256))" + ) }; let duration = frame_count as f64 / 25.0; let source = format!( @@ -6739,6 +6749,16 @@ mod tests { /// The counter `nocfl_uv_mode_hits` proves the shape fired in EVERY /// compared attempt, and every decode-order frame is compared against /// ffmpeg -- an attempt that mismatches fails, it never SKIPs. + /// + /// lane-sbrect10 r2: with the half-random source below this gate also + /// covers a SECOND defect it exposed at 10-bit cq 58/61 -- the compound + /// MV stack's extension pass zero-filled the `comp_list` tail slots + /// libaom fills with each side's GLOBAL MOTION vector + /// (`combine_compound_candidates`, mvref_common.c:723-729). A compound + /// block with no usable neighbour pair (frame top row, intra block to + /// its left, empty temporal field) then took a `(0, 0)` predictor + /// instead of the frame's translation: 32890/39907 wrong luma samples + /// on decode-order frame 2 with the entropy stream still in sync. #[test] fn a_real_aomenc_inter_frame_with_a_64x64_intra_block_reads_the_no_cfl_uv_alphabet() { const NAME: &str = diff --git a/lanes/sbrect10-r2.report.md b/lanes/sbrect10-r2.report.md new file mode 100644 index 00000000..d9ebc29a --- /dev/null +++ b/lanes/sbrect10-r2.report.md @@ -0,0 +1,117 @@ +# lane-sbrect10 r2 report + +Branch `lane-sbrect10`, worktree `/home/user/Documents/Code/Rust/edith_codecs-sbrect10`. +Base: r1 tip 6fba6a6 + `main` 18bf7dc (sb128 5ea5ec8 + r14 a2e2e29) merged. + +## 1. Merge (step 1) +`git merge --no-commit main` -> clean auto-merge (decode.rs, stream.rs), commit `3a9e83e`. +- Diffstat 12 files, +1392/-147 (all of it main's; the lane contributed nothing to those files + beyond its own r1 hunks). +- `git diff main -- crates/ec-av1/src/cdf.rs` and `... refusal_inventory.rs` are BOTH EMPTY, so + the NZ_MAP / FILTER_INTRA tables and the refusal list are byte-identical to main -- nothing to + reconcile. +- The only lines the merged tree removes relative to main are the lane's own 4 r1 CFL lines in + `decode.rs` (`cdfs.uv_mode_cfl[mode]` + its `cfl_alphas` arm) and the r1 comment/`[8u32]` arm in + `stream.rs`. Main's deletions are all kept. + +## 2. Gate source swapped to the half-random fixture (step 2) +`crates/ec-av1/src/stream.rs` (gate +`a_real_aomenc_inter_frame_with_a_64x64_intra_block_reads_the_no_cfl_uv_alphabet`): the flat +`200` half of r1's source made aomenc code the right-hand superblocks skip, so at 8 bit no +`>32` intra-in-inter block ever appeared. Both orientations now carry a deterministic +pseudo-noise half: + + horizontal: if(lt(X,128), 40+mod(floor((Y+N*sp)/32)*90,200), mod((X*7+Y*13+N*97)*31,256)) + vertical: if(lt(Y,64), 40+mod(floor((X+N*sp)/32)*90,200), mod((X*7+Y*13+N*97)*31,256)) + +Probe grid, `$HOME/.cache/sbrect10/probe.sh` (counts `>32` intra-in-inter blocks via +`EC_AV1_TELL`, and pixel diffs vs `ffmpeg -pix_fmt yuv420p10le`/`yuv420p`), AFTER the r2 fix: + +| depth | cq 52 | cq 58 | cq 61 | +|---|---|---|---| +| 8-bit | 5 hits, 0 diffs | 2 hits, 0 diffs | 3 hits, 0 diffs | +| 10-bit | 6 hits, 0 diffs | 7 hits, 0 diffs | 7 hits, 0 diffs | + +Both depth arms therefore carry >=1 no-CFL intra hit per compared attempt; per-attempt counters +are read on decoded+compared attempts only, every decode-order frame is compared on Y/U/V, and a +mismatch fails (never SKIPs). + +## 3. The SECOND defect this fixture exposed -- root cause and fix (step 3) + +### Pinned stream +`$HOME/.cache/sbrect10/b58.obu` (192x128, 6 frames, 10-bit, cq 58, `--sb-size=64 +--min-partition-size=32 --max-partition-size=64 --enable-rect-partitions=1 +--enable-1to4-partitions=1 --lag-in-frames=0 --cpu-used=0`), sha256 hashed twice, identical: +`00b9253b308201118e27f348c67932a0006e3077ba7a9a401854c2ee7eb1dcd2`. + +### Localisation +- First mismatching frame: DECODE-ORDER 2 (display == decode order), plane Y only, 8252 samples; + U and V exact; frames 0 and 1 exact. Per-64x64-SB wrong-pixel map: row0 `[0, 0, 4092]`, + row1 `[0, 64, 4096]` -- i.e. the whole right superblock column, plus 64 deblock-bleed pixels. +- NOT an entropy desync: the 180-TU `all_zero` range ladder is IDENTICAL between our + `EC_TRACE_COEFF` and instrumented `aomdec`'s (`diff a.az o.az` empty over the WHOLE stream), + and blocks decoded AFTER the wrong ones (mi(16,0)) are pixel-exact. +- NOT a clamp: an `EC_DBG_CLAMP` probe on both the dequant clamp + (`+-(1 << (7 + bd))`) and every `clamp_range` in `transform.rs` fired ZERO times on this stream. +- The divergence is in the MV VALUES. `EC_TRACE_MODE` diff, decode-order frame 2: + + aomdec mi(0,32) mode=24 ref0=1 ref1=4 mv0=(128,-16) ours mv0=(-1,2) + aomdec mi(0,40) mode=17 ref0=1 ref1=4 mv0=(128,-16) ours mv0=(-1,2) + aomdec mi(8,32) / mi(8,40) / mi(16,40): same (128,-16) vs (-1,2) + aomdec mi(16,32) mode=24 mv0=(-176,256) ours (-241,264) + aomdec mi(24,32) mode=16 mv0=(-128,56) ours (-193,64) + + The NEAREST_NEARESTMV blocks take stack[0] directly, so libaom's stack[0].this_mv is + `(128,-16)` where ours is `(0,0)`; the NEWMV/NEW_NEWMV blocks are off by exactly the same + predictor delta. +- Our stack for mi(0,32): two entries, `this=(0,0) comp=(0,0) w=2` -- the signature of libaom's + compound EXTENSION pass appending `comp_list[0]` and `comp_list[1]` into an empty stack. +- The temporal field is genuinely empty for that frame and this is CORRECT: a new + `EC_TRACE_TPL` field dump (`EC_TPL_FIELD`) shows oh=1 and oh=2 all-`.` and oh=3 onward all-`#`; + frame 2's LAST projection is suppressed by `is_lst_overlay` and its LAST2 is the key frame. + +### Root cause +`crates/ec-av1/src/mvstack.rs:1482` `combine_compound_candidates` filled the `comp_list` tail +slots (the ones left over after `ref_id` then `ref_diff`) with `(0, 0)`. libaom fills them with +that side's GLOBAL MOTION vector -- `setup_ref_mv_list`, `av1/common/mvref_common.c:729`: + + for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx) + comp_list[comp_idx][idx] = gm_mv_candidates[idx]; + +The zero fill was only right under IDENTITY global motion, which the module's original reduction +assumed; aomenc has `--enable-global-motion` ON by default. A compound block with no usable +neighbour pair (frame top row -> no above, 64x64 INTRA block to its left, empty temporal field) +therefore took a `(0, 0)` predictor where libaom took the frame's translation. + +Class: `parsed then discarded` / reduction-assumption -- global motion is parsed and used by the +single-ref path (`gm_fallback`, mvstack.rs:1099-1100) but was dropped on the compound tail. + +### Fix +`crates/ec-av1/src/mvstack.rs`: `combine_compound_candidates` now takes +`gm_mv_candidates: [(i32,i32); 2]` and uses it as the per-side fill; the call site passes +`[gm_mv(gm, ref_frame.0), gm_mv(gm, ref_frame.1)]`. + +### Sibling sweep (same shape, whole crate) +Every `gm_mv_candidates` use in `mvref_common.c` was walked. The single-reference tail fill +(`mvref_common.c:787-788`, `mv_ref_list[idx] = gm_mv_candidates[0]`) is already ported +(`mvstack.rs:1099-1100` `gm_fallback`); the scan-row/col/corner `is_gm_block` substitutions +(`:92`, `:117`) already take `gm` through `scan_*_compound`; the `add_tpl_ref_mv` +`GLOBALMV_OFFSET` compares (`:379-410`) already use `gm_mv(gm, ...)` on both sides. This was the +only zero-filled site. + +## 4. Other change +`crates/ec-av1/src/decode.rs` `dump_stage16`: the `EC_AV1_PREFILT_DUMP16` dump wrote a fixed +`.f0` every frame, so only the LAST frame survived and a mid-sequence divergence could +not be read out at all. It is now indexed by decode-order picture like `EC_AV1_PREFILT_DUMP`. +`crates/ec-av1/src/motion_field.rs`: `setup_motion_field` prints an `EC_TPL_FIELD` occupancy map +under the existing `EC_TRACE_TPL` rung (this is what proved frame 2's empty temporal field). + +## 5. Refusals +None lifted this round (r1 lifted none either; this lane's work is two silent-miscode fixes). +`refusal_inventory.rs` is byte-identical to main. + +## EVIDENCE +EVIDENCE: $HOME/.cache/sbrect10/b58.obu (sha256 00b9253b...dcd2, hashed twice) | probe.sh half-random geq, 6 arms (8/10-bit x cq 52/58/61), decode + compare vs ffmpeg | pixel diffs 0/0/0/0/0/0 (was 0/32890/39907 on the 10-bit arms), >32 intra-in-inter hits 5/2/3 (8-bit) and 6/7/7 (10-bit) +EVIDENCE: $HOME/.cache/sbrect10/{a.az,o.az} | EC_TRACE_COEFF all_zero ladder, instrumented aomdec vs ours, whole stream | 180 TUs, `diff` empty -> entropy in sync, defect is reconstruction-side +EVIDENCE: $HOME/.cache/sbrect10/{a.mode,o.mode} | EC_TRACE_MODE mv0 diff, decode-order frame 2 | aomdec mi(0,32) mv0=(128,-16), ours (-1,2); 6 blocks diverge, all in the right superblock column +EVIDENCE: $HOME/.cache/sbrect10/o.tplf | EC_TPL_FIELD occupancy dump, all 5 inter frames | oh=1,2 empty / oh=3,4,5 full -> the empty temporal field at oh=2 is correct, not the defect