1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
3 //use std::collections::HashMap;
11 struct GremlinVideoDemuxer<'a> {
12 src: &'a mut ByteReader<'a>,
23 struct GDVFixedSizes {
28 const GDV_SIZE_TABLE: &[GDVFixedSizes] = &[
29 GDVFixedSizes { id: 0, width: 320, height: 200 },
30 GDVFixedSizes { id: 1, width: 640, height: 200 },
31 GDVFixedSizes { id: 2, width: 320, height: 167 },
32 GDVFixedSizes { id: 3, width: 320, height: 180 },
33 GDVFixedSizes { id: 4, width: 320, height: 400 },
34 GDVFixedSizes { id: 5, width: 320, height: 170 },
35 GDVFixedSizes { id: 6, width: 160, height: 85 },
36 GDVFixedSizes { id: 7, width: 160, height: 83 },
37 GDVFixedSizes { id: 8, width: 160, height: 90 },
38 GDVFixedSizes { id: 9, width: 280, height: 128 },
39 GDVFixedSizes { id: 10, width: 320, height: 240 },
40 GDVFixedSizes { id: 11, width: 320, height: 201 },
41 GDVFixedSizes { id: 16, width: 640, height: 400 },
42 GDVFixedSizes { id: 17, width: 640, height: 200 },
43 GDVFixedSizes { id: 18, width: 640, height: 180 },
44 GDVFixedSizes { id: 19, width: 640, height: 167 },
45 GDVFixedSizes { id: 20, width: 640, height: 170 },
46 GDVFixedSizes { id: 21, width: 320, height: 240 },
49 impl<'a> DemuxCore<'a> for GremlinVideoDemuxer<'a> {
50 #[allow(unused_variables)]
51 fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
52 let src = &mut self.src;
53 let magic = src.read_u32le()?;
54 if magic != 0x29111994 { return Err(DemuxerError::InvalidData); }
55 let id = src.read_u16le()?;
56 let frames = src.read_u16le()?;
57 let fps = src.read_u16le()?;
58 let aflags = src.read_u16le()?;
59 let rate = src.read_u16le()?;
60 let depth = src.read_u16le()?;
61 let max_fs = src.read_u16le()?;
63 let mut width = src.read_u16le()?;
64 let mut height = src.read_u16le()?;
65 if (width == 0) && (height == 0) {
66 for el in GDV_SIZE_TABLE {
73 if (width == 0) && (height == 0) { return Err(DemuxerError::InvalidData); }
76 let mut edata: Vec<u8> = Vec::with_capacity(768);
79 src.read_buf(edata.as_mut_slice())?;
81 let vhdr = NAVideoInfo::new(width as usize, height as usize, false, PAL8_FORMAT);
82 let vci = NACodecTypeInfo::Video(vhdr);
83 let vinfo = NACodecInfo::new("gdv-video", vci, if edata.is_empty() { None } else { Some(edata) });
84 self.v_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, u32::from(fps), u64::from(frames)));
86 if (aflags & 1) != 0 {
87 let channels = if (aflags & 2) != 0 { 2 } else { 1 };
88 let packed = if (aflags & 8) != 0 { 1 } else { 0 };
89 let depth = if (aflags & 4) != 0 { 16 } else { 8 };
91 let ahdr = NAAudioInfo::new(u32::from(rate), channels as u8, if depth == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
92 let ainfo = NACodecInfo::new(if packed != 0 { "gdv-audio" } else { "pcm" },
93 NACodecTypeInfo::Audio(ahdr), None);
94 self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, u32::from(rate), 0));
96 self.asize = (((rate / fps) * channels * (depth / 8)) >> packed) as usize;
97 self.apacked = (aflags & 8) != 0;
100 self.state = GDVState::NewFrame;
104 #[allow(unused_variables)]
105 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
106 if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); }
108 GDVState::NewFrame if self.asize > 0 => { self.read_achunk(strmgr) }
109 _ => { self.read_vchunk(strmgr) }
113 fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
114 Err(DemuxerError::NotPossible)
116 fn get_duration(&self) -> u64 { 0 }
118 impl<'a> NAOptionHandler for GremlinVideoDemuxer<'a> {
119 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
120 fn set_options(&mut self, _options: &[NAOption]) { }
121 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
123 /*impl<'a> Drop for GremlinVideoDemuxer<'a> {
124 #[allow(unused_variables)]
128 impl<'a> GremlinVideoDemuxer<'a> {
129 fn new(io: &'a mut ByteReader<'a>) -> Self {
130 GremlinVideoDemuxer {
135 state: GDVState::NewFrame,
143 fn read_achunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
144 self.state = GDVState::AudioRead;
145 let str = strmgr.get_stream(self.a_id.unwrap()).unwrap();
146 let (tb_num, tb_den) = str.get_timebase();
147 let ts = NATimeInfo::new(Some(u64::from(self.cur_frame)), None, None, tb_num, tb_den);
148 self.src.read_packet(str, ts, true, self.asize)
151 fn read_vchunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
152 let str = strmgr.get_stream(self.v_id.unwrap()).unwrap();
153 let src = &mut self.src;
154 let magic = src.read_u16be()?;
155 if magic != 0x0513 { return Err(DemuxerError::InvalidData); }
156 let size = (src.read_u16le()? as usize) + 4;
157 let tmp = src.peek_u32le()?;
158 let flags = (tmp & 0xFF) as usize;
159 self.state = GDVState::NewFrame;
161 let (tb_num, tb_den) = str.get_timebase();
162 let ts = NATimeInfo::new(Some(u64::from(self.cur_frame - 1)), None, None, tb_num, tb_den);
163 src.read_packet(str, ts, (flags & 64) != 0, size)
167 pub struct GDVDemuxerCreator { }
169 impl DemuxerCreator for GDVDemuxerCreator {
170 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
171 Box::new(GremlinVideoDemuxer::new(br))
173 fn get_name(&self) -> &'static str { "gdv" }
182 fn test_gdv_demux() {
183 let mut file = File::open("assets/Game/intro1.gdv").unwrap();
184 let mut fr = FileReader::new_read(&mut file);
185 let mut br = ByteReader::new(&mut fr);
186 let mut dmx = GremlinVideoDemuxer::new(&mut br);
187 let mut sm = StreamManager::new();
188 let mut si = SeekIndex::new();
189 dmx.open(&mut sm, &mut si).unwrap();
191 let pktres = dmx.get_frame(&mut sm);
192 if let Err(e) = pktres {
193 if (e as i32) == (DemuxerError::EOF as i32) { break; }
196 let pkt = pktres.unwrap();
197 println!("Got {}", pkt);