| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 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 | |
| 22 | |
| 23 | |
| 24 | const INTER_ROUND_0: u32 = 3; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | const INTER_ROUND_1: u32 = 11; |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 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 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | const SUBPEL_FILTERS_4: [[i32; 8]; 16] = [ |
| 64 | [0, 0, 0, 128, 0, 0, 0, 0], |
| 65 | [0, 0, -4, 126, 8, -2, 0, 0], |
| 66 | [0, 0, -8, 122, 18, -4, 0, 0], |
| 67 | [0, 0, -10, 116, 28, -6, 0, 0], |
| 68 | [0, 0, -12, 110, 38, -8, 0, 0], |
| 69 | [0, 0, -12, 102, 48, -10, 0, 0], |
| 70 | [0, 0, -14, 94, 58, -10, 0, 0], |
| 71 | [0, 0, -12, 84, 66, -10, 0, 0], |
| 72 | [0, 0, -12, 76, 76, -12, 0, 0], |
| 73 | [0, 0, -10, 66, 84, -12, 0, 0], |
| 74 | [0, 0, -10, 58, 94, -14, 0, 0], |
| 75 | [0, 0, -10, 48, 102, -12, 0, 0], |
| 76 | [0, 0, -8, 38, 110, -12, 0, 0], |
| 77 | [0, 0, -6, 28, 116, -10, 0, 0], |
| 78 | [0, 0, -4, 18, 122, -8, 0, 0], |
| 79 | [0, 0, -2, 8, 126, -4, 0, 0], |
| 80 | ]; |
| 81 | |
| 82 | |
| 83 | const SUBPEL_FILTERS_SMOOTH: [[i32; 8]; 16] = [ |
| 84 | [0, 0, 0, 128, 0, 0, 0, 0], |
| 85 | [0, 2, 28, 62, 34, 2, 0, 0], |
| 86 | [0, 0, 26, 62, 36, 4, 0, 0], |
| 87 | [0, 0, 22, 62, 40, 4, 0, 0], |
| 88 | [0, 0, 20, 60, 42, 6, 0, 0], |
| 89 | [0, 0, 18, 58, 44, 8, 0, 0], |
| 90 | [0, 0, 16, 56, 46, 10, 0, 0], |
| 91 | [0, -2, 16, 54, 48, 12, 0, 0], |
| 92 | [0, -2, 14, 52, 52, 14, -2, 0], |
| 93 | [0, 0, 12, 48, 54, 16, -2, 0], |
| 94 | [0, 0, 10, 46, 56, 16, 0, 0], |
| 95 | [0, 0, 8, 44, 58, 18, 0, 0], |
| 96 | [0, 0, 6, 42, 60, 20, 0, 0], |
| 97 | [0, 0, 4, 40, 62, 22, 0, 0], |
| 98 | [0, 0, 4, 36, 62, 26, 0, 0], |
| 99 | [0, 0, 2, 34, 62, 28, 2, 0], |
| 100 | ]; |
| 101 | |
| 102 | |
| 103 | const SUBPEL_FILTERS_SMOOTH_4: [[i32; 8]; 16] = [ |
| 104 | [0, 0, 0, 128, 0, 0, 0, 0], |
| 105 | [0, 0, 30, 62, 34, 2, 0, 0], |
| 106 | [0, 0, 26, 62, 36, 4, 0, 0], |
| 107 | [0, 0, 22, 62, 40, 4, 0, 0], |
| 108 | [0, 0, 20, 60, 42, 6, 0, 0], |
| 109 | [0, 0, 18, 58, 44, 8, 0, 0], |
| 110 | [0, 0, 16, 56, 46, 10, 0, 0], |
| 111 | [0, 0, 14, 54, 48, 12, 0, 0], |
| 112 | [0, 0, 12, 52, 52, 12, 0, 0], |
| 113 | [0, 0, 12, 48, 54, 14, 0, 0], |
| 114 | [0, 0, 10, 46, 56, 16, 0, 0], |
| 115 | [0, 0, 8, 44, 58, 18, 0, 0], |
| 116 | [0, 0, 6, 42, 60, 20, 0, 0], |
| 117 | [0, 0, 4, 40, 62, 22, 0, 0], |
| 118 | [0, 0, 4, 36, 62, 26, 0, 0], |
| 119 | [0, 0, 2, 34, 62, 30, 0, 0], |
| 120 | ]; |
| 121 | |
| 122 | |
| 123 | |
| 124 | const SUBPEL_FILTERS_SHARP: [[i32; 8]; 16] = [ |
| 125 | [0, 0, 0, 128, 0, 0, 0, 0], |
| 126 | [-2, 2, -6, 126, 8, -2, 2, 0], |
| 127 | [-2, 6, -12, 124, 16, -6, 4, -2], |
| 128 | [-2, 8, -18, 120, 26, -10, 6, -2], |
| 129 | [-4, 10, -22, 116, 38, -14, 6, -2], |
| 130 | [-4, 10, -22, 108, 48, -18, 8, -2], |
| 131 | [-4, 10, -24, 100, 60, -20, 8, -2], |
| 132 | [-4, 10, -24, 90, 70, -22, 10, -2], |
| 133 | [-4, 12, -24, 80, 80, -24, 12, -4], |
| 134 | [-2, 10, -22, 70, 90, -24, 10, -4], |
| 135 | [-2, 8, -20, 60, 100, -24, 10, -4], |
| 136 | [-2, 8, -18, 48, 108, -22, 10, -4], |
| 137 | [-2, 6, -14, 38, 116, -22, 10, -4], |
| 138 | [-2, 6, -10, 26, 120, -18, 8, -2], |
| 139 | [-2, 4, -6, 16, 124, -12, 6, -2], |
| 140 | [0, 2, -2, 8, 126, -6, 2, -2], |
| 141 | ]; |
| 142 | |
| 143 | |
| 144 | |
| 145 | const SUBPEL_FILTERS_BILINEAR: [[i32; 8]; 16] = [ |
| 146 | [0, 0, 0, 128, 0, 0, 0, 0], |
| 147 | [0, 0, 0, 120, 8, 0, 0, 0], |
| 148 | [0, 0, 0, 112, 16, 0, 0, 0], |
| 149 | [0, 0, 0, 104, 24, 0, 0, 0], |
| 150 | [0, 0, 0, 96, 32, 0, 0, 0], |
| 151 | [0, 0, 0, 88, 40, 0, 0, 0], |
| 152 | [0, 0, 0, 80, 48, 0, 0, 0], |
| 153 | [0, 0, 0, 72, 56, 0, 0, 0], |
| 154 | [0, 0, 0, 64, 64, 0, 0, 0], |
| 155 | [0, 0, 0, 56, 72, 0, 0, 0], |
| 156 | [0, 0, 0, 48, 80, 0, 0, 0], |
| 157 | [0, 0, 0, 40, 88, 0, 0, 0], |
| 158 | [0, 0, 0, 32, 96, 0, 0, 0], |
| 159 | [0, 0, 0, 24, 104, 0, 0, 0], |
| 160 | [0, 0, 0, 16, 112, 0, 0, 0], |
| 161 | [0, 0, 0, 8, 120, 0, 0, 0], |
| 162 | ]; |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 172 | pub enum InterpFilterKind { |
| 173 | Regular, |
| 174 | Smooth, |
| 175 | Sharp, |
| 176 | Bilinear, |
| 177 | } |
| 178 | |
| 179 | impl InterpFilterKind { |
| 180 | fn tables(self) -> (&'static [[i32; 8]; 16], &'static [[i32; 8]; 16]) { |
| 181 | match self { |
| 182 | InterpFilterKind::Regular => (&SUBPEL_FILTERS, &SUBPEL_FILTERS_4), |
| 183 | InterpFilterKind::Smooth => (&SUBPEL_FILTERS_SMOOTH, &SUBPEL_FILTERS_SMOOTH_4), |
| 184 | InterpFilterKind::Sharp => (&SUBPEL_FILTERS_SHARP, &SUBPEL_FILTERS_SHARP), |
| 185 | InterpFilterKind::Bilinear => (&SUBPEL_FILTERS_BILINEAR, &SUBPEL_FILTERS_BILINEAR), |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | pub fn from_switchable_symbol(symbol: usize) -> InterpFilterKind { |
| 196 | match symbol { |
| 197 | 0 => InterpFilterKind::Regular, |
| 198 | 1 => InterpFilterKind::Smooth, |
| 199 | 2 => InterpFilterKind::Sharp, |
| 200 | _ => panic!("switchable_interp's alphabet is exactly 3 symbols"), |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | pub fn from_header(filter: ec_av1_syntax::InterpolationFilter) -> InterpFilterKind { |
| 211 | match filter { |
| 212 | ec_av1_syntax::InterpolationFilter::Eighttap => InterpFilterKind::Regular, |
| 213 | ec_av1_syntax::InterpolationFilter::EighttapSmooth => InterpFilterKind::Smooth, |
| 214 | ec_av1_syntax::InterpolationFilter::EighttapSharp => InterpFilterKind::Sharp, |
| 215 | ec_av1_syntax::InterpolationFilter::Bilinear => InterpFilterKind::Bilinear, |
| 216 | ec_av1_syntax::InterpolationFilter::Switchable => { |
| 217 | panic!("Switchable is resolved per block, not as one frame-wide kernel") |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | #[allow(clippy::too_many_arguments)] |
| 230 | fn sample( |
| 231 | reference: &[u8], |
| 232 | stride: usize, |
| 233 | true_width: usize, |
| 234 | true_height: usize, |
| 235 | x: i32, |
| 236 | y: i32, |
| 237 | ) -> i32 { |
| 238 | let cx = x.clamp(0, true_width as i32 - 1) as usize; |
| 239 | let cy = y.clamp(0, true_height as i32 - 1) as usize; |
| 240 | i32::from(reference[cy * stride + cx]) |
| 241 | } |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | #[allow(clippy::too_many_arguments)] |
| 260 | pub fn predict( |
| 261 | reference: &[u8], |
| 262 | stride: usize, |
| 263 | true_width: usize, |
| 264 | true_height: usize, |
| 265 | x_q4: i32, |
| 266 | y_q4: i32, |
| 267 | block_w: usize, |
| 268 | block_h: usize, |
| 269 | dst: &mut [u8], |
| 270 | ) { |
| 271 | predict_with_filter( |
| 272 | reference, |
| 273 | stride, |
| 274 | true_width, |
| 275 | true_height, |
| 276 | x_q4, |
| 277 | y_q4, |
| 278 | block_w, |
| 279 | block_h, |
| 280 | InterpFilterKind::Regular, |
| 281 | dst, |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | #[allow(clippy::too_many_arguments)] |
| 289 | pub fn predict_with_filter( |
| 290 | reference: &[u8], |
| 291 | stride: usize, |
| 292 | true_width: usize, |
| 293 | true_height: usize, |
| 294 | x_q4: i32, |
| 295 | y_q4: i32, |
| 296 | block_w: usize, |
| 297 | block_h: usize, |
| 298 | filter_kind: InterpFilterKind, |
| 299 | dst: &mut [u8], |
| 300 | ) { |
| 301 | predict_with_filters( |
| 302 | reference, |
| 303 | stride, |
| 304 | true_width, |
| 305 | true_height, |
| 306 | x_q4, |
| 307 | y_q4, |
| 308 | block_w, |
| 309 | block_h, |
| 310 | filter_kind, |
| 311 | filter_kind, |
| 312 | dst, |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | #[allow(clippy::too_many_arguments)] |
| 321 | pub fn predict_with_filters( |
| 322 | reference: &[u8], |
| 323 | stride: usize, |
| 324 | true_width: usize, |
| 325 | true_height: usize, |
| 326 | x_q4: i32, |
| 327 | y_q4: i32, |
| 328 | block_w: usize, |
| 329 | block_h: usize, |
| 330 | h_kind: InterpFilterKind, |
| 331 | v_kind: InterpFilterKind, |
| 332 | dst: &mut [u8], |
| 333 | ) { |
| 334 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); |
| 335 | assert!(!reference.is_empty(), "a reference plane has samples"); |
| 336 | |
| 337 | #[cfg(test)] |
| 338 | let stage_t = std::time::Instant::now(); |
| 339 | |
| 340 | let x0 = x_q4.div_euclid(16); |
| 341 | let xfrac = x_q4.rem_euclid(16) as usize; |
| 342 | let y0 = y_q4.div_euclid(16); |
| 343 | let yfrac = y_q4.rem_euclid(16) as usize; |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | if xfrac == 0 && yfrac == 0 { |
| 358 | for row in 0..block_h { |
| 359 | let y = y0 + row as i32; |
| 360 | for col in 0..block_w { |
| 361 | let x = x0 + col as i32; |
| 362 | dst[row * block_w + col] = |
| 363 | sample(reference, stride, true_width, true_height, x, y) as u8; |
| 364 | } |
| 365 | } |
| 366 | #[cfg(test)] |
| 367 | crate::encode::stage_add(1, stage_t.elapsed()); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | let (h_wide, h_narrow) = h_kind.tables(); |
| 372 | let (v_wide, v_narrow) = v_kind.tables(); |
| 373 | let h_filter = if block_w <= 4 { |
| 374 | &h_narrow[xfrac] |
| 375 | } else { |
| 376 | &h_wide[xfrac] |
| 377 | }; |
| 378 | let v_filter = if block_h <= 4 { |
| 379 | &v_narrow[yfrac] |
| 380 | } else { |
| 381 | &v_wide[yfrac] |
| 382 | }; |
| 383 | |
| 384 | |
| 385 | |
| 386 | let rows = block_h + 7; |
| 387 | let mut intermediate = vec![0i32; rows * block_w]; |
| 388 | for r in 0..rows { |
| 389 | let y = y0 - 3 + r as i32; |
| 390 | for c in 0..block_w { |
| 391 | let mut sum = 0; |
| 392 | for (t, &tap) in h_filter.iter().enumerate() { |
| 393 | let x = x0 + c as i32 + t as i32 - 3; |
| 394 | sum += tap * sample(reference, stride, true_width, true_height, x, y); |
| 395 | } |
| 396 | intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | for row in 0..block_h { |
| 401 | for col in 0..block_w { |
| 402 | let mut sum = 0; |
| 403 | for (t, &tap) in v_filter.iter().enumerate() { |
| 404 | sum += tap * intermediate[(row + t) * block_w + col]; |
| 405 | } |
| 406 | dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | #[cfg(test)] |
| 411 | crate::encode::stage_add(1, stage_t.elapsed()); |
| 412 | } |
| 413 | |
| 414 | #[cfg(test)] |
| 415 | mod tests { |
| 416 | use super::{InterpFilterKind, predict, predict_with_filter}; |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | #[test] |
| 427 | fn smooth_filter_matches_aomdec_chroma_inter_block() { |
| 428 | #[rustfmt::skip] |
| 429 | let window: [[u8; 24]; 24] = [ |
| 430 | [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], |
| 431 | [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], |
| 432 | [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], |
| 433 | [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], |
| 434 | [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114], |
| 435 | [114,114,114,114,114,114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116,115,115,115], |
| 436 | [114,114,114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,116,116,115,115,115,115], |
| 437 | [113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,116,116,116,116,115,115,115,115], |
| 438 | [113,113,113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,115,115,115,115,115,115], |
| 439 | [112,112,112,112,112,112,112,112,112,113,113,113,113,114,114,114,114,114,115,115,115,115,115,115], |
| 440 | [111,111,111,111,111,111,111,111,112,112,112,112,113,113,113,113,114,114,114,114,115,115,115,115], |
| 441 | [110,110,110,110,110,110,110,110,111,111,111,111,112,112,112,113,113,113,113,113,114,114,114,115], |
| 442 | [109,109,109,109,109,109,109,110,110,110,110,111,111,111,111,112,112,112,112,113,113,114,114,114], |
| 443 | [108,108,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111,111,111,112,112,113,113,113], |
| 444 | [108,108,108,108,108,108,108,108,108,108,109,109,109,110,110,110,110,110,111,111,112,112,112,112], |
| 445 | [107,107,107,107,107,107,107,107,107,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111], |
| 446 | [106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,110,110,110], |
| 447 | [106,106,106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,109], |
| 448 | [105,105,105,105,105,105,105,105,106,106,106,107,107,107,107,108,108,108,108,108,108,108,108,109], |
| 449 | [104,104,104,104,104,104,104,104,104,105,105,106,106,106,107,107,107,107,107,107,107,108,108,108], |
| 450 | [102,102,102,102,102,103,103,103,103,104,104,105,105,105,106,106,107,107,107,107,107,107,107,107], |
| 451 | [101,101,101,101,101,101,101,102,102,103,103,104,104,104,105,105,106,106,106,106,106,107,107,107], |
| 452 | [100,100,100,100,100,100,100,101,101,102,102,103,103,104,104,105,105,105,106,106,106,106,106,106], |
| 453 | [99,99,99,99,99,99,100,100,100,101,101,102,103,103,104,104,105,105,105,105,105,105,105,106], |
| 454 | ]; |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | let stride = 24; |
| 461 | let mut reference = vec![0u8; stride * 24]; |
| 462 | for (r, row) in window.iter().enumerate() { |
| 463 | reference[r * stride..r * stride + stride].copy_from_slice(row); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | let x_q4 = (4 + 0) * 16 + 15; |
| 472 | let y_q4 = (4 + 0) * 16 + 7; |
| 473 | let mut dst = vec![0u8; 16 * 16]; |
| 474 | predict_with_filter( |
| 475 | &reference, |
| 476 | stride, |
| 477 | 24, |
| 478 | 24, |
| 479 | x_q4, |
| 480 | y_q4, |
| 481 | 16, |
| 482 | 16, |
| 483 | InterpFilterKind::Smooth, |
| 484 | &mut dst, |
| 485 | ); |
| 486 | #[rustfmt::skip] |
| 487 | let expected_row0: [u8; 16] = |
| 488 | [114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116]; |
| 489 | assert_eq!( |
| 490 | &dst[0..16], |
| 491 | &expected_row0, |
| 492 | "row0 vs aomdec's real av1_convolve_2d_sr_c output" |
| 493 | ); |
| 494 | #[rustfmt::skip] |
| 495 | let expected_row15: [u8; 16] = |
| 496 | [103,103,104,104,104,105,105,106,106,106,107,107,107,107,107,107]; |
| 497 | assert_eq!( |
| 498 | &dst[15 * 16..15 * 16 + 16], |
| 499 | &expected_row15, |
| 500 | "row15 vs aomdec" |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn integer_mv_is_identity() { |
| 506 | let width = 12; |
| 507 | let height = 12; |
| 508 | let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); |
| 509 | let mut dst = vec![0u8; 6 * 6]; |
| 510 | predict( |
| 511 | &reference, |
| 512 | width, |
| 513 | width, |
| 514 | height, |
| 515 | 3 * 16, |
| 516 | 4 * 16, |
| 517 | 6, |
| 518 | 6, |
| 519 | &mut dst, |
| 520 | ); |
| 521 | for row in 0..6 { |
| 522 | for col in 0..6 { |
| 523 | assert_eq!( |
| 524 | dst[row * 6 + col], |
| 525 | reference[(4 + row) * width + 3 + col], |
| 526 | "integer MV must reproduce the reference block byte-for-byte" |
| 527 | ); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn half_pel_reproduces_the_ramp_midpoint() { |
| 534 | |
| 535 | let width = 16; |
| 536 | let height = 16; |
| 537 | let reference: Vec<u8> = (0..width * height) |
| 538 | .map(|i| (2 * (i % width)) as u8) |
| 539 | .collect(); |
| 540 | let mut dst = vec![0u8; 4 * 4]; |
| 541 | |
| 542 | predict( |
| 543 | &reference, |
| 544 | width, |
| 545 | width, |
| 546 | height, |
| 547 | 5 * 16 + 8, |
| 548 | 5 * 16, |
| 549 | 4, |
| 550 | 4, |
| 551 | &mut dst, |
| 552 | ); |
| 553 | for row in 0..4 { |
| 554 | for col in 0..4 { |
| 555 | let midpoint = 2 * (5 + col) + 1; |
| 556 | assert_eq!( |
| 557 | dst[row * 4 + col] as i32, |
| 558 | midpoint as i32, |
| 559 | "half-pel MV over a linear ramp must land exactly on the midpoint" |
| 560 | ); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn constant_plane_has_unit_dc_gain_at_every_subpel_position() { |
| 567 | let width = 20; |
| 568 | let height = 20; |
| 569 | let reference = vec![142u8; width * height]; |
| 570 | for yfrac in 0..16 { |
| 571 | for xfrac in 0..16 { |
| 572 | let mut dst = vec![0u8; 5 * 5]; |
| 573 | predict( |
| 574 | &reference, |
| 575 | width, |
| 576 | width, |
| 577 | height, |
| 578 | 5 * 16 + xfrac, |
| 579 | 5 * 16 + yfrac, |
| 580 | 5, |
| 581 | 5, |
| 582 | &mut dst, |
| 583 | ); |
| 584 | assert!( |
| 585 | dst.iter().all(|&v| v == 142), |
| 586 | "DC gain must be exactly 1 at fraction ({xfrac}, {yfrac}), got {dst:?}" |
| 587 | ); |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn horizontal_and_vertical_only_filtering_differ() { |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | let width = 10; |
| 600 | let height = 10; |
| 601 | let reference: Vec<u8> = (0..height) |
| 602 | .flat_map(|y| (0..width).map(move |x| (20 * x + 4 * y) as u8)) |
| 603 | .collect(); |
| 604 | |
| 605 | let mut horiz = vec![0u8; 4 * 4]; |
| 606 | predict( |
| 607 | &reference, |
| 608 | width, |
| 609 | width, |
| 610 | height, |
| 611 | 3 * 16 + 8, |
| 612 | 3 * 16, |
| 613 | 4, |
| 614 | 4, |
| 615 | &mut horiz, |
| 616 | ); |
| 617 | let mut vert = vec![0u8; 4 * 4]; |
| 618 | predict( |
| 619 | &reference, |
| 620 | width, |
| 621 | width, |
| 622 | height, |
| 623 | 3 * 16, |
| 624 | 3 * 16 + 8, |
| 625 | 4, |
| 626 | 4, |
| 627 | &mut vert, |
| 628 | ); |
| 629 | |
| 630 | for row in 0..4 { |
| 631 | for col in 0..4 { |
| 632 | |
| 633 | assert_eq!( |
| 634 | horiz[row * 4 + col] as i32, |
| 635 | 20 * (3 + col as i32) + 10 + 4 * (3 + row as i32), |
| 636 | "horizontal-only MV must interpolate along x only" |
| 637 | ); |
| 638 | |
| 639 | assert_eq!( |
| 640 | vert[row * 4 + col] as i32, |
| 641 | 20 * (3 + col as i32) + 4 * (3 + row as i32) + 2, |
| 642 | "vertical-only MV must interpolate along y only" |
| 643 | ); |
| 644 | } |
| 645 | } |
| 646 | assert_ne!( |
| 647 | horiz, vert, |
| 648 | "horizontal-only and vertical-only sub-pel MVs must give different outputs" |
| 649 | ); |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn mv_past_the_edge_clamps_instead_of_panicking() { |
| 654 | let width = 8; |
| 655 | let height = 8; |
| 656 | let reference: Vec<u8> = (0..width * height).map(|i| (i * 3 % 200) as u8).collect(); |
| 657 | let mut dst = vec![0u8; 4 * 4]; |
| 658 | |
| 659 | |
| 660 | predict( |
| 661 | &reference, |
| 662 | width, |
| 663 | width, |
| 664 | height, |
| 665 | -50 * 16 + 5, |
| 666 | -50 * 16 + 5, |
| 667 | 4, |
| 668 | 4, |
| 669 | &mut dst, |
| 670 | ); |
| 671 | let expected = reference[0]; |
| 672 | assert!( |
| 673 | dst.iter().all(|&v| v == expected), |
| 674 | "an MV past the top-left edge must clamp to the corner sample, got {dst:?}" |
| 675 | ); |
| 676 | |
| 677 | let mut dst2 = vec![0u8; 4 * 4]; |
| 678 | predict( |
| 679 | &reference, |
| 680 | width, |
| 681 | width, |
| 682 | height, |
| 683 | 50 * 16 + 5, |
| 684 | 50 * 16 + 5, |
| 685 | 4, |
| 686 | 4, |
| 687 | &mut dst2, |
| 688 | ); |
| 689 | let expected2 = reference[height * width - 1]; |
| 690 | assert!( |
| 691 | dst2.iter().all(|&v| v == expected2), |
| 692 | "an MV past the bottom-right edge must clamp to the corner sample, got {dst2:?}" |
| 693 | ); |
| 694 | } |
| 695 | } |