ignore assets dir
[nihav.git] / nihav-core / src / codecs / mod.rs
CommitLineData
c3e7a747
KS
1use std::fmt;
2use std::ops::{Add, AddAssign, Sub, SubAssign};
3
aca89041 4use crate::frame::*;
88c03b61 5use std::rc::Rc;
32007ad9 6use std::mem;
aca89041
KS
7use crate::io::byteio::ByteIOError;
8use crate::io::bitreader::BitReaderError;
9use crate::io::codebook::CodebookError;
77d06de2
KS
10
11#[derive(Debug,Clone,Copy,PartialEq)]
12#[allow(dead_code)]
13pub 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 24pub type DecoderResult<T> = Result<T, DecoderError>;
77d06de2
KS
25
26impl From<ByteIOError> for DecoderError {
27 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
28}
29
30impl 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
39impl From<CodebookError> for DecoderError {
40 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
41}
42
e35062e7
KS
43impl From<AllocatorError> for DecoderError {
44 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
45}
46
88c03b61 47#[allow(dead_code)]
5641dccf 48pub struct HAMShuffler {
22cb00db 49 lastframe: Option<NAVideoBuffer<u8>>,
88c03b61
KS
50}
51
52impl 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 81pub struct IPShuffler {
3f29a2a8
KS
82 lastframe: Option<NAVideoBuffer<u8>>,
83}
84
85impl 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 105pub struct IPBShuffler {
32007ad9
KS
106 lastframe: Option<NAVideoBuffer<u8>>,
107 nextframe: Option<NAVideoBuffer<u8>>,
108}
109
110impl 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
155pub struct MV {
156 pub x: i16,
157 pub y: i16,
158}
159
160impl 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
195pub const ZERO_MV: MV = MV { x: 0, y: 0 };
196
197impl 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
202impl AddAssign for MV {
203 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
204}
205
206impl 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
211impl SubAssign for MV {
212 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
213}
214
215impl 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
222pub 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 228pub 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"))]
234pub mod blockdsp;
88c58b1b 235
88c58b1b 236#[cfg(feature="h263")]
5641dccf 237pub mod h263;
cf64af13 238
5641dccf
KS
239pub struct RegisteredDecoders {
240 decs: Vec<DecoderInfo>,
241}
242
243impl 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}