1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
4 struct BMVDemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
10 pkt_buf: Vec<NAPacket>,
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;
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();
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();
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());
38 let ctype = self.src.read_byte()?;
39 if ctype == 0 { // NOP chunk
42 if ctype == 1 { return Err(DemuxerError::EOF); }
43 let size = self.src.read_u24le()? as usize;
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);
59 let mut buf: Vec<u8> = vec![0; size - asize + 1];
61 self.src.read_buf(&mut buf[1..])?;
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);
73 fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> {
74 Err(DemuxerError::NotPossible)
78 impl<'a> BMVDemuxer<'a> {
79 fn new(io: &'a mut ByteReader<'a>) -> Self {
86 pkt_buf: Vec::with_capacity(1),
91 pub struct BMVDemuxerCreator { }
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))
97 fn get_name(&self) -> &'static str { "bmv" }
100 struct BMV3Demuxer<'a> {
101 src: &'a mut ByteReader<'a>,
108 pkt_buf: Vec<NAPacket>,
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;
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;
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();
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();
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());
164 let ctype = self.src.read_byte()?;
165 if ctype == 0 { // NOP chunk
168 if ctype == 1 { return Err(DemuxerError::EOF); }
169 let size = self.src.read_u24le()? as usize;
170 if size == 0 { continue; }
172 if (ctype & 0x20) != 0 {
173 if (ctype & 0x40) != 0 {
174 asize = self.asize - self.ablob;
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..])?;
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);
188 self.apos += (asize as u64) / 41 * 32;
189 self.pkt_buf.push(apkt);
194 if !self.pkt_buf.is_empty() {
195 return Ok(self.pkt_buf.pop().unwrap());
200 let mut buf: Vec<u8> = vec![0; size - asize + 1];
202 self.src.read_buf(&mut buf[1..])?;
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);
214 fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> {
215 Err(DemuxerError::NotPossible)
219 impl<'a> BMV3Demuxer<'a> {
220 fn new(io: &'a mut ByteReader<'a>) -> Self {
229 pkt_buf: Vec::with_capacity(1),
234 pub struct BMV3DemuxerCreator { }
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))
240 fn get_name(&self) -> &'static str { "bmv3" }
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();
258 let pktres = dmx.get_frame(&mut sm);
259 if let Err(e) = pktres {
260 if (e as i32) == (DemuxerError::EOF as i32) { break; }
263 let pkt = pktres.unwrap();
264 println!("Got {}", pkt);
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();
277 let pktres = dmx.get_frame(&mut sm);
278 if let Err(e) = pktres {
279 if (e as i32) == (DemuxerError::EOF as i32) { break; }
282 let pkt = pktres.unwrap();
283 println!("Got {}", pkt);