threers-codec 0.0.12

Pure-Rust media codecs: H.264/HEVC/VP9 encoders, H.264/HEVC/VP9/AV1 decoders, AAC/MP3/Opus/Vorbis/FLAC/WAV decoders, AAC-LC and MP3 encoders, MP4/WebM/Matroska/Ogg, GIF/APNG, captions, and VideoToolbox acceleration on macOS
Documentation
//! HEVC fractional sample interpolation and weighted sample prediction
//! (§8.5.3.3.3, §8.5.3.3.4). Predictions are kept at 14-bit precision until
//! the weighting step.

use super::tables::{CHROMA_FILTER, LUMA_FILTER};
use crate::decode::picture::Plane;

/// Reusable buffers, so interpolation allocates nothing per block.
#[derive(Default)]
pub struct Scratch {
    win: Vec<u16>,
    tmp: Vec<i32>,
}

/// A view of the `ww × wh` window at `(x0, y0)`: straight into the plane
/// when it lies inside, otherwise an edge-clamped copy in `win`.
fn view<'a>(
    p: &'a Plane,
    x0: isize,
    y0: isize,
    ww: usize,
    wh: usize,
    win: &'a mut Vec<u16>,
) -> (&'a [u16], usize, usize) {
    if x0 >= 0 && y0 >= 0 && x0 as usize + ww <= p.width && y0 as usize + wh <= p.height {
        return (&p.data, p.width, y0 as usize * p.width + x0 as usize);
    }
    win.clear();
    win.reserve(ww * wh);
    // Columns left of the picture repeat its first sample, right of it its
    // last; the run in between is one copy per row.
    let (pw, x0w) = (p.width as isize, x0 + ww as isize);
    let left = (-x0).clamp(0, ww as isize) as usize;
    let right = (x0w - pw).clamp(0, ww as isize - left as isize) as usize;
    let mid = ww - left - right;
    let mid_x = x0.max(0).min(pw - 1) as usize;
    for r in 0..wh {
        let y = (y0 + r as isize).clamp(0, p.height as isize - 1) as usize;
        let row = &p.data[y * p.width..(y + 1) * p.width];
        win.extend(std::iter::repeat_n(row[0], left));
        win.extend_from_slice(&row[mid_x..mid_x + mid]);
        win.extend(std::iter::repeat_n(row[p.width - 1], right));
    }
    (win, ww, 0)
}

#[allow(clippy::too_many_arguments)]
fn interp_n<const T: usize>(
    p: &Plane,
    x: isize,
    y: isize,
    fx: &[i32; T],
    fy: &[i32; T],
    xf: usize,
    yf: usize,
    w: usize,
    h: usize,
    bd: u32,
    out: &mut [i16],
    s: &mut Scratch,
) {
    let half = T / 2 - 1;
    let shift1 = (bd as i32 - 8).min(4);
    let shift3 = (14 - bd as i32).max(2);
    let (ww, wh) = (w + T - 1, h + T - 1);
    let (src, stride, base) = view(p, x - half as isize, y - half as isize, ww, wh, &mut s.win);
    match (xf, yf) {
        (0, 0) => {
            for r in 0..h {
                let row = &src[base + (r + half) * stride + half..][..w];
                for (o, &v) in out[r * w..r * w + w].iter_mut().zip(row) {
                    *o = ((v as i32) << shift3) as i16;
                }
            }
        }
        (_, 0) => {
            let mut acc = [0i32; 64];
            for r in 0..h {
                let row = &src[base + (r + half) * stride..][..ww];
                hfilter(fx, row, &mut acc[..w]);
                for (o, &a) in out[r * w..r * w + w].iter_mut().zip(acc.iter()) {
                    *o = (a >> shift1) as i16;
                }
            }
        }
        (0, _) => {
            for r in 0..h {
                let rows: [&[u16]; T] =
                    std::array::from_fn(|k| &src[base + (r + k) * stride + half..][..w]);
                vfilter(fy, &rows, &mut out[r * w..r * w + w], |a| {
                    (a >> shift1) as i16
                });
            }
        }
        _ => {
            // `hfilter` writes every entry, so the buffer only grows.
            if s.tmp.len() < wh * w {
                s.tmp.resize(wh * w, 0);
            }
            for r in 0..wh {
                let row = &src[base + r * stride..][..ww];
                let t = &mut s.tmp[r * w..r * w + w];
                hfilter(fx, row, t);
                for v in t.iter_mut() {
                    *v >>= shift1;
                }
            }
            for r in 0..h {
                let rows: [&[i32]; T] =
                    std::array::from_fn(|k| &s.tmp[(r + k) * w..(r + k) * w + w]);
                vfilter(fy, &rows, &mut out[r * w..r * w + w], |a| (a >> 6) as i16);
            }
        }
    }
}

/// `acc[c] = Σ f[k] · row[c + k]`, tap-major so each pass over `acc` is a
/// plain multiply-add the compiler can vectorise.
#[inline(always)]
fn hfilter<const T: usize>(f: &[i32; T], row: &[u16], acc: &mut [i32]) {
    let w = acc.len();
    // `T` shifted views of the row, all `w` long: the tap loop unrolls and
    // the column loop vectorises without bounds checks.
    let taps: [&[u16]; T] = std::array::from_fn(|k| &row[k..k + w]);
    for (c, a) in acc.iter_mut().enumerate() {
        let mut v = 0;
        for k in 0..T {
            v += f[k] * taps[k][c] as i32;
        }
        *a = v;
    }
}

/// `out[c] = fin(Σ f[k] · rows[k][c])`: the vertical pass, one output per
/// column in a single sweep.
#[inline(always)]
fn vfilter<const T: usize, V: Copy + Into<i32>>(
    f: &[i32; T],
    rows: &[&[V]; T],
    out: &mut [i16],
    fin: impl Fn(i32) -> i16,
) {
    for (c, o) in out.iter_mut().enumerate() {
        let mut v = 0;
        for k in 0..T {
            v += f[k] * rows[k][c].into();
        }
        *o = fin(v);
    }
}

/// Interpolate a `w × h` block whose full-sample position is `(x, y)` and
/// fraction `(xf, yf)` (quarter samples for luma, eighths for chroma).
#[allow(clippy::too_many_arguments)]
pub fn interp(
    p: &Plane,
    x: isize,
    y: isize,
    xf: usize,
    yf: usize,
    w: usize,
    h: usize,
    bd: u32,
    luma: bool,
    out: &mut [i16],
    s: &mut Scratch,
) {
    if luma {
        interp_n(
            p,
            x,
            y,
            &LUMA_FILTER[xf],
            &LUMA_FILTER[yf],
            xf,
            yf,
            w,
            h,
            bd,
            out,
            s,
        );
    } else {
        interp_n(
            p,
            x,
            y,
            &CHROMA_FILTER[xf],
            &CHROMA_FILTER[yf],
            xf,
            yf,
            w,
            h,
            bd,
            out,
            s,
        );
    }
}

/// How two (or one) 14-bit predictions become samples.
#[derive(Clone, Copy, Debug)]
pub enum Weighting {
    Default,
    /// `(w0, o0, w1, o1, log2_denom)`, offsets already at the bit depth.
    Explicit(i32, i32, i32, i32, u32),
}

/// Combine predictions and store the `w × h` block at `(x0, y0)` of `dst`.
#[allow(clippy::too_many_arguments)]
pub fn put(
    dst: &mut Plane,
    x0: usize,
    y0: usize,
    w: usize,
    h: usize,
    p0: Option<&[i16]>,
    p1: Option<&[i16]>,
    wt: Weighting,
    bd: u32,
) {
    let max = (1i32 << bd) - 1;
    let shift1 = 14 - bd as i32;
    // Clip to the picture: blocks along the right and bottom edges can
    // extend past it when the size is not a multiple of the minimum CU.
    let wc = w.min(dst.width.saturating_sub(x0));
    let hc = h.min(dst.height.saturating_sub(y0));
    let stride = dst.width;
    let rows = dst.data.chunks_exact_mut(stride).skip(y0).take(hc);
    match (p0, p1, wt) {
        (Some(a), Some(b), Weighting::Default) => {
            let shift2 = 15 - bd as i32;
            let off = 1 << (shift2 - 1);
            for (r, row) in rows.enumerate() {
                let (a, b) = (&a[r * w..][..wc], &b[r * w..][..wc]);
                for ((d, &p), &q) in row[x0..x0 + wc].iter_mut().zip(a).zip(b) {
                    *d = ((p as i32 + q as i32 + off) >> shift2).clamp(0, max) as u16;
                }
            }
        }
        (Some(a), Some(b), Weighting::Explicit(w0, o0, w1, o1, denom)) => {
            let log2wd = denom as i32 + shift1;
            let round = (o0 + o1 + 1) << log2wd;
            for (r, row) in rows.enumerate() {
                let (a, b) = (&a[r * w..][..wc], &b[r * w..][..wc]);
                for ((d, &p), &q) in row[x0..x0 + wc].iter_mut().zip(a).zip(b) {
                    let v = (p as i32 * w0 + q as i32 * w1 + round) >> (log2wd + 1);
                    *d = v.clamp(0, max) as u16;
                }
            }
        }
        (Some(a), None, wt) | (None, Some(a), wt) => {
            let explicit = match (wt, p0.is_some()) {
                (Weighting::Explicit(w0, o0, _, _, d), true) => Some((w0, o0, d)),
                (Weighting::Explicit(_, _, w1, o1, d), false) => Some((w1, o1, d)),
                _ => None,
            };
            match explicit {
                None => {
                    let off = 1 << (shift1 - 1);
                    for (r, row) in rows.enumerate() {
                        for (d, &p) in row[x0..x0 + wc].iter_mut().zip(&a[r * w..][..wc]) {
                            *d = ((p as i32 + off) >> shift1).clamp(0, max) as u16;
                        }
                    }
                }
                Some((wl, ol, d)) => {
                    let log2wd = d as i32 + shift1;
                    for (r, row) in rows.enumerate() {
                        for (d, &p) in row[x0..x0 + wc].iter_mut().zip(&a[r * w..][..wc]) {
                            let p = p as i32;
                            let v = if log2wd >= 1 {
                                ((p * wl + (1 << (log2wd - 1))) >> log2wd) + ol
                            } else {
                                p * wl + ol
                            };
                            *d = v.clamp(0, max) as u16;
                        }
                    }
                }
            }
        }
        _ => {}
    }
}