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