styx_codec/decoder/raw/
passthrough.rs

1#[cfg(feature = "image")]
2use crate::decoder::{ImageDecode, frame_to_dynamic_image};
3use crate::{Codec, CodecDescriptor, CodecError, CodecKind};
4use styx_core::prelude::*;
5
6/// Simple passthrough decoder that validates FourCc and returns the frame unchanged.
7pub struct PassthroughDecoder {
8    descriptor: CodecDescriptor,
9}
10
11impl PassthroughDecoder {
12    /// Create a passthrough decoder for the given FourCc.
13    pub fn new(fourcc: FourCc) -> Self {
14        Self {
15            descriptor: CodecDescriptor {
16                kind: CodecKind::Decoder,
17                input: fourcc,
18                output: fourcc,
19                name: "passthrough",
20                impl_name: "passthrough",
21            },
22        }
23    }
24}
25
26impl Codec for PassthroughDecoder {
27    fn descriptor(&self) -> &CodecDescriptor {
28        &self.descriptor
29    }
30
31    fn process(&self, input: FrameLease) -> Result<FrameLease, CodecError> {
32        if input.meta().format.code != self.descriptor.input {
33            return Err(CodecError::FormatMismatch {
34                expected: self.descriptor.input,
35                actual: input.meta().format.code,
36            });
37        }
38        Ok(input)
39    }
40}
41
42#[cfg(feature = "image")]
43impl ImageDecode for PassthroughDecoder {
44    fn decode_image(&self, frame: FrameLease) -> Result<image::DynamicImage, CodecError> {
45        frame_to_dynamic_image(&frame).ok_or_else(|| {
46            CodecError::Codec("unable to convert passthrough frame to DynamicImage".into())
47        })
48    }
49}