| 1 | #[cfg(feature="decoder_indeo2")] |
| 2 | pub mod indeo2; |
| 3 | |
| 4 | use frame::*; |
| 5 | use std::rc::Rc; |
| 6 | use std::cell::RefCell; |
| 7 | use io::byteio::ByteIOError; |
| 8 | use io::bitreader::BitReaderError; |
| 9 | use io::codebook::CodebookError; |
| 10 | |
| 11 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 12 | #[allow(dead_code)] |
| 13 | pub enum DecoderError { |
| 14 | InvalidData, |
| 15 | ShortData, |
| 16 | MissingReference, |
| 17 | NotImplemented, |
| 18 | Bug, |
| 19 | } |
| 20 | |
| 21 | type DecoderResult<T> = Result<T, DecoderError>; |
| 22 | |
| 23 | impl From<ByteIOError> for DecoderError { |
| 24 | fn from(_: ByteIOError) -> Self { DecoderError::ShortData } |
| 25 | } |
| 26 | |
| 27 | impl From<BitReaderError> for DecoderError { |
| 28 | fn from(e: BitReaderError) -> Self { |
| 29 | match e { |
| 30 | BitReaderError::BitstreamEnd => DecoderError::ShortData, |
| 31 | _ => DecoderError::InvalidData, |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl From<CodebookError> for DecoderError { |
| 37 | fn from(_: CodebookError) -> Self { DecoderError::InvalidData } |
| 38 | } |
| 39 | |
| 40 | #[allow(dead_code)] |
| 41 | struct HAMShuffler { |
| 42 | lastframe: Option<NAFrameRef>, |
| 43 | } |
| 44 | |
| 45 | impl HAMShuffler { |
| 46 | #[allow(dead_code)] |
| 47 | fn new() -> Self { HAMShuffler { lastframe: None } } |
| 48 | #[allow(dead_code)] |
| 49 | fn clear(&mut self) { self.lastframe = None; } |
| 50 | #[allow(dead_code)] |
| 51 | fn add_frame(&mut self, frm: NAFrame) { |
| 52 | self.lastframe = Some(Rc::new(RefCell::new(frm))); |
| 53 | } |
| 54 | #[allow(dead_code)] |
| 55 | fn clone_ref(&mut self) -> Option<NAFrameRef> { |
| 56 | match self.lastframe { |
| 57 | Some(ref frm) => Some(Rc::new(RefCell::new(NAFrame::from_copy(&frm.borrow())))), |
| 58 | None => None, |
| 59 | } |
| 60 | } |
| 61 | #[allow(dead_code)] |
| 62 | fn get_output_frame(&mut self) -> Option<NAFrameRef> { |
| 63 | match self.lastframe { |
| 64 | Some(ref frm) => Some(frm.clone()), |
| 65 | None => None, |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | pub trait NADecoder { |
| 71 | fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>; |
| 72 | fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>; |
| 73 | } |
| 74 | |
| 75 | #[derive(Clone,Copy)] |
| 76 | pub struct DecoderInfo { |
| 77 | name: &'static str, |
| 78 | get_decoder: fn () -> Box<NADecoder>, |
| 79 | } |
| 80 | |
| 81 | const DECODERS: &[DecoderInfo] = &[ |
| 82 | #[cfg(feature="decoder_indeo2")] |
| 83 | DecoderInfo { name: "indeo2", get_decoder: indeo2::get_decoder }, |
| 84 | ]; |
| 85 | |
| 86 | pub fn find_decoder(name: &str) -> Option<fn () -> Box<NADecoder>> { |
| 87 | for &dec in DECODERS { |
| 88 | if dec.name == name { |
| 89 | return Some(dec.get_decoder); |
| 90 | } |
| 91 | } |
| 92 | None |
| 93 | } |