6acacc141aeac9c241dc878a3f48fd1eb627d545
[nihav.git] / nihav-game / src / demuxers / gdv.rs
1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
3 use nihav_core::io::byteio::*;
4 use nihav_core::formats::*;
5 //use std::collections::HashMap;
6
7 enum GDVState {
8 NewFrame,
9 AudioRead,
10 }
11
12 #[allow(dead_code)]
13 struct GremlinVideoDemuxer<'a> {
14 src: &'a mut ByteReader<'a>,
15 frames: u16,
16 cur_frame: u16,
17 asize: usize,
18 apacked: bool,
19 state: GDVState,
20 pktdta: Vec<u8>,
21 a_id: Option<usize>,
22 v_id: Option<usize>,
23 }
24
25 struct GDVFixedSizes {
26 id: u16,
27 width: u16,
28 height: u16,
29 }
30 const GDV_SIZE_TABLE: &[GDVFixedSizes] = &[
31 GDVFixedSizes { id: 0, width: 320, height: 200 },
32 GDVFixedSizes { id: 1, width: 640, height: 200 },
33 GDVFixedSizes { id: 2, width: 320, height: 167 },
34 GDVFixedSizes { id: 3, width: 320, height: 180 },
35 GDVFixedSizes { id: 4, width: 320, height: 400 },
36 GDVFixedSizes { id: 5, width: 320, height: 170 },
37 GDVFixedSizes { id: 6, width: 160, height: 85 },
38 GDVFixedSizes { id: 7, width: 160, height: 83 },
39 GDVFixedSizes { id: 8, width: 160, height: 90 },
40 GDVFixedSizes { id: 9, width: 280, height: 128 },
41 GDVFixedSizes { id: 10, width: 320, height: 240 },
42 GDVFixedSizes { id: 11, width: 320, height: 201 },
43 GDVFixedSizes { id: 16, width: 640, height: 400 },
44 GDVFixedSizes { id: 17, width: 640, height: 200 },
45 GDVFixedSizes { id: 18, width: 640, height: 180 },
46 GDVFixedSizes { id: 19, width: 640, height: 167 },
47 GDVFixedSizes { id: 20, width: 640, height: 170 },
48 GDVFixedSizes { id: 21, width: 320, height: 240 },
49 ];
50
51 impl<'a> DemuxCore<'a> for GremlinVideoDemuxer<'a> {
52 #[allow(unused_variables)]
53 fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
54 let src = &mut self.src;
55 let magic = src.read_u32le()?;
56 if magic != 0x29111994 { return Err(DemuxerError::InvalidData); }
57 let id = src.read_u16le()?;
58 let frames = src.read_u16le()?;
59 let fps = src.read_u16le()?;
60 let aflags = src.read_u16le()?;
61 let rate = src.read_u16le()?;
62 let depth = src.read_u16le()?;
63 let max_fs = src.read_u16le()?;
64 src.read_skip(2)?;
65 let mut width = src.read_u16le()?;
66 let mut height = src.read_u16le()?;
67 if (width == 0) && (height == 0) {
68 for el in GDV_SIZE_TABLE {
69 if el.id == id {
70 width = el.width;
71 height = el.height;
72 break;
73 }
74 }
75 if (width == 0) && (height == 0) { return Err(DemuxerError::InvalidData); }
76 }
77 if max_fs > 0 {
78 let mut edata: Vec<u8> = Vec::with_capacity(768);
79 if depth == 1 {
80 edata.resize(768, 0);
81 src.read_buf(edata.as_mut_slice())?;
82 }
83 let vhdr = NAVideoInfo::new(width as usize, height as usize, false, PAL8_FORMAT);
84 let vci = NACodecTypeInfo::Video(vhdr);
85 let vinfo = NACodecInfo::new("gdv-video", vci, if edata.len() == 0 { None } else { Some(edata) });
86 self.v_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps as u32));
87 }
88 if (aflags & 1) != 0 {
89 let channels = if (aflags & 2) != 0 { 2 } else { 1 };
90 let packed = if (aflags & 8) != 0 { 1 } else { 0 };
91 let depth = if (aflags & 4) != 0 { 16 } else { 8 };
92
93 let ahdr = NAAudioInfo::new(rate as u32, channels as u8, if depth == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
94 let ainfo = NACodecInfo::new(if packed != 0 { "gdv-audio" } else { "pcm" },
95 NACodecTypeInfo::Audio(ahdr), None);
96 self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, rate as u32));
97
98 self.asize = (((rate / fps) * channels * (depth / 8)) >> packed) as usize;
99 self.apacked = (aflags & 8) != 0;
100 }
101 self.frames = frames;
102 self.state = GDVState::NewFrame;
103 Ok(())
104 }
105
106 #[allow(unused_variables)]
107 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
108 if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); }
109 match self.state {
110 GDVState::NewFrame if self.asize > 0 => { self.read_achunk(strmgr) }
111 _ => { self.read_vchunk(strmgr) }
112 }
113 }
114
115 #[allow(unused_variables)]
116 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
117 Err(DemuxerError::NotImplemented)
118 }
119 }
120 /*impl<'a> Drop for GremlinVideoDemuxer<'a> {
121 #[allow(unused_variables)]
122 fn drop(&mut self) {
123 }
124 }*/
125 impl<'a> GremlinVideoDemuxer<'a> {
126 fn new(io: &'a mut ByteReader<'a>) -> Self {
127 GremlinVideoDemuxer {
128 cur_frame: 0,
129 frames: 0,
130 asize: 0,
131 apacked: false,
132 state: GDVState::NewFrame,
133 pktdta: Vec::new(),
134 src: io,
135 a_id: None,
136 v_id: None,
137 }
138 }
139
140 fn read_achunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
141 self.state = GDVState::AudioRead;
142 let str = strmgr.get_stream(self.a_id.unwrap()).unwrap();
143 let (tb_num, tb_den) = str.get_timebase();
144 let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, tb_num, tb_den);
145 self.src.read_packet(str, ts, true, self.asize)
146 }
147
148 fn read_vchunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
149 let str = strmgr.get_stream(self.v_id.unwrap()).unwrap();
150 let src = &mut self.src;
151 let magic = src.read_u16be()?;
152 if magic != 0x0513 { return Err(DemuxerError::InvalidData); }
153 let size = (src.read_u16le()? as usize) + 4;
154 let tmp = src.peek_u32le()?;
155 let flags = (tmp & 0xFF) as usize;
156 self.state = GDVState::NewFrame;
157 self.cur_frame = self.cur_frame + 1;
158 let (tb_num, tb_den) = str.get_timebase();
159 let ts = NATimeInfo::new(Some((self.cur_frame - 1) as u64), None, None, tb_num, tb_den);
160 src.read_packet(str, ts, if (flags & 64) != 0 { true } else { false }, size)
161 }
162 }
163
164 pub struct GDVDemuxerCreator { }
165
166 impl DemuxerCreator for GDVDemuxerCreator {
167 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a> {
168 Box::new(GremlinVideoDemuxer::new(br))
169 }
170 fn get_name(&self) -> &'static str { "gdv" }
171 }
172
173 #[cfg(test)]
174 mod test {
175 use super::*;
176 use std::fs::File;
177
178 #[test]
179 fn test_gdv_demux() {
180 let mut file = File::open("assets/intro1.gdv").unwrap();
181 let mut fr = FileReader::new_read(&mut file);
182 let mut br = ByteReader::new(&mut fr);
183 let mut dmx = GremlinVideoDemuxer::new(&mut br);
184 let mut sm = StreamManager::new();
185 dmx.open(&mut sm).unwrap();
186 loop {
187 let pktres = dmx.get_frame(&mut sm);
188 if let Err(e) = pktres {
189 if (e as i32) == (DemuxerError::EOF as i32) { break; }
190 panic!("error");
191 }
192 let pkt = pktres.unwrap();
193 println!("Got {}", pkt);
194 }
195 }
196 }