forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits

inter.rs754 lines, 26.1 KiBHistoryRawDownload
1//! Inter prediction sample construction (spec 8.4.2): the quarter-sample luma
2//! interpolation of 8.4.2.2.1, the eighth-sample chroma interpolation of
3//! 8.4.2.2.2 and the weighted combination of 8.4.2.3.
4//!
5//! Reference planes are the same padded [`crate::decoder::Plane8`] the decoder
6//! writes into, with the borders replicated once per picture
7//! (`Plane8::extend_borders`). That turns the per-sample `Clip3` of Equations
8//! 8-239 and 8-240 into a single clamp of the block origin: any position the
9//! spec would clip to an edge sample lands on the replicated copy of that same
10//! edge sample, so the interpolation loop never bounds-checks and never
11//! branches per sample. The clamp bounds below are what makes that exact —
12//! see [`clamp_origin`].
13
14use wide::{i16x8, i16x16, i32x8};
15
16/// A reference picture plane for motion compensation.
17pub(crate) struct RefPlane<'a> {
18 /// The whole padded plane.
19 pub data: &'a [u8],
20 /// Row pitch.
21 pub stride: usize,
22 /// Index of picture sample (0, 0).
23 pub origin: usize,
24 /// Picture width in samples.
25 pub width: usize,
26 /// Picture height in samples.
27 pub height: usize,
28 /// Replicated border width on every side.
29 pub pad: usize,
30}
31
32impl RefPlane<'_> {
33 #[inline]
34 fn at(&self, x: i32, y: i32) -> usize {
35 (self.origin as isize + y as isize * self.stride as isize + x as isize) as usize
36 }
37}
38
39/// Clamp a block origin so that every tap the interpolator reads stays inside
40/// the replicated border while producing the samples Equations 8-239/8-240
41/// would have clipped to.
42///
43/// `left`/`right` are the tap reach on each side (2 and 3 for the 6-tap luma
44/// filter, 0 and 1 for the bilinear chroma filter) and `size` the block extent.
45/// Once the block is completely outside the picture every tap resolves to the
46/// same edge sample, so clamping the origin to the last position where that is
47/// still true is exact rather than approximate.
48#[inline]
49fn clamp_origin(v: i32, size: usize, extent: usize, pad: usize, left: i32, right: i32) -> i32 {
50 let lo = -(pad as i32) + left;
51 let hi = extent as i32 + pad as i32 - right - size as i32;
52 v.clamp(lo.min(hi), hi)
53}
54
55/// Sixteen samples as 16-bit lanes, zero past the end of the plane.
56///
57/// A partition is at most 16 samples wide, so one vector covers a whole row and
58/// the lanes past `w` are simply never stored. Every index the interpolator
59/// reaches is inside the replicated border (see [`clamp_origin`]), so the zero
60/// fill is reachable only by those discarded lanes.
61#[inline]
62fn load16(data: &[u8], at: usize) -> i16x16 {
63 let mut a = [0i16; 16];
64 // The fixed-length slice is what makes this one widening load: a zip
65 // against an open-ended slice leaves the trip count unknown and the
66 // compiler emits a byte loop instead.
67 match data.get(at..at + 16) {
68 Some(row) => {
69 for (o, &b) in a.iter_mut().zip(row) {
70 *o = i16::from(b);
71 }
72 }
73 None => {
74 for (o, &b) in a.iter_mut().zip(&data[at..]) {
75 *o = i16::from(b);
76 }
77 }
78 }
79 i16x16::from(a)
80}
81
82/// The same over eight lanes, for chroma blocks (at most eight wide).
83#[inline]
84fn load8(data: &[u8], at: usize) -> i16x8 {
85 let mut a = [0i16; 8];
86 match data.get(at..at + 8) {
87 Some(row) => {
88 for (o, &b) in a.iter_mut().zip(row) {
89 *o = i16::from(b);
90 }
91 }
92 None => {
93 for (o, &b) in a.iter_mut().zip(&data[at..]) {
94 *o = i16::from(b);
95 }
96 }
97 }
98 i16x8::from(a)
99}
100
101/// The same 6-tap filter over eight neighbouring positions.
102///
103/// Eight lanes is one SSE2 register, which is the widest vector this crate can
104/// assume without a target feature it is not allowed to require. A partition
105/// narrower than sixteen samples therefore costs half or a quarter as much
106/// here as it does in the sixteen-lane form, where the surplus lanes are
107/// computed and then discarded.
108#[inline]
109fn tap6_8(data: &[u8], at: usize, step: usize) -> i16x8 {
110 let l = |k: isize| load8(data, (at as isize + k * step as isize) as usize);
111 let five = i16x8::splat(5);
112 let twenty = i16x8::splat(20);
113 l(-2) - five * l(-1) + twenty * l(0) + twenty * l(1) - five * l(2) + l(3)
114}
115
116/// The same filter over sixteen positions, for a full-width partition.
117#[inline]
118fn tap6_16(data: &[u8], at: usize, step: usize) -> i16x16 {
119 let l = |k: isize| load16(data, (at as isize + k * step as isize) as usize);
120 let five = i16x16::splat(5);
121 let twenty = i16x16::splat(20);
122 l(-2) - five * l(-1) + twenty * l(0) + twenty * l(1) - five * l(2) + l(3)
123}
124
125/// `Clip1Y( ( v + 16 ) >> 5 )` over a row, storing the first `dst.len()` lanes.
126#[inline]
127fn store_half8(dst: &mut [u8], v: i16x8) {
128 let c = ((v + i16x8::splat(16)) >> 5u32)
129 .max(i16x8::ZERO)
130 .min(i16x8::splat(255));
131 for (o, &s) in dst.iter_mut().zip(c.as_array()) {
132 *o = s as u8;
133 }
134}
135
136/// The same over sixteen lanes.
137#[inline]
138fn store_half16(dst: &mut [u8], v: i16x16) {
139 let c = ((v + i16x16::splat(16)) >> 5u32)
140 .max(i16x16::ZERO)
141 .min(i16x16::splat(255));
142 for (o, &s) in dst.iter_mut().zip(c.as_array()) {
143 *o = s as u8;
144 }
145}
146
147/// One 6-tap half-sample pass over a `w` x `h` block whose top-left sample is
148/// `base`, into a `w`-pitch buffer. `step` is the tap spacing: 1 for the
149/// horizontal filter, the plane stride for the vertical one.
150///
151/// A partition is 16, 8 or 4 samples wide, so the narrow widths take a single
152/// eight-lane vector rather than a sixteen-lane one whose upper half is
153/// discarded.
154#[inline]
155fn half_block(
156 dst: &mut [u8],
157 data: &[u8],
158 base: usize,
159 stride: usize,
160 step: usize,
161 w: usize,
162 h: usize,
163) {
164 if w == 16 {
165 for row in 0..h {
166 store_half16(
167 &mut dst[row * 16..row * 16 + 16],
168 tap6_16(data, base + row * stride, step),
169 );
170 }
171 } else {
172 for row in 0..h {
173 store_half8(
174 &mut dst[row * w..row * w + w],
175 tap6_8(data, base + row * stride, step),
176 );
177 }
178 }
179}
180
181/// Eight intermediate values as 32-bit lanes, zero past the end.
182#[inline]
183fn load_inter8(v: &[i16], at: usize) -> i32x8 {
184 let mut a = [0i32; 8];
185 for (o, &s) in a.iter_mut().zip(&v[at..]) {
186 *o = i32::from(s);
187 }
188 i32x8::from(a)
189}
190
191/// The 6-tap filter over already-filtered intermediate values (Equation
192/// 8-246), eight columns at a time. Those sums reach 449820, so this pass is
193/// 32-bit where the first one was 16-bit.
194#[inline]
195fn tap6_inter8(v: &[i16], at: usize, step: usize) -> i32x8 {
196 let l = |k: isize| load_inter8(v, (at as isize + k * step as isize) as usize);
197 let five = i32x8::splat(5);
198 let twenty = i32x8::splat(20);
199 l(-2) - five * l(-1) + twenty * l(0) + twenty * l(1) - five * l(2) + l(3)
200}
201
202/// `Clip1Y( ( v + 512 ) >> 10 )` over eight lanes.
203#[inline]
204fn store_centre(dst: &mut [u8], v: i32x8) {
205 let c = ((v + i32x8::splat(512)) >> 10u32)
206 .max(i32x8::ZERO)
207 .min(i32x8::splat(255));
208 for (o, &s) in dst.iter_mut().zip(c.as_array()) {
209 *o = s as u8;
210 }
211}
212
213/// The rounded average of Equation 8-247 over `n` samples, sixteen at a time.
214#[inline]
215fn avg_rows(out: &mut [u8], a: &[u8], b: &[u8], n: usize) {
216 // Written as a plain loop over equal-length slices: this is the rounded
217 // byte average, which the compiler turns into one `pavgb` per sixteen
218 // samples — half the work of widening to 16-bit lanes and narrowing back.
219 let (out, a, b) = (&mut out[..n], &a[..n], &b[..n]);
220 for i in 0..n {
221 out[i] = avg(a[i], b[i]);
222 }
223}
224
225/// The same average, written into a destination of pitch `os` from two
226/// `w`-pitch sources.
227#[inline]
228fn avg_rows_to(out: &mut [u8], os: usize, a: &[u8], b: &[u8], w: usize, h: usize) {
229 if os == w {
230 avg_rows(out, a, b, w * h);
231 return;
232 }
233 for row in 0..h {
234 avg_rows(
235 &mut out[row * os..row * os + w],
236 &a[row * w..],
237 &b[row * w..],
238 w,
239 );
240 }
241}
242
243/// Copy `w` x `h` samples from a `w`-pitch source into an `os`-pitch
244/// destination.
245#[inline]
246fn copy_rows(out: &mut [u8], os: usize, src: &[u8], w: usize, h: usize) {
247 for row in 0..h {
248 out[row * os..row * os + w].copy_from_slice(&src[row * w..row * w + w]);
249 }
250}
251
252#[inline]
253fn clip8(v: i32) -> u8 {
254 v.clamp(0, 255) as u8
255}
256
257#[inline]
258fn avg(a: u8, b: u8) -> u8 {
259 ((u32::from(a) + u32::from(b) + 1) >> 1) as u8
260}
261
262/// Widest intermediate row the luma interpolator needs: a 16-wide partition
263/// plus the five taps of the horizontal filter.
264const TMP_W: usize = 16 + 5;
265
266/// Index of the top-left sample of a whole-sample block, clamped exactly as
267/// [`mc_luma`] clamps it.
268///
269/// A motion search spends most of its time on whole-sample positions, where
270/// prediction is a copy; this lets it read the reference plane in place
271/// instead of copying every candidate into a buffer first.
272#[inline]
273pub(crate) fn integer_origin(r: &RefPlane<'_>, x: i32, y: i32, w: usize, h: usize) -> usize {
274 let xi = clamp_origin(x, w, r.width, r.pad, 2, 3);
275 let yi = clamp_origin(y, h, r.height, r.pad, 2, 3);
276 r.at(xi, yi)
277}
278
279/// Luma quarter-sample interpolation (8.4.2.2.1) of a `w` x `h` partition whose
280/// full-sample origin is `(x, y)` and whose motion vector is `mv` in quarter
281/// samples. Writes `w` x `h` prediction samples into `out` at pitch `os`.
282///
283/// The pitch is a parameter so that the single-list, default-weight case —
284/// which is most of a P slice — interpolates straight into the picture instead
285/// of into a temporary that is then copied there.
286#[allow(clippy::too_many_arguments)]
287pub(crate) fn mc_luma(
288 r: &RefPlane<'_>,
289 x: i32,
290 y: i32,
291 mv: [i16; 2],
292 w: usize,
293 h: usize,
294 os: usize,
295 out: &mut [u8],
296) {
297 let (fx, fy) = ((mv[0] & 3) as usize, (mv[1] & 3) as usize);
298 let xi = clamp_origin(x + (mv[0] as i32 >> 2), w, r.width, r.pad, 2, 3);
299 let yi = clamp_origin(y + (mv[1] as i32 >> 2), h, r.height, r.pad, 2, 3);
300 let stride = r.stride;
301 let base = r.at(xi, yi);
302
303 // Full sample: a straight copy.
304 if fx == 0 && fy == 0 {
305 for row in 0..h {
306 let src = base + row * stride;
307 out[row * os..row * os + w].copy_from_slice(&r.data[src..src + w]);
308 }
309 return;
310 }
311
312 // Half-sample rows ("b" of Equation 8-243) for rows yi..yi+h, plus one more
313 // row when the quarter position also needs "s".
314 let need_b = fx != 0 && fy != 2;
315 let need_h = fy != 0 && fx != 2;
316 let need_j = (fx == 2 && fy != 0) || (fy == 2 && fx != 0);
317
318 let mut bb = [0u8; 16 * 17];
319 if need_b {
320 // "s" is the same filter one row down, so row fy>>1 selects b or s.
321 let row0 = yi + if fy == 3 { 1 } else { 0 };
322 half_block(&mut bb, r.data, r.at(xi, row0), stride, 1, w, h);
323 }
324 let mut hh = [0u8; 16 * 17];
325 if need_h {
326 // "m" is the same filter one column right, so column fx>>1 selects h or m.
327 let col0 = xi + if fx == 3 { 1 } else { 0 };
328 half_block(&mut hh, r.data, r.at(col0, yi), stride, stride, w, h);
329 }
330 let mut jj = [0u8; 16 * 16];
331 if need_j {
332 // Equation 8-246: filter horizontally into intermediates, then
333 // vertically over those. The horizontal pass needs two rows above and
334 // three below the partition.
335 let mut inter = [0i16; TMP_W * (16 + 5)];
336 for row in 0..h + 5 {
337 let src = r.at(xi, yi + row as i32 - 2);
338 let base = row * TMP_W;
339 if w == 16 {
340 let v = tap6_16(r.data, src, 1);
341 inter[base..base + 16].copy_from_slice(v.as_array());
342 } else {
343 let v = tap6_8(r.data, src, 1);
344 for (o, &t) in inter[base..base + w].iter_mut().zip(v.as_array()) {
345 *o = t;
346 }
347 }
348 }
349 for row in 0..h {
350 for col in (0..w).step_by(8) {
351 let v = tap6_inter8(&inter, (row + 2) * TMP_W + col, TMP_W);
352 let n = (w - col).min(8);
353 store_centre(&mut jj[row * w + col..row * w + col + n], v);
354 }
355 }
356 }
357
358 // Table 8-12: each quarter position is one of the half-sample arrays, or
359 // the average of two of them. Resolving it once per partition rather than
360 // once per sample is what keeps the copy loops straight.
361 match (fx, fy) {
362 (2, 0) => copy_rows(out, os, &bb, w, h),
363 (0, 2) => copy_rows(out, os, &hh, w, h),
364 (2, 2) => copy_rows(out, os, &jj, w, h),
365 (2, 1) | (2, 3) => avg_rows_to(out, os, &bb, &jj, w, h),
366 (1, 2) | (3, 2) => avg_rows_to(out, os, &hh, &jj, w, h),
367 (1, 0) | (3, 0) | (0, 1) | (0, 3) => {
368 // Averaged with an integer sample one row or column over.
369 let (dx, dy) = match (fx, fy) {
370 (3, 0) => (1, 0),
371 (0, 3) => (0, 1),
372 _ => (0, 0),
373 };
374 let half: &[u8] = if fy == 0 { &bb } else { &hh };
375 for row in 0..h {
376 let src = base + (row + dy) * stride + dx;
377 let s = &r.data[src..src + w];
378 let hl = &half[row * w..row * w + w];
379 let dst = &mut out[row * os..row * os + w];
380 for col in 0..w {
381 dst[col] = avg(s[col], hl[col]);
382 }
383 }
384 }
385 // Diagonal quarter positions: the two nearest half samples.
386 _ => avg_rows_to(out, os, &bb, &hh, w, h),
387 }
388}
389
390/// Equation 8-270 over a block of statically known width.
391///
392/// The width is a const parameter because a chroma block is 2, 4 or 8 samples
393/// wide and the 2x2 case dominates real streams: with `W` known the row slices
394/// have constant length, which drops the bounds checks and lets the smallest
395/// blocks compile to a handful of scalar multiplies instead of a vector setup
396/// whose lanes are then thrown away.
397#[inline]
398fn chroma_rows<const W: usize>(
399 data: &[u8],
400 base: usize,
401 stride: usize,
402 h: usize,
403 wt: [u16; 4],
404 os: usize,
405 out: &mut [u8],
406) {
407 for row in 0..h {
408 let src = base + row * stride;
409 let p = &data[src..src + W + 1];
410 let q = &data[src + stride..src + stride + W + 1];
411 let dst = &mut out[row * os..row * os + W];
412 for c in 0..W {
413 let v = u16::from(p[c]) * wt[0]
414 + u16::from(p[c + 1]) * wt[1]
415 + u16::from(q[c]) * wt[2]
416 + u16::from(q[c + 1]) * wt[3]
417 + 32;
418 dst[c] = (v >> 6) as u8;
419 }
420 }
421}
422
423/// Chroma eighth-sample interpolation (Equation 8-270) of a `w` x `h` block.
424#[allow(clippy::too_many_arguments)]
425pub(crate) fn mc_chroma(
426 r: &RefPlane<'_>,
427 x: i32,
428 y: i32,
429 mv: [i16; 2],
430 w: usize,
431 h: usize,
432 os: usize,
433 out: &mut [u8],
434) {
435 let (fx, fy) = ((mv[0] & 7) as u16, (mv[1] & 7) as u16);
436 let xi = clamp_origin(x + (mv[0] as i32 >> 3), w, r.width, r.pad, 0, 1);
437 let yi = clamp_origin(y + (mv[1] as i32 >> 3), h, r.height, r.pad, 0, 1);
438 let stride = r.stride;
439 let base = r.at(xi, yi);
440
441 // Full sample: a straight copy. A zero motion vector is the most common
442 // vector in real content, and it lands here.
443 if fx == 0 && fy == 0 {
444 for row in 0..h {
445 let src = base + row * stride;
446 out[row * os..row * os + w].copy_from_slice(&r.data[src..src + w]);
447 }
448 return;
449 }
450
451 // The four weights sum to 64, so the weighted sum of Equation 8-270 never
452 // exceeds 64 * 255 and the whole bilinear fits in 16-bit lanes.
453 let wt = [(8 - fx) * (8 - fy), fx * (8 - fy), (8 - fx) * fy, fx * fy];
454 match w {
455 2 => chroma_rows::<2>(r.data, base, stride, h, wt, os, out),
456 4 => chroma_rows::<4>(r.data, base, stride, h, wt, os, out),
457 _ => chroma_rows::<8>(r.data, base, stride, h, wt, os, out),
458 }
459}
460
461/// Weights of one component for one partition (spec 8.4.3), already resolved
462/// to the explicit/implicit/default case.
463#[derive(Clone, Copy, Debug, PartialEq, Eq)]
464pub(crate) struct Weights {
465 /// `logWDC`.
466 pub log_wd: i32,
467 /// `w0C`, `w1C`.
468 pub w: [i32; 2],
469 /// `o0C`, `o1C`.
470 pub o: [i32; 2],
471}
472
473impl Weights {
474 /// The identity weighting, which reproduces 8.4.2.3.1 exactly.
475 pub(crate) const DEFAULT: Weights = Weights {
476 log_wd: 0,
477 w: [1, 1],
478 o: [0, 0],
479 };
480
481 /// True when 8.4.2.3.1 applies, so a single-list prediction is its own
482 /// final prediction and needs no combining pass at all.
483 pub(crate) fn is_default(&self) -> bool {
484 *self == Weights::DEFAULT
485 }
486}
487
488/// Combine the list predictions of one partition into the final prediction
489/// (spec 8.4.2.3). `p0`/`p1` hold `n` samples each; the entry for a list that
490/// is not used is ignored.
491///
492/// The result is contiguous rather than written at the picture's pitch: the
493/// bi-predictive average is the one case where the samples of several rows are
494/// adjacent, and vectors that span the row boundary measured 6 to 9 per cent
495/// fewer instructions on 1080p than a per-row average at picture pitch.
496pub(crate) fn combine(
497 out: &mut [u8],
498 p0: &[u8],
499 p1: &[u8],
500 use0: bool,
501 use1: bool,
502 wt: &Weights,
503 n: usize,
504) {
505 if wt.is_default() {
506 // 8.4.2.3.1.
507 match (use0, use1) {
508 (true, false) => out[..n].copy_from_slice(&p0[..n]),
509 (false, true) => out[..n].copy_from_slice(&p1[..n]),
510 _ => avg_rows(out, p0, p1, n),
511 }
512 return;
513 }
514 // 8.4.2.3.2.
515 if use0 && use1 {
516 let round = 1 << wt.log_wd;
517 let off = (wt.o[0] + wt.o[1] + 1) >> 1;
518 for i in 0..n {
519 let v = (i32::from(p0[i]) * wt.w[0] + i32::from(p1[i]) * wt.w[1] + round)
520 >> (wt.log_wd + 1);
521 out[i] = clip8(v + off);
522 }
523 return;
524 }
525 let (src, k) = if use0 { (p0, 0) } else { (p1, 1) };
526 if wt.log_wd >= 1 {
527 let round = 1 << (wt.log_wd - 1);
528 for i in 0..n {
529 out[i] = clip8(((i32::from(src[i]) * wt.w[k] + round) >> wt.log_wd) + wt.o[k]);
530 }
531 } else {
532 for i in 0..n {
533 out[i] = clip8(i32::from(src[i]) * wt.w[k] + wt.o[k]);
534 }
535 }
536}
537
538#[cfg(test)]
539mod tests {
540 use super::*;
541
542 /// A plane whose interior is a known ramp and whose borders are replicated,
543 /// the way `Plane8::extend_borders` leaves them.
544 fn ramp_plane(w: usize, h: usize, pad: usize) -> (Vec<u8>, usize, usize) {
545 let stride = w + 2 * pad;
546 let origin = pad * stride + pad;
547 let mut data = vec![0u8; stride * (h + 2 * pad)];
548 for y in 0..h {
549 for x in 0..w {
550 data[origin + y * stride + x] = ((x * 3 + y * 7) % 251) as u8;
551 }
552 }
553 for y in 0..h {
554 let row = origin + y * stride;
555 let (l, r) = (data[row], data[row + w - 1]);
556 data[row - pad..row].fill(l);
557 data[row + w..row + w + pad].fill(r);
558 }
559 for y in 0..pad {
560 let top = origin - (y + 1) * stride - pad;
561 let src = origin - pad;
562 let (a, b) = data.split_at_mut(src);
563 a[top..top + stride].copy_from_slice(&b[..stride]);
564 let bot = origin + (h + y) * stride - pad;
565 let src = origin + (h - 1) * stride - pad;
566 let (a, b) = data.split_at_mut(bot);
567 b[..stride].copy_from_slice(&a[src..src + stride]);
568 }
569 (data, stride, origin)
570 }
571
572 fn plane(
573 data: &[u8],
574 stride: usize,
575 origin: usize,
576 w: usize,
577 h: usize,
578 pad: usize,
579 ) -> RefPlane<'_> {
580 RefPlane {
581 data,
582 stride,
583 origin,
584 width: w,
585 height: h,
586 pad,
587 }
588 }
589
590 /// The spec's clipped, per-sample definition of the same interpolation,
591 /// written straight from Equations 8-239 to 8-261 with no padding tricks.
592 #[allow(clippy::too_many_arguments)]
593 fn spec_luma(
594 pic: &[u8],
595 stride: usize,
596 origin: usize,
597 w: usize,
598 h: usize,
599 x: i32,
600 y: i32,
601 mv: [i16; 2],
602 pw: usize,
603 ph: usize,
604 out: &mut [u8],
605 ) {
606 let s = |px: i32, py: i32| -> i32 {
607 let cx = px.clamp(0, w as i32 - 1) as usize;
608 let cy = py.clamp(0, h as i32 - 1) as usize;
609 i32::from(pic[origin + cy * stride + cx])
610 };
611 let hf = |px: i32, py: i32| {
612 s(px - 2, py) - 5 * s(px - 1, py) + 20 * s(px, py) + 20 * s(px + 1, py)
613 - 5 * s(px + 2, py)
614 + s(px + 3, py)
615 };
616 let vf = |px: i32, py: i32| {
617 s(px, py - 2) - 5 * s(px, py - 1) + 20 * s(px, py) + 20 * s(px, py + 1)
618 - 5 * s(px, py + 2)
619 + s(px, py + 3)
620 };
621 let j1 = |px: i32, py: i32| {
622 let t = |k: i32| hf(px, py + k);
623 t(-2) - 5 * t(-1) + 20 * t(0) + 20 * t(1) - 5 * t(2) + t(3)
624 };
625 let (fx, fy) = ((mv[0] & 3) as i32, (mv[1] & 3) as i32);
626 let (xi, yi) = (x + (mv[0] as i32 >> 2), y + (mv[1] as i32 >> 2));
627 for row in 0..ph {
628 for col in 0..pw {
629 let (px, py) = (xi + col as i32, yi + row as i32);
630 let g = |dx: i32, dy: i32| s(px + dx, py + dy) as u8;
631 let b = |dy: i32| clip8((hf(px, py + dy) + 16) >> 5);
632 let hs = |dx: i32| clip8((vf(px + dx, py) + 16) >> 5);
633 let j = clip8((j1(px, py) + 512) >> 10);
634 out[row * pw + col] = match (fx, fy) {
635 (0, 0) => g(0, 0),
636 (1, 0) => avg(g(0, 0), b(0)),
637 (2, 0) => b(0),
638 (3, 0) => avg(g(1, 0), b(0)),
639 (0, 1) => avg(g(0, 0), hs(0)),
640 (0, 2) => hs(0),
641 (0, 3) => avg(g(0, 1), hs(0)),
642 (1, 1) => avg(b(0), hs(0)),
643 (3, 1) => avg(b(0), hs(1)),
644 (1, 3) => avg(hs(0), b(1)),
645 (3, 3) => avg(hs(1), b(1)),
646 (2, 1) => avg(b(0), j),
647 (2, 3) => avg(b(1), j),
648 (1, 2) => avg(hs(0), j),
649 (3, 2) => avg(hs(1), j),
650 _ => j,
651 };
652 }
653 }
654 }
655
656 /// Every fractional position, at interior positions and far outside the
657 /// picture, agrees with the spec's clipped definition.
658 #[test]
659 fn luma_interpolation_matches_the_clipped_definition() {
660 let (w, h, pad) = (48usize, 32usize, 32usize);
661 let (data, stride, origin) = ramp_plane(w, h, pad);
662 let r = plane(&data, stride, origin, w, h, pad);
663 let mut ours = [0u8; 256];
664 let mut theirs = [0u8; 256];
665 for &(px, py) in &[
666 (8i32, 8i32),
667 (0, 0),
668 (32, 16),
669 (-40, -40),
670 (60, 44),
671 (44, 4),
672 ] {
673 for fy in 0..4i16 {
674 for fx in 0..4i16 {
675 for &(pw, ph) in &[(16usize, 16usize), (8, 4), (4, 8), (4, 4)] {
676 let mv = [fx - 20 * 4, fy + 12];
677 mc_luma(&r, px, py, mv, pw, ph, pw, &mut ours);
678 spec_luma(&data, stride, origin, w, h, px, py, mv, pw, ph, &mut theirs);
679 assert_eq!(
680 ours[..pw * ph],
681 theirs[..pw * ph],
682 "({px},{py}) frac ({fx},{fy}) {pw}x{ph}"
683 );
684 }
685 }
686 }
687 }
688 }
689
690 #[test]
691 fn chroma_interpolation_matches_equation_8_270() {
692 let (w, h, pad) = (24usize, 16usize, 16usize);
693 let (data, stride, origin) = ramp_plane(w, h, pad);
694 let r = plane(&data, stride, origin, w, h, pad);
695 let mut ours = [0u8; 64];
696 for &(px, py) in &[(4i32, 4i32), (0, 0), (-30, -30), (30, 22)] {
697 for fy in 0..8i16 {
698 for fx in 0..8i16 {
699 let mv = [fx, fy];
700 mc_chroma(&r, px, py, mv, 8, 8, 8, &mut ours);
701 for row in 0..8i32 {
702 for col in 0..8i32 {
703 let s = |dx: i32, dy: i32| -> i32 {
704 let cx = (px + col + dx).clamp(0, w as i32 - 1) as usize;
705 let cy = (py + row + dy).clamp(0, h as i32 - 1) as usize;
706 i32::from(data[origin + cy * stride + cx])
707 };
708 let (fx, fy) = (i32::from(fx), i32::from(fy));
709 let want = ((8 - fx) * (8 - fy) * s(0, 0)
710 + fx * (8 - fy) * s(1, 0)
711 + (8 - fx) * fy * s(0, 1)
712 + fx * fy * s(1, 1)
713 + 32)
714 >> 6;
715 assert_eq!(
716 i32::from(ours[(row * 8 + col) as usize]),
717 want,
718 "({px},{py}) frac ({fx},{fy}) at ({col},{row})"
719 );
720 }
721 }
722 }
723 }
724 }
725 }
726
727 #[test]
728 fn weighted_combination_follows_8_4_2_3() {
729 let p0 = [100u8; 4];
730 let p1 = [200u8; 4];
731 let mut out = [0u8; 4];
732 combine(&mut out, &p0, &p1, true, true, &Weights::DEFAULT, 4);
733 assert_eq!(out, [150; 4]);
734 // Explicit, single list: ((100 * 2 + 2) >> 2) + 5 = 55.
735 let w = Weights {
736 log_wd: 2,
737 w: [2, 3],
738 o: [5, -5],
739 };
740 combine(&mut out, &p0, &p1, true, false, &w, 4);
741 assert_eq!(out, [55; 4]);
742 // Explicit, both lists: ((100*2 + 200*3 + 4) >> 3) + ((5 - 5 + 1) >> 1).
743 combine(&mut out, &p0, &p1, true, true, &w, 4);
744 assert_eq!(out, [100; 4]);
745 // logWD 0 skips the rounding term entirely.
746 let w0 = Weights {
747 log_wd: 0,
748 w: [2, 1],
749 o: [3, 0],
750 };
751 combine(&mut out, &p0, &p1, true, false, &w0, 4);
752 assert_eq!(out, [203; 4]);
753 }
754}