2 use std::ops::{Add, AddAssign, Sub, SubAssign};
4 pub use crate::frame::*;
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()),
80 impl Default for HAMShuffler {
81 fn default() -> Self { Self { lastframe: None } }
85 pub struct IPShuffler {
86 lastframe: Option<NAVideoBuffer<u8>>,
91 pub fn new() -> Self { IPShuffler { lastframe: None } }
93 pub fn clear(&mut self) { self.lastframe = None; }
95 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
96 self.lastframe = Some(buf);
99 pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
100 if let Some(ref frm) = self.lastframe {
108 impl Default for IPShuffler {
109 fn default() -> Self { Self { lastframe: None } }
113 pub struct IPBShuffler {
114 lastframe: Option<NAVideoBuffer<u8>>,
115 nextframe: Option<NAVideoBuffer<u8>>,
120 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
122 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
124 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
125 mem::swap(&mut self.lastframe, &mut self.nextframe);
126 self.lastframe = Some(buf);
129 pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> {
130 if let Some(ref frm) = self.lastframe {
137 pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> {
138 if let Some(ref frm) = self.nextframe {
145 pub fn get_b_fwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
146 if let Some(ref frm) = self.nextframe {
153 pub fn get_b_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
154 if let Some(ref frm) = self.lastframe {
162 impl Default for IPBShuffler {
163 fn default() -> Self { Self { lastframe: None, nextframe: None } }
166 #[derive(Debug,Clone,Copy,PartialEq)]
173 pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } }
174 pub fn pred(a: MV, b: MV, c: MV) -> Self {
180 if a.x < c.x { x = c.x; } else { x = a.x; }
184 if a.x < c.x { x = a.x; } else { x = c.x; }
194 if a.y < c.y { y = c.y; } else { y = a.y; }
198 if a.y < c.y { y = a.y; } else { y = c.y; }
207 pub const ZERO_MV: MV = MV { x: 0, y: 0 };
211 fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
214 impl AddAssign for MV {
215 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
220 fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
223 impl SubAssign for MV {
224 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
227 impl fmt::Display for MV {
228 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
229 write!(f, "{},{}", self.x, self.y)
234 pub trait NADecoder {
235 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
236 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
239 #[derive(Clone,Copy)]
240 pub struct DecoderInfo {
241 pub name: &'static str,
242 pub get_decoder: fn () -> Box<NADecoder>,
245 #[cfg(any(feature="h263"))]
248 #[cfg(feature="h263")]
251 pub struct RegisteredDecoders {
252 decs: Vec<DecoderInfo>,
255 impl RegisteredDecoders {
256 pub fn new() -> Self {
257 Self { decs: Vec::new() }
259 pub fn add_decoder(&mut self, dec: DecoderInfo) {
262 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<NADecoder>> {
263 for &dec in self.decs.iter() {
264 if dec.name == name {
265 return Some(dec.get_decoder);
270 pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {