From b6c54c5d4613a09b235c18b1fc6b11e3f8062643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 09:31:33 +0300 Subject: [PATCH] fix(av1): gate the mv-stack top-right probe on has_top_right; 128-root motion_mode/obmc CDF rows; TX_64X64 coded corner on the intra-in-inter path Three root causes behind the 128x128 inter gate's frame-5 reconstruction residue, all on a 256x256 8/10-bit stream with use_128x128_superblock: 1. mvstack.rs find_mv_stack{,_compound} called scan_top_right unconditionally. libaom setup_ref_mv_list (mvref_common.c:497/546) gates it on has_top_right, which is 0 for bs > 16 -- a 128x128 block's above-right cell is populated (previous SB row) but unavailable, so we invented a candidate and desynced the drl symbols. 2. decode.rs mapped a square block's motion_mode/obmc CDF row with trailing_zeros - 3, sending 128 to our packed BLOCK_16X32 row. Added libaom default_motion_mode_cdf / default_obmc_cdf rows 13/14/15 (64X128, 128X64, 128X128) and explicit match arms ahead of the square one. 3. A TX_64X64 luma unit on the intra-in-inter-frame path used the full 64x64 scan and tx_side; only the top-left 32x32 is coded (spec 5.11.40), which panicked the inverse transform on the 10-bit arm. All 9 decode-order frames of the 8-bit and 10-bit gate arms are now pixel-exact (EC_AV1_PREFILT_DUMP, was 19550/14164/26667/26716 wrong bytes on f5..f8); the mode-value ladder matches aomdec exactly. The tx-size-search arm is still red on 4 post-deblock chroma pixels -- a phantom chroma TU edge published by the per-vartx-leaf lf-grid write, analysed in lanes/sb128c-r5.handoff.md. --- crates/ec-av1/src/cdf.rs | 16 +++++- crates/ec-av1/src/cdf_state.rs | 4 +- crates/ec-av1/src/decode.rs | 13 ++++- crates/ec-av1/src/mvstack.rs | 14 ++++- lanes/sb128c-r5.handoff.md | 102 +++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 lanes/sb128c-r5.handoff.md diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index a21cbdfc..5191fe6d 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -1645,7 +1645,7 @@ pub const SKIP_MODE: [[u16; 3]; 3] = [[32621, 32768, 0], [20708, 32768, 0], [812 /// `motion_mode_allowed` (spec 5.11.24's `read_motion_mode`) offers /// `OBMC_CAUSAL` rather than `WARPED_CAUSAL` -- lane-motionmode round 1 /// never reaches the three-way `motion_mode_cdf`, see `decode.rs`. -pub const OBMC: [[u16; 3]; 14] = [ +pub const OBMC: [[u16; 3]; 17] = [ [10437, 32768, 0], [17432, 32768, 0], [25817, 32768, 0], @@ -1669,6 +1669,11 @@ pub const OBMC: [[u16; 3]; 14] = [ // BLOCK_8X16 (4) and BLOCK_16X8 (5) of libaom's `default_obmc_cdf`. [9371, 32768, 0], [9301, 32768, 0], + // lane-sb128c r5: the 128-root rows -- BLOCK_64X128 (13), BLOCK_128X64 + // (14), BLOCK_128X128 (15) of libaom's `default_obmc_cdf`. + [31014, 32768, 0], + [31560, 32768, 0], + [32638, 32768, 0], ]; /// `default_motion_mode_cdf` (spec 9.4, `entropymode.c`), the square-bsize @@ -1678,7 +1683,7 @@ pub const OBMC: [[u16; 3]; 14] = [ /// `read_motion_mode` reads instead of [`OBMC`] whenever `motion_mode_allowed` /// (spec 5.11.24) resolves to `WARPED_CAUSAL`-eligible (`num_proj_ref >= 1` /// under `allow_warped_motion`) -- lane-warp round 1. -pub const MOTION_MODE: [[u16; 4]; 14] = [ +pub const MOTION_MODE: [[u16; 4]; 17] = [ [7651, 24760, 32768, 0], [19419, 26810, 32768, 0], [26260, 29116, 32768, 0], @@ -1698,6 +1703,13 @@ pub const MOTION_MODE: [[u16; 4]; 14] = [ // `default_motion_mode_cdf` indices 4 and 5). [4738, 24765, 32768, 0], [5391, 25528, 32768, 0], + // lane-sb128c r5: BLOCK_64X128 / 128X64 / 128X128 rows (libaom + // `default_motion_mode_cdf` indices 13, 14, 15). Without them a 128x128 + // inter block fell through `w == h => trailing_zeros - 3` to row 4 (the + // BLOCK_16X32 row) and narrowed the msac by the wrong interval. + [28898, 30397, 32768, 0], + [30878, 31335, 32768, 0], + [32507, 32558, 32768, 0], ]; /// `default_interintra_cdf` (spec 9.4): `interintra`, indexed by diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 89334535..cbdf2b36 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -522,13 +522,13 @@ pub(crate) struct Cdfs { /// `BLOCK_8X8`, 1 = `BLOCK_16X16`, 2 = `BLOCK_32X32`, 3 = `BLOCK_64X64` /// (`default_obmc_cdf`'s own `BLOCK_SIZES_ALL` indices 3/6/9/12 -- /// this decoder only ever codes those four square sizes). - pub obmc: [[u16; 3]; 14], + pub obmc: [[u16; 3]; 17], /// `motion_mode` (spec 5.11.24's `read_motion_mode`), the 3-symbol /// `SIMPLE_TRANSLATION`/`OBMC_CAUSAL`/`WARPED_CAUSAL` alphabet read /// instead of [`Self::obmc`] whenever `motion_mode_allowed` resolves to /// `WARPED_CAUSAL`-eligible -- same square-bsize indexing as `obmc`. /// lane-warp round 1. - pub motion_mode: [[u16; 4]; 14], + pub motion_mode: [[u16; 4]; 17], /// `interintra`, indexed by `size_group_lookup[bsize]` -- see /// `cdf::INTERINTRA`'s own doc. pub interintra: [[u16; 3]; 4], diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b4c14851..8e36a077 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -17968,6 +17968,11 @@ fn decode_inter_block( // interval narrowing, not a wrong value (class // wrong-alphabet-same-value). let bsize_idx = match (write_w, write_h) { + // lane-sb128c r5: the 128 root's own rows come FIRST -- + // the square arm below maps 128 to row 4 (BLOCK_16X32). + (64, 128) => 14, + (128, 64) => 15, + (128, 128) => 16, (w, h) if w == h => (w.trailing_zeros() - 3) as usize, (16, 32) => 4, (32, 16) => 5, @@ -18985,7 +18990,11 @@ fn decode_inter_block( dec, cdfs, txbset_for(tx_px, reduced), - &default_scan(tx_px), + // lane-sb128c r5: a TX_64X64 unit codes only its + // top-left 32x32 of frequencies (spec 5.11.40), so + // the scan and `tx_side` are the CODED corner while + // `side` stays the transform's true 64. + &default_scan(tx_px.min(32)), 0, neighbours.around_mi(tu_mi, tx_px)[0], mode, @@ -18996,7 +19005,7 @@ fn decode_inter_block( tu_px, tu_py, tx_px, - tx_px, + tx_px.min(32), base_q_idx, None, filter_intra, diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index cb4e42e9..77d590a2 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -875,7 +875,12 @@ pub fn find_mv_stack_with_sign_bias( &mut newmv_count, &mut processed_cols, ); - let found_top_right = scan_top_right( + // libaom `setup_ref_mv_list` gates this probe on `has_top_right` + // (mvref_common.c:497/546): the diagonally-above-right unit of a 128x128 + // block sits in decoded memory but is NOT available (bs > 16), so an + // ungated grid probe invents a candidate and desyncs the drl symbols. + let found_top_right = has_top_right(mi_row, mi_col, bw4, bh4) + && scan_top_right( grid, mi_row, mi_col, @@ -1668,7 +1673,12 @@ pub fn find_mv_stack_compound( &mut newmv_count, &mut processed_cols, ); - let found_top_right = scan_top_right_compound( + // libaom `setup_ref_mv_list` gates this probe on `has_top_right` + // (mvref_common.c:497/546): the diagonally-above-right unit of a 128x128 + // block sits in decoded memory but is NOT available (bs > 16), so an + // ungated grid probe invents a candidate and desyncs the drl symbols. + let found_top_right = has_top_right(mi_row, mi_col, bw4, bh4) + && scan_top_right_compound( grid, mi_row, mi_col, diff --git a/lanes/sb128c-r5.handoff.md b/lanes/sb128c-r5.handoff.md new file mode 100644 index 00000000..c284dcaf --- /dev/null +++ b/lanes/sb128c-r5.handoff.md @@ -0,0 +1,102 @@ +# lane-sb128c r5 -> r6 handoff + +Worktree `edith_codecs-sb128c`, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-sb128c`. +Work stream: `$HOME/.cache/sb128c-work/g-61-8-0.obu` (arm 1) and `a3.obu` (arm 3, +copied out of the gate's pinned `/tmp` dir -- keep it, /tmp is reaped). + +## (1) The r4 residue: THREE root causes, all fixed, arms 1+2 GREEN + +r4's "frame 5 pre-filter wrong, entropy in sync" premise was WRONG: the mode-value +ladder (`EC_TRACE_MODE`, values only -- our `EC_MODE_VAL` prints its `rng` at +libaom's `EC_MODE_MV` point, so RANGE is not comparable line-for-line) diverges at +mode-value line 43, `mi(32,0)`, a 128x128 NEWMV block: + +``` +oracle EC_MODE_VAL mi_row=32 mi_col=0 mode=16 ref0=1 mv0=(63,0) stack=1 +ours EC_MODE_VAL mi_row=32 mi_col=0 mode=16 ref0=1 mv0=(0,0) stack=2 +``` + +a) **mv stack: the top-right probe was never gated on `has_top_right`.** +`crates/ec-av1/src/mvstack.rs:882` (`find_mv_stack`) and `:1680` +(`find_mv_stack_compound`) called `scan_top_right{,_compound}` unconditionally. +libaom `setup_ref_mv_list` (mvref_common.c:497 + :546) computes +`has_tr = has_top_right(cm, xd, mi_row, mi_col, AOMMAX(width,height))` and only +then `scan_blk_mbmi(-1, xd->width)`. `has_top_right` returns 0 for `bs > 16`, so a +128x128 block has NO top-right candidate -- but the cell diagonally above-right of +`mi(32,0)` (= `mi(31,32)`, in the previous SB row) IS populated, so our grid probe +invented `(441,441) w=644` and the extra stack entry desynced the drl symbols. +The module's own `mvstack::has_top_right` port already existed and was correct; it +just was not wired into the mv-stack scan (only into the warp-sample gather). + +b) **`motion_mode`/`obmc` CDF row 4 for a 128x128 block.** +`decode.rs:17977` mapped square sizes with `w == h => (w.trailing_zeros() - 3)`, +which sends 128 to row 4 -- our packed `BLOCK_16X32` row. Wrong alphabet narrowing, +no wrong value (class `wrong-alphabet-same-value`). Added libaom +`default_motion_mode_cdf` / `default_obmc_cdf` rows 13/14/15 (BLOCK_64X128, +128X64, 128X128) as our packed indices 14/15/16 in `cdf.rs` (arrays 14 -> 17, +`cdf_state.rs` types too) and explicit `(64,128)/(128,64)/(128,128)` match arms +BEFORE the square arm. + +c) **A TX_64X64 luma unit of an INTRA-in-inter 128 block used the full 64x64 scan.** +`decode.rs:18995` passed `default_scan(tx_px)` and `tx_side = tx_px`; a TX_64X64 +codes only its top-left 32x32 (spec 5.11.40). Panicked +`transform.rs:828 assertion left==right: one coefficient per position, 1024 vs 4096` +on gate arm 2. Now `.min(32)` on both, matching every other 64-capable site +(`coeff_tx_side`, `default_scan(tx_px.min(32))` at :17429/:18494). + +EVIDENCE: `$HOME/.cache/sb128c-work/pf-{o,m}.f0..f8` | `EC_AV1_PREFILT_DUMP` on +aomdec oracle + `decode_probe`, g-61-8-0.obu | all 9 decode-order frames 0 bytes +differ (was 19550/14164/26667/26716 on f5..f8), mode-value ladder MODE_IDENTICAL. + +Gate `a_real_aomenc_inter_128x128_none_root_decodes_pixel_exact`: +- arm 1 (yuv420p, tx_search=0, seed 61): **9/9 frames exact**, `inter_sb128_none_hits=14 chroma_units=16 intra_128_in_inter=0` +- arm 2 (yuv420p10le, tx_search=0, seed 61): **9/9 frames exact**, `hits=29 chroma_units=28 intra_128_in_inter=1` +- arm 3 (yuv420p, tx_search=1, seed 62): **RED**, 7 bytes, see below. +`a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact` and +`a_real_aomenc_128x128_superblock_stream_decodes_pixel_exact` stay green. + +## (2) EXACT NEXT STEP: arm 3, a phantom CHROMA deblock edge (4 px), stage-bisected + +Stream `$HOME/.cache/sb128c-work/a3.obu` (909 B, 256x256 8-bit, 9 decode-order +frames). Mode ladder is IDENTICAL to the oracle -- no entropy defect. + +Stage bisection (oracle rungs `EC_AV1_PREFILT_DUMP` / `EC_AV1_POSTDEBLOCK_DUMP` / +`EC_AV1_FINAL_DUMP`; NOTE our POSTDEBLOCK dump indexes 0,2,4,... so oracle `fN` +pairs with ours `f2N`, and on THIS stream it really is pre-CDEF): +``` +pre-filter f0..f8 : 0 0 0 0 7 6 6 6 0 (f4+ is propagation) +post-deblock f3 : 4 px <-- the defect +final f3 : 7 px, f4..f7 6 px each +``` +The 4 post-deblock pixels are V-plane only: +`V(46,56) V(46,57) V(47,54) V(47,55)`, ours 170 vs oracle 167 -- two pairs +straddling a VERTICAL chroma edge at chroma x=56 (luma x=112), inside the +`mi(0,0)` `side=128 is_inter=1 skip=0` block of decode-order frame 3. + +Root cause (identified, NOT yet fixed): `decode.rs:19303`, the per-vartx-leaf +`fill_lf_grid_rect` loop in `decode_inter_block`, passes the LUMA leaf extent, and +`fill_lf_grid_rect` derives `uv_tx_w/h` from its own `w_mi/h_mi` (`decode.rs:4211`). +So a 16x16 luma leaf publishes a chroma TU extent of 8 and creates chroma edges +every 8 px -- e.g. at chroma 56. libaom takes a chroma plane's transform size from +`av1_get_max_uv_txsize(mbmi->bsize, ...)` (the BLOCK's, = TX_32X32 for a 128 root) +in `set_one_param_for_line_chroma`, never from the luma var-tx leaf. Fix shape: +leave the chroma half of the grid at the BLOCK's uv extent when writing per-leaf +(a leaf-only luma write, or an explicit uv re-publish after the loop, mirroring +r4's `suppress_internal_lf_edges`). Beware r4's trap: the block-level uv `.min(32)` +cap must stay (an INTRA 128 block's internal chroma edges ARE filtered). +Re-run the key-frame 128 sibling gate after touching this (class +`fix-trades-sibling-gate`). + +## (3) NOT STARTED + +deferred: 128-level HORZ/VERT + AB partitions (key and inter) -- r5's budget went +to the three r4-residue root causes; unblocked by nothing. Refusal +`refusal_inventory.rs:45` "a 128x128 superblock HORZ/VERT or AB partition (only +SPLIT and NONE are decoded at the 128 root)" is untouched. `cdf::MOTION_MODE`/ +`cdf::OBMC` now already carry the BLOCK_64X128 / BLOCK_128X64 rows that work needs. +deferred: full suite -- not run this round (r4's was also incomplete); r6 must run +`systemd-run --user --unit=sb128c-suite-$(date +%s) -p MemoryMax=10G --same-dir bash -lc '...'` +per COMMON, log to `$HOME/.cache/sb128c-suite-r6.log`. +deferred: ReelC re-probe -- not re-run; r4 measured both extracts still stopping at +the 128 HORZ/VERT/AB refusal with `EC_AV1_FINAL_DUMP` count 1, byte-identical to +ffmpeg. Nothing in r5 touches that stop string.