From e67ae7fa5e449ad0a9c79f1d5104f0a9c1a3cabe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 22:20:00 +0300 Subject: [PATCH] feat(av1): per-reference GLOBAL warp on compound blocks (was silently translational) decode_inter_block's compound branch predicted both taps through mc::predict_compound_intermediate; libaom runs av1_init_warp_params per reference, so each ref of a GLOBAL_GLOBALMV block under a ROTZOOM/AFFINE model is filtered through av1_warp_plane into the compound intermediate. No refusal named this -- silently wrong pixels. warp.rs: warp_affine split into warp_inner + store closure, hardcoded BD=8 replaced by crate::decode::bit_depth(), new warp_affine_compound (av1_highbd_warp_affine_c's is_compound branch, unbiased to match this crate's intermediate domain). mc.rs: diffwtd_mask's round was missing libaom's (bd - 8) term. New gate a_real_compound_global_warp_stream_decodes_pixel_exact: 6/6 pixel-exact, compound_warp_hits=37. 10-bit arm ignored, blocked by a pre-existing 10-bit compound MC defect (ablation in its doc comment). --- crates/ec-av1/src/decode.rs | 130 +++++++++++++++++++++-- crates/ec-av1/src/mc.rs | 10 +- crates/ec-av1/src/stream.rs | 201 ++++++++++++++++++++++++++++++++++++ crates/ec-av1/src/warp.rs | 120 ++++++++++++++++++--- lanes/cwarp-r1.report.md | 98 ++++++++++++++++++ 5 files changed, 536 insertions(+), 23 deletions(-) create mode 100644 lanes/cwarp-r1.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 86c7cf41..f4e898e0 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -638,6 +638,21 @@ pub(crate) fn warp_selected_hits() -> usize { WARP_SELECTED_HITS.with(|c| c.get()) } +// lane-cwarp r1: how many COMPOUND_REFERENCE blocks had at least one +// reference predicted through the per-ref GLOBAL warp (`av1_warp_plane` +// into the compound intermediate) rather than translationally -- the +// gate's proof that a `GLOBAL_GLOBALMV` block under a ROTZOOM/AFFINE model +// really reached [`crate::warp::warp_affine_compound`]. +thread_local! { + static COMPOUND_WARP_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`COMPOUND_WARP_HITS`]. +pub(crate) fn compound_warp_hits() -> usize { + COMPOUND_WARP_HITS.with(|c| c.get()) +} + // How many blocks decoded `interintra == 1` with the non-wedge blended // prediction applied (lane-interintra r1) -- the gate's proof that a // stream actually exercised interintra. @@ -9058,6 +9073,45 @@ fn decode_inter_block( && global_motion[(ref1 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation; let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; + // lane-cwarp r1: `is_global_mv_block` (blockd.h:421-429) is + // PER REFERENCE SLOT -- mode GLOBAL_GLOBALMV, block >= 8x8, and + // THAT slot's own gm model > TRANSLATION. `bw4`/`bh4` come from + // the write extent below, so recompute the size bound from + // `side` here (the block's own size, what libaom's `bsize` + // is). + let is_global_mv0 = is_globalmv + && side >= 8 + && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1; + let is_global_mv1 = is_globalmv + && side >= 8 + && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1; + // libaom `av1_init_warp_params` + `allow_warp`'s + // `global_warp_allowed` branch, run once per reference for a + // compound block exactly as for a single-ref one (reconinter.c + // `build_inter_predictors_8x8_and_bigger` loops over `ref` and + // calls `av1_init_warp_params` inside the loop) -- WARPED_CAUSAL + // never reaches here (spec 5.11.27 / libaom `read_motion_mode`: + // `has_second_ref` returns SIMPLE_TRANSLATION), so the local-warp + // arm of `allow_warp` is dead on this path and only the global + // one applies. `force_integer_mv` short-circuits + // `av1_init_warp_params` before `allow_warp`. + let compound_warp = |ref_frame: i8, eligible: bool| { + if eligible + && !force_integer_mv + && !global_motion[(ref_frame - LAST_FRAME) as usize].invalid + { + crate::warp::global_warp_params( + global_motion[(ref_frame - LAST_FRAME) as usize].params, + ) + } else { + None + } + }; + let warp0 = compound_warp(ref0, is_global_mv0); + let warp1 = compound_warp(ref1, is_global_mv1); + if warp0.is_some() || warp1.is_some() { + COMPOUND_WARP_HITS.with(|c| c.set(c.get() + 1)); + } // spec `get_ref_filter_type`: matches when EITHER of the // neighbour's two references equals this block's own ref0 -- // `above_ref1`/`left_ref1` is the neighbour's real second @@ -9108,12 +9162,8 @@ fn decode_inter_block( // own gm model > TRANSLATION (IDENTITY excluded // too -- the two-predicate trap, distinct from // `gm_nontrans` above which allows IDENTITY). - is_global_mv0: is_globalmv - && bw4.min(bh4) >= 2 - && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1, - is_global_mv1: is_globalmv - && bw4.min(bh4) >= 2 - && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1, + is_global_mv0, + is_global_mv1, }, ); } @@ -9456,6 +9506,16 @@ fn decode_inter_block( v_filter, &mut inter0_y, ); + // lane-cwarp r1: this reference's own GLOBAL warp replaces the + // translational tap (libaom `av1_warp_plane` with + // `conv_params->is_compound`); the blend below is unchanged. + if let Some(wp) = &warp0 { + crate::warp::warp_affine_compound( + wp, &py0.data, py0.true_width as i32, py0.true_height as i32, + py0.width as i32, &mut inter0_y, px as i32, py as i32, side as i32, + side as i32, side as i32, 0, 0, + ); + } let mut inter1_y = vec![0i32; side * side]; mc::predict_compound_intermediate( &py1.data, @@ -9470,6 +9530,16 @@ fn decode_inter_block( v_filter, &mut inter1_y, ); + // lane-cwarp r1: this reference's own GLOBAL warp replaces the + // translational tap (libaom `av1_warp_plane` with + // `conv_params->is_compound`); the blend below is unchanged. + if let Some(wp) = &warp1 { + crate::warp::warp_affine_compound( + wp, &py1.data, py1.true_width as i32, py1.true_height as i32, + py1.width as i32, &mut inter1_y, px as i32, py as i32, side as i32, + side as i32, side as i32, 0, 0, + ); + } let mut pred_y = vec![0u16; side * side]; let mut diffwtd_mask_y = Vec::new(); // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR @@ -9505,6 +9575,18 @@ fn decode_inter_block( v_filter, &mut inter0_u, ); + // `av1_init_warp_params` bails when the PLANE's block is + // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma + // block of an 8x8 luma block stays translational. + if let Some(wp) = &warp0 { + if chroma_side >= 8 { + crate::warp::warp_affine_compound( + wp, &pu0.data, pu0.true_width as i32, pu0.true_height as i32, + pu0.width as i32, &mut inter0_u, cpx as i32, cpy as i32, + chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, + ); + } + } let mut inter1_u = vec![0i32; chroma_side * chroma_side]; mc::predict_compound_intermediate( &pu1.data, @@ -9519,6 +9601,18 @@ fn decode_inter_block( v_filter, &mut inter1_u, ); + // `av1_init_warp_params` bails when the PLANE's block is + // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma + // block of an 8x8 luma block stays translational. + if let Some(wp) = &warp1 { + if chroma_side >= 8 { + crate::warp::warp_affine_compound( + wp, &pu1.data, pu1.true_width as i32, pu1.true_height as i32, + pu1.width as i32, &mut inter1_u, cpx as i32, cpy as i32, + chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, + ); + } + } let mut pred_u = vec![0u16; chroma_side * chroma_side]; if let Some(mask_y) = mask_y { mc::blend_masked_compound( @@ -9549,6 +9643,18 @@ fn decode_inter_block( v_filter, &mut inter0_v, ); + // `av1_init_warp_params` bails when the PLANE's block is + // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma + // block of an 8x8 luma block stays translational. + if let Some(wp) = &warp0 { + if chroma_side >= 8 { + crate::warp::warp_affine_compound( + wp, &pv0.data, pv0.true_width as i32, pv0.true_height as i32, + pv0.width as i32, &mut inter0_v, cpx as i32, cpy as i32, + chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, + ); + } + } let mut inter1_v = vec![0i32; chroma_side * chroma_side]; mc::predict_compound_intermediate( &pv1.data, @@ -9563,6 +9669,18 @@ fn decode_inter_block( v_filter, &mut inter1_v, ); + // `av1_init_warp_params` bails when the PLANE's block is + // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma + // block of an 8x8 luma block stays translational. + if let Some(wp) = &warp1 { + if chroma_side >= 8 { + crate::warp::warp_affine_compound( + wp, &pv1.data, pv1.true_width as i32, pv1.true_height as i32, + pv1.width as i32, &mut inter1_v, cpx as i32, cpy as i32, + chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, + ); + } + } let mut pred_v = vec![0u16; chroma_side * chroma_side]; if let Some(mask_y) = mask_y { mc::blend_masked_compound( diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index 3e927d5a..180e0bff 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -674,7 +674,15 @@ pub fn diffwtd_mask(pred0: &[i32], pred1: &[i32], inv: bool, mask: &mut [u8]) { assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); assert_eq!(mask.len(), pred0.len(), "one mask byte per pixel"); for i in 0..mask.len() { - let diff = round2((pred0[i] - pred1[i]).abs(), INTER_POST_ROUND); + // lane-cwarp r1: libaom's `round` is `2*FILTER_BITS - round_0 - + // round_1 + (bd - 8)` (`diffwtd_mask_d16`, reconinter.c:306) -- the + // `(bd - 8)` term (which this function's own doc comment already + // named) was missing, so every 10-bit DIFFWTD mask was computed 4x + // too coarse. Caught by lane-cwarp's 10-bit gate arm: a 10-bit + // compound stream with global motion AND warp disabled still + // mismatched at frame 1. + let round = INTER_POST_ROUND + u32::from(crate::decode::bit_depth() - 8); + let diff = round2((pred0[i] - pred1[i]).abs(), round); let m = (38 + diff / 16).clamp(0, 64); mask[i] = if inv { (64 - m) as u8 } else { m as u8 }; } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index f1595d1d..a3ae999f 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5960,6 +5960,207 @@ mod tests { ); } + /// lane-cwarp r1: a real `--enable-global-motion=1 --enable-dist-wtd-comp=1` + /// stream over a rotating/zooming source, so a `GLOBAL_GLOBALMV` compound + /// block lands under a ROTZOOM/AFFINE model and each of its two references + /// is predicted through its OWN global warp + /// (`crate::warp::warp_affine_compound`, libaom `av1_warp_plane` with + /// `conv_params->is_compound`) rather than translationally. Before r1 that + /// path was SILENTLY WRONG -- no refusal named it, both taps went through + /// `mc::predict_compound_intermediate`. `compound_warp_hits() > 0` is a + /// hard assert: a run where the encoder never emitted one proves nothing. + /// A decode error or a pixel mismatch is a FAILURE, never a skip. + fn run_compound_global_warp_gate(name: &str, ten_bit: bool) { + let _gate_lock = lock_gate_counters(); + assert!(have_ffmpeg(), "{name}: ffmpeg required"); + assert!(have_aomenc(), "{name}: no aomenc at {}", aomenc_path().display()); + let (width, height, frame_count) = (64usize, 64usize, 24usize); + let before = crate::decode::compound_warp_hits(); + let mut matched = 0u32; + let mut named_refusals = 0u32; + let mut attempts = 0u32; + for cq in [32u32, 45, 55] { + for (i, rate) in [0.010f64, 0.022].iter().enumerate() { + attempts += 1; + let duration = frame_count as f64 / 25.0; + // A rotation about the frame centre plus a slow zoom is what + // aomenc's global-motion search fits a ROTZOOM/AFFINE model to; + // mandelbrot gives it real, non-flat texture to track. + let source = format!( + "mandelbrot=size={w}x{h}:rate=25:start_x={sx}:start_y=-0.4,\ + rotate=a={rate}*n:ow={w}:oh={h}:c=black,scale={width}:{height}", + w = width * 2, + h = height * 2, + sx = -0.6 + 0.01 * (i as f64), + ); + let pix_fmt = if ten_bit { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-t", + &duration.to_string(), "-pix_fmt", pix_fmt, "-strict", "-1", + "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let cq_arg = format!("--cq-level={cq}"); + let mut args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + &cq_arg, + "--cpu-used=0", + "--kf-max-dist=1000", + "--threads=1", + "--row-mt=0", + "--auto-alt-ref=1", + "--lag-in-frames=25", + "--enable-fwd-kf=0", + "--enable-order-hint=1", + // the two tools this gate exists for + "--enable-global-motion=1", + "--enable-dist-wtd-comp=1", + "--enable-warped-motion=1", + "--enable-obmc=1", + "--enable-masked-comp=1", + "--enable-onesided-comp=0", + "--enable-interintra-comp=1", + "--enable-interintra-wedge=0", + "--enable-smooth-interintra=1", + "--tune-content=default", + // partition/intra tools this decoder does not code yet -- + // same exclusion list every compound gate carries. + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", + "--enable-angle-delta=0", + "--enable-tx-size-search=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--max-partition-size=32", + "--min-partition-size=32", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + ]; + if ten_bit { + args.extend_from_slice(&["--input-bit-depth=10", "--bit-depth=10"]); + } + args.extend_from_slice(&["--obu", "-o", "-", "-"]); + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{name} failed outright, not a named refusal (cq {cq}): {msg}" + ); + assert!( + !msg.contains("warp"), + "{name} refused on warp (cq {cq}) -- warp decode is ported: {msg}" + ); + named_refusals += 1; + eprintln!("{name} cq {cq} rate {rate} refusal: {msg}"); + continue; + } + Ok(frames) => frames, + }; + let want = if ten_bit { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; + assert_eq!(frames.len(), frame_count); + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") + && frames + .iter() + .zip(&want) + .any(|(g, w)| g.y != w.y || g.u != w.u || g.v != w.v) + { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (cq {cq}) to {path}"); + } + for (i, (got, want)) in frames.iter().zip(&want).enumerate() { + assert_eq!(got.y, want.y, "{name} frame {i} luma vs ffmpeg (cq {cq})"); + assert_eq!(got.u, want.u, "{name} frame {i} U vs ffmpeg (cq {cq})"); + assert_eq!(got.v, want.v, "{name} frame {i} V vs ffmpeg (cq {cq})"); + } + matched += 1; + } + } + let hits = crate::decode::compound_warp_hits() - before; + eprintln!( + "{name}: {matched}/{attempts} pixel-exact, {named_refusals} named refusals, \ + compound_warp_hits={hits}" + ); + assert!( + hits > 0, + "{name}: no compound block was predicted through a per-ref global warp \ + ({matched} matches, {named_refusals} refusals of {attempts}) -- gate vacuous" + ); + assert!(matched > 0, "{name}: every attempt refused -- no pixel comparison ran"); + } + + /// 8-bit arm of [`run_compound_global_warp_gate`]. + #[test] + fn a_real_compound_global_warp_stream_decodes_pixel_exact() { + run_compound_global_warp_gate("a_real_compound_global_warp_stream_decodes_pixel_exact", false); + } + + /// 10-bit arm of [`run_compound_global_warp_gate`] -- both of his films are + /// `yuv420p10le`, and `warp_affine`'s `bd` was hardcoded 8 before r1. + /// + /// IGNORED, blocked by a PRE-EXISTING 10-bit COMPOUND defect this lane did + /// not introduce and does not own. Ablation run by lane-cwarp r1 on this + /// very recipe: with `--enable-global-motion=0 --enable-warped-motion=0` + /// (no warp of any kind reachable) the 10-bit stream STILL mismatches at + /// frame 1 luma, cq 32; with `--auto-alt-ref=0 --lag-in-frames=0 + /// --enable-dist-wtd-comp=0` (single-reference 10-bit inter only) the same + /// recipe is 6/6 pixel-exact. So: 10-bit single-ref inter is exact, 10-bit + /// compound is not. `mc::diffwtd_mask`'s missing `(bd - 8)` round term was + /// found and fixed while bisecting this, but is NOT the whole defect. + /// Un-ignore the moment the 10-bit compound path is fixed -- the warp + /// wiring under test here is bit-depth generic already. + #[test] + #[ignore = "blocked by a pre-existing 10-bit compound MC defect (see doc comment)"] + fn a_real_compound_global_warp_10bit_stream_decodes_pixel_exact() { + run_compound_global_warp_gate( + "a_real_compound_global_warp_10bit_stream_decodes_pixel_exact", + true, + ); + } + /// lane-maskcomp r2 / lane-wedge r3: `--enable-masked-comp=1` streams /// must consume `compound_type`/`wedge_idx`/`wedge_sign`/`mask_type` /// entropy-exact AND build the real blend, DIFFWTD or WEDGE -- diff --git a/crates/ec-av1/src/warp.rs b/crates/ec-av1/src/warp.rs index 96e7124b..22ea8949 100644 --- a/crates/ec-av1/src/warp.rs +++ b/crates/ec-av1/src/warp.rs @@ -2,11 +2,13 @@ //! `av1/common/warped_motion.c`: estimate a 2x3 affine model from up to //! `LEAST_SQUARES_SAMPLES_MAX` neighbour-block motion samples //! ([`find_projection`]), then filter the reference through it with an 8x8 -//! tiled, two-pass 8-tap warped filter ([`warp_affine`]). 8-bit, -//! non-compound only (this decoder's warp arm only ever reaches a -//! single-ref, non-skip-mode block, spec `is_motion_variation_allowed_bsize` -//! -- `conv_params->is_compound` is always false on that path, so libaom's -//! compound/highbd branches are dropped). +//! tiled, two-pass 8-tap warped filter ([`warp_affine`]). Bit-depth generic +//! (lane-cwarp: `bd` is `crate::decode::bit_depth()`, was a hardcoded 8). +//! `WARPED_CAUSAL` is single-ref by construction (spec 5.11.27: +//! `motion_mode` is only read when `RefFrame[1] <= INTRA_FRAME`), but GLOBAL +//! warp applies per reference on a compound block too -- +//! [`warp_affine_compound`] is that path (libaom `av1_warp_plane` with +//! `conv_params->is_compound`). /// `WARPEDMODEL_PREC_BITS` (`av1/common/mv.h`). const WARPEDMODEL_PREC_BITS: u32 = 16; @@ -419,11 +421,13 @@ fn clip_pixel(v: i32) -> u16 { v.clamp(0, crate::decode::sample_max()) as u16 } -/// `av1_warp_affine_c` (`warped_motion.c`), 8-bit, non-compound only: +/// `av1_highbd_warp_affine_c` (`warped_motion.c`)'s non-compound branch: /// filters `reference` (row-major, `stride`, true extent `width`x`height`) /// through `params`' affine model into `dst` (row-major, `p_stride`), an /// `p_width`x`p_height` block whose top-left sits at `(p_col, p_row)` in the -/// (possibly chroma-subsampled) plane `dst` belongs to. +/// (possibly chroma-subsampled) plane `dst` belongs to. Bit depth comes from +/// the stream (`crate::decode::bit_depth`), the same thread-local +/// [`clip_pixel`] already clamps against -- libaom's `bd` parameter. #[allow(clippy::too_many_arguments)] pub fn warp_affine( params: &WarpParams, @@ -439,6 +443,91 @@ pub fn warp_affine( p_stride: i32, subsampling_x: i32, subsampling_y: i32, +) { + let bd = i32::from(crate::decode::bit_depth()); + // `reduce_bits_vert`, non-compound: `2 * FILTER_BITS - round_0`. + let reduce_bits_vert = 2 * FILTER_BITS - REDUCE_BITS_HORIZ; + warp_inner( + params, reference, width, height, stride, p_col, p_row, p_width, p_height, + subsampling_x, subsampling_y, bd, reduce_bits_vert, + |row, col, sum| { + dst[row * p_stride as usize + col] = + clip_pixel(sum - (1 << (bd - 1)) - (1 << bd)); + }, + ); +} + +/// lane-cwarp: `av1_highbd_warp_affine_c`'s `conv_params->is_compound` +/// branch (`do_average == 0`, i.e. "write the CONV_BUF entry"): identical +/// filter, but the vertical pass rounds by `conv_params->round_1` +/// (`COMPOUND_ROUND1_BITS == 7`, [`mc::predict_compound_intermediate`]'s +/// `INTER_ROUND_1_COMPOUND`) instead of `2*FILTER_BITS - round_0`, and the +/// result stays in the compound intermediate domain rather than becoming a +/// pixel. libaom keeps a constant bias in its CONV_BUF (the +/// `1 << offset_bits_horiz` / `1 << offset_bits_vert` terms, which survive +/// both roundings exactly because both exceed their shift) and subtracts +/// `(1 << (offset_bits - round_1)) + (1 << (offset_bits - round_1 - 1))` +/// at the blend; this crate's intermediates are UNBIASED (see +/// [`mc::diffwtd_mask`]'s derivation), so the identical constant -- +/// `(1 << (bd + 4)) + (1 << (bd + 3))` -- is removed here instead, making +/// the output directly interchangeable with +/// [`mc::predict_compound_intermediate`]'s for +/// [`mc::combine_compound`]/[`mc::blend_masked_compound`]. +#[allow(clippy::too_many_arguments)] +pub fn warp_affine_compound( + params: &WarpParams, + reference: &[u16], + width: i32, + height: i32, + stride: i32, + dst: &mut [i32], + p_col: i32, + p_row: i32, + p_width: i32, + p_height: i32, + p_stride: i32, + subsampling_x: i32, + subsampling_y: i32, +) { + let bd = i32::from(crate::decode::bit_depth()); + let bias = (1 << (bd + 4)) + (1 << (bd + 3)); + warp_inner( + params, reference, width, height, stride, p_col, p_row, p_width, p_height, + subsampling_x, subsampling_y, bd, COMPOUND_ROUND1_BITS, + |row, col, sum| { + dst[row * p_stride as usize + col] = sum - bias; + }, + ); +} + +/// `conv_params->round_0` (`ROUND0_BITS`), the warp filter's horizontal +/// rounding -- `InterRound0`, bit-depth independent for 8/10-bit content +/// (libaom only raises it at `bd == 12`, which this decoder refuses). +const REDUCE_BITS_HORIZ: i32 = 3; +/// `COMPOUND_ROUND1_BITS` (`convolve.h`): `conv_params->round_1` when +/// `is_compound`. +const COMPOUND_ROUND1_BITS: i32 = 7; + +/// The shared body of [`warp_affine`]/[`warp_affine_compound`] +/// (`av1_highbd_warp_affine_c`): everything up to and including the +/// vertical `ROUND_POWER_OF_TWO(sum, reduce_bits_vert)`, with `store` +/// receiving `(row, col, sum)` in block-local coordinates. +#[allow(clippy::too_many_arguments)] +fn warp_inner( + params: &WarpParams, + reference: &[u16], + width: i32, + height: i32, + stride: i32, + p_col: i32, + p_row: i32, + p_width: i32, + p_height: i32, + subsampling_x: i32, + subsampling_y: i32, + bd: i32, + reduce_bits_vert: i32, + mut store: impl FnMut(usize, usize, i32), ) { let mat = ¶ms.wmmat; let (alpha, beta, gamma, delta) = ( @@ -447,11 +536,9 @@ pub fn warp_affine( params.gamma as i64, params.delta as i64, ); - const BD: i32 = 8; - let reduce_bits_horiz = 3; // conv_params->round_0, non-compound 8-bit (InterRound0) - let reduce_bits_vert = 2 * FILTER_BITS - reduce_bits_horiz; // InterRound1 - let offset_bits_horiz = BD + FILTER_BITS - 1; - let offset_bits_vert = BD + 2 * FILTER_BITS - reduce_bits_horiz; + let reduce_bits_horiz = REDUCE_BITS_HORIZ; + let offset_bits_horiz = bd + FILTER_BITS - 1; + let offset_bits_vert = bd + 2 * FILTER_BITS - reduce_bits_horiz; let mut i = p_row; while i < p_row + p_height { @@ -509,10 +596,11 @@ pub fn warp_affine( sum += tmp[(k + m + 4) as usize][(l + 4) as usize] * coeffs[m as usize] as i32; } sum = round2(sum as i64, reduce_bits_vert as u32) as i32; - let out_row = (i - p_row + k + 4) as usize; - let out_col = (j - p_col + l + 4) as usize; - dst[out_row * p_stride as usize + out_col] = - clip_pixel(sum - (1 << (BD - 1)) - (1 << BD)); + store( + (i - p_row + k + 4) as usize, + (j - p_col + l + 4) as usize, + sum, + ); sy += gamma; l += 1; } diff --git a/lanes/cwarp-r1.report.md b/lanes/cwarp-r1.report.md new file mode 100644 index 00000000..c3618587 --- /dev/null +++ b/lanes/cwarp-r1.report.md @@ -0,0 +1,98 @@ +# lane-cwarp r1 -- per-reference GLOBAL warp on compound blocks + +VERDICT: DONE for the 8-bit path (new hard gate green, 37 real compound-warp +blocks). 10-bit arm is written but `#[ignore]`d, blocked by a PRE-EXISTING +10-bit *compound* MC defect this lane isolated but does not own. + +## Defect (charter premise re-measured, held) +`decode_inter_block`'s COMPOUND_REFERENCE branch predicted BOTH taps +translationally through `mc::predict_compound_intermediate`. libaom runs +`av1_init_warp_params` inside its per-reference loop +(`reconinter.c build_inter_predictors_8x8_and_bigger`), so each reference of a +`GLOBAL_GLOBALMV` block whose own gm model is > TRANSLATION is filtered through +`av1_warp_plane` into the compound intermediate. No refusal named this: the +pixels were silently wrong. Confirmed live -- the new gate hits 37 such blocks +in 6 real aomenc streams. + +`WARPED_CAUSAL` on a compound block: confirmed impossible, left alone. +libaom `decodemv.c read_motion_mode` -> `motion_mode_allowed` returns +SIMPLE_TRANSLATION for `has_second_ref(mbmi)`; spec 5.11.27 the same. So only +the GLOBAL arm of `allow_warp` is live on the compound path. + +## Changed +- `crates/ec-av1/src/warp.rs` + - `warp_affine` split into a shared `warp_inner` + a store closure; the old + hardcoded `const BD: i32 = 8` is now `crate::decode::bit_depth()` (the same + thread-local `clip_pixel`/`sample_max` already use, so no call site moved). + `REDUCE_BITS_HORIZ`/`COMPOUND_ROUND1_BITS` named from libaom's + `conv_params->round_0`/`COMPOUND_ROUND1_BITS`. + NOTE for lane-hbdinter: this is the whole of the warp bit-depth change -- + one `bd` local and two offset expressions, no signature change. + - new `warp_affine_compound`: `av1_highbd_warp_affine_c`'s + `conv_params->is_compound && !do_average` branch -- vertical pass rounds by + `round_1 == 7` and the output stays in the compound intermediate domain. + libaom's CONV_BUF carries a constant bias that its blend subtracts + (`(1 << (offset_bits - round_1)) + (1 << (offset_bits - round_1 - 1))`); + this crate's intermediates are unbiased, so exactly that constant -- + `(1 << (bd+4)) + (1 << (bd+3))` -- is removed here, making the output + interchangeable with `mc::predict_compound_intermediate`'s for + `combine_compound`/`blend_masked_compound`. Both offset terms survive their + roundings exactly (each exceeds its shift), so bias removal is bit-exact, + not an approximation. +- `crates/ec-av1/src/decode.rs` + - compound branch: `is_global_mv0`/`is_global_mv1` hoisted out of the MiGrid + stamp (and their size bound corrected from `bw4.min(bh4) >= 2`, which used + the frame-CLIPPED write extent, to `side >= 8`, libaom's unclipped `bsize` + -- differs only for a block whose visible extent is 4px); + `compound_warp(ref, eligible)` builds each ref's `global_warp_params` + (skipped on `force_integer_mv` or `gm.invalid`, mirroring + `av1_init_warp_params` + `allow_warp`), and each of the six + `predict_compound_intermediate` outputs is replaced by + `warp_affine_compound` when that ref warps. Chroma is gated on + `chroma_side >= 8` -- `av1_init_warp_params` returns early on + `block_width < 8`, and that is the PLANE's block, so an 8x8 luma block's + 4x4 chroma stays translational. + - new `COMPOUND_WARP_HITS` counter + `compound_warp_hits()`. +- `crates/ec-av1/src/mc.rs` -- `diffwtd_mask`'s round is + `2*FILTER_BITS - round_0 - round_1 + (bd - 8)` (libaom `diffwtd_mask_d16`, + reconinter.c:306); the `(bd - 8)` term was missing (this function's own doc + comment already named it), so every 10-bit DIFFWTD mask was 4x too coarse. + Found while bisecting the 10-bit failure. 8-bit unaffected (`bd-8 == 0`). + UNGATED: the only 10-bit compound gate is the ignored one below. +- `crates/ec-av1/src/stream.rs` -- `run_compound_global_warp_gate` + the two + gates below. No refusal string existed for this defect, so none is lifted; + `refusal_inventory.rs`/`gate_coverage.rs` needed no change (both green). + +## Gate +`a_real_compound_global_warp_stream_decodes_pixel_exact` (8-bit): rotating +mandelbrot (`rotate=a=RATE*n` over a 2x source, scaled to 64x64), 24 frames, +`--enable-global-motion=1 --enable-dist-wtd-comp=1 --lag-in-frames=25 +--auto-alt-ref=1 --enable-warped-motion=1 --enable-masked-comp=1`, 3 cq x 2 +rotation rates. Hard-asserts `compound_warp_hits() > 0` and `matched > 0`; a +decode error or a pixel mismatch is a panic, never a skip. + +EVIDENCE: cargo test -p ec-av1 --release --lib -- a_real_compound_global_warp --nocapture (EC_AV1_REQUIRE_AOMENC=1) | 6 real aomenc encodes, every display frame's Y/U/V vs ffmpeg | `6/6 pixel-exact, 0 named refusals, compound_warp_hits=37` + +EVIDENCE: same command with `--enable-global-motion=0 --enable-warped-motion=0` and, separately, `--auto-alt-ref=0 --lag-in-frames=0 --enable-dist-wtd-comp=0` (10-bit arm) | 2 ablation runs | warp-free 10-bit compound FAILS at frame 1 luma cq 32; 10-bit single-ref inter is 6/6 pixel-exact -> the residual is 10-bit COMPOUND, not warp + +EVIDENCE: cargo test -p ec-av1 --release --lib (EC_AV1_REQUIRE_AOMENC=1) | whole ec-av1 lib suite | **271 passed, 0 failed, 23 ignored**, 312s + +Hidden frames: the gate encodes with `--auto-alt-ref=1 --lag-in-frames=25`, so +hidden alt-refs exist and every shown frame that references them is compared; +a wrong hidden-frame reconstruction propagates into those. There is no +decode-order oracle compare here (the repo's `EC_AV1_DECODE_ORDER_DUMP` rung is +8-bit-only and manual) -- same standard as the other compound gates. + +## Residue +- deferred(the 10-bit compound MC defect): `a_real_compound_global_warp_10bit_stream_decodes_pixel_exact` + is `#[ignore]`d with the ablation in its doc comment. Unblocked by whoever + owns 10-bit compound MC; un-ignore it then -- the warp path under it is + already bit-depth generic. +- flagged (same defect shape, NOT fixed): `decode_inter_block8`'s compound + branch (decode.rs ~10969-11072 on this branch) has the identical + translational-only compound prediction. For an 8x8 leaf the luma block is + exactly 8x8, so libaom WOULD warp luma there (chroma 4x4 would not). Not + wired this round: the 8x8 leaf's own gates are the subject of lane-gmaffine + and a change here would land untested. +- accepted: `mc::diffwtd_mask`'s `(bd - 8)` fix is source-verified against + libaom but has no green 10-bit gate behind it yet (see above).