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