add interfaces for raw data stream handling
[nihav.git] / nihav-core / src / codecs / mod.rs
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 pub use crate::options::*;
7 pub use std::str::FromStr;
8
9 /// A list specifying general decoding errors.
10 #[derive(Debug,Clone,Copy,PartialEq)]
11 #[allow(dead_code)]
12 pub enum DecoderError {
13 /// No frame was provided.
14 NoFrame,
15 /// Allocation failed.
16 AllocError,
17 /// Operation requires repeating.
18 TryAgain,
19 /// Invalid input data was provided.
20 InvalidData,
21 /// Checksum verification failed.
22 ChecksumError,
23 /// Provided input turned out to be incomplete.
24 ShortData,
25 /// Decoder could not decode provided frame because it references some missing previous frame.
26 MissingReference,
27 /// Feature is not implemented.
28 NotImplemented,
29 /// Some bug in decoder. It should not happen yet it might.
30 Bug,
31 }
32
33 /// A specialised `Result` type for decoding operations.
34 pub type DecoderResult<T> = Result<T, DecoderError>;
35
36 impl From<ByteIOError> for DecoderError {
37 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
38 }
39
40 impl From<BitReaderError> for DecoderError {
41 fn from(e: BitReaderError) -> Self {
42 match e {
43 BitReaderError::BitstreamEnd => DecoderError::ShortData,
44 _ => DecoderError::InvalidData,
45 }
46 }
47 }
48
49 impl From<CodebookError> for DecoderError {
50 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
51 }
52
53 impl From<AllocatorError> for DecoderError {
54 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
55 }
56
57 /// Auxiliary structure for storing data used by decoder but also controlled by the caller.
58 pub struct NADecoderSupport {
59 /// Frame buffer pool for 8-bit or packed video frames.
60 pub pool_u8: NAVideoBufferPool<u8>,
61 /// Frame buffer pool for 16-bit video frames.
62 pub pool_u16: NAVideoBufferPool<u16>,
63 /// Frame buffer pool for 32-bit video frames.
64 pub pool_u32: NAVideoBufferPool<u32>,
65 }
66
67 impl NADecoderSupport {
68 /// Constructs a new instance of `NADecoderSupport`.
69 pub fn new() -> Self {
70 Self {
71 pool_u8: NAVideoBufferPool::new(0),
72 pool_u16: NAVideoBufferPool::new(0),
73 pool_u32: NAVideoBufferPool::new(0),
74 }
75 }
76 }
77
78 impl Default for NADecoderSupport {
79 fn default() -> Self { Self::new() }
80 }
81
82 /// Decoder trait.
83 pub trait NADecoder: NAOptionHandler {
84 /// Initialises the decoder.
85 ///
86 /// It takes [`NADecoderSupport`] allocated by the caller and `NACodecInfoRef` provided by demuxer.
87 ///
88 /// [`NADecoderSupport`]: ./struct.NADecoderSupport.html
89 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()>;
90 /// Decodes a single frame.
91 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
92 /// Tells decoder to clear internal state (e.g. after error or seeking).
93 fn flush(&mut self);
94 }
95
96 /// Decoder information used during creating a decoder for requested codec.
97 #[derive(Clone,Copy)]
98 pub struct DecoderInfo {
99 /// Short decoder name.
100 pub name: &'static str,
101 /// The function that creates a decoder instance.
102 pub get_decoder: fn () -> Box<dyn NADecoder + Send>,
103 }
104
105 /// Structure for registering known decoders.
106 ///
107 /// It is supposed to be filled using `register_all_decoders()` from some decoders crate and then it can be used to create decoders for the requested codecs.
108 #[derive(Default)]
109 pub struct RegisteredDecoders {
110 decs: Vec<DecoderInfo>,
111 }
112
113 impl RegisteredDecoders {
114 /// Constructs a new instance of `RegisteredDecoders`.
115 pub fn new() -> Self {
116 Self { decs: Vec::new() }
117 }
118 /// Adds another decoder to the registry.
119 pub fn add_decoder(&mut self, dec: DecoderInfo) {
120 self.decs.push(dec);
121 }
122 /// Searches for the decoder for the provided name and returns a function for creating it on success.
123 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<dyn NADecoder + Send>> {
124 for &dec in self.decs.iter() {
125 if dec.name == name {
126 return Some(dec.get_decoder);
127 }
128 }
129 None
130 }
131 /// Provides an iterator over currently registered decoders.
132 pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {
133 self.decs.iter()
134 }
135 }
136
137 /// Frame skipping mode for decoders.
138 #[derive(Clone,Copy,PartialEq,Debug)]
139 pub enum FrameSkipMode {
140 /// Decode all frames.
141 None,
142 /// Decode all key frames.
143 KeyframesOnly,
144 /// Decode only intra frames.
145 IntraOnly,
146 }
147
148 impl Default for FrameSkipMode {
149 fn default() -> Self {
150 FrameSkipMode::None
151 }
152 }
153
154 impl FromStr for FrameSkipMode {
155 type Err = DecoderError;
156
157 fn from_str(s: &str) -> Result<Self, Self::Err> {
158 match s {
159 FRAME_SKIP_OPTION_VAL_NONE => Ok(FrameSkipMode::None),
160 FRAME_SKIP_OPTION_VAL_KEYFRAME => Ok(FrameSkipMode::KeyframesOnly),
161 FRAME_SKIP_OPTION_VAL_INTRA => Ok(FrameSkipMode::IntraOnly),
162 _ => Err(DecoderError::InvalidData),
163 }
164 }
165 }
166
167 impl ToString for FrameSkipMode {
168 fn to_string(&self) -> String {
169 match *self {
170 FrameSkipMode::None => FRAME_SKIP_OPTION_VAL_NONE.to_string(),
171 FrameSkipMode::KeyframesOnly => FRAME_SKIP_OPTION_VAL_KEYFRAME.to_string(),
172 FrameSkipMode::IntraOnly => FRAME_SKIP_OPTION_VAL_INTRA.to_string(),
173 }
174 }
175 }
176
177 /// A list specifying general encoding errors.
178 #[derive(Debug,Clone,Copy,PartialEq)]
179 #[allow(dead_code)]
180 pub enum EncoderError {
181 /// No frame was provided.
182 NoFrame,
183 /// Allocation failed.
184 AllocError,
185 /// Operation requires repeating.
186 TryAgain,
187 /// Input format is not supported by codec.
188 FormatError,
189 /// Invalid input parameters were provided.
190 InvalidParameters,
191 /// Feature is not implemented.
192 NotImplemented,
193 /// Some bug in encoder. It should not happen yet it might.
194 Bug,
195 }
196
197 /// A specialised `Result` type for decoding operations.
198 pub type EncoderResult<T> = Result<T, EncoderError>;
199
200 impl From<ByteIOError> for EncoderError {
201 fn from(_: ByteIOError) -> Self { EncoderError::Bug }
202 }
203
204 impl From<AllocatorError> for EncoderError {
205 fn from(_: AllocatorError) -> Self { EncoderError::AllocError }
206 }
207
208 /// Encoding parameter flag to force constant bitrate mode.
209 pub const ENC_MODE_CBR: u64 = 1 << 0;
210 /// Encoding parameter flag to force constant framerate mode.
211 pub const ENC_MODE_CFR: u64 = 1 << 1;
212
213 /// Encoding parameters.
214 #[derive(Clone,Copy,PartialEq)]
215 pub struct EncodeParameters {
216 /// Input format.
217 pub format: NACodecTypeInfo,
218 /// Time base numerator. Ignored for audio.
219 pub tb_num: u32,
220 /// Time base denominator. Ignored for audio.
221 pub tb_den: u32,
222 /// Bitrate in kilobits per second.
223 pub bitrate: u32,
224 /// A collection of various boolean encoder settings like CBR mode.
225 ///
226 /// See `ENC_MODE_*` constants for available options.
227 pub flags: u64,
228 /// Encoding quality.
229 pub quality: u8,
230 }
231
232 impl Default for EncodeParameters {
233 fn default() -> EncodeParameters {
234 EncodeParameters {
235 format: NACodecTypeInfo::None,
236 tb_num: 0,
237 tb_den: 0,
238 bitrate: 0,
239 flags: 0,
240 quality: 0,
241 }
242 }
243 }
244
245 /// Encoder trait.
246 ///
247 /// Overall encoding is more complex than decoding.
248 /// There are at least two issues that should be addressed: input format and the need for lookahead.
249 ///
250 /// Some formats (like MPEG-1 ones) have fixed picture dimensions and framerate, or sampling rate.
251 /// Some formats accept only pictures with dimensions being multiple of eight or sixteen.
252 /// Some audio formats work only with monaural sound.
253 /// In order to account for all this user first needs to check whether encoder can handle provided input format as is or some conversion is required.
254 /// That is why `NAEncoder` has [`negotiate_format`] function that performs such check and returns what encoder can handle.
255 ///
256 /// Additionally, encoders for complex formats often need several frames lookahead to encode data efficiently, actual frame encoding may take place only when some frames are accumulated.
257 /// That is why encoder has two functions, one for queueing frames for encoding and one for obtaining encoded packets when they are available.
258 /// In result encoder should first queue a frame for encoding with [`encode`] and then retrieve zero or more encoded packets with [`get_packet`] in a loop.
259 ///
260 /// Overall encoding loop should look like this:
261 /// ```ignore
262 /// let encoder = ...; // create encoder
263 /// let enc_params = encoder.negotiate_format(input_enc_params)?; // negotiate format
264 /// let output_stream = encoder.init(stream_no, enc_params)?;
265 /// while let Some(frame) = queue.get_frame() {
266 /// // convert to the format encoder expects if required
267 /// encoder.encode(frame)?;
268 /// while let Ok(enc_pkt) = encoder.get_packet()? {
269 /// // send encoded packet to a muxer for example
270 /// }
271 /// }
272 /// // retrieve the rest of encoded packets
273 /// encoder.flush()?;
274 /// while let Ok(enc_pkt) = encoder.get_packet()? {
275 /// // send encoded packet to a muxer for example
276 /// }
277 /// ```
278 ///
279 /// [`negotiate_format`]: ./trait.NAEncoder.html#tymethod.negotiate_format
280 /// [`encode`]: ./trait.NAEncoder.html#tymethod.encode
281 /// [`get_packet`]: ./trait.NAEncoder.html#tymethod.get_packet
282 pub trait NAEncoder: NAOptionHandler {
283 /// Tries to negotiate input format.
284 ///
285 /// This function takes input encoding parameters and returns adjusted encoding parameters if input ones make sense.
286 /// If input parameters are empty then the default parameters are returned.
287 ///
288 /// # Example
289 /// ```ignore
290 /// let enc_params = [ EncodeParameters {...}, ..., EncodeParameters::default() ];
291 /// let mut target_params = EncodeParameters::default();
292 /// for params in enc_params.iter() {
293 /// if let Ok(dparams) = encoder.negotiate_format(params) {
294 /// target_params = dparams;
295 /// break;
296 /// }
297 /// }
298 /// // since negotiate_format(EncodeParameters::default()) will return a valid format, target_params should be valid here
299 /// let stream = encoder.init(stream_id, target_params)?;
300 /// // convert input into format defined in target_params, feed to the encoder, ...
301 /// ```
302 fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters>;
303 /// Initialises the encoder.
304 fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef>;
305 /// Takes a single frame for encoding.
306 fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()>;
307 /// Returns encoded packet if available.
308 fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>>;
309 /// Tells encoder to encode all data it currently has.
310 fn flush(&mut self) -> EncoderResult<()>;
311 }
312
313 /// Encoder information used during creating an encoder for requested codec.
314 #[derive(Clone,Copy)]
315 pub struct EncoderInfo {
316 /// Short encoder name.
317 pub name: &'static str,
318 /// The function that creates an encoder instance.
319 pub get_encoder: fn () -> Box<dyn NAEncoder + Send>,
320 }
321
322 /// Structure for registering known encoders.
323 ///
324 /// It is supposed to be filled using `register_all_decoders()` from some encoders crate and then it can be used to create encoders for the requested codecs.
325 #[derive(Default)]
326 pub struct RegisteredEncoders {
327 encs: Vec<EncoderInfo>,
328 }
329
330 impl RegisteredEncoders {
331 /// Constructs a new instance of `RegisteredEncoders`.
332 pub fn new() -> Self {
333 Self { encs: Vec::new() }
334 }
335 /// Adds another encoder to the registry.
336 pub fn add_encoder(&mut self, enc: EncoderInfo) {
337 self.encs.push(enc);
338 }
339 /// Searches for the encoder for the provided name and returns a function for creating it on success.
340 pub fn find_encoder(&self, name: &str) -> Option<fn () -> Box<dyn NAEncoder + Send>> {
341 for &enc in self.encs.iter() {
342 if enc.name == name {
343 return Some(enc.get_encoder);
344 }
345 }
346 None
347 }
348 /// Provides an iterator over currently registered encoders.
349 pub fn iter(&self) -> std::slice::Iter<EncoderInfo> {
350 self.encs.iter()
351 }
352 }
353
354 /// Trait for packetisers (objects that form full packets from raw stream data).
355 pub trait NAPacketiser {
356 /// Queues new raw stream data for parsing.
357 ///
358 /// Returns false is the internal buffer grows too large.
359 fn add_data(&mut self, src: &[u8]) -> bool;
360 /// Tries to retrieve stream information from the data.
361 ///
362 /// Returns [`NAStream`] reference on success (with stream ID set to `id`), [`ShortData`] when there is not enough data to parse the headers and other errors in case there was an error parsing the data.
363 ///
364 /// [`NAStream`]: ../frame/struct.NAStream.html
365 /// [`ShortData`]: ./enum.DecoderError.html#variant.ShortData
366 fn parse_stream(&mut self, id: u32) -> DecoderResult<NAStreamRef>;
367 /// Tries to discard junk data until the first possible packet header.
368 ///
369 /// Returns the number of bytes skipped.
370 fn skip_junk(&mut self) -> DecoderResult<usize>;
371 /// Tries to form full packet from the already queued data.
372 ///
373 /// The function should be called repeatedly until it returns nothing or an error.
374 fn get_packet(&mut self, stream: NAStreamRef) -> DecoderResult<Option<NAPacket>>;
375 /// Resets the internal buffer.
376 fn reset(&mut self);
377 }
378
379 /// Decoder information used during creating a packetiser for requested codec.
380 #[derive(Clone,Copy)]
381 pub struct PacketiserInfo {
382 /// Short packetiser name.
383 pub name: &'static str,
384 /// The function that creates a packetiser instance.
385 pub get_packetiser: fn () -> Box<dyn NAPacketiser + Send>,
386 }
387
388 /// Structure for registering known packetisers.
389 ///
390 /// It is supposed to be filled using `register_all_packetisers()` from some decoders crate and then it can be used to create packetisers for the requested codecs.
391 #[derive(Default)]
392 pub struct RegisteredPacketisers {
393 packs: Vec<PacketiserInfo>,
394 }
395
396 impl RegisteredPacketisers {
397 /// Constructs a new instance of `RegisteredPacketisers`.
398 pub fn new() -> Self {
399 Self { packs: Vec::new() }
400 }
401 /// Adds another packetiser to the registry.
402 pub fn add_packetiser(&mut self, pack: PacketiserInfo) {
403 self.packs.push(pack);
404 }
405 /// Searches for the packetiser for the provided name and returns a function for creating it on success.
406 pub fn find_packetiser(&self, name: &str) -> Option<fn () -> Box<dyn NAPacketiser + Send>> {
407 for &pack in self.packs.iter() {
408 if pack.name == name {
409 return Some(pack.get_packetiser);
410 }
411 }
412 None
413 }
414 /// Provides an iterator over currently registered packetiser.
415 pub fn iter(&self) -> std::slice::Iter<PacketiserInfo> {
416 self.packs.iter()
417 }
418 }
419