2 use std::ops::{Add, AddAssign, Sub, SubAssign};
4 pub use crate::frame::*;
6 use crate::io::byteio::ByteIOError;
7 use crate::io::bitreader::BitReaderError;
8 use crate::io::codebook::CodebookError;
10 #[derive(Debug,Clone,Copy,PartialEq)]
12 pub enum DecoderError {
23 pub type DecoderResult<T> = Result<T, DecoderError>;
25 impl From<ByteIOError> for DecoderError {
26 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
29 impl From<BitReaderError> for DecoderError {
30 fn from(e: BitReaderError) -> Self {
32 BitReaderError::BitstreamEnd => DecoderError::ShortData,
33 _ => DecoderError::InvalidData,
38 impl From<CodebookError> for DecoderError {
39 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
42 impl From<AllocatorError> for DecoderError {
43 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
47 pub struct HAMShuffler {
48 lastframe: Option<NAVideoBufferRef<u8>>,
53 pub fn new() -> Self { HAMShuffler { lastframe: None } }
55 pub fn clear(&mut self) { self.lastframe = None; }
57 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
58 self.lastframe = Some(buf);
61 pub fn clone_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
62 if let Some(ref mut frm) = self.lastframe {
63 let newfrm = frm.copy_buffer();
64 *frm = newfrm.clone().into_ref();
65 Some(newfrm.into_ref())
71 pub fn get_output_frame(&mut self) -> Option<NAVideoBufferRef<u8>> {
72 match self.lastframe {
73 Some(ref frm) => Some(frm.clone()),
79 impl Default for HAMShuffler {
80 fn default() -> Self { Self { lastframe: None } }
84 pub struct IPShuffler {
85 lastframe: Option<NAVideoBufferRef<u8>>,
90 pub fn new() -> Self { IPShuffler { lastframe: None } }
92 pub fn clear(&mut self) { self.lastframe = None; }
94 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
95 self.lastframe = Some(buf);
98 pub fn get_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
99 if let Some(ref frm) = self.lastframe {
107 impl Default for IPShuffler {
108 fn default() -> Self { Self { lastframe: None } }
112 pub struct IPBShuffler {
113 lastframe: Option<NAVideoBufferRef<u8>>,
114 nextframe: Option<NAVideoBufferRef<u8>>,
119 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
121 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
123 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
124 mem::swap(&mut self.lastframe, &mut self.nextframe);
125 self.lastframe = Some(buf);
128 pub fn get_lastref(&mut self) -> Option<NAVideoBufferRef<u8>> {
129 if let Some(ref frm) = self.lastframe {
136 pub fn get_nextref(&mut self) -> Option<NAVideoBufferRef<u8>> {
137 if let Some(ref frm) = self.nextframe {
144 pub fn get_b_fwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
145 if let Some(ref frm) = self.nextframe {
152 pub fn get_b_bwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
153 if let Some(ref frm) = self.lastframe {
161 impl Default for IPBShuffler {
162 fn default() -> Self { Self { lastframe: None, nextframe: None } }
165 #[derive(Debug,Clone,Copy,Default,PartialEq)]
171 #[allow(clippy::many_single_char_names)]
172 #[allow(clippy::collapsible_if)]
174 pub fn new(x: i16, y: i16) -> Self { MV{ x, y } }
175 pub fn pred(a: MV, b: MV, c: MV) -> Self {
181 if a.x < c.x { x = c.x; } else { x = a.x; }
185 if a.x < c.x { x = a.x; } else { x = c.x; }
195 if a.y < c.y { y = c.y; } else { y = a.y; }
199 if a.y < c.y { y = a.y; } else { y = c.y; }
208 pub const ZERO_MV: MV = MV { x: 0, y: 0 };
212 fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
215 impl AddAssign for MV {
216 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
221 fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
224 impl SubAssign for MV {
225 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
228 impl fmt::Display for MV {
229 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230 write!(f, "{},{}", self.x, self.y)
234 pub struct NADecoderSupport {
235 pub pool_u8: NAVideoBufferPool<u8>,
236 pub pool_u16: NAVideoBufferPool<u16>,
237 pub pool_u32: NAVideoBufferPool<u32>,
240 impl NADecoderSupport {
241 pub fn new() -> Self {
243 pool_u8: NAVideoBufferPool::new(0),
244 pool_u16: NAVideoBufferPool::new(0),
245 pool_u32: NAVideoBufferPool::new(0),
250 impl Default for NADecoderSupport {
251 fn default() -> Self { Self::new() }
255 pub trait NADecoder {
256 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()>;
257 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
260 #[derive(Clone,Copy)]
261 pub struct DecoderInfo {
262 pub name: &'static str,
263 pub get_decoder: fn () -> Box<dyn NADecoder>,
266 #[cfg(any(feature="h263"))]
269 #[cfg(feature="h263")]
273 pub struct RegisteredDecoders {
274 decs: Vec<DecoderInfo>,
277 impl RegisteredDecoders {
278 pub fn new() -> Self {
279 Self { decs: Vec::new() }
281 pub fn add_decoder(&mut self, dec: DecoderInfo) {
284 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<dyn NADecoder>> {
285 for &dec in self.decs.iter() {
286 if dec.name == name {
287 return Some(dec.get_decoder);
292 pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {