split nihav-codec-support crate from nihav-core
[nihav.git] / nihav-core / src / codecs / mod.rs
CommitLineData
3ba71779 1//! Decoder interface definitions.
4e8b4f31 2pub use crate::frame::*;
aca89041
KS
3use crate::io::byteio::ByteIOError;
4use crate::io::bitreader::BitReaderError;
5use crate::io::codebook::CodebookError;
77d06de2 6
3ba71779 7/// A list specifying general decoding errors.
77d06de2
KS
8#[derive(Debug,Clone,Copy,PartialEq)]
9#[allow(dead_code)]
10pub enum DecoderError {
3ba71779 11 /// No frame was provided.
6d3bb0b2 12 NoFrame,
3ba71779 13 /// Allocation failed.
e35062e7 14 AllocError,
3ba71779 15 /// Operation requires repeating.
503374e7 16 TryAgain,
3ba71779 17 /// Invalid input data was provided.
77d06de2 18 InvalidData,
3ba71779 19 /// Provided input turned out to be incomplete.
77d06de2 20 ShortData,
3ba71779 21 /// Decoder could not decode provided frame because it references some missing previous frame.
77d06de2 22 MissingReference,
3ba71779 23 /// Feature is not implemented.
77d06de2 24 NotImplemented,
3ba71779 25 /// Some bug in decoder. It should not happen yet it might.
77d06de2
KS
26 Bug,
27}
28
3ba71779 29/// A specialised `Result` type for decoding operations.
cf64af13 30pub type DecoderResult<T> = Result<T, DecoderError>;
77d06de2
KS
31
32impl From<ByteIOError> for DecoderError {
33 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
34}
35
36impl From<BitReaderError> for DecoderError {
37 fn from(e: BitReaderError) -> Self {
38 match e {
39 BitReaderError::BitstreamEnd => DecoderError::ShortData,
40 _ => DecoderError::InvalidData,
41 }
42 }
43}
44
45impl From<CodebookError> for DecoderError {
46 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
47}
48
e35062e7
KS
49impl From<AllocatorError> for DecoderError {
50 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
51}
52
3ba71779 53/// Auxiliary structure for storing data used by decoder but also controlled by the caller.
01613464 54pub struct NADecoderSupport {
3ba71779 55 /// Frame buffer pool for 8-bit or packed video frames.
01613464 56 pub pool_u8: NAVideoBufferPool<u8>,
3ba71779 57 /// Frame buffer pool for 16-bit video frames.
01613464 58 pub pool_u16: NAVideoBufferPool<u16>,
3ba71779 59 /// Frame buffer pool for 32-bit video frames.
01613464
KS
60 pub pool_u32: NAVideoBufferPool<u32>,
61}
62
63impl NADecoderSupport {
3ba71779 64 /// Constructs a new instance of `NADecoderSupport`.
01613464
KS
65 pub fn new() -> Self {
66 Self {
67 pool_u8: NAVideoBufferPool::new(0),
68 pool_u16: NAVideoBufferPool::new(0),
69 pool_u32: NAVideoBufferPool::new(0),
70 }
71 }
72}
73
e243ceb4
KS
74impl Default for NADecoderSupport {
75 fn default() -> Self { Self::new() }
76}
77
3ba71779 78/// Decoder trait.
77d06de2 79pub trait NADecoder {
3ba71779
KS
80 /// Initialises the decoder.
81 ///
82 /// It takes [`NADecoderSupport`] allocated by the caller and `NACodecInfoRef` provided by demuxer.
83 ///
84 /// [`NADecoderSupport`]: ./struct.NADecoderSupport.html
01613464 85 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()>;
3ba71779 86 /// Decodes a single frame.
01613464 87 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
3ba71779 88 /// Tells decoder to clear internal state (e.g. after error or seeking).
f9be4e75 89 fn flush(&mut self);
77d06de2
KS
90}
91
3ba71779 92/// Decoder information using during creating a decoder for requested codec.
70259941 93#[derive(Clone,Copy)]
2a4130ba 94pub struct DecoderInfo {
3ba71779 95 /// Short decoder name.
5641dccf 96 pub name: &'static str,
3ba71779 97 /// The function that creates a decoder instance.
08a1fab7 98 pub get_decoder: fn () -> Box<dyn NADecoder + Send>,
70259941
KS
99}
100
3ba71779
KS
101/// Structure for registering known decoders.
102///
103/// It is supposed to be filled using `register_all_codecs()` from some decoders crate and then it can be used to create decoders for the requested codecs.
e243ceb4 104#[derive(Default)]
5641dccf
KS
105pub struct RegisteredDecoders {
106 decs: Vec<DecoderInfo>,
107}
108
109impl RegisteredDecoders {
3ba71779 110 /// Constructs a new instance of `RegisteredDecoders`.
5641dccf
KS
111 pub fn new() -> Self {
112 Self { decs: Vec::new() }
113 }
3ba71779 114 /// Adds another decoder to the registry.
5641dccf
KS
115 pub fn add_decoder(&mut self, dec: DecoderInfo) {
116 self.decs.push(dec);
117 }
3ba71779 118 /// Searches for the decoder for the provided name and returns a function for creating it on success.
08a1fab7 119 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<dyn NADecoder + Send>> {
5641dccf
KS
120 for &dec in self.decs.iter() {
121 if dec.name == name {
122 return Some(dec.get_decoder);
123 }
70259941 124 }
5641dccf 125 None
70259941 126 }
3ba71779 127 /// Provides an iterator over currently registered decoders.
d10c9311
KS
128 pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {
129 self.decs.iter()
130 }
70259941 131}