From: Kostya Shishkov Date: Sat, 13 May 2017 10:39:25 +0000 (+0200) Subject: revamp stream handling in demuxers X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=20ef4353905883ca289782858ccfcd7d2146fa42;p=nihav.git revamp stream handling in demuxers --- diff --git a/src/demuxers/gdv.rs b/src/demuxers/gdv.rs index b84bc98..afdc4f6 100644 --- a/src/demuxers/gdv.rs +++ b/src/demuxers/gdv.rs @@ -1,6 +1,7 @@ use super::*; use io::byteio::*; use frame::*; +use formats::*; //use std::collections::HashMap; enum GDVState { @@ -10,18 +11,20 @@ enum GDVState { #[allow(dead_code)] pub struct GremlinVideoDemuxer<'a> { - opened: bool, - src: &'a mut ByteReader<'a>, - streams: Vec>, - frames: u16, - cur_frame: u16, - asize: usize, - apacked: bool, - state: GDVState, - pktdta: Vec, + opened: bool, + src: &'a mut ByteReader<'a>, + frames: u16, + cur_frame: u16, + asize: usize, + apacked: bool, + state: GDVState, + pktdta: Vec, + dmx: Demuxer, + a_id: Option, + v_id: Option, } -impl<'a> NADemuxer<'a> for GremlinVideoDemuxer<'a> { +impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> { #[allow(unused_variables)] fn open(&mut self) -> DemuxerResult<()> { let src = &mut self.src; @@ -37,25 +40,21 @@ impl<'a> NADemuxer<'a> for GremlinVideoDemuxer<'a> { src.read_skip(2)?; let width = src.read_u16le()?; let height = src.read_u16le()?; -println!("id {} frames {} fps {} sound {} Hz {:X} img {} - {}x{}",id,frames,fps,rate,aflags,depth,width,height); if max_fs > 0 { let vhdr = NAVideoInfo::new(width as u32, height as u32, false, PAL8_FORMAT); let vci = NACodecTypeInfo::Video(vhdr); let vinfo = NACodecInfo::new(vci, None); - let vstr = NAStream::new(StreamType::Video, 0, vinfo); - self.streams.push(Rc::new(vstr)); + self.v_id = self.dmx.add_stream(NAStream::new(StreamType::Video, 0, vinfo)); } if (aflags & 1) != 0 { let channels = if (aflags & 2) != 0 { 2 } else { 1 }; let ahdr = NAAudioInfo::new(rate as u32, channels as u8, if (aflags & 4) != 0 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2); let ainfo = NACodecInfo::new(NACodecTypeInfo::Audio(ahdr), None); - let astr = NAStream::new(StreamType::Audio, 1, ainfo); - self.streams.push(Rc::new(astr)); + self.a_id = self.dmx.add_stream(NAStream::new(StreamType::Audio, 1, ainfo)); let packed = if (aflags & 8) != 0 { 1 } else { 0 }; self.asize = (((rate / fps) * channels * (if (aflags & 4) != 0 { 2 } else { 1 })) >> packed) as usize; self.apacked = (aflags & 8) != 0; -println!("audio chunk size {}({:X})",self.asize,self.asize); } if max_fs > 0 && depth == 1 { src.read_skip(768)?; @@ -98,26 +97,20 @@ impl<'a> GremlinVideoDemuxer<'a> { state: GDVState::NewFrame, pktdta: Vec::new(), src: io, - streams: Vec::new() + a_id: None, + v_id: None, + dmx: Demuxer::new() } } - fn find_stream(&mut self, id: u32) -> Rc { - for i in 0..self.streams.len() { - if self.streams[i].get_id() == id { - return self.streams[i].clone(); - } - } - panic!("stream not found"); - } fn read_achunk(&mut self) -> DemuxerResult { self.state = GDVState::AudioRead; - let str = self.find_stream(1); + let str = self.dmx.get_stream(self.a_id.unwrap()).unwrap(); self.src.read_packet(str, Some(self.cur_frame as u64), None, None, true, self.asize) } fn read_vchunk(&mut self) -> DemuxerResult { - let str = self.find_stream(0); + let str = self.dmx.get_stream(self.v_id.unwrap()).unwrap(); let mut src = &mut self.src; let magic = src.read_u16be()?; if magic != 0x0513 { return Err(DemuxerError::InvalidData); } diff --git a/src/demuxers/mod.rs b/src/demuxers/mod.rs index 1eb6cd1..14aac93 100644 --- a/src/demuxers/mod.rs +++ b/src/demuxers/mod.rs @@ -6,7 +6,7 @@ use frame::*; //use std::collections::HashMap; use io::byteio::*; -#[derive(Debug)] +#[derive(Debug,Clone,Copy)] #[allow(dead_code)] pub enum StreamType { Video, @@ -28,17 +28,21 @@ impl fmt::Display for StreamType { #[allow(dead_code)] +#[derive(Clone)] pub struct NAStream { media_type: StreamType, id: u32, + num: usize, info: Rc, } impl NAStream { pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self { - NAStream { media_type: mt, id: id, info: Rc::new(info) } + NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) } } pub fn get_id(&self) -> u32 { self.id } + pub fn get_num(&self) -> usize { self.num } + pub fn set_num(&mut self, num: usize) { self.num = num; } pub fn get_info(&self) -> Rc { self.info.clone() } } @@ -95,7 +99,7 @@ pub enum DemuxerError { type DemuxerResult = Result; -pub trait NADemuxer<'a> { +pub trait Demux<'a> { fn open(&mut self) -> DemuxerResult<()>; fn get_frame(&mut self) -> DemuxerResult; fn seek(&mut self, time: u64) -> DemuxerResult<()>; @@ -127,16 +131,43 @@ impl<'a> NAPacketReader for ByteReader<'a> { } } -pub struct NADemuxerBuilder { +pub struct Demuxer { + streams: Vec>, +} + +impl Demuxer { + pub fn new() -> Self { Demuxer { streams: Vec::new() } } + pub fn add_stream(&mut self, stream: NAStream) -> Option { + let stream_num = self.streams.len(); + let mut str = stream.clone(); + str.set_num(stream_num); + self.streams.push(Rc::new(str)); + Some(stream_num) + } + pub fn get_stream(&self, idx: usize) -> Option> { + if idx < self.streams.len() { + Some(self.streams[idx].clone()) + } else { + None + } + } + pub fn get_stream_by_id(&self, id: u32) -> Option> { + for i in 0..self.streams.len() { + if self.streams[i].get_id() == id { + return Some(self.streams[i].clone()); + } + } + None + } } impl From for DemuxerError { fn from(_: ByteIOError) -> Self { DemuxerError::IOError } } -impl NADemuxerBuilder { - #[allow(unused_variables)] - pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult>> { - unimplemented!() - } -} +//impl NADemuxerBuilder { +// #[allow(unused_variables)] +// pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult>> { +// unimplemented!() +// } +//}