use crate::{BitDepth, ChromaFormat};
use std::fmt;
#[derive(Debug)]
pub enum EncodeError {
InvalidDimensions {
width: u32,
height: u32,
},
InvalidInput,
DctError(String),
BitstreamError(String),
IsobmffError(String),
Io(std::io::Error),
DimensionTooLarge {
width: usize,
height: usize,
},
InvalidQuality,
UnsupportedChromaFormat(ChromaFormat),
UnsupportedChromaBitDepth(BitDepth),
UnsupportedVideoMode {
chroma: ChromaFormat,
bit_depth: BitDepth,
},
SequenceMismatch(&'static str),
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidDimensions { width, height } => {
write!(f, "Invalid image dimensions: {width}x{height}")
}
Self::InvalidInput => write!(
f,
"Invalid input: plane sizes or parameters are inconsistent"
),
Self::DctError(msg) => write!(f, "DCT block error: {msg}"),
Self::BitstreamError(msg) => write!(f, "Bitstream write error: {msg}"),
Self::IsobmffError(msg) => write!(f, "ISOBMFF error: {msg}"),
Self::Io(e) => write!(f, "IO error: {e}"),
Self::DimensionTooLarge { width, height } => write!(
f,
"image dimensions {width}×{height} exceed the maximum ({})",
16534
),
Self::InvalidQuality => write!(f, "Invalid quality"),
Self::UnsupportedChromaFormat(chroma) => {
write!(f, "Chroma {:?} in current path is not supported", chroma)
}
EncodeError::UnsupportedChromaBitDepth(bp) => {
write!(f, "Bit-depth {:?} is not supported", bp)
}
EncodeError::UnsupportedVideoMode { chroma, bit_depth } => write!(
f,
"unsupported AV2 inter-video mode: {chroma:?} {bit_depth:?}; only 4:2:0 8/10-bit is currently supported"
),
EncodeError::SequenceMismatch(field) => {
write!(f, "{field} changed after the sequence header was emitted")
}
}
}
}
impl std::error::Error for EncodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
EncodeError::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for EncodeError {
fn from(e: std::io::Error) -> Self {
EncodeError::Io(e)
}
}