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