Skip to main content

threers_codec/decode/hevc/
inter.rs

1//! HEVC fractional sample interpolation and weighted sample prediction
2//! (§8.5.3.3.3, §8.5.3.3.4). Predictions are kept at 14-bit precision until
3//! the weighting step.
4
5use super::tables::{CHROMA_FILTER, LUMA_FILTER};
6use crate::decode::picture::Plane;
7
8/// Reusable buffers, so interpolation allocates nothing per block.
9#[derive(Default)]
10pub struct Scratch {
11    win: Vec<u16>,
12    tmp: Vec<i32>,
13}
14
15/// A view of the `ww × wh` window at `(x0, y0)`: straight into the plane
16/// when it lies inside, otherwise an edge-clamped copy in `win`.
17fn view<'a>(
18    p: &'a Plane,
19    x0: isize,
20    y0: isize,
21    ww: usize,
22    wh: usize,
23    win: &'a mut Vec<u16>,
24) -> (&'a [u16], usize, usize) {
25    if x0 >= 0 && y0 >= 0 && x0 as usize + ww <= p.width && y0 as usize + wh <= p.height {
26        return (&p.data, p.width, y0 as usize * p.width + x0 as usize);
27    }
28    win.clear();
29    win.reserve(ww * wh);
30    // Columns left of the picture repeat its first sample, right of it its
31    // last; the run in between is one copy per row.
32    let (pw, x0w) = (p.width as isize, x0 + ww as isize);
33    let left = (-x0).clamp(0, ww as isize) as usize;
34    let right = (x0w - pw).clamp(0, ww as isize - left as isize) as usize;
35    let mid = ww - left - right;
36    let mid_x = x0.max(0).min(pw - 1) as usize;
37    for r in 0..wh {
38        let y = (y0 + r as isize).clamp(0, p.height as isize - 1) as usize;
39        let row = &p.data[y * p.width..(y + 1) * p.width];
40        win.extend(std::iter::repeat_n(row[0], left));
41        win.extend_from_slice(&row[mid_x..mid_x + mid]);
42        win.extend(std::iter::repeat_n(row[p.width - 1], right));
43    }
44    (win, ww, 0)
45}
46
47#[allow(clippy::too_many_arguments)]
48fn interp_n<const T: usize>(
49    p: &Plane,
50    x: isize,
51    y: isize,
52    fx: &[i32; T],
53    fy: &[i32; T],
54    xf: usize,
55    yf: usize,
56    w: usize,
57    h: usize,
58    bd: u32,
59    out: &mut [i16],
60    s: &mut Scratch,
61) {
62    let half = T / 2 - 1;
63    let shift1 = (bd as i32 - 8).min(4);
64    let shift3 = (14 - bd as i32).max(2);
65    let (ww, wh) = (w + T - 1, h + T - 1);
66    let (src, stride, base) = view(p, x - half as isize, y - half as isize, ww, wh, &mut s.win);
67    match (xf, yf) {
68        (0, 0) => {
69            for r in 0..h {
70                let row = &src[base + (r + half) * stride + half..][..w];
71                for (o, &v) in out[r * w..r * w + w].iter_mut().zip(row) {
72                    *o = ((v as i32) << shift3) as i16;
73                }
74            }
75        }
76        (_, 0) => {
77            let mut acc = [0i32; 64];
78            for r in 0..h {
79                let row = &src[base + (r + half) * stride..][..ww];
80                hfilter(fx, row, &mut acc[..w]);
81                for (o, &a) in out[r * w..r * w + w].iter_mut().zip(acc.iter()) {
82                    *o = (a >> shift1) as i16;
83                }
84            }
85        }
86        (0, _) => {
87            for r in 0..h {
88                let rows: [&[u16]; T] =
89                    std::array::from_fn(|k| &src[base + (r + k) * stride + half..][..w]);
90                vfilter(fy, &rows, &mut out[r * w..r * w + w], |a| {
91                    (a >> shift1) as i16
92                });
93            }
94        }
95        _ => {
96            // `hfilter` writes every entry, so the buffer only grows.
97            if s.tmp.len() < wh * w {
98                s.tmp.resize(wh * w, 0);
99            }
100            for r in 0..wh {
101                let row = &src[base + r * stride..][..ww];
102                let t = &mut s.tmp[r * w..r * w + w];
103                hfilter(fx, row, t);
104                for v in t.iter_mut() {
105                    *v >>= shift1;
106                }
107            }
108            for r in 0..h {
109                let rows: [&[i32]; T] =
110                    std::array::from_fn(|k| &s.tmp[(r + k) * w..(r + k) * w + w]);
111                vfilter(fy, &rows, &mut out[r * w..r * w + w], |a| (a >> 6) as i16);
112            }
113        }
114    }
115}
116
117/// `acc[c] = Σ f[k] · row[c + k]`, tap-major so each pass over `acc` is a
118/// plain multiply-add the compiler can vectorise.
119#[inline(always)]
120fn hfilter<const T: usize>(f: &[i32; T], row: &[u16], acc: &mut [i32]) {
121    let w = acc.len();
122    // `T` shifted views of the row, all `w` long: the tap loop unrolls and
123    // the column loop vectorises without bounds checks.
124    let taps: [&[u16]; T] = std::array::from_fn(|k| &row[k..k + w]);
125    for (c, a) in acc.iter_mut().enumerate() {
126        let mut v = 0;
127        for k in 0..T {
128            v += f[k] * taps[k][c] as i32;
129        }
130        *a = v;
131    }
132}
133
134/// `out[c] = fin(Σ f[k] · rows[k][c])`: the vertical pass, one output per
135/// column in a single sweep.
136#[inline(always)]
137fn vfilter<const T: usize, V: Copy + Into<i32>>(
138    f: &[i32; T],
139    rows: &[&[V]; T],
140    out: &mut [i16],
141    fin: impl Fn(i32) -> i16,
142) {
143    for (c, o) in out.iter_mut().enumerate() {
144        let mut v = 0;
145        for k in 0..T {
146            v += f[k] * rows[k][c].into();
147        }
148        *o = fin(v);
149    }
150}
151
152/// Interpolate a `w × h` block whose full-sample position is `(x, y)` and
153/// fraction `(xf, yf)` (quarter samples for luma, eighths for chroma).
154#[allow(clippy::too_many_arguments)]
155pub fn interp(
156    p: &Plane,
157    x: isize,
158    y: isize,
159    xf: usize,
160    yf: usize,
161    w: usize,
162    h: usize,
163    bd: u32,
164    luma: bool,
165    out: &mut [i16],
166    s: &mut Scratch,
167) {
168    if luma {
169        interp_n(
170            p,
171            x,
172            y,
173            &LUMA_FILTER[xf],
174            &LUMA_FILTER[yf],
175            xf,
176            yf,
177            w,
178            h,
179            bd,
180            out,
181            s,
182        );
183    } else {
184        interp_n(
185            p,
186            x,
187            y,
188            &CHROMA_FILTER[xf],
189            &CHROMA_FILTER[yf],
190            xf,
191            yf,
192            w,
193            h,
194            bd,
195            out,
196            s,
197        );
198    }
199}
200
201/// How two (or one) 14-bit predictions become samples.
202#[derive(Clone, Copy, Debug)]
203pub enum Weighting {
204    Default,
205    /// `(w0, o0, w1, o1, log2_denom)`, offsets already at the bit depth.
206    Explicit(i32, i32, i32, i32, u32),
207}
208
209/// Combine predictions and store the `w × h` block at `(x0, y0)` of `dst`.
210#[allow(clippy::too_many_arguments)]
211pub fn put(
212    dst: &mut Plane,
213    x0: usize,
214    y0: usize,
215    w: usize,
216    h: usize,
217    p0: Option<&[i16]>,
218    p1: Option<&[i16]>,
219    wt: Weighting,
220    bd: u32,
221) {
222    let max = (1i32 << bd) - 1;
223    let shift1 = 14 - bd as i32;
224    // Clip to the picture: blocks along the right and bottom edges can
225    // extend past it when the size is not a multiple of the minimum CU.
226    let wc = w.min(dst.width.saturating_sub(x0));
227    let hc = h.min(dst.height.saturating_sub(y0));
228    let stride = dst.width;
229    let rows = dst.data.chunks_exact_mut(stride).skip(y0).take(hc);
230    match (p0, p1, wt) {
231        (Some(a), Some(b), Weighting::Default) => {
232            let shift2 = 15 - bd as i32;
233            let off = 1 << (shift2 - 1);
234            for (r, row) in rows.enumerate() {
235                let (a, b) = (&a[r * w..][..wc], &b[r * w..][..wc]);
236                for ((d, &p), &q) in row[x0..x0 + wc].iter_mut().zip(a).zip(b) {
237                    *d = ((p as i32 + q as i32 + off) >> shift2).clamp(0, max) as u16;
238                }
239            }
240        }
241        (Some(a), Some(b), Weighting::Explicit(w0, o0, w1, o1, denom)) => {
242            let log2wd = denom as i32 + shift1;
243            let round = (o0 + o1 + 1) << log2wd;
244            for (r, row) in rows.enumerate() {
245                let (a, b) = (&a[r * w..][..wc], &b[r * w..][..wc]);
246                for ((d, &p), &q) in row[x0..x0 + wc].iter_mut().zip(a).zip(b) {
247                    let v = (p as i32 * w0 + q as i32 * w1 + round) >> (log2wd + 1);
248                    *d = v.clamp(0, max) as u16;
249                }
250            }
251        }
252        (Some(a), None, wt) | (None, Some(a), wt) => {
253            let explicit = match (wt, p0.is_some()) {
254                (Weighting::Explicit(w0, o0, _, _, d), true) => Some((w0, o0, d)),
255                (Weighting::Explicit(_, _, w1, o1, d), false) => Some((w1, o1, d)),
256                _ => None,
257            };
258            match explicit {
259                None => {
260                    let off = 1 << (shift1 - 1);
261                    for (r, row) in rows.enumerate() {
262                        for (d, &p) in row[x0..x0 + wc].iter_mut().zip(&a[r * w..][..wc]) {
263                            *d = ((p as i32 + off) >> shift1).clamp(0, max) as u16;
264                        }
265                    }
266                }
267                Some((wl, ol, d)) => {
268                    let log2wd = d as i32 + shift1;
269                    for (r, row) in rows.enumerate() {
270                        for (d, &p) in row[x0..x0 + wc].iter_mut().zip(&a[r * w..][..wc]) {
271                            let p = p as i32;
272                            let v = if log2wd >= 1 {
273                                ((p * wl + (1 << (log2wd - 1))) >> log2wd) + ol
274                            } else {
275                                p * wl + ol
276                            };
277                            *d = v.clamp(0, max) as u16;
278                        }
279                    }
280                }
281            }
282        }
283        _ => {}
284    }
285}