forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 commits

warp.rs133 lines, 5.5 KiBHistoryRawDownload
1//! The shear validity of a global motion model (spec 7.11.3.6, 7.11.3.7).
2//!
3//! `VAWarpedMotionParamsAV1` carries an `invalid` flag alongside the warp
4//! matrix, because a syntactically legal affine model can still describe a
5//! shear the motion compensation filter cannot apply — in which case the block
6//! falls back to translation. The check is pure arithmetic on the coded
7//! parameters, so it belongs with the parse rather than with the decoder.
8
9/// `DIV_LUT_PREC_BITS` (spec 3).
10const DIV_LUT_PREC_BITS: u32 = 14;
11/// `DIV_LUT_BITS` (spec 3).
12const DIV_LUT_BITS: u32 = 8;
13/// `WARP_PARAM_REDUCE_BITS` (spec 3).
14const WARP_PARAM_REDUCE_BITS: u32 = 6;
15/// `WARPEDMODEL_PREC_BITS` (spec 3).
16const WARPEDMODEL_PREC_BITS: u32 = 16;
17
18/// `Div_Lut` (spec 7.11.3.7), 257 entries of `DIV_LUT_PREC_BITS` precision.
19static DIV_LUT: [i64; 257] = [
20 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768, 15709, 15650,
21 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142, 15087, 15033, 14980, 14926,
22 14873, 14821, 14769, 14717, 14665, 14614, 14564, 14513, 14463, 14413, 14364, 14315, 14266,
23 14218, 14170, 14122, 14075, 14028, 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662,
24 13618, 13574, 13530, 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107,
25 13066, 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633, 12596,
26 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228, 12193, 12157, 12122,
27 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848, 11815, 11782, 11749, 11716, 11683,
28 11651, 11619, 11586, 11555, 11523, 11491, 11460, 11429, 11398, 11367, 11336, 11305, 11275,
29 11245, 11215, 11185, 11155, 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894,
30 10866, 10838, 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
31 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255, 10230, 10205,
32 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986, 9963, 9939, 9916, 9892, 9869,
33 9846, 9823, 9800, 9777, 9754, 9732, 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511,
34 9489, 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259, 9239, 9218, 9198, 9178,
35 9158, 9138, 9118, 9098, 9079, 9059, 9039, 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867,
36 8849, 8830, 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630, 8613, 8595, 8577,
37 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439, 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306,
38 8289, 8273, 8257, 8240, 8224, 8208, 8192,
39];
40
41/// `Round2(x, n)` (spec 4.7).
42fn round2(x: i64, n: u32) -> i64 {
43 if n == 0 {
44 return x;
45 }
46 (x + (1 << (n - 1))) >> n
47}
48
49/// `Round2Signed(x, n)` (spec 4.7).
50fn round2_signed(x: i64, n: u32) -> i64 {
51 if x >= 0 { round2(x, n) } else { -round2(-x, n) }
52}
53
54/// `resolve_divisor(d)` (spec 7.11.3.7).
55fn resolve_divisor(d: i64) -> (u32, i64) {
56 let abs_d = d.unsigned_abs();
57 if abs_d == 0 {
58 // FloorLog2(0) is undefined; a zero divisor cannot produce a valid
59 // shear, and the caller's validity check rejects the model anyway.
60 return (DIV_LUT_PREC_BITS, 0);
61 }
62 let n = 63 - abs_d.leading_zeros();
63 let e = abs_d as i64 - (1i64 << n);
64 let f = if n > DIV_LUT_BITS {
65 round2(e, n - DIV_LUT_BITS)
66 } else {
67 e << (DIV_LUT_BITS - n)
68 };
69 let factor = DIV_LUT[(f as usize).min(DIV_LUT.len() - 1)];
70 (n + DIV_LUT_PREC_BITS, if d < 0 { -factor } else { factor })
71}
72
73/// `setup_shear(warpParams)` (spec 7.11.3.6): is this warp model usable?
74///
75/// True is `warpValid`; false is `VAWarpedMotionParamsAV1::invalid`.
76pub(crate) fn warp_valid(params: &[i32; 6]) -> bool {
77 let p: [i64; 6] = [
78 params[0] as i64,
79 params[1] as i64,
80 params[2] as i64,
81 params[3] as i64,
82 params[4] as i64,
83 params[5] as i64,
84 ];
85 let one = 1i64 << WARPEDMODEL_PREC_BITS;
86 let alpha0 = (p[2] - one).clamp(-32768, 32767);
87 let beta0 = p[3].clamp(-32768, 32767);
88 let (div_shift, div_factor) = resolve_divisor(p[2]);
89 let v = p[4] << WARPEDMODEL_PREC_BITS;
90 let gamma0 = round2_signed(v * div_factor, div_shift).clamp(-32768, 32767);
91 let w = p[3] * p[4];
92 let delta0 = (p[5] - round2_signed(w * div_factor, div_shift) - one).clamp(-32768, 32767);
93
94 let reduce = |x: i64| round2_signed(x, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
95 let (alpha, beta, gamma, delta) = (
96 reduce(alpha0),
97 reduce(beta0),
98 reduce(gamma0),
99 reduce(delta0),
100 );
101
102 4 * alpha.abs() + 7 * beta.abs() < one && 4 * gamma.abs() + 4 * delta.abs() < one
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn identity_warp_is_valid() {
111 let one = 1 << WARPEDMODEL_PREC_BITS;
112 assert!(warp_valid(&[0, 0, one, 0, 0, one]));
113 }
114
115 #[test]
116 fn an_extreme_shear_is_rejected() {
117 let one = 1 << WARPEDMODEL_PREC_BITS;
118 // beta of a quarter of unity alone breaks 7 * |beta| < 1 << 16.
119 assert!(!warp_valid(&[0, 0, one, one / 4, 0, one]));
120 // A degenerate zero scale cannot be sheared either.
121 assert!(!warp_valid(&[0, 0, 0, 0, 0, one]));
122 }
123
124 #[test]
125 fn div_lut_anchors_match_the_spec_table() {
126 assert_eq!(DIV_LUT[0], 16384);
127 assert_eq!(DIV_LUT[256], 8192);
128 assert_eq!(DIV_LUT.len(), 257);
129 // resolve_divisor(1 << 16) is an exact power of two: factor 16384.
130 assert_eq!(resolve_divisor(1 << 16), (16 + 14, 16384));
131 assert_eq!(resolve_divisor(-(1 << 16)), (30, -16384));
132 }
133}