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