forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

inter16.rs116 lines, 4.2 KiBHistoryRawDownload
1//! Fractional-sample interpolation (clause 8.4.2.2) for planes of `u16`
2//! samples, written straight from the equations: each partition reads a
3//! clamped window of the reference and computes in `i32`. The 8-bit decoder
4//! has its own SIMD kernels in [`crate::inter`]; this is the High 10 path.
5
6use crate::dpb::PicPlane;
7
8/// Largest window a 16x16 luma partition reads: 21 x 21.
9const WIN: usize = 21 * 21;
10
11/// Copy the `ww` x `wh` window whose top-left sample is `(x0, y0)` out of the
12/// plane, clamping coordinates into the picture (Equations 8-239/8-240).
13fn window(p: &PicPlane<u16>, x0: i32, y0: i32, ww: usize, wh: usize, buf: &mut [i32]) {
14 let (pw, ph) = (p.width as i32, p.height as i32);
15 for r in 0..wh {
16 let y = (y0 + r as i32).clamp(0, ph - 1) as usize;
17 let row = p.at(0, y);
18 for c in 0..ww {
19 let x = (x0 + c as i32).clamp(0, pw - 1) as usize;
20 buf[r * ww + c] = i32::from(p.data[row + x]);
21 }
22 }
23}
24
25/// Luma prediction samples (8.4.2.2.1) of a `w` x `h` partition at `(x, y)`
26/// displaced by `mv` (quarter samples), into `out` at pitch `w`.
27#[allow(clippy::too_many_arguments)]
28pub(crate) fn luma_pred(
29 p: &PicPlane<u16>,
30 x: i32,
31 y: i32,
32 mv: [i16; 2],
33 w: usize,
34 h: usize,
35 max: i32,
36 out: &mut [i32; 256],
37) {
38 let (fx, fy) = ((mv[0] & 3) as i32, (mv[1] & 3) as i32);
39 let (xi, yi) = (x + (i32::from(mv[0]) >> 2), y + (i32::from(mv[1]) >> 2));
40 let (ww, wh) = (w + 5, h + 5);
41 let mut win = [0i32; WIN];
42 window(p, xi - 2, yi - 2, ww, wh, &mut win);
43 let clip = |v: i32| v.clamp(0, max);
44 for row in 0..h {
45 for col in 0..w {
46 let s = |dx: i32, dy: i32| {
47 win[((row as i32 + 2 + dy) as usize) * ww + (col as i32 + 2 + dx) as usize]
48 };
49 let hf = |dy: i32| {
50 s(-2, dy) - 5 * s(-1, dy) + 20 * s(0, dy) + 20 * s(1, dy) - 5 * s(2, dy) + s(3, dy)
51 };
52 let vf = |dx: i32| {
53 s(dx, -2) - 5 * s(dx, -1) + 20 * s(dx, 0) + 20 * s(dx, 1) - 5 * s(dx, 2) + s(dx, 3)
54 };
55 let g = |dx: i32, dy: i32| s(dx, dy);
56 let b = |dy: i32| clip((hf(dy) + 16) >> 5);
57 let hs = |dx: i32| clip((vf(dx) + 16) >> 5);
58 let j = || {
59 let t = |k: i32| hf(k);
60 clip((t(-2) - 5 * t(-1) + 20 * t(0) + 20 * t(1) - 5 * t(2) + t(3) + 512) >> 10)
61 };
62 let avg = |a: i32, b: i32| (a + b + 1) >> 1;
63 out[row * w + col] = match (fx, fy) {
64 (0, 0) => g(0, 0),
65 (1, 0) => avg(g(0, 0), b(0)),
66 (2, 0) => b(0),
67 (3, 0) => avg(g(1, 0), b(0)),
68 (0, 1) => avg(g(0, 0), hs(0)),
69 (0, 2) => hs(0),
70 (0, 3) => avg(g(0, 1), hs(0)),
71 (1, 1) => avg(b(0), hs(0)),
72 (3, 1) => avg(b(0), hs(1)),
73 (1, 3) => avg(hs(0), b(1)),
74 (3, 3) => avg(hs(1), b(1)),
75 (2, 1) => avg(b(0), j()),
76 (2, 3) => avg(b(1), j()),
77 (1, 2) => avg(hs(0), j()),
78 (3, 2) => avg(hs(1), j()),
79 _ => j(),
80 };
81 }
82 }
83}
84
85/// Chroma prediction samples (8.4.2.2.2, Equation 8-270) of a `w` x `h` block
86/// at `(x, y)` displaced by `mv` (eighth samples), into `out` at pitch `w`.
87#[allow(clippy::too_many_arguments)]
88pub(crate) fn chroma_pred(
89 p: &PicPlane<u16>,
90 x: i32,
91 y: i32,
92 mv: [i16; 2],
93 w: usize,
94 h: usize,
95 out: &mut [i32; 256],
96) {
97 let (fx, fy) = ((mv[0] & 7) as i32, (mv[1] & 7) as i32);
98 let (xi, yi) = (x + (i32::from(mv[0]) >> 3), y + (i32::from(mv[1]) >> 3));
99 let (ww, wh) = (w + 1, h + 1);
100 let mut win = [0i32; 9 * 17];
101 window(p, xi, yi, ww, wh, &mut win);
102 for row in 0..h {
103 for col in 0..w {
104 let a = win[row * ww + col];
105 let b = win[row * ww + col + 1];
106 let c = win[(row + 1) * ww + col];
107 let d = win[(row + 1) * ww + col + 1];
108 out[row * w + col] = ((8 - fx) * (8 - fy) * a
109 + fx * (8 - fy) * b
110 + (8 - fx) * fy * c
111 + fx * fy * d
112 + 32)
113 >> 6;
114 }
115 }
116}