Skip to main content

oxideav_codec/
lib.rs

1//! Codec traits and registry.
2//!
3//! Format crates implement `Decoder` / `Encoder` and register themselves by
4//! building `DecoderFactory` / `EncoderFactory` values. The central `oxideav`
5//! aggregator pulls everything together into a single `Registry`.
6
7pub mod registry;
8
9use oxideav_core::{CodecId, CodecParameters, ExecutionContext, Frame, Packet, Result};
10
11/// A packet-to-frame decoder.
12pub trait Decoder: Send {
13    fn codec_id(&self) -> &CodecId;
14
15    /// Feed one compressed packet. May or may not produce a frame immediately —
16    /// call `receive_frame` in a loop afterwards.
17    fn send_packet(&mut self, packet: &Packet) -> Result<()>;
18
19    /// Pull the next decoded frame, if any. Returns `Error::NeedMore` when the
20    /// decoder needs another packet.
21    fn receive_frame(&mut self) -> Result<Frame>;
22
23    /// Signal end-of-stream. After this, `receive_frame` will drain buffered
24    /// frames and eventually return `Error::Eof`.
25    fn flush(&mut self) -> Result<()>;
26
27    /// Advisory: announce the runtime environment (today: a thread budget
28    /// for codec-internal parallelism). Called at most once, before the
29    /// first `send_packet`. Default no-op; codecs that want to run
30    /// slice-/GOP-/tile-parallel override this to capture the budget.
31    /// Ignoring the hint is always safe — callers must still work with
32    /// a decoder that runs serial.
33    fn set_execution_context(&mut self, _ctx: &ExecutionContext) {}
34}
35
36/// A frame-to-packet encoder.
37pub trait Encoder: Send {
38    fn codec_id(&self) -> &CodecId;
39
40    /// Parameters describing this encoder's output stream (to feed into a muxer).
41    fn output_params(&self) -> &CodecParameters;
42
43    fn send_frame(&mut self, frame: &Frame) -> Result<()>;
44
45    fn receive_packet(&mut self) -> Result<Packet>;
46
47    fn flush(&mut self) -> Result<()>;
48
49    /// Advisory: announce the runtime environment. Same semantics as
50    /// [`Decoder::set_execution_context`].
51    fn set_execution_context(&mut self, _ctx: &ExecutionContext) {}
52}
53
54/// Factory that builds a decoder for a given codec parameter set.
55pub type DecoderFactory = fn(params: &CodecParameters) -> Result<Box<dyn Decoder>>;
56
57/// Factory that builds an encoder for a given codec parameter set.
58pub type EncoderFactory = fn(params: &CodecParameters) -> Result<Box<dyn Encoder>>;
59
60pub use registry::{CodecImplementation, CodecRegistry};