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) -> 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.len() > 0 {
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 += (nblocks as u64) * 32;
55 self.pkt_buf.push(apkt);
59 let mut buf: Vec<u8> = Vec::with_capacity(size - asize + 1);
60 buf.resize(size - asize + 1, 0);
62 self.src.read_buf(&mut buf[1..])?;
64 let str = strmgr.get_stream(self.vid_id).unwrap();
65 let (tb_num, tb_den) = str.get_timebase();
66 let ts = NATimeInfo::new(Some(self.vpos), None, None, tb_num, tb_den);
67 let pkt = NAPacket::new(str, ts, (ctype & 3) == 3, buf);
74 #[allow(unused_variables)]
75 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
76 Err(DemuxerError::NotImplemented)
80 impl<'a> BMVDemuxer<'a> {
81 fn new(io: &'a mut ByteReader<'a>) -> Self {
88 pkt_buf: Vec::with_capacity(1),
93 pub struct BMVDemuxerCreator { }
95 impl DemuxerCreator for BMVDemuxerCreator {
96 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a> {
97 Box::new(BMVDemuxer::new(br))
99 fn get_name(&self) -> &'static str { "bmv" }
108 fn test_bmv_demux() {
109 let mut file = File::open("assets/Game/DW2-MOUSE.BMV").unwrap();
110 let mut fr = FileReader::new_read(&mut file);
111 let mut br = ByteReader::new(&mut fr);
112 let mut dmx = BMVDemuxer::new(&mut br);
113 let mut sm = StreamManager::new();
114 dmx.open(&mut sm).unwrap();
116 let pktres = dmx.get_frame(&mut sm);
117 if let Err(e) = pktres {
118 if (e as i32) == (DemuxerError::EOF as i32) { break; }
121 let pkt = pktres.unwrap();
122 println!("Got {}", pkt);