2 use std::ops::{Add, AddAssign, Sub, SubAssign};
7 use crate::io::byteio::ByteIOError;
8 use crate::io::bitreader::BitReaderError;
9 use crate::io::codebook::CodebookError;
11 #[derive(Debug,Clone,Copy,PartialEq)]
13 pub enum DecoderError {
24 pub type DecoderResult<T> = Result<T, DecoderError>;
26 impl From<ByteIOError> for DecoderError {
27 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
30 impl From<BitReaderError> for DecoderError {
31 fn from(e: BitReaderError) -> Self {
33 BitReaderError::BitstreamEnd => DecoderError::ShortData,
34 _ => DecoderError::InvalidData,
39 impl From<CodebookError> for DecoderError {
40 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
43 impl From<AllocatorError> for DecoderError {
44 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
48 pub struct HAMShuffler {
49 lastframe: Option<NAVideoBuffer<u8>>,
54 pub fn new() -> Self { HAMShuffler { lastframe: None } }
56 pub fn clear(&mut self) { self.lastframe = None; }
58 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
59 self.lastframe = Some(buf);
62 pub fn clone_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
63 if let Some(ref mut frm) = self.lastframe {
64 let newfrm = frm.copy_buffer();
65 *frm = newfrm.clone();
72 pub fn get_output_frame(&mut self) -> Option<NAVideoBuffer<u8>> {
73 match self.lastframe {
74 Some(ref frm) => Some(frm.clone()),
81 pub struct IPShuffler {
82 lastframe: Option<NAVideoBuffer<u8>>,
87 pub fn new() -> Self { IPShuffler { lastframe: None } }
89 pub fn clear(&mut self) { self.lastframe = None; }
91 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
92 self.lastframe = Some(buf);
95 pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
96 if let Some(ref frm) = self.lastframe {
105 pub struct IPBShuffler {
106 lastframe: Option<NAVideoBuffer<u8>>,
107 nextframe: Option<NAVideoBuffer<u8>>,
112 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
114 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
116 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
117 mem::swap(&mut self.lastframe, &mut self.nextframe);
118 self.lastframe = Some(buf);
121 pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> {
122 if let Some(ref frm) = self.lastframe {
129 pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> {
130 if let Some(ref frm) = self.nextframe {
137 pub fn get_b_fwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
138 if let Some(ref frm) = self.nextframe {
145 pub fn get_b_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
146 if let Some(ref frm) = self.lastframe {
154 #[derive(Debug,Clone,Copy,PartialEq)]
161 pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } }
162 pub fn pred(a: MV, b: MV, c: MV) -> Self {
168 if a.x < c.x { x = c.x; } else { x = a.x; }
172 if a.x < c.x { x = a.x; } else { x = c.x; }
182 if a.y < c.y { y = c.y; } else { y = a.y; }
186 if a.y < c.y { y = a.y; } else { y = c.y; }
195 pub const ZERO_MV: MV = MV { x: 0, y: 0 };
199 fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
202 impl AddAssign for MV {
203 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
208 fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
211 impl SubAssign for MV {
212 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
215 impl fmt::Display for MV {
216 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
217 write!(f, "{},{}", self.x, self.y)
222 pub trait NADecoder {
223 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
224 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
227 #[derive(Clone,Copy)]
228 pub struct DecoderInfo {
229 pub name: &'static str,
230 pub get_decoder: fn () -> Box<NADecoder>,
233 #[cfg(any(feature="h263"))]
236 #[cfg(feature="h263")]
239 pub struct RegisteredDecoders {
240 decs: Vec<DecoderInfo>,
243 impl RegisteredDecoders {
244 pub fn new() -> Self {
245 Self { decs: Vec::new() }
247 pub fn add_decoder(&mut self, dec: DecoderInfo) {
250 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<NADecoder>> {
251 for &dec in self.decs.iter() {
252 if dec.name == name {
253 return Some(dec.get_decoder);