use std::fmt;
use std::io::{Read, Write};
use std::str::FromStr;
use std::time::Duration;
use crate::{
Frame, FrameInfo, FramePsnr, MediaError, PixelFormat, Result, SettingManifest, SettingValue,
Timestamp,
};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CodecId(String);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameRate {
pub numerator: u32,
pub denominator: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VideoRateControl {
#[default]
CodecDefault,
Lossless,
ConstantQuantizer(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ReconstructionMode {
#[default]
None,
MetricsOnly,
Frames,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VideoSettingValue {
Boolean(bool),
Integer(i64),
Text(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VideoEncoderSetting {
pub name: String,
pub value: VideoSettingValue,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VideoEncoderConfig {
pub codec: CodecId,
pub input: FrameInfo,
pub frame_rate: Option<FrameRate>,
pub frame_limit: Option<usize>,
pub rate_control: VideoRateControl,
pub reconstruction: ReconstructionMode,
pub settings: Vec<VideoEncoderSetting>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VideoChunkKind {
Config,
Frame,
EndOfStream,
Stream,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncodedVideoChunk {
pub codec: CodecId,
pub kind: VideoChunkKind,
pub data: Vec<u8>,
pub frame_index: Option<usize>,
pub pts: Option<Timestamp>,
pub dts: Option<Timestamp>,
pub duration: Option<Timestamp>,
pub keyframe: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FrameEncodeMetrics {
pub frame_index: usize,
pub frame_count: Option<usize>,
pub encoded_bytes: usize,
pub psnr: Option<f64>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct VideoEncodeOutput {
pub chunks: Vec<EncodedVideoChunk>,
pub reconstructions: Vec<Frame>,
pub metrics: Vec<FrameEncodeMetrics>,
}
pub trait VideoEncoderSession {
fn codec(&self) -> &CodecId;
fn config(&self) -> &VideoEncoderConfig;
fn encode_frame(&mut self, frame: Frame) -> Result<VideoEncodeOutput>;
fn flush(&mut self) -> Result<VideoEncodeOutput> {
Ok(VideoEncodeOutput::default())
}
}
#[doc(hidden)]
pub type VideoEncoderSessionFactory =
fn(VideoEncoderConfig) -> Result<Box<dyn VideoEncoderSession>>;
pub trait RawVideoFrameSource {
fn read_frame(&mut self, frame: &mut [u8]) -> Result<bool>;
}
impl<F> RawVideoFrameSource for F
where
F: FnMut(&mut [u8]) -> Result<bool>,
{
fn read_frame(&mut self, frame: &mut [u8]) -> Result<bool> {
self(frame)
}
}
pub struct RawVideoFrameReadSource<R> {
inner: R,
}
impl<R: Read> RawVideoFrameReadSource<R> {
pub fn new(inner: R) -> Self {
Self { inner }
}
pub fn into_inner(self) -> R {
self.inner
}
}
impl<R: Read> RawVideoFrameSource for RawVideoFrameReadSource<R> {
fn read_frame(&mut self, frame: &mut [u8]) -> Result<bool> {
let mut offset = 0usize;
while offset < frame.len() {
let count = self
.inner
.read(&mut frame[offset..])
.map_err(|err| MediaError::Message(format!("failed to read input frame: {err}")))?;
if count == 0 {
if offset == 0 {
return Ok(false);
}
return Err(MediaError::ShortFrameRead {
expected: frame.len(),
actual: offset,
});
}
offset += count;
}
Ok(true)
}
}
pub struct RawVideoFrameSourceReadAdapter<S> {
source: S,
frame: Vec<u8>,
frame_offset: usize,
frames_read: usize,
frame_limit: Option<usize>,
}
impl<S> RawVideoFrameSourceReadAdapter<S>
where
S: RawVideoFrameSource,
{
pub fn new(source: S, info: FrameInfo) -> Self {
let frame_len = info.expected_len();
Self {
source,
frame: vec![0; frame_len],
frame_offset: frame_len,
frames_read: 0,
frame_limit: None,
}
}
pub fn with_frame_limit(mut self, frame_limit: usize) -> Self {
self.frame_limit = Some(frame_limit);
self
}
pub const fn frames_read(&self) -> usize {
self.frames_read
}
pub fn into_inner(self) -> S {
self.source
}
fn load_frame(&mut self) -> std::io::Result<bool> {
if self
.frame_limit
.is_some_and(|limit| self.frames_read >= limit)
{
return Ok(false);
}
let has_frame = self
.source
.read_frame(&mut self.frame)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))?;
if !has_frame {
return Ok(false);
}
self.frames_read += 1;
self.frame_offset = 0;
Ok(true)
}
}
impl<S> Read for RawVideoFrameSourceReadAdapter<S>
where
S: RawVideoFrameSource,
{
fn read(&mut self, output: &mut [u8]) -> std::io::Result<usize> {
if output.is_empty() {
return Ok(0);
}
let mut written = 0usize;
while written < output.len() {
if self.frame_offset == self.frame.len() && !self.load_frame()? {
break;
}
let remaining_frame = self.frame.len() - self.frame_offset;
let count = remaining_frame.min(output.len() - written);
output[written..written + count]
.copy_from_slice(&self.frame[self.frame_offset..self.frame_offset + count]);
self.frame_offset += count;
written += count;
}
Ok(written)
}
}
#[derive(Debug, Clone, Copy)]
pub struct VideoEncoderManifest {
pub name: &'static str,
pub feature: &'static str,
pub summary: &'static str,
pub settings: &'static [SettingManifest],
pub accepts_format: fn(PixelFormat) -> bool,
pub supports_lossless_format: fn(PixelFormat) -> bool,
create_session_hook: VideoEncoderSessionFactory,
encode_source_hook: VideoEncodeSourceFn,
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct VideoEncodeSourceRequest<'a> {
#[doc(hidden)]
pub config: &'a VideoEncoderConfig,
}
pub struct VideoEncodeFrameMetrics<'a> {
pub frame_idx: usize,
pub frame_count: Option<usize>,
pub bitstream_bytes: usize,
pub total_bitstream_bytes: usize,
pub encode_elapsed: Duration,
pub psnr: Option<FramePsnr>,
pub source: &'a [u8],
pub reconstruction: &'a [u8],
}
pub type VideoEncodeFrameMetricsCallback<'a> =
&'a mut dyn for<'frame> FnMut(VideoEncodeFrameMetrics<'frame>);
#[doc(hidden)]
pub type VideoEncodeSourceFn = for<'request, 'callback> fn(
&mut dyn RawVideoFrameSource,
&mut dyn Write,
Option<&mut dyn Write>,
VideoEncodeSourceRequest<'request>,
Option<VideoEncodeFrameMetricsCallback<'callback>>,
) -> Result<()>;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct VideoEncoderManifestHooks {
#[doc(hidden)]
pub create_session: VideoEncoderSessionFactory,
#[doc(hidden)]
pub encode_source: VideoEncodeSourceFn,
}
impl CodecId {
pub fn new(value: impl Into<String>) -> Result<Self> {
let value = value.into();
validate_codec_id(&value)?;
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for CodecId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CodecId").field(&self.0).finish()
}
}
impl fmt::Display for CodecId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for CodecId {
type Err = MediaError;
fn from_str(value: &str) -> Result<Self> {
Self::new(value)
}
}
impl TryFrom<&str> for CodecId {
type Error = MediaError;
fn try_from(value: &str) -> Result<Self> {
Self::new(value)
}
}
impl FrameRate {
pub fn new(numerator: u32, denominator: u32) -> Result<Self> {
if numerator == 0 || denominator == 0 {
return Err(MediaError::Message(
"frame rate numerator and denominator must be non-zero".to_string(),
));
}
Ok(Self {
numerator,
denominator,
})
}
}
impl VideoRateControl {
pub fn constant_quantizer(qp: u8) -> Result<Self> {
if qp == 0 {
return Err(MediaError::Message(
"constant quantizer must be in the range 1..255".to_string(),
));
}
Ok(Self::ConstantQuantizer(qp))
}
}
impl VideoSettingValue {
pub fn as_cli_value(&self) -> String {
match self {
Self::Boolean(value) => value.to_string(),
Self::Integer(value) => value.to_string(),
Self::Text(value) => value.clone(),
}
}
}
impl From<bool> for VideoSettingValue {
fn from(value: bool) -> Self {
Self::Boolean(value)
}
}
impl From<i64> for VideoSettingValue {
fn from(value: i64) -> Self {
Self::Integer(value)
}
}
impl From<i32> for VideoSettingValue {
fn from(value: i32) -> Self {
Self::Integer(i64::from(value))
}
}
impl From<u8> for VideoSettingValue {
fn from(value: u8) -> Self {
Self::Integer(i64::from(value))
}
}
impl From<String> for VideoSettingValue {
fn from(value: String) -> Self {
Self::Text(value)
}
}
impl From<&str> for VideoSettingValue {
fn from(value: &str) -> Self {
Self::Text(value.to_string())
}
}
impl VideoEncoderSetting {
pub fn new(name: impl Into<String>, value: VideoSettingValue) -> Result<Self> {
let name = name.into();
validate_setting_name(&name)?;
Ok(Self { name, value })
}
pub fn boolean(name: impl Into<String>, value: bool) -> Result<Self> {
Self::new(name, VideoSettingValue::Boolean(value))
}
pub fn integer(name: impl Into<String>, value: i64) -> Result<Self> {
Self::new(name, VideoSettingValue::Integer(value))
}
pub fn text(name: impl Into<String>, value: impl Into<String>) -> Result<Self> {
Self::new(name, VideoSettingValue::Text(value.into()))
}
pub fn as_cli_spec(&self) -> String {
format!("{}={}", self.name, self.value.as_cli_value())
}
}
impl VideoEncoderConfig {
pub fn new(codec: CodecId, input: FrameInfo) -> Self {
Self {
codec,
input,
frame_rate: None,
frame_limit: None,
rate_control: VideoRateControl::CodecDefault,
reconstruction: ReconstructionMode::None,
settings: Vec::new(),
}
}
pub fn for_codec(codec: impl AsRef<str>, input: FrameInfo) -> Result<Self> {
Ok(Self::new(CodecId::new(codec.as_ref())?, input))
}
pub fn with_frame_rate(mut self, frame_rate: FrameRate) -> Self {
self.frame_rate = Some(frame_rate);
self
}
pub fn with_frame_limit(mut self, frame_limit: usize) -> Self {
self.frame_limit = Some(frame_limit);
self
}
pub fn with_rate_control(mut self, rate_control: VideoRateControl) -> Self {
self.rate_control = rate_control;
self
}
pub fn with_reconstruction(mut self, reconstruction: ReconstructionMode) -> Self {
self.reconstruction = reconstruction;
self
}
pub fn with_setting(mut self, setting: VideoEncoderSetting) -> Self {
self.settings.push(setting);
self
}
pub fn setting_specs(&self) -> Vec<String> {
let mut specs = Vec::new();
match self.rate_control {
VideoRateControl::CodecDefault => {}
VideoRateControl::Lossless => specs.push("lossless=true".to_string()),
VideoRateControl::ConstantQuantizer(qp) => specs.push(format!("qp={qp}")),
}
specs.extend(self.settings.iter().map(VideoEncoderSetting::as_cli_spec));
specs
}
}
impl EncodedVideoChunk {
pub fn new(codec: CodecId, kind: VideoChunkKind, data: Vec<u8>) -> Self {
Self {
codec,
kind,
data,
frame_index: None,
pts: None,
dts: None,
duration: None,
keyframe: false,
}
}
}
impl VideoEncodeOutput {
pub fn from_chunk(chunk: EncodedVideoChunk) -> Self {
Self {
chunks: vec![chunk],
reconstructions: Vec::new(),
metrics: Vec::new(),
}
}
}
impl VideoEncoderManifest {
#[doc(hidden)]
pub const fn new(
name: &'static str,
feature: &'static str,
summary: &'static str,
settings: &'static [SettingManifest],
accepts_format: fn(PixelFormat) -> bool,
supports_lossless_format: fn(PixelFormat) -> bool,
hooks: VideoEncoderManifestHooks,
) -> Self {
Self {
name,
feature,
summary,
settings,
accepts_format,
supports_lossless_format,
create_session_hook: hooks.create_session,
encode_source_hook: hooks.encode_source,
}
}
#[doc(hidden)]
pub fn session_factory(self) -> VideoEncoderSessionFactory {
self.create_session_hook
}
#[doc(hidden)]
pub fn source_encode_hook(self) -> VideoEncodeSourceFn {
self.encode_source_hook
}
pub fn codec_id(self) -> Result<CodecId> {
CodecId::new(self.name)
}
pub fn setting(self, name: &str) -> Option<SettingManifest> {
self.settings
.iter()
.copied()
.find(|setting| setting.name == name)
}
pub fn declared_setting(self, name: &str) -> Option<SettingManifest> {
crate::GLOBAL_SETTINGS
.iter()
.copied()
.find(|setting| setting.name == name)
.or_else(|| self.setting(name))
}
pub fn apply_setting_specs<I, S>(
self,
mut config: VideoEncoderConfig,
specs: I,
) -> Result<VideoEncoderConfig>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
if config.codec.as_str() != self.name {
return Err(MediaError::UnsupportedCodec {
codec: config.codec.to_string(),
reason: format!("manifest '{}' cannot configure it", self.name),
});
}
let mut seen = Vec::new();
for spec in specs {
let spec = spec.as_ref();
let name = crate::setting_name(spec);
validate_setting_name(name)?;
if seen.iter().any(|seen_name| seen_name == name) {
return Err(MediaError::DuplicateSetting {
setting: name.to_string(),
});
}
seen.push(name.to_string());
let Some(manifest) = self.declared_setting(name) else {
return Err(MediaError::UnknownSetting {
codec: self.name.to_string(),
setting: name.to_string(),
});
};
let value = crate::setting_value(spec).unwrap_or("true");
if !manifest.value.accepts(value) {
return Err(MediaError::InvalidSettingValue {
codec: self.name.to_string(),
setting: manifest.name.to_string(),
expected: crate::setting_values_label(manifest),
actual: value.to_string(),
});
}
if name == "lossless" {
let lossless = parse_manifest_bool(self.name, manifest, value)?;
if lossless {
if matches!(config.rate_control, VideoRateControl::ConstantQuantizer(_)) {
return Err(MediaError::ConflictingSettings {
setting: name.to_string(),
conflict: "VideoRateControl::ConstantQuantizer".to_string(),
});
}
config.rate_control = VideoRateControl::Lossless;
} else if matches!(config.rate_control, VideoRateControl::Lossless) {
config.rate_control = VideoRateControl::CodecDefault;
}
continue;
}
if name == "qp" {
if matches!(config.rate_control, VideoRateControl::Lossless) {
return Err(MediaError::ConflictingSettings {
setting: name.to_string(),
conflict: "VideoRateControl::Lossless".to_string(),
});
}
let qp = value
.parse::<u8>()
.map_err(|_| MediaError::InvalidSettingValue {
codec: self.name.to_string(),
setting: manifest.name.to_string(),
expected: crate::setting_values_label(manifest),
actual: value.to_string(),
})?;
config.rate_control = VideoRateControl::constant_quantizer(qp)?;
continue;
}
let setting = video_setting_from_manifest(self.name, manifest, value)?;
config.settings.retain(|setting| setting.name != name);
config.settings.push(setting);
}
self.validate_config(&config)?;
Ok(config)
}
pub fn effective_setting_specs(self, config: &VideoEncoderConfig) -> Result<Vec<String>> {
self.validate_config(config)?;
let mut specs = Vec::new();
push_effective_setting_specs(config, crate::GLOBAL_SETTINGS, &mut specs);
push_effective_setting_specs(config, self.settings, &mut specs);
Ok(specs)
}
pub fn accepts_frame_info(self, input: FrameInfo) -> bool {
(self.accepts_format)(input.format)
}
pub fn supports_lossless_frame_info(self, input: FrameInfo) -> bool {
(self.supports_lossless_format)(input.format)
}
pub fn validate_config(self, config: &VideoEncoderConfig) -> Result<()> {
if config.codec.as_str() != self.name {
return Err(MediaError::UnsupportedCodec {
codec: config.codec.to_string(),
reason: format!("manifest '{}' cannot create it", self.name),
});
}
if !self.accepts_frame_info(config.input) {
return Err(MediaError::UnsupportedPixelFormat {
codec: self.name.to_string(),
format: config.input.format.to_string(),
});
}
if matches!(config.rate_control, VideoRateControl::Lossless)
&& !self.supports_lossless_frame_info(config.input)
{
return Err(MediaError::UnsupportedPixelFormat {
codec: self.name.to_string(),
format: format!("lossless {}", config.input.format),
});
}
let mut seen = Vec::new();
for setting in &config.settings {
if setting.name == "lossless" {
return Err(MediaError::ConflictingSettings {
setting: setting.name.clone(),
conflict: "VideoRateControl::Lossless".to_string(),
});
}
if matches!(config.rate_control, VideoRateControl::ConstantQuantizer(_))
&& setting.name == "qp"
{
return Err(MediaError::ConflictingSettings {
setting: setting.name.clone(),
conflict: "VideoRateControl::ConstantQuantizer".to_string(),
});
}
if matches!(config.rate_control, VideoRateControl::Lossless) && setting.name == "qp" {
return Err(MediaError::ConflictingSettings {
setting: setting.name.clone(),
conflict: "lossless rate control".to_string(),
});
}
if seen.contains(&setting.name.as_str()) {
return Err(MediaError::DuplicateSetting {
setting: setting.name.clone(),
});
}
seen.push(setting.name.as_str());
let Some(manifest) = self.setting(&setting.name) else {
return Err(MediaError::UnknownSetting {
codec: self.name.to_string(),
setting: setting.name.clone(),
});
};
if !setting_value_matches_manifest(setting, manifest.value) {
return Err(MediaError::InvalidSettingValue {
codec: self.name.to_string(),
setting: manifest.name.to_string(),
expected: crate::setting_values_label(manifest),
actual: setting.value.as_cli_value(),
});
}
}
Ok(())
}
}
fn parse_manifest_bool(
codec: &'static str,
manifest: SettingManifest,
value: &str,
) -> Result<bool> {
match value {
"true" | "1" | "yes" | "on" => Ok(true),
"false" | "0" | "no" | "off" => Ok(false),
_ => Err(MediaError::InvalidSettingValue {
codec: codec.to_string(),
setting: manifest.name.to_string(),
expected: crate::setting_values_label(manifest),
actual: value.to_string(),
}),
}
}
fn video_setting_from_manifest(
codec: &'static str,
manifest: SettingManifest,
value: &str,
) -> Result<VideoEncoderSetting> {
match manifest.value {
SettingValue::Boolean => VideoEncoderSetting::boolean(
manifest.name,
parse_manifest_bool(codec, manifest, value)?,
),
SettingValue::Choice(_) => VideoEncoderSetting::text(manifest.name, value),
SettingValue::IntegerRange { .. } | SettingValue::SignedIntegerRange { .. } => {
let value = value
.parse::<i64>()
.map_err(|_| MediaError::InvalidSettingValue {
codec: codec.to_string(),
setting: manifest.name.to_string(),
expected: crate::setting_values_label(manifest),
actual: value.to_string(),
})?;
VideoEncoderSetting::integer(manifest.name, value)
}
}
}
fn push_effective_setting_specs(
config: &VideoEncoderConfig,
manifests: &[SettingManifest],
specs: &mut Vec<String>,
) {
for manifest in manifests {
if let Some(value) = effective_setting_value(config, *manifest) {
specs.push(format!("{}={value}", manifest.name));
}
}
}
fn effective_setting_value(
config: &VideoEncoderConfig,
manifest: SettingManifest,
) -> Option<String> {
if manifest.name == "lossless" {
return Some(matches!(config.rate_control, VideoRateControl::Lossless).to_string());
}
if manifest.name == "qp" {
return match config.rate_control {
VideoRateControl::ConstantQuantizer(qp) => Some(qp.to_string()),
VideoRateControl::CodecDefault | VideoRateControl::Lossless => {
manifest.default_value.map(str::to_string)
}
};
}
config
.settings
.iter()
.find(|setting| setting.name == manifest.name)
.map(|setting| setting.value.as_cli_value())
.or_else(|| manifest.default_value.map(str::to_string))
}
fn setting_value_matches_manifest(setting: &VideoEncoderSetting, manifest: SettingValue) -> bool {
match (manifest, &setting.value) {
(SettingValue::Boolean, VideoSettingValue::Boolean(_)) => true,
(SettingValue::Choice(values), VideoSettingValue::Text(value)) => {
values.contains(&value.as_str())
}
(SettingValue::IntegerRange { min, max }, VideoSettingValue::Integer(value)) => {
(i64::from(min)..=i64::from(max)).contains(value)
}
(SettingValue::SignedIntegerRange { min, max }, VideoSettingValue::Integer(value)) => {
(i64::from(min)..=i64::from(max)).contains(value)
}
_ => false,
}
}
fn validate_codec_id(value: &str) -> Result<()> {
if value.is_empty() {
return Err(MediaError::Message("codec id cannot be empty".to_string()));
}
if !value
.bytes()
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
{
return Err(MediaError::Message(format!(
"codec id '{value}' must contain only lowercase ASCII letters, digits, or '-'"
)));
}
Ok(())
}
fn validate_setting_name(value: &str) -> Result<()> {
if value.is_empty() {
return Err(MediaError::Message(
"setting name cannot be empty".to_string(),
));
}
if !value
.bytes()
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
{
return Err(MediaError::Message(format!(
"setting name '{value}' must contain only lowercase ASCII letters, digits, or '-'"
)));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{PixelFormat, SettingManifest, SettingSpecForm, SettingSpecManifest, SettingValue};
const GOP_FORMS: &[SettingSpecForm] = &[SettingSpecForm {
syntax: "gop=<-1..65535>",
summary: "set temporal intra period",
}];
const GOP_SPEC: SettingSpecManifest = SettingSpecManifest {
forms: GOP_FORMS,
examples: &[],
notes: &[],
};
const GOP_SETTING: SettingManifest = SettingManifest {
name: "gop",
value: SettingValue::SignedIntegerRange {
min: -1,
max: 65535,
},
default_value: Some("-1"),
spec: &GOP_SPEC,
summary: "set temporal intra period",
};
const TEST_SETTINGS: &[SettingManifest] = &[GOP_SETTING];
const TEST_CODEC: VideoEncoderManifest = VideoEncoderManifest::new(
"test",
"test-codec",
"test codec",
TEST_SETTINGS,
test_accepts_format,
test_supports_lossless_format,
VideoEncoderManifestHooks {
create_session: test_create_session,
encode_source: test_encode_source,
},
);
struct TestSession {
config: VideoEncoderConfig,
}
impl VideoEncoderSession for TestSession {
fn codec(&self) -> &CodecId {
&self.config.codec
}
fn config(&self) -> &VideoEncoderConfig {
&self.config
}
fn encode_frame(&mut self, _frame: Frame) -> Result<VideoEncodeOutput> {
Ok(VideoEncodeOutput::default())
}
}
fn test_accepts_format(format: PixelFormat) -> bool {
format == PixelFormat::Yuv420p8
}
fn test_supports_lossless_format(format: PixelFormat) -> bool {
test_accepts_format(format)
}
fn test_create_session(config: VideoEncoderConfig) -> Result<Box<dyn VideoEncoderSession>> {
Ok(Box::new(TestSession { config }))
}
fn test_encode_source(
_source: &mut dyn RawVideoFrameSource,
_output: &mut dyn Write,
_recon: Option<&mut dyn Write>,
_request: VideoEncodeSourceRequest<'_>,
_frame_metrics: Option<VideoEncodeFrameMetricsCallback<'_>>,
) -> Result<()> {
Ok(())
}
#[test]
fn codec_id_accepts_stable_lowercase_names() {
assert_eq!(CodecId::new("av2").unwrap().as_str(), "av2");
assert_eq!(CodecId::new("vvc-main10").unwrap().as_str(), "vvc-main10");
}
#[test]
fn codec_id_rejects_names_that_do_not_round_trip_to_cli() {
assert!(CodecId::new("").is_err());
assert!(CodecId::new("AV2").is_err());
assert!(CodecId::new("vvc/main10").is_err());
}
#[test]
fn config_collects_rate_control_and_extension_settings_as_specs() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let config = VideoEncoderConfig::new(CodecId::new("vvc").unwrap(), info)
.with_rate_control(VideoRateControl::constant_quantizer(24).unwrap())
.with_setting(VideoEncoderSetting::integer("gop", 30).unwrap())
.with_setting(VideoEncoderSetting::text("fast-search", "moderate").unwrap());
assert_eq!(
config.setting_specs(),
vec![
"qp=24".to_string(),
"gop=30".to_string(),
"fast-search=moderate".to_string()
]
);
}
#[test]
fn lossless_rate_control_maps_to_a_normal_setting_spec() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let config = VideoEncoderConfig::new(CodecId::new("av2").unwrap(), info)
.with_rate_control(VideoRateControl::Lossless);
assert_eq!(config.setting_specs(), vec!["lossless=true".to_string()]);
}
#[test]
fn manifest_validates_codec_format_and_settings() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let config = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info)
.with_setting(VideoEncoderSetting::integer("gop", -1).unwrap());
TEST_CODEC
.validate_config(&config)
.expect("valid manifest config");
}
#[test]
fn manifest_applies_cli_style_setting_specs_to_config() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let config = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info);
let config = TEST_CODEC
.apply_setting_specs(config, ["lossless", "gop=30"])
.expect("settings should apply");
assert!(matches!(config.rate_control, VideoRateControl::Lossless));
assert_eq!(config.setting_specs(), vec!["lossless=true", "gop=30"]);
assert_eq!(
TEST_CODEC.effective_setting_specs(&config).unwrap(),
vec!["lossless=true", "gop=30"]
);
}
#[test]
fn manifest_renders_effective_setting_defaults() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let config = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info);
assert_eq!(
TEST_CODEC.effective_setting_specs(&config).unwrap(),
vec!["lossless=false", "gop=-1"]
);
}
#[test]
fn manifest_setting_specs_reject_duplicate_names() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let config = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info);
assert!(matches!(
TEST_CODEC
.apply_setting_specs(config, ["gop=0", "gop=30"])
.unwrap_err(),
MediaError::DuplicateSetting { setting } if setting == "gop"
));
}
#[test]
fn manifest_rejects_unknown_and_duplicate_settings() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let unknown = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info)
.with_setting(VideoEncoderSetting::boolean("missing", true).unwrap());
let duplicate = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info)
.with_setting(VideoEncoderSetting::integer("gop", -1).unwrap())
.with_setting(VideoEncoderSetting::integer("gop", 0).unwrap());
assert!(matches!(
TEST_CODEC.validate_config(&unknown).unwrap_err(),
MediaError::UnknownSetting { codec, setting }
if codec == "test" && setting == "missing"
));
assert!(matches!(
TEST_CODEC.validate_config(&duplicate).unwrap_err(),
MediaError::DuplicateSetting { setting } if setting == "gop"
));
}
#[test]
fn manifest_rejects_invalid_setting_values_and_conflicts() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let invalid_value = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info)
.with_setting(VideoEncoderSetting::integer("gop", -2).unwrap());
let conflicting_qp = VideoEncoderConfig::new(CodecId::new("test").unwrap(), info)
.with_rate_control(VideoRateControl::constant_quantizer(24).unwrap())
.with_setting(VideoEncoderSetting::integer("qp", 19).unwrap());
assert!(matches!(
TEST_CODEC.validate_config(&invalid_value).unwrap_err(),
MediaError::InvalidSettingValue {
codec,
setting,
expected,
actual,
} if codec == "test"
&& setting == "gop"
&& expected == "-1..65535"
&& actual == "-2"
));
assert!(matches!(
TEST_CODEC.validate_config(&conflicting_qp).unwrap_err(),
MediaError::ConflictingSettings { setting, conflict }
if setting == "qp" && conflict == "VideoRateControl::ConstantQuantizer"
));
}
#[test]
fn manifest_rejects_codec_and_format_mismatches_structurally() {
let info = FrameInfo::new(16, 16, PixelFormat::Yuv420p8).unwrap();
let wrong_codec = VideoEncoderConfig::new(CodecId::new("other").unwrap(), info);
let wrong_format = VideoEncoderConfig::new(
CodecId::new("test").unwrap(),
FrameInfo::new(16, 16, PixelFormat::Rgb24).unwrap(),
);
assert!(matches!(
TEST_CODEC.validate_config(&wrong_codec).unwrap_err(),
MediaError::UnsupportedCodec { codec, .. } if codec == "other"
));
assert!(matches!(
TEST_CODEC.validate_config(&wrong_format).unwrap_err(),
MediaError::UnsupportedPixelFormat { codec, format }
if codec == "test" && format == "rgb24"
));
}
#[test]
fn raw_video_read_source_reports_short_frames_structurally() {
let mut source = RawVideoFrameReadSource::new(std::io::Cursor::new(vec![1, 2, 3]));
let mut frame = [0u8; 4];
let err = source.read_frame(&mut frame).unwrap_err();
assert!(matches!(
err,
MediaError::ShortFrameRead {
expected: 4,
actual: 3,
}
));
}
}