use crate::{Error, I420Frame, Result};
pub(crate) const MAX_FPS: u32 = 120;
const DEFAULT_MAX_WIDTH: u32 = 7680;
const DEFAULT_MAX_HEIGHT: u32 = 4320;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Codec {
Vp9,
Av1,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EncoderConfig {
pub width: u32,
pub height: u32,
pub fps: u32,
pub bitrate_kbps: u32,
pub temporal_layers: Option<u8>,
pub threads: Option<u32>,
pub max_width: u32,
pub max_height: u32,
}
impl EncoderConfig {
#[must_use]
pub fn new(width: u32, height: u32, fps: u32, bitrate_kbps: u32) -> Self {
Self {
width,
height,
fps,
bitrate_kbps,
temporal_layers: None,
threads: None,
max_width: DEFAULT_MAX_WIDTH,
max_height: DEFAULT_MAX_HEIGHT,
}
}
#[must_use]
pub fn with_temporal_layers(mut self, layers: u8) -> Self {
self.temporal_layers = Some(layers);
self
}
#[must_use]
pub fn with_threads(mut self, threads: u32) -> Self {
self.threads = Some(threads);
self
}
#[must_use]
pub fn with_max_size(mut self, max_width: u32, max_height: u32) -> Self {
self.max_width = max_width;
self.max_height = max_height;
self
}
#[cfg_attr(
not(any(
feature = "svt-av1",
feature = "libvpx",
all(
any(feature = "nvenc", feature = "amf", feature = "qsv"),
any(target_os = "windows", target_os = "linux")
),
all(feature = "vaapi", target_os = "linux")
)),
allow(dead_code)
)]
pub(crate) fn validate_common(&self, max_layers: u8) -> Result<()> {
if self.width > self.max_width || self.height > self.max_height {
return Err(Error::InvalidConfig(format!(
"size {}x{} is larger than the maximum picture size {}x{}",
self.width, self.height, self.max_width, self.max_height
)));
}
if self.fps == 0 || self.fps > MAX_FPS {
return Err(Error::InvalidConfig(format!(
"fps {} (1 to {MAX_FPS} are accepted)",
self.fps
)));
}
if self.bitrate_kbps == 0 {
return Err(Error::InvalidConfig("bitrate 0".into()));
}
match self.temporal_layers {
Some(layers) if layers == 0 || layers > max_layers => Err(Error::InvalidConfig(
format!("{layers} temporal layers (this encoder produces 1 to {max_layers})"),
)),
_ => Ok(()),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EncodedFrame {
pub data: Vec<u8>,
pub keyframe: bool,
pub temporal_layer: u8,
pub pts: i64,
}
pub trait VideoEncoder: Send {
fn codec(&self) -> Codec;
fn config(&self) -> EncoderConfig;
fn temporal_layers(&self) -> u8;
fn encode(
&mut self,
frame: &I420Frame,
pts: i64,
force_keyframe: bool,
) -> Result<Vec<EncodedFrame>>;
fn set_resolution(&mut self, width: u32, height: u32) -> Result<()>;
fn set_bitrate(&mut self, bitrate_kbps: u32) -> Result<()>;
fn set_fps(&mut self, fps: u32) -> Result<()>;
}
pub trait VideoDecoder: Send {
fn codec(&self) -> Codec;
fn decode(&mut self, data: &[u8]) -> Result<Option<I420Frame>>;
}
#[cfg(test)]
mod tests {
use super::*;
fn config() -> EncoderConfig {
EncoderConfig::new(1280, 720, 30, 1500)
}
#[test]
fn defaults_leave_every_setting_to_the_encoder() {
let config = config();
assert_eq!(config.temporal_layers, None);
assert_eq!(config.threads, None);
assert_eq!((config.max_width, config.max_height), (7680, 4320));
config.validate_common(3).expect("valid");
}
#[test]
fn builders_set_the_settings() {
let config = config()
.with_temporal_layers(1)
.with_threads(2)
.with_max_size(1920, 1080);
assert_eq!(config.temporal_layers, Some(1));
assert_eq!(config.threads, Some(2));
assert_eq!((config.max_width, config.max_height), (1920, 1080));
config.validate_common(3).expect("valid");
}
#[test]
fn accepts_frame_rates_from_1_to_120() {
for fps in [1, 60, 61, 120] {
EncoderConfig { fps, ..config() }
.validate_common(3)
.expect("valid");
}
for fps in [0, 121, 240] {
let result = EncoderConfig { fps, ..config() }.validate_common(3);
assert!(matches!(result, Err(Error::InvalidConfig(_))), "fps {fps}");
}
}
#[test]
fn rejects_a_picture_above_the_maximum_size() {
for (width, height) in [(7681, 720), (1280, 4321)] {
let result = EncoderConfig::new(width, height, 30, 1500).validate_common(3);
assert!(matches!(result, Err(Error::InvalidConfig(_))));
}
EncoderConfig::new(7680, 4320, 30, 1500)
.validate_common(3)
.expect("the default maximum itself is valid");
let small = config().with_max_size(1279, 720).validate_common(3);
assert!(matches!(small, Err(Error::InvalidConfig(_))));
}
#[test]
fn rejects_layer_counts_the_encoder_does_not_produce() {
for layers in [1, 2, 3] {
config()
.with_temporal_layers(layers)
.validate_common(3)
.expect("valid");
}
for layers in [0, 4] {
let result = config().with_temporal_layers(layers).validate_common(3);
assert!(matches!(result, Err(Error::InvalidConfig(_))));
}
}
}