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;
9 /// A list specifying general decoding errors.
10 #[derive(Debug,Clone,Copy,PartialEq)]
12 pub enum DecoderError {
13 /// No frame was provided.
15 /// Allocation failed.
17 /// Operation requires repeating.
19 /// Invalid input data was provided.
21 /// Checksum verification failed.
23 /// Provided input turned out to be incomplete.
25 /// Decoder could not decode provided frame because it references some missing previous frame.
27 /// Feature is not implemented.
29 /// Some bug in decoder. It should not happen yet it might.
33 /// A specialised `Result` type for decoding operations.
34 pub type DecoderResult<T> = Result<T, DecoderError>;
36 impl From<ByteIOError> for DecoderError {
37 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
40 impl From<BitReaderError> for DecoderError {
41 fn from(e: BitReaderError) -> Self {
43 BitReaderError::BitstreamEnd => DecoderError::ShortData,
44 _ => DecoderError::InvalidData,
49 impl From<CodebookError> for DecoderError {
50 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
53 impl From<AllocatorError> for DecoderError {
54 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
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>,
67 impl NADecoderSupport {
68 /// Constructs a new instance of `NADecoderSupport`.
69 pub fn new() -> Self {
71 pool_u8: NAVideoBufferPool::new(0),
72 pool_u16: NAVideoBufferPool::new(0),
73 pool_u32: NAVideoBufferPool::new(0),
78 impl Default for NADecoderSupport {
79 fn default() -> Self { Self::new() }
83 pub trait NADecoder: NAOptionHandler {
84 /// Initialises the decoder.
86 /// It takes [`NADecoderSupport`] allocated by the caller and `NACodecInfoRef` provided by demuxer.
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).
96 /// Decoder information used during creating a decoder for requested codec.
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>,
105 /// Structure for registering known decoders.
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.
109 pub struct RegisteredDecoders {
110 decs: Vec<DecoderInfo>,
113 impl RegisteredDecoders {
114 /// Constructs a new instance of `RegisteredDecoders`.
115 pub fn new() -> Self {
116 Self { decs: Vec::new() }
118 /// Adds another decoder to the registry.
119 pub fn add_decoder(&mut self, dec: DecoderInfo) {
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);
131 /// Provides an iterator over currently registered decoders.
132 pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {
137 /// Multithreaded decoder trait.
138 pub trait NADecoderMT: NAOptionHandler {
139 /// Initialises the decoder.
141 /// It takes [`NADecoderSupport`] allocated by the caller and `NACodecInfoRef` provided by demuxer.
143 /// [`NADecoderSupport`]: ./struct.NADecoderSupport.html
144 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef, nthreads: usize) -> DecoderResult<()>;
145 /// Checks if a new frame can be queued for encoding.
146 fn can_take_input(&mut self) -> bool;
147 /// Queues a frame for decoding.
149 /// Returns flag signalling whether the frame was queued or an error if it occured during the preparation stage.
151 /// Parameter `id` is used to distinguish pictures as the output may be an error code with no timestamp available.
152 fn queue_pkt(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket, id: u32) -> DecoderResult<bool>;
153 /// Checks if some frames are already decoded and waiting to be retrieved.
154 fn has_output(&mut self) -> bool;
155 /// Waits for a frame to be decoded.
157 /// In case there are no decoded frames yet, `None` is returned.
158 /// Otherwise, a decoding result and the input picture ID are returned.
159 fn get_frame(&mut self) -> (DecoderResult<NAFrameRef>, u32);
160 /// Tells decoder to clear internal state (e.g. after error or seeking).
164 /// Decoder information used during creating a multi-threaded decoder for requested codec.
165 #[derive(Clone,Copy)]
166 pub struct MTDecoderInfo {
167 /// Short decoder name.
168 pub name: &'static str,
169 /// The function that creates a multi-threaded decoder instance.
170 pub get_decoder: fn () -> Box<dyn NADecoderMT + Send>,
173 /// Structure for registering known multi-threaded decoders.
175 /// It is supposed to be filled using `register_all_mt_decoders()` from some decoders crate and then it can be used to create multi-threaded decoders for the requested codecs.
177 pub struct RegisteredMTDecoders {
178 decs: Vec<MTDecoderInfo>,
181 impl RegisteredMTDecoders {
182 /// Constructs a new instance of `RegisteredMTDecoders`.
183 pub fn new() -> Self {
184 Self { decs: Vec::new() }
186 /// Adds another decoder to the registry.
187 pub fn add_decoder(&mut self, dec: MTDecoderInfo) {
190 /// Searches for the decoder for the provided name and returns a function for creating it on success.
191 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<dyn NADecoderMT + Send>> {
192 for &dec in self.decs.iter() {
193 if dec.name == name {
194 return Some(dec.get_decoder);
199 /// Provides an iterator over currently registered decoders.
200 pub fn iter(&self) -> std::slice::Iter<MTDecoderInfo> {
205 /// Frame skipping mode for decoders.
206 #[derive(Clone,Copy,PartialEq,Debug,Default)]
207 pub enum FrameSkipMode {
208 /// Decode all frames.
211 /// Decode all key frames.
213 /// Decode only intra frames.
217 impl FromStr for FrameSkipMode {
218 type Err = DecoderError;
220 fn from_str(s: &str) -> Result<Self, Self::Err> {
222 FRAME_SKIP_OPTION_VAL_NONE => Ok(FrameSkipMode::None),
223 FRAME_SKIP_OPTION_VAL_KEYFRAME => Ok(FrameSkipMode::KeyframesOnly),
224 FRAME_SKIP_OPTION_VAL_INTRA => Ok(FrameSkipMode::IntraOnly),
225 _ => Err(DecoderError::InvalidData),
230 impl ToString for FrameSkipMode {
231 fn to_string(&self) -> String {
233 FrameSkipMode::None => FRAME_SKIP_OPTION_VAL_NONE.to_string(),
234 FrameSkipMode::KeyframesOnly => FRAME_SKIP_OPTION_VAL_KEYFRAME.to_string(),
235 FrameSkipMode::IntraOnly => FRAME_SKIP_OPTION_VAL_INTRA.to_string(),
240 /// A list specifying general encoding errors.
241 #[derive(Debug,Clone,Copy,PartialEq)]
243 pub enum EncoderError {
244 /// No frame was provided.
246 /// Allocation failed.
248 /// Operation requires repeating.
250 /// Input format is not supported by codec.
252 /// Invalid input parameters were provided.
254 /// Feature is not implemented.
256 /// Some bug in encoder. It should not happen yet it might.
260 /// A specialised `Result` type for decoding operations.
261 pub type EncoderResult<T> = Result<T, EncoderError>;
263 impl From<ByteIOError> for EncoderError {
264 fn from(_: ByteIOError) -> Self { EncoderError::Bug }
267 impl From<AllocatorError> for EncoderError {
268 fn from(_: AllocatorError) -> Self { EncoderError::AllocError }
271 /// Encoding parameter flag to force constant bitrate mode.
272 pub const ENC_MODE_CBR: u64 = 1 << 0;
273 /// Encoding parameter flag to force constant framerate mode.
274 pub const ENC_MODE_CFR: u64 = 1 << 1;
276 /// Encoder supports constant bitrate mode.
277 pub const ENC_CAPS_CBR: u64 = 1 << 0;
278 /// Encoder supports skip frames.
279 pub const ENC_CAPS_SKIPFRAME: u64 = 1 << 1;
280 /// Encoder supports mid-stream parameters change.
281 pub const ENC_CAPS_PARAMCHANGE: u64 = 1 << 2;
283 /// Encoding parameters.
284 #[derive(Clone,Copy,PartialEq)]
285 pub struct EncodeParameters {
287 pub format: NACodecTypeInfo,
288 /// Time base numerator.
290 /// Audio encoders generally do not need it but some may use it to set e.g. frame length, so set it to the video/container codec timebase in such case.
292 /// Time base denominator.
294 /// Audio encoders generally do not need it but some may use it to set e.g. frame length, so set it to the video/container codec timebase in such case.
296 /// Bitrate in bits per second.
298 /// A collection of various boolean encoder settings like CBR mode.
300 /// See `ENC_MODE_*` constants for available options.
302 /// Encoding quality.
306 impl Default for EncodeParameters {
307 fn default() -> EncodeParameters {
309 format: NACodecTypeInfo::None,
321 /// Overall encoding is more complex than decoding.
322 /// There are at least two issues that should be addressed: input format and the need for lookahead.
324 /// Some formats (like MPEG-1 ones) have fixed picture dimensions and framerate, or sampling rate.
325 /// Some formats accept only pictures with dimensions being multiple of eight or sixteen.
326 /// Some audio formats work only with monaural sound.
327 /// 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.
328 /// That is why `NAEncoder` has [`negotiate_format`] function that performs such check and returns what encoder can handle.
330 /// 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.
331 /// That is why encoder has two functions, one for queueing frames for encoding and one for obtaining encoded packets when they are available.
332 /// 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.
334 /// Overall encoding loop should look like this:
336 /// let encoder = ...; // create encoder
337 /// let enc_params = encoder.negotiate_format(input_enc_params)?; // negotiate format
338 /// let output_stream = encoder.init(stream_no, enc_params)?;
339 /// while let Some(frame) = queue.get_frame() {
340 /// // convert to the format encoder expects if required
341 /// encoder.encode(frame)?;
342 /// while let Ok(enc_pkt) = encoder.get_packet()? {
343 /// // send encoded packet to a muxer for example
346 /// // retrieve the rest of encoded packets
347 /// encoder.flush()?;
348 /// while let Ok(enc_pkt) = encoder.get_packet()? {
349 /// // send encoded packet to a muxer for example
353 /// [`negotiate_format`]: ./trait.NAEncoder.html#tymethod.negotiate_format
354 /// [`encode`]: ./trait.NAEncoder.html#tymethod.encode
355 /// [`get_packet`]: ./trait.NAEncoder.html#tymethod.get_packet
356 pub trait NAEncoder: NAOptionHandler {
357 /// Tries to negotiate input format.
359 /// This function takes input encoding parameters and returns adjusted encoding parameters if input ones make sense.
360 /// If input parameters are empty then the default parameters are returned.
364 /// let enc_params = [ EncodeParameters {...}, ..., EncodeParameters::default() ];
365 /// let mut target_params = EncodeParameters::default();
366 /// for params in enc_params.iter() {
367 /// if let Ok(dparams) = encoder.negotiate_format(params) {
368 /// target_params = dparams;
372 /// // since negotiate_format(EncodeParameters::default()) will return a valid format, target_params should be valid here
373 /// let stream = encoder.init(stream_id, target_params)?;
374 /// // convert input into format defined in target_params, feed to the encoder, ...
376 fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters>;
377 /// Queries encoder capabilities.
379 /// See `ENC_CAPS_*` for examples.
380 fn get_capabilities(&self) -> u64;
381 /// Initialises the encoder.
382 fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef>;
383 /// Takes a single frame for encoding.
384 fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()>;
385 /// Returns encoded packet if available.
386 fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>>;
387 /// Tells encoder to encode all data it currently has.
388 fn flush(&mut self) -> EncoderResult<()>;
391 /// Encoder information used during creating an encoder for requested codec.
392 #[derive(Clone,Copy)]
393 pub struct EncoderInfo {
394 /// Short encoder name.
395 pub name: &'static str,
396 /// The function that creates an encoder instance.
397 pub get_encoder: fn () -> Box<dyn NAEncoder + Send>,
400 /// Structure for registering known encoders.
402 /// 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.
404 pub struct RegisteredEncoders {
405 encs: Vec<EncoderInfo>,
408 impl RegisteredEncoders {
409 /// Constructs a new instance of `RegisteredEncoders`.
410 pub fn new() -> Self {
411 Self { encs: Vec::new() }
413 /// Adds another encoder to the registry.
414 pub fn add_encoder(&mut self, enc: EncoderInfo) {
417 /// Searches for the encoder for the provided name and returns a function for creating it on success.
418 pub fn find_encoder(&self, name: &str) -> Option<fn () -> Box<dyn NAEncoder + Send>> {
419 for &enc in self.encs.iter() {
420 if enc.name == name {
421 return Some(enc.get_encoder);
426 /// Provides an iterator over currently registered encoders.
427 pub fn iter(&self) -> std::slice::Iter<EncoderInfo> {
432 /// Trait for packetisers (objects that form full packets from raw stream data).
433 pub trait NAPacketiser {
434 /// Provides the reference stream from the demuxer to the packetiser.
436 /// This may be useful in cases when packetiser cannot determine stream parameters by itself.
437 fn attach_stream(&mut self, stream: NAStreamRef);
438 /// Queues new raw stream data for parsing.
440 /// Returns false is the internal buffer grows too large.
441 fn add_data(&mut self, src: &[u8]) -> bool;
442 /// Tries to retrieve stream information from the data.
444 /// Returns [`NAStream`] reference on success (with stream ID set to `id`), [`ShortData`] when there is not enough data to parse the headers, [`MissingReference`] when stream parsing is not possible without reference information provided by [`attach_stream`] and other errors in case there was an error parsing the data.
446 /// [`NAStream`]: ../frame/struct.NAStream.html
447 /// [`ShortData`]: ./enum.DecoderError.html#variant.ShortData
448 /// [`MissingReference`]: ./enum.DecoderError.html#variant.MissingReference
449 /// [`attach_stream`]: ./trait.NAPacketiser.html#tymethod.attach_stream
450 fn parse_stream(&mut self, id: u32) -> DecoderResult<NAStreamRef>;
451 /// Tries to discard junk data until the first possible packet header.
453 /// Returns the number of bytes skipped.
454 fn skip_junk(&mut self) -> DecoderResult<usize>;
455 /// Tries to form full packet from the already queued data.
457 /// The function should be called repeatedly until it returns nothing or an error.
458 fn get_packet(&mut self, stream: NAStreamRef) -> DecoderResult<Option<NAPacket>>;
459 /// Resets the internal buffer.
461 /// Tells how much data is left in the internal buffer.
462 fn bytes_left(&self) -> usize;
465 /// Decoder information used during creating a packetiser for requested codec.
466 #[derive(Clone,Copy)]
467 pub struct PacketiserInfo {
468 /// Short packetiser name.
469 pub name: &'static str,
470 /// The function that creates a packetiser instance.
471 pub get_packetiser: fn () -> Box<dyn NAPacketiser + Send>,
474 /// Structure for registering known packetisers.
476 /// 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.
478 pub struct RegisteredPacketisers {
479 packs: Vec<PacketiserInfo>,
482 impl RegisteredPacketisers {
483 /// Constructs a new instance of `RegisteredPacketisers`.
484 pub fn new() -> Self {
485 Self { packs: Vec::new() }
487 /// Adds another packetiser to the registry.
488 pub fn add_packetiser(&mut self, pack: PacketiserInfo) {
489 self.packs.push(pack);
491 /// Searches for the packetiser for the provided name and returns a function for creating it on success.
492 pub fn find_packetiser(&self, name: &str) -> Option<fn () -> Box<dyn NAPacketiser + Send>> {
493 for &pack in self.packs.iter() {
494 if pack.name == name {
495 return Some(pack.get_packetiser);
500 /// Provides an iterator over currently registered packetiser.
501 pub fn iter(&self) -> std::slice::Iter<PacketiserInfo> {