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