diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 43eb62c3..af8ee876 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -20277,6 +20277,15 @@ const CDEF_DIRECTIONS: [[(i32, i32); 2]; 8] = [ const CDEF_VERY_LARGE: i32 = 0x4000; const CDEF_PRI_TAPS: [[i32; 2]; 2] = [[4, 2], [3, 3]]; const CDEF_SEC_TAPS: [i32; 2] = [2, 1]; +/// lane-av1422filter: libaom `av1_cdef_filter_fb` (cdef_block.c) remaps the +/// luma direction before filtering CHROMA whenever `xdec != ydec` — the +/// direction grid lives on the subsampled chroma grid, so a direction found +/// on the 8x8 luma block means a different line there. `conv422` applies +/// under 4:2:2 (`xdec` set), `conv440` under its mirror (not expressible in +/// the profiles this decoder accepts, kept for fidelity). 4:2:0 and 4:4:4 +/// (`xdec == ydec`) take no remap, exactly as before this lane. +const CDEF_DIR_CONV422: [usize; 8] = [7, 0, 2, 4, 5, 6, 6, 6]; +const CDEF_DIR_CONV440: [usize; 8] = [1, 2, 2, 2, 3, 4, 6, 0]; fn cdef_msb(x: i32) -> i32 { 31 - x.leading_zeros() as i32 @@ -24802,7 +24811,19 @@ fn cdef_band( let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; - let uv_dir = if t_uv != 0 { dir } else { 0 }; + // lane-av1422filter: the chroma direction is the luma one + // remapped through `conv422`/`conv440` under asymmetric + // subsampling (see [`CDEF_DIR_CONV422`]); identical to the + // raw luma direction for 4:2:0 and 4:4:4. + let uv_dir = if t_uv != 0 { + match (ss_x(fctx), ss_y(fctx)) { + (1, 0) => CDEF_DIR_CONV422[dir], + (0, 1) => CDEF_DIR_CONV440[dir], + _ => dir, + } + } else { + 0 + }; let enable_primary_uv = t_uv != 0; let enable_secondary_uv = uv_sec_strength != 0; if let Some(w) = wrote.as_deref_mut() { @@ -25005,8 +25026,11 @@ fn pipe_run( let j = k - 2; if let Some((sy2, su2, sv2)) = snap.as_mut() { snap_band(sy2.data.to_mut(), y, 0, j * 64, (j + 1) * 64, cfg.cdef_on); - snap_band(su2.data.to_mut(), u, 1, j * 32, (j + 1) * 32, cfg.cdef_on); - snap_band(sv2.data.to_mut(), v, 1, j * 32, (j + 1) * 32, cfg.cdef_on); + // lane-av1422filter: chroma band rows follow the frame's ss_y + // (4:2:2 chroma bands the luma 64-row grid, not 32). + let (c_lo, c_hi) = ((j * 64) >> ss_y(fctx), ((j + 1) * 64) >> ss_y(fctx)); + snap_band(su2.data.to_mut(), u, ss_y(fctx) as u32, c_lo, c_hi, cfg.cdef_on); + snap_band(sv2.data.to_mut(), v, ss_y(fctx) as u32, c_lo, c_hi, cfg.cdef_on); } } if cfg.cdef_on && k >= 3 && k - 3 < nb { @@ -25061,8 +25085,23 @@ fn pipe_run_lr( let (fw, fh) = (cfg.frame_w, cfg.frame_h); let dims = [ (fw, fh, 0u32, y.width, y.data.len()), - (fw.div_ceil(2), fh.div_ceil(2), 1, u.width, u.data.len()), - (fw.div_ceil(2), fh.div_ceil(2), 1, v.width, v.data.len()), + // lane-av1422filter: chroma plane extents and stripe geometry follow + // the frame's own subsampling -- 4:2:2 chroma is full-height with + // luma-grid stripes (only 4:2:0 halves both). + ( + round_ss(fw, ss_x(fctx)), + round_ss(fh, ss_y(fctx)), + ss_y(fctx) as u32, + u.width, + u.data.len(), + ), + ( + round_ss(fw, ss_x(fctx)), + round_ss(fh, ss_y(fctx)), + ss_y(fctx) as u32, + v.width, + v.data.len(), + ), ]; let mut planes: Vec = Vec::new(); for (p, &(pw, ph, ss, stride, len)) in dims.iter().enumerate() { @@ -28043,8 +28082,20 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None && lr.loop_restoration_size[p] != 0 }); - let deblocked = - lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1))); + let deblocked = lr_active.then(|| { + ( + plane_snapshot(&y, 0), + // lane-av1422filter: the bands are the LR stripe-boundary context + // (`RESTORATION_UNIT_OFFSET >> ss_y` above/below each 64-luma-row + // stripe). Chroma stripes are half-height ONLY under 4:2:0; a + // 4:2:2 frame's chroma (ss_y = 0) stripes on the luma grid, so + // the hardcoded `1` saved bands at rows 26/60/92/124 where LR + // reads rows 54..57/118..121 -- every 4:2:2 boundary + // substitution read uninitialised zeros (frame-0 V (48,0)). + plane_snapshot(&u, ss_y(fctx) as u32), + plane_snapshot(&v, ss_y(fctx) as u32), + ) + }); // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter // dump above these bisect WHICH filter stage introduced a mismatch. @@ -42387,7 +42438,15 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( (false, None) => None, } } else { - lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1))) + // lane-av1422filter: chroma band geometry follows the frame's own + // ss_y (see the keyframe path's note) -- 4:2:2 stripes full-height. + lr_active.then(|| { + ( + plane_snapshot(&y, 0), + plane_snapshot(&u, ss_y(fctx) as u32), + plane_snapshot(&v, ss_y(fctx) as u32), + ) + }) }; // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter diff --git a/lanes/av1422filter.report.md b/lanes/av1422filter.report.md new file mode 100644 index 00000000..5886e4f0 --- /dev/null +++ b/lanes/av1422filter.report.md @@ -0,0 +1,109 @@ +# lane-av1-422filter — the 4:2:2 loop-filter chroma class: NAMED and FIXED + +## Outcome + +1. REMEASURED first (pinned fixture `/tmp/t422.obu` sha256 + `d3fa4beeb1309033e4a03f5ae5c431c8fa8386ba1ada088684710423c7938129`, + single-frame cut `/tmp/t422f0.obu` sha256 + `3dd3f768ef553c02316848c2d3184c3765030c0a6fcb95887e4719add9ded3af`, + 256x144 / chroma 128x144, local `EC_AV1_ALLOW_422_PROBE` bypass applied + and reverted byte-identical after): with `EC_AV1_PREFILT_DUMP` / + `EC_AV1_POSTDEBLOCK_DUMP` / `EC_AV1_POSTCDEF_DUMP` / `EC_AV1_FINAL_DUMP` + on both sides, the frame-0 pre-filter AND post-deblock planes are + byte-exact (deblock did identical work both sides: 797/637/727 samples + changed, same set) — the 1236/1263 final chroma diffs are introduced + AFTER deblock. Attribution ladder, ours vs the instrumented aomdec: + + | stage | Y | U | V | + |----------------------|------------|-----------------|-----------------| + | pre-filter | 0 | 0 | 0 | + | post-deblock | 0 | 0 | 0 | + | post-CDEF | 0 | **251** (50,0) | **259** (48,0) | + | final | 0 | **1236→253** | **1263→266** | + + (the pre-fix row shows the parent's numbers; the ticket's first final + sample V (48,0) is literally the first post-CDEF defect sample.) +2. The oracle had NO 8-bit post-CDEF rung (its `EC_AV1_POSTCDEF_DUMP` was + 16-bit only; the earlier `aom/pcd.f0` measurement was actually its + post-deblock output — the rung had to be re-derived to isolate CDEF from + LR). Added an 8-bit `EC_AV1_POSTCDEF_DUMP` twin to the oracle's + `decodeframe.c` (right after the `do_cdef` block, full aligned buffer, + same shape as the POSTDEBLOCK rung) and rebuilt + `$HOME/.cache/aom-oracle/build` aomdec with ninja. The CDEF verdict was + then direct: 251/259 samples, first exactly U (50,0) / V (48,0). +3. DEFECT 1 (the big one) — **the 4:2:2 LR boundary-band snapshot is taken + on a 4:2:0 stripe grid**. `plane_snapshot` copies only the 4-row bands + LR's stripe substitution reads (`stripe_h - offset` per 64 rows); the + chroma calls hardcoded `ss_y = 1`, so for 4:2:2 (chroma stripes on the + luma 64-row grid, offset 8) the bands landed at rows 26/60/92/124 while + LR read rows 54..57/118..121 — every boundary substitution read + uninitialised scratch (zeros): 999 U / 963 V samples filtered where aom + filters nothing, |delta| up to 24 at the top rows. Fixed at all three + sites of the class: the keyframe and inter non-pipelined + `plane_snapshot(&u/&v, ss_y(fctx))`, the pipeline's `snap_band` band rows + `(j*64) >> ss_y`, and `pipe_run_lr`'s chroma `dims` (plane extents + `round_ss`, ss carried, not `div_ceil(2)`+1). 4:2:0 is bit-identical + (ss_y=1 reproduces the old bands/rows/dims exactly). +4. DEFECT 2 — **the CDEF chroma direction remap was missing**. libaom + `av1_cdef_filter_fb` (cdef_block.c) remaps the luma direction through + `conv422 = [7,0,2,4,5,6,6,6]` (in place; V then inherits U's remapped + grid) whenever `xdec != ydec` — the direction grid lives on the + subsampled chroma grid. The port filtered chroma with the raw luma + direction: after the LR fix the residual was exactly the CDEF stage, + 251/259 samples all ±1/±2 (168 we-filtered-aom-doesn't, 71 + aom-filters-we-don't, 12 value diffs on shared samples; scalar and AVX2 + kernels byte-identical, so shared logic, not the SIMD path). Fixed with + `CDEF_DIR_CONV422`/`CDEF_DIR_CONV440` applied for `(ss_x, ss_y)` = + (1,0)/(0,1); 4:2:0 and 4:4:4 take `_ => dir` unchanged. +5. MEASURED after both fixes: **frame 0 of the pinned 4:2:2 fixture is + byte-exact vs the oracle at PREFILT, POSTDEBLOCK, POSTCDEF and FINAL on + Y, U and V** (0 diffs everywhere). Non-vacuity on this frame: + `chroma422_square` = 24, `chroma422_rect` = 96, `chroma422_sub8` = 26 — + the touched 422 arms provably ran. Instrument for the LR half: a python + re-computation of the stripe walk + `lr_src_row` substitution + the + port's own wiener rounding reproduced the oracle's post-LR chroma plane + EXACTLY from the oracle's post-CDEF input (0 mismatches), which pinned + the residual defect to the snapshot input, not the filter math. +6. REMAINING: none for this class. The `read_coeffs_rect` frame-9 scratch + panic (inter frames of the 22-frame stream) is untouched, as chartered. + +## Decision + +- The header refusal STAYS unconditional in `stream.rs` — the probe bypass + was local-only, applied and reverted byte-identical before the commit + (`git diff` after the revert carries only the decode.rs fix); the + refusal-by-name gate re-verified firing. +- `read_coeffs_rect` untouched. The 4:2:0 group-tail SKIP arm untouched. + `sub8_leaf_chroma422` (parent lane's three fixes) untouched. +- The 8-bit `EC_AV1_POSTCDEF_DUMP` rung was added to the ORACLE's source + tree (`$HOME/.cache/aom-oracle/src/av1/decoder/decodeframe.c`, rebuilt + with ninja), not to this repo's `scripts/instrument-aom-oracle.sh` — the + script's generator should grow the same patch if the oracle is ever + rebuilt from scratch. + +## Verification + +- `cargo check -p ec-av1` (target `~/.cache/cargo-target-av1422filter`): + 0 warnings, 0 errors. +- Frame-0 stage dumps, ours vs the instrumented aomdec (cropped + comparisons; our stage dumps are 256x160/128x160 padded, the oracle's + cropped 256x144/128x144 — the comparator crops): all four stages, all + three planes, 0 diffs. +- 420 gates green (all with the fix in place): + `stream::tests::a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_ + whose_tx_split_decodes_pixel_exact` under `EC_AV1_REQUIRE_AOMENC=1` + (1 passed), `stream::tests::a_real_aomenc_stream_with_restoration_reads_ + lr_symbols_correctly` (1 passed — the LR-snapshot regression gate), + `a_superres_key_frame_with_cdef_and_loop_restoration_decodes_pixel_exact` + (1 passed), `a_non_420_subsampled_sequence_header_is_refused_by_name` + (1 passed). +- Scalar-vs-AVX2 CDEF chroma A/B on this frame: 0 diffs between kernels + (the 4x8 chroma shape was already SIMD-faithful; the defect was the + direction input). + +## State + +- Commit on lane-av1-422filter (no push): the LR snapshot ss_y fix (three + sites) + the CDEF conv422 direction remap + this report. +- The instrumented oracle build at `$HOME/.cache/aom-oracle/build` now + carries the 8-bit `EC_AV1_POSTCDEF_DUMP` rung for future lanes.