use std::borrow::Cow;
use std::ffi::CStr;
use std::mem::transmute;
use std::ptr;
pub use crate::ffi::vpx_bit_depth::*;
pub use crate::ffi::vpx_codec_err_t::*;
pub use crate::ffi::vpx_rational;
pub use crate::ffi::vpx_rc_mode::*;
use vpx_sys as ffi;
pub mod encoder;
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum Error {
Generic(u32),
Mem,
AbiMismatch,
Incapable,
UnsupportedBitstream,
UnsupportedFrame,
CorruptFrame,
InvalidParam,
ListEnd,
}
use crate::ffi::vpx_codec_err_t as ErrorEnum;
impl From<ErrorEnum> for Error {
fn from(v: ErrorEnum) -> Self {
match v {
ffi::VPX_CODEC_MEM_ERROR => Self::Mem,
ffi::VPX_CODEC_ABI_MISMATCH => Self::AbiMismatch,
ffi::VPX_CODEC_INCAPABLE => Self::Incapable,
ffi::VPX_CODEC_UNSUP_BITSTREAM => Self::UnsupportedBitstream,
ffi::VPX_CODEC_UNSUP_FEATURE => Self::UnsupportedFrame,
ffi::VPX_CODEC_CORRUPT_FRAME => Self::CorruptFrame,
ffi::VPX_CODEC_INVALID_PARAM => Self::InvalidParam,
ffi::VPX_CODEC_LIST_END => Self::ListEnd,
n => Self::Generic(n as u32),
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<Self as std::fmt::Debug>::fmt(self, fmt)
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Self::Generic(_) => "Unspecified error",
Self::Mem => "Memory operation failed",
Self::AbiMismatch => "ABI version mismatch",
Self::Incapable => "Algorithm does not have required capability",
Self::UnsupportedBitstream => "The given bitstream is not supported",
Self::UnsupportedFrame => "Encoded bitstream uses an unsupported feature",
Self::CorruptFrame => "The coded data for this stream is corrupt or incomplete",
Self::InvalidParam => "An application-supplied parameter is not valid",
Self::ListEnd => "An iterator reached the end of list",
}
}
}
pub type Rect = ffi::vpx_image_rect_t;
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
#[allow(non_camel_case_types)]
pub enum Format {
YV12_VPX,
I420_VPX,
YV12,
I420 { hi_bit_depth: bool },
I422 { hi_bit_depth: bool },
I440 { hi_bit_depth: bool },
I444 { hi_bit_depth: bool },
}
impl From<Format> for ffi::vpx_img_fmt_t {
fn from(val: Format) -> Self {
use crate::Format::{I420, I420_VPX, I422, I440, I444, YV12, YV12_VPX};
use crate::ffi::vpx_img_fmt::*;
match val {
I420_VPX => VPX_IMG_FMT_I420,
YV12 | YV12_VPX => VPX_IMG_FMT_YV12,
I420 { hi_bit_depth: false } => VPX_IMG_FMT_I420,
I422 { hi_bit_depth: false } => VPX_IMG_FMT_I422,
I440 { hi_bit_depth: false } => VPX_IMG_FMT_I440,
I444 { hi_bit_depth: false } => VPX_IMG_FMT_I444,
I420 { hi_bit_depth: true } => VPX_IMG_FMT_I42016,
I422 { hi_bit_depth: true } => VPX_IMG_FMT_I42216,
I440 { hi_bit_depth: true } => VPX_IMG_FMT_I44016,
I444 { hi_bit_depth: true } => VPX_IMG_FMT_I44416,
}
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
#[allow(non_camel_case_types)]
pub enum ColorSpace {
BT601,
BT709,
SMPTE170,
SMPTE240,
BT2020,
SRGB,
}
impl From<ColorSpace> for ffi::vpx_color_space_t {
fn from(val: ColorSpace) -> Self {
match val {
ColorSpace::BT601 => Self::VPX_CS_BT_601,
ColorSpace::BT709 => Self::VPX_CS_BT_709,
ColorSpace::SMPTE170 => Self::VPX_CS_SMPTE_170,
ColorSpace::SMPTE240 => Self::VPX_CS_SMPTE_240,
ColorSpace::BT2020 => Self::VPX_CS_BT_2020,
ColorSpace::SRGB => Self::VPX_CS_SRGB,
}
}
}
pub struct Image<'a>(ffi::vpx_image_t, Format, #[allow(unused)] Cow<'a, [u8]>);
impl<'a> Image<'a> {
#[must_use]
pub fn new(data: Cow<'a, [u8]>, fmt: Format,
color_space: ColorSpace,
width: u32, height: u32,
stride: u32) -> Self
{
Self::try_new(data, fmt, color_space, width, height, stride).expect("Image::new")
}
#[must_use]
pub fn try_new(data: Cow<'a, [u8]>, fmt: Format,
color_space: ColorSpace,
width: u32, height: u32,
stride: u32) -> Result<Self, Error>
{
let mut t = ffi::vpx_image_t::default();
let wrapped = unsafe {
ffi::vpx_img_wrap(ptr::from_mut(&mut t),
fmt.into(), width,
height, stride,
data.as_ptr().cast_mut())
};
if wrapped.is_null() {
return Err(Error::Generic(0));
}
let uv_planes = if t.fmt == ffi::vpx_img_fmt::VPX_IMG_FMT_NV12 { 1 } else { 2 };
let required = (u64::from(t.h) * t.stride[ffi::VPX_PLANE_Y as usize] as u64
+ uv_planes * (u64::from(t.h) >> t.y_chroma_shift) * t.stride[ffi::VPX_PLANE_U as usize] as u64) as usize;
if data.len() < required {
debug_assert!(false, "buffer too small for {fmt:?} {width}x{height}: needs {required} bytes, got {}", data.len());
return Err(Error::InvalidParam);
}
t.cs = color_space.into();
Ok(Image(t, fmt, data))
}
#[must_use]
pub fn get_format(&self) -> Format { self.1 }
pub fn set_rect(&mut self, rect: Rect) -> Result<(), ()> {
let res = unsafe { ffi::vpx_img_set_rect(ptr::from_mut(&mut self.0), rect.x, rect.y, rect.w, rect.h) };
if res == 0 {
Ok(())
} else {
Err(())
}
}
pub fn flip(&mut self) {
unsafe {
ffi::vpx_img_flip(ptr::from_mut(&mut self.0));
}
}
}
impl Drop for Image<'_> {
fn drop(&mut self) {
unsafe { ffi::vpx_img_free(ptr::from_mut(&mut self.0)) }
}
}
#[derive(Debug, Clone)]
pub struct Frame<'a> {
data: &'a [u8],
pub pts: u64,
pub duration: u64,
pub flags: ffi::vpx_codec_frame_flags_t,
pub partition_id: i32,
}
pub const FRAME_IS_KEY: u32 = 0x1;
pub const FRAME_IS_DROPPABLE: u32 = 0x2;
pub const FRAME_IS_INVISIBLE: u32 = 0x4;
pub const FRAME_IS_FRAGMENT: u32 = 0x8;
impl<'a> Frame<'a> {
#[must_use]
pub fn data(&self) -> &'a [u8] { self.data }
#[must_use]
pub fn is_keyframe(&self) -> bool {
self.flags & FRAME_IS_KEY != 0
}
#[must_use]
pub fn is_droppable(&self) -> bool {
self.flags & FRAME_IS_DROPPABLE != 0
}
#[must_use]
pub fn is_invisible(&self) -> bool {
self.flags & FRAME_IS_INVISIBLE != 0
}
#[must_use]
pub fn is_fragment(&self) -> bool {
self.flags & FRAME_IS_FRAGMENT != 0
}
}
impl<'a> From<&'a ffi::vpx_codec_cx_pkt__bindgen_ty_1__bindgen_ty_1> for Frame<'a> {
fn from(v: &'a ffi::vpx_codec_cx_pkt__bindgen_ty_1__bindgen_ty_1) -> Self {
let data: &'a [u8] = unsafe { std::slice::from_raw_parts(v.buf as *const u8, v.sz) };
Frame {
data,
pts: v.pts as u64,
duration: v.duration,
flags: v.flags,
partition_id: v.partition_id,
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Kind {
Decoder,
Encoder,
}
pub trait Interface: InternalInterface + Default {
type Context;
type Cfg;
fn name(&self) -> &'static str {
let pname = unsafe { ffi::vpx_codec_iface_name(self.iface()) };
let str = unsafe { CStr::from_ptr(pname).to_str().unwrap() };
unsafe { transmute(str) }
}
fn kind(&self) -> Kind;
fn create(
&self, cfg: <Self as Interface>::Cfg, flags: ffi::vpx_codec_flags_t,
) -> Result<<Self as Interface>::Context, Error>;
}
#[doc(hidden)]
pub trait InternalInterface {
fn iface(&self) -> *mut ffi::vpx_codec_iface_t;
}