use anyhow::Result;
#[cfg(all(feature = "libvpx", not(target_arch = "wasm32")))]
pub mod libvpx;
#[cfg(all(feature = "libvpx", not(target_arch = "wasm32")))]
pub use libvpx::Vp9Encoder;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EncoderConfig {
pub width: u32,
pub height: u32,
pub framerate: u32,
pub bitrate_kbps: u32,
pub keyframe_interval: u32,
pub min_quantizer: u32,
pub max_quantizer: u32,
pub cpu_used: u8,
}
impl Default for EncoderConfig {
fn default() -> Self {
Self {
width: 640,
height: 480,
framerate: 30,
bitrate_kbps: 500,
keyframe_interval: 150,
min_quantizer: 40,
max_quantizer: 60,
cpu_used: 7,
}
}
}
#[derive(Debug, Clone)]
pub struct EncodedFrame {
pub data: Vec<u8>,
pub is_keyframe: bool,
pub pts: i64,
}
pub trait Encodable {
fn new(config: EncoderConfig) -> Result<Self>
where
Self: Sized;
fn update_bitrate_kbps(&mut self, kbps: u32) -> Result<()>;
fn encode(&mut self, pts: i64, i420: &[u8]) -> Result<Option<EncodedFrame>>;
}
pub fn create_encoder(
codec: crate::decoder::VideoCodec,
cfg: EncoderConfig,
) -> Result<Box<dyn Encodable + Send>> {
use crate::decoder::VideoCodec;
match codec {
VideoCodec::Vp9Profile0Level10Bit8 => Ok(Box::new(crate::vp9::Vp9Encoder::new(cfg)?)),
other => Err(anyhow::anyhow!(
"no pure-Rust encoder available for codec {other:?}"
)),
}
}