decoders framework and sample decoder
[nihav.git] / src / demuxers / mod.rs
CommitLineData
66116504 1#[cfg(feature="demuxer_gdv")]
5869fd63 2pub mod gdv;
66116504 3#[cfg(feature="demuxer_avi")]
ca41f125 4pub mod avi;
5869fd63
KS
5
6use std::fmt;
7use std::rc::Rc;
8use frame::*;
66116504 9use std::collections::HashMap;
5869fd63
KS
10use io::byteio::*;
11
20ef4353 12#[derive(Debug,Clone,Copy)]
5869fd63
KS
13#[allow(dead_code)]
14pub enum StreamType {
15 Video,
16 Audio,
17 Subtitles,
18 Data,
83e603fa 19 None,
5869fd63
KS
20}
21
22impl fmt::Display for StreamType {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 match *self {
25 StreamType::Video => write!(f, "Video"),
26 StreamType::Audio => write!(f, "Audio"),
27 StreamType::Subtitles => write!(f, "Subtitles"),
28 StreamType::Data => write!(f, "Data"),
83e603fa 29 StreamType::None => write!(f, "-"),
5869fd63
KS
30 }
31 }
32}
33
34
35#[allow(dead_code)]
20ef4353 36#[derive(Clone)]
8869d452 37pub struct NAStream {
5869fd63
KS
38 media_type: StreamType,
39 id: u32,
20ef4353 40 num: usize,
8869d452 41 info: Rc<NACodecInfo>,
5869fd63
KS
42}
43
8869d452
KS
44impl NAStream {
45 pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
20ef4353 46 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
5869fd63
KS
47 }
48 pub fn get_id(&self) -> u32 { self.id }
20ef4353
KS
49 pub fn get_num(&self) -> usize { self.num }
50 pub fn set_num(&mut self, num: usize) { self.num = num; }
8869d452 51 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
5869fd63
KS
52}
53
8869d452 54impl fmt::Display for NAStream {
5869fd63 55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83e603fa 56 write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties())
5869fd63
KS
57 }
58}
59
60#[allow(dead_code)]
8869d452
KS
61pub struct NAPacket {
62 stream: Rc<NAStream>,
5869fd63
KS
63 pts: Option<u64>,
64 dts: Option<u64>,
65 duration: Option<u64>,
66 buffer: Rc<Vec<u8>>,
67 keyframe: bool,
68// options: HashMap<String, NAValue<'a>>,
69}
70
8869d452
KS
71impl NAPacket {
72 pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
5869fd63
KS
73// let mut vec: Vec<u8> = Vec::new();
74// vec.resize(size, 0);
75 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
76 }
8869d452 77 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
5869fd63 78 pub fn get_pts(&self) -> Option<u64> { self.pts }
66116504
KS
79 pub fn get_dts(&self) -> Option<u64> { self.dts }
80 pub fn get_duration(&self) -> Option<u64> { self.duration }
81 pub fn is_keyframe(&self) -> bool { self.keyframe }
5869fd63
KS
82 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
83}
84
66116504
KS
85impl Drop for NAPacket {
86 fn drop(&mut self) {}
87}
88
8869d452 89impl fmt::Display for NAPacket {
5869fd63
KS
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
92 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
93 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
94 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
95 if self.keyframe { foo = format!("{} kf", foo); }
96 foo = foo + "]";
97 write!(f, "{}", foo)
98 }
99}
100
101#[derive(Debug)]
102#[allow(dead_code)]
103pub enum DemuxerError {
104 EOF,
105 NoSuchInput,
106 InvalidData,
107 IOError,
108 NotImplemented,
109 MemoryError,
110}
111
112type DemuxerResult<T> = Result<T, DemuxerError>;
113
20ef4353 114pub trait Demux<'a> {
5869fd63 115 fn open(&mut self) -> DemuxerResult<()>;
66116504
KS
116 fn get_num_streams(&self) -> usize;
117 fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>>;
5869fd63
KS
118 fn get_frame(&mut self) -> DemuxerResult<NAPacket>;
119 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
120}
121
8869d452
KS
122pub trait NAPacketReader {
123 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
5869fd63
KS
124 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
125}
126
8869d452
KS
127impl<'a> NAPacketReader for ByteReader<'a> {
128 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
5869fd63
KS
129 let mut buf: Vec<u8> = Vec::with_capacity(size);
130 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
131 buf.resize(size, 0);
132 let res = self.read_buf(buf.as_mut_slice());
133 if let Err(_) = res { return Err(DemuxerError::IOError); }
5869fd63
KS
134 let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
135 Ok(pkt)
136 }
137 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
138 let mut refbuf = pkt.get_buffer();
139 let mut buf = Rc::make_mut(&mut refbuf);
140 let res = self.read_buf(buf.as_mut_slice());
141 if let Err(_) = res { return Err(DemuxerError::IOError); }
5869fd63
KS
142 Ok(())
143 }
144}
145
20ef4353
KS
146pub struct Demuxer {
147 streams: Vec<Rc<NAStream>>,
148}
149
150impl Demuxer {
151 pub fn new() -> Self { Demuxer { streams: Vec::new() } }
152 pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> {
153 let stream_num = self.streams.len();
154 let mut str = stream.clone();
155 str.set_num(stream_num);
156 self.streams.push(Rc::new(str));
157 Some(stream_num)
158 }
159 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
160 if idx < self.streams.len() {
161 Some(self.streams[idx].clone())
162 } else {
163 None
164 }
165 }
166 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
167 for i in 0..self.streams.len() {
168 if self.streams[i].get_id() == id {
169 return Some(self.streams[i].clone());
170 }
171 }
172 None
173 }
66116504 174 pub fn get_num_streams(&self) -> usize { self.streams.len() }
5869fd63
KS
175}
176
177impl From<ByteIOError> for DemuxerError {
178 fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
179}
180
66116504
KS
181pub trait FrameFromPacket {
182 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame;
183 fn fill_timestamps(&mut self, pkt: &NAPacket);
184}
185
186impl FrameFromPacket for NAFrame {
187 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame {
188 NAFrame::new(pkt.pts, pkt.dts, pkt.duration, info, HashMap::new())
189 }
190 fn fill_timestamps(&mut self, pkt: &NAPacket) {
191 self.set_pts(pkt.pts);
192 self.set_dts(pkt.dts);
193 self.set_duration(pkt.duration);
194 }
195}