forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

warp.rs973 lines, 36.8 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(
332 mat[2] - (1 << WARPEDMODEL_PREC_BITS),
333 i16::MIN as i64,
334 i16::MAX as i64,
335 );
336 let beta = clamp(mat[3], i16::MIN as i64, i16::MAX as i64);
337 let (y_mag, shift) = resolve_divisor_32(mat[2].unsigned_abs() as u32);
338 let y = if mat[2] < 0 { -y_mag } else { y_mag };
339 let v = (mat[4] * (1 << WARPEDMODEL_PREC_BITS)) * y;
340 let mut gamma = clamp(
341 round2_signed(v, shift as u32),
342 i16::MIN as i64,
343 i16::MAX as i64,
344 );
345 let v = (mat[3] * mat[4]) * y;
346 let mut delta = clamp(
347 mat[5] - round2_signed(v, shift as u32) - (1 << WARPEDMODEL_PREC_BITS),
348 i16::MIN as i64,
349 i16::MAX as i64,
350 );
351
352 alpha = round2_signed(alpha, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
353 let beta = round2_signed(beta, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
354 gamma = round2_signed(gamma, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
355 delta = round2_signed(delta, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS;
356
357 if (4 * alpha.abs() + 7 * beta.abs() >= (1 << WARPEDMODEL_PREC_BITS))
358 || (4 * gamma.abs() + 4 * delta.abs() >= (1 << WARPEDMODEL_PREC_BITS))
359 {
360 return None;
361 }
362 Some((alpha as i16, beta as i16, gamma as i16, delta as i16))
363}
364
365/// `av1_selectSamples` (`mvref_common.c`): keeps only the samples whose own
366/// MV agrees with the block's coded `mv` (`(row, col)`) within
367/// `clamp(max(bw, bh), 16, 112)` (1/8-pel), run only when more than one
368/// sample was found (`mbmi->num_proj_ref > 1` gate, `decodemv.c`). Always
369/// keeps at least one (the C code's own `AOMMAX(ret, 1)` floor -- this port
370/// mirrors it by never truncating below index 1 when the filter would
371/// otherwise empty the vec).
372pub fn select_samples(mv: (i32, i32), samples: &mut Vec<Sample>, bw: i32, bh: i32) {
373 let thresh = bw.max(bh).clamp(16, 112);
374 let mut kept: Vec<Sample> = samples
375 .iter()
376 .copied()
377 .filter(|s| {
378 let dx = s.pts2.0 - s.pts1.0 - mv.1;
379 let dy = s.pts2.1 - s.pts1.1 - mv.0;
380 dx.abs() + dy.abs() <= thresh
381 })
382 .collect();
383 if kept.is_empty() {
384 kept.push(samples[0]);
385 }
386 *samples = kept;
387}
388
389/// `av1_find_projection`: estimates the affine model, `None` when either the
390/// least-squares fit is singular or the resulting shear is not
391/// filter-representable -- both spec fallback cases collapse to the same
392/// "use the block's own translational MV instead" caller behaviour.
393pub fn find_projection(
394 samples: &[Sample],
395 bw: i32,
396 bh: i32,
397 mvx: i32,
398 mvy: i32,
399 mi_row: i32,
400 mi_col: i32,
401) -> Option<WarpParams> {
402 let wmmat = find_affine_int(samples, bw, bh, mvx, mvy, mi_row, mi_col)?;
403 let (alpha, beta, gamma, delta) = get_shear_params(wmmat)?;
404 Some(WarpParams {
405 wmmat,
406 alpha,
407 beta,
408 gamma,
409 delta,
410 })
411}
412
413/// lane-gm r4: `allow_warp`'s `global_warp_allowed` branch
414/// (`reconinter.c:50`) -- reuses the SAME shear decomposition
415/// [`find_projection`]'s local-warp path calls (`get_shear_params`), just
416/// keyed on the frame's already-coded `global_motion[ref]` model instead of
417/// a per-block least-squares fit. Caller gates this on `is_global_mv_block
418/// && !global_motion[ref].invalid` (matching `allow_warp` exactly); `None`
419/// here means the model's own shear is not filter-representable even though
420/// the frame header's `warpValid` bit said otherwise for a `mat[2] <= 0`
421/// edge `get_shear_params` additionally rejects -- caller keeps its
422/// translational fallback in that rare case.
423pub fn global_warp_params(wmmat: [i32; 6]) -> Option<WarpParams> {
424 let (alpha, beta, gamma, delta) = get_shear_params(wmmat)?;
425 Some(WarpParams {
426 wmmat,
427 alpha,
428 beta,
429 gamma,
430 delta,
431 })
432}
433
434fn clip_pixel(v: i32, fctx: &crate::decode::FrameCtx) -> u16 {
435 v.clamp(0, crate::decode::sample_max(fctx)) as u16
436}
437
438/// `av1_highbd_warp_affine_c` (`warped_motion.c`)'s non-compound branch:
439/// filters `reference` (row-major, `stride`, true extent `width`x`height`)
440/// through `params`' affine model into `dst` (row-major, `p_stride`), an
441/// `p_width`x`p_height` block whose top-left sits at `(p_col, p_row)` in the
442/// (possibly chroma-subsampled) plane `dst` belongs to. Bit depth comes from
443/// the stream (`crate::decode::bit_depth`), the same thread-local
444/// [`clip_pixel`] already clamps against -- libaom's `bd` parameter.
445#[allow(clippy::too_many_arguments)]
446pub(crate) fn warp_affine(
447 params: &WarpParams,
448 reference: &[u16],
449 width: i32,
450 height: i32,
451 stride: i32,
452 dst: &mut [u16],
453 p_col: i32,
454 p_row: i32,
455 p_width: i32,
456 p_height: i32,
457 p_stride: i32,
458 subsampling_x: i32,
459 subsampling_y: i32,
460 fctx: &crate::decode::FrameCtx,
461) {
462 if crate::envflags::env_flag!("EC_MC_TRACE") {
463 eprintln!("EC_MC_WARP x={p_col} y={p_row} w={p_width} h={p_height}");
464 }
465 let bd = i32::from(crate::decode::bit_depth(fctx));
466 // `reduce_bits_vert`, non-compound: `2 * FILTER_BITS - round_0` (the
467 // bumped `conv_params->round_0`, [`warp_round_0`]).
468 let reduce_bits_vert = 2 * FILTER_BITS - warp_round_0(bd);
469 warp_inner(
470 params,
471 reference,
472 width,
473 height,
474 stride,
475 p_col,
476 p_row,
477 p_width,
478 p_height,
479 subsampling_x,
480 subsampling_y,
481 bd,
482 reduce_bits_vert,
483 |row, col, sum| {
484 dst[row * p_stride as usize + col] =
485 clip_pixel(sum - (1 << (bd - 1)) - (1 << bd), fctx);
486 },
487 );
488}
489
490/// lane-cwarp: `av1_highbd_warp_affine_c`'s `conv_params->is_compound`
491/// branch (`do_average == 0`, i.e. "write the CONV_BUF entry"): identical
492/// filter, but the vertical pass rounds by `conv_params->round_1`
493/// (`COMPOUND_ROUND1_BITS == 7`, [`mc::predict_compound_intermediate`]'s
494/// `INTER_ROUND_1_COMPOUND`) instead of `2*FILTER_BITS - round_0`, and the
495/// result stays in the compound intermediate domain rather than becoming a
496/// pixel. libaom keeps a constant bias in its CONV_BUF (the
497/// `1 << offset_bits_horiz` / `1 << offset_bits_vert` terms, which survive
498/// both roundings exactly because both exceed their shift) and subtracts
499/// `(1 << (offset_bits - round_1)) + (1 << (offset_bits - round_1 - 1))`
500/// at the blend; this crate's intermediates are UNBIASED (see
501/// [`mc::diffwtd_mask`]'s derivation), so the identical constant, derived
502/// from the bumped `round_0` below, is removed here instead, making
503/// the output directly interchangeable with
504/// [`mc::predict_compound_intermediate`]'s for
505/// [`mc::combine_compound`]/[`mc::blend_masked_compound`].
506#[allow(clippy::too_many_arguments)]
507pub(crate) fn warp_affine_compound(
508 params: &WarpParams,
509 reference: &[u16],
510 width: i32,
511 height: i32,
512 stride: i32,
513 dst: &mut [i32],
514 p_col: i32,
515 p_row: i32,
516 p_width: i32,
517 p_height: i32,
518 p_stride: i32,
519 subsampling_x: i32,
520 subsampling_y: i32,
521 fctx: &crate::decode::FrameCtx,
522) {
523 let bd = i32::from(crate::decode::bit_depth(fctx));
524 // The CONV_BUF bias libaom subtracts at the blend:
525 // `(1 << (offset_bits - round_1)) + (1 << (offset_bits - round_1 - 1))`
526 // with `offset_bits = bd + 2 * FILTER_BITS - round_0` (round_0 the bumped
527 // [`warp_round_0`], round_1 [`COMPOUND_ROUND1_BITS`], unbumped for
528 // compound): `(1 << (bd + 4)) + (1 << (bd + 3))` at 8/10-bit, one octave
529 // less at 12-bit.
530 let offset_bits = bd + 2 * FILTER_BITS - warp_round_0(bd);
531 let bias = (1 << (offset_bits - COMPOUND_ROUND1_BITS))
532 + (1 << (offset_bits - COMPOUND_ROUND1_BITS - 1));
533 warp_inner(
534 params,
535 reference,
536 width,
537 height,
538 stride,
539 p_col,
540 p_row,
541 p_width,
542 p_height,
543 subsampling_x,
544 subsampling_y,
545 bd,
546 COMPOUND_ROUND1_BITS,
547 |row, col, sum| {
548 dst[row * p_stride as usize + col] = sum - bias;
549 },
550 );
551}
552
553/// `ROUND0_BITS` (`convolve.h`): `conv_params->round_0` before
554/// `get_conv_params_no_round`'s high-bitdepth buffer-range bump.
555const ROUND0_BITS: i32 = 3;
556/// `COMPOUND_ROUND1_BITS` (`convolve.h`): `conv_params->round_1` when
557/// `is_compound` (NOT bumped at 12 bits -- the bump moves `round_0` and, for
558/// non-compound, `round_1` by the same delta, keeping the filter gain
559/// constant).
560const COMPOUND_ROUND1_BITS: i32 = 7;
561
562/// `conv_params->round_0` after `get_conv_params_no_round`'s bump
563/// (`convolve.h`): `round_0 += max(bd + FILTER_BITS - round_0 - 14, 0)` --
564/// 3 at 8/10-bit, 5 at 12-bit (`intbufrange = bd + FILTER_BITS - round_0 +
565/// 2` overflows 16 bits from bd 12 on). The warp filter's
566/// `reduce_bits_horiz` IS `conv_params->round_0` (`warped_motion.c:295`),
567/// and the non-compound `reduce_bits_vert` plus both `offset_bits_*` terms
568/// read back through it, so this one parameter moves every shift the warp
569/// filter runs (lane-av112bitw: the 12-bit warp witness decodes byte-exact
570/// through it).
571fn warp_round_0(bd: i32) -> i32 {
572 ROUND0_BITS + (bd + FILTER_BITS - ROUND0_BITS - 14).max(0)
573}
574
575/// The shared body of [`warp_affine`]/[`warp_affine_compound`]
576/// (`av1_highbd_warp_affine_c`): everything up to and including the
577/// vertical `ROUND_POWER_OF_TWO(sum, reduce_bits_vert)`, with `store`
578/// receiving `(row, col, sum)` in block-local coordinates.
579#[allow(clippy::too_many_arguments)]
580fn warp_inner(
581 params: &WarpParams,
582 reference: &[u16],
583 width: i32,
584 height: i32,
585 stride: i32,
586 p_col: i32,
587 p_row: i32,
588 p_width: i32,
589 p_height: i32,
590 subsampling_x: i32,
591 subsampling_y: i32,
592 bd: i32,
593 reduce_bits_vert: i32,
594 mut store: impl FnMut(usize, usize, i32),
595) {
596 let mat = &params.wmmat;
597 let (alpha, beta, gamma, delta) = (
598 params.alpha as i64,
599 params.beta as i64,
600 params.gamma as i64,
601 params.delta as i64,
602 );
603 let reduce_bits_horiz = warp_round_0(bd);
604 let offset_bits_horiz = bd + FILTER_BITS - 1;
605 let offset_bits_vert = bd + 2 * FILTER_BITS - reduce_bits_horiz;
606
607 let mut i = p_row;
608 while i < p_row + p_height {
609 let mut j = p_col;
610 while j < p_col + p_width {
611 let src_x = ((j + 4) << subsampling_x) as i64;
612 let src_y = ((i + 4) << subsampling_y) as i64;
613 let dst_x = mat[2] as i64 * src_x + mat[3] as i64 * src_y + mat[0] as i64;
614 let dst_y = mat[4] as i64 * src_x + mat[5] as i64 * src_y + mat[1] as i64;
615 let x4 = dst_x >> subsampling_x;
616 let y4 = dst_y >> subsampling_y;
617
618 let ix4 = (x4 >> WARPEDMODEL_PREC_BITS) as i32;
619 let mut sx4 = x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
620 let iy4 = (y4 >> WARPEDMODEL_PREC_BITS) as i32;
621 let mut sy4 = y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
622
623 sx4 += alpha * -4 + beta * -4;
624 sy4 += gamma * -4 + delta * -4;
625 sx4 &= !((1i64 << WARP_PARAM_REDUCE_BITS) - 1);
626 sy4 &= !((1i64 << WARP_PARAM_REDUCE_BITS) - 1);
627
628 let mut tmp = [[0i32; 8]; 15];
629 for k in -7..8i32 {
630 let iy = (iy4 + k).clamp(0, height - 1);
631 let mut sx = sx4 + beta * (k as i64 + 4);
632 for l in -4..4i32 {
633 let ix = ix4 + l - 3;
634 let offs = (round2(sx, WARPEDDIFF_PREC_BITS) as i32 + WARPEDPIXEL_PREC_SHIFTS)
635 as usize;
636 let coeffs = &AV1_WARPED_FILTER[offs];
637 let mut sum: i32 = 1 << offset_bits_horiz;
638 for (m, &c) in coeffs.iter().enumerate() {
639 let sample_x = (ix + m as i32).clamp(0, width - 1);
640 sum += i32::from(reference[(iy * stride + sample_x) as usize]) * c as i32;
641 }
642 sum = round2(sum as i64, reduce_bits_horiz as u32) as i32;
643 tmp[(k + 7) as usize][(l + 4) as usize] = sum;
644 sx += alpha;
645 }
646 }
647
648 let k_hi = (4i32).min(p_row + p_height - i - 4);
649 let mut k = -4i32;
650 while k < k_hi {
651 let mut sy = sy4 + delta * (k as i64 + 4);
652 let l_hi = (4i32).min(p_col + p_width - j - 4);
653 let mut l = -4i32;
654 while l < l_hi {
655 let offs = (round2(sy, WARPEDDIFF_PREC_BITS) as i32 + WARPEDPIXEL_PREC_SHIFTS)
656 as usize;
657 let coeffs = &AV1_WARPED_FILTER[offs];
658 let mut sum: i32 = 1 << offset_bits_vert;
659 for m in 0..8i32 {
660 sum +=
661 tmp[(k + m + 4) as usize][(l + 4) as usize] * coeffs[m as usize] as i32;
662 }
663 sum = round2(sum as i64, reduce_bits_vert as u32) as i32;
664 store(
665 (i - p_row + k + 4) as usize,
666 (j - p_col + l + 4) as usize,
667 sum,
668 );
669 sy += gamma;
670 l += 1;
671 }
672 k += 1;
673 }
674 j += 8;
675 }
676 i += 8;
677 }
678}
679
680/// `av1_warped_filter` (`warped_motion.c`): 193 rows (`WARPEDPIXEL_PREC_SHIFTS
681/// * 3 + 1`), one per 1/64-pel fraction across `[-1, 2)`, each an 8-tap
682/// kernel (row 192 is a dummy replicate of row 191, transcribed verbatim).
683#[rustfmt::skip]
684const AV1_WARPED_FILTER: [[i16; 8]; 193] = [
685 [0, 0, 127, 1, 0, 0, 0, 0],
686 [0, -1, 127, 2, 0, 0, 0, 0],
687 [1, -3, 127, 4, -1, 0, 0, 0],
688 [1, -4, 126, 6, -2, 1, 0, 0],
689 [1, -5, 126, 8, -3, 1, 0, 0],
690 [1, -6, 125, 11, -4, 1, 0, 0],
691 [1, -7, 124, 13, -4, 1, 0, 0],
692 [2, -8, 123, 15, -5, 1, 0, 0],
693 [2, -9, 122, 18, -6, 1, 0, 0],
694 [2, -10, 121, 20, -6, 1, 0, 0],
695 [2, -11, 120, 22, -7, 2, 0, 0],
696 [2, -12, 119, 25, -8, 2, 0, 0],
697 [3, -13, 117, 27, -8, 2, 0, 0],
698 [3, -13, 116, 29, -9, 2, 0, 0],
699 [3, -14, 114, 32, -10, 3, 0, 0],
700 [3, -15, 113, 35, -10, 2, 0, 0],
701 [3, -15, 111, 37, -11, 3, 0, 0],
702 [3, -16, 109, 40, -11, 3, 0, 0],
703 [3, -16, 108, 42, -12, 3, 0, 0],
704 [4, -17, 106, 45, -13, 3, 0, 0],
705 [4, -17, 104, 47, -13, 3, 0, 0],
706 [4, -17, 102, 50, -14, 3, 0, 0],
707 [4, -17, 100, 52, -14, 3, 0, 0],
708 [4, -18, 98, 55, -15, 4, 0, 0],
709 [4, -18, 96, 58, -15, 3, 0, 0],
710 [4, -18, 94, 60, -16, 4, 0, 0],
711 [4, -18, 91, 63, -16, 4, 0, 0],
712 [4, -18, 89, 65, -16, 4, 0, 0],
713 [4, -18, 87, 68, -17, 4, 0, 0],
714 [4, -18, 85, 70, -17, 4, 0, 0],
715 [4, -18, 82, 73, -17, 4, 0, 0],
716 [4, -18, 80, 75, -17, 4, 0, 0],
717 [4, -18, 78, 78, -18, 4, 0, 0],
718 [4, -17, 75, 80, -18, 4, 0, 0],
719 [4, -17, 73, 82, -18, 4, 0, 0],
720 [4, -17, 70, 85, -18, 4, 0, 0],
721 [4, -17, 68, 87, -18, 4, 0, 0],
722 [4, -16, 65, 89, -18, 4, 0, 0],
723 [4, -16, 63, 91, -18, 4, 0, 0],
724 [4, -16, 60, 94, -18, 4, 0, 0],
725 [3, -15, 58, 96, -18, 4, 0, 0],
726 [4, -15, 55, 98, -18, 4, 0, 0],
727 [3, -14, 52, 100, -17, 4, 0, 0],
728 [3, -14, 50, 102, -17, 4, 0, 0],
729 [3, -13, 47, 104, -17, 4, 0, 0],
730 [3, -13, 45, 106, -17, 4, 0, 0],
731 [3, -12, 42, 108, -16, 3, 0, 0],
732 [3, -11, 40, 109, -16, 3, 0, 0],
733 [3, -11, 37, 111, -15, 3, 0, 0],
734 [2, -10, 35, 113, -15, 3, 0, 0],
735 [3, -10, 32, 114, -14, 3, 0, 0],
736 [2, -9, 29, 116, -13, 3, 0, 0],
737 [2, -8, 27, 117, -13, 3, 0, 0],
738 [2, -8, 25, 119, -12, 2, 0, 0],
739 [2, -7, 22, 120, -11, 2, 0, 0],
740 [1, -6, 20, 121, -10, 2, 0, 0],
741 [1, -6, 18, 122, -9, 2, 0, 0],
742 [1, -5, 15, 123, -8, 2, 0, 0],
743 [1, -4, 13, 124, -7, 1, 0, 0],
744 [1, -4, 11, 125, -6, 1, 0, 0],
745 [1, -3, 8, 126, -5, 1, 0, 0],
746 [1, -2, 6, 126, -4, 1, 0, 0],
747 [0, -1, 4, 127, -3, 1, 0, 0],
748 [0, 0, 2, 127, -1, 0, 0, 0],
749 [0, 0, 0, 127, 1, 0, 0, 0],
750 [0, 0, -1, 127, 2, 0, 0, 0],
751 [0, 1, -3, 127, 4, -2, 1, 0],
752 [0, 1, -5, 127, 6, -2, 1, 0],
753 [0, 2, -6, 126, 8, -3, 1, 0],
754 [-1, 2, -7, 126, 11, -4, 2, -1],
755 [-1, 3, -8, 125, 13, -5, 2, -1],
756 [-1, 3, -10, 124, 16, -6, 3, -1],
757 [-1, 4, -11, 123, 18, -7, 3, -1],
758 [-1, 4, -12, 122, 20, -7, 3, -1],
759 [-1, 4, -13, 121, 23, -8, 3, -1],
760 [-2, 5, -14, 120, 25, -9, 4, -1],
761 [-1, 5, -15, 119, 27, -10, 4, -1],
762 [-1, 5, -16, 118, 30, -11, 4, -1],
763 [-2, 6, -17, 116, 33, -12, 5, -1],
764 [-2, 6, -17, 114, 35, -12, 5, -1],
765 [-2, 6, -18, 113, 38, -13, 5, -1],
766 [-2, 7, -19, 111, 41, -14, 6, -2],
767 [-2, 7, -19, 110, 43, -15, 6, -2],
768 [-2, 7, -20, 108, 46, -15, 6, -2],
769 [-2, 7, -20, 106, 49, -16, 6, -2],
770 [-2, 7, -21, 104, 51, -16, 7, -2],
771 [-2, 7, -21, 102, 54, -17, 7, -2],
772 [-2, 8, -21, 100, 56, -18, 7, -2],
773 [-2, 8, -22, 98, 59, -18, 7, -2],
774 [-2, 8, -22, 96, 62, -19, 7, -2],
775 [-2, 8, -22, 94, 64, -19, 7, -2],
776 [-2, 8, -22, 91, 67, -20, 8, -2],
777 [-2, 8, -22, 89, 69, -20, 8, -2],
778 [-2, 8, -22, 87, 72, -21, 8, -2],
779 [-2, 8, -21, 84, 74, -21, 8, -2],
780 [-2, 8, -22, 82, 77, -21, 8, -2],
781 [-2, 8, -21, 79, 79, -21, 8, -2],
782 [-2, 8, -21, 77, 82, -22, 8, -2],
783 [-2, 8, -21, 74, 84, -21, 8, -2],
784 [-2, 8, -21, 72, 87, -22, 8, -2],
785 [-2, 8, -20, 69, 89, -22, 8, -2],
786 [-2, 8, -20, 67, 91, -22, 8, -2],
787 [-2, 7, -19, 64, 94, -22, 8, -2],
788 [-2, 7, -19, 62, 96, -22, 8, -2],
789 [-2, 7, -18, 59, 98, -22, 8, -2],
790 [-2, 7, -18, 56, 100, -21, 8, -2],
791 [-2, 7, -17, 54, 102, -21, 7, -2],
792 [-2, 7, -16, 51, 104, -21, 7, -2],
793 [-2, 6, -16, 49, 106, -20, 7, -2],
794 [-2, 6, -15, 46, 108, -20, 7, -2],
795 [-2, 6, -15, 43, 110, -19, 7, -2],
796 [-2, 6, -14, 41, 111, -19, 7, -2],
797 [-1, 5, -13, 38, 113, -18, 6, -2],
798 [-1, 5, -12, 35, 114, -17, 6, -2],
799 [-1, 5, -12, 33, 116, -17, 6, -2],
800 [-1, 4, -11, 30, 118, -16, 5, -1],
801 [-1, 4, -10, 27, 119, -15, 5, -1],
802 [-1, 4, -9, 25, 120, -14, 5, -2],
803 [-1, 3, -8, 23, 121, -13, 4, -1],
804 [-1, 3, -7, 20, 122, -12, 4, -1],
805 [-1, 3, -7, 18, 123, -11, 4, -1],
806 [-1, 3, -6, 16, 124, -10, 3, -1],
807 [-1, 2, -5, 13, 125, -8, 3, -1],
808 [-1, 2, -4, 11, 126, -7, 2, -1],
809 [0, 1, -3, 8, 126, -6, 2, 0],
810 [0, 1, -2, 6, 127, -5, 1, 0],
811 [0, 1, -2, 4, 127, -3, 1, 0],
812 [0, 0, 0, 2, 127, -1, 0, 0],
813 [0, 0, 0, 1, 127, 0, 0, 0],
814 [0, 0, 0, -1, 127, 2, 0, 0],
815 [0, 0, 1, -3, 127, 4, -1, 0],
816 [0, 0, 1, -4, 126, 6, -2, 1],
817 [0, 0, 1, -5, 126, 8, -3, 1],
818 [0, 0, 1, -6, 125, 11, -4, 1],
819 [0, 0, 1, -7, 124, 13, -4, 1],
820 [0, 0, 2, -8, 123, 15, -5, 1],
821 [0, 0, 2, -9, 122, 18, -6, 1],
822 [0, 0, 2, -10, 121, 20, -6, 1],
823 [0, 0, 2, -11, 120, 22, -7, 2],
824 [0, 0, 2, -12, 119, 25, -8, 2],
825 [0, 0, 3, -13, 117, 27, -8, 2],
826 [0, 0, 3, -13, 116, 29, -9, 2],
827 [0, 0, 3, -14, 114, 32, -10, 3],
828 [0, 0, 3, -15, 113, 35, -10, 2],
829 [0, 0, 3, -15, 111, 37, -11, 3],
830 [0, 0, 3, -16, 109, 40, -11, 3],
831 [0, 0, 3, -16, 108, 42, -12, 3],
832 [0, 0, 4, -17, 106, 45, -13, 3],
833 [0, 0, 4, -17, 104, 47, -13, 3],
834 [0, 0, 4, -17, 102, 50, -14, 3],
835 [0, 0, 4, -17, 100, 52, -14, 3],
836 [0, 0, 4, -18, 98, 55, -15, 4],
837 [0, 0, 4, -18, 96, 58, -15, 3],
838 [0, 0, 4, -18, 94, 60, -16, 4],
839 [0, 0, 4, -18, 91, 63, -16, 4],
840 [0, 0, 4, -18, 89, 65, -16, 4],
841 [0, 0, 4, -18, 87, 68, -17, 4],
842 [0, 0, 4, -18, 85, 70, -17, 4],
843 [0, 0, 4, -18, 82, 73, -17, 4],
844 [0, 0, 4, -18, 80, 75, -17, 4],
845 [0, 0, 4, -18, 78, 78, -18, 4],
846 [0, 0, 4, -17, 75, 80, -18, 4],
847 [0, 0, 4, -17, 73, 82, -18, 4],
848 [0, 0, 4, -17, 70, 85, -18, 4],
849 [0, 0, 4, -17, 68, 87, -18, 4],
850 [0, 0, 4, -16, 65, 89, -18, 4],
851 [0, 0, 4, -16, 63, 91, -18, 4],
852 [0, 0, 4, -16, 60, 94, -18, 4],
853 [0, 0, 3, -15, 58, 96, -18, 4],
854 [0, 0, 4, -15, 55, 98, -18, 4],
855 [0, 0, 3, -14, 52, 100, -17, 4],
856 [0, 0, 3, -14, 50, 102, -17, 4],
857 [0, 0, 3, -13, 47, 104, -17, 4],
858 [0, 0, 3, -13, 45, 106, -17, 4],
859 [0, 0, 3, -12, 42, 108, -16, 3],
860 [0, 0, 3, -11, 40, 109, -16, 3],
861 [0, 0, 3, -11, 37, 111, -15, 3],
862 [0, 0, 2, -10, 35, 113, -15, 3],
863 [0, 0, 3, -10, 32, 114, -14, 3],
864 [0, 0, 2, -9, 29, 116, -13, 3],
865 [0, 0, 2, -8, 27, 117, -13, 3],
866 [0, 0, 2, -8, 25, 119, -12, 2],
867 [0, 0, 2, -7, 22, 120, -11, 2],
868 [0, 0, 1, -6, 20, 121, -10, 2],
869 [0, 0, 1, -6, 18, 122, -9, 2],
870 [0, 0, 1, -5, 15, 123, -8, 2],
871 [0, 0, 1, -4, 13, 124, -7, 1],
872 [0, 0, 1, -4, 11, 125, -6, 1],
873 [0, 0, 1, -3, 8, 126, -5, 1],
874 [0, 0, 1, -2, 6, 126, -4, 1],
875 [0, 0, 0, -1, 4, 127, -3, 1],
876 [0, 0, 0, 0, 2, 127, -1, 0],
877 [0, 0, 0, 0, 2, 127, -1, 0],
878];
879
880/// `div_lut` (`warped_motion.c`).
881#[rustfmt::skip]
882const DIV_LUT: [u16; 257] = [
883 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
884 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
885 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
886 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
887 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
888 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
889 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
890 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
891 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
892 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
893 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
894 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
895 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
896 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
897 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
898 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
899 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
900 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
901 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
902 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
903 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
904 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
905 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
906 8240, 8224, 8208, 8192,
907];
908
909#[cfg(test)]
910mod gm_mv_tests {
911 use super::gm_get_motion_vector;
912 use ec_av1_syntax::WarpModel;
913
914 /// `TRANSLATION`: `row = wmmat[0] >> 13`, `col = wmmat[1] >> 13` (spec's
915 /// own swapped-axis bug, kept). `100000 >> 13 = 12` (`100000 / 8192 =
916 /// 12.207`, floor 12); `-50000 >> 13 = -7` (`-50000 / 8192 = -6.1035`,
917 /// floor -7, arithmetic shift rounds toward -inf).
918 #[test]
919 fn translation_shifts_params_by_13() {
920 let params = [100_000, -50_000, 1 << 16, 0, 0, 1 << 16];
921 let mv = gm_get_motion_vector(WarpModel::Translation, &params, 0, 0, 2, 2, true, false);
922 assert_eq!(mv, (12, -7));
923 }
924
925 /// `ROTZOOM`, block centre `x = mi_col*4 + bw4*4/2 = 2*4 + 4*4/2 = 16`,
926 /// `y = mi_row*4 + bh4*4/2 = 1*4 + 4*4/2 = 12`.
927 /// `xc = (params[2]-2^16)*x + params[3]*y + params[0]`
928 /// `= 500*16 + 100*12 + 1000 = 8000 + 1200 + 1000 = 10200`.
929 /// `yc = params[4]*x + (params[5]-2^16)*y + params[1]`
930 /// `= -50*16 + (-300)*12 + 2000 = -800 - 3600 + 2000 = -2400`.
931 /// `hp`: `row = Round2Signed(yc,13) = -Round2(2400,13) = -((2400+4096)>>13)
932 /// = -(6496>>13) = -0 = 0`; `col = Round2Signed(xc,13) =
933 /// (10200+4096)>>13 = 14296>>13 = 1`.
934 #[test]
935 fn rotzoom_projects_block_centre_hp() {
936 let params = [1000, 2000, (1 << 16) + 500, 100, -50, (1 << 16) - 300];
937 let mv = gm_get_motion_vector(WarpModel::Rotzoom, &params, 1, 2, 4, 4, true, false);
938 assert_eq!(mv, (0, 1));
939 }
940
941 /// Same model/geometry as [`rotzoom_projects_block_centre_hp`], not high
942 /// precision: `convert_to_trans_prec` shifts by 14 instead of 13 then
943 /// doubles. `row = -Round2(2400,14)*2 = -((2400+8192)>>14)*2 =
944 /// -(10592>>14)*2 = 0`; `col = Round2(10200,14)*2 = ((10200+8192)>>14)*2
945 /// = (18392>>14)*2 = 1*2 = 2`.
946 #[test]
947 fn rotzoom_projects_block_centre_no_hp() {
948 let params = [1000, 2000, (1 << 16) + 500, 100, -50, (1 << 16) - 300];
949 let mv = gm_get_motion_vector(WarpModel::Rotzoom, &params, 1, 2, 4, 4, false, false);
950 assert_eq!(mv, (0, 2));
951 }
952
953 /// `force_integer_mv`'s `integer_mv_precision`, applied to
954 /// [`translation_shifts_params_by_13`]'s raw `(12, -7)`: `12 % 8 = 4`,
955 /// `abs(4) > 4` false, so `12 -> 12-4 = 8`. `-7 % 8 = -7` (Rust's
956 /// truncating `%`), `abs(-7) > 4` true and `-7 < 0`, so
957 /// `-7 -> (-7 - (-7)) - 8 = -8`.
958 #[test]
959 fn translation_force_integer_mv_rounds_to_full_pel() {
960 let params = [100_000, -50_000, 1 << 16, 0, 0, 1 << 16];
961 let mv = gm_get_motion_vector(WarpModel::Translation, &params, 0, 0, 2, 2, true, true);
962 assert_eq!(mv, (8, -8));
963 }
964
965 #[test]
966 fn identity_is_always_zero() {
967 let params = [999, -999, (1 << 16) + 1, 5, 5, (1 << 16) - 1];
968 assert_eq!(
969 gm_get_motion_vector(WarpModel::Identity, &params, 3, 4, 4, 4, true, false),
970 (0, 0)
971 );
972 }
973}