]> git.nihav.org Git - nihav.git/blame - nihav-game/src/demuxers/bmv.rs
introduce option handling for demuxers
[nihav.git] / nihav-game / src / demuxers / bmv.rs
CommitLineData
9067c1f8
KS
1use nihav_core::frame::*;
2use nihav_core::demuxers::*;
3
4struct BMVDemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
6 vid_id: usize,
7 aud_id: usize,
8 vpos: u64,
9 apos: u64,
10 pkt_buf: Vec<NAPacket>,
11}
12
13impl<'a> DemuxCore<'a> for BMVDemuxer<'a> {
14 #[allow(unused_variables)]
33b5a8f0 15 fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
9067c1f8
KS
16 let src = &mut self.src;
17
18 let vhdr = NAVideoInfo::new(640, 429, false, PAL8_FORMAT);
19 let vci = NACodecTypeInfo::Video(vhdr);
20 let vinfo = NACodecInfo::new("bmv-video", vci, None);
21 self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, 12)).unwrap();
22
23 let ahdr = NAAudioInfo::new(22050, 2, SND_S16_FORMAT, 1);
24 let ainfo = NACodecInfo::new("bmv-audio", NACodecTypeInfo::Audio(ahdr), None);
25 self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, 22050)).unwrap();
26
27 self.vpos = 0;
28 self.apos = 0;
29 Ok(())
30 }
31
32 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
e69b1148 33 if !self.pkt_buf.is_empty() {
9067c1f8
KS
34 return Ok(self.pkt_buf.pop().unwrap());
35 }
36
37 loop {
38 let ctype = self.src.read_byte()?;
39 if ctype == 0 { // NOP chunk
40 continue;
41 }
42 if ctype == 1 { return Err(DemuxerError::EOF); }
43 let size = self.src.read_u24le()? as usize;
44 validate!(size > 0);
45 let asize;
46 if (ctype & 0x20) != 0 {
47 let nblocks = self.src.peek_byte()?;
48 asize = (nblocks as usize) * 65 + 1;
49 validate!(asize < size);
50 let str = strmgr.get_stream(self.aud_id).unwrap();
51 let (tb_num, tb_den) = str.get_timebase();
52 let ts = NATimeInfo::new(Some(self.apos), None, None, tb_num, tb_den);
53 let apkt = self.src.read_packet(str, ts, false, asize)?;
e69b1148 54 self.apos += u64::from(nblocks) * 32;
9067c1f8
KS
55 self.pkt_buf.push(apkt);
56 } else {
57 asize = 0;
58 }
e69b1148 59 let mut buf: Vec<u8> = vec![0; size - asize + 1];
9067c1f8
KS
60 buf[0] = ctype;
61 self.src.read_buf(&mut buf[1..])?;
62
63 let str = strmgr.get_stream(self.vid_id).unwrap();
64 let (tb_num, tb_den) = str.get_timebase();
65 let ts = NATimeInfo::new(Some(self.vpos), None, None, tb_num, tb_den);
66 let pkt = NAPacket::new(str, ts, (ctype & 3) == 3, buf);
67
68 self.vpos += 1;
69 return Ok(pkt);
70 }
71 }
72
33b5a8f0
KS
73 fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> {
74 Err(DemuxerError::NotPossible)
9067c1f8
KS
75 }
76}
77
787b8d03
KS
78impl<'a> NAOptionHandler for BMVDemuxer<'a> {
79 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
80 fn set_options(&mut self, _options: &[NAOption]) { }
81 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
82}
83
9067c1f8
KS
84impl<'a> BMVDemuxer<'a> {
85 fn new(io: &'a mut ByteReader<'a>) -> Self {
86 Self {
87 src: io,
88 vid_id: 0,
89 aud_id: 0,
90 vpos: 0,
91 apos: 0,
92 pkt_buf: Vec::with_capacity(1),
93 }
94 }
95}
96
97pub struct BMVDemuxerCreator { }
98
99impl DemuxerCreator for BMVDemuxerCreator {
6011e201 100 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
9067c1f8
KS
101 Box::new(BMVDemuxer::new(br))
102 }
103 fn get_name(&self) -> &'static str { "bmv" }
104}
105
ecda1cc1
KS
106struct BMV3Demuxer<'a> {
107 src: &'a mut ByteReader<'a>,
108 vid_id: usize,
109 aud_id: usize,
110 vpos: u64,
111 apos: u64,
112 asize: usize,
113 ablob: usize,
114 pkt_buf: Vec<NAPacket>,
115}
116
117impl<'a> DemuxCore<'a> for BMV3Demuxer<'a> {
118 #[allow(unused_variables)]
33b5a8f0 119 fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
ecda1cc1
KS
120 let src = &mut self.src;
121
122 let mut magic = [0u8; 4];
123 src.read_buf(&mut magic)?;
124 validate!(&magic[0..] == b"BMVi");
125 let size = src.read_u32le()?;
126 validate!(size == 24);
127 let _slot_size = src.read_u32le()? as usize;
128 let nframes = src.read_u32le()? as usize;
129 let _prefetch_slots = src.read_u16le()?;
130 let _cache_size = src.read_u16le()?;
131 let fps = src.read_u16le()? as u32;
132 let audio_size = src.read_u16le()? as usize;
133 let audio_blob_size = src.read_u16le()? as usize;
134 let _audio_id = src.read_byte()?;
135 let _video_id = src.read_byte()?;
136 let width = src.read_u16le()? as usize;
137 let height = src.read_u16le()? as usize;
138 validate!(nframes > 0);
139 validate!((width > 0) && (width <= 640));
140 validate!((height > 0) && (height <= 432));
141 validate!((audio_size > audio_blob_size) && (audio_blob_size > 0) && (audio_size % audio_blob_size == 0));
142 let mut dta = [0u8; 4];
143 src.read_buf(&mut dta)?;
144 validate!(&dta[0..] == b"DATA");
145 let data_size = src.read_u32le()? as usize;
146 validate!(data_size > 0);
147 self.asize = audio_size;
148 self.ablob = audio_blob_size;
149
150 let vhdr = NAVideoInfo::new(width, height, false, RGB565_FORMAT);
151 let vci = NACodecTypeInfo::Video(vhdr);
152 let vinfo = NACodecInfo::new("bmv3-video", vci, None);
153 self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 256, fps)).unwrap();
154
155 let ahdr = NAAudioInfo::new(22050, 2, SND_S16_FORMAT, audio_blob_size);
156 let ainfo = NACodecInfo::new("bmv3-audio", NACodecTypeInfo::Audio(ahdr), None);
157 self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, 22050)).unwrap();
158
159 self.vpos = 0;
160 self.apos = 0;
161 Ok(())
162 }
163
164 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
e69b1148 165 if !self.pkt_buf.is_empty() {
ecda1cc1
KS
166 return Ok(self.pkt_buf.pop().unwrap());
167 }
168
169 loop {
170 let ctype = self.src.read_byte()?;
171 if ctype == 0 { // NOP chunk
172 continue;
173 }
174 if ctype == 1 { return Err(DemuxerError::EOF); }
175 let size = self.src.read_u24le()? as usize;
176 if size == 0 { continue; }
177 let asize;
178 if (ctype & 0x20) != 0 {
179 if (ctype & 0x40) != 0 {
180 asize = self.asize - self.ablob;
181 } else {
182 asize = self.asize;
183 }
184 validate!(asize <= size);
e69b1148 185 let mut buf: Vec<u8> = vec![0; asize + 1];
ecda1cc1
KS
186 buf[0] = (self.src.tell() & 1) as u8;
187 self.src.read_buf(&mut buf[1..])?;
188
189 let str = strmgr.get_stream(self.aud_id).unwrap();
190 let (tb_num, tb_den) = str.get_timebase();
191 let ts = NATimeInfo::new(Some(self.apos), None, None, tb_num, tb_den);
192 let apkt = NAPacket::new(str, ts, false, buf);
193
194 self.apos += (asize as u64) / 41 * 32;
195 self.pkt_buf.push(apkt);
196 } else {
197 asize = 0;
198 }
726973ae 199 if size == asize {
e69b1148 200 if !self.pkt_buf.is_empty() {
726973ae
KS
201 return Ok(self.pkt_buf.pop().unwrap());
202 } else {
203 continue;
204 }
205 }
e69b1148 206 let mut buf: Vec<u8> = vec![0; size - asize + 1];
ecda1cc1
KS
207 buf[0] = ctype;
208 self.src.read_buf(&mut buf[1..])?;
209
210 let str = strmgr.get_stream(self.vid_id).unwrap();
211 let (tb_num, tb_den) = str.get_timebase();
212 let ts = NATimeInfo::new(Some(self.vpos), None, None, tb_num, tb_den);
213 let pkt = NAPacket::new(str, ts, (ctype & 3) == 3, buf);
214
215 self.vpos += 1;
216 return Ok(pkt);
217 }
218 }
219
33b5a8f0
KS
220 fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> {
221 Err(DemuxerError::NotPossible)
ecda1cc1
KS
222 }
223}
224
787b8d03
KS
225impl<'a> NAOptionHandler for BMV3Demuxer<'a> {
226 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
227 fn set_options(&mut self, _options: &[NAOption]) { }
228 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
229}
230
ecda1cc1
KS
231impl<'a> BMV3Demuxer<'a> {
232 fn new(io: &'a mut ByteReader<'a>) -> Self {
233 Self {
234 src: io,
235 vid_id: 0,
236 aud_id: 0,
237 vpos: 0,
238 apos: 0,
239 asize: 0,
240 ablob: 0,
241 pkt_buf: Vec::with_capacity(1),
242 }
243 }
244}
245
246pub struct BMV3DemuxerCreator { }
247
248impl DemuxerCreator for BMV3DemuxerCreator {
6011e201 249 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
ecda1cc1
KS
250 Box::new(BMV3Demuxer::new(br))
251 }
252 fn get_name(&self) -> &'static str { "bmv3" }
253}
254
9067c1f8
KS
255#[cfg(test)]
256mod test {
257 use super::*;
258 use std::fs::File;
259
260 #[test]
261 fn test_bmv_demux() {
1678d59a 262 let mut file = File::open("assets/Game/DW2-MOUSE.BMV").unwrap();
9067c1f8
KS
263 let mut fr = FileReader::new_read(&mut file);
264 let mut br = ByteReader::new(&mut fr);
265 let mut dmx = BMVDemuxer::new(&mut br);
266 let mut sm = StreamManager::new();
caf0f37e
KS
267 let mut si = SeekIndex::new();
268 dmx.open(&mut sm, &mut si).unwrap();
9067c1f8
KS
269 loop {
270 let pktres = dmx.get_frame(&mut sm);
271 if let Err(e) = pktres {
272 if (e as i32) == (DemuxerError::EOF as i32) { break; }
273 panic!("error");
274 }
275 let pkt = pktres.unwrap();
276 println!("Got {}", pkt);
277 }
278 }
ecda1cc1
KS
279 #[test]
280 fn test_bmv3_demux() {
281 let mut file = File::open("assets/Game/DW3-Loffnote.bmv").unwrap();
282 let mut fr = FileReader::new_read(&mut file);
283 let mut br = ByteReader::new(&mut fr);
284 let mut dmx = BMV3Demuxer::new(&mut br);
285 let mut sm = StreamManager::new();
caf0f37e
KS
286 let mut si = SeekIndex::new();
287 dmx.open(&mut sm, &mut si).unwrap();
ecda1cc1
KS
288 loop {
289 let pktres = dmx.get_frame(&mut sm);
290 if let Err(e) = pktres {
291 if (e as i32) == (DemuxerError::EOF as i32) { break; }
292 panic!("error");
293 }
294 let pkt = pktres.unwrap();
295 println!("Got {}", pkt);
296 }
297 }
9067c1f8 298}