Clamp inter MC reads to the reference's true extent, not its coding stride
Browse files at this commit8d520eff03a05c8f4c46114169ac64bdfbb2adf9Parent: 68e9a25- fix
- clamp inter MC reads to the reference's true extent, not its coding stride
3 changed files +93 -19
- crates/ec-av1/src/encode.rs +37 -3
- crates/ec-av1/src/mc.rs +45 -13
- crates/ec-av1/src/motion.rs +11 -3
crates/ec-av1/src/encode.rsmodified+37 -3
| @@ -1478,6 +1478,7 @@ fn mc_trial( | ||
| 1478 | 1478 | mv: (i32, i32), |
| 1479 | 1479 | luma: bool, |
| 1480 | 1480 | reference: &[u8], |
| 1481 | + stride: usize, | |
| 1481 | 1482 | ref_width: usize, |
| 1482 | 1483 | ref_height: usize, |
| 1483 | 1484 | skip: bool, |
| @@ -1490,6 +1491,7 @@ fn mc_trial( | ||
| 1490 | 1491 | let mut prediction = vec![0u8; side * side]; |
| 1491 | 1492 | mc::predict( |
| 1492 | 1493 | reference, |
| 1494 | + stride, | |
| 1493 | 1495 | ref_width, |
| 1494 | 1496 | ref_height, |
| 1495 | 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); | |
| 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 | + ); | |
| 1620 | 1647 | let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV |
| 1621 | 1648 | + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero |
| 1622 | 1649 | + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV |
| @@ -1636,6 +1663,7 @@ fn search_inter_block( | ||
| 1636 | 1663 | ref_luma.0, |
| 1637 | 1664 | ref_luma.1, |
| 1638 | 1665 | ref_luma.2, |
| 1666 | + ref_luma.3, | |
| 1639 | 1667 | false, |
| 1640 | 1668 | search.base_q_idx, |
| 1641 | 1669 | search.deadzone, |
| @@ -1651,6 +1679,7 @@ fn search_inter_block( | ||
| 1651 | 1679 | ref_u.0, |
| 1652 | 1680 | ref_u.1, |
| 1653 | 1681 | ref_u.2, |
| 1682 | + ref_u.3, | |
| 1654 | 1683 | false, |
| 1655 | 1684 | search.base_q_idx, |
| 1656 | 1685 | search.deadzone, |
| @@ -1666,6 +1695,7 @@ fn search_inter_block( | ||
| 1666 | 1695 | ref_v.0, |
| 1667 | 1696 | ref_v.1, |
| 1668 | 1697 | ref_v.2, |
| 1698 | + ref_v.3, | |
| 1669 | 1699 | false, |
| 1670 | 1700 | search.base_q_idx, |
| 1671 | 1701 | search.deadzone, |
| @@ -1706,6 +1736,7 @@ fn search_inter_block( | ||
| 1706 | 1736 | ref_luma.0, |
| 1707 | 1737 | ref_luma.1, |
| 1708 | 1738 | ref_luma.2, |
| 1739 | + ref_luma.3, | |
| 1709 | 1740 | &source_block, |
| 1710 | 1741 | x, |
| 1711 | 1742 | y, |
| @@ -1726,6 +1757,7 @@ fn search_inter_block( | ||
| 1726 | 1757 | ref_luma.0, |
| 1727 | 1758 | ref_luma.1, |
| 1728 | 1759 | ref_luma.2, |
| 1760 | + ref_luma.3, | |
| 1729 | 1761 | false, |
| 1730 | 1762 | search.base_q_idx, |
| 1731 | 1763 | search.deadzone, |
| @@ -1741,6 +1773,7 @@ fn search_inter_block( | ||
| 1741 | 1773 | ref_u.0, |
| 1742 | 1774 | ref_u.1, |
| 1743 | 1775 | ref_u.2, |
| 1776 | + ref_u.3, | |
| 1744 | 1777 | false, |
| 1745 | 1778 | search.base_q_idx, |
| 1746 | 1779 | search.deadzone, |
| @@ -1756,6 +1789,7 @@ fn search_inter_block( | ||
| 1756 | 1789 | ref_v.0, |
| 1757 | 1790 | ref_v.1, |
| 1758 | 1791 | ref_v.2, |
| 1792 | + ref_v.3, | |
| 1759 | 1793 | false, |
| 1760 | 1794 | search.base_q_idx, |
| 1761 | 1795 | search.deadzone, |
crates/ec-av1/src/mc.rsmodified+45 -13
| @@ -52,18 +52,33 @@ const SUBPEL_FILTERS: [[i32; 8]; 16] = [ | ||
| 52 | 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 (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]) | |
| 63 | 73 | } |
| 64 | 74 | |
| 65 | 75 | /// 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 | 83 | /// `x_q4` / `y_q4` are the block's top-left position in the reference, in |
| 69 | 84 | /// 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 | 91 | #[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape |
| 77 | 92 | pub fn predict( |
| 78 | 93 | reference: &[u8], |
| 79 | - ref_width: usize, | |
| 80 | - ref_height: usize, | |
| 94 | + stride: usize, | |
| 95 | + true_width: usize, | |
| 96 | + true_height: usize, | |
| 81 | 97 | x_q4: i32, |
| 82 | 98 | y_q4: i32, |
| 83 | 99 | block_w: usize, |
| @@ -105,7 +121,7 @@ pub fn predict( | ||
| 105 | 121 | let mut sum = 0; |
| 106 | 122 | for (t, &tap) in h_filter.iter().enumerate() { |
| 107 | 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 | 126 | intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); |
| 111 | 127 | } |
| @@ -132,7 +148,17 @@ mod tests { | ||
| 132 | 148 | let height = 12; |
| 133 | 149 | let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); |
| 134 | 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 | 162 | for row in 0..6 { |
| 137 | 163 | for col in 0..6 { |
| 138 | 164 | assert_eq!( |
| @@ -157,6 +183,7 @@ mod tests { | ||
| 157 | 183 | predict( |
| 158 | 184 | &reference, |
| 159 | 185 | width, |
| 186 | + width, | |
| 160 | 187 | height, |
| 161 | 188 | 5 * 16 + 8, |
| 162 | 189 | 5 * 16, |
| @@ -187,6 +214,7 @@ mod tests { | ||
| 187 | 214 | predict( |
| 188 | 215 | &reference, |
| 189 | 216 | width, |
| 217 | + width, | |
| 190 | 218 | height, |
| 191 | 219 | 5 * 16 + xfrac, |
| 192 | 220 | 5 * 16 + yfrac, |
| @@ -219,6 +247,7 @@ mod tests { | ||
| 219 | 247 | predict( |
| 220 | 248 | &reference, |
| 221 | 249 | width, |
| 250 | + width, | |
| 222 | 251 | height, |
| 223 | 252 | 3 * 16 + 8, |
| 224 | 253 | 3 * 16, |
| @@ -230,6 +259,7 @@ mod tests { | ||
| 230 | 259 | predict( |
| 231 | 260 | &reference, |
| 232 | 261 | width, |
| 262 | + width, | |
| 233 | 263 | height, |
| 234 | 264 | 3 * 16, |
| 235 | 265 | 3 * 16 + 8, |
| @@ -271,6 +301,7 @@ mod tests { | ||
| 271 | 301 | predict( |
| 272 | 302 | &reference, |
| 273 | 303 | width, |
| 304 | + width, | |
| 274 | 305 | height, |
| 275 | 306 | -50 * 16 + 5, |
| 276 | 307 | -50 * 16 + 5, |
| @@ -288,6 +319,7 @@ mod tests { | ||
| 288 | 319 | predict( |
| 289 | 320 | &reference, |
| 290 | 321 | width, |
| 322 | + width, | |
| 291 | 323 | height, |
| 292 | 324 | 50 * 16 + 5, |
| 293 | 325 | 50 * 16 + 5, |
crates/ec-av1/src/motion.rsmodified+11 -3
| @@ -191,6 +191,7 @@ pub struct MotionSearch { | ||
| 191 | 191 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor |
| 192 | 192 | pub fn search( |
| 193 | 193 | reference: &[u8], |
| 194 | + stride: usize, | |
| 194 | 195 | ref_width: usize, |
| 195 | 196 | ref_height: usize, |
| 196 | 197 | source: &[u8], |
| @@ -202,8 +203,8 @@ pub fn search( | ||
| 202 | 203 | lambda: f64, |
| 203 | 204 | ) -> MotionSearch { |
| 204 | 205 | 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, | |
| 207 | 208 | ); |
| 208 | 209 | result |
| 209 | 210 | } |
| @@ -214,6 +215,7 @@ pub fn search( | ||
| 214 | 215 | #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor |
| 215 | 216 | fn search_traced( |
| 216 | 217 | reference: &[u8], |
| 218 | + stride: usize, | |
| 217 | 219 | ref_width: usize, |
| 218 | 220 | ref_height: usize, |
| 219 | 221 | source: &[u8], |
| @@ -226,6 +228,7 @@ fn search_traced( | ||
| 226 | 228 | ) -> (MotionSearch, Vec<f64>) { |
| 227 | 229 | search_traced_from_step( |
| 228 | 230 | reference, |
| 231 | + stride, | |
| 229 | 232 | ref_width, |
| 230 | 233 | ref_height, |
| 231 | 234 | source, |
| @@ -245,6 +248,7 @@ fn search_traced( | ||
| 245 | 248 | #[allow(clippy::too_many_arguments)] |
| 246 | 249 | fn search_traced_from_step( |
| 247 | 250 | reference: &[u8], |
| 251 | + stride: usize, | |
| 248 | 252 | ref_width: usize, |
| 249 | 253 | ref_height: usize, |
| 250 | 254 | source: &[u8], |
| @@ -264,7 +268,7 @@ fn search_traced_from_step( | ||
| 264 | 268 | let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; |
| 265 | 269 | let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; |
| 266 | 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 | 273 | let sad: f64 = source |
| 270 | 274 | .iter() |
| @@ -424,6 +428,7 @@ mod tests { | ||
| 424 | 428 | let result = search( |
| 425 | 429 | &plane, |
| 426 | 430 | width, |
| 431 | + width, | |
| 427 | 432 | height, |
| 428 | 433 | &block, |
| 429 | 434 | anchor_x, |
| @@ -470,6 +475,7 @@ mod tests { | ||
| 470 | 475 | let result = search( |
| 471 | 476 | &plane, |
| 472 | 477 | width, |
| 478 | + width, | |
| 473 | 479 | height, |
| 474 | 480 | &block, |
| 475 | 481 | anchor_x, |
| @@ -505,6 +511,7 @@ mod tests { | ||
| 505 | 511 | let (_result, trace) = search_traced( |
| 506 | 512 | &plane, |
| 507 | 513 | width, |
| 514 | + width, | |
| 508 | 515 | height, |
| 509 | 516 | &block, |
| 510 | 517 | anchor_x, |
| @@ -571,6 +578,7 @@ mod tests { | ||
| 571 | 578 | let (result, trace) = search_traced_from_step( |
| 572 | 579 | &plane, |
| 573 | 580 | width, |
| 581 | + width, | |
| 574 | 582 | height, |
| 575 | 583 | &block, |
| 576 | 584 | anchor_x, |