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