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(
1478 mv: (i32, i32),1478 mv: (i32, i32),
1479 luma: bool,1479 luma: bool,
1480 reference: &[u8],1480 reference: &[u8],
1481+ stride: usize,
1481 ref_width: usize,1482 ref_width: usize,
1482 ref_height: usize,1483 ref_height: usize,
1483 skip: bool,1484 skip: bool,
@@ -1490,6 +1491,7 @@ fn mc_trial(
1490 let mut prediction = vec![0u8; side * side];1491 let mut prediction = vec![0u8; side * side];
1491 mc::predict(1492 mc::predict(
1492 reference,1493 reference,
1494+ stride,
1493 ref_width,1495 ref_width,
1494 ref_height,1496 ref_height,
1495 x_q4,1497 x_q4,
@@ -1614,9 +1616,34 @@ fn search_inter_block(
1614 });1616 });
1615 }1617 }
1616 1618
1617- let ref_luma = (&reference.y, reference.width, reference.height);1619+ // The reference frame buffer a spec decoder holds is exactly the true
1618- let ref_u = (&reference.u, reference.width / 2, reference.height / 2);1620+ // (unpadded) coded extent -- a superblock whose partition stops early at
1619- let ref_v = (&reference.v, reference.width / 2, reference.height / 2);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+ );
1620 let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV1647 let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV
1621 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero1648 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero
1622 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV1649 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV
@@ -1636,6 +1663,7 @@ fn search_inter_block(
1636 ref_luma.0,1663 ref_luma.0,
1637 ref_luma.1,1664 ref_luma.1,
1638 ref_luma.2,1665 ref_luma.2,
1666+ ref_luma.3,
1639 false,1667 false,
1640 search.base_q_idx,1668 search.base_q_idx,
1641 search.deadzone,1669 search.deadzone,
@@ -1651,6 +1679,7 @@ fn search_inter_block(
1651 ref_u.0,1679 ref_u.0,
1652 ref_u.1,1680 ref_u.1,
1653 ref_u.2,1681 ref_u.2,
1682+ ref_u.3,
1654 false,1683 false,
1655 search.base_q_idx,1684 search.base_q_idx,
1656 search.deadzone,1685 search.deadzone,
@@ -1666,6 +1695,7 @@ fn search_inter_block(
1666 ref_v.0,1695 ref_v.0,
1667 ref_v.1,1696 ref_v.1,
1668 ref_v.2,1697 ref_v.2,
1698+ ref_v.3,
1669 false,1699 false,
1670 search.base_q_idx,1700 search.base_q_idx,
1671 search.deadzone,1701 search.deadzone,
@@ -1706,6 +1736,7 @@ fn search_inter_block(
1706 ref_luma.0,1736 ref_luma.0,
1707 ref_luma.1,1737 ref_luma.1,
1708 ref_luma.2,1738 ref_luma.2,
1739+ ref_luma.3,
1709 &source_block,1740 &source_block,
1710 x,1741 x,
1711 y,1742 y,
@@ -1726,6 +1757,7 @@ fn search_inter_block(
1726 ref_luma.0,1757 ref_luma.0,
1727 ref_luma.1,1758 ref_luma.1,
1728 ref_luma.2,1759 ref_luma.2,
1760+ ref_luma.3,
1729 false,1761 false,
1730 search.base_q_idx,1762 search.base_q_idx,
1731 search.deadzone,1763 search.deadzone,
@@ -1741,6 +1773,7 @@ fn search_inter_block(
1741 ref_u.0,1773 ref_u.0,
1742 ref_u.1,1774 ref_u.1,
1743 ref_u.2,1775 ref_u.2,
1776+ ref_u.3,
1744 false,1777 false,
1745 search.base_q_idx,1778 search.base_q_idx,
1746 search.deadzone,1779 search.deadzone,
@@ -1756,6 +1789,7 @@ fn search_inter_block(
1756 ref_v.0,1789 ref_v.0,
1757 ref_v.1,1790 ref_v.1,
1758 ref_v.2,1791 ref_v.2,
1792+ ref_v.3,
1759 false,1793 false,
1760 search.base_q_idx,1794 search.base_q_idx,
1761 search.deadzone,1795 search.deadzone,
crates/ec-av1/src/mc.rsmodified+45 -13
@@ -52,18 +52,33 @@ const SUBPEL_FILTERS: [[i32; 8]; 16] = [
52 [0, 0, -2, 8, 126, -6, 2, 0],52 [0, 0, -2, 8, 126, -6, 2, 0],
53 ];53 ];
54 54
55-/// A reference sample at `(x, y)`, clamped to the plane's bounds (spec55+/// A reference sample at `(x, y)`, clamped to the frame's true (unpadded)
56-/// 7.11.3.4's `Clip3` against the frame's edges) rather than read out of56+/// extent -- `true_width`/`true_height`, which can be narrower than
57-/// range -- a motion vector that points past the frame repeats the edge57+/// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3`
58-/// sample instead of panicking.58+/// against the frame's edges): a motion vector that points past the true
59-fn sample(reference: &[u8], ref_width: usize, ref_height: usize, x: i32, y: i32) -> i32 {59+/// edge repeats the true edge's sample instead of panicking or picking up
60- let cx = x.clamp(0, ref_width as i32 - 1) as usize;60+/// whatever this encoder's own uncoded padding columns/rows hold.
61- let cy = y.clamp(0, ref_height as i32 - 1) as usize;61+#[allow(clippy::too_many_arguments)]
62- i32::from(reference[cy * ref_width + cx])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])
63 }73 }
64 74
65 /// Predicts one `block_w * block_h` block into `dst`, row-major, from75 /// 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).
67 ///82 ///
68 /// `x_q4` / `y_q4` are the block's top-left position in the reference, in83 /// `x_q4` / `y_q4` are the block's top-left position in the reference, in
69 /// 1/16-pel units (an integer MV has its low 4 bits zero); this function84 /// 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)
76 #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape91 #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape
77 pub fn predict(92 pub fn predict(
78 reference: &[u8],93 reference: &[u8],
79- ref_width: usize,94+ stride: usize,
80- ref_height: usize,95+ true_width: usize,
96+ true_height: usize,
81 x_q4: i32,97 x_q4: i32,
82 y_q4: i32,98 y_q4: i32,
83 block_w: usize,99 block_w: usize,
@@ -105,7 +121,7 @@ pub fn predict(
105 let mut sum = 0;121 let mut sum = 0;
106 for (t, &tap) in h_filter.iter().enumerate() {122 for (t, &tap) in h_filter.iter().enumerate() {
107 let x = x0 + c as i32 + t as i32 - 3;123 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);
109 }125 }
110 intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0);126 intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0);
111 }127 }
@@ -132,7 +148,17 @@ mod tests {
132 let height = 12;148 let height = 12;
133 let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect();149 let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect();
134 let mut dst = vec![0u8; 6 * 6];150 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+ );
136 for row in 0..6 {162 for row in 0..6 {
137 for col in 0..6 {163 for col in 0..6 {
138 assert_eq!(164 assert_eq!(
@@ -157,6 +183,7 @@ mod tests {
157 predict(183 predict(
158 &reference,184 &reference,
159 width,185 width,
186+ width,
160 height,187 height,
161 5 * 16 + 8,188 5 * 16 + 8,
162 5 * 16,189 5 * 16,
@@ -187,6 +214,7 @@ mod tests {
187 predict(214 predict(
188 &reference,215 &reference,
189 width,216 width,
217+ width,
190 height,218 height,
191 5 * 16 + xfrac,219 5 * 16 + xfrac,
192 5 * 16 + yfrac,220 5 * 16 + yfrac,
@@ -219,6 +247,7 @@ mod tests {
219 predict(247 predict(
220 &reference,248 &reference,
221 width,249 width,
250+ width,
222 height,251 height,
223 3 * 16 + 8,252 3 * 16 + 8,
224 3 * 16,253 3 * 16,
@@ -230,6 +259,7 @@ mod tests {
230 predict(259 predict(
231 &reference,260 &reference,
232 width,261 width,
262+ width,
233 height,263 height,
234 3 * 16,264 3 * 16,
235 3 * 16 + 8,265 3 * 16 + 8,
@@ -271,6 +301,7 @@ mod tests {
271 predict(301 predict(
272 &reference,302 &reference,
273 width,303 width,
304+ width,
274 height,305 height,
275 -50 * 16 + 5,306 -50 * 16 + 5,
276 -50 * 16 + 5,307 -50 * 16 + 5,
@@ -288,6 +319,7 @@ mod tests {
288 predict(319 predict(
289 &reference,320 &reference,
290 width,321 width,
322+ width,
291 height,323 height,
292 50 * 16 + 5,324 50 * 16 + 5,
293 50 * 16 + 5,325 50 * 16 + 5,
crates/ec-av1/src/motion.rsmodified+11 -3
@@ -191,6 +191,7 @@ pub struct MotionSearch {
191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor191 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
192 pub fn search(192 pub fn search(
193 reference: &[u8],193 reference: &[u8],
194+ stride: usize,
194 ref_width: usize,195 ref_width: usize,
195 ref_height: usize,196 ref_height: usize,
196 source: &[u8],197 source: &[u8],
@@ -202,8 +203,8 @@ pub fn search(
202 lambda: f64,203 lambda: f64,
203 ) -> MotionSearch {204 ) -> MotionSearch {
204 let (result, _trace) = search_traced(205 let (result, _trace) = search_traced(
205- reference, ref_width, ref_height, source, block_x, block_y, block_w, block_h, pred_mv,206+ reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h,
206- lambda,207+ pred_mv, lambda,
207 );208 );
208 result209 result
209 }210 }
@@ -214,6 +215,7 @@ pub fn search(
214 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor215 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
215 fn search_traced(216 fn search_traced(
216 reference: &[u8],217 reference: &[u8],
218+ stride: usize,
217 ref_width: usize,219 ref_width: usize,
218 ref_height: usize,220 ref_height: usize,
219 source: &[u8],221 source: &[u8],
@@ -226,6 +228,7 @@ fn search_traced(
226 ) -> (MotionSearch, Vec<f64>) {228 ) -> (MotionSearch, Vec<f64>) {
227 search_traced_from_step(229 search_traced_from_step(
228 reference,230 reference,
231+ stride,
229 ref_width,232 ref_width,
230 ref_height,233 ref_height,
231 source,234 source,
@@ -245,6 +248,7 @@ fn search_traced(
245 #[allow(clippy::too_many_arguments)]248 #[allow(clippy::too_many_arguments)]
246 fn search_traced_from_step(249 fn search_traced_from_step(
247 reference: &[u8],250 reference: &[u8],
251+ stride: usize,
248 ref_width: usize,252 ref_width: usize,
249 ref_height: usize,253 ref_height: usize,
250 source: &[u8],254 source: &[u8],
@@ -264,7 +268,7 @@ fn search_traced_from_step(
264 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;268 let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3;
265 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;269 let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3;
266 predict(270 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,
268 );272 );
269 let sad: f64 = source273 let sad: f64 = source
270 .iter()274 .iter()
@@ -424,6 +428,7 @@ mod tests {
424 let result = search(428 let result = search(
425 &plane,429 &plane,
426 width,430 width,
431+ width,
427 height,432 height,
428 &block,433 &block,
429 anchor_x,434 anchor_x,
@@ -470,6 +475,7 @@ mod tests {
470 let result = search(475 let result = search(
471 &plane,476 &plane,
472 width,477 width,
478+ width,
473 height,479 height,
474 &block,480 &block,
475 anchor_x,481 anchor_x,
@@ -505,6 +511,7 @@ mod tests {
505 let (_result, trace) = search_traced(511 let (_result, trace) = search_traced(
506 &plane,512 &plane,
507 width,513 width,
514+ width,
508 height,515 height,
509 &block,516 &block,
510 anchor_x,517 anchor_x,
@@ -571,6 +578,7 @@ mod tests {
571 let (result, trace) = search_traced_from_step(578 let (result, trace) = search_traced_from_step(
572 &plane,579 &plane,
573 width,580 width,
581+ width,
574 height,582 height,
575 &block,583 &block,
576 anchor_x,584 anchor_x,