use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::VideoFrame;
pub trait VideoDecoders {
fn support(&self, description: &VideoStreamDescription) -> VideoDecodeSupport;
fn open(&self, setup: VideoStreamSetup, options: VideoDecoderOptions)
-> VideoDecoderOpenFuture;
}
pub trait VideoDecoder {
fn kind(&self) -> CodecKind;
fn decode(&self, chunk: EncodedChunk) -> VideoDecoderFuture;
fn next_frame(&self) -> DecodedFrameFuture;
fn ended(&self) -> Pin<Box<dyn Future<Output = DecoderError>>>;
fn drain(&self) -> VideoDecoderFuture;
fn reset(&self);
fn configure(&self, setup: VideoStreamSetup) -> VideoDecoderFuture;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VideoCodec {
H264,
Hevc,
Vp8,
Vp9,
Av1,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NalFormat {
LengthPrefixed(Vec<u8>),
StartCodes(Option<Vec<u8>>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum VideoStreamSetup {
H264(NalFormat),
Hevc(NalFormat),
Vp8,
Vp9(Option<Vec<u8>>),
Av1(Option<Vec<u8>>),
}
impl VideoStreamSetup {
pub fn codec(&self) -> VideoCodec {
match self {
VideoStreamSetup::H264(_) => VideoCodec::H264,
VideoStreamSetup::Hevc(_) => VideoCodec::Hevc,
VideoStreamSetup::Vp8 => VideoCodec::Vp8,
VideoStreamSetup::Vp9(_) => VideoCodec::Vp9,
VideoStreamSetup::Av1(_) => VideoCodec::Av1,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EncodedChunk {
pub data: Vec<u8>,
pub timestamp: i64,
pub duration: Option<u64>,
pub key_frame: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CodecChoice {
#[default]
Any,
Hardware,
Software,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CodecKind {
Hardware,
Software,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PictureSize {
pub width: u32,
pub height: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ChromaSampling {
Yuv420,
Yuv422,
Yuv444,
Gray,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VideoStreamDescription {
pub codec: VideoCodec,
pub profile: Option<u32>,
pub level: Option<u32>,
pub bit_depth: u8,
pub chroma: ChromaSampling,
pub size: PictureSize,
pub frame_rate: Option<f32>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct VideoDecodeSupport {
pub hardware: DecodeSupport,
pub software: DecodeSupport,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DecodeSupport {
Yes,
No,
Unknown,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VideoDecoderOptions {
pub decoder: CodecChoice,
pub purpose: DecodingPurpose,
pub frame_limit: u32,
pub largest_size: Option<PictureSize>,
pub default_color: Option<crate::VideoColor>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DecodingPurpose {
Live,
#[default]
Playback,
Background,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecoderError {
Unsupported(String),
Busy,
BadData(String),
Lost,
Failed(String),
}
pub type VideoDecoderOpenFuture =
Pin<Box<dyn Future<Output = Result<Arc<dyn VideoDecoder>, DecoderError>>>>;
pub type VideoDecoderFuture = Pin<Box<dyn Future<Output = Result<(), DecoderError>>>>;
pub type DecodedFrameFuture = Pin<Box<dyn Future<Output = Option<Arc<dyn VideoFrame>>>>>;