5 //use std::collections::HashMap;
13 struct GremlinVideoDemuxer<'a> {
15 src: &'a mut ByteReader<'a>,
27 struct GDVFixedSizes {
32 const GDV_SIZE_TABLE: &[GDVFixedSizes] = &[
33 GDVFixedSizes { id: 0, width: 320, height: 200 },
34 GDVFixedSizes { id: 1, width: 640, height: 200 },
35 GDVFixedSizes { id: 2, width: 320, height: 167 },
36 GDVFixedSizes { id: 3, width: 320, height: 180 },
37 GDVFixedSizes { id: 4, width: 320, height: 400 },
38 GDVFixedSizes { id: 5, width: 320, height: 170 },
39 GDVFixedSizes { id: 6, width: 160, height: 85 },
40 GDVFixedSizes { id: 7, width: 160, height: 83 },
41 GDVFixedSizes { id: 8, width: 160, height: 90 },
42 GDVFixedSizes { id: 9, width: 280, height: 128 },
43 GDVFixedSizes { id: 10, width: 320, height: 240 },
44 GDVFixedSizes { id: 11, width: 320, height: 201 },
45 GDVFixedSizes { id: 16, width: 640, height: 400 },
46 GDVFixedSizes { id: 17, width: 640, height: 200 },
47 GDVFixedSizes { id: 18, width: 640, height: 180 },
48 GDVFixedSizes { id: 19, width: 640, height: 167 },
49 GDVFixedSizes { id: 20, width: 640, height: 170 },
50 GDVFixedSizes { id: 21, width: 320, height: 240 },
53 impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> {
54 #[allow(unused_variables)]
55 fn open(&mut self) -> DemuxerResult<()> {
56 let src = &mut self.src;
57 let magic = src.read_u32le()?;
58 if magic != 0x29111994 { return Err(DemuxerError::InvalidData); }
59 let id = src.read_u16le()?;
60 let frames = src.read_u16le()?;
61 let fps = src.read_u16le()?;
62 let aflags = src.read_u16le()?;
63 let rate = src.read_u16le()?;
64 let depth = src.read_u16le()?;
65 let max_fs = src.read_u16le()?;
67 let mut width = src.read_u16le()?;
68 let mut height = src.read_u16le()?;
69 if (width == 0) && (height == 0) {
70 for el in GDV_SIZE_TABLE {
77 if (width == 0) && (height == 0) { return Err(DemuxerError::InvalidData); }
80 let mut edata: Vec<u8> = Vec::with_capacity(768);
83 src.read_buf(edata.as_mut_slice())?;
85 let vhdr = NAVideoInfo::new(width as usize, height as usize, false, PAL8_FORMAT);
86 let vci = NACodecTypeInfo::Video(vhdr);
87 let vinfo = NACodecInfo::new("gdv-video", vci, if edata.len() == 0 { None } else { Some(edata) });
88 self.v_id = self.dmx.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps as u32));
90 if (aflags & 1) != 0 {
91 let channels = if (aflags & 2) != 0 { 2 } else { 1 };
92 let packed = if (aflags & 8) != 0 { 1 } else { 0 };
93 let depth = if (aflags & 4) != 0 { 16 } else { 8 };
95 let ahdr = NAAudioInfo::new(rate as u32, channels as u8, if depth == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
96 let ainfo = NACodecInfo::new(if packed != 0 { "gdv-audio" } else { "pcm" },
97 NACodecTypeInfo::Audio(ahdr), None);
98 self.a_id = self.dmx.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, rate as u32));
100 self.asize = (((rate / fps) * channels * (depth / 8)) >> packed) as usize;
101 self.apacked = (aflags & 8) != 0;
103 self.frames = frames;
105 self.state = GDVState::NewFrame;
109 #[allow(unused_variables)]
110 fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
111 if !self.opened { return Err(DemuxerError::NoSuchInput); }
112 if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); }
114 GDVState::NewFrame if self.asize > 0 => { self.read_achunk() }
115 _ => { self.read_vchunk() }
119 fn get_num_streams(&self) -> usize { self.dmx.get_num_streams() }
120 fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> { self.dmx.get_stream(idx) }
122 #[allow(unused_variables)]
123 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
124 if !self.opened { return Err(DemuxerError::NoSuchInput); }
125 Err(DemuxerError::NotImplemented)
128 /*impl<'a> Drop for GremlinVideoDemuxer<'a> {
129 #[allow(unused_variables)]
133 impl<'a> GremlinVideoDemuxer<'a> {
134 fn new(io: &'a mut ByteReader<'a>) -> Self {
135 GremlinVideoDemuxer {
141 state: GDVState::NewFrame,
150 fn read_achunk(&mut self) -> DemuxerResult<NAPacket> {
151 self.state = GDVState::AudioRead;
152 let str = self.dmx.get_stream(self.a_id.unwrap()).unwrap();
153 let (tb_num, tb_den) = str.get_timebase();
154 let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, tb_num, tb_den);
155 self.src.read_packet(str, ts, true, self.asize)
158 fn read_vchunk(&mut self) -> DemuxerResult<NAPacket> {
159 let str = self.dmx.get_stream(self.v_id.unwrap()).unwrap();
160 let mut src = &mut self.src;
161 let magic = src.read_u16be()?;
162 if magic != 0x0513 { return Err(DemuxerError::InvalidData); }
163 let size = (src.read_u16le()? as usize) + 4;
164 let tmp = src.peek_u32le()?;
165 let flags = (tmp & 0xFF) as usize;
166 self.state = GDVState::NewFrame;
167 self.cur_frame = self.cur_frame + 1;
168 let (tb_num, tb_den) = str.get_timebase();
169 let ts = NATimeInfo::new(Some((self.cur_frame - 1) as u64), None, None, tb_num, tb_den);
170 src.read_packet(str, ts, if (flags & 64) != 0 { true } else { false }, size)
174 pub struct GDVDemuxerCreator { }
176 impl DemuxerCreator for GDVDemuxerCreator {
177 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<Demux<'a> + 'a> {
178 Box::new(GremlinVideoDemuxer::new(br))
180 fn get_name(&self) -> &'static str { "gdv" }
189 fn test_gdv_demux() {
190 let mut file = File::open("assets/intro1.gdv").unwrap();
191 let mut fr = FileReader::new_read(&mut file);
192 let mut br = ByteReader::new(&mut fr);
193 let mut dmx = GremlinVideoDemuxer::new(&mut br);
196 let pktres = dmx.get_frame();
197 if let Err(e) = pktres {
198 if (e as i32) == (DemuxerError::EOF as i32) { break; }
201 let pkt = pktres.unwrap();
202 println!("Got {}", pkt);