split NihAV into subcrates
[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
KS
5use std::rc::Rc;
6use std::cell::RefCell;
32007ad9 7use std::mem;
aca89041
KS
8use crate::io::byteio::ByteIOError;
9use crate::io::bitreader::BitReaderError;
10use crate::io::codebook::CodebookError;
77d06de2
KS
11
12#[derive(Debug,Clone,Copy,PartialEq)]
13#[allow(dead_code)]
14pub enum DecoderError {
6d3bb0b2 15 NoFrame,
e35062e7 16 AllocError,
503374e7 17 TryAgain,
77d06de2
KS
18 InvalidData,
19 ShortData,
20 MissingReference,
21 NotImplemented,
22 Bug,
23}
24
cf64af13 25pub type DecoderResult<T> = Result<T, DecoderError>;
77d06de2
KS
26
27impl From<ByteIOError> for DecoderError {
28 fn from(_: ByteIOError) -> Self { DecoderError::ShortData }
29}
30
31impl 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
40impl From<CodebookError> for DecoderError {
41 fn from(_: CodebookError) -> Self { DecoderError::InvalidData }
42}
43
e35062e7
KS
44impl From<AllocatorError> for DecoderError {
45 fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
46}
47
cf64af13 48macro_rules! validate {
2324f30b 49 ($a:expr) => { if !$a { println!("check failed at {}:{}", file!(), line!()); return Err(DecoderError::InvalidData); } };
cf64af13
KS
50}
51
88c03b61 52#[allow(dead_code)]
5641dccf 53pub struct HAMShuffler {
22cb00db 54 lastframe: Option<NAVideoBuffer<u8>>,
88c03b61
KS
55}
56
57impl HAMShuffler {
58 #[allow(dead_code)]
5641dccf 59 pub fn new() -> Self { HAMShuffler { lastframe: None } }
88c03b61 60 #[allow(dead_code)]
5641dccf 61 pub fn clear(&mut self) { self.lastframe = None; }
88c03b61 62 #[allow(dead_code)]
5641dccf 63 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
22cb00db 64 self.lastframe = Some(buf);
88c03b61
KS
65 }
66 #[allow(dead_code)]
5641dccf 67 pub fn clone_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
3bd541c8 68 if let Some(ref mut frm) = self.lastframe {
22cb00db 69 let newfrm = frm.copy_buffer();
3bd541c8
KS
70 *frm = newfrm.clone();
71 Some(newfrm)
5c253468
KS
72 } else {
73 None
74 }
88c03b61
KS
75 }
76 #[allow(dead_code)]
5641dccf 77 pub fn get_output_frame(&mut self) -> Option<NAVideoBuffer<u8>> {
88c03b61
KS
78 match self.lastframe {
79 Some(ref frm) => Some(frm.clone()),
80 None => None,
81 }
82 }
83}
84
3f29a2a8 85#[allow(dead_code)]
5641dccf 86pub struct IPShuffler {
3f29a2a8
KS
87 lastframe: Option<NAVideoBuffer<u8>>,
88}
89
90impl IPShuffler {
91 #[allow(dead_code)]
5641dccf 92 pub fn new() -> Self { IPShuffler { lastframe: None } }
3f29a2a8 93 #[allow(dead_code)]
5641dccf 94 pub fn clear(&mut self) { self.lastframe = None; }
3f29a2a8 95 #[allow(dead_code)]
5641dccf 96 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
3f29a2a8
KS
97 self.lastframe = Some(buf);
98 }
99 #[allow(dead_code)]
5641dccf 100 pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
3f29a2a8
KS
101 if let Some(ref frm) = self.lastframe {
102 Some(frm.clone())
103 } else {
104 None
105 }
32007ad9
KS
106 }
107}
108
109#[allow(dead_code)]
5641dccf 110pub struct IPBShuffler {
32007ad9
KS
111 lastframe: Option<NAVideoBuffer<u8>>,
112 nextframe: Option<NAVideoBuffer<u8>>,
113}
114
115impl IPBShuffler {
116 #[allow(dead_code)]
5641dccf 117 pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
32007ad9 118 #[allow(dead_code)]
5641dccf 119 pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
32007ad9 120 #[allow(dead_code)]
5641dccf 121 pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
32007ad9
KS
122 mem::swap(&mut self.lastframe, &mut self.nextframe);
123 self.lastframe = Some(buf);
124 }
125 #[allow(dead_code)]
5641dccf 126 pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> {
32007ad9
KS
127 if let Some(ref frm) = self.lastframe {
128 Some(frm.clone())
129 } else {
130 None
131 }
132 }
133 #[allow(dead_code)]
5641dccf 134 pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> {
32007ad9
KS
135 if let Some(ref frm) = self.nextframe {
136 Some(frm.clone())
137 } else {
138 None
139 }
3f29a2a8 140 }
3c8c667d 141 #[allow(dead_code)]
5641dccf 142 pub fn get_b_fwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
3c8c667d
KS
143 if let Some(ref frm) = self.nextframe {
144 Some(frm.clone())
145 } else {
146 None
147 }
148 }
149 #[allow(dead_code)]
5641dccf 150 pub fn get_b_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
3c8c667d
KS
151 if let Some(ref frm) = self.lastframe {
152 Some(frm.clone())
153 } else {
154 None
155 }
156 }
3f29a2a8
KS
157}
158
52aad9fe 159#[derive(Debug,Clone,Copy,PartialEq)]
c3e7a747
KS
160pub struct MV {
161 pub x: i16,
162 pub y: i16,
163}
164
165impl 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
200pub const ZERO_MV: MV = MV { x: 0, y: 0 };
201
202impl 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
207impl AddAssign for MV {
208 fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
209}
210
211impl 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
216impl SubAssign for MV {
217 fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
218}
219
220impl 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
77d06de2
KS
227pub trait NADecoder {
228 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
88c03b61 229 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
77d06de2
KS
230}
231
70259941 232#[derive(Clone,Copy)]
2a4130ba 233pub struct DecoderInfo {
5641dccf
KS
234 pub name: &'static str,
235 pub get_decoder: fn () -> Box<NADecoder>,
70259941
KS
236}
237
5641dccf
KS
238#[cfg(any(feature="h263"))]
239pub mod blockdsp;
88c58b1b 240
750b299c
KS
241#[cfg(feature="decoder_clearvideo")]
242mod clearvideo;
e120f8fd
KS
243#[cfg(feature="decoder_gdvvid")]
244mod gremlinvideo;
88c58b1b 245#[cfg(feature="h263")]
5641dccf 246pub mod h263;
cf64af13 247
5a932f64
KS
248#[cfg(feature="decoder_aac")]
249mod aac;
aab478c1
KS
250#[cfg(feature="decoder_atrac3")]
251mod atrac3;
3f29a2a8
KS
252#[cfg(feature="decoder_pcm")]
253mod pcm;
f4bb5ccb
KS
254#[cfg(feature="decoder_sipro")]
255mod sipro;
16dd4f44
KS
256#[cfg(feature="decoder_ts102366")]
257mod ts102366;
3f29a2a8 258
2a4130ba 259const DECODERS: &[DecoderInfo] = &[
750b299c
KS
260#[cfg(feature="decoder_clearvideo")]
261 DecoderInfo { name: "clearvideo", get_decoder: clearvideo::get_decoder },
262#[cfg(feature="decoder_clearvideo")]
263 DecoderInfo { name: "clearvideo_rm", get_decoder: clearvideo::get_decoder_rm },
cf64af13 264
3234da61
KS
265#[cfg(feature="decoder_pcm")]
266 DecoderInfo { name: "pcm", get_decoder: pcm::get_decoder },
f4bb5ccb
KS
267#[cfg(feature="decoder_sipro")]
268 DecoderInfo { name: "sipro", get_decoder: sipro::get_decoder },
16dd4f44
KS
269#[cfg(feature="decoder_ts102366")]
270 DecoderInfo { name: "ac3", get_decoder: ts102366::get_decoder },
aab478c1
KS
271#[cfg(feature="decoder_atrac3")]
272 DecoderInfo { name: "atrac3", get_decoder: atrac3::get_decoder },
5a932f64
KS
273#[cfg(feature="decoder_aac")]
274 DecoderInfo { name: "aac", get_decoder: aac::get_decoder },
70259941
KS
275];
276
5641dccf
KS
277pub fn core_register_all_codecs(rd: &mut RegisteredDecoders) {
278 for decoder in DECODERS.into_iter() {
279 rd.add_decoder(decoder.clone());
280 }
281}
282
283pub struct RegisteredDecoders {
284 decs: Vec<DecoderInfo>,
285}
286
287impl RegisteredDecoders {
288 pub fn new() -> Self {
289 Self { decs: Vec::new() }
290 }
291 pub fn add_decoder(&mut self, dec: DecoderInfo) {
292 self.decs.push(dec);
293 }
294 pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<NADecoder>> {
295 for &dec in self.decs.iter() {
296 if dec.name == name {
297 return Some(dec.get_decoder);
298 }
70259941 299 }
5641dccf 300 None
70259941 301 }
70259941 302}