Sub-pel motion compensation (spec 7.11.3, EIGHTTAP regular)
Browse files at this commit082d9933fc615d63c1f037e49ff685f60feaa10fParent: 81c7624- feat
- sub-pel motion compensation (spec 7.11.3, EIGHTTAP regular)
2 changed files +305 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/mc.rs +304 -0
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -18,6 +18,7 @@ mod cdf_state; | ||
| 18 | 18 | pub mod encode; |
| 19 | 19 | pub mod frame; |
| 20 | 20 | pub mod intra; |
| 21 | +pub mod mc; | |
| 21 | 22 | pub mod msac; |
| 22 | 23 | pub mod obu; |
| 23 | 24 | pub mod quant; |
crates/ec-av1/src/mc.rsadded+304 -0
| @@ -0,0 +1,304 @@ | ||
| 1 | +//! Sub-pel motion compensation, spec 7.11.3 ("Motion vector scaling process" | |
| 2 | +//! through "Block inter predictor process"), the REGULAR (`EIGHTTAP`) filter | |
| 3 | +//! only, 8-bit samples only. | |
| 4 | +//! | |
| 5 | +//! A block's inter prediction is separable: an 8-tap horizontal pass over the | |
| 6 | +//! reference, rounded down by `InterRound0`, feeds an 8-tap vertical pass | |
| 7 | +//! over that intermediate, rounded down by `InterRound1`. Both fractions run | |
| 8 | +//! in 1/16-pel steps -- luma motion vectors are stored at 1/8-pel and always | |
| 9 | +//! land on an even step; chroma vectors, once scaled for subsampling, can | |
| 10 | +//! land on any of the 16. | |
| 11 | + | |
| 12 | +/// `Round2` (spec 4.7): halves round up. | |
| 13 | +fn round2(value: i32, shift: u32) -> i32 { | |
| 14 | + if shift == 0 { | |
| 15 | + value | |
| 16 | + } else { | |
| 17 | + (value + (1 << (shift - 1))) >> shift | |
| 18 | + } | |
| 19 | +} | |
| 20 | + | |
| 21 | +/// `InterRound0` (spec 7.11.3.2), 8-bit, non-compound: the shift the | |
| 22 | +/// horizontal pass's sum is brought down by before it is stored as the | |
| 23 | +/// intermediate. | |
| 24 | +const INTER_ROUND_0: u32 = 3; | |
| 25 | +/// `InterRound1` (spec 7.11.3.2), 8-bit, non-compound: the shift the vertical | |
| 26 | +/// pass's sum is brought down by to land back on an 8-bit sample. Composed | |
| 27 | +/// with `INTER_ROUND_0` the two shifts are the filter's own gain (each pass | |
| 28 | +/// sums to 128, `128 * 128 == 1 << 14 == 1 << (INTER_ROUND_0 + INTER_ROUND_1)`) | |
| 29 | +/// -- so an identity or DC input comes back exactly, by construction of the | |
| 30 | +/// table rather than by a fitted constant. | |
| 31 | +const INTER_ROUND_1: u32 = 11; | |
| 32 | + | |
| 33 | +/// `Subpel_Filters[EIGHTTAP]` (spec 7.11.3.3): 16 rows, one per 1/16-pel | |
| 34 | +/// fraction, each an 8-tap kernel centred so tap index 3 lands on the integer | |
| 35 | +/// sample (fraction 0 is the identity tap `[.., 128, ..]`). | |
| 36 | +const SUBPEL_FILTERS: [[i32; 8]; 16] = [ | |
| 37 | + [0, 0, 0, 128, 0, 0, 0, 0], | |
| 38 | + [0, 2, -6, 126, 8, -2, 0, 0], | |
| 39 | + [0, 2, -10, 122, 18, -4, 0, 0], | |
| 40 | + [0, 2, -12, 116, 28, -8, 2, 0], | |
| 41 | + [0, 2, -14, 110, 38, -10, 2, 0], | |
| 42 | + [0, 2, -14, 102, 48, -12, 2, 0], | |
| 43 | + [0, 2, -16, 94, 58, -12, 2, 0], | |
| 44 | + [0, 2, -14, 84, 66, -12, 2, 0], | |
| 45 | + [0, 2, -14, 76, 76, -14, 2, 0], | |
| 46 | + [0, 2, -12, 66, 84, -14, 2, 0], | |
| 47 | + [0, 2, -12, 58, 94, -16, 2, 0], | |
| 48 | + [0, 2, -12, 48, 102, -14, 2, 0], | |
| 49 | + [0, 2, -10, 38, 110, -14, 2, 0], | |
| 50 | + [0, 2, -8, 28, 116, -12, 2, 0], | |
| 51 | + [0, 0, -4, 18, 122, -10, 2, 0], | |
| 52 | + [0, 0, -2, 8, 126, -6, 2, 0], | |
| 53 | +]; | |
| 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]) | |
| 63 | +} | |
| 64 | + | |
| 65 | +/// Predicts one `block_w * block_h` block into `dst`, row-major, from | |
| 66 | +/// `reference` (row-major, `ref_width * ref_height`, stride `ref_width`). | |
| 67 | +/// | |
| 68 | +/// `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 function | |
| 70 | +/// splits each into its whole-sample part and its 0..16 fraction itself, so | |
| 71 | +/// the caller need not pre-split a motion vector. | |
| 72 | +/// | |
| 73 | +/// # Panics | |
| 74 | +/// Panics when `dst` is not `block_w * block_h` long, or the reference is | |
| 75 | +/// empty. | |
| 76 | +#[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape | |
| 77 | +pub fn predict( | |
| 78 | + reference: &[u8], | |
| 79 | + ref_width: usize, | |
| 80 | + ref_height: usize, | |
| 81 | + x_q4: i32, | |
| 82 | + y_q4: i32, | |
| 83 | + block_w: usize, | |
| 84 | + block_h: usize, | |
| 85 | + dst: &mut [u8], | |
| 86 | +) { | |
| 87 | + assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | |
| 88 | + assert!(!reference.is_empty(), "a reference plane has samples"); | |
| 89 | + | |
| 90 | + let x0 = x_q4.div_euclid(16); | |
| 91 | + let xfrac = x_q4.rem_euclid(16) as usize; | |
| 92 | + let y0 = y_q4.div_euclid(16); | |
| 93 | + let yfrac = y_q4.rem_euclid(16) as usize; | |
| 94 | + | |
| 95 | + let h_filter = &SUBPEL_FILTERS[xfrac]; | |
| 96 | + let v_filter = &SUBPEL_FILTERS[yfrac]; | |
| 97 | + | |
| 98 | + // The vertical pass reads 3 rows above and 4 below the block, so the | |
| 99 | + // horizontal pass must produce that many extra intermediate rows. | |
| 100 | + let rows = block_h + 7; | |
| 101 | + let mut intermediate = vec![0i32; rows * block_w]; | |
| 102 | + for r in 0..rows { | |
| 103 | + let y = y0 - 3 + r as i32; | |
| 104 | + for c in 0..block_w { | |
| 105 | + let mut sum = 0; | |
| 106 | + for (t, &tap) in h_filter.iter().enumerate() { | |
| 107 | + let x = x0 + c as i32 + t as i32 - 3; | |
| 108 | + sum += tap * sample(reference, ref_width, ref_height, x, y); | |
| 109 | + } | |
| 110 | + intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + for row in 0..block_h { | |
| 115 | + for col in 0..block_w { | |
| 116 | + let mut sum = 0; | |
| 117 | + for (t, &tap) in v_filter.iter().enumerate() { | |
| 118 | + sum += tap * intermediate[(row + t) * block_w + col]; | |
| 119 | + } | |
| 120 | + dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8; | |
| 121 | + } | |
| 122 | + } | |
| 123 | +} | |
| 124 | + | |
| 125 | +#[cfg(test)] | |
| 126 | +mod tests { | |
| 127 | + use super::predict; | |
| 128 | + | |
| 129 | + #[test] | |
| 130 | + fn integer_mv_is_identity() { | |
| 131 | + let width = 12; | |
| 132 | + let height = 12; | |
| 133 | + let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); | |
| 134 | + let mut dst = vec![0u8; 6 * 6]; | |
| 135 | + predict(&reference, width, height, 3 * 16, 4 * 16, 6, 6, &mut dst); | |
| 136 | + for row in 0..6 { | |
| 137 | + for col in 0..6 { | |
| 138 | + assert_eq!( | |
| 139 | + dst[row * 6 + col], | |
| 140 | + reference[(4 + row) * width + 3 + col], | |
| 141 | + "integer MV must reproduce the reference block byte-for-byte" | |
| 142 | + ); | |
| 143 | + } | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + #[test] | |
| 148 | + fn half_pel_reproduces_the_ramp_midpoint() { | |
| 149 | + // Step 2 so every half-pel position lands on an exact integer. | |
| 150 | + let width = 16; | |
| 151 | + let height = 16; | |
| 152 | + let reference: Vec<u8> = (0..width * height) | |
| 153 | + .map(|i| (2 * (i % width)) as u8) | |
| 154 | + .collect(); | |
| 155 | + let mut dst = vec![0u8; 4 * 4]; | |
| 156 | + // Half-pel in x only: fraction 8/16, integer part at column 5. | |
| 157 | + predict( | |
| 158 | + &reference, | |
| 159 | + width, | |
| 160 | + height, | |
| 161 | + 5 * 16 + 8, | |
| 162 | + 5 * 16, | |
| 163 | + 4, | |
| 164 | + 4, | |
| 165 | + &mut dst, | |
| 166 | + ); | |
| 167 | + for row in 0..4 { | |
| 168 | + for col in 0..4 { | |
| 169 | + let midpoint = 2 * (5 + col) + 1; // halfway between col and col+1 | |
| 170 | + assert_eq!( | |
| 171 | + dst[row * 4 + col] as i32, | |
| 172 | + midpoint as i32, | |
| 173 | + "half-pel MV over a linear ramp must land exactly on the midpoint" | |
| 174 | + ); | |
| 175 | + } | |
| 176 | + } | |
| 177 | + } | |
| 178 | + | |
| 179 | + #[test] | |
| 180 | + fn constant_plane_has_unit_dc_gain_at_every_subpel_position() { | |
| 181 | + let width = 20; | |
| 182 | + let height = 20; | |
| 183 | + let reference = vec![142u8; width * height]; | |
| 184 | + for yfrac in 0..16 { | |
| 185 | + for xfrac in 0..16 { | |
| 186 | + let mut dst = vec![0u8; 5 * 5]; | |
| 187 | + predict( | |
| 188 | + &reference, | |
| 189 | + width, | |
| 190 | + height, | |
| 191 | + 5 * 16 + xfrac, | |
| 192 | + 5 * 16 + yfrac, | |
| 193 | + 5, | |
| 194 | + 5, | |
| 195 | + &mut dst, | |
| 196 | + ); | |
| 197 | + assert!( | |
| 198 | + dst.iter().all(|&v| v == 142), | |
| 199 | + "DC gain must be exactly 1 at fraction ({xfrac}, {yfrac}), got {dst:?}" | |
| 200 | + ); | |
| 201 | + } | |
| 202 | + } | |
| 203 | + } | |
| 204 | + | |
| 205 | + #[test] | |
| 206 | + fn horizontal_and_vertical_only_filtering_differ() { | |
| 207 | + // A plane linear in both x and y with different, even coefficients so | |
| 208 | + // each axis's half-pel interpolation lands on an exact integer: this | |
| 209 | + // pins the actual value each pass produces, not just that the two | |
| 210 | + // differ, so swapping which pass reads which axis is caught even if | |
| 211 | + // it happened to produce two merely-different numbers. | |
| 212 | + let width = 10; | |
| 213 | + let height = 10; | |
| 214 | + let reference: Vec<u8> = (0..height) | |
| 215 | + .flat_map(|y| (0..width).map(move |x| (20 * x + 4 * y) as u8)) | |
| 216 | + .collect(); | |
| 217 | + | |
| 218 | + let mut horiz = vec![0u8; 4 * 4]; | |
| 219 | + predict( | |
| 220 | + &reference, | |
| 221 | + width, | |
| 222 | + height, | |
| 223 | + 3 * 16 + 8, | |
| 224 | + 3 * 16, | |
| 225 | + 4, | |
| 226 | + 4, | |
| 227 | + &mut horiz, | |
| 228 | + ); | |
| 229 | + let mut vert = vec![0u8; 4 * 4]; | |
| 230 | + predict( | |
| 231 | + &reference, | |
| 232 | + width, | |
| 233 | + height, | |
| 234 | + 3 * 16, | |
| 235 | + 3 * 16 + 8, | |
| 236 | + 4, | |
| 237 | + 4, | |
| 238 | + &mut vert, | |
| 239 | + ); | |
| 240 | + | |
| 241 | + for row in 0..4 { | |
| 242 | + for col in 0..4 { | |
| 243 | + // Horizontal half-pel: x interpolates to x0+col+0.5, y stays put. | |
| 244 | + assert_eq!( | |
| 245 | + horiz[row * 4 + col] as i32, | |
| 246 | + 20 * (3 + col as i32) + 10 + 4 * (3 + row as i32), | |
| 247 | + "horizontal-only MV must interpolate along x only" | |
| 248 | + ); | |
| 249 | + // Vertical half-pel: y interpolates to y0+row+0.5, x stays put. | |
| 250 | + assert_eq!( | |
| 251 | + vert[row * 4 + col] as i32, | |
| 252 | + 20 * (3 + col as i32) + 4 * (3 + row as i32) + 2, | |
| 253 | + "vertical-only MV must interpolate along y only" | |
| 254 | + ); | |
| 255 | + } | |
| 256 | + } | |
| 257 | + assert_ne!( | |
| 258 | + horiz, vert, | |
| 259 | + "horizontal-only and vertical-only sub-pel MVs must give different outputs" | |
| 260 | + ); | |
| 261 | + } | |
| 262 | + | |
| 263 | + #[test] | |
| 264 | + fn mv_past_the_edge_clamps_instead_of_panicking() { | |
| 265 | + let width = 8; | |
| 266 | + let height = 8; | |
| 267 | + let reference: Vec<u8> = (0..width * height).map(|i| (i * 3 % 200) as u8).collect(); | |
| 268 | + let mut dst = vec![0u8; 4 * 4]; | |
| 269 | + // Whole-sample position far above and to the left of the plane, with | |
| 270 | + // an odd fraction so both filter passes exercise the clamp. | |
| 271 | + predict( | |
| 272 | + &reference, | |
| 273 | + width, | |
| 274 | + height, | |
| 275 | + -50 * 16 + 5, | |
| 276 | + -50 * 16 + 5, | |
| 277 | + 4, | |
| 278 | + 4, | |
| 279 | + &mut dst, | |
| 280 | + ); | |
| 281 | + let expected = reference[0]; // the whole plane clamps to the top-left corner | |
| 282 | + assert!( | |
| 283 | + dst.iter().all(|&v| v == expected), | |
| 284 | + "an MV past the top-left edge must clamp to the corner sample, got {dst:?}" | |
| 285 | + ); | |
| 286 | + | |
| 287 | + let mut dst2 = vec![0u8; 4 * 4]; | |
| 288 | + predict( | |
| 289 | + &reference, | |
| 290 | + width, | |
| 291 | + height, | |
| 292 | + 50 * 16 + 5, | |
| 293 | + 50 * 16 + 5, | |
| 294 | + 4, | |
| 295 | + 4, | |
| 296 | + &mut dst2, | |
| 297 | + ); | |
| 298 | + let expected2 = reference[height * width - 1]; // clamps to the bottom-right corner | |
| 299 | + assert!( | |
| 300 | + dst2.iter().all(|&v| v == expected2), | |
| 301 | + "an MV past the bottom-right edge must clamp to the corner sample, got {dst2:?}" | |
| 302 | + ); | |
| 303 | + } | |
| 304 | +} |