libvpx-native 0.4.5

Rust idiomatic wrapper to libvpx
Documentation
use crate::{Error, InternalInterface, Kind};
use std::ops::{Deref, DerefMut};
use vpx_sys as ffi;

#[derive(Debug)]
pub struct Cfg(ffi::vpx_codec_enc_cfg_t);
impl Default for Cfg {
    fn default() -> Self {
        let interface = Interface;
        let mut cfg = ffi::vpx_codec_enc_cfg_t::default();
        let err = unsafe { ffi::vpx_codec_enc_config_default(interface.iface(), std::ptr::from_mut(&mut cfg), 0) };
        assert_eq!(err, ffi::VPX_CODEC_OK);
        Self(cfg)
    }
}
impl AsRef<ffi::vpx_codec_enc_cfg_t> for Cfg {
    fn as_ref(&self) -> &ffi::vpx_codec_enc_cfg_t {
        &self.0
    }
}
impl AsMut<ffi::vpx_codec_enc_cfg_t> for Cfg {
    fn as_mut(&mut self) -> &mut ffi::vpx_codec_enc_cfg_t {
        &mut self.0
    }
}
impl Deref for Cfg {
    type Target = ffi::vpx_codec_enc_cfg_t;

    fn deref(&self) -> &ffi::vpx_codec_enc_cfg_t {
        &self.0
    }
}
impl DerefMut for Cfg {
    fn deref_mut(&mut self) -> &mut ffi::vpx_codec_enc_cfg_t {
        &mut self.0
    }
}

#[derive(Copy, Clone)]
pub struct Interface;
impl Default for Interface {
    fn default() -> Self {
        Self
    }
}
impl crate::Interface for Interface {
    type Context = Context;
    type Cfg = Cfg;
    fn kind(&self) -> Kind { Kind::Encoder }

    fn create(&self, cfg: <Self as crate::Interface>::Cfg,
              flags: ffi::vpx_codec_flags_t) ->
        Result<<Self as crate::Interface>::Context, Error>
    {
        let mut ctx = ffi::vpx_codec_ctx_t::default();
        let err = unsafe {
            ffi::vpx_codec_enc_init_ver(std::ptr::from_mut(&mut ctx),
                                        self.iface(),
                                        std::ptr::from_ref(&cfg.0),
                                        flags,
                                        ffi::VPX_ENCODER_ABI_VERSION as i32)
        };
        if err == ffi::VPX_CODEC_OK {
            Ok(Context(ctx))
        } else {
            Err(From::from(err))
        }
    }
}
impl InternalInterface for Interface {
    fn iface(&self) -> *mut ffi::vpx_codec_iface_t {
        &raw mut ffi::vpx_codec_vp9_cx_algo
    }
}

pub struct Context(ffi::vpx_codec_ctx_t);
unsafe impl Send for Context {}
impl Drop for Context {
    fn drop(&mut self) {
        unsafe {
            ffi::vpx_codec_destroy(std::ptr::from_mut(&mut self.0));
        }
    }
}
impl super::InternalEncoder for Context {
    fn get_ref_ctx(&self) -> *const ffi::vpx_codec_ctx_t {
        std::ptr::from_ref(&self.0)
    }

    fn get_mut_ctx(&mut self) -> *mut ffi::vpx_codec_ctx_t {
        std::ptr::from_mut(&mut self.0)
    }
}
impl super::Encoder for Context {
    type Cfg = Cfg;
}