X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fdemuxers%2Fgdv.rs;h=16eae335321325b605ca0471a0e0567fb326f8e8;hb=750b299cebf79e53a0ce37c59afdadec8f527c7b;hp=c556197828dec46036905efe4e499d1bcb860b40;hpb=5869fd634c6a174ef2c541ddad4b1a4e9ec26d29;p=nihav.git diff --git a/src/demuxers/gdv.rs b/src/demuxers/gdv.rs index c556197..16eae33 100644 --- a/src/demuxers/gdv.rs +++ b/src/demuxers/gdv.rs @@ -1,7 +1,7 @@ use super::*; -use io::byteio::*; -use frame::*; -use std::io::SeekFrom; +//use io::byteio::*; +//use frame::*; +use formats::*; //use std::collections::HashMap; enum GDVState { @@ -10,21 +10,47 @@ 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, +struct GremlinVideoDemuxer<'a> { + src: &'a mut ByteReader<'a>, + frames: u16, + cur_frame: u16, + asize: usize, + apacked: bool, + state: GDVState, + pktdta: Vec, + a_id: Option, + v_id: Option, } -impl<'a> NADemuxer<'a> for GremlinVideoDemuxer<'a> { +struct GDVFixedSizes { + id: u16, + width: u16, + height: u16, +} +const GDV_SIZE_TABLE: &[GDVFixedSizes] = &[ + GDVFixedSizes { id: 0, width: 320, height: 200 }, + GDVFixedSizes { id: 1, width: 640, height: 200 }, + GDVFixedSizes { id: 2, width: 320, height: 167 }, + GDVFixedSizes { id: 3, width: 320, height: 180 }, + GDVFixedSizes { id: 4, width: 320, height: 400 }, + GDVFixedSizes { id: 5, width: 320, height: 170 }, + GDVFixedSizes { id: 6, width: 160, height: 85 }, + GDVFixedSizes { id: 7, width: 160, height: 83 }, + GDVFixedSizes { id: 8, width: 160, height: 90 }, + GDVFixedSizes { id: 9, width: 280, height: 128 }, + GDVFixedSizes { id: 10, width: 320, height: 240 }, + GDVFixedSizes { id: 11, width: 320, height: 201 }, + GDVFixedSizes { id: 16, width: 640, height: 400 }, + GDVFixedSizes { id: 17, width: 640, height: 200 }, + GDVFixedSizes { id: 18, width: 640, height: 180 }, + GDVFixedSizes { id: 19, width: 640, height: 167 }, + GDVFixedSizes { id: 20, width: 640, height: 170 }, + GDVFixedSizes { id: 21, width: 320, height: 240 }, +]; + +impl<'a> DemuxCore<'a> for GremlinVideoDemuxer<'a> { #[allow(unused_variables)] - fn open(&mut self) -> DemuxerResult<()> { + fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> { let src = &mut self.src; let magic = src.read_u32le()?; if magic != 0x29111994 { return Err(DemuxerError::InvalidData); } @@ -36,50 +62,58 @@ impl<'a> NADemuxer<'a> for GremlinVideoDemuxer<'a> { let depth = src.read_u16le()?; let max_fs = src.read_u16le()?; 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); + let mut width = src.read_u16le()?; + let mut height = src.read_u16le()?; + if (width == 0) && (height == 0) { + for el in GDV_SIZE_TABLE { + if el.id == id { + width = el.width; + height = el.height; + break; + } + } + if (width == 0) && (height == 0) { return Err(DemuxerError::InvalidData); } + } if max_fs > 0 { - let vhdr = NAVideoInfo::new(width as u32, height as u32, false, PAL8_FORMAT); + let mut edata: Vec = Vec::with_capacity(768); + if depth == 1 { + edata.resize(768, 0); + src.read_buf(edata.as_mut_slice())?; + } + let vhdr = NAVideoInfo::new(width as usize, height as usize, 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)); + let vinfo = NACodecInfo::new("gdv-video", vci, if edata.len() == 0 { None } else { Some(edata) }); + self.v_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps as u32)); } 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)); + let packed = if (aflags & 8) != 0 { 1 } else { 0 }; + let depth = if (aflags & 4) != 0 { 16 } else { 8 }; + + let ahdr = NAAudioInfo::new(rate as u32, channels as u8, if depth == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2); + let ainfo = NACodecInfo::new(if packed != 0 { "gdv-audio" } else { "pcm" }, + NACodecTypeInfo::Audio(ahdr), None); + self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, rate as u32)); - 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.asize = (((rate / fps) * channels * (depth / 8)) >> 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)?; } self.frames = frames; - self.opened = true; self.state = GDVState::NewFrame; Ok(()) } #[allow(unused_variables)] - fn get_frame(&mut self) -> DemuxerResult { - if !self.opened { return Err(DemuxerError::NoSuchInput); } + fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult { if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); } match self.state { - GDVState::NewFrame if self.asize > 0 => { self.read_achunk() } - _ => { self.read_vchunk() } + GDVState::NewFrame if self.asize > 0 => { self.read_achunk(strmgr) } + _ => { self.read_vchunk(strmgr) } } } #[allow(unused_variables)] fn seek(&mut self, time: u64) -> DemuxerResult<()> { - if !self.opened { return Err(DemuxerError::NoSuchInput); } Err(DemuxerError::NotImplemented) } } @@ -89,47 +123,51 @@ println!("audio chunk size {}({:X})",self.asize,self.asize); } }*/ impl<'a> GremlinVideoDemuxer<'a> { - pub fn new(io: &'a mut ByteReader<'a>) -> Self { + fn new(io: &'a mut ByteReader<'a>) -> Self { GremlinVideoDemuxer { cur_frame: 0, frames: 0, - opened: false, asize: 0, apacked: false, state: GDVState::NewFrame, pktdta: Vec::new(), src: io, - streams: Vec::new() + a_id: None, + v_id: None, } } - 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 { + fn read_achunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult { self.state = GDVState::AudioRead; - let str = self.find_stream(1); - self.src.read_packet(str, Some(self.cur_frame as u64), None, None, true, self.asize) + let str = strmgr.get_stream(self.a_id.unwrap()).unwrap(); + let (tb_num, tb_den) = str.get_timebase(); + let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, tb_num, tb_den); + self.src.read_packet(str, ts, true, self.asize) } - fn read_vchunk(&mut self) -> DemuxerResult { - let str = self.find_stream(0); - let mut src = &mut self.src; + fn read_vchunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult { + let str = strmgr.get_stream(self.v_id.unwrap()).unwrap(); + let src = &mut self.src; let magic = src.read_u16be()?; if magic != 0x0513 { return Err(DemuxerError::InvalidData); } let size = (src.read_u16le()? as usize) + 4; - let tmp = src.read_u32le()?; + let tmp = src.peek_u32le()?; let flags = (tmp & 0xFF) as usize; - src.seek(SeekFrom::Current(-4))?; self.state = GDVState::NewFrame; self.cur_frame = self.cur_frame + 1; - src.read_packet(str, Some((self.cur_frame - 1) as u64), None, None, if (flags & 64) != 0 { true } else { false }, size) + let (tb_num, tb_den) = str.get_timebase(); + let ts = NATimeInfo::new(Some((self.cur_frame - 1) as u64), None, None, tb_num, tb_den); + src.read_packet(str, ts, if (flags & 64) != 0 { true } else { false }, size) + } +} + +pub struct GDVDemuxerCreator { } + +impl DemuxerCreator for GDVDemuxerCreator { + fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box + 'a> { + Box::new(GremlinVideoDemuxer::new(br)) } + fn get_name(&self) -> &'static str { "gdv" } } #[cfg(test)] @@ -143,9 +181,10 @@ mod test { let mut fr = FileReader::new_read(&mut file); let mut br = ByteReader::new(&mut fr); let mut dmx = GremlinVideoDemuxer::new(&mut br); - dmx.open().unwrap(); + let mut sm = StreamManager::new(); + dmx.open(&mut sm).unwrap(); loop { - let pktres = dmx.get_frame(); + let pktres = dmx.get_frame(&mut sm); if let Err(e) = pktres { if (e as i32) == (DemuxerError::EOF as i32) { break; } panic!("error");