Compound MV stack zero-filled the comp_list tail libaom fills with global motion
Browse files at this commite2010eff05090e11496989e2c4cfe8655d6e87e7Parent: d03e23e- fix
- compound MV stack zero-filled the comp_list tail libaom fills with global motion
5 changed files +174 -7
- crates/ec-av1/src/decode.rs +6 -1
- crates/ec-av1/src/motion_field.rs +9 -0
- crates/ec-av1/src/mvstack.rs +20 -4
- crates/ec-av1/src/stream.rs +22 -2
- lanes/sbrect10-r2.report.md +117 -0
crates/ec-av1/src/decode.rsmodified+6 -1
| @@ -8753,8 +8753,13 @@ fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { | ||
| 8753 | 8753 | /// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). |
| 8754 | 8754 | fn dump_stage16(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf, fw: usize, fh: usize) { |
| 8755 | 8755 | use std::io::Write; |
| 8756 | + // lane-sbrect10 r2: index the dump by decode-order picture like | |
| 8757 | + // EC_AV1_PREFILT_DUMP already does -- a fixed `.f0` name kept only the | |
| 8758 | + // LAST frame, so a mid-sequence divergence could not be read out at all. | |
| 8759 | + static STAGE16_IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 8756 | 8760 | if let Ok(path) = std::env::var(var) |
| 8757 | - && let Ok(mut f) = std::fs::File::create(format!("{path}.f0")) | |
| 8761 | + && let idx = STAGE16_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst) | |
| 8762 | + && let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) | |
| 8758 | 8763 | { |
| 8759 | 8764 | 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))] { |
| 8760 | 8765 | for row in 0..h { |
crates/ec-av1/src/motion_field.rsmodified+9 -0
| @@ -442,6 +442,15 @@ pub fn setup_motion_field( | ||
| 442 | 442 | ); |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | + if std::env::var_os("EC_TRACE_TPL").is_some() { | |
| 446 | + eprintln!("EC_TPL_FIELD oh={cur_order_hint} rows={rows} cols={cols}"); | |
| 447 | + for r in 0..rows { | |
| 448 | + let row: String = (0..cols) | |
| 449 | + .map(|c| if cells[r * cols + c].is_some() { '#' } else { '.' }) | |
| 450 | + .collect(); | |
| 451 | + eprintln!("EC_TPL_FIELD r{r} {row}"); | |
| 452 | + } | |
| 453 | + } | |
| 445 | 454 | TplField { cols, rows, cells } |
| 446 | 455 | } |
| 447 | 456 |
crates/ec-av1/src/mvstack.rsmodified+20 -4
| @@ -1479,7 +1479,20 @@ fn process_compound_ref_mv_candidate( | ||
| 1479 | 1479 | /// sign-flipped) entries, position-wise and independently per side, into up |
| 1480 | 1480 | /// to two full `(mv0, mv1)` pair candidates — missing slots default to |
| 1481 | 1481 | /// `(0, 0)`, matching libaom's zero-initialized `combined_mvs`. |
| 1482 | -fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, i32)); 2] { | |
| 1482 | +fn combine_compound_candidates( | |
| 1483 | + lists: &CompoundRefLists, | |
| 1484 | + gm_mv_candidates: [(i32, i32); 2], | |
| 1485 | +) -> [((i32, i32), (i32, i32)); 2] { | |
| 1486 | + // lane-sbrect10 r2: the tail slots libaom leaves after `ref_id` and | |
| 1487 | + // `ref_diff` are filled with that side's GLOBAL MOTION vector, never with | |
| 1488 | + // zero (`setup_ref_mv_list`, mvref_common.c:723-729: | |
| 1489 | + // `for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx) | |
| 1490 | + // comp_list[comp_idx][idx] = gm_mv_candidates[idx];`). | |
| 1491 | + // Zero-filling was right only under IDENTITY global motion, which the | |
| 1492 | + // module doc's original reduction assumed; aomenc turns global motion on | |
| 1493 | + // by default, so a compound block with no usable neighbour pair took a | |
| 1494 | + // (0, 0) predictor where libaom took the frame's translation -- silently | |
| 1495 | + // wrong pixels with the entropy stream still in sync. | |
| 1483 | 1496 | let side_slots = |side: usize| -> [(i32, i32); 2] { |
| 1484 | 1497 | let merged: Vec<(i32, i32)> = lists.ref_id[side] |
| 1485 | 1498 | .iter() |
| @@ -1487,7 +1500,7 @@ fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, | ||
| 1487 | 1500 | .take(2) |
| 1488 | 1501 | .copied() |
| 1489 | 1502 | .collect(); |
| 1490 | - std::array::from_fn(|i| merged.get(i).copied().unwrap_or((0, 0))) | |
| 1503 | + std::array::from_fn(|i| merged.get(i).copied().unwrap_or(gm_mv_candidates[side])) | |
| 1491 | 1504 | }; |
| 1492 | 1505 | let side0 = side_slots(0); |
| 1493 | 1506 | let side1 = side_slots(1); |
| @@ -1802,7 +1815,10 @@ pub fn find_mv_stack_compound( | ||
| 1802 | 1815 | // bump) — otherwise append `comp_list[0]` as a brand-new entry. When |
| 1803 | 1816 | // the stack is empty, both `comp_list[0]` and `comp_list[1]` are |
| 1804 | 1817 | // appended unconditionally, with no dedup check at all. |
| 1805 | - let comp_list = combine_compound_candidates(&lists); | |
| 1818 | + let comp_list = combine_compound_candidates( | |
| 1819 | + &lists, | |
| 1820 | + [gm_mv(gm, ref_frame.0), gm_mv(gm, ref_frame.1)], | |
| 1821 | + ); | |
| 1806 | 1822 | if candidates.len() == 1 { |
| 1807 | 1823 | let (mv0, mv1) = if comp_list[0] == (candidates[0].mv0, candidates[0].mv1) { |
| 1808 | 1824 | comp_list[1] |
| @@ -2824,7 +2840,7 @@ mod tests { | ||
| 2824 | 2840 | // side 1 has only one entry -> comp_idx 1 falls back to ref_diff. |
| 2825 | 2841 | lists.ref_diff[1].push((4, 4)); |
| 2826 | 2842 | |
| 2827 | - let combined = combine_compound_candidates(&lists); | |
| 2843 | + let combined = combine_compound_candidates(&lists, [(0, 0), (0, 0)]); | |
| 2828 | 2844 | assert_eq!(combined[0], ((1, 1), (3, 3))); |
| 2829 | 2845 | assert_eq!(combined[1], ((2, 2), (4, 4))); |
| 2830 | 2846 | } |
crates/ec-av1/src/stream.rsmodified+22 -2
| @@ -6586,10 +6586,20 @@ mod tests { | ||
| 6586 | 6586 | let vertical_split = attempt % 2 == 1; |
| 6587 | 6587 | let cq = [58, 52, 45, 61][(attempt / 2 % 4) as usize]; |
| 6588 | 6588 | let sp = 8 + (attempt / 8) * 4; |
| 6589 | + // Half ramp, half deterministic pseudo-noise: the flat "200" | |
| 6590 | + // half of r1's source made aomenc code the right-hand | |
| 6591 | + // superblocks skip, so at 8 bit no >32 intra block ever | |
| 6592 | + // appeared (probed cq 45/52/58/61/63 = 0 hits). The textured | |
| 6593 | + // half forces intra above 32x32 in an inter frame at BOTH | |
| 6594 | + // depths, so both arms carry the shape. | |
| 6589 | 6595 | let geq = if vertical_split { |
| 6590 | - format!("if(lt(Y,64), 40+mod(floor((X+N*{sp})/32)*90,200), 200)") | |
| 6596 | + format!( | |
| 6597 | + "if(lt(Y,64), 40+mod(floor((X+N*{sp})/32)*90,200), mod((X*7+Y*13+N*97)*31,256))" | |
| 6598 | + ) | |
| 6591 | 6599 | } else { |
| 6592 | - format!("if(lt(X,64), 40+mod(floor((Y+N*{sp})/32)*90,200), 200)") | |
| 6600 | + format!( | |
| 6601 | + "if(lt(X,128), 40+mod(floor((Y+N*{sp})/32)*90,200), mod((X*7+Y*13+N*97)*31,256))" | |
| 6602 | + ) | |
| 6593 | 6603 | }; |
| 6594 | 6604 | let duration = frame_count as f64 / 25.0; |
| 6595 | 6605 | let source = format!( |
| @@ -6739,6 +6749,16 @@ mod tests { | ||
| 6739 | 6749 | /// The counter `nocfl_uv_mode_hits` proves the shape fired in EVERY |
| 6740 | 6750 | /// compared attempt, and every decode-order frame is compared against |
| 6741 | 6751 | /// ffmpeg -- an attempt that mismatches fails, it never SKIPs. |
| 6752 | + /// | |
| 6753 | + /// lane-sbrect10 r2: with the half-random source below this gate also | |
| 6754 | + /// covers a SECOND defect it exposed at 10-bit cq 58/61 -- the compound | |
| 6755 | + /// MV stack's extension pass zero-filled the `comp_list` tail slots | |
| 6756 | + /// libaom fills with each side's GLOBAL MOTION vector | |
| 6757 | + /// (`combine_compound_candidates`, mvref_common.c:723-729). A compound | |
| 6758 | + /// block with no usable neighbour pair (frame top row, intra block to | |
| 6759 | + /// its left, empty temporal field) then took a `(0, 0)` predictor | |
| 6760 | + /// instead of the frame's translation: 32890/39907 wrong luma samples | |
| 6761 | + /// on decode-order frame 2 with the entropy stream still in sync. | |
| 6742 | 6762 | #[test] |
| 6743 | 6763 | fn a_real_aomenc_inter_frame_with_a_64x64_intra_block_reads_the_no_cfl_uv_alphabet() { |
| 6744 | 6764 | const NAME: &str = |
lanes/sbrect10-r2.report.mdadded+117 -0
| @@ -0,0 +1,117 @@ | ||
| 1 | +# lane-sbrect10 r2 report | |
| 2 | + | |
| 3 | +Branch `lane-sbrect10`, worktree `/home/user/Documents/Code/Rust/edith_codecs-sbrect10`. | |
| 4 | +Base: r1 tip 6fba6a6 + `main` 18bf7dc (sb128 5ea5ec8 + r14 a2e2e29) merged. | |
| 5 | + | |
| 6 | +## 1. Merge (step 1) | |
| 7 | +`git merge --no-commit main` -> clean auto-merge (decode.rs, stream.rs), commit `3a9e83e`. | |
| 8 | +- Diffstat 12 files, +1392/-147 (all of it main's; the lane contributed nothing to those files | |
| 9 | + beyond its own r1 hunks). | |
| 10 | +- `git diff main -- crates/ec-av1/src/cdf.rs` and `... refusal_inventory.rs` are BOTH EMPTY, so | |
| 11 | + the NZ_MAP / FILTER_INTRA tables and the refusal list are byte-identical to main -- nothing to | |
| 12 | + reconcile. | |
| 13 | +- The only lines the merged tree removes relative to main are the lane's own 4 r1 CFL lines in | |
| 14 | + `decode.rs` (`cdfs.uv_mode_cfl[mode]` + its `cfl_alphas` arm) and the r1 comment/`[8u32]` arm in | |
| 15 | + `stream.rs`. Main's deletions are all kept. | |
| 16 | + | |
| 17 | +## 2. Gate source swapped to the half-random fixture (step 2) | |
| 18 | +`crates/ec-av1/src/stream.rs` (gate | |
| 19 | +`a_real_aomenc_inter_frame_with_a_64x64_intra_block_reads_the_no_cfl_uv_alphabet`): the flat | |
| 20 | +`200` half of r1's source made aomenc code the right-hand superblocks skip, so at 8 bit no | |
| 21 | +`>32` intra-in-inter block ever appeared. Both orientations now carry a deterministic | |
| 22 | +pseudo-noise half: | |
| 23 | + | |
| 24 | + horizontal: if(lt(X,128), 40+mod(floor((Y+N*sp)/32)*90,200), mod((X*7+Y*13+N*97)*31,256)) | |
| 25 | + vertical: if(lt(Y,64), 40+mod(floor((X+N*sp)/32)*90,200), mod((X*7+Y*13+N*97)*31,256)) | |
| 26 | + | |
| 27 | +Probe grid, `$HOME/.cache/sbrect10/probe.sh` (counts `>32` intra-in-inter blocks via | |
| 28 | +`EC_AV1_TELL`, and pixel diffs vs `ffmpeg -pix_fmt yuv420p10le`/`yuv420p`), AFTER the r2 fix: | |
| 29 | + | |
| 30 | +| depth | cq 52 | cq 58 | cq 61 | | |
| 31 | +|---|---|---|---| | |
| 32 | +| 8-bit | 5 hits, 0 diffs | 2 hits, 0 diffs | 3 hits, 0 diffs | | |
| 33 | +| 10-bit | 6 hits, 0 diffs | 7 hits, 0 diffs | 7 hits, 0 diffs | | |
| 34 | + | |
| 35 | +Both depth arms therefore carry >=1 no-CFL intra hit per compared attempt; per-attempt counters | |
| 36 | +are read on decoded+compared attempts only, every decode-order frame is compared on Y/U/V, and a | |
| 37 | +mismatch fails (never SKIPs). | |
| 38 | + | |
| 39 | +## 3. The SECOND defect this fixture exposed -- root cause and fix (step 3) | |
| 40 | + | |
| 41 | +### Pinned stream | |
| 42 | +`$HOME/.cache/sbrect10/b58.obu` (192x128, 6 frames, 10-bit, cq 58, `--sb-size=64 | |
| 43 | +--min-partition-size=32 --max-partition-size=64 --enable-rect-partitions=1 | |
| 44 | +--enable-1to4-partitions=1 --lag-in-frames=0 --cpu-used=0`), sha256 hashed twice, identical: | |
| 45 | +`00b9253b308201118e27f348c67932a0006e3077ba7a9a401854c2ee7eb1dcd2`. | |
| 46 | + | |
| 47 | +### Localisation | |
| 48 | +- First mismatching frame: DECODE-ORDER 2 (display == decode order), plane Y only, 8252 samples; | |
| 49 | + U and V exact; frames 0 and 1 exact. Per-64x64-SB wrong-pixel map: row0 `[0, 0, 4092]`, | |
| 50 | + row1 `[0, 64, 4096]` -- i.e. the whole right superblock column, plus 64 deblock-bleed pixels. | |
| 51 | +- NOT an entropy desync: the 180-TU `all_zero` range ladder is IDENTICAL between our | |
| 52 | + `EC_TRACE_COEFF` and instrumented `aomdec`'s (`diff a.az o.az` empty over the WHOLE stream), | |
| 53 | + and blocks decoded AFTER the wrong ones (mi(16,0)) are pixel-exact. | |
| 54 | +- NOT a clamp: an `EC_DBG_CLAMP` probe on both the dequant clamp | |
| 55 | + (`+-(1 << (7 + bd))`) and every `clamp_range` in `transform.rs` fired ZERO times on this stream. | |
| 56 | +- The divergence is in the MV VALUES. `EC_TRACE_MODE` diff, decode-order frame 2: | |
| 57 | + | |
| 58 | + aomdec mi(0,32) mode=24 ref0=1 ref1=4 mv0=(128,-16) ours mv0=(-1,2) | |
| 59 | + aomdec mi(0,40) mode=17 ref0=1 ref1=4 mv0=(128,-16) ours mv0=(-1,2) | |
| 60 | + aomdec mi(8,32) / mi(8,40) / mi(16,40): same (128,-16) vs (-1,2) | |
| 61 | + aomdec mi(16,32) mode=24 mv0=(-176,256) ours (-241,264) | |
| 62 | + aomdec mi(24,32) mode=16 mv0=(-128,56) ours (-193,64) | |
| 63 | + | |
| 64 | + The NEAREST_NEARESTMV blocks take stack[0] directly, so libaom's stack[0].this_mv is | |
| 65 | + `(128,-16)` where ours is `(0,0)`; the NEWMV/NEW_NEWMV blocks are off by exactly the same | |
| 66 | + predictor delta. | |
| 67 | +- Our stack for mi(0,32): two entries, `this=(0,0) comp=(0,0) w=2` -- the signature of libaom's | |
| 68 | + compound EXTENSION pass appending `comp_list[0]` and `comp_list[1]` into an empty stack. | |
| 69 | +- The temporal field is genuinely empty for that frame and this is CORRECT: a new | |
| 70 | + `EC_TRACE_TPL` field dump (`EC_TPL_FIELD`) shows oh=1 and oh=2 all-`.` and oh=3 onward all-`#`; | |
| 71 | + frame 2's LAST projection is suppressed by `is_lst_overlay` and its LAST2 is the key frame. | |
| 72 | + | |
| 73 | +### Root cause | |
| 74 | +`crates/ec-av1/src/mvstack.rs:1482` `combine_compound_candidates` filled the `comp_list` tail | |
| 75 | +slots (the ones left over after `ref_id` then `ref_diff`) with `(0, 0)`. libaom fills them with | |
| 76 | +that side's GLOBAL MOTION vector -- `setup_ref_mv_list`, `av1/common/mvref_common.c:729`: | |
| 77 | + | |
| 78 | + for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx) | |
| 79 | + comp_list[comp_idx][idx] = gm_mv_candidates[idx]; | |
| 80 | + | |
| 81 | +The zero fill was only right under IDENTITY global motion, which the module's original reduction | |
| 82 | +assumed; aomenc has `--enable-global-motion` ON by default. A compound block with no usable | |
| 83 | +neighbour pair (frame top row -> no above, 64x64 INTRA block to its left, empty temporal field) | |
| 84 | +therefore took a `(0, 0)` predictor where libaom took the frame's translation. | |
| 85 | + | |
| 86 | +Class: `parsed then discarded` / reduction-assumption -- global motion is parsed and used by the | |
| 87 | +single-ref path (`gm_fallback`, mvstack.rs:1099-1100) but was dropped on the compound tail. | |
| 88 | + | |
| 89 | +### Fix | |
| 90 | +`crates/ec-av1/src/mvstack.rs`: `combine_compound_candidates` now takes | |
| 91 | +`gm_mv_candidates: [(i32,i32); 2]` and uses it as the per-side fill; the call site passes | |
| 92 | +`[gm_mv(gm, ref_frame.0), gm_mv(gm, ref_frame.1)]`. | |
| 93 | + | |
| 94 | +### Sibling sweep (same shape, whole crate) | |
| 95 | +Every `gm_mv_candidates` use in `mvref_common.c` was walked. The single-reference tail fill | |
| 96 | +(`mvref_common.c:787-788`, `mv_ref_list[idx] = gm_mv_candidates[0]`) is already ported | |
| 97 | +(`mvstack.rs:1099-1100` `gm_fallback`); the scan-row/col/corner `is_gm_block` substitutions | |
| 98 | +(`:92`, `:117`) already take `gm` through `scan_*_compound`; the `add_tpl_ref_mv` | |
| 99 | +`GLOBALMV_OFFSET` compares (`:379-410`) already use `gm_mv(gm, ...)` on both sides. This was the | |
| 100 | +only zero-filled site. | |
| 101 | + | |
| 102 | +## 4. Other change | |
| 103 | +`crates/ec-av1/src/decode.rs` `dump_stage16`: the `EC_AV1_PREFILT_DUMP16` dump wrote a fixed | |
| 104 | +`<prefix>.f0` every frame, so only the LAST frame survived and a mid-sequence divergence could | |
| 105 | +not be read out at all. It is now indexed by decode-order picture like `EC_AV1_PREFILT_DUMP`. | |
| 106 | +`crates/ec-av1/src/motion_field.rs`: `setup_motion_field` prints an `EC_TPL_FIELD` occupancy map | |
| 107 | +under the existing `EC_TRACE_TPL` rung (this is what proved frame 2's empty temporal field). | |
| 108 | + | |
| 109 | +## 5. Refusals | |
| 110 | +None lifted this round (r1 lifted none either; this lane's work is two silent-miscode fixes). | |
| 111 | +`refusal_inventory.rs` is byte-identical to main. | |
| 112 | + | |
| 113 | +## EVIDENCE | |
| 114 | +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) | |
| 115 | +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 | |
| 116 | +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 | |
| 117 | +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 |