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