#![forbid(unsafe_code)]
mod encoder;
mod rate;
mod scale;
mod tokens;
mod transform;
use std::fmt;
pub use scale::scale_i420;
pub use vp8_decoder::Picture;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TemporalLayers {
#[default]
L1T1,
L1T2,
L1T3,
}
impl TemporalLayers {
pub fn count(self) -> u8 {
match self {
TemporalLayers::L1T1 => 1,
TemporalLayers::L1T2 => 2,
TemporalLayers::L1T3 => 3,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EncoderConfig {
pub width: u32,
pub height: u32,
pub fps: u32,
pub bitrate_bps: u32,
pub max_bitrate_bps: u32,
pub keyframe_interval: Option<u32>,
pub temporal_layers: TemporalLayers,
}
impl EncoderConfig {
pub fn new(width: u32, height: u32, fps: u32, bitrate_bps: u32) -> Self {
EncoderConfig {
width,
height,
fps,
bitrate_bps,
max_bitrate_bps: bitrate_bps.saturating_add(bitrate_bps / 2),
keyframe_interval: None,
temporal_layers: TemporalLayers::L1T1,
}
}
pub fn with_temporal_layers(mut self, layers: TemporalLayers) -> Self {
self.temporal_layers = layers;
self
}
pub fn with_keyframe_interval(mut self, frames: Option<u32>) -> Self {
self.keyframe_interval = frames;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EncodedFrame {
pub data: Vec<u8>,
pub keyframe: bool,
pub temporal_id: u8,
pub layer_sync: bool,
pub q_index: u8,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum EncodeError {
InvalidConfig(&'static str),
SizeMismatch {
expected: (u32, u32),
got: (u32, u32),
},
InvalidPicture,
Internal(String),
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EncodeError::InvalidConfig(why) => write!(f, "invalid encoder config: {why}"),
EncodeError::SizeMismatch { expected, got } => {
write!(f, "picture is {}x{}, the encoder is {}x{}", got.0, got.1, expected.0, expected.1)
}
EncodeError::InvalidPicture => f.write_str("picture planes are too short for its size"),
EncodeError::Internal(why) => write!(f, "internal encoder error: {why}"),
}
}
}
impl std::error::Error for EncodeError {}
pub struct Encoder {
core: encoder::Core,
}
impl Encoder {
pub fn new(config: EncoderConfig) -> Result<Self, EncodeError> {
Ok(Encoder {
core: encoder::Core::new(config)?,
})
}
pub fn encode(&mut self, picture: &Picture<'_>, force_keyframe: bool) -> Result<Option<EncodedFrame>, EncodeError> {
self.core.encode(picture, force_keyframe)
}
pub fn request_keyframe(&mut self) {
self.core.request_keyframe();
}
pub fn set_rates(&mut self, bitrate_bps: u32, fps: u32) {
self.core.set_rates(bitrate_bps, fps);
}
pub fn config(&self) -> &EncoderConfig {
&self.core.cfg
}
}
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;