use crate::{DecodeError, DecodeOptions, ImageInfo};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use valo::PixelBuffer;
pub type Decoding<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
pub trait Decoder: Send + Sync {
fn name(&self) -> &'static str;
fn open<'a>(
&'a self,
request: &'a OpenRequest,
) -> Decoding<'a, Result<Box<dyn FrameReader>, OpenError>>;
}
#[derive(Clone)]
pub struct OpenRequest {
pub encoded: Arc<[u8]>,
pub options: DecodeOptions,
pub device: wgpu::Device,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OpenError {
Unsupported(String),
Failed(DecodeError),
}
impl From<DecodeError> for OpenError {
fn from(error: DecodeError) -> Self {
Self::Failed(error)
}
}
pub trait FrameReader {
fn info(&self) -> ImageInfo;
fn next_frame(&mut self) -> Decoding<'_, Result<DecodedFrame, DecodeError>>;
}
pub struct DecodedFrame {
pub pixels: FramePixels,
pub duration: Duration,
}
pub enum FramePixels {
Cpu(PixelBuffer),
Gpu(wgpu::Texture),
}
impl DecodedFrame {
pub fn still(pixels: FramePixels) -> Self {
Self {
pixels,
duration: Duration::ZERO,
}
}
}