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