1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
5 const OLD_HEADER_SIZE: usize = 0x330;
6 const NEW_HEADER_SIZE: usize = 0x34;
7 const HEADER1_SIZE: usize = 28;
8 const HEADER2_OFF: usize = HEADER1_SIZE + 768;
9 const FRAME_HDR_SIZE: usize = 10;
11 const CHTYPE_VIDEO: u8 = 0x02;
12 const CHTYPE_AUDIO: u8 = 0x01;
19 hdr: [u8; FRAME_HDR_SIZE],
23 struct VMDDemuxer<'a> {
24 src: &'a mut ByteReader<'a>,
30 frames: Vec<FrameRec>,
33 impl<'a> DemuxCore<'a> for VMDDemuxer<'a> {
34 #[allow(unused_variables)]
35 fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
36 let src = &mut self.src;
38 let mut header: [u8; OLD_HEADER_SIZE] = [0; OLD_HEADER_SIZE];
39 src.read_buf(&mut header[..HEADER1_SIZE])?;
40 let hdr_size = read_u16le(&header)? as usize + 2;
41 validate!(hdr_size == OLD_HEADER_SIZE || hdr_size == NEW_HEADER_SIZE);
42 if hdr_size == OLD_HEADER_SIZE {
43 src.read_buf(&mut header[HEADER1_SIZE..][..768])?;
45 src.read_buf(&mut header[HEADER2_OFF..])?;
47 let mut width = read_u16le(&header[12..])? as usize;
48 let mut height = read_u16le(&header[14..])? as usize;
49 self.is_indeo = &header[24..27] == b"iv3";
50 if self.is_indeo && width > 320 {
55 let nframes = read_u16le(&header[6..])? as usize;
56 let fpb = read_u16le(&header[18..])? as usize;
57 validate!(nframes > 0 && fpb > 0);
59 let mut edata: Vec<u8> = Vec::with_capacity(OLD_HEADER_SIZE);
60 edata.extend_from_slice(&header);
61 let vhdr = NAVideoInfo::new(width, height, false, PAL8_FORMAT);
62 let vci = NACodecTypeInfo::Video(vhdr);
63 let vinfo = NACodecInfo::new(if !self.is_indeo { "vmd-video" } else { "indeo3" }, vci, Some(edata));
64 self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, 12, nframes as u64)).unwrap();
66 let is_ext_audio = (hdr_size & 0xF) == 4;
67 let ext_audio_id = if is_ext_audio {
71 validate!(ext_audio_id >= 3 && ext_audio_id <= 6);
74 let srate = u32::from(read_u16le(&header[804..])?);
77 let bsize = read_u16le(&header[806..])? as usize;
78 let channels = if (header[811] & 0x8F) != 0 { 2 } else { 1 };
80 if (bsize & 0x8000) != 0 {
82 block_size = 0x10000 - bsize;
88 let mut aedata: Vec<u8> = Vec::with_capacity(2);
89 aedata.extend_from_slice(&header[810..][..2]);
90 let ahdr = NAAudioInfo::new(srate, channels, if is16bit { SND_S16P_FORMAT } else { SND_U8_FORMAT }, block_size);
91 let ac_name = if !is_ext_audio {
102 let ainfo = NACodecInfo::new(ac_name, NACodecTypeInfo::Audio(ahdr), Some(aedata));
103 self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, srate, 0)).unwrap();
108 let adelay = u32::from(read_u16le(&header[808..])?);
109 let idx_off = u64::from(read_u32le(&header[812..])?);
110 src.seek(SeekFrom::Start(idx_off))?;
111 let mut offs: Vec<u32> = Vec::with_capacity(nframes);
112 for i in 0..nframes {
113 let _flags = src.read_u16le()?;
114 let off = src.read_u32le()?;
117 self.frames.reserve(nframes * fpb);
118 let mut ats = adelay;
119 for i in 0..nframes {
120 let mut off = offs[i];
122 let chtype = src.read_byte()?;
124 let mut size = src.read_u32le()?;
125 let mut hdr: [u8; FRAME_HDR_SIZE] = [0; FRAME_HDR_SIZE];
126 src.read_buf(&mut hdr)?;
127 if (i == 0) && (chtype == CHTYPE_AUDIO) && (size > 4) && ((size as usize) < block_size/2) {
130 if (chtype == CHTYPE_VIDEO || chtype == CHTYPE_AUDIO) && (size > 0) {
131 let ts = if (i == 0) || (chtype != CHTYPE_AUDIO) {
136 self.frames.push(FrameRec { chtype, size, hdr, off, ts });
138 if i > 0 && chtype == CHTYPE_AUDIO {
142 validate!(off.checked_add(size).is_some());
152 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
153 if self.fno >= self.frames.len() { return Err(DemuxerError::EOF); }
154 let cur_frame = &self.frames[self.fno];
155 //println!("fno {} -> type {} size {} @ {:X} ts {}", self.fno, cur_frame.chtype, cur_frame.size, cur_frame.off, cur_frame.ts);
156 let next_pos = u64::from(cur_frame.off);
157 if self.src.tell() != next_pos {
158 self.src.seek(SeekFrom::Start(next_pos))?;
161 let is_video = cur_frame.chtype == CHTYPE_VIDEO;
162 let mut buf: Vec<u8> = Vec::with_capacity(FRAME_HDR_SIZE + (cur_frame.size as usize));
163 if !((is_video && self.is_indeo) || (!is_video && self.is_lhaud)) {
164 buf.extend_from_slice(&cur_frame.hdr);
165 buf.resize(FRAME_HDR_SIZE + (cur_frame.size as usize), 0);
166 self.src.read_buf(&mut buf[FRAME_HDR_SIZE..])?;
168 buf.resize(cur_frame.size as usize, 0);
169 self.src.read_buf(&mut buf)?;
174 let str_id = if is_video { self.vid_id } else { self.aud_id };
175 let str = strmgr.get_stream(str_id).unwrap();
176 let (tb_num, tb_den) = str.get_timebase();
177 let ts = NATimeInfo::new(Some(u64::from(cur_frame.ts)), None, None, tb_num, tb_den);
178 let pkt = NAPacket::new(str, ts, false, buf);
183 fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
184 Err(DemuxerError::NotPossible)
187 fn get_duration(&self) -> u64 { 0 }
190 impl<'a> NAOptionHandler for VMDDemuxer<'a> {
191 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
192 fn set_options(&mut self, _options: &[NAOption]) { }
193 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
196 impl<'a> VMDDemuxer<'a> {
197 fn new(io: &'a mut ByteReader<'a>) -> Self {
210 pub struct VMDDemuxerCreator { }
212 impl DemuxerCreator for VMDDemuxerCreator {
213 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
214 Box::new(VMDDemuxer::new(br))
216 fn get_name(&self) -> &'static str { "vmd" }
225 fn test_vmd_demux() {
226 let mut file = File::open("assets/Game/128.vmd").unwrap();
227 //let mut file = File::open("assets/Game/1491.VMD").unwrap();
228 let mut fr = FileReader::new_read(&mut file);
229 let mut br = ByteReader::new(&mut fr);
230 let mut dmx = VMDDemuxer::new(&mut br);
231 let mut sm = StreamManager::new();
232 let mut si = SeekIndex::new();
233 dmx.open(&mut sm, &mut si).unwrap();
235 let pktres = dmx.get_frame(&mut sm);
236 if let Err(e) = pktres {
237 if (e as i32) == (DemuxerError::EOF as i32) { break; }
240 let pkt = pktres.unwrap();
241 println!("Got {}", pkt);