libvpx-native 0.4.5

Rust idiomatic wrapper to libvpx
Documentation
use super::{Error, Frame, Image};
use std::io;
use vpx_sys as ffi;

pub mod vp9;

pub const DL_REALTIME: u64 = 1;
pub const DL_GOOD_QUALITY: u64 = 1_000_000;
pub const DL_BEST_QUALITY: u64 = 0;

#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Default)]
pub struct FrameFlags {
    keyframe: bool,
}

#[doc(hidden)]
impl From<FrameFlags> for ffi::vpx_enc_frame_flags_t {
    fn from(val: FrameFlags) -> Self {
        if val.keyframe { 1 } else { 0 }
    }
}

impl FrameFlags {
    #[must_use]
    pub fn new() -> Self { Self::default() }

    #[must_use]
    pub fn keyframe(mut self, keyframe: bool) -> Self {
        self.keyframe = keyframe;
        self
    }
}

pub trait Encoder: InternalEncoder
where <Self as Encoder>::Cfg: AsRef<ffi::vpx_codec_enc_cfg_t>
{
    type Cfg;
    fn set_cfg(&mut self, cfg: Self::Cfg) -> Result<(), Error> {
        let res = unsafe {
            ffi::vpx_codec_enc_config_set(self.get_mut_ctx(),
                                          std::ptr::from_ref(cfg.as_ref()))
        };
        if res == ffi::VPX_CODEC_OK {
            Ok(())
        } else {
            Err(Error::from(res))
        }
    }

    /// `duration` must be non-zero.
    fn encode(&mut self, image: &Image<'_>,
              pts: ffi::vpx_codec_pts_t,
              duration: u64,
              flags: FrameFlags,
              deadline: u64) -> Result<(), Error> {
        let res = unsafe {
            ffi::vpx_codec_encode(self.get_mut_ctx(),
                                  std::ptr::from_ref(&image.0),
                                  pts,
                                  duration as _,
                                  flags.into(),
                                  deadline as _)
        };
        if res == ffi::VPX_CODEC_OK {
            Ok(())
        } else {
            Err(Error::from(res))
        }
    }

    /// Call once there are no more frames to encode.
    fn flush(&mut self,
             pts: ffi::vpx_codec_pts_t,
             duration: u64,
             flags: ffi::vpx_enc_frame_flags_t,
             deadline: u64) -> Result<(), Error>
    {
        let res = unsafe {
            ffi::vpx_codec_encode(self.get_mut_ctx(),
                                  std::ptr::null(),
                                  pts, duration as _,
                                  flags, deadline as _)
        };
        if res == ffi::VPX_CODEC_OK {
            Ok(())
        } else {
            Err(Error::from(res))
        }
    }

    fn packets<T: PacketWriter>(&mut self, dest: &mut T) -> Result<(), io::Error> {
        use std::slice::from_raw_parts;
        let mut iter: ffi::vpx_codec_iter_t = std::ptr::null_mut();
        unsafe {
            loop {
                let pkt = ffi::vpx_codec_get_cx_data(self.get_mut_ctx(), std::ptr::from_mut(&mut iter));
                if pkt.is_null() {
                    return Ok(());
                }

                let pkt: &ffi::vpx_codec_cx_pkt_t = &*pkt;
                match pkt.kind {
                    ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_CX_FRAME_PKT => {
                        let frame = Frame::from(&pkt.data.frame);
                        dest.write_frame(&frame)?;
                    },
                    ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_STATS_PKT | ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_FPMB_STATS_PKT => {
                        let buf = &pkt.data.twopass_stats;
                        let buf = from_raw_parts(buf.buf as *const u8, buf.sz as usize);
                        dest.write_two_pass_stats(buf)?;
                    },
                    ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_PSNR_PKT => {
                        let psnr = &pkt.data.psnr;
                        dest.write_psnr(&psnr.samples, &psnr.sse, &psnr.psnr)?;
                    },
                    kind @ ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_CUSTOM_PKT => {
                        dest.write_custom(kind, &pkt.data)?;
                    },
                }
            }
        }
    }
}

#[doc(hidden)]
pub trait InternalEncoder {
    fn get_ref_ctx(&self) -> *const ffi::vpx_codec_ctx_t;
    fn get_mut_ctx(&mut self) -> *mut ffi::vpx_codec_ctx_t;
}

pub trait PacketWriter {
    fn write_frame(&mut self, _frame: &Frame<'_>) -> Result<(), io::Error> { Ok(()) }
    fn write_two_pass_stats(&mut self, _stats: &[u8]) -> Result<(), io::Error> { Ok(()) }
    fn write_first_pass_mb_stats(&mut self, _stats: &[u8]) -> Result<(), io::Error> { Ok(()) }
    fn write_psnr(&mut self, _samples: &[u32; 4], _sse: &[u64; 4],
                  _psnr: &[f64; 4]) -> Result<(), io::Error> { Ok(()) }
    fn write_custom(&mut self,
                    _kind: ffi::vpx_codec_cx_pkt_kind,
                    _data: &ffi::vpx_codec_cx_pkt__bindgen_ty_1) -> Result<(), io::Error> { Ok(()) }
}