forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits

compound.rs121 lines, 5.0 KiBHistoryRawDownload
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`.
11const 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.
16const 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`).
21const 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.
36pub(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)]
79mod 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}