| 1 | #[cfg(feature="demuxer_gdv")] |
| 2 | pub mod gdv; |
| 3 | #[cfg(feature="demuxer_avi")] |
| 4 | pub mod avi; |
| 5 | |
| 6 | use std::fmt; |
| 7 | use std::rc::Rc; |
| 8 | use frame::*; |
| 9 | use std::collections::HashMap; |
| 10 | use io::byteio::*; |
| 11 | |
| 12 | #[derive(Debug,Clone,Copy)] |
| 13 | #[allow(dead_code)] |
| 14 | pub enum StreamType { |
| 15 | Video, |
| 16 | Audio, |
| 17 | Subtitles, |
| 18 | Data, |
| 19 | None, |
| 20 | } |
| 21 | |
| 22 | impl 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"), |
| 29 | StreamType::None => write!(f, "-"), |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | #[allow(dead_code)] |
| 36 | #[derive(Clone)] |
| 37 | pub struct NAStream { |
| 38 | media_type: StreamType, |
| 39 | id: u32, |
| 40 | num: usize, |
| 41 | info: Rc<NACodecInfo>, |
| 42 | } |
| 43 | |
| 44 | impl NAStream { |
| 45 | pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self { |
| 46 | NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) } |
| 47 | } |
| 48 | pub fn get_id(&self) -> u32 { self.id } |
| 49 | pub fn get_num(&self) -> usize { self.num } |
| 50 | pub fn set_num(&mut self, num: usize) { self.num = num; } |
| 51 | pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() } |
| 52 | } |
| 53 | |
| 54 | impl fmt::Display for NAStream { |
| 55 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 56 | write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties()) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[allow(dead_code)] |
| 61 | pub struct NAPacket { |
| 62 | stream: Rc<NAStream>, |
| 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 | |
| 71 | impl NAPacket { |
| 72 | pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self { |
| 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 | } |
| 77 | pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() } |
| 78 | pub fn get_pts(&self) -> Option<u64> { self.pts } |
| 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 } |
| 82 | pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() } |
| 83 | } |
| 84 | |
| 85 | impl Drop for NAPacket { |
| 86 | fn drop(&mut self) {} |
| 87 | } |
| 88 | |
| 89 | impl fmt::Display for NAPacket { |
| 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)] |
| 103 | pub enum DemuxerError { |
| 104 | EOF, |
| 105 | NoSuchInput, |
| 106 | InvalidData, |
| 107 | IOError, |
| 108 | NotImplemented, |
| 109 | MemoryError, |
| 110 | } |
| 111 | |
| 112 | type DemuxerResult<T> = Result<T, DemuxerError>; |
| 113 | |
| 114 | pub trait Demux<'a> { |
| 115 | fn open(&mut self) -> DemuxerResult<()>; |
| 116 | fn get_num_streams(&self) -> usize; |
| 117 | fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>>; |
| 118 | fn get_frame(&mut self) -> DemuxerResult<NAPacket>; |
| 119 | fn seek(&mut self, time: u64) -> DemuxerResult<()>; |
| 120 | } |
| 121 | |
| 122 | pub 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>; |
| 124 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
| 125 | } |
| 126 | |
| 127 | impl<'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> { |
| 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); } |
| 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); } |
| 142 | Ok(()) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | pub struct Demuxer { |
| 147 | streams: Vec<Rc<NAStream>>, |
| 148 | } |
| 149 | |
| 150 | impl 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 | } |
| 174 | pub fn get_num_streams(&self) -> usize { self.streams.len() } |
| 175 | } |
| 176 | |
| 177 | impl From<ByteIOError> for DemuxerError { |
| 178 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } |
| 179 | } |
| 180 | |
| 181 | pub trait FrameFromPacket { |
| 182 | fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame; |
| 183 | fn fill_timestamps(&mut self, pkt: &NAPacket); |
| 184 | } |
| 185 | |
| 186 | impl 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 | } |