Merge branch 'lane-t900' (186f1390): mv clamp window keeps signed frame-edge distances for overhanging blocks; the 10-bit 3840x1608 head decodes 100 frames byte-exact, pinned witness + unit test
Browse files at this commit5 changed files +229 -2
- crates/ec-av1/examples/decode_probe.rs +4 -0
- crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obu +0 -0
- crates/ec-av1/src/mvstack.rs +49 -2
- crates/ec-av1/src/stream.rs +65 -0
- lanes/t900-r11.handoff.md +111 -0
crates/ec-av1/examples/decode_probe.rsmodified+4 -0
| @@ -46,6 +46,10 @@ fn main() { | ||
| 46 | 46 | println!("interintra_rect: {}", ec_av1::decode::interintra_rect_hits()); |
| 47 | 47 | println!("gm_nontrans_small_side: {}", ec_av1::decode::gm_nontrans_small_side_hits()); |
| 48 | 48 | println!("tr_reach_longer_side: {}", ec_av1::decode::tr_reach_longer_side_hits()); |
| 49 | + println!( | |
| 50 | + "mv_clamp_edge_overhang: {}", | |
| 51 | + ec_av1::mvstack::mv_clamp_edge_overhang_hits() | |
| 52 | + ); | |
| 49 | 53 | let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits(); |
| 50 | 54 | println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}"); |
| 51 | 55 | let si = ec_av1::stream::sub8_intra_rect_hits(); |
crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obuadded+0 -0
Binary file changed
crates/ec-av1/src/mvstack.rsmodified+49 -2
| @@ -804,6 +804,19 @@ const MV_BORDER: i32 = 16 << 3; | ||
| 804 | 804 | /// a `mi_cols`-by-`mi_rows` frame: the distance from the block's own edge to |
| 805 | 805 | /// the matching frame edge, plus the block's own span and [`MV_BORDER`] of |
| 806 | 806 | /// slack, in every direction. |
| 807 | +// lane-t900 r11: stack entries whose clamp window is narrowed by a NEGATIVE | |
| 808 | +// `mb_to_right_edge`/`mb_to_bottom_edge` -- i.e. blocks overhanging the frame's | |
| 809 | +// right/bottom edge whose predictor the frame-edge clamp actually moves. Zero | |
| 810 | +// of these before the r11 fix, because the window was computed in `usize`. | |
| 811 | +thread_local! { | |
| 812 | + static MV_CLAMP_EDGE_OVERHANG_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 813 | +} | |
| 814 | + | |
| 815 | +/// Current value of [`MV_CLAMP_EDGE_OVERHANG_HITS`]. | |
| 816 | +pub fn mv_clamp_edge_overhang_hits() -> usize { | |
| 817 | + MV_CLAMP_EDGE_OVERHANG_HITS.with(std::cell::Cell::get) | |
| 818 | +} | |
| 819 | + | |
| 807 | 820 | fn clamp_mv_ref( |
| 808 | 821 | mv: (i32, i32), |
| 809 | 822 | mi_row: usize, |
| @@ -817,9 +830,24 @@ fn clamp_mv_ref( | ||
| 817 | 830 | let to_subpel = |mi_units: usize| (mi_units as i32) * 4 * 8; |
| 818 | 831 | let (bw8, bh8) = (to_subpel(bw4), to_subpel(bh4)); |
| 819 | 832 | let to_left = -to_subpel(mi_col); |
| 820 | - let to_right = to_subpel(mi_cols.saturating_sub(bw4).saturating_sub(mi_col)); | |
| 833 | + // lane-t900 r11: libaom's `mb_to_right_edge`/`mb_to_bottom_edge` | |
| 834 | + // (`set_mi_row_col`, av1_common_int.h) are SIGNED and go NEGATIVE for a | |
| 835 | + // block that overhangs the frame's right/bottom edge -- the block keeps | |
| 836 | + // its full `bw4`/`bh4` there, so `mi_cols - bw4 - mi_col < 0`. Saturating | |
| 837 | + // in `usize` clipped that to 0 and left the clamp window ~2 blocks too | |
| 838 | + // wide, so an edge block's stack entry was never clamped. | |
| 839 | + let signed = |a: usize, b: usize, c: usize| (a as i32 - b as i32 - c as i32) * 4 * 8; | |
| 840 | + let to_right = signed(mi_cols, bw4, mi_col); | |
| 821 | 841 | let to_top = -to_subpel(mi_row); |
| 822 | - let to_bottom = to_subpel(mi_rows.saturating_sub(bh4).saturating_sub(mi_row)); | |
| 842 | + let to_bottom = signed(mi_rows, bh4, mi_row); | |
| 843 | + if to_right < 0 || to_bottom < 0 { | |
| 844 | + let (row, col) = mv; | |
| 845 | + let clamped_row = row.clamp(to_top - bh8 - MV_BORDER, to_bottom + bh8 + MV_BORDER); | |
| 846 | + let clamped_col = col.clamp(to_left - bw8 - MV_BORDER, to_right + bw8 + MV_BORDER); | |
| 847 | + if clamped_row != row || clamped_col != col { | |
| 848 | + MV_CLAMP_EDGE_OVERHANG_HITS.with(|c| c.set(c.get() + 1)); | |
| 849 | + } | |
| 850 | + } | |
| 823 | 851 | |
| 824 | 852 | let (row, col) = mv; |
| 825 | 853 | ( |
| @@ -3182,4 +3210,23 @@ mod tests { | ||
| 3182 | 3210 | assert_eq!(whole_frame, REF_CAT_LEVEL + 2 * 4 + CORNER_WEIGHT); |
| 3183 | 3211 | assert_ne!(tiled, whole_frame); |
| 3184 | 3212 | } |
| 3213 | + | |
| 3214 | + /// lane-t900 r11 regression: the frame-edge clamp window is SIGNED. | |
| 3215 | + /// | |
| 3216 | + /// Numbers taken straight off the cross-decoder ladder on a 10-bit | |
| 3217 | + /// 3840x1608 stream (`mi_cols=960`, `mi_rows=402`): the 64x64 NEWMV block | |
| 3218 | + /// at mi(400,368) overhangs the bottom edge by 14 mi units, so libaom's | |
| 3219 | + /// `mb_to_bottom_edge = (402 - 16 - 400) * 4 * 8 = -448` and the stack | |
| 3220 | + /// entry's row is clamped to `-448 + 512 + 128 = 192`. Computing that in | |
| 3221 | + /// `usize` saturated the term to 0 and let a row of 384 through, which is | |
| 3222 | + /// 24 px of extra downward motion -- the block's MC then read the | |
| 3223 | + /// replicated last row of the reference. | |
| 3224 | + #[test] | |
| 3225 | + fn clamp_mv_ref_window_is_signed_at_the_frame_bottom_edge() { | |
| 3226 | + let clamped = clamp_mv_ref((384, -320), 400, 368, 16, 16, 960, 402); | |
| 3227 | + assert_eq!(clamped, (192, -320)); | |
| 3228 | + // A block fully inside the frame keeps the wide window. | |
| 3229 | + assert_eq!(clamp_mv_ref((384, -320), 100, 368, 16, 16, 960, 402), (384, -320)); | |
| 3230 | + } | |
| 3231 | + | |
| 3185 | 3232 | } |
crates/ec-av1/src/stream.rsmodified+65 -0
| @@ -30615,6 +30615,71 @@ mod tests { | ||
| 30615 | 30615 | } |
| 30616 | 30616 | |
| 30617 | 30617 | |
| 30618 | + /// lane-t900 r11: the frame-edge MV clamp witness. | |
| 30619 | + /// | |
| 30620 | + /// `crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obu` (72158 bytes, | |
| 30621 | + /// sha256 451a9b138995e636f22308847a123b5c8621d4908cd93772cf5bbf2b37df39d7, | |
| 30622 | + /// truncated twice independently to identical hashes) is the first 80 | |
| 30623 | + /// frame-carrying OBUs of a 10-bit 3840x1608 HDR film head -- the SMALLEST | |
| 30624 | + /// such prefix that fires the clamp (N=79 fires 0, N=80 fires 5). 55 | |
| 30625 | + /// decode-order frames, 52 shown, every decode-order frame byte-identical | |
| 30626 | + /// to instrumented aomdec (`EC_AV1_FINAL_DUMP`, see the r11 report). | |
| 30627 | + /// | |
| 30628 | + /// The path it pins: libaom's `mb_to_right_edge` / `mb_to_bottom_edge` | |
| 30629 | + /// (`set_mi_row_col`, av1_common_int.h) are SIGNED, and a block that | |
| 30630 | + /// overhangs the frame's right/bottom edge keeps its full `bw4`/`bh4`, so | |
| 30631 | + /// `mi_cols - bw4 - mi_col` and `mi_rows - bh4 - mi_row` go NEGATIVE there | |
| 30632 | + /// and `clamp_mv_ref` (mvref_common.h) narrows the stack window by that | |
| 30633 | + /// much. This frame height is 1608 -> `mi_rows == 402`, so every 64x64 | |
| 30634 | + /// block on the bottom row overhangs by 14 mi units. Computing that | |
| 30635 | + /// difference in `usize` saturated it to 0 and left the window 448 subpel | |
| 30636 | + /// too wide, so an edge block's predictor was never clamped -- one 64x64 | |
| 30637 | + /// NEWMV block at mi(400,368) took `mv0=(224,568)` where libaom takes | |
| 30638 | + /// `(32,568)`, its MC then read the replicated bottom row instead of real | |
| 30639 | + /// reference rows, and the frame missed by 221 bytes. | |
| 30640 | + #[test] | |
| 30641 | + fn a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel_exact() { | |
| 30642 | + const NAME: &str = "a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel_exact"; | |
| 30643 | + if !have_ffmpeg() { | |
| 30644 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 30645 | + return; | |
| 30646 | + } | |
| 30647 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | |
| 30648 | + .join("fixtures/reelb_head_mvclamp_witness.obu"); | |
| 30649 | + let stream = std::fs::read(&path) | |
| 30650 | + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); | |
| 30651 | + let (width, height) = (3840usize, 1608usize); | |
| 30652 | + let clamp_before = crate::mvstack::mv_clamp_edge_overhang_hits(); | |
| 30653 | + let frames = match decode_stream(&stream) { | |
| 30654 | + Ok(frames) => frames, | |
| 30655 | + Err(e) => panic!("{NAME}: decode_stream refused: {e}"), | |
| 30656 | + }; | |
| 30657 | + let clamped = crate::mvstack::mv_clamp_edge_overhang_hits() - clamp_before; | |
| 30658 | + assert!( | |
| 30659 | + clamped > 0, | |
| 30660 | + "{NAME}: mv_clamp_edge_overhang never fired -- the gate would be vacuous for it" | |
| 30661 | + ); | |
| 30662 | + assert_eq!(frames.len(), 52, "{NAME}: 52 shown frames"); | |
| 30663 | + assert_eq!((frames[0].width, frames[0].height), (width, height)); | |
| 30664 | + let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len()); | |
| 30665 | + assert_eq!( | |
| 30666 | + ffmpeg_frames.len(), | |
| 30667 | + frames.len(), | |
| 30668 | + "{NAME}: ffmpeg returned {} frames, we decoded {}", | |
| 30669 | + ffmpeg_frames.len(), | |
| 30670 | + frames.len() | |
| 30671 | + ); | |
| 30672 | + for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | |
| 30673 | + assert_eq!(ours.y, theirs.y, "{NAME}: frame {i} luma vs ffmpeg"); | |
| 30674 | + assert_eq!(ours.u, theirs.u, "{NAME}: frame {i} U vs ffmpeg"); | |
| 30675 | + assert_eq!(ours.v, theirs.v, "{NAME}: frame {i} V vs ffmpeg"); | |
| 30676 | + } | |
| 30677 | + eprintln!( | |
| 30678 | + "{NAME}: 52 shown frames pixel-exact on every plane; \ | |
| 30679 | + mv_clamp_edge_overhang={clamped}" | |
| 30680 | + ); | |
| 30681 | + } | |
| 30682 | + | |
| 30618 | 30683 | /// lane-sub8x4 r2, the gate that lifts BOTH sub-8x8 intra refusals ("an |
| 30619 | 30684 | /// intra 4x4 block inside an inter frame's sub-8x8 split" and "an intra |
| 30620 | 30685 | /// 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition"). |
lanes/t900-r11.handoff.mdadded+111 -0
| @@ -0,0 +1,111 @@ | ||
| 1 | +# lane-t900 r11 handoff -- target A was NOT loop restoration: it was the mv-stack frame-edge clamp, computed in `usize` | |
| 2 | + | |
| 3 | +Tip: `2b56599b` on `lane-t900` (r10 tip `2aa87a34`, merged to main as `99692ed0`; main | |
| 4 | +fast-forwarded into the lane before any edit). | |
| 5 | + | |
| 6 | +## Target A -- 4K decode frame 88, 221 bytes: ROOT CAUSE FOUND, FIXED, gated | |
| 7 | + | |
| 8 | +**The charter premise (self-guided restoration last stripe) is DEAD.** The stage | |
| 9 | +bisection killed it in two runs: the 221-byte mismatch is already present in the | |
| 10 | +PRE-loop-filter dump, so no filter stage is involved. | |
| 11 | + | |
| 12 | +* post-deblock dump (`EC_AV1_POSTDEBLOCK_DUMP16` both decoders): rows 1594..1605 differ. | |
| 13 | +* pre-filter dump (`EC_AV1_PREFILT_DUMP16` both decoders): Y rows 1600,1601,1602, | |
| 14 | + x 1472..1535 -- exactly one 64x64 block -- plus 4 chroma samples. | |
| 15 | + | |
| 16 | +Ladder: `EC_TRACE_MODE` both decoders at decode frame 88 (ours labels frames with | |
| 17 | +`EC_PICT idx=`, aomdec does not -- locate its lines by matching our `rng`): | |
| 18 | + | |
| 19 | + aomdec EC_MODE mi_row=400 mi_col=368 rng=32962 | |
| 20 | + EC_MODE_MV mode=16 mv0=(32,568) rng=42514 | |
| 21 | + EC_STACK ref=1 i=0 this=(192,-320) w=2 | |
| 22 | + ours EC_MODE mi_row=400 mi_col=368 rng=32962 is_inter=1 skip=1 side=64 | |
| 23 | + EC_STACK ref=1 i=0 this=(384,-320) w=2 | |
| 24 | + EC_MODE_VAL mode=16 mv0=(224,568) rng=42514 | |
| 25 | + | |
| 26 | +Identical range at both rungs -- the entropy stream never diverged, only the | |
| 27 | +reconstructed MV. `EC_TRACE_TPL` on both showed every temporal candidate at that | |
| 28 | +block is `mfmv0=(0,0)`, so the stack entry is not temporal-projection related. | |
| 29 | + | |
| 30 | +Root cause (`crates/ec-av1/src/mvstack.rs:807` `clamp_mv_ref`): libaom's | |
| 31 | +`mb_to_right_edge`/`mb_to_bottom_edge` (`set_mi_row_col`, av1_common_int.h) are | |
| 32 | +SIGNED and a block overhanging the frame's right/bottom edge keeps its full | |
| 33 | +`bw4`/`bh4`. Frame height 1608 -> `mi_rows == 402`, so the 64x64 block at | |
| 34 | +mi_row 400 has `mb_to_bottom_edge = (402-16-400)*4*8 = -448` and the window top is | |
| 35 | +`-448 + 512 + 128 = 192`. We computed that with `usize::saturating_sub` -> 0, window | |
| 36 | +`640`, so 384 passed through unclamped. Full-pel MV, so the block is a straight copy: | |
| 37 | +with row 224 (28 px) every source row was past 1607 and our MC replicated the last | |
| 38 | +reference row; libaom with row 32 (4 px) reads real reference rows 1604..1606 of | |
| 39 | +decode-order frame 83 (verified by patch search: `f88[1600:1603,1472:1536]` occurs | |
| 40 | +exactly once in the reference set, at frame 83 row 1604 col 1543 = mv (4 px, 71 px)). | |
| 41 | + | |
| 42 | +Fix `2b56599b`: both edge terms computed in `i32`; new counter | |
| 43 | +`mvstack::mv_clamp_edge_overhang_hits` (printed by `decode_probe`). | |
| 44 | + | |
| 45 | +Measured effect: | |
| 46 | + | |
| 47 | +| stream | before | after | | |
| 48 | +|---|---|---| | |
| 49 | +| 4K head, 148-OBU / 100-frame prefix (`~/.cache/t900-tmp/hgw_a.obu`) | 1 frame differs (f88, 221 B) | **0 of 100** | | |
| 50 | +| 4K head, full 10 s (`~/.cache/hg-0-10s.obu`, 241 f) | not measured before | 66 differ, first **161**, worst 199 B (f188) | | |
| 51 | +| 1080p @900 s cut (267 f) | 36 differ, first 41, worst 13 B | unchanged (36 / 41 / 13) | | |
| 52 | +| 1080p @5400 s cut (275 f) | 134 differ, first 61, f113=1942 B | unchanged (134 / 61 / f113=1942, worst 2410 @ f244) | | |
| 53 | +| 1080p head 0-10 s (241 f) | 78 differ, first 86 | unchanged (78 / 86, worst 5214 @ f188) | | |
| 54 | +| 1080p 16-frame head cut `t900_h.obu` | exact | exact | | |
| 55 | + | |
| 56 | +Gates (both PASS, `cargo test -p ec-av1 --lib -- frame_edge_mv_clamping clamp_mv_ref_window` | |
| 57 | +-> 2 passed, 0 failed, 179.96 s): | |
| 58 | + | |
| 59 | +* `stream::tests::a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel_exact` -- | |
| 60 | + new fixture `crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obu`, 72158 bytes, sha256 | |
| 61 | + `451a9b138995e636f22308847a123b5c8621d4908cd93772cf5bbf2b37df39d7` (truncated twice, | |
| 62 | + identical hashes), 80 frame-carrying OBUs = 55 decode / 52 shown frames of the 4K head. | |
| 63 | + SMALLEST prefix that fires the counter (N=79 -> 0, N=80 -> 5). All 55 decode-order | |
| 64 | + frames byte-identical to aomdec; the gate compares the 52 shown frames on all planes | |
| 65 | + vs ffmpeg and hard-asserts `mv_clamp_edge_overhang > 0`. | |
| 66 | + CAVEAT, stated plainly: this fixture stops before frame 88, so it is a REGRESSION guard, | |
| 67 | + not a reproducer of the 221-byte defect. The defect witness is the unit test: | |
| 68 | +* `mvstack::tests::clamp_mv_ref_window_is_signed_at_the_frame_bottom_edge` -- the ladder's | |
| 69 | + exact numbers (`clamp_mv_ref((384,-320), 400, 368, 16, 16, 960, 402) == (192,-320)`), | |
| 70 | + which the pre-r11 code fails. | |
| 71 | + | |
| 72 | +No existing fixture in the repo fires the counter (all 12 `crates/ec-av1/fixtures/*.obu` | |
| 73 | +and `fixtures/*.obu` measured at 0), which is why a new one was needed. | |
| 74 | + | |
| 75 | +## Target B -- 1080p bottom-edge chroma MC clamp: NOT fixed, and NOT the same class | |
| 76 | + | |
| 77 | +Unchanged by A (cut_900 still 36 frames, first bad f41 = 1 byte). The r8 localisation | |
| 78 | +still stands (skip 32x32 mi(192,176) mv0=(-4,14) SMOOTH, chroma vertical taps at the | |
| 79 | +last chroma row). Note for the next round: that block DOES overhang (mi_rows=198, | |
| 80 | +bh4=8, 192+8=200), but its MV is far inside the clamp window, so the r11 fix cannot | |
| 81 | +touch it -- the charter's "reference plane replicates from the aligned rather than the | |
| 82 | +cropped height" premise is still UNTESTED. For 1920x792 the chroma crop height (396) | |
| 83 | +and the mi-aligned chroma height (396) are EQUAL, so a crop-vs-aligned difference cannot | |
| 84 | +be the explanation there; measure the actual tap rows instead (`EC_PRED`/MC rung on both | |
| 85 | +decoders at chroma (353,395), frame 41 of `~/.cache/t900-tmp/cut_900.obu`). | |
| 86 | + | |
| 87 | +## Target C -- the KB walls: localised, not diagnosed | |
| 88 | + | |
| 89 | +`@5400` decode frame 113 (1942 B) via `python3 sb.py d_r11_cut_5400 113 64`: | |
| 90 | +**CHROMA ONLY, luma bit-exact.** Deltas +-1..2, concentrated in | |
| 91 | +SB(0,6)..SB(0,8)/SB(1,7)/SB(1,8) -- chroma x 220..260, y 16..32 (luma x 440..520, | |
| 92 | +y 32..64) plus a 11-sample tail at SB(8,13). Every block in that region is INTRA | |
| 93 | +inside an inter frame (`EC_TRACE_MODE`, frame 113: mi(8..16, 112..128) all | |
| 94 | +`is_inter=0 side=16`). So the wall is a chroma intra path (CfL / chroma angular / | |
| 95 | +chroma TX), not MC and not entropy-visible at the mode layer. | |
| 96 | +1080p head f130 (1143 B) and the true worst f188 (5214 B) NOT measured this round. | |
| 97 | + | |
| 98 | +## Exact next steps | |
| 99 | + | |
| 100 | +1. Target C: run `EC_PRED` on BOTH decoders for cut_5400 frame 113 restricted to | |
| 101 | + plane=1/2 at mi(8,112) (ours prints pixel origin x=224 y=16; aomdec prints | |
| 102 | + mi_row/mi_col/plane/row_off/col_off + `ft=`), and compare `sum=`/`row0=`/`col0=`: | |
| 103 | + that separates chroma prediction from chroma residual in one run. | |
| 104 | +2. Target A residue: the 4K frontier moved from frame 88 to **frame 161** of | |
| 105 | + `~/.cache/hg-0-10s.obu` (dumps kept in `~/.cache/t900-tmp/d_r11_reelb10/`); start there | |
| 106 | + with `px4k.py d_r11_reelb10 161 3840 1608`. | |
| 107 | +3. Target B as above. | |
| 108 | + | |
| 109 | +Tooling notes for the successor: `px4k.py <dumpdir> <frame> 3840 1608` (new this round, | |
| 110 | +`~/.cache/t900-tmp/`) is the 4K twin of `px.py`; `~/.cache/reelckf-work/trunc.py <in> <N> | |
| 111 | +<out>` keeps N frame-carrying OBUs; `cmp.sh <name> <obu>` writes `d_<name>/`. |