zvidlib-vp9-decoder 0.5.1

Pure-Rust VP9 software decoder for zvidlib
Documentation
//! The sample type of a frame buffer: `u8` for 8-bit (profile 0) streams and
//! `u16` for 10- and 12-bit (profile 2) ones.
//!
//! The frame buffers, the tile decoder and the loop filter are generic over
//! it. Each kernel is chosen here: 8-bit samples run the kernels that
//! dispatch to [`crate::vp9_simd`], and high-bit-depth samples the scalar
//! forms of libvpx's `vpx_highbd_*` functions, which are the 8-bit kernels
//! with the clipping range, the edge and DC defaults and the loop filter
//! thresholds scaled to the bit depth, and inverse transforms of their own
//! (`highbd_idct1d`).

use std::fmt::Debug;

use super::loopfilter::{Pixels, Thresholds};
use super::recon::{self, IntraEdges, Kernel};
use crate::vp9_simd::{self, loopfilter::Taps};

/// A loop filter's length, as [`Pixel::filter_edge_vector`] takes it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FilterTaps {
    Four,
    Eight,
    Sixteen,
}

/// One sample of a frame buffer.
pub trait Pixel: Copy + Default + Debug + PartialEq + Send + Sync + 'static {
    /// The sample with value `value`, which is within the bit depth's range.
    fn from_i32(value: i32) -> Self;
    fn to_i32(self) -> i32;
    /// Appends `samples` as libvpx writes them out: one byte each at 8 bits,
    /// two little-endian bytes each above.
    fn extend_bytes(samples: &[Self], out: &mut Vec<u8>);

    /// [`recon::inverse_transform_add`] at `bit_depth`.
    #[allow(clippy::too_many_arguments)]
    fn inverse_transform_add(
        coefficients: &[i32],
        dest: &mut [Self],
        stride: usize,
        tx_size: u8,
        tx_type: u8,
        eob: usize,
        lossless: bool,
        bit_depth: u8,
    );

    /// [`recon::predict_intra`] at `bit_depth`.
    #[allow(clippy::too_many_arguments)]
    fn predict_intra(
        dest: &mut [Self],
        stride: usize,
        bs: usize,
        mode: u8,
        edges: &IntraEdges<Self>,
        have_left: bool,
        have_above: bool,
        bit_depth: u8,
    );

    /// [`recon::convolve`] at `bit_depth`.
    #[allow(clippy::too_many_arguments)]
    fn convolve(
        src: &[Self],
        origin: usize,
        src_stride: usize,
        dest: &mut [Self],
        stride: usize,
        w: usize,
        h: usize,
        kernel: &Kernel,
        x_frac: i32,
        x_step: i32,
        y_frac: i32,
        y_step: i32,
        average: bool,
        temp: &mut [Self; 64 * 135],
        bit_depth: u8,
    );

    /// Filters one edge with a vector kernel, returning `false` when there
    /// is none for this sample type or host and the scalar filter must run.
    #[allow(clippy::too_many_arguments)]
    fn filter_edge_vector(
        pixels: &mut Pixels<'_, Self>,
        start: isize,
        step: isize,
        along: isize,
        count: usize,
        t: Thresholds,
        taps: FilterTaps,
    ) -> bool;
}

impl Pixel for u8 {
    #[inline]
    fn from_i32(value: i32) -> Self {
        value as u8
    }

    #[inline]
    fn to_i32(self) -> i32 {
        i32::from(self)
    }

    fn extend_bytes(samples: &[Self], out: &mut Vec<u8>) {
        out.extend_from_slice(samples);
    }

    #[inline]
    fn inverse_transform_add(
        coefficients: &[i32],
        dest: &mut [Self],
        stride: usize,
        tx_size: u8,
        tx_type: u8,
        eob: usize,
        lossless: bool,
        _bit_depth: u8,
    ) {
        recon::inverse_transform_add(coefficients, dest, stride, tx_size, tx_type, eob, lossless);
    }

    #[inline]
    fn predict_intra(
        dest: &mut [Self],
        stride: usize,
        bs: usize,
        mode: u8,
        edges: &IntraEdges<Self>,
        have_left: bool,
        have_above: bool,
        _bit_depth: u8,
    ) {
        recon::predict_intra(dest, stride, bs, mode, edges, have_left, have_above);
    }

    #[inline]
    fn convolve(
        src: &[Self],
        origin: usize,
        src_stride: usize,
        dest: &mut [Self],
        stride: usize,
        w: usize,
        h: usize,
        kernel: &Kernel,
        x_frac: i32,
        x_step: i32,
        y_frac: i32,
        y_step: i32,
        average: bool,
        temp: &mut [Self; 64 * 135],
        _bit_depth: u8,
    ) {
        recon::convolve(
            src, origin, src_stride, dest, stride, w, h, kernel, x_frac, x_step, y_frac, y_step,
            average, temp,
        );
    }

    #[inline]
    fn filter_edge_vector(
        pixels: &mut Pixels<'_, Self>,
        start: isize,
        step: isize,
        along: isize,
        count: usize,
        t: Thresholds,
        taps: FilterTaps,
    ) -> bool {
        let taps = match taps {
            FilterTaps::Four => Taps::Four,
            FilterTaps::Eight => Taps::Eight,
            FilterTaps::Sixteen => Taps::Sixteen,
        };
        vp9_simd::filter_edge(pixels.isa, pixels.data, start, step, along, count, t, taps)
    }
}

impl Pixel for u16 {
    #[inline]
    fn from_i32(value: i32) -> Self {
        value as u16
    }

    #[inline]
    fn to_i32(self) -> i32 {
        i32::from(self)
    }

    fn extend_bytes(samples: &[Self], out: &mut Vec<u8>) {
        out.reserve(samples.len() * 2);
        for sample in samples {
            out.extend_from_slice(&sample.to_le_bytes());
        }
    }

    #[inline]
    fn inverse_transform_add(
        coefficients: &[i32],
        dest: &mut [Self],
        stride: usize,
        tx_size: u8,
        tx_type: u8,
        eob: usize,
        lossless: bool,
        bit_depth: u8,
    ) {
        recon::inverse_transform_add_high(
            coefficients,
            dest,
            stride,
            tx_size,
            tx_type,
            eob,
            lossless,
            bit_depth,
        );
    }

    #[inline]
    fn predict_intra(
        dest: &mut [Self],
        stride: usize,
        bs: usize,
        mode: u8,
        edges: &IntraEdges<Self>,
        have_left: bool,
        have_above: bool,
        bit_depth: u8,
    ) {
        recon::predict_intra_generic(
            dest, stride, bs, mode, edges, have_left, have_above, bit_depth,
        );
    }

    #[inline]
    fn convolve(
        src: &[Self],
        origin: usize,
        src_stride: usize,
        dest: &mut [Self],
        stride: usize,
        w: usize,
        h: usize,
        kernel: &Kernel,
        x_frac: i32,
        x_step: i32,
        y_frac: i32,
        y_step: i32,
        average: bool,
        temp: &mut [Self; 64 * 135],
        bit_depth: u8,
    ) {
        recon::convolve_generic(
            src, origin, src_stride, dest, stride, w, h, kernel, x_frac, x_step, y_frac, y_step,
            average, temp, bit_depth,
        );
    }

    #[inline]
    fn filter_edge_vector(
        _pixels: &mut Pixels<'_, Self>,
        _start: isize,
        _step: isize,
        _along: isize,
        _count: usize,
        _t: Thresholds,
        _taps: FilterTaps,
    ) -> bool {
        false
    }
}