Compound CONV_BUF blend + distance-weight assign, not wired yet
Browse files at this commit181a784c6b10ca18d8dce43a499fafc2d645dab8Parent: 214cdf2- feat
- compound CONV_BUF blend + distance-weight assign, not wired yet
3 changed files +270 -0
- crates/ec-av1/src/compound.rs +121 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/mc.rs +148 -0
crates/ec-av1/src/compound.rsadded+121 -0
| @@ -0,0 +1,121 @@ | ||
| 1 | +//! Compound-reference blend weights (spec 7.11.3.15's "Distance weights | |
| 2 | +//! process", libaom's `av1_dist_wtd_comp_weight_assign` in | |
| 3 | +//! `reconinter.c`) -- pure, independent of the CDF/bitstream plumbing that | |
| 4 | +//! decides *whether* a block reads `compound_idx` at all (lane-av1comp, | |
| 5 | +//! decode.rs). Kept its own module so it is testable without a live | |
| 6 | +//! decoder: `dist_wtd_comp_weight_assign` takes exactly the four order | |
| 7 | +//! hints libaom's version reads off `AV1_COMMON`/`MB_MODE_INFO`. | |
| 8 | + | |
| 9 | +/// `MAX_FRAME_DISTANCE` (spec/`enums.h`): `(1 << FRAME_OFFSET_BITS) - 1`, | |
| 10 | +/// `FRAME_OFFSET_BITS == 5`. | |
| 11 | +const MAX_FRAME_DISTANCE: i32 = (1 << 5) - 1; | |
| 12 | + | |
| 13 | +/// `quant_dist_weight` (libaom `common_data.h`) -- the three candidate | |
| 14 | +/// weight ratios the search below picks between, before falling back to the | |
| 15 | +/// index-3 row when either distance is zero. | |
| 16 | +const QUANT_DIST_WEIGHT: [[i32; 2]; 4] = [[2, 3], [2, 5], [2, 7], [1, MAX_FRAME_DISTANCE]]; | |
| 17 | + | |
| 18 | +/// `quant_dist_lookup_table` (libaom `common_data.h`): the actual | |
| 19 | +/// `fwd_offset`/`bck_offset` pair each `QUANT_DIST_WEIGHT` row resolves to | |
| 20 | +/// (both always sum to 16, `1 << DIST_PRECISION_BITS`). | |
| 21 | +const QUANT_DIST_LOOKUP_TABLE: [[i32; 2]; 4] = [[9, 7], [11, 5], [12, 4], [13, 3]]; | |
| 22 | + | |
| 23 | +/// `av1_dist_wtd_comp_weight_assign` (spec 7.11.3.15 / libaom | |
| 24 | +/// `reconinter.c`): `(fwd_weight, bck_weight)` for a compound block, given | |
| 25 | +/// this frame's own `order_hint` and the two references' order hints -- | |
| 26 | +/// `ref0_order_hint` names `mbmi->ref_frame[0]` (libaom's "`bck`", despite | |
| 27 | +/// the name -- it is nearer in decode order for a typical GOP but the | |
| 28 | +/// spec's algebra does not assume that), `ref1_order_hint` names | |
| 29 | +/// `ref_frame[1]` ("`fwd`"). | |
| 30 | +/// | |
| 31 | +/// `compound_idx == 1` (or `!is_compound`, never reached from here) is the | |
| 32 | +/// simple-average caller's job: it never calls this and uses `(8, 8)` | |
| 33 | +/// directly (spec 7.11.3.15's `use_dist_wtd_comp_avg == 0` branch) -- | |
| 34 | +/// this function only ever implements the `use_dist_wtd_comp_avg == 1` | |
| 35 | +/// (`compound_idx == 0`) branch. | |
| 36 | +pub(crate) fn dist_wtd_comp_weight_assign( | |
| 37 | + order_hint_bits: u32, | |
| 38 | + cur_order_hint: u32, | |
| 39 | + ref0_order_hint: u32, | |
| 40 | + ref1_order_hint: u32, | |
| 41 | +) -> (i32, i32) { | |
| 42 | + let d0 = | |
| 43 | + crate::motion_field::get_relative_dist(order_hint_bits, ref1_order_hint, cur_order_hint) | |
| 44 | + .abs() | |
| 45 | + .clamp(0, MAX_FRAME_DISTANCE); | |
| 46 | + let d1 = | |
| 47 | + crate::motion_field::get_relative_dist(order_hint_bits, cur_order_hint, ref0_order_hint) | |
| 48 | + .abs() | |
| 49 | + .clamp(0, MAX_FRAME_DISTANCE); | |
| 50 | + | |
| 51 | + let order = usize::from(d0 <= d1); | |
| 52 | + | |
| 53 | + if d0 == 0 || d1 == 0 { | |
| 54 | + return ( | |
| 55 | + QUANT_DIST_LOOKUP_TABLE[3][order], | |
| 56 | + QUANT_DIST_LOOKUP_TABLE[3][1 - order], | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + let mut i = 0; | |
| 61 | + while i < 3 { | |
| 62 | + let c0 = QUANT_DIST_WEIGHT[i][order]; | |
| 63 | + let c1 = QUANT_DIST_WEIGHT[i][1 - order]; | |
| 64 | + let d0_c0 = d0 * c0; | |
| 65 | + let d1_c1 = d1 * c1; | |
| 66 | + if (d0 > d1 && d0_c0 < d1_c1) || (d0 <= d1 && d0_c0 > d1_c1) { | |
| 67 | + break; | |
| 68 | + } | |
| 69 | + i += 1; | |
| 70 | + } | |
| 71 | + | |
| 72 | + ( | |
| 73 | + QUANT_DIST_LOOKUP_TABLE[i][order], | |
| 74 | + QUANT_DIST_LOOKUP_TABLE[i][1 - order], | |
| 75 | + ) | |
| 76 | +} | |
| 77 | + | |
| 78 | +#[cfg(test)] | |
| 79 | +mod tests { | |
| 80 | + use super::dist_wtd_comp_weight_assign; | |
| 81 | + | |
| 82 | + /// Equidistant references (spec/libaom's `d0 == d1` case) land on | |
| 83 | + /// `order == 1` (`d0 <= d1`) throughout, and the very first weight | |
| 84 | + /// candidate (`i == 0`, ratio `2:3`) always survives the search's break | |
| 85 | + /// condition when `d0_c0 == d1_c1`'s neither branch fires -- so a | |
| 86 | + /// symmetric GOP (order hints `8` behind and `8` ahead of a `16`-hint | |
| 87 | + /// current frame) resolves to the row-0 weights `(7, 9)`: `order == 1` | |
| 88 | + /// (`d0 <= d1`) selects `QUANT_DIST_LOOKUP_TABLE[0][1] == 7` for | |
| 89 | + /// `fwd_offset` and `QUANT_DIST_LOOKUP_TABLE[0][0] == 9` for | |
| 90 | + /// `bck_offset`. | |
| 91 | + #[test] | |
| 92 | + fn equidistant_references_pick_the_first_weight_row() { | |
| 93 | + assert_eq!(dist_wtd_comp_weight_assign(8, 16, 8, 24), (7, 9)); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /// A reference at zero relative distance (this frame repeats one of its | |
| 97 | + /// own references' order hint -- degenerate but spec-legal) always | |
| 98 | + /// takes the index-3 lookup row (`13`/`3`) rather than entering the | |
| 99 | + /// weight search, matching libaom's `d0 == 0 || d1 == 0` early return. | |
| 100 | + #[test] | |
| 101 | + fn zero_distance_reference_takes_the_index_3_row() { | |
| 102 | + // ref1 (fwd) coincides with cur -> d0 == 0. | |
| 103 | + assert_eq!(dist_wtd_comp_weight_assign(8, 16, 8, 16), (3, 13)); | |
| 104 | + } | |
| 105 | + | |
| 106 | + /// Weights always sum to `1 << DIST_PRECISION_BITS == 16` -- every row | |
| 107 | + /// of `QUANT_DIST_LOOKUP_TABLE` was picked to hold that invariant, and | |
| 108 | + /// [`combine_compound`](crate::mc::combine_compound)'s final shift | |
| 109 | + /// (`InterPostRound + DIST_PRECISION_BITS`) assumes it. | |
| 110 | + #[test] | |
| 111 | + fn weights_always_sum_to_sixteen() { | |
| 112 | + for cur in [0u32, 16, 40] { | |
| 113 | + for r0 in [0u32, 8, 32] { | |
| 114 | + for r1 in [4u32, 24, 48] { | |
| 115 | + let (fwd, bck) = dist_wtd_comp_weight_assign(8, cur, r0, r1); | |
| 116 | + assert_eq!(fwd + bck, 16, "cur={cur} r0={r0} r1={r1}"); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + } | |
| 120 | + } | |
| 121 | +} |
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | pub mod bits; |
| 16 | 16 | pub mod cdf; |
| 17 | 17 | mod cdf_state; |
| 18 | +mod compound; | |
| 18 | 19 | pub mod decode; |
| 19 | 20 | pub mod encode; |
| 20 | 21 | pub mod encoder; |
crates/ec-av1/src/mc.rsmodified+148 -0
| @@ -411,6 +411,154 @@ pub fn predict_with_filters( | ||
| 411 | 411 | crate::encode::stage_add(1, stage_t.elapsed()); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | +/// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch, | |
| 415 | +/// `COMPOUND_ROUND1_BITS` in libaom's `convolve.h`): 4 bits shallower than | |
| 416 | +/// [`INTER_ROUND_1`] so the vertical pass lands in the `CONV_BUF` domain | |
| 417 | +/// (still above 8-bit pixel range) instead of a finished sample -- | |
| 418 | +/// `predict_compound_intermediate` below stops there and leaves the final | |
| 419 | +/// clip to whichever combine step (simple/distance-weighted average, or a | |
| 420 | +/// future masked blend) runs on the pair of intermediates. | |
| 421 | +const INTER_ROUND_1_COMPOUND: u32 = 7; | |
| 422 | + | |
| 423 | +/// `InterPostRound` (spec 7.11.3.2): `2 * FILTER_BITS - (InterRound0 + | |
| 424 | +/// InterRound1)` with the *compound* `InterRound1` above -- `2*7 - (3+7) == | |
| 425 | +/// 4`. [`combine_compound`] folds this together with `DIST_PRECISION_BITS` | |
| 426 | +/// (below) into one final shift. | |
| 427 | +const INTER_POST_ROUND: u32 = 4; | |
| 428 | + | |
| 429 | +/// `DIST_PRECISION_BITS` (spec 7.11.3.15 / libaom `enums.h`): the two | |
| 430 | +/// weights [`combine_compound`] takes always sum to `1 << 4 == 16`, whether | |
| 431 | +/// they are the simple-average split (8/8) or a distance-weighted split | |
| 432 | +/// from [`crate::compound::dist_wtd_comp_weight_assign`]. | |
| 433 | +const DIST_PRECISION_BITS: u32 = 4; | |
| 434 | + | |
| 435 | +/// [`predict_with_filters`]'s compound counterpart: same two-pass separable | |
| 436 | +/// filter, but the vertical pass rounds by [`INTER_ROUND_1_COMPOUND`] | |
| 437 | +/// instead of [`INTER_ROUND_1`] and is never clipped to a pixel -- the | |
| 438 | +/// `CONV_BUF` intermediate domain spec 7.11.3.2 keeps a compound block's two | |
| 439 | +/// per-reference predictions in until [`combine_compound`] blends them. | |
| 440 | +/// `dst` receives one `i32` per sample, row-major, same shape as | |
| 441 | +/// [`predict_with_filters`]'s `u8` `dst`. | |
| 442 | +/// | |
| 443 | +/// # Panics | |
| 444 | +/// Panics when `dst` is not `block_w * block_h` long, or the reference is | |
| 445 | +/// empty. | |
| 446 | +#[allow(clippy::too_many_arguments)] | |
| 447 | +pub fn predict_compound_intermediate( | |
| 448 | + reference: &[u8], | |
| 449 | + stride: usize, | |
| 450 | + true_width: usize, | |
| 451 | + true_height: usize, | |
| 452 | + x_q4: i32, | |
| 453 | + y_q4: i32, | |
| 454 | + block_w: usize, | |
| 455 | + block_h: usize, | |
| 456 | + h_kind: InterpFilterKind, | |
| 457 | + v_kind: InterpFilterKind, | |
| 458 | + dst: &mut [i32], | |
| 459 | +) { | |
| 460 | + assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | |
| 461 | + assert!(!reference.is_empty(), "a reference plane has samples"); | |
| 462 | + | |
| 463 | + let x0 = x_q4.div_euclid(16); | |
| 464 | + let xfrac = x_q4.rem_euclid(16) as usize; | |
| 465 | + let y0 = y_q4.div_euclid(16); | |
| 466 | + let yfrac = y_q4.rem_euclid(16) as usize; | |
| 467 | + | |
| 468 | + let (h_wide, h_narrow) = h_kind.tables(); | |
| 469 | + let (v_wide, v_narrow) = v_kind.tables(); | |
| 470 | + let h_filter = if block_w <= 4 { | |
| 471 | + &h_narrow[xfrac] | |
| 472 | + } else { | |
| 473 | + &h_wide[xfrac] | |
| 474 | + }; | |
| 475 | + let v_filter = if block_h <= 4 { | |
| 476 | + &v_narrow[yfrac] | |
| 477 | + } else { | |
| 478 | + &v_wide[yfrac] | |
| 479 | + }; | |
| 480 | + | |
| 481 | + let rows = block_h + 7; | |
| 482 | + let mut intermediate = vec![0i32; rows * block_w]; | |
| 483 | + for r in 0..rows { | |
| 484 | + let y = y0 - 3 + r as i32; | |
| 485 | + for c in 0..block_w { | |
| 486 | + let mut sum = 0; | |
| 487 | + for (t, &tap) in h_filter.iter().enumerate() { | |
| 488 | + let x = x0 + c as i32 + t as i32 - 3; | |
| 489 | + sum += tap * sample(reference, stride, true_width, true_height, x, y); | |
| 490 | + } | |
| 491 | + intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); | |
| 492 | + } | |
| 493 | + } | |
| 494 | + | |
| 495 | + for row in 0..block_h { | |
| 496 | + for col in 0..block_w { | |
| 497 | + let mut sum = 0; | |
| 498 | + for (t, &tap) in v_filter.iter().enumerate() { | |
| 499 | + sum += tap * intermediate[(row + t) * block_w + col]; | |
| 500 | + } | |
| 501 | + dst[row * block_w + col] = round2(sum, INTER_ROUND_1_COMPOUND); | |
| 502 | + } | |
| 503 | + } | |
| 504 | +} | |
| 505 | + | |
| 506 | +/// Blends two [`predict_compound_intermediate`] outputs into a finished | |
| 507 | +/// 8-bit block (spec 7.11.3.15's weighted-average combine, the | |
| 508 | +/// `comp_group_idx == 0` path -- `fwd_weight`/`bck_weight` are either the | |
| 509 | +/// simple-average split `(8, 8)` or [`crate::compound::dist_wtd_comp_weight_assign`]'s | |
| 510 | +/// output; both always sum to `1 << DIST_PRECISION_BITS`). Masked compound | |
| 511 | +/// (`comp_group_idx == 1`, wedge/diffwtd) is a different combine this | |
| 512 | +/// function does not cover -- decode.rs still refuses those by name. | |
| 513 | +pub fn combine_compound( | |
| 514 | + pred0: &[i32], | |
| 515 | + pred1: &[i32], | |
| 516 | + fwd_weight: i32, | |
| 517 | + bck_weight: i32, | |
| 518 | + dst: &mut [u8], | |
| 519 | +) { | |
| 520 | + assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); | |
| 521 | + assert_eq!(dst.len(), pred0.len(), "the destination is the block"); | |
| 522 | + for i in 0..dst.len() { | |
| 523 | + let sum = pred0[i] * fwd_weight + pred1[i] * bck_weight; | |
| 524 | + dst[i] = round2(sum, INTER_POST_ROUND + DIST_PRECISION_BITS).clamp(0, 255) as u8; | |
| 525 | + } | |
| 526 | +} | |
| 527 | + | |
| 528 | +/// Whole-pel identity case: [`predict_compound_intermediate`]'s two-pass | |
| 529 | +/// filter reduces to `16 * source_sample` exactly (the fraction-0 row is a | |
| 530 | +/// single `128` tap both passes, and `128*128 == 1 << 14`, `14 - 7 == 7 == | |
| 531 | +/// INTER_ROUND_1_COMPOUND`, so the extra `2^4` of gain the compound path | |
| 532 | +/// keeps over [`predict`]'s 11-bit round survives untouched) -- | |
| 533 | +/// [`combine_compound`] then divides that back out exactly with an 8/8 | |
| 534 | +/// simple-average weight split, so a compound block whose two references | |
| 535 | +/// (and MVs) happen to coincide reproduces the plain source sample, not an | |
| 536 | +/// off-by-one from double rounding. | |
| 537 | +#[test] | |
| 538 | +fn compound_intermediate_whole_pel_identity_round_trips_through_combine() { | |
| 539 | + let reference = vec![100u8; 16 * 16]; | |
| 540 | + let mut pred0 = vec![0i32; 16]; | |
| 541 | + predict_compound_intermediate( | |
| 542 | + &reference, | |
| 543 | + 16, | |
| 544 | + 16, | |
| 545 | + 16, | |
| 546 | + 0, | |
| 547 | + 0, | |
| 548 | + 4, | |
| 549 | + 4, | |
| 550 | + InterpFilterKind::Regular, | |
| 551 | + InterpFilterKind::Regular, | |
| 552 | + &mut pred0, | |
| 553 | + ); | |
| 554 | + assert!(pred0.iter().all(|&v| v == 1600), "{pred0:?}"); | |
| 555 | + | |
| 556 | + let pred1 = pred0.clone(); | |
| 557 | + let mut dst = vec![0u8; 16]; | |
| 558 | + combine_compound(&pred0, &pred1, 8, 8, &mut dst); | |
| 559 | + assert!(dst.iter().all(|&v| v == 100), "{dst:?}"); | |
| 560 | +} | |
| 561 | + | |
| 414 | 562 | #[cfg(test)] |
| 415 | 563 | mod tests { |
| 416 | 564 | use super::{InterpFilterKind, predict, predict_with_filter}; |