From 8d520eff03a05c8f4c46114169ac64bdfbb2adf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 03:04:44 +0300 Subject: [PATCH] fix(av1): clamp inter MC reads to the reference's true extent, not its coding stride sequence_round_trips_at_a_non_multiple_size (160x96) failed on inter-frame chroma/luma content: a spec decoder's reference buffer holds exactly FrameWidth x FrameHeight (mi_cols*4 x mi_rows*4) -- a superblock's partition stops early at has_cols/has_rows and never codes syntax for padding columns/rows beyond the true edge, so those samples are never part of the decoded picture. MC's Clip3 (spec 7.11.3.4) must clamp reference reads to that true extent, replicating the true edge, not to this crate's own padded-to-SUPERBLOCK coding surface. `mc::sample`/`mc::predict` used one width/height pair for both the buffer's row stride and the Clip3 clamp bound, conflating the two; split into a `stride` param (the buffer's real row length, unchanged) plus `true_width`/`true_height` (the clamp bound, now `Plane::true_width`/ `true_height` at the call site) threaded through `motion::search` and `encode::mc_trial`. Gate: 126 passed/1 failed/4 ignored -> 127 passed/0 failed/4 ignored. --- crates/ec-av1/src/encode.rs | 40 +++++++++++++++++++++++-- crates/ec-av1/src/mc.rs | 58 ++++++++++++++++++++++++++++--------- crates/ec-av1/src/motion.rs | 14 +++++++-- 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 048abfc0..45376fd3 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1478,6 +1478,7 @@ fn mc_trial( mv: (i32, i32), luma: bool, reference: &[u8], + stride: usize, ref_width: usize, ref_height: usize, skip: bool, @@ -1490,6 +1491,7 @@ fn mc_trial( let mut prediction = vec![0u8; side * side]; mc::predict( reference, + stride, ref_width, ref_height, x_q4, @@ -1614,9 +1616,34 @@ fn search_inter_block( }); } - let ref_luma = (&reference.y, reference.width, reference.height); - let ref_u = (&reference.u, reference.width / 2, reference.height / 2); - let ref_v = (&reference.v, reference.width / 2, reference.height / 2); + // The reference frame buffer a spec decoder holds is exactly the true + // (unpadded) coded extent -- a superblock whose partition stops early at + // `has_cols`/`has_rows` never codes syntax for the padding columns/rows + // beyond it, so those samples are never part of the decoded picture. + // Motion compensation clamps reads to that true extent (spec 7.11.3.4's + // reference sample position clamp), not to this crate's own + // padded-to-`SUPERBLOCK` coding surface -- passing the padded width/height + // here would let a fractional-pel read past the true edge pick up this + // encoder's own uncoded padding instead of the true edge's replicated + // value a decoder computes. + let ref_luma = ( + &reference.y, + reference.width, + luma.true_width, + luma.true_height, + ); + let ref_u = ( + &reference.u, + reference.width / 2, + chroma[0].true_width, + chroma[0].true_height, + ); + let ref_v = ( + &reference.v, + reference.width / 2, + chroma[1].true_width, + chroma[1].true_height, + ); let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV @@ -1636,6 +1663,7 @@ fn search_inter_block( ref_luma.0, ref_luma.1, ref_luma.2, + ref_luma.3, false, search.base_q_idx, search.deadzone, @@ -1651,6 +1679,7 @@ fn search_inter_block( ref_u.0, ref_u.1, ref_u.2, + ref_u.3, false, search.base_q_idx, search.deadzone, @@ -1666,6 +1695,7 @@ fn search_inter_block( ref_v.0, ref_v.1, ref_v.2, + ref_v.3, false, search.base_q_idx, search.deadzone, @@ -1706,6 +1736,7 @@ fn search_inter_block( ref_luma.0, ref_luma.1, ref_luma.2, + ref_luma.3, &source_block, x, y, @@ -1726,6 +1757,7 @@ fn search_inter_block( ref_luma.0, ref_luma.1, ref_luma.2, + ref_luma.3, false, search.base_q_idx, search.deadzone, @@ -1741,6 +1773,7 @@ fn search_inter_block( ref_u.0, ref_u.1, ref_u.2, + ref_u.3, false, search.base_q_idx, search.deadzone, @@ -1756,6 +1789,7 @@ fn search_inter_block( ref_v.0, ref_v.1, ref_v.2, + ref_v.3, false, search.base_q_idx, search.deadzone, diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index a37630f2..dc122f76 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -52,18 +52,33 @@ const SUBPEL_FILTERS: [[i32; 8]; 16] = [ [0, 0, -2, 8, 126, -6, 2, 0], ]; -/// A reference sample at `(x, y)`, clamped to the plane's bounds (spec -/// 7.11.3.4's `Clip3` against the frame's edges) rather than read out of -/// range -- a motion vector that points past the frame repeats the edge -/// sample instead of panicking. -fn sample(reference: &[u8], ref_width: usize, ref_height: usize, x: i32, y: i32) -> i32 { - let cx = x.clamp(0, ref_width as i32 - 1) as usize; - let cy = y.clamp(0, ref_height as i32 - 1) as usize; - i32::from(reference[cy * ref_width + cx]) +/// A reference sample at `(x, y)`, clamped to the frame's true (unpadded) +/// extent -- `true_width`/`true_height`, which can be narrower than +/// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3` +/// against the frame's edges): a motion vector that points past the true +/// edge repeats the true edge's sample instead of panicking or picking up +/// whatever this encoder's own uncoded padding columns/rows hold. +#[allow(clippy::too_many_arguments)] +fn sample( + reference: &[u8], + stride: usize, + true_width: usize, + true_height: usize, + x: i32, + y: i32, +) -> i32 { + let cx = x.clamp(0, true_width as i32 - 1) as usize; + let cy = y.clamp(0, true_height as i32 - 1) as usize; + i32::from(reference[cy * stride + cx]) } /// Predicts one `block_w * block_h` block into `dst`, row-major, from -/// `reference` (row-major, `ref_width * ref_height`, stride `ref_width`). +/// `reference` (row-major, stride `stride`, true content +/// `true_width` x `true_height` -- a reference frame's decoded picture +/// buffer can be wider/taller than the true frame it holds, when this +/// encoder pads a picture to a whole number of superblocks; a spec decoder +/// never codes syntax for those extra columns/rows, so motion compensation +/// must clamp to the true extent, not the buffer's own stride). /// /// `x_q4` / `y_q4` are the block's top-left position in the reference, in /// 1/16-pel units (an integer MV has its low 4 bits zero); this function @@ -76,8 +91,9 @@ fn sample(reference: &[u8], ref_width: usize, ref_height: usize, x: i32, y: i32) #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape pub fn predict( reference: &[u8], - ref_width: usize, - ref_height: usize, + stride: usize, + true_width: usize, + true_height: usize, x_q4: i32, y_q4: i32, block_w: usize, @@ -105,7 +121,7 @@ pub fn predict( let mut sum = 0; for (t, &tap) in h_filter.iter().enumerate() { let x = x0 + c as i32 + t as i32 - 3; - sum += tap * sample(reference, ref_width, ref_height, x, y); + sum += tap * sample(reference, stride, true_width, true_height, x, y); } intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); } @@ -132,7 +148,17 @@ mod tests { let height = 12; let reference: Vec = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); let mut dst = vec![0u8; 6 * 6]; - predict(&reference, width, height, 3 * 16, 4 * 16, 6, 6, &mut dst); + predict( + &reference, + width, + width, + height, + 3 * 16, + 4 * 16, + 6, + 6, + &mut dst, + ); for row in 0..6 { for col in 0..6 { assert_eq!( @@ -157,6 +183,7 @@ mod tests { predict( &reference, width, + width, height, 5 * 16 + 8, 5 * 16, @@ -187,6 +214,7 @@ mod tests { predict( &reference, width, + width, height, 5 * 16 + xfrac, 5 * 16 + yfrac, @@ -219,6 +247,7 @@ mod tests { predict( &reference, width, + width, height, 3 * 16 + 8, 3 * 16, @@ -230,6 +259,7 @@ mod tests { predict( &reference, width, + width, height, 3 * 16, 3 * 16 + 8, @@ -271,6 +301,7 @@ mod tests { predict( &reference, width, + width, height, -50 * 16 + 5, -50 * 16 + 5, @@ -288,6 +319,7 @@ mod tests { predict( &reference, width, + width, height, 50 * 16 + 5, 50 * 16 + 5, diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index a9f4d3e0..9c9f066f 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -191,6 +191,7 @@ pub struct MotionSearch { #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor pub fn search( reference: &[u8], + stride: usize, ref_width: usize, ref_height: usize, source: &[u8], @@ -202,8 +203,8 @@ pub fn search( lambda: f64, ) -> MotionSearch { let (result, _trace) = search_traced( - reference, ref_width, ref_height, source, block_x, block_y, block_w, block_h, pred_mv, - lambda, + reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h, + pred_mv, lambda, ); result } @@ -214,6 +215,7 @@ pub fn search( #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor fn search_traced( reference: &[u8], + stride: usize, ref_width: usize, ref_height: usize, source: &[u8], @@ -226,6 +228,7 @@ fn search_traced( ) -> (MotionSearch, Vec) { search_traced_from_step( reference, + stride, ref_width, ref_height, source, @@ -245,6 +248,7 @@ fn search_traced( #[allow(clippy::too_many_arguments)] fn search_traced_from_step( reference: &[u8], + stride: usize, ref_width: usize, ref_height: usize, source: &[u8], @@ -264,7 +268,7 @@ fn search_traced_from_step( let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; predict( - reference, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst, + reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst, ); let sad: f64 = source .iter() @@ -424,6 +428,7 @@ mod tests { let result = search( &plane, width, + width, height, &block, anchor_x, @@ -470,6 +475,7 @@ mod tests { let result = search( &plane, width, + width, height, &block, anchor_x, @@ -505,6 +511,7 @@ mod tests { let (_result, trace) = search_traced( &plane, width, + width, height, &block, anchor_x, @@ -571,6 +578,7 @@ mod tests { let (result, trace) = search_traced_from_step( &plane, width, + width, height, &block, anchor_x,