add missing codec descriptions
[nihav.git] / nihav-core / src / codecs / mod.rs
CommitLineData
c3e7a747
KS
1use std::fmt;
2use std::ops::{Add, AddAssign, Sub, SubAssign};
3
4e8b4f31
KS
4pub use crate::frame::*;
5pub use 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
241e56f1
KS
80impl Default for HAMShuffler {
81 fn default() -> Self { Self { lastframe: None } }
82}
83
3f29a2a8 84#[allow(dead_code)]
5641dccf 85pub struct IPShuffler {
3f29a2a8
KS
86 lastframe: Option<NAVideoBuffer<u8>>,
87}
88
89impl IPShuffler {
90 #[allow(dead_code)]
5641dccf 91 pub fn new() -> Self { IPShuffler { lastframe: None } }
3f29a2a8 92 #[allow(dead_code)]
5641dccf 93 pub fn clear(&mut self) { self.lastframe = None; }
3f29a2a8 94 #[allow(dead_code)]
5641dccf 95 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
3f29a2a8
KS
96 self.lastframe = Some(buf);
97 }
98 #[allow(dead_code)]
5641dccf 99 pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
3f29a2a8
KS
100 if let Some(ref frm) = self.lastframe {
101 Some(frm.clone())
102 } else {
103 None
104 }
32007ad9
KS
105 }
106}
107
241e56f1
KS
108impl Default for IPShuffler {
109 fn default() -> Self { Self { lastframe: None } }
110}
111
32007ad9 112#[allow(dead_code)]
5641dccf 113pub struct IPBShuffler {
32007ad9
KS
114 lastframe: Option<NAVideoBuffer<u8>>,
115 nextframe: Option<NAVideoBuffer<u8>>,
116}
117
118impl IPBShuffler {
119 #[allow(dead_code)]
5641dccf 120 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
32007ad9 121 #[allow(dead_code)]
5641dccf 122 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
32007ad9 123 #[allow(dead_code)]
5641dccf 124 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
32007ad9
KS
125 mem::swap(&mut self.lastframe, &mut self.nextframe);
126 self.lastframe = Some(buf);
127 }
128 #[allow(dead_code)]
5641dccf 129 pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> {
32007ad9
KS
130 if let Some(ref frm) = self.lastframe {
131 Some(frm.clone())
132 } else {
133 None
134 }
135 }
136 #[allow(dead_code)]
5641dccf 137 pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> {
32007ad9
KS
138 if let Some(ref frm) = self.nextframe {
139 Some(frm.clone())
140 } else {
141 None
142 }
3f29a2a8 143 }
3c8c667d 144 #[allow(dead_code)]
5641dccf 145 pub fn get_b_fwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
3c8c667d
KS
146 if let Some(ref frm) = self.nextframe {
147 Some(frm.clone())
148 } else {
149 None
150 }
151 }
152 #[allow(dead_code)]
5641dccf 153 pub fn get_b_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
3c8c667d
KS
154 if let Some(ref frm) = self.lastframe {
155 Some(frm.clone())
156 } else {
157 None
158 }
159 }
3f29a2a8
KS
160}
161
241e56f1
KS
162impl Default for IPBShuffler {
163 fn default() -> Self { Self { lastframe: None, nextframe: None } }
164}
165
52aad9fe 166#[derive(Debug,Clone,Copy,PartialEq)]
c3e7a747
KS
167pub struct MV {
168 pub x: i16,
169 pub y: i16,
170}
171
172impl 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
207pub const ZERO_MV: MV = MV { x: 0, y: 0 };
208
209impl 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
214impl AddAssign for MV {
215 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
216}
217
218impl 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
223impl SubAssign for MV {
224 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
225}
226
227impl 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
77d06de2
KS
234pub trait NADecoder {
235 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
88c03b61 236 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
77d06de2
KS
237}
238
70259941 239#[derive(Clone,Copy)]
2a4130ba 240pub struct DecoderInfo {
5641dccf
KS
241 pub name: &'static str,
242 pub get_decoder: fn () -> Box<NADecoder>,
70259941
KS
243}
244
5641dccf
KS
245#[cfg(any(feature="h263"))]
246pub mod blockdsp;
88c58b1b 247
88c58b1b 248#[cfg(feature="h263")]
5641dccf 249pub mod h263;
cf64af13 250
5641dccf
KS
251pub struct RegisteredDecoders {
252 decs: Vec<DecoderInfo>,
253}
254
255impl 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 }
70259941 267 }
5641dccf 268 None
70259941 269 }
70259941 270}