forge
Explore
Log in

Don't you dare go Hollow

Clamp inter MC reads to the reference's true extent, not its coding stride

Browse files at this commit

8d520eff03a05c8f4c46114169ac64bdfbb2adf9Ahmet Kaan GümüşParent: 68e9a25
  • fix
    • clamp inter MC reads to the reference's true extent, not its coding stride
Show full message
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.rsmodified+37 -3
@@ -1478,6 +1478,7 @@ fn mc_trial(
14781478 mv: (i32, i32),
14791479 luma: bool,
14801480 reference: &[u8],
1481+ stride: usize,
14811482 ref_width: usize,
14821483 ref_height: usize,
14831484 skip: bool,
@@ -1490,6 +1491,7 @@ fn mc_trial(
14901491 let mut prediction = vec![0u8; side * side];
14911492 mc::predict(
14921493 reference,
1494+ stride,
14931495 ref_width,
14941496 ref_height,
14951497 x_q4,
@@ -1614,9 +1616,34 @@ fn search_inter_block(
16141616 });
16151617 }
16161618
1617- let ref_luma = (&reference.y, reference.width, reference.height);
1618- let ref_u = (&reference.u, reference.width / 2, reference.height / 2);
1619- let ref_v = (&reference.v, reference.width / 2, reference.height / 2);
1619+ // The reference frame buffer a spec decoder holds is exactly the true
1620+ // (unpadded) coded extent -- a superblock whose partition stops early at
1621+ // `has_cols`/`has_rows` never codes syntax for the padding columns/rows
1622+ // beyond it, so those samples are never part of the decoded picture.
1623+ // Motion compensation clamps reads to that true extent (spec 7.11.3.4's
1624+ // reference sample position clamp), not to this crate's own
1625+ // padded-to-`SUPERBLOCK` coding surface -- passing the padded width/height
1626+ // here would let a fractional-pel read past the true edge pick up this
1627+ // encoder's own uncoded padding instead of the true edge's replicated
1628+ // value a decoder computes.
1629+ let ref_luma = (
1630+ &reference.y,
1631+ reference.width,
1632+ luma.true_width,
1633+ luma.true_height,
1634+ );
1635+ let ref_u = (
1636+ &reference.u,
1637+ reference.width / 2,
1638+ chroma[0].true_width,
1639+ chroma[0].true_height,
1640+ );
1641+ let ref_v = (
1642+ &reference.v,
1643+ reference.width / 2,
1644+ chroma[1].true_width,
1645+ chroma[1].true_height,
1646+ );
16201647 let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV
16211648 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero
16221649 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV
@@ -1636,6 +1663,7 @@ fn search_inter_block(
16361663 ref_luma.0,
16371664 ref_luma.1,
16381665 ref_luma.2,
1666+ ref_luma.3,
16391667 false,
16401668 search.base_q_idx,
16411669 search.deadzone,
@@ -1651,6 +1679,7 @@ fn search_inter_block(
16511679 ref_u.0,
16521680 ref_u.1,
16531681 ref_u.2,
1682+ ref_u.3,
16541683 false,
16551684 search.base_q_idx,
16561685 search.deadzone,
@@ -1666,6 +1695,7 @@ fn search_inter_block(
16661695 ref_v.0,
16671696 ref_v.1,
16681697 ref_v.2,
1698+ ref_v.3,
16691699 false,
16701700 search.base_q_idx,
16711701 search.deadzone,
@@ -1706,6 +1736,7 @@ fn search_inter_block(
17061736 ref_luma.0,
17071737 ref_luma.1,
17081738 ref_luma.2,
1739+ ref_luma.3,
17091740 &source_block,
17101741 x,
17111742 y,
@@ -1726,6 +1757,7 @@ fn search_inter_block(
17261757 ref_luma.0,
17271758 ref_luma.1,
17281759 ref_luma.2,
1760+ ref_luma.3,
17291761 false,
17301762 search.base_q_idx,
17311763 search.deadzone,
@@ -1741,6 +1773,7 @@ fn search_inter_block(
17411773 ref_u.0,
17421774 ref_u.1,
17431775 ref_u.2,
1776+ ref_u.3,
17441777 false,
17451778 search.base_q_idx,
17461779 search.deadzone,
@@ -1756,6 +1789,7 @@ fn search_inter_block(
17561789 ref_v.0,
17571790 ref_v.1,
17581791 ref_v.2,
1792+ ref_v.3,
17591793 false,
17601794 search.base_q_idx,
17611795 search.deadzone,
crates/ec-av1/src/mc.rsmodified+45 -13
@@ -52,18 +52,33 @@ const SUBPEL_FILTERS: [[i32; 8]; 16] = [
5252 [0, 0, -2, 8, 126, -6, 2, 0],
5353 ];
5454
55-/// A reference sample at `(x, y)`, clamped to the plane's bounds (spec
56-/// 7.11.3.4's `Clip3` against the frame's edges) rather than read out of
57-/// range -- a motion vector that points past the frame repeats the edge
58-/// sample instead of panicking.
59-fn sample(reference: &[u8], ref_width: usize, ref_height: usize, x: i32, y: i32) -> i32 {
60- let cx = x.clamp(0, ref_width as i32 - 1) as usize;
61- let cy = y.clamp(0, ref_height as i32 - 1) as usize;
62- i32::from(reference[cy * ref_width + cx])
55+/// A reference sample at `(x, y)`, clamped to the frame's true (unpadded)
56+/// extent -- `true_width`/`true_height`, which can be narrower than
57+/// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3`
58+/// against the frame's edges): a motion vector that points past the true
59+/// edge repeats the true edge's sample instead of panicking or picking up
60+/// whatever this encoder's own uncoded padding columns/rows hold.
61+#[allow(clippy::too_many_arguments)]
62+fn sample(
63+ reference: &[u8],
64+ stride: usize,
65+ true_width: usize,
66+ true_height: usize,
67+ x: i32,
68+ y: i32,
69+) -> i32 {
70+ let cx = x.clamp(0, true_width as i32 - 1) as usize;
71+ let cy = y.clamp(0, true_height as i32 - 1) as usize;
72+ i32::from(reference[cy * stride + cx])
6373 }
6474
6575 /// Predicts one `block_w * block_h` block into `dst`, row-major, from
66-/// `reference` (row-major, `ref_width * ref_height`, stride `ref_width`).
76+/// `reference` (row-major, stride `stride`, true content
77+/// `true_width` x `true_height` -- a reference frame's decoded picture
78+/// buffer can be wider/taller than the true frame it holds, when this
79+/// encoder pads a picture to a whole number of superblocks; a spec decoder
80+/// never codes syntax for those extra columns/rows, so motion compensation
81+/// must clamp to the true extent, not the buffer's own stride).
6782 ///
6883 /// `x_q4` / `y_q4` are the block's top-left position in the reference, in
6984 /// 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)
7691 #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape
7792 pub fn predict(
7893 reference: &[u8],
79- ref_width: usize,
80- ref_height: usize,
94+ stride: usize,
95+ true_width: usize,
96+ true_height: usize,
8197 x_q4: i32,
8298 y_q4: i32,
8399 block_w: usize,
@@ -105,7 +121,7 @@ pub fn predict(
105121 let mut sum = 0;
106122 for (t, &tap) in h_filter.iter().enumerate() {
107123 let x = x0 + c as i32 + t as i32 - 3;
108- sum += tap * sample(reference, ref_width, ref_height, x, y);
124+ sum += tap * sample(reference, stride, true_width, true_height, x, y);
109125 }
110126 intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0);
111127 }
@@ -132,7 +148,17 @@ mod tests {
132148 let height = 12;
133149 let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect();
134150 let mut dst = vec![0u8; 6 * 6];
135- predict(&reference, width, height, 3 * 16, 4 * 16, 6, 6, &mut dst);
151+ predict(
152+ &reference,
153+ width,
154+ width,
155+ height,
156+ 3 * 16,
157+ 4 * 16,
158+ 6,
159+ 6,
160+ &mut dst,
161+ );
136162 for row in 0..6 {
137163 for col in 0..6 {
138164 assert_eq!(
@@ -157,6 +183,7 @@ mod tests {
157183 predict(
158184 &reference,
159185 width,
186+ width,
160187 height,
161188 5 * 16 + 8,
162189 5 * 16,
@@ -187,6 +214,7 @@ mod tests {
187214 predict(
188215 &reference,
189216 width,
217+ width,
190218 height,
191219 5 * 16 + xfrac,
192220 5 * 16 + yfrac,
@@ -219,6 +247,7 @@ mod tests {
219247 predict(
220248 &reference,
221249 width,
250+ width,
222251 height,
223252 3 * 16 + 8,
224253 3 * 16,
@@ -230,6 +259,7 @@ mod tests {
230259 predict(
231260 &reference,
232261 width,
262+ width,
233263 height,
234264 3 * 16,
235265 3 * 16 + 8,
@@ -271,6 +301,7 @@ mod tests {
271301 predict(
272302 &reference,
273303 width,
304+ width,
274305 height,
275306 -50 * 16 + 5,
276307 -50 * 16 + 5,
@@ -288,6 +319,7 @@ mod tests {
288319 predict(
289320 &reference,
290321 width,
322+ width,
291323 height,
292324 50 * 16 + 5,
293325 50 * 16 + 5,
crates/ec-av1/src/motion.rsmodified+11 -3
@@ -191,6 +191,7 @@ pub struct MotionSearch {
191191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
192192 pub fn search(
193193 reference: &[u8],
194+ stride: usize,
194195 ref_width: usize,
195196 ref_height: usize,
196197 source: &[u8],
@@ -202,8 +203,8 @@ pub fn search(
202203 lambda: f64,
203204 ) -> MotionSearch {
204205 let (result, _trace) = search_traced(
205- reference, ref_width, ref_height, source, block_x, block_y, block_w, block_h, pred_mv,
206- lambda,
206+ reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h,
207+ pred_mv, lambda,
207208 );
208209 result
209210 }
@@ -214,6 +215,7 @@ pub fn search(
214215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
215216 fn search_traced(
216217 reference: &[u8],
218+ stride: usize,
217219 ref_width: usize,
218220 ref_height: usize,
219221 source: &[u8],
@@ -226,6 +228,7 @@ fn search_traced(
226228 ) -> (MotionSearch, Vec<f64>) {
227229 search_traced_from_step(
228230 reference,
231+ stride,
229232 ref_width,
230233 ref_height,
231234 source,
@@ -245,6 +248,7 @@ fn search_traced(
245248 #[allow(clippy::too_many_arguments)]
246249 fn search_traced_from_step(
247250 reference: &[u8],
251+ stride: usize,
248252 ref_width: usize,
249253 ref_height: usize,
250254 source: &[u8],
@@ -264,7 +268,7 @@ fn search_traced_from_step(
264268 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;
265269 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;
266270 predict(
267- reference, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst,
271+ reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst,
268272 );
269273 let sad: f64 = source
270274 .iter()
@@ -424,6 +428,7 @@ mod tests {
424428 let result = search(
425429 &plane,
426430 width,
431+ width,
427432 height,
428433 &block,
429434 anchor_x,
@@ -470,6 +475,7 @@ mod tests {
470475 let result = search(
471476 &plane,
472477 width,
478+ width,
473479 height,
474480 &block,
475481 anchor_x,
@@ -505,6 +511,7 @@ mod tests {
505511 let (_result, trace) = search_traced(
506512 &plane,
507513 width,
514+ width,
508515 height,
509516 &block,
510517 anchor_x,
@@ -571,6 +578,7 @@ mod tests {
571578 let (result, trace) = search_traced_from_step(
572579 &plane,
573580 width,
581+ width,
574582 height,
575583 &block,
576584 anchor_x,