re-export some core dependencies
[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 #[allow(dead_code)]
81 pub struct IPShuffler {
82 lastframe: Option<NAVideoBuffer<u8>>,
83 }
84
85 impl IPShuffler {
86 #[allow(dead_code)]
87 pub fn new() -> Self { IPShuffler { lastframe: None } }
88 #[allow(dead_code)]
89 pub fn clear(&mut self) { self.lastframe = None; }
90 #[allow(dead_code)]
91 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
92 self.lastframe = Some(buf);
93 }
94 #[allow(dead_code)]
95 pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
96 if let Some(ref frm) = self.lastframe {
97 Some(frm.clone())
98 } else {
99 None
100 }
101 }
102 }
103
104 #[allow(dead_code)]
105 pub struct IPBShuffler {
106 lastframe: Option<NAVideoBuffer<u8>>,
107 nextframe: Option<NAVideoBuffer<u8>>,
108 }
109
110 impl IPBShuffler {
111 #[allow(dead_code)]
112 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
113 #[allow(dead_code)]
114 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
115 #[allow(dead_code)]
116 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
117 mem::swap(&mut self.lastframe, &mut self.nextframe);
118 self.lastframe = Some(buf);
119 }
120 #[allow(dead_code)]
121 pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> {
122 if let Some(ref frm) = self.lastframe {
123 Some(frm.clone())
124 } else {
125 None
126 }
127 }
128 #[allow(dead_code)]
129 pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> {
130 if let Some(ref frm) = self.nextframe {
131 Some(frm.clone())
132 } else {
133 None
134 }
135 }
136 #[allow(dead_code)]
137 pub fn get_b_fwdref(&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_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
146 if let Some(ref frm) = self.lastframe {
147 Some(frm.clone())
148 } else {
149 None
150 }
151 }
152 }
153
154 #[derive(Debug,Clone,Copy,PartialEq)]
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
222 pub trait NADecoder {
223 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
224 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
225 }
226
227 #[derive(Clone,Copy)]
228 pub struct DecoderInfo {
229 pub name: &'static str,
230 pub get_decoder: fn () -> Box<NADecoder>,
231 }
232
233 #[cfg(any(feature="h263"))]
234 pub mod blockdsp;
235
236 #[cfg(feature="h263")]
237 pub mod h263;
238
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 }
255 }
256 None
257 }
258 }