forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

mc.rs843 lines, 31.9 KiBHistoryRawDownload
1//! Sub-pel motion compensation, spec 7.11.3 ("Motion vector scaling process"
2//! through "Block inter predictor process"), the REGULAR (`EIGHTTAP`) filter
3//! only, 8-bit samples only.
4//!
5//! A block's inter prediction is separable: an 8-tap horizontal pass over the
6//! reference, rounded down by `InterRound0`, feeds an 8-tap vertical pass
7//! over that intermediate, rounded down by `InterRound1`. Both fractions run
8//! in 1/16-pel steps -- luma motion vectors are stored at 1/8-pel and always
9//! land on an even step; chroma vectors, once scaled for subsampling, can
10//! land on any of the 16.
11
12/// `Round2` (spec 4.7): halves round up.
13fn round2(value: i32, shift: u32) -> i32 {
14 if shift == 0 {
15 value
16 } else {
17 (value + (1 << (shift - 1))) >> shift
18 }
19}
20
21/// `InterRound0` (spec 7.11.3.2), 8-bit, non-compound: the shift the
22/// horizontal pass's sum is brought down by before it is stored as the
23/// intermediate.
24const INTER_ROUND_0: u32 = 3;
25/// `InterRound1` (spec 7.11.3.2), 8-bit, non-compound: the shift the vertical
26/// pass's sum is brought down by to land back on an 8-bit sample. Composed
27/// with `INTER_ROUND_0` the two shifts are the filter's own gain (each pass
28/// sums to 128, `128 * 128 == 1 << 14 == 1 << (INTER_ROUND_0 + INTER_ROUND_1)`)
29/// -- so an identity or DC input comes back exactly, by construction of the
30/// table rather than by a fitted constant.
31const INTER_ROUND_1: u32 = 11;
32
33/// `Subpel_Filters[EIGHTTAP]` (spec 7.11.3.3): 16 rows, one per 1/16-pel
34/// fraction, each an 8-tap kernel centred so tap index 3 lands on the integer
35/// sample (fraction 0 is the identity tap `[.., 128, ..]`).
36const SUBPEL_FILTERS: [[i32; 8]; 16] = [
37 [0, 0, 0, 128, 0, 0, 0, 0],
38 [0, 2, -6, 126, 8, -2, 0, 0],
39 [0, 2, -10, 122, 18, -4, 0, 0],
40 [0, 2, -12, 116, 28, -8, 2, 0],
41 [0, 2, -14, 110, 38, -10, 2, 0],
42 [0, 2, -14, 102, 48, -12, 2, 0],
43 [0, 2, -16, 94, 58, -12, 2, 0],
44 [0, 2, -14, 84, 66, -12, 2, 0],
45 [0, 2, -14, 76, 76, -14, 2, 0],
46 [0, 2, -12, 66, 84, -14, 2, 0],
47 [0, 2, -12, 58, 94, -16, 2, 0],
48 [0, 2, -12, 48, 102, -14, 2, 0],
49 [0, 2, -10, 38, 110, -14, 2, 0],
50 [0, 2, -8, 28, 116, -12, 2, 0],
51 [0, 0, -4, 18, 122, -10, 2, 0],
52 [0, 0, -2, 8, 126, -6, 2, 0],
53];
54
55/// `Subpel_Filters[EIGHTTAP]`'s narrow-block counterpart (spec 7.11.3.4's
56/// filter selection is per-axis: a `predict` axis whose output block
57/// dimension is 4 or less reads this table instead of [`SUBPEL_FILTERS`]),
58/// mirroring `av1_sub_pel_filters_4` (`filter.h`): the same 8-slot shape but
59/// zeroed at taps 0/1/6/7, so a 4-wide (or 4-tall) block's filter never
60/// reaches a full 3 samples past its own edge. Every 4:2:0 chroma block
61/// under an 8x8 (or smaller) luma leaf hits this on at least one axis --
62/// this is not an edge case, it is the common chroma shape.
63const SUBPEL_FILTERS_4: [[i32; 8]; 16] = [
64 [0, 0, 0, 128, 0, 0, 0, 0],
65 [0, 0, -4, 126, 8, -2, 0, 0],
66 [0, 0, -8, 122, 18, -4, 0, 0],
67 [0, 0, -10, 116, 28, -6, 0, 0],
68 [0, 0, -12, 110, 38, -8, 0, 0],
69 [0, 0, -12, 102, 48, -10, 0, 0],
70 [0, 0, -14, 94, 58, -10, 0, 0],
71 [0, 0, -12, 84, 66, -10, 0, 0],
72 [0, 0, -12, 76, 76, -12, 0, 0],
73 [0, 0, -10, 66, 84, -12, 0, 0],
74 [0, 0, -10, 58, 94, -14, 0, 0],
75 [0, 0, -10, 48, 102, -12, 0, 0],
76 [0, 0, -8, 38, 110, -12, 0, 0],
77 [0, 0, -6, 28, 116, -10, 0, 0],
78 [0, 0, -4, 18, 122, -8, 0, 0],
79 [0, 0, -2, 8, 126, -4, 0, 0],
80];
81
82/// `Subpel_Filters[EIGHTTAP_SMOOTH]` (`filter.h`'s `av1_sub_pel_filters_8smooth`).
83const SUBPEL_FILTERS_SMOOTH: [[i32; 8]; 16] = [
84 [0, 0, 0, 128, 0, 0, 0, 0],
85 [0, 2, 28, 62, 34, 2, 0, 0],
86 [0, 0, 26, 62, 36, 4, 0, 0],
87 [0, 0, 22, 62, 40, 4, 0, 0],
88 [0, 0, 20, 60, 42, 6, 0, 0],
89 [0, 0, 18, 58, 44, 8, 0, 0],
90 [0, 0, 16, 56, 46, 10, 0, 0],
91 [0, -2, 16, 54, 48, 12, 0, 0],
92 [0, -2, 14, 52, 52, 14, -2, 0],
93 [0, 0, 12, 48, 54, 16, -2, 0],
94 [0, 0, 10, 46, 56, 16, 0, 0],
95 [0, 0, 8, 44, 58, 18, 0, 0],
96 [0, 0, 6, 42, 60, 20, 0, 0],
97 [0, 0, 4, 40, 62, 22, 0, 0],
98 [0, 0, 4, 36, 62, 26, 0, 0],
99 [0, 0, 2, 34, 62, 28, 2, 0],
100];
101
102/// `SUBPEL_FILTERS_SMOOTH`'s narrow-block counterpart (`av1_sub_pel_filters_4smooth`).
103const SUBPEL_FILTERS_SMOOTH_4: [[i32; 8]; 16] = [
104 [0, 0, 0, 128, 0, 0, 0, 0],
105 [0, 0, 30, 62, 34, 2, 0, 0],
106 [0, 0, 26, 62, 36, 4, 0, 0],
107 [0, 0, 22, 62, 40, 4, 0, 0],
108 [0, 0, 20, 60, 42, 6, 0, 0],
109 [0, 0, 18, 58, 44, 8, 0, 0],
110 [0, 0, 16, 56, 46, 10, 0, 0],
111 [0, 0, 14, 54, 48, 12, 0, 0],
112 [0, 0, 12, 52, 52, 12, 0, 0],
113 [0, 0, 12, 48, 54, 14, 0, 0],
114 [0, 0, 10, 46, 56, 16, 0, 0],
115 [0, 0, 8, 44, 58, 18, 0, 0],
116 [0, 0, 6, 42, 60, 20, 0, 0],
117 [0, 0, 4, 40, 62, 22, 0, 0],
118 [0, 0, 4, 36, 62, 26, 0, 0],
119 [0, 0, 2, 34, 62, 30, 0, 0],
120];
121
122/// `Subpel_Filters[EIGHTTAP_SHARP]` (`av1_sub_pel_filters_8sharp`) -- unlike
123/// REGULAR/SMOOTH, spec 7.11.3.4 never swaps this for a narrow-block table.
124const SUBPEL_FILTERS_SHARP: [[i32; 8]; 16] = [
125 [0, 0, 0, 128, 0, 0, 0, 0],
126 [-2, 2, -6, 126, 8, -2, 2, 0],
127 [-2, 6, -12, 124, 16, -6, 4, -2],
128 [-2, 8, -18, 120, 26, -10, 6, -2],
129 [-4, 10, -22, 116, 38, -14, 6, -2],
130 [-4, 10, -22, 108, 48, -18, 8, -2],
131 [-4, 10, -24, 100, 60, -20, 8, -2],
132 [-4, 10, -24, 90, 70, -22, 10, -2],
133 [-4, 12, -24, 80, 80, -24, 12, -4],
134 [-2, 10, -22, 70, 90, -24, 10, -4],
135 [-2, 8, -20, 60, 100, -24, 10, -4],
136 [-2, 8, -18, 48, 108, -22, 10, -4],
137 [-2, 6, -14, 38, 116, -22, 10, -4],
138 [-2, 6, -10, 26, 120, -18, 8, -2],
139 [-2, 4, -6, 16, 124, -12, 6, -2],
140 [0, 2, -2, 8, 126, -6, 2, -2],
141];
142
143/// `Subpel_Filters[BILINEAR]` (`av1_bilinear_filters`) -- also never swapped
144/// for a narrow-block table (it is already only 2 non-zero taps).
145const SUBPEL_FILTERS_BILINEAR: [[i32; 8]; 16] = [
146 [0, 0, 0, 128, 0, 0, 0, 0],
147 [0, 0, 0, 120, 8, 0, 0, 0],
148 [0, 0, 0, 112, 16, 0, 0, 0],
149 [0, 0, 0, 104, 24, 0, 0, 0],
150 [0, 0, 0, 96, 32, 0, 0, 0],
151 [0, 0, 0, 88, 40, 0, 0, 0],
152 [0, 0, 0, 80, 48, 0, 0, 0],
153 [0, 0, 0, 72, 56, 0, 0, 0],
154 [0, 0, 0, 64, 64, 0, 0, 0],
155 [0, 0, 0, 56, 72, 0, 0, 0],
156 [0, 0, 0, 48, 80, 0, 0, 0],
157 [0, 0, 0, 40, 88, 0, 0, 0],
158 [0, 0, 0, 32, 96, 0, 0, 0],
159 [0, 0, 0, 24, 104, 0, 0, 0],
160 [0, 0, 0, 16, 112, 0, 0, 0],
161 [0, 0, 0, 8, 120, 0, 0, 0],
162];
163
164/// Which of the spec's four `interpolation_filter` kernels a block's motion
165/// compensation reads (spec 6.8.9 / `frame_header`'s `interpolation_filter`).
166/// A frame codes exactly one of these when not `Switchable`; a `Switchable`
167/// frame codes one per block per direction (spec 5.11.20's
168/// `read_interp_filter`, see `decode::decode_inter_block`), so `predict`'s
169/// callers resolve `Switchable` to a concrete pair of kinds themselves --
170/// this type only ever names the resolved kernel, never `Switchable` itself.
171#[derive(Clone, Copy, Debug, PartialEq, Eq)]
172pub enum InterpFilterKind {
173 Regular,
174 Smooth,
175 Sharp,
176 Bilinear,
177}
178
179impl InterpFilterKind {
180 fn tables(self) -> (&'static [[i32; 8]; 16], &'static [[i32; 8]; 16]) {
181 match self {
182 InterpFilterKind::Regular => (&SUBPEL_FILTERS, &SUBPEL_FILTERS_4),
183 InterpFilterKind::Smooth => (&SUBPEL_FILTERS_SMOOTH, &SUBPEL_FILTERS_SMOOTH_4),
184 InterpFilterKind::Sharp => (&SUBPEL_FILTERS_SHARP, &SUBPEL_FILTERS_SHARP),
185 InterpFilterKind::Bilinear => (&SUBPEL_FILTERS_BILINEAR, &SUBPEL_FILTERS_BILINEAR),
186 }
187 }
188
189 /// The three-symbol alphabet spec 5.11.20's `interp_filter[dir]` reads
190 /// under a `Switchable` frame decodes to (`EIGHTTAP`/`_SMOOTH`/`_SHARP`
191 /// in that order -- `BILINEAR` is never a `switchable_interp` outcome).
192 ///
193 /// # Panics
194 /// Panics when `symbol` is not `0..=2`.
195 pub fn from_switchable_symbol(symbol: usize) -> InterpFilterKind {
196 match symbol {
197 0 => InterpFilterKind::Regular,
198 1 => InterpFilterKind::Smooth,
199 2 => InterpFilterKind::Sharp,
200 _ => panic!("switchable_interp's alphabet is exactly 3 symbols"),
201 }
202 }
203
204 /// The frame header's own `interpolation_filter` (spec 6.8.9), for the
205 /// non-`Switchable` values a fixed-filter frame codes directly.
206 ///
207 /// # Panics
208 /// Panics on `Switchable`, which is not a concrete kernel -- callers
209 /// resolve it per block instead (spec 5.11.20).
210 pub fn from_header(filter: ec_av1_syntax::InterpolationFilter) -> InterpFilterKind {
211 match filter {
212 ec_av1_syntax::InterpolationFilter::Eighttap => InterpFilterKind::Regular,
213 ec_av1_syntax::InterpolationFilter::EighttapSmooth => InterpFilterKind::Smooth,
214 ec_av1_syntax::InterpolationFilter::EighttapSharp => InterpFilterKind::Sharp,
215 ec_av1_syntax::InterpolationFilter::Bilinear => InterpFilterKind::Bilinear,
216 ec_av1_syntax::InterpolationFilter::Switchable => {
217 panic!("Switchable is resolved per block, not as one frame-wide kernel")
218 }
219 }
220 }
221}
222
223/// A reference sample at `(x, y)`, clamped to the frame's true (unpadded)
224/// extent -- `true_width`/`true_height`, which can be narrower than
225/// `stride` -- rather than read out of range (spec 7.11.3.4's `Clip3`
226/// against the frame's edges): a motion vector that points past the true
227/// edge repeats the true edge's sample instead of panicking or picking up
228/// whatever this encoder's own uncoded padding columns/rows hold.
229#[allow(clippy::too_many_arguments)]
230fn sample(
231 reference: &[u8],
232 stride: usize,
233 true_width: usize,
234 true_height: usize,
235 x: i32,
236 y: i32,
237) -> i32 {
238 let cx = x.clamp(0, true_width as i32 - 1) as usize;
239 let cy = y.clamp(0, true_height as i32 - 1) as usize;
240 i32::from(reference[cy * stride + cx])
241}
242
243/// Predicts one `block_w * block_h` block into `dst`, row-major, from
244/// `reference` (row-major, stride `stride`, true content
245/// `true_width` x `true_height` -- a reference frame's decoded picture
246/// buffer can be wider/taller than the true frame it holds, when this
247/// encoder pads a picture to a whole number of superblocks; a spec decoder
248/// never codes syntax for those extra columns/rows, so motion compensation
249/// must clamp to the true extent, not the buffer's own stride).
250///
251/// `x_q4` / `y_q4` are the block's top-left position in the reference, in
252/// 1/16-pel units (an integer MV has its low 4 bits zero); this function
253/// splits each into its whole-sample part and its 0..16 fraction itself, so
254/// the caller need not pre-split a motion vector.
255///
256/// # Panics
257/// Panics when `dst` is not `block_w * block_h` long, or the reference is
258/// empty.
259#[allow(clippy::too_many_arguments)] // one reference plane, one position, one block shape
260pub fn predict(
261 reference: &[u8],
262 stride: usize,
263 true_width: usize,
264 true_height: usize,
265 x_q4: i32,
266 y_q4: i32,
267 block_w: usize,
268 block_h: usize,
269 dst: &mut [u8],
270) {
271 predict_with_filter(
272 reference,
273 stride,
274 true_width,
275 true_height,
276 x_q4,
277 y_q4,
278 block_w,
279 block_h,
280 InterpFilterKind::Regular,
281 dst,
282 );
283}
284
285/// [`predict`], selecting the interpolation filter kernel explicitly (spec
286/// 6.8.9's `interpolation_filter`) instead of always `Regular` -- the same
287/// kernel both directions.
288#[allow(clippy::too_many_arguments)]
289pub fn predict_with_filter(
290 reference: &[u8],
291 stride: usize,
292 true_width: usize,
293 true_height: usize,
294 x_q4: i32,
295 y_q4: i32,
296 block_w: usize,
297 block_h: usize,
298 filter_kind: InterpFilterKind,
299 dst: &mut [u8],
300) {
301 predict_with_filters(
302 reference,
303 stride,
304 true_width,
305 true_height,
306 x_q4,
307 y_q4,
308 block_w,
309 block_h,
310 filter_kind,
311 filter_kind,
312 dst,
313 );
314}
315
316/// [`predict_with_filter`], with the horizontal (`interp_filter[0]`) and
317/// vertical (`interp_filter[1]`) kernels chosen independently -- spec
318/// 5.11.20's per-block `SWITCHABLE` read is per-direction (`enable_dual_filter`),
319/// so the two passes can genuinely differ.
320#[allow(clippy::too_many_arguments)]
321pub fn predict_with_filters(
322 reference: &[u8],
323 stride: usize,
324 true_width: usize,
325 true_height: usize,
326 x_q4: i32,
327 y_q4: i32,
328 block_w: usize,
329 block_h: usize,
330 h_kind: InterpFilterKind,
331 v_kind: InterpFilterKind,
332 dst: &mut [u8],
333) {
334 assert_eq!(dst.len(), block_w * block_h, "the destination is the block");
335 assert!(!reference.is_empty(), "a reference plane has samples");
336
337 #[cfg(test)]
338 let stage_t = std::time::Instant::now();
339
340 let x0 = x_q4.div_euclid(16);
341 let xfrac = x_q4.rem_euclid(16) as usize;
342 let y0 = y_q4.div_euclid(16);
343 let yfrac = y_q4.rem_euclid(16) as usize;
344
345 // Whole-pel fast path: `SUBPEL_FILTERS[0]` is the identity tap (`128` at
346 // its centre, everything else `0`), and both rounding shifts are exactly
347 // that filter's own gain (see `INTER_ROUND_1`'s doc comment), so the two
348 // 8-tap passes reproduce the reference sample byte-for-byte here --
349 // `integer_mv_is_identity` below pins that. Skipping straight to a
350 // clamped copy is bit-identical, not an approximation: it matters
351 // because the motion search's own log/diamond stage (`motion.rs`) is
352 // whole-pel-only and calls `predict` for every candidate it prices, so
353 // this path is what most of a search's `predict` calls actually hit
354 // (measured: `stage_timing_breakdown_inter` attributed 403 of a 720p
355 // inter frame's 408ms motion-search bucket to `predict`, almost all of
356 // it whole-pel candidates from stage 1).
357 if xfrac == 0 && yfrac == 0 {
358 for row in 0..block_h {
359 let y = y0 + row as i32;
360 for col in 0..block_w {
361 let x = x0 + col as i32;
362 dst[row * block_w + col] =
363 sample(reference, stride, true_width, true_height, x, y) as u8;
364 }
365 }
366 #[cfg(test)]
367 crate::encode::stage_add(1, stage_t.elapsed());
368 return;
369 }
370
371 let (h_wide, h_narrow) = h_kind.tables();
372 let (v_wide, v_narrow) = v_kind.tables();
373 let h_filter = if block_w <= 4 {
374 &h_narrow[xfrac]
375 } else {
376 &h_wide[xfrac]
377 };
378 let v_filter = if block_h <= 4 {
379 &v_narrow[yfrac]
380 } else {
381 &v_wide[yfrac]
382 };
383
384 // The vertical pass reads 3 rows above and 4 below the block, so the
385 // horizontal pass must produce that many extra intermediate rows.
386 let rows = block_h + 7;
387 let mut intermediate = vec![0i32; rows * block_w];
388 for r in 0..rows {
389 let y = y0 - 3 + r as i32;
390 for c in 0..block_w {
391 let mut sum = 0;
392 for (t, &tap) in h_filter.iter().enumerate() {
393 let x = x0 + c as i32 + t as i32 - 3;
394 sum += tap * sample(reference, stride, true_width, true_height, x, y);
395 }
396 intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0);
397 }
398 }
399
400 for row in 0..block_h {
401 for col in 0..block_w {
402 let mut sum = 0;
403 for (t, &tap) in v_filter.iter().enumerate() {
404 sum += tap * intermediate[(row + t) * block_w + col];
405 }
406 dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8;
407 }
408 }
409
410 #[cfg(test)]
411 crate::encode::stage_add(1, stage_t.elapsed());
412}
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.
421const 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.
427const 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`].
433const 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)]
447pub 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.
513pub 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]
538fn 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
562#[cfg(test)]
563mod tests {
564 use super::{InterpFilterKind, predict, predict_with_filter};
565
566 /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma
567 /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15),
568 /// bw=bh=16) predicted the wrong value because this decoder always used
569 /// the REGULAR filter table -- aomdec's own `av1_convolve_2d_sr_c` trace
570 /// showed this block actually used `EIGHTTAP_SMOOTH`
571 /// (`0 0 2 34 62 28 2 0` at xfrac=15, not REGULAR's `0 0 -2 8 126 -6 2
572 /// 0`). Window and expected output dumped straight from an
573 /// instrumented aomdec's `av1_convolve_2d_sr_c` on the pinned stream.
574 #[test]
575 fn smooth_filter_matches_aomdec_chroma_inter_block() {
576 #[rustfmt::skip]
577 let window: [[u8; 24]; 24] = [
578 [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
579 [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
580 [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
581 [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
582 [114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,117,117,117,116,115,115,114,114],
583 [114,114,114,114,114,114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116,115,115,115],
584 [114,114,114,114,114,114,114,114,114,114,115,115,115,115,116,116,116,116,116,116,115,115,115,115],
585 [113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,116,116,116,116,115,115,115,115],
586 [113,113,113,113,113,113,113,113,113,113,114,114,114,114,115,115,115,115,115,115,115,115,115,115],
587 [112,112,112,112,112,112,112,112,112,113,113,113,113,114,114,114,114,114,115,115,115,115,115,115],
588 [111,111,111,111,111,111,111,111,112,112,112,112,113,113,113,113,114,114,114,114,115,115,115,115],
589 [110,110,110,110,110,110,110,110,111,111,111,111,112,112,112,113,113,113,113,113,114,114,114,115],
590 [109,109,109,109,109,109,109,110,110,110,110,111,111,111,111,112,112,112,112,113,113,114,114,114],
591 [108,108,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111,111,111,112,112,113,113,113],
592 [108,108,108,108,108,108,108,108,108,108,109,109,109,110,110,110,110,110,111,111,112,112,112,112],
593 [107,107,107,107,107,107,107,107,107,108,108,108,109,109,109,109,109,110,110,110,110,111,111,111],
594 [106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,110,110,110],
595 [106,106,106,106,106,106,106,106,106,107,107,107,107,108,108,108,108,109,109,109,109,109,109,109],
596 [105,105,105,105,105,105,105,105,106,106,106,107,107,107,107,108,108,108,108,108,108,108,108,109],
597 [104,104,104,104,104,104,104,104,104,105,105,106,106,106,107,107,107,107,107,107,107,108,108,108],
598 [102,102,102,102,102,103,103,103,103,104,104,105,105,105,106,106,107,107,107,107,107,107,107,107],
599 [101,101,101,101,101,101,101,102,102,103,103,104,104,104,105,105,106,106,106,106,106,107,107,107],
600 [100,100,100,100,100,100,100,101,101,102,102,103,103,104,104,105,105,105,106,106,106,106,106,106],
601 [99,99,99,99,99,99,100,100,100,101,101,102,103,103,104,104,105,105,105,105,105,105,105,106],
602 ];
603 // The dumped window spans real cols/rows -4..19; the block itself
604 // starts at real (0, 0), so pad the reference plane so col/row 4 of
605 // this buffer is real col/row 0, and hand `predict_with_filter` the
606 // real (0, 0) coordinates -- clamping then reproduces the identical
607 // extended border the dump already captured.
608 let stride = 24;
609 let mut reference = vec![0u8; stride * 24];
610 for (r, row) in window.iter().enumerate() {
611 reference[r * stride..r * stride + stride].copy_from_slice(row);
612 }
613 // predict()'s own edge clamp starts at true (0,0); shift so the
614 // dumped window's row/col 4 lands there by biasing x_q4/y_q4's
615 // whole-pel part by -4 and reading from a plane whose true origin
616 // is the window's row/col 4 -- simplest is to pass true_width /
617 // true_height covering the whole dumped window and offset the
618 // whole-pel part of x_q4/y_q4 by +4 to land on real (0,0).
619 let x_q4 = (4 + 0) * 16 + 15; // real block x0=0, xfrac=15
620 let y_q4 = (4 + 0) * 16 + 7; // real block y0=0, yfrac=7
621 let mut dst = vec![0u8; 16 * 16];
622 predict_with_filter(
623 &reference,
624 stride,
625 24,
626 24,
627 x_q4,
628 y_q4,
629 16,
630 16,
631 InterpFilterKind::Smooth,
632 &mut dst,
633 );
634 #[rustfmt::skip]
635 let expected_row0: [u8; 16] =
636 [114,114,114,114,115,115,115,116,116,116,116,116,117,117,116,116];
637 assert_eq!(
638 &dst[0..16],
639 &expected_row0,
640 "row0 vs aomdec's real av1_convolve_2d_sr_c output"
641 );
642 #[rustfmt::skip]
643 let expected_row15: [u8; 16] =
644 [103,103,104,104,104,105,105,106,106,106,107,107,107,107,107,107];
645 assert_eq!(
646 &dst[15 * 16..15 * 16 + 16],
647 &expected_row15,
648 "row15 vs aomdec"
649 );
650 }
651
652 #[test]
653 fn integer_mv_is_identity() {
654 let width = 12;
655 let height = 12;
656 let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect();
657 let mut dst = vec![0u8; 6 * 6];
658 predict(
659 &reference,
660 width,
661 width,
662 height,
663 3 * 16,
664 4 * 16,
665 6,
666 6,
667 &mut dst,
668 );
669 for row in 0..6 {
670 for col in 0..6 {
671 assert_eq!(
672 dst[row * 6 + col],
673 reference[(4 + row) * width + 3 + col],
674 "integer MV must reproduce the reference block byte-for-byte"
675 );
676 }
677 }
678 }
679
680 #[test]
681 fn half_pel_reproduces_the_ramp_midpoint() {
682 // Step 2 so every half-pel position lands on an exact integer.
683 let width = 16;
684 let height = 16;
685 let reference: Vec<u8> = (0..width * height)
686 .map(|i| (2 * (i % width)) as u8)
687 .collect();
688 let mut dst = vec![0u8; 4 * 4];
689 // Half-pel in x only: fraction 8/16, integer part at column 5.
690 predict(
691 &reference,
692 width,
693 width,
694 height,
695 5 * 16 + 8,
696 5 * 16,
697 4,
698 4,
699 &mut dst,
700 );
701 for row in 0..4 {
702 for col in 0..4 {
703 let midpoint = 2 * (5 + col) + 1; // halfway between col and col+1
704 assert_eq!(
705 dst[row * 4 + col] as i32,
706 midpoint as i32,
707 "half-pel MV over a linear ramp must land exactly on the midpoint"
708 );
709 }
710 }
711 }
712
713 #[test]
714 fn constant_plane_has_unit_dc_gain_at_every_subpel_position() {
715 let width = 20;
716 let height = 20;
717 let reference = vec![142u8; width * height];
718 for yfrac in 0..16 {
719 for xfrac in 0..16 {
720 let mut dst = vec![0u8; 5 * 5];
721 predict(
722 &reference,
723 width,
724 width,
725 height,
726 5 * 16 + xfrac,
727 5 * 16 + yfrac,
728 5,
729 5,
730 &mut dst,
731 );
732 assert!(
733 dst.iter().all(|&v| v == 142),
734 "DC gain must be exactly 1 at fraction ({xfrac}, {yfrac}), got {dst:?}"
735 );
736 }
737 }
738 }
739
740 #[test]
741 fn horizontal_and_vertical_only_filtering_differ() {
742 // A plane linear in both x and y with different, even coefficients so
743 // each axis's half-pel interpolation lands on an exact integer: this
744 // pins the actual value each pass produces, not just that the two
745 // differ, so swapping which pass reads which axis is caught even if
746 // it happened to produce two merely-different numbers.
747 let width = 10;
748 let height = 10;
749 let reference: Vec<u8> = (0..height)
750 .flat_map(|y| (0..width).map(move |x| (20 * x + 4 * y) as u8))
751 .collect();
752
753 let mut horiz = vec![0u8; 4 * 4];
754 predict(
755 &reference,
756 width,
757 width,
758 height,
759 3 * 16 + 8,
760 3 * 16,
761 4,
762 4,
763 &mut horiz,
764 );
765 let mut vert = vec![0u8; 4 * 4];
766 predict(
767 &reference,
768 width,
769 width,
770 height,
771 3 * 16,
772 3 * 16 + 8,
773 4,
774 4,
775 &mut vert,
776 );
777
778 for row in 0..4 {
779 for col in 0..4 {
780 // Horizontal half-pel: x interpolates to x0+col+0.5, y stays put.
781 assert_eq!(
782 horiz[row * 4 + col] as i32,
783 20 * (3 + col as i32) + 10 + 4 * (3 + row as i32),
784 "horizontal-only MV must interpolate along x only"
785 );
786 // Vertical half-pel: y interpolates to y0+row+0.5, x stays put.
787 assert_eq!(
788 vert[row * 4 + col] as i32,
789 20 * (3 + col as i32) + 4 * (3 + row as i32) + 2,
790 "vertical-only MV must interpolate along y only"
791 );
792 }
793 }
794 assert_ne!(
795 horiz, vert,
796 "horizontal-only and vertical-only sub-pel MVs must give different outputs"
797 );
798 }
799
800 #[test]
801 fn mv_past_the_edge_clamps_instead_of_panicking() {
802 let width = 8;
803 let height = 8;
804 let reference: Vec<u8> = (0..width * height).map(|i| (i * 3 % 200) as u8).collect();
805 let mut dst = vec![0u8; 4 * 4];
806 // Whole-sample position far above and to the left of the plane, with
807 // an odd fraction so both filter passes exercise the clamp.
808 predict(
809 &reference,
810 width,
811 width,
812 height,
813 -50 * 16 + 5,
814 -50 * 16 + 5,
815 4,
816 4,
817 &mut dst,
818 );
819 let expected = reference[0]; // the whole plane clamps to the top-left corner
820 assert!(
821 dst.iter().all(|&v| v == expected),
822 "an MV past the top-left edge must clamp to the corner sample, got {dst:?}"
823 );
824
825 let mut dst2 = vec![0u8; 4 * 4];
826 predict(
827 &reference,
828 width,
829 width,
830 height,
831 50 * 16 + 5,
832 50 * 16 + 5,
833 4,
834 4,
835 &mut dst2,
836 );
837 let expected2 = reference[height * width - 1]; // clamps to the bottom-right corner
838 assert!(
839 dst2.iter().all(|&v| v == expected2),
840 "an MV past the bottom-right edge must clamp to the corner sample, got {dst2:?}"
841 );
842 }
843}