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