]> git.nihav.org Git - nihav.git/blob - vmd.rs
b670b63b406ace4f212088b2c95a11c5650253b1
[nihav.git] / vmd.rs
1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
3 use std::io::SeekFrom;
4
5 const HEADER_SIZE: usize = 0x330;
6 const FRAME_HDR_SIZE: usize = 10;
7
8 const CHTYPE_VIDEO: u8 = 0x02;
9 const CHTYPE_AUDIO: u8 = 0x01;
10
11 #[derive(Clone,Copy)]
12 struct FrameRec {
13 chtype: u8,
14 size: u32,
15 off: u32,
16 hdr: [u8; FRAME_HDR_SIZE],
17 ts: u32,
18 }
19
20 struct VMDDemuxer<'a> {
21 src: &'a mut ByteReader<'a>,
22 vid_id: usize,
23 aud_id: usize,
24 fno: usize,
25 is_indeo: bool,
26 frames: Vec<FrameRec>,
27 }
28
29 impl<'a> DemuxCore<'a> for VMDDemuxer<'a> {
30 #[allow(unused_variables)]
31 fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
32 let src = &mut self.src;
33
34 let mut header: [u8; HEADER_SIZE] = [0; HEADER_SIZE];
35 src.read_buf(&mut header)?;
36
37 let mut width = read_u16le(&header[12..])? as usize;
38 let mut height = read_u16le(&header[14..])? as usize;
39 self.is_indeo = &header[24..27] == b"iv3";
40 if self.is_indeo && width > 320 {
41 width >>= 1;
42 height >>= 1;
43 }
44
45 let nframes = read_u16le(&header[6..])? as usize;
46 let fpb = read_u16le(&header[18..])? as usize;
47 validate!(nframes > 0 && fpb > 0);
48
49 let mut edata: Vec<u8> = Vec::with_capacity(HEADER_SIZE);
50 edata.extend_from_slice(&header);
51 let vhdr = NAVideoInfo::new(width, height, false, PAL8_FORMAT);
52 let vci = NACodecTypeInfo::Video(vhdr);
53 let vinfo = NACodecInfo::new(if !self.is_indeo { "vmd-video" } else { "indeo3" }, vci, Some(edata));
54 self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, 12)).unwrap();
55
56 let srate = read_u16le(&header[804..])? as u32;
57 let block_size;
58 if srate > 0 {
59 let bsize = read_u16le(&header[806..])? as usize;
60 let channels = if (header[811] & 0x8F) != 0 { 2 } else { 1 };
61 let is16bit;
62 if (bsize & 0x8000) != 0 {
63 is16bit = true;
64 block_size = 0x10000 - bsize;
65 } else {
66 is16bit = false;
67 block_size = bsize;
68 }
69
70 let ahdr = NAAudioInfo::new(srate, channels, if is16bit { SND_S16P_FORMAT } else { SND_U8_FORMAT }, block_size);
71 let ainfo = NACodecInfo::new("vmd-audio", NACodecTypeInfo::Audio(ahdr), None);
72 self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, srate)).unwrap();
73 } else {
74 block_size = 0;
75 }
76
77 let adelay = read_u16le(&header[808..])? as u32;
78 let idx_off = read_u32le(&header[812..])? as u64;
79 src.seek(SeekFrom::Start(idx_off))?;
80 let mut offs: Vec<u32> = Vec::with_capacity(nframes);
81 for i in 0..nframes {
82 let _flags = src.read_u16le()?;
83 let off = src.read_u32le()?;
84 offs.push(off);
85 }
86 self.frames.reserve(nframes * fpb);
87 let mut ats = adelay;
88 for i in 0..nframes {
89 let mut off = offs[i];
90 for _ in 0..fpb {
91 let chtype = src.read_byte()?;
92 src.read_skip(1)?;
93 let mut size = src.read_u32le()?;
94 let mut hdr: [u8; FRAME_HDR_SIZE] = [0; FRAME_HDR_SIZE];
95 src.read_buf(&mut hdr)?;
96 if (i == 0) && (chtype == CHTYPE_AUDIO) && (size > 4) && ((size as usize) < block_size/2) {
97 size += 0x10000;
98 }
99 if (chtype == CHTYPE_VIDEO || chtype == CHTYPE_AUDIO) && (size > 0) {
100 let ts = if (i == 0) || (chtype != CHTYPE_AUDIO) {
101 i as u32
102 } else {
103 ats
104 };
105 self.frames.push(FrameRec { chtype, size, hdr, off, ts });
106 }
107 if i > 0 && chtype == CHTYPE_AUDIO {
108 ats += 1;
109 }
110 if chtype != 0 {
111 validate!(off.checked_add(size).is_some());
112 off += size;
113 }
114 }
115 }
116
117 self.fno = 0;
118 Ok(())
119 }
120
121 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
122 if self.fno >= self.frames.len() { return Err(DemuxerError::EOF); }
123 let cur_frame = &self.frames[self.fno];
124 //println!("fno {} -> type {} size {} @ {:X} ts {}", self.fno, cur_frame.chtype, cur_frame.size, cur_frame.off, cur_frame.ts);
125 let next_pos = cur_frame.off as u64;
126 if self.src.tell() != next_pos {
127 self.src.seek(SeekFrom::Start(next_pos))?;
128 }
129
130 let is_video = cur_frame.chtype == CHTYPE_VIDEO;
131 let mut buf: Vec<u8> = Vec::with_capacity(FRAME_HDR_SIZE + (cur_frame.size as usize));
132 if !self.is_indeo || !is_video {
133 buf.extend_from_slice(&cur_frame.hdr);
134 buf.resize(FRAME_HDR_SIZE + (cur_frame.size as usize), 0);
135 self.src.read_buf(&mut buf[FRAME_HDR_SIZE..])?;
136 } else {
137 buf.resize(cur_frame.size as usize, 0);
138 self.src.read_buf(&mut buf)?;
139 }
140
141 self.fno += 1;
142
143 let str_id = if is_video { self.vid_id } else { self.aud_id };
144 let str = strmgr.get_stream(str_id).unwrap();
145 let (tb_num, tb_den) = str.get_timebase();
146 let ts = NATimeInfo::new(Some(cur_frame.ts as u64), None, None, tb_num, tb_den);
147 let pkt = NAPacket::new(str, ts, false, buf);
148
149 Ok(pkt)
150 }
151
152 #[allow(unused_variables)]
153 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
154 Err(DemuxerError::NotImplemented)
155 }
156 }
157
158 impl<'a> VMDDemuxer<'a> {
159 fn new(io: &'a mut ByteReader<'a>) -> Self {
160 Self {
161 src: io,
162 vid_id: 0,
163 aud_id: 0,
164 fno: 0,
165 is_indeo: false,
166 frames: Vec::new(),
167 }
168 }
169 }
170
171 pub struct VMDDemuxerCreator { }
172
173 impl DemuxerCreator for VMDDemuxerCreator {
174 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a> {
175 Box::new(VMDDemuxer::new(br))
176 }
177 fn get_name(&self) -> &'static str { "vmd" }
178 }
179
180 #[cfg(test)]
181 mod test {
182 use super::*;
183 use std::fs::File;
184
185 #[test]
186 fn test_vmd_demux() {
187 let mut file = File::open("assets/Game/128.vmd").unwrap();
188 //let mut file = File::open("assets/Game/1491.VMD").unwrap();
189 let mut fr = FileReader::new_read(&mut file);
190 let mut br = ByteReader::new(&mut fr);
191 let mut dmx = VMDDemuxer::new(&mut br);
192 let mut sm = StreamManager::new();
193 dmx.open(&mut sm).unwrap();
194 loop {
195 let pktres = dmx.get_frame(&mut sm);
196 if let Err(e) = pktres {
197 if (e as i32) == (DemuxerError::EOF as i32) { break; }
198 panic!("error");
199 }
200 let pkt = pktres.unwrap();
201 println!("Got {}", pkt);
202 }
203 }
204 }