simplify error handling
[nihav.git] / src / codecs / mod.rs
CommitLineData
77d06de2
KS
1#[cfg(feature="decoder_indeo2")]
2pub mod indeo2;
3
4use std::rc::Rc;
5use frame::*;
77d06de2
KS
6use io::byteio::ByteIOError;
7use io::bitreader::BitReaderError;
8use io::codebook::CodebookError;
9
10#[derive(Debug,Clone,Copy,PartialEq)]
11#[allow(dead_code)]
12pub enum DecoderError {
13 InvalidData,
14 ShortData,
15 MissingReference,
16 NotImplemented,
17 Bug,
18}
19
20type DecoderResult<T> = Result<T, DecoderError>;
21
22impl From<ByteIOError> for DecoderError {
23 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
24}
25
26impl From<BitReaderError> for DecoderError {
27 fn from(e: BitReaderError) -> Self {
28 match e {
29 BitReaderError::BitstreamEnd => DecoderError::ShortData,
30 _ => DecoderError::InvalidData,
31 }
32 }
33}
34
35impl From<CodebookError> for DecoderError {
36 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
37}
38
39pub trait NADecoder {
40 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
41 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<Rc<NAFrame>>;
42}
43
70259941 44#[derive(Clone,Copy)]
2a4130ba 45pub struct DecoderInfo {
70259941
KS
46 name: &'static str,
47 get_decoder: fn () -> Box<NADecoder>,
48}
49
2a4130ba 50const DECODERS: &[DecoderInfo] = &[
70259941 51#[cfg(feature="decoder_indeo2")]
2a4130ba 52 DecoderInfo { name: "indeo2", get_decoder: indeo2::get_decoder },
70259941
KS
53];
54
55pub fn find_decoder(name: &str) -> Option<fn () -> Box<NADecoder>> {
56 for &dec in DECODERS {
57 if dec.name == name {
58 return Some(dec.get_decoder);
59 }
60 }
61 None
62}