]> git.nihav.org Git - nihav.git/blob - bmv.rs
8baa6e4eca3fbf77735e4f24f27c55b78a95193f
[nihav.git] / 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) -> 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.len() > 0 {
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 += (nblocks as u64) * 32;
55 self.pkt_buf.push(apkt);
56 } else {
57 asize = 0;
58 }
59 let mut buf: Vec<u8> = Vec::with_capacity(size - asize + 1);
60 buf.resize(size - asize + 1, 0);
61 buf[0] = ctype;
62 self.src.read_buf(&mut buf[1..])?;
63
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);
68
69 self.vpos += 1;
70 return Ok(pkt);
71 }
72 }
73
74 #[allow(unused_variables)]
75 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
76 Err(DemuxerError::NotImplemented)
77 }
78 }
79
80 impl<'a> BMVDemuxer<'a> {
81 fn new(io: &'a mut ByteReader<'a>) -> Self {
82 Self {
83 src: io,
84 vid_id: 0,
85 aud_id: 0,
86 vpos: 0,
87 apos: 0,
88 pkt_buf: Vec::with_capacity(1),
89 }
90 }
91 }
92
93 pub struct BMVDemuxerCreator { }
94
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))
98 }
99 fn get_name(&self) -> &'static str { "bmv" }
100 }
101
102 #[cfg(test)]
103 mod test {
104 use super::*;
105 use std::fs::File;
106
107 #[test]
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();
115 loop {
116 let pktres = dmx.get_frame(&mut sm);
117 if let Err(e) = pktres {
118 if (e as i32) == (DemuxerError::EOF as i32) { break; }
119 panic!("error");
120 }
121 let pkt = pktres.unwrap();
122 println!("Got {}", pkt);
123 }
124 }
125 }