]>
Commit | Line | Data |
---|---|---|
1 | use nihav_core::frame::*; | |
2 | use nihav_core::demuxers::*; | |
3 | use std::io::SeekFrom; | |
4 | ||
5 | #[cfg(feature="decoder_vx")] | |
6 | use super::super::codecs::vx::VXVideoParser; | |
7 | ||
8 | const AUDIO_EXTRADATA_LEN: usize = 3124; | |
9 | ||
10 | struct VXDemuxer<'a> { | |
11 | src: &'a mut ByteReader<'a>, | |
12 | vid_id: usize, | |
13 | aud_id: usize, | |
14 | fps: u32, | |
15 | video_pos: u64, | |
16 | vno: u64, | |
17 | num_vfrm: u64, | |
18 | num_aud: usize, | |
19 | ano: u64, | |
20 | num_afrm: u64, | |
21 | seektab: Vec<(u32, u32)>, | |
22 | #[cfg(feature="decoder_vx")] | |
23 | parser: Option<VXVideoParser>, | |
24 | parse: bool, | |
25 | apkt: Option<NAPacket>, | |
26 | } | |
27 | ||
28 | impl<'a> DemuxCore<'a> for VXDemuxer<'a> { | |
29 | fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> { | |
30 | let src = &mut self.src; | |
31 | ||
32 | let mut magic = [0u8; 4]; | |
33 | src.read_buf(&mut magic)?; | |
34 | validate!(&magic == b"VXDS"); | |
35 | let nframes = src.read_u32le()? as usize; | |
36 | let width = src.read_u32le()? as usize; | |
37 | let height = src.read_u32le()? as usize; | |
38 | let fps = src.read_u32le()?; | |
39 | validate!(fps > (1 << 16) && fps < (100 << 16)); | |
40 | let quant = src.read_u32le()?; | |
41 | validate!(quant > 0 && quant < 256); | |
42 | let srate = src.read_u32le()?; | |
43 | let num_audio_tracks = src.read_u32le()? as usize; | |
44 | let _max_frame_size = src.read_u32le()? as usize; | |
45 | let audio_off = src.read_u32le()? as u64; | |
46 | validate!(audio_off == 0 || audio_off >= 0x30); | |
47 | let vinfo_off = src.read_u32le()? as u64; | |
48 | validate!(vinfo_off == 0 || vinfo_off >= 0x30); | |
49 | let num_keypos = src.read_u32le()? as usize; | |
50 | ||
51 | let vhdr = NAVideoInfo::new(width, height, false, YUV420_FORMAT); | |
52 | let vci = NACodecTypeInfo::Video(vhdr); | |
53 | let edata = vec![quant as u8, 0, 0, 0]; | |
54 | let vinfo = NACodecInfo::new("vxvideo", vci, Some(edata)); | |
55 | self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 65536, fps, nframes as u64)).unwrap(); | |
56 | ||
57 | if num_audio_tracks != 0 && self.parse { | |
58 | validate!(audio_off + ((num_audio_tracks * AUDIO_EXTRADATA_LEN) as u64) == vinfo_off); | |
59 | src.seek(SeekFrom::Start(audio_off))?; | |
60 | let mut edata = vec![0u8; AUDIO_EXTRADATA_LEN * num_audio_tracks]; | |
61 | src.read_buf(edata.as_mut_slice())?; | |
62 | let ahdr = NAAudioInfo::new(srate, num_audio_tracks as u8, SND_S16P_FORMAT, 1); | |
63 | let ainfo = NACodecInfo::new("vxaudio", NACodecTypeInfo::Audio(ahdr), Some(edata)); | |
64 | self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 128, srate, 0)).unwrap(); | |
65 | self.num_afrm = nframes as u64; | |
66 | self.ano = 0; | |
67 | self.num_aud = num_audio_tracks; | |
68 | } else { | |
69 | self.aud_id = self.vid_id + 1; | |
70 | } | |
71 | ||
72 | if num_keypos > 0 { | |
73 | src.seek(SeekFrom::Start(vinfo_off))?; | |
74 | let mut last_frame = src.read_u32le()?; | |
75 | let mut last_offset = src.read_u32le()?; | |
76 | validate!(last_frame == 0 && last_offset == 0x30); | |
77 | self.seektab.clear(); | |
78 | self.seektab.push((last_frame, last_offset)); | |
79 | for _ in 1..num_keypos { | |
80 | let frameno = src.read_u32le()?; | |
81 | let offset = src.read_u32le()?; | |
82 | validate!(frameno > last_frame && offset > last_offset); | |
83 | self.seektab.push((frameno, offset)); | |
84 | last_frame = frameno; | |
85 | last_offset = offset; | |
86 | } | |
87 | } | |
88 | ||
89 | #[cfg(not(feature="decoder_vx"))] | |
90 | if self.parse == true { | |
91 | println!("Frame parsing is not enabled in this build"); | |
92 | } | |
93 | ||
94 | #[cfg(feature="decoder_vx")] | |
95 | if self.parse { | |
96 | self.parser = Some(VXVideoParser::new(width, height)); | |
97 | } | |
98 | ||
99 | self.fps = fps; | |
100 | self.video_pos = 0x30; | |
101 | self.vno = 0; | |
102 | self.num_vfrm = nframes as u64; | |
103 | Ok(()) | |
104 | } | |
105 | ||
106 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { | |
107 | if self.apkt.is_some() { | |
108 | let mut ret = None; | |
109 | std::mem::swap(&mut self.apkt, &mut ret); | |
110 | return Ok(ret.unwrap()); | |
111 | } | |
112 | if self.vno >= self.num_vfrm { return Err(DemuxerError::EOF); } | |
113 | self.src.seek(SeekFrom::Start(self.video_pos))?; | |
114 | let stream = strmgr.get_stream(self.vid_id); | |
115 | if stream.is_none() { return Err(DemuxerError::InvalidData); } | |
116 | let stream = stream.unwrap(); | |
117 | let ts = stream.make_ts(Some(self.vno), None, None); | |
118 | let size = self.src.read_u16le()? as usize; | |
119 | validate!(size > 2); | |
120 | let num_achunks = self.src.read_u16le()?; | |
121 | let fsize = size - 2; | |
122 | let mut buf = vec![0; fsize + 4]; | |
123 | write_u32le(&mut buf, (fsize * 8) as u32)?; | |
124 | self.src.read_buf(&mut buf[4..])?; | |
125 | #[cfg(feature="decoder_vx")] | |
126 | if self.num_aud > 0 { | |
127 | if let Some(ref mut parser) = self.parser { | |
128 | if let Ok(nbits) = parser.parse_frame(&buf[4..]) { | |
129 | write_u32le(&mut buf, nbits as u32)?; | |
130 | let vpart_size = 4 + ((nbits + 15) & !15) / 8; | |
131 | ||
132 | if let Some(astream) = strmgr.get_stream(self.aud_id) { | |
133 | let mut audio = vec![0; buf.len() - vpart_size + 2]; | |
134 | write_u16le(&mut audio, num_achunks)?; | |
135 | audio[2..].copy_from_slice(&buf[vpart_size..]); | |
136 | let ts_audio = astream.make_ts(Some(self.ano), None, None); | |
137 | self.apkt = Some(NAPacket::new(astream, ts_audio, true, audio)); | |
138 | self.ano += u64::from(num_achunks); | |
139 | } | |
140 | ||
141 | buf.truncate(vpart_size); | |
142 | } else { | |
143 | println!("failed to parse video frame size, corrupted frame?"); | |
144 | } | |
145 | } | |
146 | } | |
147 | let keyframe = self.vno == 0 || | |
148 | self.seektab.binary_search_by_key(&self.vno, |&(frm, _)| u64::from(frm)).is_ok(); | |
149 | let pkt = NAPacket::new(stream, ts, keyframe, buf); | |
150 | self.video_pos = self.src.tell(); | |
151 | self.vno += 1; | |
152 | Ok(pkt) | |
153 | } | |
154 | ||
155 | fn seek(&mut self, time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { | |
156 | if self.seektab.len() < 2 { | |
157 | Err(DemuxerError::NotPossible) | |
158 | } else { | |
159 | let fno = match time { | |
160 | NATimePoint::Milliseconds(ms) => { | |
161 | NATimeInfo::time_to_ts(ms, 1000, 65536, self.fps) | |
162 | }, | |
163 | NATimePoint::PTS(ts) => ts, | |
164 | NATimePoint::None => return Err(DemuxerError::NotPossible) | |
165 | }; | |
166 | if fno >= self.num_vfrm { | |
167 | return Err(DemuxerError::SeekError); | |
168 | } | |
169 | let idx = match self.seektab.binary_search_by_key(&fno, |&(frm, _)| u64::from(frm)) { | |
170 | Ok(val) => val, | |
171 | Err(val) => val - 1, | |
172 | }; | |
173 | self.vno = u64::from(self.seektab[idx].0); | |
174 | self.video_pos = u64::from(self.seektab[idx].1); | |
175 | Ok(()) | |
176 | } | |
177 | } | |
178 | ||
179 | fn get_duration(&self) -> u64 { 0 } | |
180 | } | |
181 | ||
182 | const PARSE_FRAMES_OPT: &str = "parse_frames"; | |
183 | const DEMUXER_OPTS: &[NAOptionDefinition] = &[ | |
184 | NAOptionDefinition { | |
185 | name: PARSE_FRAMES_OPT, | |
186 | description: "parse frames to split them into video and audio parts", | |
187 | opt_type: NAOptionDefinitionType::Bool }, | |
188 | ]; | |
189 | ||
190 | impl<'a> NAOptionHandler for VXDemuxer<'a> { | |
191 | fn get_supported_options(&self) -> &[NAOptionDefinition] { DEMUXER_OPTS } | |
192 | fn set_options(&mut self, options: &[NAOption]) { | |
193 | for option in options.iter() { | |
194 | for opt_def in DEMUXER_OPTS.iter() { | |
195 | if opt_def.check(option).is_ok() { | |
196 | if let (PARSE_FRAMES_OPT, NAValue::Bool(ref bval)) = (option.name, &option.value) { | |
197 | self.parse = *bval; | |
198 | } | |
199 | } | |
200 | } | |
201 | } | |
202 | } | |
203 | fn query_option_value(&self, name: &str) -> Option<NAValue> { | |
204 | match name { | |
205 | PARSE_FRAMES_OPT => Some(NAValue::Bool(self.parse)), | |
206 | _ => None, | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | impl<'a> VXDemuxer<'a> { | |
212 | fn new(io: &'a mut ByteReader<'a>) -> Self { | |
213 | Self { | |
214 | vid_id: 0, | |
215 | aud_id: 0, | |
216 | fps: 0, | |
217 | video_pos: 0, | |
218 | num_aud: 0, | |
219 | vno: 0, | |
220 | ano: 0, | |
221 | num_vfrm: 0, | |
222 | num_afrm: 0, | |
223 | src: io, | |
224 | seektab: Vec::new(), | |
225 | #[cfg(feature="decoder_vx")] | |
226 | parser: None, | |
227 | parse: true, | |
228 | apkt: None, | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
233 | pub struct VXDemuxerCreator { } | |
234 | ||
235 | impl DemuxerCreator for VXDemuxerCreator { | |
236 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { | |
237 | Box::new(VXDemuxer::new(br)) | |
238 | } | |
239 | fn get_name(&self) -> &'static str { "vx" } | |
240 | } | |
241 | ||
242 | #[cfg(test)] | |
243 | mod test { | |
244 | use super::*; | |
245 | use std::fs::File; | |
246 | ||
247 | #[test] | |
248 | fn test_vx_demux() { | |
249 | // sample from some game | |
250 | let mut file = File::open("assets/Game/bioware.vx").unwrap(); | |
251 | //let mut file = File::open("assets/Game/BS_01_Intro.vx").unwrap(); | |
252 | //let mut file = File::open("assets/Game/sega.vx").unwrap(); | |
253 | let mut fr = FileReader::new_read(&mut file); | |
254 | let mut br = ByteReader::new(&mut fr); | |
255 | let mut dmx = VXDemuxer::new(&mut br); | |
256 | let mut sm = StreamManager::new(); | |
257 | let mut si = SeekIndex::new(); | |
258 | dmx.open(&mut sm, &mut si).unwrap(); | |
259 | loop { | |
260 | let pktres = dmx.get_frame(&mut sm); | |
261 | if let Err(e) = pktres { | |
262 | if (e as i32) == (DemuxerError::EOF as i32) { break; } | |
263 | panic!("error"); | |
264 | } | |
265 | let pkt = pktres.unwrap(); | |
266 | println!("Got {}", pkt); | |
267 | } | |
268 | } | |
269 | } |