]>
Commit | Line | Data |
---|---|---|
ab683361 | 1 | //! Demuxer definitions. |
4e8b4f31 KS |
2 | pub use crate::frame::*; |
3 | pub use crate::io::byteio::*; | |
5869fd63 | 4 | |
ab683361 | 5 | /// A list specifying general demuxing errors. |
b1be9318 | 6 | #[derive(Debug,Clone,Copy,PartialEq)] |
5869fd63 KS |
7 | #[allow(dead_code)] |
8 | pub enum DemuxerError { | |
ab683361 | 9 | /// Reader got to end of stream. |
5869fd63 | 10 | EOF, |
ab683361 | 11 | /// Demuxer encountered empty container. |
5869fd63 | 12 | NoSuchInput, |
ab683361 | 13 | /// Demuxer encountered invalid input data. |
5869fd63 | 14 | InvalidData, |
ab683361 | 15 | /// Data reading error. |
5869fd63 | 16 | IOError, |
ab683361 | 17 | /// Feature is not implemented. |
5869fd63 | 18 | NotImplemented, |
ab683361 | 19 | /// Allocation failed. |
5869fd63 | 20 | MemoryError, |
ab683361 | 21 | /// The operation should be repeated. |
fe07b469 | 22 | TryAgain, |
ab683361 | 23 | /// Seeking failed. |
33b5a8f0 | 24 | SeekError, |
ab683361 | 25 | /// Operation cannot succeed in principle (e.g. seeking in a format not supporting seeking). |
33b5a8f0 | 26 | NotPossible, |
5869fd63 KS |
27 | } |
28 | ||
ab683361 | 29 | /// A specialised `Result` type for demuxing operations. |
5641dccf | 30 | pub type DemuxerResult<T> = Result<T, DemuxerError>; |
5869fd63 | 31 | |
ab683361 | 32 | /// A trait for demuxing operations. |
bcfeae48 | 33 | pub trait DemuxCore<'a> { |
ab683361 | 34 | /// Opens the input stream, reads required headers and prepares everything for packet demuxing. |
33b5a8f0 | 35 | fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()>; |
ab683361 | 36 | /// Demuxes a packet. |
bcfeae48 | 37 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>; |
ab683361 | 38 | /// Seeks to the requested time. |
33b5a8f0 | 39 | fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()>; |
5869fd63 KS |
40 | } |
41 | ||
ab683361 | 42 | /// An auxiliary trait to make bytestream reader read packet data. |
8869d452 | 43 | pub trait NAPacketReader { |
ab683361 | 44 | /// Reads input and constructs a packet containing it. |
70910ac3 | 45 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; |
ab683361 | 46 | /// Reads input into already existing packet. |
5869fd63 KS |
47 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
48 | } | |
49 | ||
8869d452 | 50 | impl<'a> NAPacketReader for ByteReader<'a> { |
70910ac3 | 51 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> { |
5869fd63 KS |
52 | let mut buf: Vec<u8> = Vec::with_capacity(size); |
53 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } | |
54 | buf.resize(size, 0); | |
e243ceb4 | 55 | self.read_buf(buf.as_mut_slice())?; |
e189501e | 56 | let pkt = NAPacket::new(str, ts, kf, buf); |
5869fd63 KS |
57 | Ok(pkt) |
58 | } | |
59 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { | |
60 | let mut refbuf = pkt.get_buffer(); | |
1a967e6b | 61 | let buf = refbuf.as_mut().unwrap(); |
e243ceb4 | 62 | self.read_buf(buf.as_mut_slice())?; |
5869fd63 KS |
63 | Ok(()) |
64 | } | |
65 | } | |
66 | ||
ab683361 | 67 | /// An auxiliary structure for operations with individual streams inside the container. |
e243ceb4 | 68 | #[derive(Default)] |
bcfeae48 | 69 | pub struct StreamManager { |
70910ac3 | 70 | streams: Vec<NAStreamRef>, |
bcfeae48 KS |
71 | ignored: Vec<bool>, |
72 | no_ign: bool, | |
20ef4353 KS |
73 | } |
74 | ||
bcfeae48 | 75 | impl StreamManager { |
ab683361 | 76 | /// Constructs a new instance of `StreamManager`. |
bcfeae48 KS |
77 | pub fn new() -> Self { |
78 | StreamManager { | |
79 | streams: Vec::new(), | |
80 | ignored: Vec::new(), | |
81 | no_ign: true, | |
82 | } | |
83 | } | |
ab683361 | 84 | /// Returns stream iterator. |
bcfeae48 KS |
85 | pub fn iter(&self) -> StreamIter { StreamIter::new(&self.streams) } |
86 | ||
ab683361 | 87 | /// Adds a new stream. |
20ef4353 KS |
88 | pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> { |
89 | let stream_num = self.streams.len(); | |
90 | let mut str = stream.clone(); | |
91 | str.set_num(stream_num); | |
70910ac3 | 92 | self.streams.push(str.into_ref()); |
bcfeae48 | 93 | self.ignored.push(false); |
20ef4353 KS |
94 | Some(stream_num) |
95 | } | |
ab683361 | 96 | /// Returns stream with the requested index. |
70910ac3 | 97 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
20ef4353 KS |
98 | if idx < self.streams.len() { |
99 | Some(self.streams[idx].clone()) | |
100 | } else { | |
101 | None | |
102 | } | |
103 | } | |
ab683361 | 104 | /// Returns stream with the requested stream ID. |
70910ac3 | 105 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
20ef4353 KS |
106 | for i in 0..self.streams.len() { |
107 | if self.streams[i].get_id() == id { | |
108 | return Some(self.streams[i].clone()); | |
109 | } | |
110 | } | |
111 | None | |
112 | } | |
ab683361 | 113 | /// Returns the number of known streams. |
66116504 | 114 | pub fn get_num_streams(&self) -> usize { self.streams.len() } |
ab683361 | 115 | /// Reports whether the stream is marked as ignored. |
bcfeae48 KS |
116 | pub fn is_ignored(&self, idx: usize) -> bool { |
117 | if self.no_ign { | |
118 | true | |
119 | } else if idx < self.ignored.len() { | |
120 | self.ignored[idx] | |
121 | } else { | |
122 | false | |
123 | } | |
124 | } | |
ab683361 | 125 | /// Reports whether the stream with certain ID is marked as ignored. |
ce52b3b5 KS |
126 | pub fn is_ignored_id(&self, id: u32) -> bool { |
127 | for i in 0..self.streams.len() { | |
128 | if self.streams[i].get_id() == id { | |
129 | return self.ignored[i]; | |
130 | } | |
131 | } | |
132 | false | |
133 | } | |
ab683361 | 134 | /// Marks requested stream as ignored. |
bcfeae48 KS |
135 | pub fn set_ignored(&mut self, idx: usize) { |
136 | if idx < self.ignored.len() { | |
137 | self.ignored[idx] = true; | |
138 | self.no_ign = false; | |
139 | } | |
140 | } | |
ab683361 | 141 | /// Clears the ignored mark for the requested stream. |
bcfeae48 KS |
142 | pub fn set_unignored(&mut self, idx: usize) { |
143 | if idx < self.ignored.len() { | |
144 | self.ignored[idx] = false; | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
ab683361 | 149 | /// Stream iterator. |
bcfeae48 | 150 | pub struct StreamIter<'a> { |
e243ceb4 | 151 | streams: &'a [NAStreamRef], |
bcfeae48 KS |
152 | pos: usize, |
153 | } | |
154 | ||
155 | impl<'a> StreamIter<'a> { | |
ab683361 | 156 | /// Constructs a new instance of `StreamIter`. |
e243ceb4 KS |
157 | pub fn new(streams: &'a [NAStreamRef]) -> Self { |
158 | StreamIter { streams, pos: 0 } | |
bcfeae48 KS |
159 | } |
160 | } | |
161 | ||
162 | impl<'a> Iterator for StreamIter<'a> { | |
70910ac3 | 163 | type Item = NAStreamRef; |
bcfeae48 KS |
164 | |
165 | fn next(&mut self) -> Option<Self::Item> { | |
166 | if self.pos >= self.streams.len() { return None; } | |
167 | let ret = self.streams[self.pos].clone(); | |
168 | self.pos += 1; | |
169 | Some(ret) | |
170 | } | |
171 | } | |
172 | ||
ab683361 | 173 | /// Seeking modes. |
33b5a8f0 KS |
174 | #[derive(Clone,Copy,PartialEq)] |
175 | pub enum SeekIndexMode { | |
ab683361 | 176 | /// No seeking index present. |
33b5a8f0 | 177 | None, |
ab683361 | 178 | /// Seeking index is present. |
33b5a8f0 | 179 | Present, |
ab683361 | 180 | /// Seeking index should be constructed by the demuxer if possible. |
33b5a8f0 KS |
181 | Automatic, |
182 | } | |
183 | ||
184 | impl Default for SeekIndexMode { | |
185 | fn default() -> Self { SeekIndexMode::None } | |
186 | } | |
187 | ||
ab683361 | 188 | /// A structure holding seeking information. |
33b5a8f0 KS |
189 | #[derive(Clone,Copy,Default)] |
190 | pub struct SeekEntry { | |
ab683361 KS |
191 | /// Time in milliseconds. |
192 | pub time: u64, | |
193 | /// PTS | |
33b5a8f0 | 194 | pub pts: u64, |
ab683361 | 195 | /// Position in file. |
33b5a8f0 KS |
196 | pub pos: u64, |
197 | } | |
198 | ||
ab683361 | 199 | /// Seeking information for individual streams. |
33b5a8f0 KS |
200 | #[derive(Clone)] |
201 | pub struct StreamSeekInfo { | |
ab683361 | 202 | /// Stream ID. |
33b5a8f0 | 203 | pub id: u32, |
ab683361 | 204 | /// Index is present. |
33b5a8f0 | 205 | pub filled: bool, |
ab683361 | 206 | /// Packet seeking information. |
33b5a8f0 KS |
207 | pub entries: Vec<SeekEntry>, |
208 | } | |
209 | ||
210 | impl StreamSeekInfo { | |
ab683361 | 211 | /// Constructs a new `StreamSeekInfo` instance. |
4cfb5dd4 | 212 | pub fn new(id: u32) -> Self { |
33b5a8f0 | 213 | Self { |
4cfb5dd4 | 214 | id, |
33b5a8f0 KS |
215 | filled: false, |
216 | entries: Vec::new(), | |
217 | } | |
218 | } | |
ab683361 | 219 | /// Adds new seeking point. |
33b5a8f0 KS |
220 | pub fn add_entry(&mut self, entry: SeekEntry) { |
221 | self.entries.push(entry); | |
222 | } | |
ab683361 | 223 | /// Searches for an appropriate seek position before requested time. |
4cfb5dd4 | 224 | pub fn find_pos(&self, time: u64) -> Option<SeekEntry> { |
33b5a8f0 KS |
225 | if !self.entries.is_empty() { |
226 | // todo something faster like binary search | |
4cfb5dd4 KS |
227 | let mut cand = None; |
228 | for entry in self.entries.iter() { | |
229 | if entry.time <= time { | |
230 | cand = Some(*entry); | |
33b5a8f0 KS |
231 | } else { |
232 | break; | |
233 | } | |
234 | } | |
4cfb5dd4 | 235 | cand |
33b5a8f0 KS |
236 | } else { |
237 | None | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
ab683361 | 242 | /// Structure for holding seeking point search results. |
33b5a8f0 KS |
243 | #[derive(Clone,Copy,Default)] |
244 | pub struct SeekIndexResult { | |
ab683361 | 245 | /// Packet PTS. |
33b5a8f0 | 246 | pub pts: u64, |
ab683361 | 247 | /// Position in file. |
33b5a8f0 | 248 | pub pos: u64, |
ab683361 | 249 | /// Stream ID. |
33b5a8f0 KS |
250 | pub str_id: u32, |
251 | } | |
252 | ||
ab683361 | 253 | /// Seek information for the whole container. |
33b5a8f0 KS |
254 | #[derive(Default)] |
255 | pub struct SeekIndex { | |
ab683361 | 256 | /// Seek information for individual streams. |
33b5a8f0 | 257 | pub seek_info: Vec<StreamSeekInfo>, |
ab683361 | 258 | /// Seeking index mode. |
33b5a8f0 | 259 | pub mode: SeekIndexMode, |
ab683361 | 260 | /// Ignore index flag. |
9da33f04 | 261 | pub skip_index: bool, |
33b5a8f0 KS |
262 | } |
263 | ||
264 | impl SeekIndex { | |
ab683361 | 265 | /// Constructs a new `SeekIndex` instance. |
33b5a8f0 | 266 | pub fn new() -> Self { Self::default() } |
6492988d KS |
267 | pub fn add_stream(&mut self, id: u32) -> usize { |
268 | let ret = self.stream_id_to_index(id); | |
269 | if ret.is_none() { | |
4cfb5dd4 | 270 | self.seek_info.push(StreamSeekInfo::new(id)); |
6492988d KS |
271 | self.seek_info.len() - 1 |
272 | } else { | |
273 | ret.unwrap() | |
33b5a8f0 KS |
274 | } |
275 | } | |
ab683361 | 276 | /// Adds a new stream to the index. |
33b5a8f0 KS |
277 | pub fn stream_id_to_index(&self, id: u32) -> Option<usize> { |
278 | for (idx, str) in self.seek_info.iter().enumerate() { | |
279 | if str.id == id { | |
280 | return Some(idx); | |
281 | } | |
282 | } | |
283 | None | |
284 | } | |
ab683361 | 285 | /// Returns stream reference for provided stream ID. |
4cfb5dd4 KS |
286 | pub fn get_stream_index(&mut self, id: u32) -> Option<&mut StreamSeekInfo> { |
287 | for str in self.seek_info.iter_mut() { | |
288 | if str.id == id { | |
289 | return Some(str); | |
290 | } | |
291 | } | |
292 | None | |
293 | } | |
ab683361 | 294 | /// Adds seeking information to the index. |
6492988d KS |
295 | pub fn add_entry(&mut self, id: u32, entry: SeekEntry) { |
296 | let mut idx = self.stream_id_to_index(id); | |
297 | if idx.is_none() { | |
298 | idx = Some(self.add_stream(id)); | |
299 | } | |
300 | self.seek_info[idx.unwrap()].add_entry(entry); | |
301 | self.seek_info[idx.unwrap()].filled = true; | |
302 | } | |
ab683361 | 303 | /// Searches for a seek position before requested time. |
33b5a8f0 KS |
304 | pub fn find_pos(&self, time: u64) -> Option<SeekIndexResult> { |
305 | let mut cand = None; | |
306 | for str in self.seek_info.iter() { | |
307 | if !str.filled { continue; } | |
4cfb5dd4 KS |
308 | let res = str.find_pos(time); |
309 | if res.is_none() { continue; } | |
310 | let res = res.unwrap(); | |
33b5a8f0 | 311 | if cand.is_none() { |
4cfb5dd4 | 312 | cand = Some(SeekIndexResult { pts: res.pts, pos: res.pos, str_id: str.id }); |
33b5a8f0 | 313 | } else if let Some(entry) = cand { |
4cfb5dd4 KS |
314 | if res.pos < entry.pos { |
315 | cand = Some(SeekIndexResult { pts: res.pts, pos: res.pos, str_id: str.id }); | |
33b5a8f0 KS |
316 | } |
317 | } | |
318 | } | |
319 | cand | |
320 | } | |
321 | } | |
322 | ||
ab683361 | 323 | /// Demuxer structure with auxiliary data. |
bcfeae48 | 324 | pub struct Demuxer<'a> { |
6011e201 | 325 | dmx: Box<dyn DemuxCore<'a> + 'a>, |
bcfeae48 | 326 | streams: StreamManager, |
33b5a8f0 | 327 | seek_idx: SeekIndex, |
bcfeae48 KS |
328 | } |
329 | ||
330 | impl<'a> Demuxer<'a> { | |
ab683361 | 331 | /// Constructs a new `Demuxer` instance. |
33b5a8f0 | 332 | fn new(dmx: Box<dyn DemuxCore<'a> + 'a>, str: StreamManager, seek_idx: SeekIndex) -> Self { |
bcfeae48 | 333 | Demuxer { |
e243ceb4 | 334 | dmx, |
bcfeae48 | 335 | streams: str, |
33b5a8f0 | 336 | seek_idx, |
bcfeae48 KS |
337 | } |
338 | } | |
ab683361 | 339 | /// Returns a stream reference by its number. |
70910ac3 | 340 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
bcfeae48 KS |
341 | self.streams.get_stream(idx) |
342 | } | |
ab683361 | 343 | /// Returns a stream reference by its ID. |
70910ac3 | 344 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
bcfeae48 KS |
345 | self.streams.get_stream_by_id(id) |
346 | } | |
ab683361 | 347 | /// Reports the total number of streams. |
bcfeae48 KS |
348 | pub fn get_num_streams(&self) -> usize { |
349 | self.streams.get_num_streams() | |
350 | } | |
ab683361 | 351 | /// Returns an iterator over streams. |
bcfeae48 KS |
352 | pub fn get_streams(&self) -> StreamIter { |
353 | self.streams.iter() | |
354 | } | |
ab683361 | 355 | /// Returns 'ignored' marker for requested stream. |
bcfeae48 KS |
356 | pub fn is_ignored_stream(&self, idx: usize) -> bool { |
357 | self.streams.is_ignored(idx) | |
358 | } | |
ab683361 | 359 | /// Sets 'ignored' marker for requested stream. |
bcfeae48 KS |
360 | pub fn set_ignored_stream(&mut self, idx: usize) { |
361 | self.streams.set_ignored(idx) | |
362 | } | |
ab683361 | 363 | /// Clears 'ignored' marker for requested stream. |
bcfeae48 KS |
364 | pub fn set_unignored_stream(&mut self, idx: usize) { |
365 | self.streams.set_unignored(idx) | |
366 | } | |
367 | ||
ab683361 | 368 | /// Demuxes a new packet from the container. |
bcfeae48 KS |
369 | pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> { |
370 | loop { | |
371 | let res = self.dmx.get_frame(&mut self.streams); | |
372 | if self.streams.no_ign || res.is_err() { return res; } | |
373 | let res = res.unwrap(); | |
374 | let idx = res.get_stream().get_num(); | |
375 | if !self.is_ignored_stream(idx) { | |
376 | return Ok(res); | |
377 | } | |
378 | } | |
379 | } | |
ab683361 | 380 | /// Seeks to the requested time (in milliseconds) if possible. |
bcfeae48 | 381 | pub fn seek(&mut self, time: u64) -> DemuxerResult<()> { |
9da33f04 KS |
382 | if self.seek_idx.skip_index { |
383 | return Err(DemuxerError::NotPossible); | |
384 | } | |
33b5a8f0 KS |
385 | self.dmx.seek(time, &self.seek_idx) |
386 | } | |
ab683361 | 387 | /// Returns internal seek index. |
33b5a8f0 KS |
388 | pub fn get_seek_index(&self) -> &SeekIndex { |
389 | &self.seek_idx | |
bcfeae48 | 390 | } |
5869fd63 KS |
391 | } |
392 | ||
393 | impl From<ByteIOError> for DemuxerError { | |
394 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } | |
395 | } | |
396 | ||
ab683361 | 397 | /// The trait for creating demuxers. |
eb71d98f | 398 | pub trait DemuxerCreator { |
ab683361 | 399 | /// Creates new demuxer instance that will use `ByteReader` source as an input. |
6011e201 | 400 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a>; |
ab683361 | 401 | /// Returns the name of current demuxer creator (equal to the container name it can demux). |
eb71d98f KS |
402 | fn get_name(&self) -> &'static str; |
403 | } | |
404 | ||
ab683361 | 405 | /// Creates demuxer for a provided bytestream. |
bcfeae48 KS |
406 | pub fn create_demuxer<'a>(dmxcr: &DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> { |
407 | let mut dmx = dmxcr.new_demuxer(br); | |
408 | let mut str = StreamManager::new(); | |
33b5a8f0 KS |
409 | let mut seek_idx = SeekIndex::new(); |
410 | dmx.open(&mut str, &mut seek_idx)?; | |
411 | Ok(Demuxer::new(dmx, str, seek_idx)) | |
bcfeae48 | 412 | } |
5641dccf | 413 | |
ab683361 | 414 | /// List of registered demuxers. |
e243ceb4 | 415 | #[derive(Default)] |
5641dccf KS |
416 | pub struct RegisteredDemuxers { |
417 | dmxs: Vec<&'static DemuxerCreator>, | |
418 | } | |
419 | ||
420 | impl RegisteredDemuxers { | |
ab683361 | 421 | /// Constructs a new `RegisteredDemuxers` instance. |
5641dccf KS |
422 | pub fn new() -> Self { |
423 | Self { dmxs: Vec::new() } | |
424 | } | |
ab683361 | 425 | /// Registers a new demuxer. |
5641dccf KS |
426 | pub fn add_demuxer(&mut self, dmx: &'static DemuxerCreator) { |
427 | self.dmxs.push(dmx); | |
428 | } | |
ab683361 | 429 | /// Searches for a demuxer that supports requested container format. |
5641dccf KS |
430 | pub fn find_demuxer(&self, name: &str) -> Option<&DemuxerCreator> { |
431 | for &dmx in self.dmxs.iter() { | |
432 | if dmx.get_name() == name { | |
433 | return Some(dmx); | |
434 | } | |
435 | } | |
436 | None | |
437 | } | |
1a967e6b | 438 | } |