1 #[cfg(feature="demuxer_gdv")]
3 #[cfg(feature="demuxer_avi")]
9 use std::collections::HashMap;
12 /// Possible stream types.
13 #[derive(Debug,Clone,Copy)]
22 /// any data stream (or might be an unrecognized audio/video stream)
24 /// nonexistent stream
28 impl fmt::Display for StreamType {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31 StreamType::Video => write!(f, "Video"),
32 StreamType::Audio => write!(f, "Audio"),
33 StreamType::Subtitles => write!(f, "Subtitles"),
34 StreamType::Data => write!(f, "Data"),
35 StreamType::None => write!(f, "-"),
44 media_type: StreamType,
47 info: Rc<NACodecInfo>,
51 pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
52 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
54 pub fn get_id(&self) -> u32 { self.id }
55 pub fn get_num(&self) -> usize { self.num }
56 pub fn set_num(&mut self, num: usize) { self.num = num; }
57 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
60 impl fmt::Display for NAStream {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties())
71 duration: Option<u64>,
74 // options: HashMap<String, NAValue<'a>>,
78 pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
79 // let mut vec: Vec<u8> = Vec::new();
80 // vec.resize(size, 0);
81 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
83 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
84 pub fn get_pts(&self) -> Option<u64> { self.pts }
85 pub fn get_dts(&self) -> Option<u64> { self.dts }
86 pub fn get_duration(&self) -> Option<u64> { self.duration }
87 pub fn is_keyframe(&self) -> bool { self.keyframe }
88 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
91 impl Drop for NAPacket {
95 impl fmt::Display for NAPacket {
96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
98 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
99 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
100 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
101 if self.keyframe { foo = format!("{} kf", foo); }
109 pub enum DemuxerError {
118 type DemuxerResult<T> = Result<T, DemuxerError>;
120 pub trait Demux<'a> {
121 fn open(&mut self) -> DemuxerResult<()>;
122 fn get_num_streams(&self) -> usize;
123 fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>>;
124 fn get_frame(&mut self) -> DemuxerResult<NAPacket>;
125 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
128 pub trait NAPacketReader {
129 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
130 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
133 impl<'a> NAPacketReader for ByteReader<'a> {
134 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
135 let mut buf: Vec<u8> = Vec::with_capacity(size);
136 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
138 let res = self.read_buf(buf.as_mut_slice());
139 if let Err(_) = res { return Err(DemuxerError::IOError); }
140 let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
143 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
144 let mut refbuf = pkt.get_buffer();
145 let mut buf = Rc::make_mut(&mut refbuf);
146 let res = self.read_buf(buf.as_mut_slice());
147 if let Err(_) = res { return Err(DemuxerError::IOError); }
153 streams: Vec<Rc<NAStream>>,
157 pub fn new() -> Self { Demuxer { streams: Vec::new() } }
158 pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> {
159 let stream_num = self.streams.len();
160 let mut str = stream.clone();
161 str.set_num(stream_num);
162 self.streams.push(Rc::new(str));
165 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
166 if idx < self.streams.len() {
167 Some(self.streams[idx].clone())
172 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
173 for i in 0..self.streams.len() {
174 if self.streams[i].get_id() == id {
175 return Some(self.streams[i].clone());
180 pub fn get_num_streams(&self) -> usize { self.streams.len() }
183 impl From<ByteIOError> for DemuxerError {
184 fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
187 pub trait FrameFromPacket {
188 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame;
189 fn fill_timestamps(&mut self, pkt: &NAPacket);
192 impl FrameFromPacket for NAFrame {
193 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame {
194 NAFrame::new(pkt.pts, pkt.dts, pkt.duration, info, HashMap::new())
196 fn fill_timestamps(&mut self, pkt: &NAPacket) {
197 self.set_pts(pkt.pts);
198 self.set_dts(pkt.dts);
199 self.set_duration(pkt.duration);
203 ///The structure used to create demuxers.
204 pub trait DemuxerCreator {
205 /// Create new demuxer instance that will use `ByteReader` source as an input.
206 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<Demux<'a> + 'a>;
207 /// Get the name of current demuxer creator.
208 fn get_name(&self) -> &'static str;
211 const DEMUXERS: &[&'static DemuxerCreator] = &[
212 #[cfg(feature="demuxer_avi")]
213 &avi::AVIDemuxerCreator {},
214 #[cfg(feature="demuxer_gdv")]
215 &gdv::GDVDemuxerCreator {},
218 pub fn find_demuxer(name: &str) -> Option<&DemuxerCreator> {
219 for &dmx in DEMUXERS {
220 if dmx.get_name() == name {