| 1 | use std::fmt; |
| 2 | use std::ops::{Add, AddAssign, Sub, SubAssign}; |
| 3 | |
| 4 | pub use crate::frame::*; |
| 5 | use std::mem; |
| 6 | use crate::io::byteio::ByteIOError; |
| 7 | use crate::io::bitreader::BitReaderError; |
| 8 | use crate::io::codebook::CodebookError; |
| 9 | |
| 10 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 11 | #[allow(dead_code)] |
| 12 | pub enum DecoderError { |
| 13 | NoFrame, |
| 14 | AllocError, |
| 15 | TryAgain, |
| 16 | InvalidData, |
| 17 | ShortData, |
| 18 | MissingReference, |
| 19 | NotImplemented, |
| 20 | Bug, |
| 21 | } |
| 22 | |
| 23 | pub type DecoderResult<T> = Result<T, DecoderError>; |
| 24 | |
| 25 | impl From<ByteIOError> for DecoderError { |
| 26 | fn from(_: ByteIOError) -> Self { DecoderError::ShortData } |
| 27 | } |
| 28 | |
| 29 | impl From<BitReaderError> for DecoderError { |
| 30 | fn from(e: BitReaderError) -> Self { |
| 31 | match e { |
| 32 | BitReaderError::BitstreamEnd => DecoderError::ShortData, |
| 33 | _ => DecoderError::InvalidData, |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl From<CodebookError> for DecoderError { |
| 39 | fn from(_: CodebookError) -> Self { DecoderError::InvalidData } |
| 40 | } |
| 41 | |
| 42 | impl From<AllocatorError> for DecoderError { |
| 43 | fn from(_: AllocatorError) -> Self { DecoderError::AllocError } |
| 44 | } |
| 45 | |
| 46 | #[allow(dead_code)] |
| 47 | pub struct HAMShuffler { |
| 48 | lastframe: Option<NAVideoBuffer<u8>>, |
| 49 | } |
| 50 | |
| 51 | impl HAMShuffler { |
| 52 | #[allow(dead_code)] |
| 53 | pub fn new() -> Self { HAMShuffler { lastframe: None } } |
| 54 | #[allow(dead_code)] |
| 55 | pub fn clear(&mut self) { self.lastframe = None; } |
| 56 | #[allow(dead_code)] |
| 57 | pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) { |
| 58 | self.lastframe = Some(buf); |
| 59 | } |
| 60 | #[allow(dead_code)] |
| 61 | pub fn clone_ref(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 62 | if let Some(ref mut frm) = self.lastframe { |
| 63 | let newfrm = frm.copy_buffer(); |
| 64 | *frm = newfrm.clone(); |
| 65 | Some(newfrm) |
| 66 | } else { |
| 67 | None |
| 68 | } |
| 69 | } |
| 70 | #[allow(dead_code)] |
| 71 | pub fn get_output_frame(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 72 | match self.lastframe { |
| 73 | Some(ref frm) => Some(frm.clone()), |
| 74 | None => None, |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Default for HAMShuffler { |
| 80 | fn default() -> Self { Self { lastframe: None } } |
| 81 | } |
| 82 | |
| 83 | #[allow(dead_code)] |
| 84 | pub struct IPShuffler { |
| 85 | lastframe: Option<NAVideoBuffer<u8>>, |
| 86 | } |
| 87 | |
| 88 | impl IPShuffler { |
| 89 | #[allow(dead_code)] |
| 90 | pub fn new() -> Self { IPShuffler { lastframe: None } } |
| 91 | #[allow(dead_code)] |
| 92 | pub fn clear(&mut self) { self.lastframe = None; } |
| 93 | #[allow(dead_code)] |
| 94 | pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) { |
| 95 | self.lastframe = Some(buf); |
| 96 | } |
| 97 | #[allow(dead_code)] |
| 98 | pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 99 | if let Some(ref frm) = self.lastframe { |
| 100 | Some(frm.clone()) |
| 101 | } else { |
| 102 | None |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl Default for IPShuffler { |
| 108 | fn default() -> Self { Self { lastframe: None } } |
| 109 | } |
| 110 | |
| 111 | #[allow(dead_code)] |
| 112 | pub struct IPBShuffler { |
| 113 | lastframe: Option<NAVideoBuffer<u8>>, |
| 114 | nextframe: Option<NAVideoBuffer<u8>>, |
| 115 | } |
| 116 | |
| 117 | impl IPBShuffler { |
| 118 | #[allow(dead_code)] |
| 119 | pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } } |
| 120 | #[allow(dead_code)] |
| 121 | pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; } |
| 122 | #[allow(dead_code)] |
| 123 | pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) { |
| 124 | mem::swap(&mut self.lastframe, &mut self.nextframe); |
| 125 | self.lastframe = Some(buf); |
| 126 | } |
| 127 | #[allow(dead_code)] |
| 128 | pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 129 | if let Some(ref frm) = self.lastframe { |
| 130 | Some(frm.clone()) |
| 131 | } else { |
| 132 | None |
| 133 | } |
| 134 | } |
| 135 | #[allow(dead_code)] |
| 136 | pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 137 | if let Some(ref frm) = self.nextframe { |
| 138 | Some(frm.clone()) |
| 139 | } else { |
| 140 | None |
| 141 | } |
| 142 | } |
| 143 | #[allow(dead_code)] |
| 144 | pub fn get_b_fwdref(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 145 | if let Some(ref frm) = self.nextframe { |
| 146 | Some(frm.clone()) |
| 147 | } else { |
| 148 | None |
| 149 | } |
| 150 | } |
| 151 | #[allow(dead_code)] |
| 152 | pub fn get_b_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> { |
| 153 | if let Some(ref frm) = self.lastframe { |
| 154 | Some(frm.clone()) |
| 155 | } else { |
| 156 | None |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl Default for IPBShuffler { |
| 162 | fn default() -> Self { Self { lastframe: None, nextframe: None } } |
| 163 | } |
| 164 | |
| 165 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 166 | pub struct MV { |
| 167 | pub x: i16, |
| 168 | pub y: i16, |
| 169 | } |
| 170 | |
| 171 | impl MV { |
| 172 | pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } } |
| 173 | pub fn pred(a: MV, b: MV, c: MV) -> Self { |
| 174 | let x; |
| 175 | if a.x < b.x { |
| 176 | if b.x < c.x { |
| 177 | x = b.x; |
| 178 | } else { |
| 179 | if a.x < c.x { x = c.x; } else { x = a.x; } |
| 180 | } |
| 181 | } else { |
| 182 | if b.x < c.x { |
| 183 | if a.x < c.x { x = a.x; } else { x = c.x; } |
| 184 | } else { |
| 185 | x = b.x; |
| 186 | } |
| 187 | } |
| 188 | let y; |
| 189 | if a.y < b.y { |
| 190 | if b.y < c.y { |
| 191 | y = b.y; |
| 192 | } else { |
| 193 | if a.y < c.y { y = c.y; } else { y = a.y; } |
| 194 | } |
| 195 | } else { |
| 196 | if b.y < c.y { |
| 197 | if a.y < c.y { y = a.y; } else { y = c.y; } |
| 198 | } else { |
| 199 | y = b.y; |
| 200 | } |
| 201 | } |
| 202 | MV { x: x, y: y } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | pub const ZERO_MV: MV = MV { x: 0, y: 0 }; |
| 207 | |
| 208 | impl Add for MV { |
| 209 | type Output = MV; |
| 210 | fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } } |
| 211 | } |
| 212 | |
| 213 | impl AddAssign for MV { |
| 214 | fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; } |
| 215 | } |
| 216 | |
| 217 | impl Sub for MV { |
| 218 | type Output = MV; |
| 219 | fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } } |
| 220 | } |
| 221 | |
| 222 | impl SubAssign for MV { |
| 223 | fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; } |
| 224 | } |
| 225 | |
| 226 | impl fmt::Display for MV { |
| 227 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 228 | write!(f, "{},{}", self.x, self.y) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | pub trait NADecoder { |
| 234 | fn init(&mut self, info: NACodecInfoRef) -> DecoderResult<()>; |
| 235 | fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>; |
| 236 | } |
| 237 | |
| 238 | #[derive(Clone,Copy)] |
| 239 | pub struct DecoderInfo { |
| 240 | pub name: &'static str, |
| 241 | pub get_decoder: fn () -> Box<NADecoder>, |
| 242 | } |
| 243 | |
| 244 | #[cfg(any(feature="h263"))] |
| 245 | pub mod blockdsp; |
| 246 | |
| 247 | #[cfg(feature="h263")] |
| 248 | pub mod h263; |
| 249 | |
| 250 | pub struct RegisteredDecoders { |
| 251 | decs: Vec<DecoderInfo>, |
| 252 | } |
| 253 | |
| 254 | impl RegisteredDecoders { |
| 255 | pub fn new() -> Self { |
| 256 | Self { decs: Vec::new() } |
| 257 | } |
| 258 | pub fn add_decoder(&mut self, dec: DecoderInfo) { |
| 259 | self.decs.push(dec); |
| 260 | } |
| 261 | pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<NADecoder>> { |
| 262 | for &dec in self.decs.iter() { |
| 263 | if dec.name == name { |
| 264 | return Some(dec.get_decoder); |
| 265 | } |
| 266 | } |
| 267 | None |
| 268 | } |
| 269 | pub fn iter(&self) -> std::slice::Iter<DecoderInfo> { |
| 270 | self.decs.iter() |
| 271 | } |
| 272 | } |