U7: drop inline test modules (merge, amvp, motion_field test_shared), rdoq_full_recompute test and mc_tests.rs (test bloat, no output change; motion_field trace hunk not split out)
Browse files at this commit349abd6f9a094260249c28a99ec6cbe6462fe404Parent: 38b53e56 changed files +0 -2001
- crates/ec-h265/src/dec/amvp.rs +0 -230
- crates/ec-h265/src/dec/mc.rs +0 -4
- crates/ec-h265/src/dec/mc_tests.rs +0 -1137
- crates/ec-h265/src/dec/merge.rs +0 -362
- crates/ec-h265/src/dec/motion_field.rs +0 -10
- crates/ec-h265/src/residual.rs +0 -258
crates/ec-h265/src/dec/amvp.rsmodified+0 -230
| @@ -146,233 +146,3 @@ fn wrap16(v: i32) -> i16 { | |||
| 146 | let signed = if u >= 1 << 15 { u - (1 << 16) } else { u }; | 146 | let signed = if u >= 1 << 15 { u - (1 << 16) } else { u }; |
| 147 | signed as i16 | 147 | signed as i16 |
| 148 | } | 148 | } |
| 149 | - | ||
| 150 | -#[cfg(test)] | ||
| 151 | -mod tests { | ||
| 152 | - use super::super::cu_syntax::InterPredIdc; | ||
| 153 | - use super::super::motion_field::{MotionField, MotionParams, SliceRefs, test_shared}; | ||
| 154 | - use super::super::rps::{RefEntry, RefPicLists}; | ||
| 155 | - use super::super::shared::SharedPic; | ||
| 156 | - use super::*; | ||
| 157 | - use std::sync::Arc; | ||
| 158 | - | ||
| 159 | - fn entry(id: u32, poc: i32, long_term: bool) -> RefEntry { | ||
| 160 | - RefEntry { id, poc, long_term } | ||
| 161 | - } | ||
| 162 | - | ||
| 163 | - fn params(temporal: bool) -> MotionParams { | ||
| 164 | - MotionParams { | ||
| 165 | - is_b: false, | ||
| 166 | - max_merge_cand: 5, | ||
| 167 | - log2_par_mrg_level: 2, | ||
| 168 | - ctb_log2: 6, | ||
| 169 | - collocated_from_l0: true, | ||
| 170 | - temporal_mvp: temporal, | ||
| 171 | - no_backward_pred: true, | ||
| 172 | - } | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - fn l0(ref_idx: i8, mv: [i16; 2]) -> PbMotion { | ||
| 176 | - PbMotion::new([true, false], [ref_idx, -1], [mv, [0, 0]]) | ||
| 177 | - } | ||
| 178 | - | ||
| 179 | - /// An AMVP PU at (16,16) of size 16x16 with list 0, reference `ref_idx` and | ||
| 180 | - /// difference `mvd`, using predictor `flag`. | ||
| 181 | - fn pu(mvd: [i32; 2], flag: u8) -> PredictionUnit { | ||
| 182 | - PredictionUnit { | ||
| 183 | - x: 16, | ||
| 184 | - y: 16, | ||
| 185 | - width: 16, | ||
| 186 | - height: 16, | ||
| 187 | - inter_pred_idc: InterPredIdc::PredL0, | ||
| 188 | - mvp_flag: [flag, 0], | ||
| 189 | - mvd: [mvd, [0, 0]], | ||
| 190 | - ..PredictionUnit::default() | ||
| 191 | - } | ||
| 192 | - } | ||
| 193 | - | ||
| 194 | - /// Run the AMVP derivation for `pu` with `field` as the current motion field. | ||
| 195 | - fn run( | ||
| 196 | - field: &mut MotionField, | ||
| 197 | - lists: &RefPicLists, | ||
| 198 | - cur_poc: i32, | ||
| 199 | - col: Option<Arc<SharedPic>>, | ||
| 200 | - temporal: bool, | ||
| 201 | - pu: &PredictionUnit, | ||
| 202 | - ) -> PbMotion { | ||
| 203 | - let src = SliceMotion { | ||
| 204 | - params: params(temporal), | ||
| 205 | - lists, | ||
| 206 | - cur_poc, | ||
| 207 | - field, | ||
| 208 | - col, | ||
| 209 | - publish: None, | ||
| 210 | - slice: 0, | ||
| 211 | - }; | ||
| 212 | - derive_amvp(&src, pu) | ||
| 213 | - } | ||
| 214 | - | ||
| 215 | - fn lists(poc: i32, long_term: bool) -> RefPicLists { | ||
| 216 | - RefPicLists { | ||
| 217 | - l0: vec![entry(0, poc, long_term)], | ||
| 218 | - l1: Vec::new(), | ||
| 219 | - } | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | - #[test] | ||
| 223 | - fn spatial_a_with_the_same_reference_is_used_unscaled() { | ||
| 224 | - // A1 = (15,31) refers to POC 4, the target. Its vector is the predictor. | ||
| 225 | - let mut field = MotionField::new(64, 64); | ||
| 226 | - field.store(0, 16, 16, 16, 0, l0(0, [40, -12])); | ||
| 227 | - let got = run(&mut field, &lists(4, false), 8, None, false, &pu([1, 1], 0)); | ||
| 228 | - assert_eq!(got, l0(0, [41, -11])); | ||
| 229 | - } | ||
| 230 | - | ||
| 231 | - #[test] | ||
| 232 | - fn spatial_a_scales_by_distance() { | ||
| 233 | - // Current POC 8, target POC 4 (tb = 4). A1 refers to POC 0 (td = 8), mv 64: | ||
| 234 | - // tx = (16384 + 4) / 8 = 2048, dsf = (4 * 2048 + 32) >> 6 = 128, mv' = 32. | ||
| 235 | - // L0[0] is the target (POC 4); the neighbour uses L0[1] (POC 0). | ||
| 236 | - let lists = RefPicLists { | ||
| 237 | - l0: vec![entry(0, 4, false), entry(1, 0, false)], | ||
| 238 | - l1: Vec::new(), | ||
| 239 | - }; | ||
| 240 | - let mut field = MotionField::new(64, 64); | ||
| 241 | - field.store(0, 16, 16, 16, 0, l0(1, [64, 0])); | ||
| 242 | - let got = run(&mut field, &lists, 8, None, false, &pu([0, 0], 0)); | ||
| 243 | - assert_eq!(got, l0(0, [32, 0])); | ||
| 244 | - } | ||
| 245 | - | ||
| 246 | - #[test] | ||
| 247 | - fn spatial_a_scales_with_negative_distance() { | ||
| 248 | - // Current POC 8, target POC 4 (tb = 4). A1 refers to POC 12 (td = -4), mv 64: | ||
| 249 | - // tx = (16384 + 2) / -4 = -4096, dsf = (4 * -4096 + 32) >> 6 = -256, | ||
| 250 | - // mv' = -64. | ||
| 251 | - let lists = RefPicLists { | ||
| 252 | - l0: vec![entry(0, 4, false), entry(1, 12, false)], | ||
| 253 | - l1: Vec::new(), | ||
| 254 | - }; | ||
| 255 | - let mut field = MotionField::new(64, 64); | ||
| 256 | - field.store(0, 16, 16, 16, 0, l0(1, [64, 0])); | ||
| 257 | - let got = run(&mut field, &lists, 8, None, false, &pu([0, 0], 0)); | ||
| 258 | - assert_eq!(got, l0(0, [-64, 0])); | ||
| 259 | - } | ||
| 260 | - | ||
| 261 | - #[test] | ||
| 262 | - fn long_term_neighbour_is_not_scaled() { | ||
| 263 | - // The target is long-term (POC 4) and so is the neighbour's reference (POC 0): | ||
| 264 | - // the vector is used as it is. | ||
| 265 | - let lists = RefPicLists { | ||
| 266 | - l0: vec![entry(0, 4, true), entry(1, 0, true)], | ||
| 267 | - l1: Vec::new(), | ||
| 268 | - }; | ||
| 269 | - let mut field = MotionField::new(64, 64); | ||
| 270 | - field.store(0, 16, 16, 16, 0, l0(1, [64, 0])); | ||
| 271 | - let got = run(&mut field, &lists, 8, None, false, &pu([0, 0], 0)); | ||
| 272 | - assert_eq!(got, l0(0, [64, 0])); | ||
| 273 | - } | ||
| 274 | - | ||
| 275 | - #[test] | ||
| 276 | - fn long_term_mismatch_skips_the_neighbour() { | ||
| 277 | - // The target is long-term but the neighbour's reference is short-term: the | ||
| 278 | - // neighbour gives no predictor, and the list is zero-filled. | ||
| 279 | - let lists = RefPicLists { | ||
| 280 | - l0: vec![entry(0, 4, true), entry(1, 0, false)], | ||
| 281 | - l1: Vec::new(), | ||
| 282 | - }; | ||
| 283 | - let mut field = MotionField::new(64, 64); | ||
| 284 | - field.store(0, 16, 16, 16, 0, l0(1, [64, 0])); | ||
| 285 | - let got = run(&mut field, &lists, 8, None, false, &pu([0, 0], 0)); | ||
| 286 | - assert_eq!(got, l0(0, [0, 0])); | ||
| 287 | - } | ||
| 288 | - | ||
| 289 | - #[test] | ||
| 290 | - fn b_candidate_fills_the_a_slot_when_no_a_neighbour() { | ||
| 291 | - // No A neighbour. B1 = (31,15) refers to the target POC 4: it fills the A slot, | ||
| 292 | - // and the second B pass scales it back to itself (td = tb), so the list is | ||
| 293 | - // [B, 0] and flag 0 picks B. | ||
| 294 | - let mut field = MotionField::new(64, 64); | ||
| 295 | - field.store(16, 0, 16, 16, 0, l0(0, [20, 6])); | ||
| 296 | - let got = run(&mut field, &lists(4, false), 8, None, false, &pu([0, 0], 0)); | ||
| 297 | - assert_eq!(got, l0(0, [20, 6])); | ||
| 298 | - let other = run(&mut field, &lists(4, false), 8, None, false, &pu([0, 0], 1)); | ||
| 299 | - assert_eq!(other, l0(0, [0, 0])); | ||
| 300 | - } | ||
| 301 | - | ||
| 302 | - #[test] | ||
| 303 | - fn equal_a_and_b_collapse_to_one_predictor() { | ||
| 304 | - // A1 and B1 carry the same vector toward the target: the list is [A, 0]. | ||
| 305 | - let mut field = MotionField::new(64, 64); | ||
| 306 | - field.store(0, 16, 16, 16, 0, l0(0, [7, 7])); | ||
| 307 | - field.store(16, 0, 16, 16, 0, l0(0, [7, 7])); | ||
| 308 | - let got = run(&mut field, &lists(4, false), 8, None, false, &pu([0, 0], 1)); | ||
| 309 | - assert_eq!(got, l0(0, [0, 0])); | ||
| 310 | - let first = run(&mut field, &lists(4, false), 8, None, false, &pu([0, 0], 0)); | ||
| 311 | - assert_eq!(first, l0(0, [7, 7])); | ||
| 312 | - } | ||
| 313 | - | ||
| 314 | - #[test] | ||
| 315 | - fn temporal_fills_the_second_slot() { | ||
| 316 | - // A1 gives one predictor, so the collocated vector is the second. The collocated | ||
| 317 | - // picture is POC 8 with a vector toward POC 4 (td = 4), and the target is POC 4 | ||
| 318 | - // (tb = 4): no scaling. | ||
| 319 | - let mut col_field = MotionField::new(64, 64); | ||
| 320 | - col_field.slices.push(SliceRefs { | ||
| 321 | - poc: [vec![4], vec![]], | ||
| 322 | - long_term: [vec![false], vec![]], | ||
| 323 | - }); | ||
| 324 | - col_field.store(32, 32, 16, 16, 0, l0(0, [-9, 5])); | ||
| 325 | - let col = test_shared(&col_field, 8); | ||
| 326 | - let mut field = MotionField::new(64, 64); | ||
| 327 | - field.store(0, 16, 16, 16, 0, l0(0, [1, 1])); | ||
| 328 | - let got = run( | ||
| 329 | - &mut field, | ||
| 330 | - &lists(4, false), | ||
| 331 | - 8, | ||
| 332 | - Some(col), | ||
| 333 | - true, | ||
| 334 | - &pu([0, 0], 1), | ||
| 335 | - ); | ||
| 336 | - assert_eq!(got, l0(0, [-9, 5])); | ||
| 337 | - } | ||
| 338 | - | ||
| 339 | - #[test] | ||
| 340 | - fn mv_wraps_at_sixteen_bits() { | ||
| 341 | - // mvp 32000 plus mvd 1000 is 33000, which wraps to 33000 - 65536 = -32536. | ||
| 342 | - // mvp -32000 plus mvd -1000 is -33000, which wraps to 32536. | ||
| 343 | - assert_eq!(wrap16(33000), -32536); | ||
| 344 | - assert_eq!(wrap16(-33000), 32536); | ||
| 345 | - // The boundaries: 32767 stays, 32768 becomes -32768. | ||
| 346 | - assert_eq!(wrap16(32767), 32767); | ||
| 347 | - assert_eq!(wrap16(32768), -32768); | ||
| 348 | - assert_eq!(wrap16(-32768), -32768); | ||
| 349 | - assert_eq!(wrap16(-32769), 32767); | ||
| 350 | - } | ||
| 351 | - | ||
| 352 | - #[test] | ||
| 353 | - fn mvd_wrap_through_the_full_derivation() { | ||
| 354 | - let mut field = MotionField::new(64, 64); | ||
| 355 | - field.store(0, 16, 16, 16, 0, l0(0, [32000, -32000])); | ||
| 356 | - let got = run( | ||
| 357 | - &mut field, | ||
| 358 | - &lists(4, false), | ||
| 359 | - 8, | ||
| 360 | - None, | ||
| 361 | - false, | ||
| 362 | - &pu([1000, -1000], 0), | ||
| 363 | - ); | ||
| 364 | - assert_eq!(got, l0(0, [-32536, 32536])); | ||
| 365 | - } | ||
| 366 | - | ||
| 367 | - #[test] | ||
| 368 | - fn scale_mv_with_negative_and_positive_distances() { | ||
| 369 | - // tb / td = 1/2 with td > 0: half the vector. | ||
| 370 | - assert_eq!(scale_mv([64, 0], 4, 2), [32, 0]); | ||
| 371 | - // td < 0, tb > 0: the vector flips. | ||
| 372 | - assert_eq!(scale_mv([64, 0], -4, 4), [-64, 0]); | ||
| 373 | - // Both negative, ratio 1: the vector is unchanged. | ||
| 374 | - assert_eq!(scale_mv([-30, 7], -4, -4), [-30, 7]); | ||
| 375 | - // The result is clipped to 16 bits. | ||
| 376 | - assert_eq!(scale_mv([32767, 0], 1, 127), [32767, 0]); | ||
| 377 | - } | ||
| 378 | -} | ||
crates/ec-h265/src/dec/mc.rsmodified+0 -4
| @@ -474,7 +474,3 @@ pub(super) fn check_out(dst: &PlaneMut<'_>, blk: Block) -> Result<()> { | |||
| 474 | } | 474 | } |
| 475 | check_depth(dst.bit_depth) | 475 | check_depth(dst.bit_depth) |
| 476 | } | 476 | } |
| 477 | - | ||
| 478 | -#[cfg(test)] | ||
| 479 | -#[path = "mc_tests.rs"] | ||
| 480 | -mod tests; | ||
crates/ec-h265/src/dec/mc_tests.rsdeleted+0 -1137
| @@ -1,1137 +0,0 @@ | |||
| 1 | -//! Tests of the motion compensation: hand-computed values for every fraction, | ||
| 2 | -//! edge clamping, weighted prediction, a per-sample reference model compared on | ||
| 3 | -//! random blocks, and a 1080p timing run. | ||
| 4 | - | ||
| 5 | -use crate::dec::cu_syntax::Motion; | ||
| 6 | -use crate::dec::mc::{ | ||
| 7 | - Block, McScratch, PlaneMut, PlaneRef, RefPicture, interp_chroma, interp_luma, predict_pu, | ||
| 8 | -}; | ||
| 9 | -use crate::dec::weighted::{ExplicitWeights, Weight, store}; | ||
| 10 | -use ec_h265_syntax::{PredWeightTable, WeightEntry}; | ||
| 11 | - | ||
| 12 | -/// A plane of `w x h` samples from `f(x, y)`, stride `w`. | ||
| 13 | -fn plane_from(w: usize, h: usize, bd: u32, f: impl Fn(usize, usize) -> u16) -> Vec<u16> { | ||
| 14 | - (0..h) | ||
| 15 | - .flat_map(|y| (0..w).map(move |x| (x, y))) | ||
| 16 | - .map(|(x, y)| f(x, y)) | ||
| 17 | - .map(|v| v.min((1 << bd) - 1)) | ||
| 18 | - .collect() | ||
| 19 | -} | ||
| 20 | - | ||
| 21 | -fn view(samples: &[u16], w: usize, h: usize, bd: u32) -> PlaneRef<'_> { | ||
| 22 | - PlaneRef { | ||
| 23 | - samples, | ||
| 24 | - stride: w, | ||
| 25 | - width: w, | ||
| 26 | - height: h, | ||
| 27 | - bit_depth: bd, | ||
| 28 | - } | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -fn motion(pred_flags: [bool; 2], ref_idx: [i8; 2], mv: [[i32; 2]; 2]) -> Motion { | ||
| 32 | - Motion { | ||
| 33 | - pred_flags, | ||
| 34 | - ref_idx, | ||
| 35 | - mv, | ||
| 36 | - } | ||
| 37 | -} | ||
| 38 | - | ||
| 39 | -/// A 16x16 luma plane that is zero except for `value` at (8, 8). | ||
| 40 | -fn impulse16(value: u16) -> Vec<u16> { | ||
| 41 | - plane_from(16, 16, 10, |x, y| if (x, y) == (8, 8) { value } else { 0 }) | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -#[test] | ||
| 45 | -fn luma_impulse_fractions_8bit_hand_values() { | ||
| 46 | - // Impulse of 100 at column 8; the block starts at column 4 on row 8, so output | ||
| 47 | - // j reads the impulse with tap index 7 - j. shift1 = 0 at 8 bits. | ||
| 48 | - let p = impulse16(100); | ||
| 49 | - let pl = view(&p, 16, 16, 8); | ||
| 50 | - let blk = Block { | ||
| 51 | - x: 4, | ||
| 52 | - y: 8, | ||
| 53 | - width: 4, | ||
| 54 | - height: 1, | ||
| 55 | - }; | ||
| 56 | - let mut scratch = McScratch::new(); | ||
| 57 | - let mut out = [0i32; 4]; | ||
| 58 | - let cases: [(i32, [i32; 4]); 3] = [ | ||
| 59 | - (1, [0, 100, -500, 1700]), | ||
| 60 | - (2, [-100, 400, -1100, 4000]), | ||
| 61 | - (3, [-100, 400, -1000, 5800]), | ||
| 62 | - ]; | ||
| 63 | - for (frac, want) in cases { | ||
| 64 | - interp_luma(&pl, blk, [frac, 0], &mut scratch, &mut out).unwrap(); | ||
| 65 | - assert_eq!(out, want, "luma horizontal fraction {frac}"); | ||
| 66 | - } | ||
| 67 | - // Full-sample position: the impulse shifted by shift3 = 14 - 8 = 6. | ||
| 68 | - let full = Block { | ||
| 69 | - x: 8, | ||
| 70 | - y: 8, | ||
| 71 | - width: 1, | ||
| 72 | - height: 1, | ||
| 73 | - }; | ||
| 74 | - interp_luma(&pl, full, [0, 0], &mut scratch, &mut out).unwrap(); | ||
| 75 | - assert_eq!(out[0], 100 << 6); | ||
| 76 | -} | ||
| 77 | - | ||
| 78 | -#[test] | ||
| 79 | -fn luma_impulse_fractions_10bit_hand_values() { | ||
| 80 | - // shift1 = 2 at 10 bits, so each tap sum is divided by 4 (exact here). | ||
| 81 | - let p = impulse16(1000); | ||
| 82 | - let pl = view(&p, 16, 16, 10); | ||
| 83 | - let blk = Block { | ||
| 84 | - x: 4, | ||
| 85 | - y: 8, | ||
| 86 | - width: 4, | ||
| 87 | - height: 1, | ||
| 88 | - }; | ||
| 89 | - let mut scratch = McScratch::new(); | ||
| 90 | - let mut out = [0i32; 4]; | ||
| 91 | - let cases: [(i32, [i32; 4]); 3] = [ | ||
| 92 | - (1, [0, 250, -1250, 4250]), | ||
| 93 | - (2, [-250, 1000, -2750, 10000]), | ||
| 94 | - (3, [-250, 1000, -2500, 14500]), | ||
| 95 | - ]; | ||
| 96 | - for (frac, want) in cases { | ||
| 97 | - interp_luma(&pl, blk, [frac, 0], &mut scratch, &mut out).unwrap(); | ||
| 98 | - assert_eq!(out, want, "10-bit luma horizontal fraction {frac}"); | ||
| 99 | - } | ||
| 100 | - let full = Block { | ||
| 101 | - x: 8, | ||
| 102 | - y: 8, | ||
| 103 | - width: 1, | ||
| 104 | - height: 1, | ||
| 105 | - }; | ||
| 106 | - interp_luma(&pl, full, [0, 0], &mut scratch, &mut out).unwrap(); | ||
| 107 | - assert_eq!(out[0], 1000 << 4, "10-bit full sample shift3 = 4"); | ||
| 108 | -} | ||
| 109 | - | ||
| 110 | -#[test] | ||
| 111 | -fn luma_vertical_only_matches_horizontal_by_symmetry() { | ||
| 112 | - let p = impulse16(100); | ||
| 113 | - let pl = view(&p, 16, 16, 8); | ||
| 114 | - // Block column 8, rows 4..8: output row k reads the impulse at tap 7 - k. | ||
| 115 | - let blk = Block { | ||
| 116 | - x: 8, | ||
| 117 | - y: 4, | ||
| 118 | - width: 1, | ||
| 119 | - height: 4, | ||
| 120 | - }; | ||
| 121 | - let mut scratch = McScratch::new(); | ||
| 122 | - let mut out = [0i32; 4]; | ||
| 123 | - interp_luma(&pl, blk, [0, 1], &mut scratch, &mut out).unwrap(); | ||
| 124 | - assert_eq!(out, [0, 100, -500, 1700]); | ||
| 125 | - interp_luma(&pl, blk, [0, 3], &mut scratch, &mut out).unwrap(); | ||
| 126 | - assert_eq!(out, [-100, 400, -1000, 5800]); | ||
| 127 | -} | ||
| 128 | - | ||
| 129 | -#[test] | ||
| 130 | -fn luma_two_dimensional_hand_values() { | ||
| 131 | - // 10 bits, impulse 4096 at (8, 8), block at (4, 4), fractions (2, 2). | ||
| 132 | - // Horizontal pass on the impulse row gives tmp = [-1024, 4096, -11264, 40960] | ||
| 133 | - // (shift1 = 2), and the vertical pass with shift2 = 6 yields the 4x4 block. | ||
| 134 | - // Built raw: plane_from would clamp the impulse to the 10-bit range. | ||
| 135 | - let mut p = vec![0u16; 16 * 16]; | ||
| 136 | - p[8 * 16 + 8] = 4096; | ||
| 137 | - let pl = view(&p, 16, 16, 10); | ||
| 138 | - let blk = Block { | ||
| 139 | - x: 4, | ||
| 140 | - y: 4, | ||
| 141 | - width: 4, | ||
| 142 | - height: 4, | ||
| 143 | - }; | ||
| 144 | - let mut scratch = McScratch::new(); | ||
| 145 | - let mut out = [0i32; 16]; | ||
| 146 | - interp_luma(&pl, blk, [2, 2], &mut scratch, &mut out).unwrap(); | ||
| 147 | - assert_eq!( | ||
| 148 | - out, | ||
| 149 | - [ | ||
| 150 | - 16, -64, 176, -640, // | ||
| 151 | - -64, 256, -704, 2560, // | ||
| 152 | - 176, -704, 1936, -7040, // | ||
| 153 | - -640, 2560, -7040, 25600, | ||
| 154 | - ] | ||
| 155 | - ); | ||
| 156 | -} | ||
| 157 | - | ||
| 158 | -#[test] | ||
| 159 | -fn chroma_impulse_fractions_hand_values() { | ||
| 160 | - // 8 bits, impulse 100 at (8, 8) in the chroma plane; block at column 5 (so | ||
| 161 | - // output j reads tap 4 - j), the four taps that land in range being j = 1..=3. | ||
| 162 | - let p = plane_from(16, 16, 8, |x, y| if (x, y) == (8, 8) { 100 } else { 0 }); | ||
| 163 | - let pl = view(&p, 16, 16, 8); | ||
| 164 | - let blk = Block { | ||
| 165 | - x: 5, | ||
| 166 | - y: 8, | ||
| 167 | - width: 4, | ||
| 168 | - height: 1, | ||
| 169 | - }; | ||
| 170 | - let mut scratch = McScratch::new(); | ||
| 171 | - let mut out = [0i32; 4]; | ||
| 172 | - let cases: [(i32, [i32; 4]); 7] = [ | ||
| 173 | - (1, [0, -200, 1000, 5800]), | ||
| 174 | - (2, [0, -200, 1600, 5400]), | ||
| 175 | - (3, [0, -400, 2800, 4600]), | ||
| 176 | - (4, [0, -400, 3600, 3600]), | ||
| 177 | - (5, [0, -600, 4600, 2800]), | ||
| 178 | - (6, [0, -400, 5400, 1600]), | ||
| 179 | - (7, [0, -200, 5800, 1000]), | ||
| 180 | - ]; | ||
| 181 | - for (frac, want) in cases { | ||
| 182 | - interp_chroma(&pl, blk, [frac, 0], &mut scratch, &mut out).unwrap(); | ||
| 183 | - assert_eq!(out, want, "chroma horizontal eighth {frac}"); | ||
| 184 | - } | ||
| 185 | - let full = Block { | ||
| 186 | - x: 8, | ||
| 187 | - y: 8, | ||
| 188 | - width: 1, | ||
| 189 | - height: 1, | ||
| 190 | - }; | ||
| 191 | - interp_chroma(&pl, full, [0, 0], &mut scratch, &mut out).unwrap(); | ||
| 192 | - assert_eq!(out[0], 100 << 6); | ||
| 193 | -} | ||
| 194 | - | ||
| 195 | -#[test] | ||
| 196 | -fn chroma_motion_vector_splits_integer_and_eighth() { | ||
| 197 | - // A chroma mv of 8 + 3 eighths moves one whole sample plus the eighth-3 filter. | ||
| 198 | - let p = plane_from(16, 16, 8, |x, y| if (x, y) == (8, 8) { 100 } else { 0 }); | ||
| 199 | - let pl = view(&p, 16, 16, 8); | ||
| 200 | - let mut scratch = McScratch::new(); | ||
| 201 | - let mut a = [0i32; 4]; | ||
| 202 | - let mut b = [0i32; 4]; | ||
| 203 | - let start = Block { | ||
| 204 | - x: 4, | ||
| 205 | - y: 8, | ||
| 206 | - width: 4, | ||
| 207 | - height: 1, | ||
| 208 | - }; | ||
| 209 | - interp_chroma(&pl, start, [11, 0], &mut scratch, &mut a).unwrap(); | ||
| 210 | - let shifted = Block { | ||
| 211 | - x: 5, | ||
| 212 | - y: 8, | ||
| 213 | - width: 4, | ||
| 214 | - height: 1, | ||
| 215 | - }; | ||
| 216 | - interp_chroma(&pl, shifted, [3, 0], &mut scratch, &mut b).unwrap(); | ||
| 217 | - assert_eq!(a, b); | ||
| 218 | -} | ||
| 219 | - | ||
| 220 | -#[test] | ||
| 221 | -fn luma_edges_clamp_at_all_four_borders() { | ||
| 222 | - // v(x, y) = 10 * y + x on an 8x8 plane: every border has distinct values. | ||
| 223 | - let p = plane_from(8, 8, 8, |x, y| (10 * y + x) as u16); | ||
| 224 | - let pl = view(&p, 8, 8, 8); | ||
| 225 | - let mut scratch = McScratch::new(); | ||
| 226 | - let mut out = [0i32; 16]; | ||
| 227 | - // Far left: every column reads column 0, so each row is v(0, y) << 6. | ||
| 228 | - let blk = Block { | ||
| 229 | - x: 0, | ||
| 230 | - y: 0, | ||
| 231 | - width: 4, | ||
| 232 | - height: 4, | ||
| 233 | - }; | ||
| 234 | - interp_luma(&pl, blk, [-4000, 0], &mut scratch, &mut out).unwrap(); | ||
| 235 | - for r in 0..4 { | ||
| 236 | - for c in 0..4 { | ||
| 237 | - assert_eq!(out[r * 4 + c], (10 * r as i32) << 6, "far left"); | ||
| 238 | - } | ||
| 239 | - } | ||
| 240 | - // Far right: every column reads column 7. | ||
| 241 | - let blk = Block { | ||
| 242 | - x: 4, | ||
| 243 | - y: 0, | ||
| 244 | - width: 4, | ||
| 245 | - height: 4, | ||
| 246 | - }; | ||
| 247 | - interp_luma(&pl, blk, [4000, 0], &mut scratch, &mut out).unwrap(); | ||
| 248 | - for r in 0..4 { | ||
| 249 | - for c in 0..4 { | ||
| 250 | - assert_eq!(out[r * 4 + c], (10 * r as i32 + 7) << 6, "far right"); | ||
| 251 | - } | ||
| 252 | - } | ||
| 253 | - // Far up: every row reads row 0, so each column is v(x, 0) = x. | ||
| 254 | - let blk = Block { | ||
| 255 | - x: 4, | ||
| 256 | - y: 0, | ||
| 257 | - width: 4, | ||
| 258 | - height: 4, | ||
| 259 | - }; | ||
| 260 | - interp_luma(&pl, blk, [0, -4000], &mut scratch, &mut out).unwrap(); | ||
| 261 | - for r in 0..4 { | ||
| 262 | - for c in 0..4 { | ||
| 263 | - assert_eq!(out[r * 4 + c], (4 + c as i32) << 6, "far up"); | ||
| 264 | - } | ||
| 265 | - } | ||
| 266 | - // Far down: every row reads row 7. | ||
| 267 | - let blk = Block { | ||
| 268 | - x: 4, | ||
| 269 | - y: 4, | ||
| 270 | - width: 4, | ||
| 271 | - height: 4, | ||
| 272 | - }; | ||
| 273 | - interp_luma(&pl, blk, [0, 4000], &mut scratch, &mut out).unwrap(); | ||
| 274 | - for r in 0..4 { | ||
| 275 | - for c in 0..4 { | ||
| 276 | - assert_eq!(out[r * 4 + c], ((70 + 4 + c) as i32) << 6, "far down"); | ||
| 277 | - } | ||
| 278 | - } | ||
| 279 | - // Far outside in both directions, to each corner. | ||
| 280 | - let corners = [ | ||
| 281 | - (0, 0, -4000, -4000, 0), | ||
| 282 | - (4, 0, 4000, -4000, 7), | ||
| 283 | - (0, 4, -4000, 4000, 70), | ||
| 284 | - (4, 4, 4000, 4000, 77), | ||
| 285 | - ]; | ||
| 286 | - for (x, y, mx, my, want) in corners { | ||
| 287 | - let blk = Block { | ||
| 288 | - x, | ||
| 289 | - y, | ||
| 290 | - width: 4, | ||
| 291 | - height: 4, | ||
| 292 | - }; | ||
| 293 | - interp_luma(&pl, blk, [mx, my], &mut scratch, &mut out).unwrap(); | ||
| 294 | - assert!(out.iter().all(|&v| v == want << 6), "corner {want}"); | ||
| 295 | - } | ||
| 296 | -} | ||
| 297 | - | ||
| 298 | -#[test] | ||
| 299 | -fn luma_fractional_edges_clamp_to_the_border_sample() { | ||
| 300 | - // A fractional mv far outside reads one border column (or row) only, so the | ||
| 301 | - // filter sums to 64 times that border sample. | ||
| 302 | - let p = plane_from(8, 8, 8, |x, y| (10 * y + x) as u16); | ||
| 303 | - let pl = view(&p, 8, 8, 8); | ||
| 304 | - let mut scratch = McScratch::new(); | ||
| 305 | - let mut out = [0i32; 16]; | ||
| 306 | - let blk = Block { | ||
| 307 | - x: 0, | ||
| 308 | - y: 0, | ||
| 309 | - width: 4, | ||
| 310 | - height: 4, | ||
| 311 | - }; | ||
| 312 | - // -4001 quarter samples: integer part -1001, fraction 3. | ||
| 313 | - interp_luma(&pl, blk, [-4001, 0], &mut scratch, &mut out).unwrap(); | ||
| 314 | - for r in 0..4 { | ||
| 315 | - assert!( | ||
| 316 | - out[r * 4..r * 4 + 4] | ||
| 317 | - .iter() | ||
| 318 | - .all(|&v| v == 64 * 10 * r as i32) | ||
| 319 | - ); | ||
| 320 | - } | ||
| 321 | - interp_luma(&pl, blk, [0, -4001], &mut scratch, &mut out).unwrap(); | ||
| 322 | - for c in 0..4 { | ||
| 323 | - assert!((0..4).all(|r| out[r * 4 + c] == 64 * c as i32)); | ||
| 324 | - } | ||
| 325 | -} | ||
| 326 | - | ||
| 327 | -#[test] | ||
| 328 | -fn chroma_edges_clamp_at_all_four_borders() { | ||
| 329 | - let p = plane_from(8, 8, 8, |x, y| (10 * y + x) as u16); | ||
| 330 | - let pl = view(&p, 8, 8, 8); | ||
| 331 | - let mut scratch = McScratch::new(); | ||
| 332 | - let mut out = [0i32; 4]; | ||
| 333 | - let blk = Block { | ||
| 334 | - x: 0, | ||
| 335 | - y: 0, | ||
| 336 | - width: 2, | ||
| 337 | - height: 2, | ||
| 338 | - }; | ||
| 339 | - // Eighth-sample mv of -8001: integer part -1001, fraction 7. | ||
| 340 | - interp_chroma(&pl, blk, [-8001, 0], &mut scratch, &mut out).unwrap(); | ||
| 341 | - assert_eq!(out, [0, 0, 640, 640]); | ||
| 342 | - let blk = Block { | ||
| 343 | - x: 6, | ||
| 344 | - y: 6, | ||
| 345 | - width: 2, | ||
| 346 | - height: 2, | ||
| 347 | - }; | ||
| 348 | - interp_chroma(&pl, blk, [8000, 8000], &mut scratch, &mut out).unwrap(); | ||
| 349 | - assert!(out.iter().all(|&v| v == 77 << 6)); | ||
| 350 | -} | ||
| 351 | - | ||
| 352 | -#[test] | ||
| 353 | -fn default_uni_and_bi_hand_values() { | ||
| 354 | - let mut buf = [0u16; 4]; | ||
| 355 | - let blk = Block { | ||
| 356 | - x: 0, | ||
| 357 | - y: 0, | ||
| 358 | - width: 4, | ||
| 359 | - height: 1, | ||
| 360 | - }; | ||
| 361 | - // 8 bits: shift1 = 6 for uni, shift2 = 7 for bi. | ||
| 362 | - let mut dst = PlaneMut { | ||
| 363 | - samples: &mut buf, | ||
| 364 | - stride: 4, | ||
| 365 | - bit_depth: 8, | ||
| 366 | - origin: 0, | ||
| 367 | - }; | ||
| 368 | - store( | ||
| 369 | - &mut dst, | ||
| 370 | - blk, | ||
| 371 | - [Some(&[6400, -100, 16320, 16384]), None], | ||
| 372 | - None, | ||
| 373 | - ) | ||
| 374 | - .unwrap(); | ||
| 375 | - assert_eq!(buf, [100, 0, 255, 255]); | ||
| 376 | - | ||
| 377 | - let mut buf = [0u16; 2]; | ||
| 378 | - let blk2 = Block { | ||
| 379 | - x: 0, | ||
| 380 | - y: 0, | ||
| 381 | - width: 2, | ||
| 382 | - height: 1, | ||
| 383 | - }; | ||
| 384 | - let mut dst = PlaneMut { | ||
| 385 | - samples: &mut buf, | ||
| 386 | - stride: 2, | ||
| 387 | - bit_depth: 8, | ||
| 388 | - origin: 0, | ||
| 389 | - }; | ||
| 390 | - store( | ||
| 391 | - &mut dst, | ||
| 392 | - blk2, | ||
| 393 | - [Some(&[6400, 6400]), Some(&[6400, 6464])], | ||
| 394 | - None, | ||
| 395 | - ) | ||
| 396 | - .unwrap(); | ||
| 397 | - assert_eq!(buf, [100, 101]); | ||
| 398 | - | ||
| 399 | - // 10 bits: shift1 = 4 for uni, shift2 = 5 for bi. | ||
| 400 | - let mut buf = [0u16; 3]; | ||
| 401 | - let blk3 = Block { | ||
| 402 | - x: 0, | ||
| 403 | - y: 0, | ||
| 404 | - width: 3, | ||
| 405 | - height: 1, | ||
| 406 | - }; | ||
| 407 | - let mut dst = PlaneMut { | ||
| 408 | - samples: &mut buf, | ||
| 409 | - stride: 3, | ||
| 410 | - bit_depth: 10, | ||
| 411 | - origin: 0, | ||
| 412 | - }; | ||
| 413 | - store(&mut dst, blk3, [Some(&[6400, 16368, 16384]), None], None).unwrap(); | ||
| 414 | - assert_eq!(buf, [400, 1023, 1023]); | ||
| 415 | - let mut dst = PlaneMut { | ||
| 416 | - samples: &mut buf, | ||
| 417 | - stride: 3, | ||
| 418 | - bit_depth: 10, | ||
| 419 | - origin: 0, | ||
| 420 | - }; | ||
| 421 | - store( | ||
| 422 | - &mut dst, | ||
| 423 | - blk3, | ||
| 424 | - [Some(&[6400, 0, 0]), Some(&[12800, 0, 0])], | ||
| 425 | - None, | ||
| 426 | - ) | ||
| 427 | - .unwrap(); | ||
| 428 | - assert_eq!(buf[0], 600); | ||
| 429 | -} | ||
| 430 | - | ||
| 431 | -#[test] | ||
| 432 | -fn explicit_uni_and_bi_hand_values_8bit() { | ||
| 433 | - let blk = Block { | ||
| 434 | - x: 0, | ||
| 435 | - y: 0, | ||
| 436 | - width: 1, | ||
| 437 | - height: 1, | ||
| 438 | - }; | ||
| 439 | - let mut buf = [0u16; 1]; | ||
| 440 | - // denom 2, w = 3, o = 5: ((6400*3 + 128) >> 8) + 5 = 75 + 5. | ||
| 441 | - let mut dst = PlaneMut { | ||
| 442 | - samples: &mut buf, | ||
| 443 | - stride: 1, | ||
| 444 | - bit_depth: 8, | ||
| 445 | - origin: 0, | ||
| 446 | - }; | ||
| 447 | - let w = [Weight { w: 3, o: 5 }, Weight::default()]; | ||
| 448 | - store(&mut dst, blk, [Some(&[6400]), None], Some((2, w))).unwrap(); | ||
| 449 | - assert_eq!(buf[0], 80); | ||
| 450 | - | ||
| 451 | - // Negative weight -1 and offset 200: ((-6400 + 128) >> 8) + 200 = -25 + 200. | ||
| 452 | - let w = [Weight { w: -1, o: 200 }, Weight::default()]; | ||
| 453 | - let mut dst = PlaneMut { | ||
| 454 | - samples: &mut buf, | ||
| 455 | - stride: 1, | ||
| 456 | - bit_depth: 8, | ||
| 457 | - origin: 0, | ||
| 458 | - }; | ||
| 459 | - store(&mut dst, blk, [Some(&[6400]), None], Some((2, w))).unwrap(); | ||
| 460 | - assert_eq!(buf[0], 175); | ||
| 461 | - | ||
| 462 | - // Negative offset clipping at zero: w = -1, o = -200 gives -225 -> 0. | ||
| 463 | - let w = [Weight { w: -1, o: -200 }, Weight::default()]; | ||
| 464 | - let mut dst = PlaneMut { | ||
| 465 | - samples: &mut buf, | ||
| 466 | - stride: 1, | ||
| 467 | - bit_depth: 8, | ||
| 468 | - origin: 0, | ||
| 469 | - }; | ||
| 470 | - store(&mut dst, blk, [Some(&[6400]), None], Some((2, w))).unwrap(); | ||
| 471 | - assert_eq!(buf[0], 0); | ||
| 472 | - | ||
| 473 | - // Bi: w0 = -2, o0 = -3 with p0 = 6400; w1 = 6, o1 = 7 with p1 = 12800. | ||
| 474 | - // (-12800 + 76800 + ((-3 + 7 + 1) << 8)) >> 9 = 127. | ||
| 475 | - let w = [Weight { w: -2, o: -3 }, Weight { w: 6, o: 7 }]; | ||
| 476 | - let mut dst = PlaneMut { | ||
| 477 | - samples: &mut buf, | ||
| 478 | - stride: 1, | ||
| 479 | - bit_depth: 8, | ||
| 480 | - origin: 0, | ||
| 481 | - }; | ||
| 482 | - store(&mut dst, blk, [Some(&[6400]), Some(&[12800])], Some((2, w))).unwrap(); | ||
| 483 | - assert_eq!(buf[0], 127); | ||
| 484 | -} | ||
| 485 | - | ||
| 486 | -#[test] | ||
| 487 | -fn explicit_uni_hand_value_10bit_with_scaled_offset() { | ||
| 488 | - // The caller scales offsets by 1 << (BitDepth - 8); 5 << 2 = 20. | ||
| 489 | - // shift1 = 4 at 10 bits, log2WD = 2 + 4 = 6: ((6400*3 + 32) >> 6) + 20 = 300 + 20. | ||
| 490 | - let blk = Block { | ||
| 491 | - x: 0, | ||
| 492 | - y: 0, | ||
| 493 | - width: 1, | ||
| 494 | - height: 1, | ||
| 495 | - }; | ||
| 496 | - let mut buf = [0u16; 1]; | ||
| 497 | - let mut dst = PlaneMut { | ||
| 498 | - samples: &mut buf, | ||
| 499 | - stride: 1, | ||
| 500 | - bit_depth: 10, | ||
| 501 | - origin: 0, | ||
| 502 | - }; | ||
| 503 | - let w = [Weight { w: 3, o: 20 }, Weight::default()]; | ||
| 504 | - store(&mut dst, blk, [Some(&[6400]), None], Some((2, w))).unwrap(); | ||
| 505 | - assert_eq!(buf[0], 320); | ||
| 506 | -} | ||
| 507 | - | ||
| 508 | -#[test] | ||
| 509 | -fn weight_table_derivation_with_chroma_offsets() { | ||
| 510 | - // luma denom 2, chroma denom 3. Entry 0: luma delta -1 offset 5; chroma deltas | ||
| 511 | - // 3 and -2 with the ChromaOffset values the parser already derived (-7, 9). | ||
| 512 | - // Entry 1 has no flags, so every weight is the default and offsets are 0. | ||
| 513 | - let table = PredWeightTable { | ||
| 514 | - luma_log2_weight_denom: 2, | ||
| 515 | - delta_chroma_log2_weight_denom: 1, | ||
| 516 | - l0: vec![ | ||
| 517 | - WeightEntry { | ||
| 518 | - luma: Some((-1, 5)), | ||
| 519 | - chroma: Some([(3, -7), (-2, 9)]), | ||
| 520 | - }, | ||
| 521 | - WeightEntry::default(), | ||
| 522 | - ], | ||
| 523 | - l1: vec![], | ||
| 524 | - }; | ||
| 525 | - let ew = ExplicitWeights::from_table(&table, 8, 8, false).unwrap(); | ||
| 526 | - assert_eq!(ew.log2_denom, [2, 3, 3]); | ||
| 527 | - assert_eq!( | ||
| 528 | - ew.entries[0][0], | ||
| 529 | - [ | ||
| 530 | - Weight { w: 3, o: 5 }, | ||
| 531 | - Weight { w: 11, o: -7 }, | ||
| 532 | - Weight { w: 6, o: 9 }, | ||
| 533 | - ] | ||
| 534 | - ); | ||
| 535 | - assert_eq!( | ||
| 536 | - ew.entries[0][1], | ||
| 537 | - [ | ||
| 538 | - Weight { w: 4, o: 0 }, | ||
| 539 | - Weight { w: 8, o: 0 }, | ||
| 540 | - Weight { w: 8, o: 0 } | ||
| 541 | - ] | ||
| 542 | - ); | ||
| 543 | - // At 10 bits every offset is scaled by 4. | ||
| 544 | - let ew10 = ExplicitWeights::from_table(&table, 10, 10, false).unwrap(); | ||
| 545 | - assert_eq!(ew10.entries[0][0][0], Weight { w: 3, o: 20 }); | ||
| 546 | - assert_eq!(ew10.entries[0][0][1].o, -28); | ||
| 547 | - assert_eq!(ew10.entries[0][0][2].o, 36); | ||
| 548 | - // A coded weight delta outside -128..=127 is corrupt. | ||
| 549 | - let bad = PredWeightTable { | ||
| 550 | - luma_log2_weight_denom: 0, | ||
| 551 | - delta_chroma_log2_weight_denom: 0, | ||
| 552 | - l0: vec![WeightEntry { | ||
| 553 | - luma: Some((200, 0)), | ||
| 554 | - chroma: None, | ||
| 555 | - }], | ||
| 556 | - l1: vec![], | ||
| 557 | - }; | ||
| 558 | - assert!(ExplicitWeights::from_table(&bad, 8, 8, false).is_err()); | ||
| 559 | -} | ||
| 560 | - | ||
| 561 | -#[test] | ||
| 562 | -fn predict_pu_constant_planes_recover_the_value() { | ||
| 563 | - // A constant picture predicts itself for any motion and any weighting that is | ||
| 564 | - // the identity: default bi of 100 and 100 gives 100. | ||
| 565 | - let bd = 8; | ||
| 566 | - let luma = plane_from(64, 64, bd, |_, _| 100); | ||
| 567 | - let chroma = plane_from(32, 32, bd, |_, _| 100); | ||
| 568 | - let pic: RefPicture<'_> = [ | ||
| 569 | - view(&luma, 64, 64, bd), | ||
| 570 | - view(&chroma, 32, 32, bd), | ||
| 571 | - view(&chroma, 32, 32, bd), | ||
| 572 | - ]; | ||
| 573 | - let refs = [&[pic][..], &[pic][..]]; | ||
| 574 | - let mut oy = vec![0u16; 64 * 64]; | ||
| 575 | - let mut ou = vec![0u16; 32 * 32]; | ||
| 576 | - let mut ov = vec![0u16; 32 * 32]; | ||
| 577 | - let mut out = [ | ||
| 578 | - PlaneMut { | ||
| 579 | - samples: &mut oy, | ||
| 580 | - stride: 64, | ||
| 581 | - bit_depth: bd, | ||
| 582 | - origin: 0, | ||
| 583 | - }, | ||
| 584 | - PlaneMut { | ||
| 585 | - samples: &mut ou, | ||
| 586 | - stride: 32, | ||
| 587 | - bit_depth: bd, | ||
| 588 | - origin: 0, | ||
| 589 | - }, | ||
| 590 | - PlaneMut { | ||
| 591 | - samples: &mut ov, | ||
| 592 | - stride: 32, | ||
| 593 | - bit_depth: bd, | ||
| 594 | - origin: 0, | ||
| 595 | - }, | ||
| 596 | - ]; | ||
| 597 | - let mut scratch = McScratch::new(); | ||
| 598 | - let pu = Block { | ||
| 599 | - x: 16, | ||
| 600 | - y: 8, | ||
| 601 | - width: 8, | ||
| 602 | - height: 4, | ||
| 603 | - }; | ||
| 604 | - let m = motion([true, true], [0, 0], [[-13, 7], [22, -5]]); | ||
| 605 | - predict_pu(refs, &m, None, pu, &mut out, &mut scratch).unwrap(); | ||
| 606 | - for (plane, stride, w, h, x, y) in [ | ||
| 607 | - (&oy, 64, 8, 4, 16, 8), | ||
| 608 | - (&ou, 32, 4, 2, 8, 4), | ||
| 609 | - (&ov, 32, 4, 2, 8, 4), | ||
| 610 | - ] { | ||
| 611 | - for r in 0..h { | ||
| 612 | - for c in 0..w { | ||
| 613 | - assert_eq!(plane[(y + r) * stride + x + c], 100); | ||
| 614 | - } | ||
| 615 | - } | ||
| 616 | - } | ||
| 617 | -} | ||
| 618 | - | ||
| 619 | -#[test] | ||
| 620 | -fn predict_pu_rejects_bad_input() { | ||
| 621 | - let bd = 8; | ||
| 622 | - let luma = plane_from(16, 16, bd, |_, _| 1); | ||
| 623 | - let chroma = plane_from(8, 8, bd, |_, _| 1); | ||
| 624 | - let pic: RefPicture<'_> = [ | ||
| 625 | - view(&luma, 16, 16, bd), | ||
| 626 | - view(&chroma, 8, 8, bd), | ||
| 627 | - view(&chroma, 8, 8, bd), | ||
| 628 | - ]; | ||
| 629 | - let refs = [&[pic][..], &[][..]]; | ||
| 630 | - let mut oy = vec![0u16; 16 * 16]; | ||
| 631 | - let mut ou = vec![0u16; 8 * 8]; | ||
| 632 | - let mut ov = vec![0u16; 8 * 8]; | ||
| 633 | - let mut out = [ | ||
| 634 | - PlaneMut { | ||
| 635 | - samples: &mut oy, | ||
| 636 | - stride: 16, | ||
| 637 | - bit_depth: bd, | ||
| 638 | - origin: 0, | ||
| 639 | - }, | ||
| 640 | - PlaneMut { | ||
| 641 | - samples: &mut ou, | ||
| 642 | - stride: 8, | ||
| 643 | - bit_depth: bd, | ||
| 644 | - origin: 0, | ||
| 645 | - }, | ||
| 646 | - PlaneMut { | ||
| 647 | - samples: &mut ov, | ||
| 648 | - stride: 8, | ||
| 649 | - bit_depth: bd, | ||
| 650 | - origin: 0, | ||
| 651 | - }, | ||
| 652 | - ]; | ||
| 653 | - let mut scratch = McScratch::new(); | ||
| 654 | - let pu = Block { | ||
| 655 | - x: 0, | ||
| 656 | - y: 0, | ||
| 657 | - width: 8, | ||
| 658 | - height: 8, | ||
| 659 | - }; | ||
| 660 | - // No list in use. | ||
| 661 | - let none = motion([false, false], [0, 0], [[0; 2]; 2]); | ||
| 662 | - assert!(predict_pu(refs, &none, None, pu, &mut out, &mut scratch).is_err()); | ||
| 663 | - // refIdxL1 names a picture list 1 does not have. | ||
| 664 | - let l1 = motion([false, true], [0, 0], [[0; 2]; 2]); | ||
| 665 | - assert!(predict_pu(refs, &l1, None, pu, &mut out, &mut scratch).is_err()); | ||
| 666 | - // A rectangle past the plane edge. | ||
| 667 | - let off = Block { | ||
| 668 | - x: 12, | ||
| 669 | - y: 0, | ||
| 670 | - width: 8, | ||
| 671 | - height: 8, | ||
| 672 | - }; | ||
| 673 | - let ok = motion([true, false], [0, 0], [[0; 2]; 2]); | ||
| 674 | - assert!(predict_pu(refs, &ok, None, off, &mut out, &mut scratch).is_err()); | ||
| 675 | -} | ||
| 676 | - | ||
| 677 | -// --------------------------------------------------------------------------- | ||
| 678 | -// Reference model: a deliberately naive transcription of 8.5.3.3 per sample. | ||
| 679 | -// It shares no code with the optimised path and copies the filter tables from | ||
| 680 | -// the spec again, so a transcription error in either shows up as a mismatch. | ||
| 681 | -// --------------------------------------------------------------------------- | ||
| 682 | - | ||
| 683 | -mod naive { | ||
| 684 | - use crate::dec::cu_syntax::Motion; | ||
| 685 | - use crate::dec::mc::{Block, PlaneRef, RefPicture}; | ||
| 686 | - use crate::dec::weighted::ExplicitWeights; | ||
| 687 | - | ||
| 688 | - /// Table 8-11, luma. Row 0 is unused. | ||
| 689 | - const LUMA: [[i64; 8]; 4] = [ | ||
| 690 | - [0, 0, 0, 64, 0, 0, 0, 0], | ||
| 691 | - [-1, 4, -10, 58, 17, -5, 1, 0], | ||
| 692 | - [-1, 4, -11, 40, 40, -11, 4, -1], | ||
| 693 | - [0, 1, -5, 17, 58, -10, 4, -1], | ||
| 694 | - ]; | ||
| 695 | - /// Table 8-12, chroma. Row 0 is unused. | ||
| 696 | - const CHROMA: [[i64; 4]; 8] = [ | ||
| 697 | - [0, 64, 0, 0], | ||
| 698 | - [-2, 58, 10, -2], | ||
| 699 | - [-4, 54, 16, -2], | ||
| 700 | - [-6, 46, 28, -4], | ||
| 701 | - [-4, 36, 36, -4], | ||
| 702 | - [-4, 28, 46, -6], | ||
| 703 | - [-2, 16, 54, -4], | ||
| 704 | - [-2, 10, 58, -2], | ||
| 705 | - ]; | ||
| 706 | - | ||
| 707 | - /// `ref[Clip3(0, W-1, x)][Clip3(0, H-1, y)]`, one sample. | ||
| 708 | - fn sample(p: &PlaneRef<'_>, x: i64, y: i64) -> i64 { | ||
| 709 | - let cx = x.clamp(0, p.width as i64 - 1) as usize; | ||
| 710 | - let cy = y.clamp(0, p.height as i64 - 1) as usize; | ||
| 711 | - i64::from(p.samples[cy * p.stride + cx]) | ||
| 712 | - } | ||
| 713 | - | ||
| 714 | - /// One predSampleLX at integer position (xi, yi) with fractions (fx, fy). | ||
| 715 | - fn interp(p: &PlaneRef<'_>, xi: i64, yi: i64, fx: usize, fy: usize, luma: bool) -> i64 { | ||
| 716 | - let bd = i64::from(p.bit_depth); | ||
| 717 | - let shift1 = (bd - 8).min(4); | ||
| 718 | - let shift2 = 6; | ||
| 719 | - let shift3 = (14 - bd).max(2); | ||
| 720 | - let taps: i64 = if luma { 8 } else { 4 }; | ||
| 721 | - let pad = taps / 2 - 1; | ||
| 722 | - let coef = |frac: usize, i: i64| -> i64 { | ||
| 723 | - if luma { | ||
| 724 | - LUMA[frac][i as usize] | ||
| 725 | - } else { | ||
| 726 | - CHROMA[frac][i as usize] | ||
| 727 | - } | ||
| 728 | - }; | ||
| 729 | - if fx == 0 && fy == 0 { | ||
| 730 | - return sample(p, xi, yi) << shift3; | ||
| 731 | - } | ||
| 732 | - if fy == 0 { | ||
| 733 | - let mut s = 0; | ||
| 734 | - for i in 0..taps { | ||
| 735 | - s += coef(fx, i) * sample(p, xi + i - pad, yi); | ||
| 736 | - } | ||
| 737 | - return s >> shift1; | ||
| 738 | - } | ||
| 739 | - if fx == 0 { | ||
| 740 | - let mut s = 0; | ||
| 741 | - for i in 0..taps { | ||
| 742 | - s += coef(fy, i) * sample(p, xi, yi + i - pad); | ||
| 743 | - } | ||
| 744 | - return s >> shift1; | ||
| 745 | - } | ||
| 746 | - let mut s = 0; | ||
| 747 | - for n in 0..taps { | ||
| 748 | - let mut temp = 0; | ||
| 749 | - for i in 0..taps { | ||
| 750 | - temp += coef(fx, i) * sample(p, xi + i - pad, yi + n - pad); | ||
| 751 | - } | ||
| 752 | - s += coef(fy, n) * (temp >> shift1); | ||
| 753 | - } | ||
| 754 | - s >> shift2 | ||
| 755 | - } | ||
| 756 | - | ||
| 757 | - /// The weighted sample prediction of 8.5.3.3.4 for one sample. | ||
| 758 | - /// `preds[list]` is `Some(predSamplesLX)` for each list in use. | ||
| 759 | - fn weight(bd: i64, preds: [Option<i64>; 2], explicit: Option<(i64, [(i64, i64); 2])>) -> i64 { | ||
| 760 | - let max = (1 << bd) - 1; | ||
| 761 | - let shift1 = 14 - bd; | ||
| 762 | - let clip = |v: i64| v.clamp(0, max); | ||
| 763 | - match (preds, explicit) { | ||
| 764 | - ([Some(p0), Some(p1)], None) => { | ||
| 765 | - let shift2 = 15 - bd; | ||
| 766 | - clip((p0 + p1 + (1 << (shift2 - 1))) >> shift2) | ||
| 767 | - } | ||
| 768 | - ([Some(p0), Some(p1)], Some((denom, [(w0, o0), (w1, o1)]))) => { | ||
| 769 | - let log2wd = denom + shift1; | ||
| 770 | - clip((p0 * w0 + p1 * w1 + ((o0 + o1 + 1) << log2wd)) >> (log2wd + 1)) | ||
| 771 | - } | ||
| 772 | - ([Some(p), None], None) | ([None, Some(p)], None) => { | ||
| 773 | - clip((p + (1 << (shift1 - 1))) >> shift1) | ||
| 774 | - } | ||
| 775 | - ([Some(p), None], Some((denom, [(w, o), _]))) => { | ||
| 776 | - let log2wd = denom + shift1; | ||
| 777 | - clip(if log2wd >= 1 { | ||
| 778 | - ((p * w + (1 << (log2wd - 1))) >> log2wd) + o | ||
| 779 | - } else { | ||
| 780 | - p * w + o | ||
| 781 | - }) | ||
| 782 | - } | ||
| 783 | - ([None, Some(p)], Some((denom, [_, (w, o)]))) => { | ||
| 784 | - let log2wd = denom + shift1; | ||
| 785 | - clip(if log2wd >= 1 { | ||
| 786 | - ((p * w + (1 << (log2wd - 1))) >> log2wd) + o | ||
| 787 | - } else { | ||
| 788 | - p * w + o | ||
| 789 | - }) | ||
| 790 | - } | ||
| 791 | - _ => panic!("naive model: no prediction"), | ||
| 792 | - } | ||
| 793 | - } | ||
| 794 | - | ||
| 795 | - /// The final samples of one PU component, row-major `blk.width * blk.height`. | ||
| 796 | - pub fn predict_component( | ||
| 797 | - refs: [&[RefPicture<'_>]; 2], | ||
| 798 | - motion: &Motion, | ||
| 799 | - weights: Option<&ExplicitWeights>, | ||
| 800 | - blk: Block, | ||
| 801 | - comp: usize, | ||
| 802 | - ) -> Vec<i64> { | ||
| 803 | - let luma = comp == 0; | ||
| 804 | - let bd = i64::from( | ||
| 805 | - refs.iter() | ||
| 806 | - .enumerate() | ||
| 807 | - .find_map(|(l, r)| { | ||
| 808 | - motion.pred_flags[l].then(|| r[motion.ref_idx[l] as usize][comp].bit_depth) | ||
| 809 | - }) | ||
| 810 | - .unwrap(), | ||
| 811 | - ); | ||
| 812 | - let mut out = Vec::with_capacity(blk.width * blk.height); | ||
| 813 | - for r in 0..blk.height { | ||
| 814 | - for c in 0..blk.width { | ||
| 815 | - let mut preds = [None, None]; | ||
| 816 | - let mut wts = [(0i64, 0i64); 2]; | ||
| 817 | - for list in 0..2 { | ||
| 818 | - if !motion.pred_flags[list] { | ||
| 819 | - continue; | ||
| 820 | - } | ||
| 821 | - let pic = &refs[list][motion.ref_idx[list] as usize]; | ||
| 822 | - let p = &pic[comp]; | ||
| 823 | - let mv = motion.mv[list]; | ||
| 824 | - let (xi, yi, fx, fy) = if luma { | ||
| 825 | - ( | ||
| 826 | - blk.x as i64 + i64::from(mv[0] >> 2) + c as i64, | ||
| 827 | - blk.y as i64 + i64::from(mv[1] >> 2) + r as i64, | ||
| 828 | - (mv[0] & 3) as usize, | ||
| 829 | - (mv[1] & 3) as usize, | ||
| 830 | - ) | ||
| 831 | - } else { | ||
| 832 | - ( | ||
| 833 | - blk.x as i64 + i64::from(mv[0] >> 3) + c as i64, | ||
| 834 | - blk.y as i64 + i64::from(mv[1] >> 3) + r as i64, | ||
| 835 | - (mv[0] & 7) as usize, | ||
| 836 | - (mv[1] & 7) as usize, | ||
| 837 | - ) | ||
| 838 | - }; | ||
| 839 | - preds[list] = Some(interp(p, xi, yi, fx, fy, luma)); | ||
| 840 | - if let Some(ew) = weights { | ||
| 841 | - let w = ew.entries[list][motion.ref_idx[list] as usize][comp]; | ||
| 842 | - wts[list] = (i64::from(w.w), i64::from(w.o)); | ||
| 843 | - } | ||
| 844 | - } | ||
| 845 | - let explicit = weights.map(|ew| (i64::from(ew.log2_denom[comp]), wts)); | ||
| 846 | - out.push(weight(bd, preds, explicit)); | ||
| 847 | - } | ||
| 848 | - } | ||
| 849 | - out | ||
| 850 | - } | ||
| 851 | -} | ||
| 852 | - | ||
| 853 | -/// SplitMix64: a fixed-seed generator, so every run sees the same blocks. | ||
| 854 | -struct Rng(u64); | ||
| 855 | - | ||
| 856 | -impl Rng { | ||
| 857 | - fn next_u64(&mut self) -> u64 { | ||
| 858 | - self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); | ||
| 859 | - let mut z = self.0; | ||
| 860 | - z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); | ||
| 861 | - z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); | ||
| 862 | - z ^ (z >> 31) | ||
| 863 | - } | ||
| 864 | - | ||
| 865 | - fn below(&mut self, n: u64) -> u64 { | ||
| 866 | - self.next_u64() % n | ||
| 867 | - } | ||
| 868 | - | ||
| 869 | - fn range(&mut self, lo: i64, hi: i64) -> i64 { | ||
| 870 | - lo + (self.next_u64() % (hi - lo + 1) as u64) as i64 | ||
| 871 | - } | ||
| 872 | -} | ||
| 873 | - | ||
| 874 | -/// An owned picture: Y, Cb, Cr planes at the 4:2:0 sizes of a `w x h` luma picture. | ||
| 875 | -struct Pic { | ||
| 876 | - planes: [Vec<u16>; 3], | ||
| 877 | - w: usize, | ||
| 878 | - h: usize, | ||
| 879 | - bd: u32, | ||
| 880 | -} | ||
| 881 | - | ||
| 882 | -impl Pic { | ||
| 883 | - fn random(rng: &mut Rng, w: usize, h: usize, bd: u32) -> Pic { | ||
| 884 | - let max = (1u64 << bd) - 1; | ||
| 885 | - let mut fill = | ||
| 886 | - |n: usize| -> Vec<u16> { (0..n).map(|_| rng.below(max + 1) as u16).collect() }; | ||
| 887 | - let planes = [fill(w * h), fill(w * h / 4), fill(w * h / 4)]; | ||
| 888 | - Pic { planes, w, h, bd } | ||
| 889 | - } | ||
| 890 | - | ||
| 891 | - fn view(&self) -> RefPicture<'_> { | ||
| 892 | - let dims = [ | ||
| 893 | - (self.w, self.h), | ||
| 894 | - (self.w / 2, self.h / 2), | ||
| 895 | - (self.w / 2, self.h / 2), | ||
| 896 | - ]; | ||
| 897 | - std::array::from_fn(|c| PlaneRef { | ||
| 898 | - samples: &self.planes[c], | ||
| 899 | - stride: dims[c].0, | ||
| 900 | - width: dims[c].0, | ||
| 901 | - height: dims[c].1, | ||
| 902 | - bit_depth: self.bd, | ||
| 903 | - }) | ||
| 904 | - } | ||
| 905 | -} | ||
| 906 | - | ||
| 907 | -#[test] | ||
| 908 | -fn random_blocks_match_the_reference_model() { | ||
| 909 | - let mut rng = Rng(0x5eed_1234_abcd_0001); | ||
| 910 | - let mut scratch = McScratch::new(); | ||
| 911 | - let mut checked = 0usize; | ||
| 912 | - let mut sizes_8x4_or_4x8 = 0usize; | ||
| 913 | - let mut uni = 0usize; | ||
| 914 | - let mut bi = 0usize; | ||
| 915 | - let mut explicit = 0usize; | ||
| 916 | - for case in 0..10_000 { | ||
| 917 | - let bd = if rng.below(2) == 0 { 8 } else { 10 }; | ||
| 918 | - let pw = 8 * rng.range(8, 28) as usize; | ||
| 919 | - let ph = 8 * rng.range(8, 28) as usize; | ||
| 920 | - // Sizes 4..=64 in steps of 4; every eighth case is forced to 8x4 or 4x8. | ||
| 921 | - let (w, h) = if case % 8 == 0 { | ||
| 922 | - if rng.below(2) == 0 { (8, 4) } else { (4, 8) } | ||
| 923 | - } else { | ||
| 924 | - (4 * rng.range(1, 16) as usize, 4 * rng.range(1, 16) as usize) | ||
| 925 | - }; | ||
| 926 | - if (w, h) == (8, 4) || (w, h) == (4, 8) { | ||
| 927 | - sizes_8x4_or_4x8 += 1; | ||
| 928 | - } | ||
| 929 | - let x = 4 * rng.below(((pw - w) / 4 + 1) as u64) as usize; | ||
| 930 | - let y = 4 * rng.below(((ph - h) / 4 + 1) as u64) as usize; | ||
| 931 | - | ||
| 932 | - let lists: [Vec<Pic>; 2] = [ | ||
| 933 | - (0..rng.range(1, 3)) | ||
| 934 | - .map(|_| Pic::random(&mut rng, pw, ph, bd)) | ||
| 935 | - .collect(), | ||
| 936 | - (0..rng.range(1, 3)) | ||
| 937 | - .map(|_| Pic::random(&mut rng, pw, ph, bd)) | ||
| 938 | - .collect(), | ||
| 939 | - ]; | ||
| 940 | - let views: [Vec<RefPicture<'_>>; 2] = [ | ||
| 941 | - lists[0].iter().map(Pic::view).collect(), | ||
| 942 | - lists[1].iter().map(Pic::view).collect(), | ||
| 943 | - ]; | ||
| 944 | - | ||
| 945 | - let pred_flags = match rng.below(3) { | ||
| 946 | - 0 => [true, false], | ||
| 947 | - 1 => [false, true], | ||
| 948 | - _ => [true, true], | ||
| 949 | - }; | ||
| 950 | - if pred_flags[0] && pred_flags[1] { | ||
| 951 | - bi += 1; | ||
| 952 | - } else { | ||
| 953 | - uni += 1; | ||
| 954 | - } | ||
| 955 | - let mut ref_idx = [0i8; 2]; | ||
| 956 | - let mut mv = [[0i32; 2]; 2]; | ||
| 957 | - let reach = 4 * (pw.max(ph) as i64 + 96); | ||
| 958 | - for (list, mv_list) in mv.iter_mut().enumerate() { | ||
| 959 | - ref_idx[list] = rng.below(lists[list].len() as u64) as i8; | ||
| 960 | - *mv_list = std::array::from_fn(|_| { | ||
| 961 | - if rng.below(2) == 0 { | ||
| 962 | - rng.range(-reach, reach) as i32 | ||
| 963 | - } else { | ||
| 964 | - rng.range(-40, 40) as i32 | ||
| 965 | - } | ||
| 966 | - }); | ||
| 967 | - } | ||
| 968 | - let m = Motion { | ||
| 969 | - pred_flags, | ||
| 970 | - ref_idx, | ||
| 971 | - mv, | ||
| 972 | - }; | ||
| 973 | - | ||
| 974 | - let weights = if rng.below(2) == 0 { | ||
| 975 | - None | ||
| 976 | - } else { | ||
| 977 | - explicit += 1; | ||
| 978 | - let shift = bd - 8; | ||
| 979 | - let denom: [u32; 3] = [ | ||
| 980 | - rng.below(8) as u32, | ||
| 981 | - rng.below(8) as u32, | ||
| 982 | - rng.below(8) as u32, | ||
| 983 | - ]; | ||
| 984 | - let mut entries: [Vec<[Weight; 3]>; 2] = [Vec::new(), Vec::new()]; | ||
| 985 | - for (list, slot) in entries.iter_mut().enumerate() { | ||
| 986 | - *slot = (0..lists[list].len()) | ||
| 987 | - .map(|_| { | ||
| 988 | - std::array::from_fn(|_| Weight { | ||
| 989 | - w: rng.range(-128, 127) as i32, | ||
| 990 | - o: (rng.range(-128, 127) as i32) << shift, | ||
| 991 | - }) | ||
| 992 | - }) | ||
| 993 | - .collect(); | ||
| 994 | - } | ||
| 995 | - Some(ExplicitWeights { | ||
| 996 | - log2_denom: denom, | ||
| 997 | - entries, | ||
| 998 | - }) | ||
| 999 | - }; | ||
| 1000 | - | ||
| 1001 | - let pu = Block { | ||
| 1002 | - x, | ||
| 1003 | - y, | ||
| 1004 | - width: w, | ||
| 1005 | - height: h, | ||
| 1006 | - }; | ||
| 1007 | - let mut planes = [ | ||
| 1008 | - vec![0u16; pw * ph], | ||
| 1009 | - vec![0u16; pw * ph / 4], | ||
| 1010 | - vec![0u16; pw * ph / 4], | ||
| 1011 | - ]; | ||
| 1012 | - let dims = [(pw, ph), (pw / 2, ph / 2), (pw / 2, ph / 2)]; | ||
| 1013 | - { | ||
| 1014 | - let [a, b, c] = &mut planes; | ||
| 1015 | - let mut out = [ | ||
| 1016 | - PlaneMut { | ||
| 1017 | - samples: a, | ||
| 1018 | - stride: pw, | ||
| 1019 | - bit_depth: bd, | ||
| 1020 | - origin: 0, | ||
| 1021 | - }, | ||
| 1022 | - PlaneMut { | ||
| 1023 | - samples: b, | ||
| 1024 | - stride: pw / 2, | ||
| 1025 | - bit_depth: bd, | ||
| 1026 | - origin: 0, | ||
| 1027 | - }, | ||
| 1028 | - PlaneMut { | ||
| 1029 | - samples: c, | ||
| 1030 | - stride: pw / 2, | ||
| 1031 | - bit_depth: bd, | ||
| 1032 | - origin: 0, | ||
| 1033 | - }, | ||
| 1034 | - ]; | ||
| 1035 | - let refs = [&views[0][..], &views[1][..]]; | ||
| 1036 | - predict_pu(refs, &m, weights.as_ref(), pu, &mut out, &mut scratch).unwrap(); | ||
| 1037 | - } | ||
| 1038 | - | ||
| 1039 | - let refs = [&views[0][..], &views[1][..]]; | ||
| 1040 | - for comp in 0..3 { | ||
| 1041 | - let blk = if comp == 0 { | ||
| 1042 | - pu | ||
| 1043 | - } else { | ||
| 1044 | - Block { | ||
| 1045 | - x: x / 2, | ||
| 1046 | - y: y / 2, | ||
| 1047 | - width: w / 2, | ||
| 1048 | - height: h / 2, | ||
| 1049 | - } | ||
| 1050 | - }; | ||
| 1051 | - let want = naive::predict_component(refs, &m, weights.as_ref(), blk, comp); | ||
| 1052 | - let stride = dims[comp].0; | ||
| 1053 | - for r in 0..blk.height { | ||
| 1054 | - for c in 0..blk.width { | ||
| 1055 | - let got = i64::from(planes[comp][(blk.y + r) * stride + blk.x + c]); | ||
| 1056 | - assert_eq!( | ||
| 1057 | - got, | ||
| 1058 | - want[r * blk.width + c], | ||
| 1059 | - "case {case} bd {bd} comp {comp} pu {pu:?} mv {mv:?} refs {ref_idx:?} flags {pred_flags:?} r {r} c {c}" | ||
| 1060 | - ); | ||
| 1061 | - } | ||
| 1062 | - } | ||
| 1063 | - } | ||
| 1064 | - checked += 1; | ||
| 1065 | - } | ||
| 1066 | - assert_eq!(checked, 10_000); | ||
| 1067 | - eprintln!( | ||
| 1068 | - "mc reference model: {checked} random PUs agree ({uni} uni, {bi} bi, {explicit} explicit weighted, {sizes_8x4_or_4x8} of 8x4/4x8)" | ||
| 1069 | - ); | ||
| 1070 | -} | ||
| 1071 | - | ||
| 1072 | -/// One 1080p frame of 8x8 bi-predicted PUs, luma and 4:2:0 chroma. Timing only; | ||
| 1073 | -/// run with `--release --nocapture` to see the figure. | ||
| 1074 | -#[test] | ||
| 1075 | -fn bench_1080p_bipred_8x8_frame() { | ||
| 1076 | - let (pw, ph, bd) = (1920usize, 1080usize, 10u32); | ||
| 1077 | - let mut rng = Rng(42); | ||
| 1078 | - let pic0 = Pic::random(&mut rng, pw, ph, bd); | ||
| 1079 | - let pic1 = Pic::random(&mut rng, pw, ph, bd); | ||
| 1080 | - let l0 = [pic0.view()]; | ||
| 1081 | - let l1 = [pic1.view()]; | ||
| 1082 | - let mut planes = [ | ||
| 1083 | - vec![0u16; pw * ph], | ||
| 1084 | - vec![0u16; pw * ph / 4], | ||
| 1085 | - vec![0u16; pw * ph / 4], | ||
| 1086 | - ]; | ||
| 1087 | - let [a, b, c] = &mut planes; | ||
| 1088 | - let mut out = [ | ||
| 1089 | - PlaneMut { | ||
| 1090 | - samples: a, | ||
| 1091 | - stride: pw, | ||
| 1092 | - bit_depth: bd, | ||
| 1093 | - origin: 0, | ||
| 1094 | - }, | ||
| 1095 | - PlaneMut { | ||
| 1096 | - samples: b, | ||
| 1097 | - stride: pw / 2, | ||
| 1098 | - bit_depth: bd, | ||
| 1099 | - origin: 0, | ||
| 1100 | - }, | ||
| 1101 | - PlaneMut { | ||
| 1102 | - samples: c, | ||
| 1103 | - stride: pw / 2, | ||
| 1104 | - bit_depth: bd, | ||
| 1105 | - origin: 0, | ||
| 1106 | - }, | ||
| 1107 | - ]; | ||
| 1108 | - let mut scratch = McScratch::new(); | ||
| 1109 | - let start = std::time::Instant::now(); | ||
| 1110 | - for by in 0..ph / 8 { | ||
| 1111 | - for bx in 0..pw / 8 { | ||
| 1112 | - let m = Motion { | ||
| 1113 | - pred_flags: [true, true], | ||
| 1114 | - ref_idx: [0, 0], | ||
| 1115 | - mv: [ | ||
| 1116 | - [(bx as i32 * 7 % 61) - 30, (by as i32 * 5 % 53) - 26], | ||
| 1117 | - [(by as i32 * 3 % 47) - 23, (bx as i32 * 11 % 59) - 29], | ||
| 1118 | - ], | ||
| 1119 | - }; | ||
| 1120 | - let pu = Block { | ||
| 1121 | - x: bx * 8, | ||
| 1122 | - y: by * 8, | ||
| 1123 | - width: 8, | ||
| 1124 | - height: 8, | ||
| 1125 | - }; | ||
| 1126 | - predict_pu([&l0, &l1], &m, None, pu, &mut out, &mut scratch).unwrap(); | ||
| 1127 | - } | ||
| 1128 | - } | ||
| 1129 | - let elapsed = start.elapsed(); | ||
| 1130 | - let checksum: u64 = planes.iter().flatten().map(|&v| u64::from(v)).sum(); | ||
| 1131 | - assert!(checksum > 0); | ||
| 1132 | - eprintln!( | ||
| 1133 | - "bench: 1080p frame, {} bi-pred 8x8 PUs (luma + 4:2:0 chroma): {:.2} ms (checksum {checksum})", | ||
| 1134 | - (pw / 8) * (ph / 8), | ||
| 1135 | - elapsed.as_secs_f64() * 1e3 | ||
| 1136 | - ); | ||
| 1137 | -} | ||
crates/ec-h265/src/dec/merge.rsmodified+0 -362
| @@ -193,365 +193,3 @@ fn zero_candidate(is_b: bool, ref_idx: usize) -> PbMotion { | |||
| 193 | PbMotion::new([true, false], [r, -1], [[0, 0], [0, 0]]) | 193 | PbMotion::new([true, false], [r, -1], [[0, 0], [0, 0]]) |
| 194 | } | 194 | } |
| 195 | } | 195 | } |
| 196 | - | ||
| 197 | -#[cfg(test)] | ||
| 198 | -mod tests { | ||
| 199 | - use super::super::cu_syntax::InterPredIdc; | ||
| 200 | - use super::super::motion_field::{MotionField, MotionParams, SliceRefs, test_shared}; | ||
| 201 | - use super::super::rps::{RefEntry, RefPicLists}; | ||
| 202 | - use super::super::shared::SharedPic; | ||
| 203 | - use super::*; | ||
| 204 | - use std::sync::Arc; | ||
| 205 | - | ||
| 206 | - fn entry(id: u32, poc: i32, long_term: bool) -> RefEntry { | ||
| 207 | - RefEntry { id, poc, long_term } | ||
| 208 | - } | ||
| 209 | - | ||
| 210 | - fn params(is_b: bool, max: usize, par: u32) -> MotionParams { | ||
| 211 | - MotionParams { | ||
| 212 | - is_b, | ||
| 213 | - max_merge_cand: max, | ||
| 214 | - log2_par_mrg_level: par, | ||
| 215 | - ctb_log2: 6, | ||
| 216 | - collocated_from_l0: true, | ||
| 217 | - temporal_mvp: false, | ||
| 218 | - no_backward_pred: false, | ||
| 219 | - } | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | - fn uni(list: usize, ref_idx: i8, mv: [i16; 2]) -> PbMotion { | ||
| 223 | - let mut pred = [false, false]; | ||
| 224 | - pred[list] = true; | ||
| 225 | - let mut r = [-1, -1]; | ||
| 226 | - r[list] = ref_idx; | ||
| 227 | - let mut m = [[0i16; 2]; 2]; | ||
| 228 | - m[list] = mv; | ||
| 229 | - PbMotion::new(pred, r, m) | ||
| 230 | - } | ||
| 231 | - | ||
| 232 | - fn pu_at(x: usize, y: usize, w: usize, h: usize, merge_idx: u8) -> PredictionUnit { | ||
| 233 | - PredictionUnit { | ||
| 234 | - x, | ||
| 235 | - y, | ||
| 236 | - width: w, | ||
| 237 | - height: h, | ||
| 238 | - merge_flag: true, | ||
| 239 | - merge_idx, | ||
| 240 | - inter_pred_idc: InterPredIdc::PredL0, | ||
| 241 | - ..PredictionUnit::default() | ||
| 242 | - } | ||
| 243 | - } | ||
| 244 | - | ||
| 245 | - fn cu(x: usize, y: usize, size: usize, part: PartMode) -> CuGeometry { | ||
| 246 | - CuGeometry { | ||
| 247 | - x, | ||
| 248 | - y, | ||
| 249 | - size, | ||
| 250 | - part_mode: part, | ||
| 251 | - skip: false, | ||
| 252 | - } | ||
| 253 | - } | ||
| 254 | - | ||
| 255 | - fn p_lists() -> RefPicLists { | ||
| 256 | - RefPicLists { | ||
| 257 | - l0: vec![entry(0, 0, false)], | ||
| 258 | - l1: Vec::new(), | ||
| 259 | - } | ||
| 260 | - } | ||
| 261 | - | ||
| 262 | - /// The merge result for one PU, in a 64x64 picture with `field` as the current | ||
| 263 | - /// motion field and `col` as the collocated picture. | ||
| 264 | - fn merge_with( | ||
| 265 | - field: &mut MotionField, | ||
| 266 | - lists: &RefPicLists, | ||
| 267 | - p: MotionParams, | ||
| 268 | - col: Option<Arc<SharedPic>>, | ||
| 269 | - geom: CuGeometry, | ||
| 270 | - pu: PredictionUnit, | ||
| 271 | - pu_index: usize, | ||
| 272 | - ) -> PbMotion { | ||
| 273 | - let src = SliceMotion { | ||
| 274 | - params: p, | ||
| 275 | - lists, | ||
| 276 | - cur_poc: 4, | ||
| 277 | - field, | ||
| 278 | - col, | ||
| 279 | - publish: None, | ||
| 280 | - slice: 0, | ||
| 281 | - }; | ||
| 282 | - derive_merge(&src, &geom, pu_index, &pu) | ||
| 283 | - } | ||
| 284 | - | ||
| 285 | - #[test] | ||
| 286 | - fn no_neighbours_gives_zero_candidate() { | ||
| 287 | - let mut field = MotionField::new(64, 64); | ||
| 288 | - let got = merge_with( | ||
| 289 | - &mut field, | ||
| 290 | - &p_lists(), | ||
| 291 | - params(false, 5, 2), | ||
| 292 | - None, | ||
| 293 | - cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 294 | - pu_at(16, 16, 16, 16, 0), | ||
| 295 | - 0, | ||
| 296 | - ); | ||
| 297 | - assert_eq!(got, uni(0, 0, [0, 0])); | ||
| 298 | - } | ||
| 299 | - | ||
| 300 | - #[test] | ||
| 301 | - fn a1_is_first_and_intra_or_missing_neighbour_is_skipped() { | ||
| 302 | - let mut field = MotionField::new(64, 64); | ||
| 303 | - // A1 of the 16x16 block at (16,16) is (15,31): store motion there. | ||
| 304 | - let m = uni(0, 0, [12, -7]); | ||
| 305 | - field.store(0, 16, 16, 16, 0, m); | ||
| 306 | - // B1 (31,15) stays missing (intra or not decoded). | ||
| 307 | - let got = merge_with( | ||
| 308 | - &mut field, | ||
| 309 | - &p_lists(), | ||
| 310 | - params(false, 5, 2), | ||
| 311 | - None, | ||
| 312 | - cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 313 | - pu_at(16, 16, 16, 16, 0), | ||
| 314 | - 0, | ||
| 315 | - ); | ||
| 316 | - assert_eq!(got, m); | ||
| 317 | - } | ||
| 318 | - | ||
| 319 | - #[test] | ||
| 320 | - fn neighbour_in_other_slice_is_unavailable() { | ||
| 321 | - let mut field = MotionField::new(64, 64); | ||
| 322 | - field.store(0, 16, 16, 16, 1, uni(0, 0, [12, -7])); | ||
| 323 | - let got = merge_with( | ||
| 324 | - &mut field, | ||
| 325 | - &p_lists(), | ||
| 326 | - params(false, 5, 2), | ||
| 327 | - None, | ||
| 328 | - cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 329 | - pu_at(16, 16, 16, 16, 0), | ||
| 330 | - 0, | ||
| 331 | - ); | ||
| 332 | - assert_eq!(got, uni(0, 0, [0, 0])); | ||
| 333 | - } | ||
| 334 | - | ||
| 335 | - #[test] | ||
| 336 | - fn b1_identical_to_a1_is_pruned() { | ||
| 337 | - let mut field = MotionField::new(64, 64); | ||
| 338 | - let a = uni(0, 0, [1, 2]); | ||
| 339 | - let n = uni(0, 0, [5, 6]); | ||
| 340 | - field.store(0, 16, 16, 16, 0, a); // A1 | ||
| 341 | - field.store(16, 0, 16, 16, 0, a); // B1 (31,15): same as A1, pruned | ||
| 342 | - field.store(32, 0, 16, 16, 0, n); // B0 (32,15) | ||
| 343 | - // merge_idx 1 is B0, since B1 is pruned. | ||
| 344 | - let got = merge_with( | ||
| 345 | - &mut field, | ||
| 346 | - &p_lists(), | ||
| 347 | - params(false, 5, 2), | ||
| 348 | - None, | ||
| 349 | - cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 350 | - pu_at(16, 16, 16, 16, 1), | ||
| 351 | - 0, | ||
| 352 | - ); | ||
| 353 | - assert_eq!(got, n); | ||
| 354 | - } | ||
| 355 | - | ||
| 356 | - #[test] | ||
| 357 | - fn parallel_merge_level_hides_neighbours_in_the_same_region() { | ||
| 358 | - // Log2ParMrgLevel 4 (16x16 regions). The 8x8 CU at (8,8) shares one list | ||
| 359 | - // (singleMCLFlag), so A1 (7,15) and B1 (15,7) are in its own region and gone. | ||
| 360 | - let mut field = MotionField::new(64, 64); | ||
| 361 | - field.store(0, 8, 8, 8, 0, uni(0, 0, [9, 9])); // A1 position, same region | ||
| 362 | - field.store(8, 0, 8, 8, 0, uni(0, 0, [8, 8])); // B1 position, same region | ||
| 363 | - let far = uni(0, 0, [-3, 4]); | ||
| 364 | - field.store(16, 0, 8, 8, 0, far); // B0 (16,7), another region | ||
| 365 | - let got = merge_with( | ||
| 366 | - &mut field, | ||
| 367 | - &p_lists(), | ||
| 368 | - params(false, 5, 4), | ||
| 369 | - None, | ||
| 370 | - cu(8, 8, 8, PartMode::Part2Nx2N), | ||
| 371 | - pu_at(8, 8, 8, 8, 0), | ||
| 372 | - 0, | ||
| 373 | - ); | ||
| 374 | - assert_eq!(got, far); | ||
| 375 | - } | ||
| 376 | - | ||
| 377 | - #[test] | ||
| 378 | - fn parallel_merge_level_two_uses_the_neighbour() { | ||
| 379 | - // The same layout with Log2ParMrgLevel 2: nothing is in the same region, so A1 is used. | ||
| 380 | - let mut field = MotionField::new(64, 64); | ||
| 381 | - let a = uni(0, 0, [9, 9]); | ||
| 382 | - field.store(0, 8, 8, 8, 0, a); | ||
| 383 | - field.store(8, 0, 8, 8, 0, uni(0, 0, [8, 8])); | ||
| 384 | - let got = merge_with( | ||
| 385 | - &mut field, | ||
| 386 | - &p_lists(), | ||
| 387 | - params(false, 5, 2), | ||
| 388 | - None, | ||
| 389 | - cu(8, 8, 8, PartMode::Part2Nx2N), | ||
| 390 | - pu_at(8, 8, 8, 8, 0), | ||
| 391 | - 0, | ||
| 392 | - ); | ||
| 393 | - assert_eq!(got, a); | ||
| 394 | - } | ||
| 395 | - | ||
| 396 | - #[test] | ||
| 397 | - fn second_vertical_partition_skips_a1() { | ||
| 398 | - // Nx2N, partIdx 1 at (x+8,y): A1 is the first partition, so it is not a candidate. | ||
| 399 | - let mut field = MotionField::new(64, 64); | ||
| 400 | - field.store(16, 16, 8, 16, 0, uni(0, 0, [1, 1])); // the first partition | ||
| 401 | - let got = merge_with( | ||
| 402 | - &mut field, | ||
| 403 | - &p_lists(), | ||
| 404 | - params(false, 5, 2), | ||
| 405 | - None, | ||
| 406 | - cu(16, 16, 16, PartMode::PartNx2N), | ||
| 407 | - pu_at(24, 16, 8, 16, 0), | ||
| 408 | - 1, | ||
| 409 | - ); | ||
| 410 | - assert_eq!(got, uni(0, 0, [0, 0])); | ||
| 411 | - } | ||
| 412 | - | ||
| 413 | - #[test] | ||
| 414 | - fn temporal_candidate_scales_with_negative_distance() { | ||
| 415 | - // Current POC 2; the collocated picture is POC 8 and its vector points to POC 4. | ||
| 416 | - // The current L0[0] is POC 6, after the current picture: tb = -4, td = 4. | ||
| 417 | - // mv 64: tx = (16384 + 2) / 4 = 4096, dsf = (-4 * 4096 + 32) >> 6 = -256, | ||
| 418 | - // mv' = Sign(-256 * 64) * ((16384 + 127) >> 8) = -64. | ||
| 419 | - let mut col_field = MotionField::new(64, 64); | ||
| 420 | - col_field.slices.push(SliceRefs { | ||
| 421 | - poc: [vec![4], vec![]], | ||
| 422 | - long_term: [vec![false], vec![]], | ||
| 423 | - }); | ||
| 424 | - col_field.store(32, 32, 16, 16, 0, uni(0, 0, [64, 0])); | ||
| 425 | - let col = test_shared(&col_field, 8); | ||
| 426 | - let lists = RefPicLists { | ||
| 427 | - l0: vec![entry(0, 6, false)], | ||
| 428 | - l1: Vec::new(), | ||
| 429 | - }; | ||
| 430 | - let mut field = MotionField::new(64, 64); | ||
| 431 | - let mut p = params(false, 5, 2); | ||
| 432 | - p.temporal_mvp = true; | ||
| 433 | - let src = SliceMotion { | ||
| 434 | - params: p, | ||
| 435 | - lists: &lists, | ||
| 436 | - cur_poc: 2, | ||
| 437 | - field: &mut field, | ||
| 438 | - col: Some(col), | ||
| 439 | - publish: None, | ||
| 440 | - slice: 0, | ||
| 441 | - }; | ||
| 442 | - // A 16x16 block at (16,16): bottom-right (32,32) is inside the picture and CTB row. | ||
| 443 | - let got = derive_merge( | ||
| 444 | - &src, | ||
| 445 | - &cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 446 | - 0, | ||
| 447 | - &pu_at(16, 16, 16, 16, 0), | ||
| 448 | - ); | ||
| 449 | - assert_eq!(got, uni(0, 0, [-64, 0])); | ||
| 450 | - } | ||
| 451 | - | ||
| 452 | - #[test] | ||
| 453 | - fn temporal_candidate_with_long_term_mismatch_is_unavailable() { | ||
| 454 | - // The current L0[0] is long-term, the collocated reference is short-term: | ||
| 455 | - // the candidate is unavailable and the zero candidate is used. | ||
| 456 | - let mut col_field = MotionField::new(64, 64); | ||
| 457 | - col_field.slices.push(SliceRefs { | ||
| 458 | - poc: [vec![4], vec![]], | ||
| 459 | - long_term: [vec![false], vec![]], | ||
| 460 | - }); | ||
| 461 | - col_field.store(32, 32, 16, 16, 0, uni(0, 0, [64, 0])); | ||
| 462 | - let col = test_shared(&col_field, 8); | ||
| 463 | - let lists = RefPicLists { | ||
| 464 | - l0: vec![entry(0, 0, true)], | ||
| 465 | - l1: Vec::new(), | ||
| 466 | - }; | ||
| 467 | - let mut field = MotionField::new(64, 64); | ||
| 468 | - let mut p = params(false, 5, 2); | ||
| 469 | - p.temporal_mvp = true; | ||
| 470 | - let src = SliceMotion { | ||
| 471 | - params: p, | ||
| 472 | - lists: &lists, | ||
| 473 | - cur_poc: 4, | ||
| 474 | - field: &mut field, | ||
| 475 | - col: Some(col), | ||
| 476 | - publish: None, | ||
| 477 | - slice: 0, | ||
| 478 | - }; | ||
| 479 | - let got = derive_merge( | ||
| 480 | - &src, | ||
| 481 | - &cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 482 | - 0, | ||
| 483 | - &pu_at(16, 16, 16, 16, 0), | ||
| 484 | - ); | ||
| 485 | - assert_eq!(got, uni(0, 0, [0, 0])); | ||
| 486 | - } | ||
| 487 | - | ||
| 488 | - #[test] | ||
| 489 | - fn combined_bi_candidate_from_l0_and_l1_neighbours() { | ||
| 490 | - // A1 is L0-only (POC 0), B1 is L1-only (POC 6): they pair into a bi candidate. | ||
| 491 | - let lists = RefPicLists { | ||
| 492 | - l0: vec![entry(0, 0, false)], | ||
| 493 | - l1: vec![entry(1, 6, false)], | ||
| 494 | - }; | ||
| 495 | - let mut field = MotionField::new(64, 64); | ||
| 496 | - let m0 = uni(0, 0, [2, 2]); | ||
| 497 | - let m1 = uni(1, 0, [-5, 9]); | ||
| 498 | - field.store(0, 16, 16, 16, 0, m0); // A1 | ||
| 499 | - field.store(16, 0, 16, 16, 0, m1); // B1 (31,15) | ||
| 500 | - let got = merge_with( | ||
| 501 | - &mut field, | ||
| 502 | - &lists, | ||
| 503 | - params(true, 5, 2), | ||
| 504 | - None, | ||
| 505 | - cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 506 | - pu_at(16, 16, 16, 16, 2), | ||
| 507 | - 0, | ||
| 508 | - ); | ||
| 509 | - let expect = PbMotion::new([true, true], [0, 0], [[2, 2], [-5, 9]]); | ||
| 510 | - assert_eq!(got, expect); | ||
| 511 | - } | ||
| 512 | - | ||
| 513 | - #[test] | ||
| 514 | - fn bi_candidate_becomes_uni_for_an_8x4_block() { | ||
| 515 | - // A B slice whose A1 is bi-predicted: an 8x4 block keeps only L0. | ||
| 516 | - let lists = RefPicLists { | ||
| 517 | - l0: vec![entry(0, 0, false)], | ||
| 518 | - l1: vec![entry(1, 8, false)], | ||
| 519 | - }; | ||
| 520 | - let mut field = MotionField::new(64, 64); | ||
| 521 | - let bi = PbMotion::new([true, true], [0, 0], [[3, -3], [4, 4]]); | ||
| 522 | - field.store(0, 16, 16, 16, 0, bi); // A1 of an 8x4 PU at (16,16) | ||
| 523 | - let got = merge_with( | ||
| 524 | - &mut field, | ||
| 525 | - &lists, | ||
| 526 | - params(true, 5, 2), | ||
| 527 | - None, | ||
| 528 | - cu(16, 16, 8, PartMode::Part2Nx2N), | ||
| 529 | - pu_at(16, 16, 8, 4, 0), | ||
| 530 | - 0, | ||
| 531 | - ); | ||
| 532 | - assert_eq!( | ||
| 533 | - got, | ||
| 534 | - PbMotion::new([true, false], [0, -1], [[3, -3], [0, 0]]) | ||
| 535 | - ); | ||
| 536 | - } | ||
| 537 | - | ||
| 538 | - #[test] | ||
| 539 | - fn zero_candidates_use_increasing_reference_indices() { | ||
| 540 | - // Two L0 references and merge_idx 1 with no spatial candidates: zero ref 1. | ||
| 541 | - let lists = RefPicLists { | ||
| 542 | - l0: vec![entry(0, 0, false), entry(1, 2, false)], | ||
| 543 | - l1: Vec::new(), | ||
| 544 | - }; | ||
| 545 | - let mut field = MotionField::new(64, 64); | ||
| 546 | - let got = merge_with( | ||
| 547 | - &mut field, | ||
| 548 | - &lists, | ||
| 549 | - params(false, 5, 2), | ||
| 550 | - None, | ||
| 551 | - cu(16, 16, 16, PartMode::Part2Nx2N), | ||
| 552 | - pu_at(16, 16, 16, 16, 1), | ||
| 553 | - 0, | ||
| 554 | - ); | ||
| 555 | - assert_eq!(got, uni(0, 1, [0, 0])); | ||
| 556 | - } | ||
| 557 | -} | ||
crates/ec-h265/src/dec/motion_field.rsmodified+0 -10
| @@ -133,14 +133,6 @@ pub struct MotionField { | |||
| 133 | words: Vec<u64>, | 133 | words: Vec<u64>, |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | -/// The collocated view of a finished test field, as a picture of its own. | ||
| 137 | -#[cfg(test)] | ||
| 138 | -pub fn test_shared(field: &MotionField, poc: i32) -> Arc<SharedPic> { | ||
| 139 | - let shared = SharedPic::new(poc, field.width(), field.height(), (8, 8), false, 1); | ||
| 140 | - shared.publish_col(field, field.height()); | ||
| 141 | - Arc::new(shared) | ||
| 142 | -} | ||
| 143 | - | ||
| 144 | impl MotionField { | 136 | impl MotionField { |
| 145 | /// An empty field for a picture of `width` by `height` luma samples. | 137 | /// An empty field for a picture of `width` by `height` luma samples. |
| 146 | pub fn new(width: usize, height: usize) -> MotionField { | 138 | pub fn new(width: usize, height: usize) -> MotionField { |
| @@ -586,9 +578,7 @@ impl<'a> SliceMotion<'a> { | |||
| 586 | // Both candidates lie in the CTB row of `y` (the bottom-right one only when it | 578 | // Both candidates lie in the CTB row of `y` (the bottom-right one only when it |
| 587 | // stays in that row), so the collocated rows up to the end of that row suffice. | 579 | // stays in that row), so the collocated rows up to the end of that row suffice. |
| 588 | let need = (((y >> ctb) + 1) << ctb).min(self.field.height()); | 580 | let need = (((y >> ctb) + 1) << ctb).min(self.field.height()); |
| 589 | - let t = super::trace::now(); | ||
| 590 | let waited = shared.wait_col_rows(need); | 581 | let waited = shared.wait_col_rows(need); |
| 591 | - super::trace::since(super::trace::Phase::WaitCol, t); | ||
| 592 | waited.ok()?; | 582 | waited.ok()?; |
| 593 | let col = shared.read_col(); | 583 | let col = shared.read_col(); |
| 594 | if (y >> ctb) == (ybr >> ctb) && ybr < self.field.height() && xbr < self.field.width() { | 584 | if (y >> ctb) == (ybr >> ctb) && ybr < self.field.height() && xbr < self.field.width() { |
crates/ec-h265/src/residual.rsmodified+0 -258
| @@ -1618,262 +1618,4 @@ mod tests { | |||
| 1618 | } | 1618 | } |
| 1619 | } | 1619 | } |
| 1620 | } | 1620 | } |
| 1621 | - | ||
| 1622 | - /// [`rdoq`], but every trial re-derives the whole block's bits from `base` | ||
| 1623 | - /// (the pre-change behaviour) instead of using the sub-block frozen-prefix | ||
| 1624 | - /// cursor. Kept only for | ||
| 1625 | - /// [`sub_block_cursor_matches_full_recompute`] to check the cursor against. | ||
| 1626 | - #[allow(clippy::too_many_arguments)] | ||
| 1627 | - fn rdoq_full_recompute( | ||
| 1628 | - coeffs: &[i32], | ||
| 1629 | - levels: &mut [i32], | ||
| 1630 | - n: usize, | ||
| 1631 | - qp: i32, | ||
| 1632 | - bit_depth: u32, | ||
| 1633 | - c_idx: usize, | ||
| 1634 | - scan_idx: usize, | ||
| 1635 | - cbf_ctx: usize, | ||
| 1636 | - lambda: f64, | ||
| 1637 | - transform_skip: Option<bool>, | ||
| 1638 | - enc: &mut CabacEncoder, | ||
| 1639 | - ) -> usize { | ||
| 1640 | - let log2_size = n.trailing_zeros(); | ||
| 1641 | - let scale = coeff_ssd_scale(n); | ||
| 1642 | - let lambda = RDOQ_LAMBDA * lambda; | ||
| 1643 | - let base = enc.snapshot(); | ||
| 1644 | - let err = |level: i32, coeff: i32| { | ||
| 1645 | - let d = f64::from(dequant_level_bd(level, n, qp, bit_depth) - coeff); | ||
| 1646 | - d * d | ||
| 1647 | - }; | ||
| 1648 | - | ||
| 1649 | - let mut dist: f64 = (0..n * n).map(|i| err(levels[i], coeffs[i])).sum(); | ||
| 1650 | - let bits = levels_bits( | ||
| 1651 | - enc, | ||
| 1652 | - &base, | ||
| 1653 | - levels, | ||
| 1654 | - log2_size, | ||
| 1655 | - c_idx, | ||
| 1656 | - scan_idx, | ||
| 1657 | - cbf_ctx, | ||
| 1658 | - true, | ||
| 1659 | - transform_skip, | ||
| 1660 | - ); | ||
| 1661 | - let mut cost = scale * dist + lambda * bits; | ||
| 1662 | - | ||
| 1663 | - let sub_scan = scan(log2_size - 2, scan_idx); | ||
| 1664 | - let pos_scan = scan(2, scan_idx); | ||
| 1665 | - let order: Vec<usize> = sub_scan | ||
| 1666 | - .iter() | ||
| 1667 | - .flat_map(|&(xs, ys)| { | ||
| 1668 | - pos_scan.iter().map(move |&(xp, yp)| { | ||
| 1669 | - (ys as usize * 4 + yp as usize) * n + xs as usize * 4 + xp as usize | ||
| 1670 | - }) | ||
| 1671 | - }) | ||
| 1672 | - .collect(); | ||
| 1673 | - | ||
| 1674 | - let sub_wide = n >> 2; | ||
| 1675 | - let mut csbf = [false; 64]; | ||
| 1676 | - let mut sub_count = [0i32; 64]; | ||
| 1677 | - let mut nz_positions: Vec<i32> = Vec::new(); | ||
| 1678 | - for (k, &i) in order.iter().enumerate().rev() { | ||
| 1679 | - if levels[i] != 0 { | ||
| 1680 | - nz_positions.push(k as i32); | ||
| 1681 | - let sub_idx = ((i / n) >> 2) * sub_wide + ((i % n) >> 2); | ||
| 1682 | - sub_count[sub_idx] += 1; | ||
| 1683 | - csbf[sub_idx] = true; | ||
| 1684 | - } | ||
| 1685 | - } | ||
| 1686 | - let mut cur_last = nz_positions.first().copied().unwrap_or(0); | ||
| 1687 | - let mut nz_idx = 0usize; | ||
| 1688 | - | ||
| 1689 | - for (k, &i) in order.iter().enumerate().rev() { | ||
| 1690 | - let level = levels[i]; | ||
| 1691 | - if level == 0 { | ||
| 1692 | - continue; | ||
| 1693 | - } | ||
| 1694 | - let next_last = nz_positions.get(nz_idx + 1).copied().unwrap_or(0); | ||
| 1695 | - nz_idx += 1; | ||
| 1696 | - if level.abs() > RDOQ_MAX_LEVEL { | ||
| 1697 | - continue; | ||
| 1698 | - } | ||
| 1699 | - let sub_idx = ((i / n) >> 2) * sub_wide + ((i % n) >> 2); | ||
| 1700 | - let mut tried = level.abs(); | ||
| 1701 | - for candidate in [level.abs() - 1, 0] { | ||
| 1702 | - if candidate == tried { | ||
| 1703 | - continue; | ||
| 1704 | - } | ||
| 1705 | - tried = candidate; | ||
| 1706 | - let keep = levels[i]; | ||
| 1707 | - levels[i] = candidate * level.signum(); | ||
| 1708 | - let now_zero = levels[i] == 0; | ||
| 1709 | - let sub_count_before = sub_count[sub_idx]; | ||
| 1710 | - let csbf_before = csbf[sub_idx]; | ||
| 1711 | - if now_zero { | ||
| 1712 | - sub_count[sub_idx] -= 1; | ||
| 1713 | - csbf[sub_idx] = sub_count[sub_idx] > 0; | ||
| 1714 | - } | ||
| 1715 | - let trial_last = if now_zero && k as i32 == cur_last { | ||
| 1716 | - next_last | ||
| 1717 | - } else { | ||
| 1718 | - cur_last | ||
| 1719 | - }; | ||
| 1720 | - let trial_dist = dist - err(keep, coeffs[i]) + err(levels[i], coeffs[i]); | ||
| 1721 | - let any = sub_count.iter().any(|&c| c > 0); | ||
| 1722 | - let trial_bits = levels_bits_with( | ||
| 1723 | - enc, | ||
| 1724 | - &base, | ||
| 1725 | - levels, | ||
| 1726 | - log2_size, | ||
| 1727 | - c_idx, | ||
| 1728 | - scan_idx, | ||
| 1729 | - cbf_ctx, | ||
| 1730 | - any, | ||
| 1731 | - transform_skip, | ||
| 1732 | - trial_last, | ||
| 1733 | - csbf, | ||
| 1734 | - ); | ||
| 1735 | - let trial = scale * trial_dist + lambda * trial_bits; | ||
| 1736 | - if trial < cost { | ||
| 1737 | - cost = trial; | ||
| 1738 | - dist = trial_dist; | ||
| 1739 | - if now_zero { | ||
| 1740 | - cur_last = trial_last; | ||
| 1741 | - } | ||
| 1742 | - } else { | ||
| 1743 | - levels[i] = keep; | ||
| 1744 | - sub_count[sub_idx] = sub_count_before; | ||
| 1745 | - csbf[sub_idx] = csbf_before; | ||
| 1746 | - } | ||
| 1747 | - } | ||
| 1748 | - } | ||
| 1749 | - let zero_dist: f64 = (0..n * n).map(|i| err(0, coeffs[i])).sum(); | ||
| 1750 | - let zero_bits = levels_bits( | ||
| 1751 | - enc, | ||
| 1752 | - &base, | ||
| 1753 | - levels, | ||
| 1754 | - log2_size, | ||
| 1755 | - c_idx, | ||
| 1756 | - scan_idx, | ||
| 1757 | - cbf_ctx, | ||
| 1758 | - false, | ||
| 1759 | - transform_skip, | ||
| 1760 | - ); | ||
| 1761 | - if scale * zero_dist + lambda * zero_bits <= cost { | ||
| 1762 | - levels[..n * n].fill(0); | ||
| 1763 | - enc.restore(&base); | ||
| 1764 | - return 0; | ||
| 1765 | - } | ||
| 1766 | - enc.restore(&base); | ||
| 1767 | - levels[..n * n].iter().filter(|&&l| l != 0).count() | ||
| 1768 | - } | ||
| 1769 | - | ||
| 1770 | - /// A tiny xorshift RNG — deterministic across runs, no extra dependency. | ||
| 1771 | - struct Xorshift(u32); | ||
| 1772 | - impl Xorshift { | ||
| 1773 | - fn next(&mut self) -> u32 { | ||
| 1774 | - self.0 ^= self.0 << 13; | ||
| 1775 | - self.0 ^= self.0 >> 17; | ||
| 1776 | - self.0 ^= self.0 << 5; | ||
| 1777 | - self.0 | ||
| 1778 | - } | ||
| 1779 | - } | ||
| 1780 | - | ||
| 1781 | - /// ENTRY GATE for lane-h265rdoq r3: the sub-block frozen-prefix cursor | ||
| 1782 | - /// [`rdoq`] now uses must produce bit-for-bit the same trial decisions — | ||
| 1783 | - /// and so the same final `levels` and the same final bitstream — as | ||
| 1784 | - /// always re-deriving every trial's bits from the block's start | ||
| 1785 | - /// ([`rdoq_full_recompute`], this round's pre-change behaviour). Covers | ||
| 1786 | - /// every block size with a sub-block split (8/16/32 — 4x4 has none), | ||
| 1787 | - /// every scan, 8 seeds, sign-hiding on/off on the final bitstream, and | ||
| 1788 | - /// deliberately includes levels of magnitude 1-3 so some trials drop the | ||
| 1789 | - /// last significant coefficient, some empty a whole sub-block (csbf | ||
| 1790 | - /// flips), and some sit right at the greater1/greater2 escape boundary. | ||
| 1791 | - #[test] | ||
| 1792 | - fn sub_block_cursor_matches_full_recompute() { | ||
| 1793 | - for log2 in [3u32, 4, 5] { | ||
| 1794 | - let n = 1usize << log2; | ||
| 1795 | - for scan_idx in 0..3 { | ||
| 1796 | - for seed in 1..=8u32 { | ||
| 1797 | - let mut rng = Xorshift(seed.wrapping_mul(2654435761).max(1)); | ||
| 1798 | - let mut coeffs = vec![0i32; n * n]; | ||
| 1799 | - for c in coeffs.iter_mut() { | ||
| 1800 | - // Skewed toward small magnitudes (0..=3) so RDOQ_MAX_LEVEL's | ||
| 1801 | - // trial window and the greater1/greater2 boundary both fire | ||
| 1802 | - // often, with occasional larger outliers left untouched. | ||
| 1803 | - let r = rng.next(); | ||
| 1804 | - *c = match r % 5 { | ||
| 1805 | - 0 => 0, | ||
| 1806 | - 1 => (r >> 8) as i32 % 2 + 1, | ||
| 1807 | - 2 => -(((r >> 8) as i32 % 2) + 1), | ||
| 1808 | - 3 => (r >> 8) as i32 % 6 + 1, | ||
| 1809 | - _ => -(((r >> 8) as i32 % 6) + 1), | ||
| 1810 | - }; | ||
| 1811 | - } | ||
| 1812 | - let qp = 20 + (seed as i32 % 20); | ||
| 1813 | - | ||
| 1814 | - let mut levels_a = coeffs.clone(); | ||
| 1815 | - let mut enc_a = CabacEncoder::counter(Contexts::new(30)); | ||
| 1816 | - let count_a = rdoq( | ||
| 1817 | - &coeffs, | ||
| 1818 | - &mut levels_a, | ||
| 1819 | - n, | ||
| 1820 | - qp, | ||
| 1821 | - 8, | ||
| 1822 | - 0, | ||
| 1823 | - scan_idx, | ||
| 1824 | - ctx::CBF_LUMA, | ||
| 1825 | - 1.0, | ||
| 1826 | - None, | ||
| 1827 | - &mut enc_a, | ||
| 1828 | - ); | ||
| 1829 | - | ||
| 1830 | - let mut levels_b = coeffs.clone(); | ||
| 1831 | - let mut enc_b = CabacEncoder::counter(Contexts::new(30)); | ||
| 1832 | - let count_b = rdoq_full_recompute( | ||
| 1833 | - &coeffs, | ||
| 1834 | - &mut levels_b, | ||
| 1835 | - n, | ||
| 1836 | - qp, | ||
| 1837 | - 8, | ||
| 1838 | - 0, | ||
| 1839 | - scan_idx, | ||
| 1840 | - ctx::CBF_LUMA, | ||
| 1841 | - 1.0, | ||
| 1842 | - None, | ||
| 1843 | - &mut enc_b, | ||
| 1844 | - ); | ||
| 1845 | - | ||
| 1846 | - assert_eq!( | ||
| 1847 | - count_a, count_b, | ||
| 1848 | - "log2={log2} scan={scan_idx} seed={seed}: non-zero count differs" | ||
| 1849 | - ); | ||
| 1850 | - assert_eq!( | ||
| 1851 | - levels_a, levels_b, | ||
| 1852 | - "log2={log2} scan={scan_idx} seed={seed}: decided levels differ" | ||
| 1853 | - ); | ||
| 1854 | - | ||
| 1855 | - if count_a == 0 { | ||
| 1856 | - continue; | ||
| 1857 | - } | ||
| 1858 | - for sign_hiding in [false, true] { | ||
| 1859 | - let mut final_levels = levels_a.clone(); | ||
| 1860 | - if sign_hiding { | ||
| 1861 | - hide_signs(&coeffs, &mut final_levels, n, 30, scan_idx); | ||
| 1862 | - } | ||
| 1863 | - let mut fenc = CabacEncoder::counter(Contexts::new(30)); | ||
| 1864 | - encode_residual( | ||
| 1865 | - &mut fenc, | ||
| 1866 | - &final_levels, | ||
| 1867 | - log2, | ||
| 1868 | - 0, | ||
| 1869 | - scan_idx, | ||
| 1870 | - sign_hiding, | ||
| 1871 | - None, | ||
| 1872 | - ); | ||
| 1873 | - assert!(fenc.bit_count() > 0); | ||
| 1874 | - } | ||
| 1875 | - } | ||
| 1876 | - } | ||
| 1877 | - } | ||
| 1878 | - } | ||
| 1879 | } | 1621 | } |