forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 commits

warp.rs911 lines, 35.1 KiBHistoryRawDownload
1//! Local warp (`WARPED_CAUSAL`) motion prediction, spec 7.11.3.6-.8 /
2//! `av1/common/warped_motion.c`: estimate a 2x3 affine model from up to
3//! `LEAST_SQUARES_SAMPLES_MAX` neighbour-block motion samples
4//! ([`find_projection`]), then filter the reference through it with an 8x8
5//! tiled, two-pass 8-tap warped filter ([`warp_affine`]). Bit-depth generic
6//! (lane-cwarp: `bd` is `crate::decode::bit_depth()`, was a hardcoded 8).
7//! `WARPED_CAUSAL` is single-ref by construction (spec 5.11.27:
8//! `motion_mode` is only read when `RefFrame[1] <= INTRA_FRAME`), but GLOBAL
9//! warp applies per reference on a compound block too --
10//! [`warp_affine_compound`] is that path (libaom `av1_warp_plane` with
11//! `conv_params->is_compound`).
12
13/// `WARPEDMODEL_PREC_BITS` (`av1/common/mv.h`).
14const WARPEDMODEL_PREC_BITS: u32 = 16;
15/// `WARPEDMODEL_TRANS_CLAMP`.
16const WARPEDMODEL_TRANS_CLAMP: i64 = 128 << WARPEDMODEL_PREC_BITS;
17/// `WARPEDMODEL_NONDIAGAFFINE_CLAMP`.
18const WARPEDMODEL_NONDIAGAFFINE_CLAMP: i64 = 1 << (WARPEDMODEL_PREC_BITS - 3);
19/// `WARPEDPIXEL_PREC_BITS`.
20const WARPEDPIXEL_PREC_BITS: u32 = 6;
21/// `WARPEDPIXEL_PREC_SHIFTS`.
22const WARPEDPIXEL_PREC_SHIFTS: i32 = 1 << WARPEDPIXEL_PREC_BITS;
23/// `WARPEDDIFF_PREC_BITS`.
24const WARPEDDIFF_PREC_BITS: u32 = WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS;
25/// `WARP_PARAM_REDUCE_BITS`.
26const WARP_PARAM_REDUCE_BITS: u32 = 6;
27/// `MI_SIZE` (4x4 luma pixels).
28const MI_SIZE: i32 = 4;
29/// `FILTER_BITS`.
30const FILTER_BITS: i32 = 7;
31/// `LEAST_SQUARES_SAMPLES_MAX`.
32pub const LEAST_SQUARES_SAMPLES_MAX: usize = 8;
33/// `LS_MV_MAX` (max mv in 1/8-pel, `find_affine_int`'s own sample gate).
34const LS_MV_MAX: i32 = 256;
35/// `LS_STEP`.
36const LS_STEP: i32 = 8;
37
38/// `Round2Signed` (spec 4.7): halves round away from zero's own sign.
39fn round2_signed(value: i64, n: u32) -> i64 {
40 if value < 0 {
41 -round2(-value, n)
42 } else {
43 round2(value, n)
44 }
45}
46
47fn round2(value: i64, n: u32) -> i64 {
48 (value + (1i64 << n >> 1)) >> n
49}
50
51/// `GM_TRANS_ONLY_PREC_DIFF` (`av1/common/mv.h`): `WARPEDMODEL_PREC_BITS
52/// (16) - 3`, the extra precision a `TRANSLATION` global-motion param has
53/// over a 1/8-pel mv.
54const GM_TRANS_ONLY_PREC_DIFF: u32 = WARPEDMODEL_PREC_BITS - 3;
55
56/// `convert_to_trans_prec` (`av1/common/mv.h`): projects a
57/// `WARPEDMODEL_PREC_BITS`-fixed-point coordinate down to 1/8-pel
58/// (`allow_high_precision_mv`) or 1/4-pel-then-doubled (not high precision).
59fn convert_to_trans_prec(allow_high_precision_mv: bool, x: i64) -> i32 {
60 if allow_high_precision_mv {
61 round2_signed(x, WARPEDMODEL_PREC_BITS - 3) as i32
62 } else {
63 (round2_signed(x, WARPEDMODEL_PREC_BITS - 2) as i32) * 2
64 }
65}
66
67/// `lower_mv_precision`'s `force_integer_mv` half, applied per component
68/// (spec 7.10.2.1's `integer_mv_precision`): rounds a 1/8-pel value to the
69/// nearest full pel (multiple of 8), ties biased away from zero.
70fn integer_mv_precision(v: i32) -> i32 {
71 let md = v % 8;
72 if md != 0 {
73 let mut v = v - md;
74 if md.abs() > 4 {
75 v += if md > 0 { 8 } else { -8 };
76 }
77 v
78 } else {
79 v
80 }
81}
82
83/// `gm_get_motion_vector` (`av1/common/mv.h:231`): the motion vector a
84/// `GLOBALMV`/`GLOBAL_GLOBALMV` block predicts with, derived from the
85/// frame's own global-motion model for `ref_frame` at this block's centre
86/// -- spec 7.10.2.1. `bw4`/`bh4` are the block's size in 4x4 units.
87/// `IDENTITY` returns `(0, 0)`; `TRANSLATION` keeps the spec's own
88/// swapped-axis bug (`row` reads `wmmat[0]`, `col` reads `wmmat[1]`) rather
89/// than "fixing" it -- that is what real encoders/decoders exchange.
90#[allow(clippy::too_many_arguments)]
91pub fn gm_get_motion_vector(
92 model: ec_av1_syntax::WarpModel,
93 params: &[i32; 6],
94 mi_row: usize,
95 mi_col: usize,
96 bw4: usize,
97 bh4: usize,
98 allow_high_precision_mv: bool,
99 force_integer_mv: bool,
100) -> (i32, i32) {
101 use ec_av1_syntax::WarpModel;
102 let (row, col) = match model {
103 WarpModel::Identity => (0, 0),
104 WarpModel::Translation => (
105 params[0] >> GM_TRANS_ONLY_PREC_DIFF,
106 params[1] >> GM_TRANS_ONLY_PREC_DIFF,
107 ),
108 WarpModel::Rotzoom | WarpModel::Affine => {
109 // `block_center_x`/`block_center_y` (mv.h:183-191): `- 1`, not
110 // just the halved block size -- lane-gm r3's root cause, missing
111 // here made every ROTZOOM/AFFINE GLOBALMV prediction off by a
112 // sub-pel fraction (small luma delta growing across P-frames as
113 // it propagates through the reference chain).
114 let x = (mi_col * MI_SIZE as usize + (bw4 * MI_SIZE as usize) / 2) as i64 - 1;
115 let y = (mi_row * MI_SIZE as usize + (bh4 * MI_SIZE as usize) / 2) as i64 - 1;
116 let xc = (params[2] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)) * x
117 + params[3] as i64 * y
118 + params[0] as i64;
119 let yc = params[4] as i64 * x
120 + (params[5] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)) * y
121 + params[1] as i64;
122 (
123 convert_to_trans_prec(allow_high_precision_mv, yc),
124 convert_to_trans_prec(allow_high_precision_mv, xc),
125 )
126 }
127 };
128 if force_integer_mv {
129 (integer_mv_precision(row), integer_mv_precision(col))
130 } else {
131 (row, col)
132 }
133}
134
135/// `get_msb` (`aom_ports/bitops.h`): index of the highest set bit.
136fn get_msb64(n: u64) -> i32 {
137 debug_assert!(n != 0);
138 63 - n.leading_zeros() as i32
139}
140
141/// `resolve_divisor_64`: decomposes `d` such that `1/d = mult / 2^shift`.
142fn resolve_divisor_64(d: u64) -> (i64, i32) {
143 const DIV_LUT_PREC_BITS: i32 = 14;
144 const DIV_LUT_BITS: i32 = 8;
145 let mut shift = get_msb64(d);
146 let e = d as i64 - (1i64 << shift);
147 let f = if shift > DIV_LUT_BITS {
148 round2(e, (shift - DIV_LUT_BITS) as u32)
149 } else {
150 e << (DIV_LUT_BITS - shift)
151 };
152 debug_assert!((0..=256).contains(&f));
153 shift += DIV_LUT_PREC_BITS;
154 (DIV_LUT[f as usize] as i64, shift)
155}
156
157/// `resolve_divisor_32`, same decomposition for a 32-bit divisor (this port
158/// only ever needs the 64-bit form, `av1_get_shear_params`'s `mat[2]` is
159/// magnitude-bounded well under 32 bits too, so reuses [`resolve_divisor_64`]
160/// -- bit-identical for any value that fits both).
161fn resolve_divisor_32(d: u32) -> (i64, i32) {
162 resolve_divisor_64(d as u64)
163}
164
165fn clamp(v: i64, lo: i64, hi: i64) -> i64 {
166 v.clamp(lo, hi)
167}
168
169/// One recorded least-squares sample: `(pts1, pts2)`, each `(x, y)` in
170/// 1/8-pel, relative to the current block's own top-left (spec
171/// `record_samples`).
172#[derive(Clone, Copy)]
173pub struct Sample {
174 pub pts1: (i32, i32),
175 pub pts2: (i32, i32),
176}
177
178/// `record_samples` (`mvref_common.c`): one neighbour's own sample, `bw`/`bh`
179/// its block size in luma pixels, `(nb_mv_col, nb_mv_row)` its own motion
180/// vector (1/8-pel).
181#[allow(clippy::too_many_arguments)]
182pub fn record_sample(
183 bw: i32,
184 bh: i32,
185 nb_mv: (i32, i32),
186 row_offset: i32,
187 sign_r: i32,
188 col_offset: i32,
189 sign_c: i32,
190) -> Sample {
191 let x = col_offset * MI_SIZE + sign_c * bw / 2 - 1;
192 let y = row_offset * MI_SIZE + sign_r * bh / 2 - 1;
193 let pts1 = (x * 8, y * 8);
194 let pts2 = (pts1.0 + nb_mv.1, pts1.1 + nb_mv.0);
195 Sample { pts1, pts2 }
196}
197
198/// The resolved affine warp model, spec 7.11.3.6's `LocalWarpParams` --
199/// `wmmat[2..6]` (the 2x2 linear part) plus the shear decomposition
200/// `av1_get_shear_params` derives from it. `wmmat[0]`/`[1]` are the
201/// translation terms.
202#[derive(Clone, Copy, Debug)]
203pub struct WarpParams {
204 pub wmmat: [i32; 6],
205 pub alpha: i16,
206 pub beta: i16,
207 pub gamma: i16,
208 pub delta: i16,
209}
210
211/// `LS_SQUARE`/`LS_PRODUCT1`/`LS_PRODUCT2` (`warped_motion.c`): the
212/// downshifted quadratic terms `find_affine_int`'s normal-equation matrix
213/// accumulates, exploiting `LS_STEP == 8` (bottom 2 bits of every term are 0).
214fn ls_square(a: i64) -> i64 {
215 (a * a * 4 + a * 4 * LS_STEP as i64 + (LS_STEP * LS_STEP * 2) as i64) >> 4
216}
217fn ls_product1(a: i64, b: i64) -> i64 {
218 (a * b * 4 + (a + b) * 2 * LS_STEP as i64 + (LS_STEP * LS_STEP) as i64) >> 4
219}
220fn ls_product2(a: i64, b: i64) -> i64 {
221 (a * b * 4 + (a + b) * 2 * LS_STEP as i64 + (LS_STEP * LS_STEP * 2) as i64) >> 4
222}
223
224fn get_mult_shift_ndiag(px: i64, i_det: i64, shift: i32) -> i32 {
225 let v = px * i_det;
226 clamp(
227 round2_signed(v, shift as u32),
228 -WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
229 WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1,
230 ) as i32
231}
232
233fn get_mult_shift_diag(px: i64, i_det: i64, shift: i32) -> i32 {
234 let v = px * i_det;
235 clamp(
236 round2_signed(v, shift as u32),
237 (1i64 << WARPEDMODEL_PREC_BITS) - WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
238 (1i64 << WARPEDMODEL_PREC_BITS) + WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1,
239 ) as i32
240}
241
242/// `find_affine_int` (`warped_motion.c`): the least-squares affine fit over
243/// `samples`, `bw`/`bh` the current block's own size in luma pixels, `(mvx,
244/// mvy)` its own coded motion vector (1/8-pel, spec's own `mvx`/`mvy` order:
245/// `col`, `row`), `mi_row`/`mi_col` its own `mi` position. Returns `None`
246/// when the accumulated matrix is singular (`Det == 0`, spec's "found = 0"
247/// path -- caller still falls back to the block's own translational MV,
248/// matching libaom's own shear-validity fallback).
249fn find_affine_int(
250 samples: &[Sample],
251 bw: i32,
252 bh: i32,
253 mvx: i32,
254 mvy: i32,
255 mi_row: i32,
256 mi_col: i32,
257) -> Option<[i32; 6]> {
258 let mut a = [[0i64; 2]; 2];
259 let mut bx = [0i64; 2];
260 let mut by = [0i64; 2];
261
262 let rsuy = bh / 2 - 1;
263 let rsux = bw / 2 - 1;
264 let suy = rsuy * 8;
265 let sux = rsux * 8;
266 let duy = suy + mvy;
267 let dux = sux + mvx;
268
269 for s in samples {
270 let dx = s.pts2.0 - dux;
271 let dy = s.pts2.1 - duy;
272 let sx = s.pts1.0 - sux;
273 let sy = s.pts1.1 - suy;
274 if (sx - dx).abs() < LS_MV_MAX && (sy - dy).abs() < LS_MV_MAX {
275 let (sx, sy, dx, dy) = (sx as i64, sy as i64, dx as i64, dy as i64);
276 a[0][0] += ls_square(sx);
277 a[0][1] += ls_product1(sx, sy);
278 a[1][1] += ls_square(sy);
279 bx[0] += ls_product2(sx, dx);
280 bx[1] += ls_product1(sy, dx);
281 by[0] += ls_product1(sx, dy);
282 by[1] += ls_product2(sy, dy);
283 }
284 }
285
286 let det = a[0][0] * a[1][1] - a[0][1] * a[0][1];
287 if det == 0 {
288 return None;
289 }
290
291 let (mut i_det, mut shift) = resolve_divisor_64(det.unsigned_abs());
292 if det < 0 {
293 i_det = -i_det;
294 }
295 shift -= WARPEDMODEL_PREC_BITS as i32;
296 if shift < 0 {
297 i_det <<= -shift;
298 shift = 0;
299 }
300
301 let px0 = a[1][1] * bx[0] - a[0][1] * bx[1];
302 let px1 = -a[0][1] * bx[0] + a[0][0] * bx[1];
303 let py0 = a[1][1] * by[0] - a[0][1] * by[1];
304 let py1 = -a[0][1] * by[0] + a[0][0] * by[1];
305
306 let mut wmmat = [0i32; 6];
307 wmmat[2] = get_mult_shift_diag(px0, i_det, shift);
308 wmmat[3] = get_mult_shift_ndiag(px1, i_det, shift);
309 wmmat[4] = get_mult_shift_ndiag(py0, i_det, shift);
310 wmmat[5] = get_mult_shift_diag(py1, i_det, shift);
311
312 let isuy = (mi_row * MI_SIZE + rsuy) as i64;
313 let isux = (mi_col * MI_SIZE + rsux) as i64;
314 let vx = mvx as i64 * (1i64 << (WARPEDMODEL_PREC_BITS - 3))
315 - (isux * (wmmat[2] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)) + isuy * wmmat[3] as i64);
316 let vy = mvy as i64 * (1i64 << (WARPEDMODEL_PREC_BITS - 3))
317 - (isux * wmmat[4] as i64 + isuy * (wmmat[5] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)));
318 wmmat[0] = clamp(vx, -WARPEDMODEL_TRANS_CLAMP, WARPEDMODEL_TRANS_CLAMP - 1) as i32;
319 wmmat[1] = clamp(vy, -WARPEDMODEL_TRANS_CLAMP, WARPEDMODEL_TRANS_CLAMP - 1) as i32;
320 Some(wmmat)
321}
322
323/// `av1_get_shear_params`: `None` on an invalid model (`mat[2] <= 0`, or the
324/// reduced shear terms fail `is_affine_shear_allowed` -- spec's own
325/// "warp filter cannot represent this" fallback).
326fn get_shear_params(wmmat: [i32; 6]) -> Option<(i16, i16, i16, i16)> {
327 if wmmat[2] <= 0 {
328 return None;
329 }
330 let mat = wmmat.map(|v| v as i64);
331 let mut alpha = clamp(mat[2] - (1 << WARPEDMODEL_PREC_BITS), i16::MIN as i64, i16::MAX as i64);
332 let beta = clamp(mat[3], i16::MIN as i64, i16::MAX as i64);
333 let (y_mag, shift) = resolve_divisor_32(mat[2].unsigned_abs() as u32);
334 let y = if mat[2] < 0 { -y_mag } else { y_mag };
335 let v = (mat[4] * (1 << WARPEDMODEL_PREC_BITS)) * y;
336 let mut gamma = clamp(round2_signed(v, shift as u32), i16::MIN as i64, i16::MAX as i64);
337 let v = (mat[3] * mat[4]) * y;
338 let mut delta = clamp(
339 mat[5] - round2_signed(v, shift as u32) - (1 << WARPEDMODEL_PREC_BITS),
340 i16::MIN as i64,
341 i16::MAX as i64,
342 );
343
344 alpha = round2_signed(alpha, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
345 let beta = round2_signed(beta, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
346 gamma = round2_signed(gamma, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
347 delta = round2_signed(delta, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
348
349 if (4 * alpha.abs() + 7 * beta.abs() >= (1 << WARPEDMODEL_PREC_BITS))
350 || (4 * gamma.abs() + 4 * delta.abs() >= (1 << WARPEDMODEL_PREC_BITS))
351 {
352 return None;
353 }
354 Some((alpha as i16, beta as i16, gamma as i16, delta as i16))
355}
356
357/// `av1_selectSamples` (`mvref_common.c`): keeps only the samples whose own
358/// MV agrees with the block's coded `mv` (`(row, col)`) within
359/// `clamp(max(bw, bh), 16, 112)` (1/8-pel), run only when more than one
360/// sample was found (`mbmi->num_proj_ref > 1` gate, `decodemv.c`). Always
361/// keeps at least one (the C code's own `AOMMAX(ret, 1)` floor -- this port
362/// mirrors it by never truncating below index 1 when the filter would
363/// otherwise empty the vec).
364pub fn select_samples(mv: (i32, i32), samples: &mut Vec<Sample>, bw: i32, bh: i32) {
365 let thresh = bw.max(bh).clamp(16, 112);
366 let mut kept: Vec<Sample> = samples
367 .iter()
368 .copied()
369 .filter(|s| {
370 let dx = s.pts2.0 - s.pts1.0 - mv.1;
371 let dy = s.pts2.1 - s.pts1.1 - mv.0;
372 dx.abs() + dy.abs() <= thresh
373 })
374 .collect();
375 if kept.is_empty() {
376 kept.push(samples[0]);
377 }
378 *samples = kept;
379}
380
381/// `av1_find_projection`: estimates the affine model, `None` when either the
382/// least-squares fit is singular or the resulting shear is not
383/// filter-representable -- both spec fallback cases collapse to the same
384/// "use the block's own translational MV instead" caller behaviour.
385pub fn find_projection(
386 samples: &[Sample],
387 bw: i32,
388 bh: i32,
389 mvx: i32,
390 mvy: i32,
391 mi_row: i32,
392 mi_col: i32,
393) -> Option<WarpParams> {
394 let wmmat = find_affine_int(samples, bw, bh, mvx, mvy, mi_row, mi_col)?;
395 let (alpha, beta, gamma, delta) = get_shear_params(wmmat)?;
396 Some(WarpParams {
397 wmmat,
398 alpha,
399 beta,
400 gamma,
401 delta,
402 })
403}
404
405/// lane-gm r4: `allow_warp`'s `global_warp_allowed` branch
406/// (`reconinter.c:50`) -- reuses the SAME shear decomposition
407/// [`find_projection`]'s local-warp path calls (`get_shear_params`), just
408/// keyed on the frame's already-coded `global_motion[ref]` model instead of
409/// a per-block least-squares fit. Caller gates this on `is_global_mv_block
410/// && !global_motion[ref].invalid` (matching `allow_warp` exactly); `None`
411/// here means the model's own shear is not filter-representable even though
412/// the frame header's `warpValid` bit said otherwise for a `mat[2] <= 0`
413/// edge `get_shear_params` additionally rejects -- caller keeps its
414/// translational fallback in that rare case.
415pub fn global_warp_params(wmmat: [i32; 6]) -> Option<WarpParams> {
416 let (alpha, beta, gamma, delta) = get_shear_params(wmmat)?;
417 Some(WarpParams { wmmat, alpha, beta, gamma, delta })
418}
419
420fn clip_pixel(v: i32) -> u16 {
421 v.clamp(0, crate::decode::sample_max()) as u16
422}
423
424/// `av1_highbd_warp_affine_c` (`warped_motion.c`)'s non-compound branch:
425/// filters `reference` (row-major, `stride`, true extent `width`x`height`)
426/// through `params`' affine model into `dst` (row-major, `p_stride`), an
427/// `p_width`x`p_height` block whose top-left sits at `(p_col, p_row)` in the
428/// (possibly chroma-subsampled) plane `dst` belongs to. Bit depth comes from
429/// the stream (`crate::decode::bit_depth`), the same thread-local
430/// [`clip_pixel`] already clamps against -- libaom's `bd` parameter.
431#[allow(clippy::too_many_arguments)]
432pub fn warp_affine(
433 params: &WarpParams,
434 reference: &[u16],
435 width: i32,
436 height: i32,
437 stride: i32,
438 dst: &mut [u16],
439 p_col: i32,
440 p_row: i32,
441 p_width: i32,
442 p_height: i32,
443 p_stride: i32,
444 subsampling_x: i32,
445 subsampling_y: i32,
446) {
447 if std::env::var_os("EC_MC_TRACE").is_some() {
448 eprintln!("EC_MC_WARP x={p_col} y={p_row} w={p_width} h={p_height}");
449 }
450 let bd = i32::from(crate::decode::bit_depth());
451 // `reduce_bits_vert`, non-compound: `2 * FILTER_BITS - round_0`.
452 let reduce_bits_vert = 2 * FILTER_BITS - REDUCE_BITS_HORIZ;
453 warp_inner(
454 params, reference, width, height, stride, p_col, p_row, p_width, p_height,
455 subsampling_x, subsampling_y, bd, reduce_bits_vert,
456 |row, col, sum| {
457 dst[row * p_stride as usize + col] =
458 clip_pixel(sum - (1 << (bd - 1)) - (1 << bd));
459 },
460 );
461}
462
463/// lane-cwarp: `av1_highbd_warp_affine_c`'s `conv_params->is_compound`
464/// branch (`do_average == 0`, i.e. "write the CONV_BUF entry"): identical
465/// filter, but the vertical pass rounds by `conv_params->round_1`
466/// (`COMPOUND_ROUND1_BITS == 7`, [`mc::predict_compound_intermediate`]'s
467/// `INTER_ROUND_1_COMPOUND`) instead of `2*FILTER_BITS - round_0`, and the
468/// result stays in the compound intermediate domain rather than becoming a
469/// pixel. libaom keeps a constant bias in its CONV_BUF (the
470/// `1 << offset_bits_horiz` / `1 << offset_bits_vert` terms, which survive
471/// both roundings exactly because both exceed their shift) and subtracts
472/// `(1 << (offset_bits - round_1)) + (1 << (offset_bits - round_1 - 1))`
473/// at the blend; this crate's intermediates are UNBIASED (see
474/// [`mc::diffwtd_mask`]'s derivation), so the identical constant --
475/// `(1 << (bd + 4)) + (1 << (bd + 3))` -- is removed here instead, making
476/// the output directly interchangeable with
477/// [`mc::predict_compound_intermediate`]'s for
478/// [`mc::combine_compound`]/[`mc::blend_masked_compound`].
479#[allow(clippy::too_many_arguments)]
480pub fn warp_affine_compound(
481 params: &WarpParams,
482 reference: &[u16],
483 width: i32,
484 height: i32,
485 stride: i32,
486 dst: &mut [i32],
487 p_col: i32,
488 p_row: i32,
489 p_width: i32,
490 p_height: i32,
491 p_stride: i32,
492 subsampling_x: i32,
493 subsampling_y: i32,
494) {
495 let bd = i32::from(crate::decode::bit_depth());
496 let bias = (1 << (bd + 4)) + (1 << (bd + 3));
497 warp_inner(
498 params, reference, width, height, stride, p_col, p_row, p_width, p_height,
499 subsampling_x, subsampling_y, bd, COMPOUND_ROUND1_BITS,
500 |row, col, sum| {
501 dst[row * p_stride as usize + col] = sum - bias;
502 },
503 );
504}
505
506/// `conv_params->round_0` (`ROUND0_BITS`), the warp filter's horizontal
507/// rounding -- `InterRound0`, bit-depth independent for 8/10-bit content
508/// (libaom only raises it at `bd == 12`, which this decoder refuses).
509const REDUCE_BITS_HORIZ: i32 = 3;
510/// `COMPOUND_ROUND1_BITS` (`convolve.h`): `conv_params->round_1` when
511/// `is_compound`.
512const COMPOUND_ROUND1_BITS: i32 = 7;
513
514/// The shared body of [`warp_affine`]/[`warp_affine_compound`]
515/// (`av1_highbd_warp_affine_c`): everything up to and including the
516/// vertical `ROUND_POWER_OF_TWO(sum, reduce_bits_vert)`, with `store`
517/// receiving `(row, col, sum)` in block-local coordinates.
518#[allow(clippy::too_many_arguments)]
519fn warp_inner(
520 params: &WarpParams,
521 reference: &[u16],
522 width: i32,
523 height: i32,
524 stride: i32,
525 p_col: i32,
526 p_row: i32,
527 p_width: i32,
528 p_height: i32,
529 subsampling_x: i32,
530 subsampling_y: i32,
531 bd: i32,
532 reduce_bits_vert: i32,
533 mut store: impl FnMut(usize, usize, i32),
534) {
535 let mat = &params.wmmat;
536 let (alpha, beta, gamma, delta) = (
537 params.alpha as i64,
538 params.beta as i64,
539 params.gamma as i64,
540 params.delta as i64,
541 );
542 let reduce_bits_horiz = REDUCE_BITS_HORIZ;
543 let offset_bits_horiz = bd + FILTER_BITS - 1;
544 let offset_bits_vert = bd + 2 * FILTER_BITS - reduce_bits_horiz;
545
546 let mut i = p_row;
547 while i < p_row + p_height {
548 let mut j = p_col;
549 while j < p_col + p_width {
550 let src_x = ((j + 4) << subsampling_x) as i64;
551 let src_y = ((i + 4) << subsampling_y) as i64;
552 let dst_x = mat[2] as i64 * src_x + mat[3] as i64 * src_y + mat[0] as i64;
553 let dst_y = mat[4] as i64 * src_x + mat[5] as i64 * src_y + mat[1] as i64;
554 let x4 = dst_x >> subsampling_x;
555 let y4 = dst_y >> subsampling_y;
556
557 let ix4 = (x4 >> WARPEDMODEL_PREC_BITS) as i32;
558 let mut sx4 = (x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i64;
559 let iy4 = (y4 >> WARPEDMODEL_PREC_BITS) as i32;
560 let mut sy4 = (y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i64;
561
562 sx4 += alpha * -4 + beta * -4;
563 sy4 += gamma * -4 + delta * -4;
564 sx4 &= !((1i64 << WARP_PARAM_REDUCE_BITS) - 1);
565 sy4 &= !((1i64 << WARP_PARAM_REDUCE_BITS) - 1);
566
567 let mut tmp = [[0i32; 8]; 15];
568 for k in -7..8i32 {
569 let iy = (iy4 + k).clamp(0, height - 1);
570 let mut sx = sx4 + beta * (k as i64 + 4);
571 for l in -4..4i32 {
572 let ix = ix4 + l - 3;
573 let offs =
574 (round2(sx, WARPEDDIFF_PREC_BITS) as i32 + WARPEDPIXEL_PREC_SHIFTS) as usize;
575 let coeffs = &AV1_WARPED_FILTER[offs];
576 let mut sum: i32 = 1 << offset_bits_horiz;
577 for (m, &c) in coeffs.iter().enumerate() {
578 let sample_x = (ix + m as i32).clamp(0, width - 1);
579 sum += i32::from(reference[(iy * stride + sample_x) as usize]) * c as i32;
580 }
581 sum = round2(sum as i64, reduce_bits_horiz as u32) as i32;
582 tmp[(k + 7) as usize][(l + 4) as usize] = sum;
583 sx += alpha;
584 }
585 }
586
587 let k_hi = (4i32).min(p_row + p_height - i - 4);
588 let mut k = -4i32;
589 while k < k_hi {
590 let mut sy = sy4 + delta * (k as i64 + 4);
591 let l_hi = (4i32).min(p_col + p_width - j - 4);
592 let mut l = -4i32;
593 while l < l_hi {
594 let offs =
595 (round2(sy, WARPEDDIFF_PREC_BITS) as i32 + WARPEDPIXEL_PREC_SHIFTS) as usize;
596 let coeffs = &AV1_WARPED_FILTER[offs];
597 let mut sum: i32 = 1 << offset_bits_vert;
598 for m in 0..8i32 {
599 sum += tmp[(k + m + 4) as usize][(l + 4) as usize] * coeffs[m as usize] as i32;
600 }
601 sum = round2(sum as i64, reduce_bits_vert as u32) as i32;
602 store(
603 (i - p_row + k + 4) as usize,
604 (j - p_col + l + 4) as usize,
605 sum,
606 );
607 sy += gamma;
608 l += 1;
609 }
610 k += 1;
611 }
612 j += 8;
613 }
614 i += 8;
615 }
616}
617
618/// `av1_warped_filter` (`warped_motion.c`): 193 rows (`WARPEDPIXEL_PREC_SHIFTS
619/// * 3 + 1`), one per 1/64-pel fraction across `[-1, 2)`, each an 8-tap
620/// kernel (row 192 is a dummy replicate of row 191, transcribed verbatim).
621#[rustfmt::skip]
622const AV1_WARPED_FILTER: [[i16; 8]; 193] = [
623 [0, 0, 127, 1, 0, 0, 0, 0],
624 [0, -1, 127, 2, 0, 0, 0, 0],
625 [1, -3, 127, 4, -1, 0, 0, 0],
626 [1, -4, 126, 6, -2, 1, 0, 0],
627 [1, -5, 126, 8, -3, 1, 0, 0],
628 [1, -6, 125, 11, -4, 1, 0, 0],
629 [1, -7, 124, 13, -4, 1, 0, 0],
630 [2, -8, 123, 15, -5, 1, 0, 0],
631 [2, -9, 122, 18, -6, 1, 0, 0],
632 [2, -10, 121, 20, -6, 1, 0, 0],
633 [2, -11, 120, 22, -7, 2, 0, 0],
634 [2, -12, 119, 25, -8, 2, 0, 0],
635 [3, -13, 117, 27, -8, 2, 0, 0],
636 [3, -13, 116, 29, -9, 2, 0, 0],
637 [3, -14, 114, 32, -10, 3, 0, 0],
638 [3, -15, 113, 35, -10, 2, 0, 0],
639 [3, -15, 111, 37, -11, 3, 0, 0],
640 [3, -16, 109, 40, -11, 3, 0, 0],
641 [3, -16, 108, 42, -12, 3, 0, 0],
642 [4, -17, 106, 45, -13, 3, 0, 0],
643 [4, -17, 104, 47, -13, 3, 0, 0],
644 [4, -17, 102, 50, -14, 3, 0, 0],
645 [4, -17, 100, 52, -14, 3, 0, 0],
646 [4, -18, 98, 55, -15, 4, 0, 0],
647 [4, -18, 96, 58, -15, 3, 0, 0],
648 [4, -18, 94, 60, -16, 4, 0, 0],
649 [4, -18, 91, 63, -16, 4, 0, 0],
650 [4, -18, 89, 65, -16, 4, 0, 0],
651 [4, -18, 87, 68, -17, 4, 0, 0],
652 [4, -18, 85, 70, -17, 4, 0, 0],
653 [4, -18, 82, 73, -17, 4, 0, 0],
654 [4, -18, 80, 75, -17, 4, 0, 0],
655 [4, -18, 78, 78, -18, 4, 0, 0],
656 [4, -17, 75, 80, -18, 4, 0, 0],
657 [4, -17, 73, 82, -18, 4, 0, 0],
658 [4, -17, 70, 85, -18, 4, 0, 0],
659 [4, -17, 68, 87, -18, 4, 0, 0],
660 [4, -16, 65, 89, -18, 4, 0, 0],
661 [4, -16, 63, 91, -18, 4, 0, 0],
662 [4, -16, 60, 94, -18, 4, 0, 0],
663 [3, -15, 58, 96, -18, 4, 0, 0],
664 [4, -15, 55, 98, -18, 4, 0, 0],
665 [3, -14, 52, 100, -17, 4, 0, 0],
666 [3, -14, 50, 102, -17, 4, 0, 0],
667 [3, -13, 47, 104, -17, 4, 0, 0],
668 [3, -13, 45, 106, -17, 4, 0, 0],
669 [3, -12, 42, 108, -16, 3, 0, 0],
670 [3, -11, 40, 109, -16, 3, 0, 0],
671 [3, -11, 37, 111, -15, 3, 0, 0],
672 [2, -10, 35, 113, -15, 3, 0, 0],
673 [3, -10, 32, 114, -14, 3, 0, 0],
674 [2, -9, 29, 116, -13, 3, 0, 0],
675 [2, -8, 27, 117, -13, 3, 0, 0],
676 [2, -8, 25, 119, -12, 2, 0, 0],
677 [2, -7, 22, 120, -11, 2, 0, 0],
678 [1, -6, 20, 121, -10, 2, 0, 0],
679 [1, -6, 18, 122, -9, 2, 0, 0],
680 [1, -5, 15, 123, -8, 2, 0, 0],
681 [1, -4, 13, 124, -7, 1, 0, 0],
682 [1, -4, 11, 125, -6, 1, 0, 0],
683 [1, -3, 8, 126, -5, 1, 0, 0],
684 [1, -2, 6, 126, -4, 1, 0, 0],
685 [0, -1, 4, 127, -3, 1, 0, 0],
686 [0, 0, 2, 127, -1, 0, 0, 0],
687 [0, 0, 0, 127, 1, 0, 0, 0],
688 [0, 0, -1, 127, 2, 0, 0, 0],
689 [0, 1, -3, 127, 4, -2, 1, 0],
690 [0, 1, -5, 127, 6, -2, 1, 0],
691 [0, 2, -6, 126, 8, -3, 1, 0],
692 [-1, 2, -7, 126, 11, -4, 2, -1],
693 [-1, 3, -8, 125, 13, -5, 2, -1],
694 [-1, 3, -10, 124, 16, -6, 3, -1],
695 [-1, 4, -11, 123, 18, -7, 3, -1],
696 [-1, 4, -12, 122, 20, -7, 3, -1],
697 [-1, 4, -13, 121, 23, -8, 3, -1],
698 [-2, 5, -14, 120, 25, -9, 4, -1],
699 [-1, 5, -15, 119, 27, -10, 4, -1],
700 [-1, 5, -16, 118, 30, -11, 4, -1],
701 [-2, 6, -17, 116, 33, -12, 5, -1],
702 [-2, 6, -17, 114, 35, -12, 5, -1],
703 [-2, 6, -18, 113, 38, -13, 5, -1],
704 [-2, 7, -19, 111, 41, -14, 6, -2],
705 [-2, 7, -19, 110, 43, -15, 6, -2],
706 [-2, 7, -20, 108, 46, -15, 6, -2],
707 [-2, 7, -20, 106, 49, -16, 6, -2],
708 [-2, 7, -21, 104, 51, -16, 7, -2],
709 [-2, 7, -21, 102, 54, -17, 7, -2],
710 [-2, 8, -21, 100, 56, -18, 7, -2],
711 [-2, 8, -22, 98, 59, -18, 7, -2],
712 [-2, 8, -22, 96, 62, -19, 7, -2],
713 [-2, 8, -22, 94, 64, -19, 7, -2],
714 [-2, 8, -22, 91, 67, -20, 8, -2],
715 [-2, 8, -22, 89, 69, -20, 8, -2],
716 [-2, 8, -22, 87, 72, -21, 8, -2],
717 [-2, 8, -21, 84, 74, -21, 8, -2],
718 [-2, 8, -22, 82, 77, -21, 8, -2],
719 [-2, 8, -21, 79, 79, -21, 8, -2],
720 [-2, 8, -21, 77, 82, -22, 8, -2],
721 [-2, 8, -21, 74, 84, -21, 8, -2],
722 [-2, 8, -21, 72, 87, -22, 8, -2],
723 [-2, 8, -20, 69, 89, -22, 8, -2],
724 [-2, 8, -20, 67, 91, -22, 8, -2],
725 [-2, 7, -19, 64, 94, -22, 8, -2],
726 [-2, 7, -19, 62, 96, -22, 8, -2],
727 [-2, 7, -18, 59, 98, -22, 8, -2],
728 [-2, 7, -18, 56, 100, -21, 8, -2],
729 [-2, 7, -17, 54, 102, -21, 7, -2],
730 [-2, 7, -16, 51, 104, -21, 7, -2],
731 [-2, 6, -16, 49, 106, -20, 7, -2],
732 [-2, 6, -15, 46, 108, -20, 7, -2],
733 [-2, 6, -15, 43, 110, -19, 7, -2],
734 [-2, 6, -14, 41, 111, -19, 7, -2],
735 [-1, 5, -13, 38, 113, -18, 6, -2],
736 [-1, 5, -12, 35, 114, -17, 6, -2],
737 [-1, 5, -12, 33, 116, -17, 6, -2],
738 [-1, 4, -11, 30, 118, -16, 5, -1],
739 [-1, 4, -10, 27, 119, -15, 5, -1],
740 [-1, 4, -9, 25, 120, -14, 5, -2],
741 [-1, 3, -8, 23, 121, -13, 4, -1],
742 [-1, 3, -7, 20, 122, -12, 4, -1],
743 [-1, 3, -7, 18, 123, -11, 4, -1],
744 [-1, 3, -6, 16, 124, -10, 3, -1],
745 [-1, 2, -5, 13, 125, -8, 3, -1],
746 [-1, 2, -4, 11, 126, -7, 2, -1],
747 [0, 1, -3, 8, 126, -6, 2, 0],
748 [0, 1, -2, 6, 127, -5, 1, 0],
749 [0, 1, -2, 4, 127, -3, 1, 0],
750 [0, 0, 0, 2, 127, -1, 0, 0],
751 [0, 0, 0, 1, 127, 0, 0, 0],
752 [0, 0, 0, -1, 127, 2, 0, 0],
753 [0, 0, 1, -3, 127, 4, -1, 0],
754 [0, 0, 1, -4, 126, 6, -2, 1],
755 [0, 0, 1, -5, 126, 8, -3, 1],
756 [0, 0, 1, -6, 125, 11, -4, 1],
757 [0, 0, 1, -7, 124, 13, -4, 1],
758 [0, 0, 2, -8, 123, 15, -5, 1],
759 [0, 0, 2, -9, 122, 18, -6, 1],
760 [0, 0, 2, -10, 121, 20, -6, 1],
761 [0, 0, 2, -11, 120, 22, -7, 2],
762 [0, 0, 2, -12, 119, 25, -8, 2],
763 [0, 0, 3, -13, 117, 27, -8, 2],
764 [0, 0, 3, -13, 116, 29, -9, 2],
765 [0, 0, 3, -14, 114, 32, -10, 3],
766 [0, 0, 3, -15, 113, 35, -10, 2],
767 [0, 0, 3, -15, 111, 37, -11, 3],
768 [0, 0, 3, -16, 109, 40, -11, 3],
769 [0, 0, 3, -16, 108, 42, -12, 3],
770 [0, 0, 4, -17, 106, 45, -13, 3],
771 [0, 0, 4, -17, 104, 47, -13, 3],
772 [0, 0, 4, -17, 102, 50, -14, 3],
773 [0, 0, 4, -17, 100, 52, -14, 3],
774 [0, 0, 4, -18, 98, 55, -15, 4],
775 [0, 0, 4, -18, 96, 58, -15, 3],
776 [0, 0, 4, -18, 94, 60, -16, 4],
777 [0, 0, 4, -18, 91, 63, -16, 4],
778 [0, 0, 4, -18, 89, 65, -16, 4],
779 [0, 0, 4, -18, 87, 68, -17, 4],
780 [0, 0, 4, -18, 85, 70, -17, 4],
781 [0, 0, 4, -18, 82, 73, -17, 4],
782 [0, 0, 4, -18, 80, 75, -17, 4],
783 [0, 0, 4, -18, 78, 78, -18, 4],
784 [0, 0, 4, -17, 75, 80, -18, 4],
785 [0, 0, 4, -17, 73, 82, -18, 4],
786 [0, 0, 4, -17, 70, 85, -18, 4],
787 [0, 0, 4, -17, 68, 87, -18, 4],
788 [0, 0, 4, -16, 65, 89, -18, 4],
789 [0, 0, 4, -16, 63, 91, -18, 4],
790 [0, 0, 4, -16, 60, 94, -18, 4],
791 [0, 0, 3, -15, 58, 96, -18, 4],
792 [0, 0, 4, -15, 55, 98, -18, 4],
793 [0, 0, 3, -14, 52, 100, -17, 4],
794 [0, 0, 3, -14, 50, 102, -17, 4],
795 [0, 0, 3, -13, 47, 104, -17, 4],
796 [0, 0, 3, -13, 45, 106, -17, 4],
797 [0, 0, 3, -12, 42, 108, -16, 3],
798 [0, 0, 3, -11, 40, 109, -16, 3],
799 [0, 0, 3, -11, 37, 111, -15, 3],
800 [0, 0, 2, -10, 35, 113, -15, 3],
801 [0, 0, 3, -10, 32, 114, -14, 3],
802 [0, 0, 2, -9, 29, 116, -13, 3],
803 [0, 0, 2, -8, 27, 117, -13, 3],
804 [0, 0, 2, -8, 25, 119, -12, 2],
805 [0, 0, 2, -7, 22, 120, -11, 2],
806 [0, 0, 1, -6, 20, 121, -10, 2],
807 [0, 0, 1, -6, 18, 122, -9, 2],
808 [0, 0, 1, -5, 15, 123, -8, 2],
809 [0, 0, 1, -4, 13, 124, -7, 1],
810 [0, 0, 1, -4, 11, 125, -6, 1],
811 [0, 0, 1, -3, 8, 126, -5, 1],
812 [0, 0, 1, -2, 6, 126, -4, 1],
813 [0, 0, 0, -1, 4, 127, -3, 1],
814 [0, 0, 0, 0, 2, 127, -1, 0],
815 [0, 0, 0, 0, 2, 127, -1, 0],
816];
817
818/// `div_lut` (`warped_motion.c`).
819#[rustfmt::skip]
820const DIV_LUT: [u16; 257] = [
821 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
822 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
823 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
824 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
825 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
826 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
827 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
828 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
829 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
830 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
831 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
832 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
833 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
834 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
835 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
836 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
837 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
838 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
839 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
840 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
841 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
842 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
843 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
844 8240, 8224, 8208, 8192,
845];
846
847#[cfg(test)]
848mod gm_mv_tests {
849 use super::gm_get_motion_vector;
850 use ec_av1_syntax::WarpModel;
851
852 /// `TRANSLATION`: `row = wmmat[0] >> 13`, `col = wmmat[1] >> 13` (spec's
853 /// own swapped-axis bug, kept). `100000 >> 13 = 12` (`100000 / 8192 =
854 /// 12.207`, floor 12); `-50000 >> 13 = -7` (`-50000 / 8192 = -6.1035`,
855 /// floor -7, arithmetic shift rounds toward -inf).
856 #[test]
857 fn translation_shifts_params_by_13() {
858 let params = [100_000, -50_000, 1 << 16, 0, 0, 1 << 16];
859 let mv = gm_get_motion_vector(WarpModel::Translation, &params, 0, 0, 2, 2, true, false);
860 assert_eq!(mv, (12, -7));
861 }
862
863 /// `ROTZOOM`, block centre `x = mi_col*4 + bw4*4/2 = 2*4 + 4*4/2 = 16`,
864 /// `y = mi_row*4 + bh4*4/2 = 1*4 + 4*4/2 = 12`.
865 /// `xc = (params[2]-2^16)*x + params[3]*y + params[0]`
866 /// `= 500*16 + 100*12 + 1000 = 8000 + 1200 + 1000 = 10200`.
867 /// `yc = params[4]*x + (params[5]-2^16)*y + params[1]`
868 /// `= -50*16 + (-300)*12 + 2000 = -800 - 3600 + 2000 = -2400`.
869 /// `hp`: `row = Round2Signed(yc,13) = -Round2(2400,13) = -((2400+4096)>>13)
870 /// = -(6496>>13) = -0 = 0`; `col = Round2Signed(xc,13) =
871 /// (10200+4096)>>13 = 14296>>13 = 1`.
872 #[test]
873 fn rotzoom_projects_block_centre_hp() {
874 let params = [1000, 2000, (1 << 16) + 500, 100, -50, (1 << 16) - 300];
875 let mv = gm_get_motion_vector(WarpModel::Rotzoom, &params, 1, 2, 4, 4, true, false);
876 assert_eq!(mv, (0, 1));
877 }
878
879 /// Same model/geometry as [`rotzoom_projects_block_centre_hp`], not high
880 /// precision: `convert_to_trans_prec` shifts by 14 instead of 13 then
881 /// doubles. `row = -Round2(2400,14)*2 = -((2400+8192)>>14)*2 =
882 /// -(10592>>14)*2 = 0`; `col = Round2(10200,14)*2 = ((10200+8192)>>14)*2
883 /// = (18392>>14)*2 = 1*2 = 2`.
884 #[test]
885 fn rotzoom_projects_block_centre_no_hp() {
886 let params = [1000, 2000, (1 << 16) + 500, 100, -50, (1 << 16) - 300];
887 let mv = gm_get_motion_vector(WarpModel::Rotzoom, &params, 1, 2, 4, 4, false, false);
888 assert_eq!(mv, (0, 2));
889 }
890
891 /// `force_integer_mv`'s `integer_mv_precision`, applied to
892 /// [`translation_shifts_params_by_13`]'s raw `(12, -7)`: `12 % 8 = 4`,
893 /// `abs(4) > 4` false, so `12 -> 12-4 = 8`. `-7 % 8 = -7` (Rust's
894 /// truncating `%`), `abs(-7) > 4` true and `-7 < 0`, so
895 /// `-7 -> (-7 - (-7)) - 8 = -8`.
896 #[test]
897 fn translation_force_integer_mv_rounds_to_full_pel() {
898 let params = [100_000, -50_000, 1 << 16, 0, 0, 1 << 16];
899 let mv = gm_get_motion_vector(WarpModel::Translation, &params, 0, 0, 2, 2, true, true);
900 assert_eq!(mv, (8, -8));
901 }
902
903 #[test]
904 fn identity_is_always_zero() {
905 let params = [999, -999, (1 << 16) + 1, 5, 5, (1 << 16) - 1];
906 assert_eq!(
907 gm_get_motion_vector(WarpModel::Identity, &params, 3, 4, 4, 4, true, false),
908 (0, 0)
909 );
910 }
911}