core: make blockdsp an independent feature
[nihav.git] / nihav-core / src / codecs / mod.rs
CommitLineData
c3e7a747
KS
1use std::fmt;
2use std::ops::{Add, AddAssign, Sub, SubAssign};
3
4e8b4f31 4pub use crate::frame::*;
32007ad9 5use std::mem;
aca89041
KS
6use crate::io::byteio::ByteIOError;
7use crate::io::bitreader::BitReaderError;
8use crate::io::codebook::CodebookError;
77d06de2
KS
9
10#[derive(Debug,Clone,Copy,PartialEq)]
11#[allow(dead_code)]
12pub enum DecoderError {
6d3bb0b2 13 NoFrame,
e35062e7 14 AllocError,
503374e7 15 TryAgain,
77d06de2
KS
16 InvalidData,
17 ShortData,
18 MissingReference,
19 NotImplemented,
20 Bug,
21}
22
cf64af13 23pub type DecoderResult<T> = Result<T, DecoderError>;
77d06de2
KS
24
25impl From<ByteIOError> for DecoderError {
26 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
27}
28
29impl From<BitReaderError> for DecoderError {
30 fn from(e: BitReaderError) -> Self {
31 match e {
32 BitReaderError::BitstreamEnd => DecoderError::ShortData,
33 _ => DecoderError::InvalidData,
34 }
35 }
36}
37
38impl From<CodebookError> for DecoderError {
39 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
40}
41
e35062e7
KS
42impl From<AllocatorError> for DecoderError {
43 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
44}
45
88c03b61 46#[allow(dead_code)]
5641dccf 47pub struct HAMShuffler {
3fc28ece 48 lastframe: Option<NAVideoBufferRef<u8>>,
88c03b61
KS
49}
50
51impl HAMShuffler {
52 #[allow(dead_code)]
5641dccf 53 pub fn new() -> Self { HAMShuffler { lastframe: None } }
88c03b61 54 #[allow(dead_code)]
5641dccf 55 pub fn clear(&mut self) { self.lastframe = None; }
88c03b61 56 #[allow(dead_code)]
3fc28ece 57 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
22cb00db 58 self.lastframe = Some(buf);
88c03b61
KS
59 }
60 #[allow(dead_code)]
3fc28ece 61 pub fn clone_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
3bd541c8 62 if let Some(ref mut frm) = self.lastframe {
22cb00db 63 let newfrm = frm.copy_buffer();
3fc28ece
KS
64 *frm = newfrm.clone().into_ref();
65 Some(newfrm.into_ref())
5c253468
KS
66 } else {
67 None
68 }
88c03b61
KS
69 }
70 #[allow(dead_code)]
3fc28ece 71 pub fn get_output_frame(&mut self) -> Option<NAVideoBufferRef<u8>> {
88c03b61
KS
72 match self.lastframe {
73 Some(ref frm) => Some(frm.clone()),
74 None => None,
75 }
76 }
77}
78
241e56f1
KS
79impl Default for HAMShuffler {
80 fn default() -> Self { Self { lastframe: None } }
81}
82
3f29a2a8 83#[allow(dead_code)]
5641dccf 84pub struct IPShuffler {
3fc28ece 85 lastframe: Option<NAVideoBufferRef<u8>>,
3f29a2a8
KS
86}
87
88impl IPShuffler {
89 #[allow(dead_code)]
5641dccf 90 pub fn new() -> Self { IPShuffler { lastframe: None } }
3f29a2a8 91 #[allow(dead_code)]
5641dccf 92 pub fn clear(&mut self) { self.lastframe = None; }
3f29a2a8 93 #[allow(dead_code)]
3fc28ece 94 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
3f29a2a8
KS
95 self.lastframe = Some(buf);
96 }
97 #[allow(dead_code)]
3fc28ece 98 pub fn get_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
3f29a2a8
KS
99 if let Some(ref frm) = self.lastframe {
100 Some(frm.clone())
101 } else {
102 None
103 }
32007ad9
KS
104 }
105}
106
241e56f1
KS
107impl Default for IPShuffler {
108 fn default() -> Self { Self { lastframe: None } }
109}
110
32007ad9 111#[allow(dead_code)]
5641dccf 112pub struct IPBShuffler {
3fc28ece
KS
113 lastframe: Option<NAVideoBufferRef<u8>>,
114 nextframe: Option<NAVideoBufferRef<u8>>,
32007ad9
KS
115}
116
117impl IPBShuffler {
118 #[allow(dead_code)]
5641dccf 119 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
32007ad9 120 #[allow(dead_code)]
5641dccf 121 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
32007ad9 122 #[allow(dead_code)]
3fc28ece 123 pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
32007ad9
KS
124 mem::swap(&mut self.lastframe, &mut self.nextframe);
125 self.lastframe = Some(buf);
126 }
127 #[allow(dead_code)]
3fc28ece 128 pub fn get_lastref(&mut self) -> Option<NAVideoBufferRef<u8>> {
32007ad9
KS
129 if let Some(ref frm) = self.lastframe {
130 Some(frm.clone())
131 } else {
132 None
133 }
134 }
135 #[allow(dead_code)]
3fc28ece 136 pub fn get_nextref(&mut self) -> Option<NAVideoBufferRef<u8>> {
32007ad9
KS
137 if let Some(ref frm) = self.nextframe {
138 Some(frm.clone())
139 } else {
140 None
141 }
3f29a2a8 142 }
3c8c667d 143 #[allow(dead_code)]
3fc28ece 144 pub fn get_b_fwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
3c8c667d
KS
145 if let Some(ref frm) = self.nextframe {
146 Some(frm.clone())
147 } else {
148 None
149 }
150 }
151 #[allow(dead_code)]
3fc28ece 152 pub fn get_b_bwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
3c8c667d
KS
153 if let Some(ref frm) = self.lastframe {
154 Some(frm.clone())
155 } else {
156 None
157 }
158 }
3f29a2a8
KS
159}
160
241e56f1
KS
161impl Default for IPBShuffler {
162 fn default() -> Self { Self { lastframe: None, nextframe: None } }
163}
164
3a1d1cfa 165#[derive(Debug,Clone,Copy,Default,PartialEq)]
c3e7a747
KS
166pub struct MV {
167 pub x: i16,
168 pub y: i16,
169}
170
e243ceb4
KS
171#[allow(clippy::many_single_char_names)]
172#[allow(clippy::collapsible_if)]
c3e7a747 173impl MV {
e243ceb4 174 pub fn new(x: i16, y: i16) -> Self { MV{ x, y } }
c3e7a747
KS
175 pub fn pred(a: MV, b: MV, c: MV) -> Self {
176 let x;
177 if a.x < b.x {
178 if b.x < c.x {
179 x = b.x;
180 } else {
181 if a.x < c.x { x = c.x; } else { x = a.x; }
182 }
183 } else {
184 if b.x < c.x {
185 if a.x < c.x { x = a.x; } else { x = c.x; }
186 } else {
187 x = b.x;
188 }
189 }
190 let y;
191 if a.y < b.y {
192 if b.y < c.y {
193 y = b.y;
194 } else {
195 if a.y < c.y { y = c.y; } else { y = a.y; }
196 }
197 } else {
198 if b.y < c.y {
199 if a.y < c.y { y = a.y; } else { y = c.y; }
200 } else {
201 y = b.y;
202 }
203 }
e243ceb4 204 MV { x, y }
c3e7a747
KS
205 }
206}
207
208pub const ZERO_MV: MV = MV { x: 0, y: 0 };
209
210impl Add for MV {
211 type Output = MV;
212 fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
213}
214
215impl AddAssign for MV {
216 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
217}
218
219impl Sub for MV {
220 type Output = MV;
221 fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
222}
223
224impl SubAssign for MV {
225 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
226}
227
228impl fmt::Display for MV {
229 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230 write!(f, "{},{}", self.x, self.y)
231 }
232}
233
01613464
KS
234pub struct NADecoderSupport {
235 pub pool_u8: NAVideoBufferPool<u8>,
236 pub pool_u16: NAVideoBufferPool<u16>,
237 pub pool_u32: NAVideoBufferPool<u32>,
238}
239
240impl NADecoderSupport {
241 pub fn new() -> Self {
242 Self {
243 pool_u8: NAVideoBufferPool::new(0),
244 pool_u16: NAVideoBufferPool::new(0),
245 pool_u32: NAVideoBufferPool::new(0),
246 }
247 }
248}
249
e243ceb4
KS
250impl Default for NADecoderSupport {
251 fn default() -> Self { Self::new() }
252}
253
c3e7a747 254
77d06de2 255pub trait NADecoder {
01613464
KS
256 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()>;
257 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
77d06de2
KS
258}
259
70259941 260#[derive(Clone,Copy)]
2a4130ba 261pub struct DecoderInfo {
5641dccf 262 pub name: &'static str,
6011e201 263 pub get_decoder: fn () -> Box<dyn NADecoder>,
70259941
KS
264}
265
01b4e528 266#[cfg(any(feature="blockdsp"))]
5641dccf 267pub mod blockdsp;
88c58b1b 268
88c58b1b 269#[cfg(feature="h263")]
5641dccf 270pub mod h263;
cf64af13 271
e243ceb4 272#[derive(Default)]
5641dccf
KS
273pub struct RegisteredDecoders {
274 decs: Vec<DecoderInfo>,
275}
276
277impl RegisteredDecoders {
278 pub fn new() -> Self {
279 Self { decs: Vec::new() }
280 }
281 pub fn add_decoder(&mut self, dec: DecoderInfo) {
282 self.decs.push(dec);
283 }
6011e201 284 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<dyn NADecoder>> {
5641dccf
KS
285 for &dec in self.decs.iter() {
286 if dec.name == name {
287 return Some(dec.get_decoder);
288 }
70259941 289 }
5641dccf 290 None
70259941 291 }
d10c9311
KS
292 pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {
293 self.decs.iter()
294 }
70259941 295}