]> git.nihav.org Git - nihav.git/blob - src/demuxers/mod.rs
rmdec: allow zero DATA and number of packets
[nihav.git] / src / demuxers / mod.rs
1 use std::rc::Rc;
2 use frame::*;
3 use io::byteio::*;
4
5 #[derive(Debug,Clone,Copy,PartialEq)]
6 #[allow(dead_code)]
7 pub enum DemuxerError {
8 EOF,
9 NoSuchInput,
10 InvalidData,
11 IOError,
12 NotImplemented,
13 MemoryError,
14 TryAgain,
15 }
16
17 type DemuxerResult<T> = Result<T, DemuxerError>;
18
19 pub trait DemuxCore<'a> {
20 fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()>;
21 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>;
22 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
23 }
24
25 pub trait NAPacketReader {
26 fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
27 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
28 }
29
30 impl<'a> NAPacketReader for ByteReader<'a> {
31 fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
32 let mut buf: Vec<u8> = Vec::with_capacity(size);
33 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
34 buf.resize(size, 0);
35 let res = self.read_buf(buf.as_mut_slice());
36 if let Err(_) = res { return Err(DemuxerError::IOError); }
37 let pkt = NAPacket::new(str, ts, kf, buf);
38 Ok(pkt)
39 }
40 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
41 let mut refbuf = pkt.get_buffer();
42 let buf = Rc::make_mut(&mut refbuf);
43 let res = self.read_buf(buf.as_mut_slice());
44 if let Err(_) = res { return Err(DemuxerError::IOError); }
45 Ok(())
46 }
47 }
48
49 pub struct StreamManager {
50 streams: Vec<Rc<NAStream>>,
51 ignored: Vec<bool>,
52 no_ign: bool,
53 }
54
55 impl StreamManager {
56 pub fn new() -> Self {
57 StreamManager {
58 streams: Vec::new(),
59 ignored: Vec::new(),
60 no_ign: true,
61 }
62 }
63 pub fn iter(&self) -> StreamIter { StreamIter::new(&self.streams) }
64
65 pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> {
66 let stream_num = self.streams.len();
67 let mut str = stream.clone();
68 str.set_num(stream_num);
69 self.streams.push(Rc::new(str));
70 self.ignored.push(false);
71 Some(stream_num)
72 }
73 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
74 if idx < self.streams.len() {
75 Some(self.streams[idx].clone())
76 } else {
77 None
78 }
79 }
80 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
81 for i in 0..self.streams.len() {
82 if self.streams[i].get_id() == id {
83 return Some(self.streams[i].clone());
84 }
85 }
86 None
87 }
88 pub fn get_num_streams(&self) -> usize { self.streams.len() }
89 pub fn is_ignored(&self, idx: usize) -> bool {
90 if self.no_ign {
91 true
92 } else if idx < self.ignored.len() {
93 self.ignored[idx]
94 } else {
95 false
96 }
97 }
98 pub fn is_ignored_id(&self, id: u32) -> bool {
99 for i in 0..self.streams.len() {
100 if self.streams[i].get_id() == id {
101 return self.ignored[i];
102 }
103 }
104 false
105 }
106 pub fn set_ignored(&mut self, idx: usize) {
107 if idx < self.ignored.len() {
108 self.ignored[idx] = true;
109 self.no_ign = false;
110 }
111 }
112 pub fn set_unignored(&mut self, idx: usize) {
113 if idx < self.ignored.len() {
114 self.ignored[idx] = false;
115 }
116 }
117 }
118
119 pub struct StreamIter<'a> {
120 streams: &'a Vec<Rc<NAStream>>,
121 pos: usize,
122 }
123
124 impl<'a> StreamIter<'a> {
125 pub fn new(streams: &'a Vec<Rc<NAStream>>) -> Self {
126 StreamIter { streams: streams, pos: 0 }
127 }
128 }
129
130 impl<'a> Iterator for StreamIter<'a> {
131 type Item = Rc<NAStream>;
132
133 fn next(&mut self) -> Option<Self::Item> {
134 if self.pos >= self.streams.len() { return None; }
135 let ret = self.streams[self.pos].clone();
136 self.pos += 1;
137 Some(ret)
138 }
139 }
140
141 pub struct Demuxer<'a> {
142 dmx: Box<DemuxCore<'a> + 'a>,
143 streams: StreamManager,
144 }
145
146 impl<'a> Demuxer<'a> {
147 fn new(dmx: Box<DemuxCore<'a> + 'a>, str: StreamManager) -> Self {
148 Demuxer {
149 dmx: dmx,
150 streams: str,
151 }
152 }
153 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
154 self.streams.get_stream(idx)
155 }
156 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
157 self.streams.get_stream_by_id(id)
158 }
159 pub fn get_num_streams(&self) -> usize {
160 self.streams.get_num_streams()
161 }
162 pub fn get_streams(&self) -> StreamIter {
163 self.streams.iter()
164 }
165 pub fn is_ignored_stream(&self, idx: usize) -> bool {
166 self.streams.is_ignored(idx)
167 }
168 pub fn set_ignored_stream(&mut self, idx: usize) {
169 self.streams.set_ignored(idx)
170 }
171 pub fn set_unignored_stream(&mut self, idx: usize) {
172 self.streams.set_unignored(idx)
173 }
174
175 pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
176 loop {
177 let res = self.dmx.get_frame(&mut self.streams);
178 if self.streams.no_ign || res.is_err() { return res; }
179 let res = res.unwrap();
180 let idx = res.get_stream().get_num();
181 if !self.is_ignored_stream(idx) {
182 return Ok(res);
183 }
184 }
185 }
186 pub fn seek(&mut self, time: u64) -> DemuxerResult<()> {
187 self.dmx.seek(time)
188 }
189 }
190
191 impl From<ByteIOError> for DemuxerError {
192 fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
193 }
194
195 ///The structure used to create demuxers.
196 pub trait DemuxerCreator {
197 /// Create new demuxer instance that will use `ByteReader` source as an input.
198 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a>;
199 /// Get the name of current demuxer creator.
200 fn get_name(&self) -> &'static str;
201 }
202
203 macro_rules! validate {
204 ($a:expr) => { if !$a { return Err(DemuxerError::InvalidData); } };
205 }
206
207 #[cfg(feature="demuxer_gdv")]
208 mod gdv;
209 #[cfg(feature="demuxer_avi")]
210 mod avi;
211 #[cfg(feature="demuxer_real")]
212 mod realmedia;
213
214
215 const DEMUXERS: &[&'static DemuxerCreator] = &[
216 #[cfg(feature="demuxer_avi")]
217 &avi::AVIDemuxerCreator {},
218 #[cfg(feature="demuxer_gdv")]
219 &gdv::GDVDemuxerCreator {},
220 #[cfg(feature="demuxer_real")]
221 &realmedia::RealMediaDemuxerCreator {},
222 //#[cfg(feature="demuxer_real")]
223 // &realmedia::RealAudioDemuxerCreator {},
224 //#[cfg(feature="demuxer_real")]
225 // &realmedia::RealIVRDemuxerCreator {},
226 ];
227
228 pub fn find_demuxer(name: &str) -> Option<&DemuxerCreator> {
229 for &dmx in DEMUXERS {
230 if dmx.get_name() == name {
231 return Some(dmx);
232 }
233 }
234 None
235 }
236
237 pub fn create_demuxer<'a>(dmxcr: &DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> {
238 let mut dmx = dmxcr.new_demuxer(br);
239 let mut str = StreamManager::new();
240 dmx.open(&mut str)?;
241 Ok(Demuxer::new(dmx, str))
242 }