]>
Commit | Line | Data |
---|---|---|
9067c1f8 KS |
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)] | |
33b5a8f0 | 15 | fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> { |
9067c1f8 KS |
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); | |
a480a0de | 21 | self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, 12, 0)).unwrap(); |
9067c1f8 KS |
22 | |
23 | let ahdr = NAAudioInfo::new(22050, 2, SND_S16_FORMAT, 1); | |
24 | let ainfo = NACodecInfo::new("bmv-audio", NACodecTypeInfo::Audio(ahdr), None); | |
a480a0de | 25 | self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, 22050, 0)).unwrap(); |
9067c1f8 KS |
26 | |
27 | self.vpos = 0; | |
28 | self.apos = 0; | |
29 | Ok(()) | |
30 | } | |
31 | ||
32 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { | |
e69b1148 | 33 | if !self.pkt_buf.is_empty() { |
9067c1f8 KS |
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); | |
817e4872 KS |
50 | let stream = strmgr.get_stream(self.aud_id).unwrap(); |
51 | let ts = stream.make_ts(Some(self.apos), None, None); | |
52 | let apkt = self.src.read_packet(stream, ts, false, asize)?; | |
e69b1148 | 53 | self.apos += u64::from(nblocks) * 32; |
9067c1f8 KS |
54 | self.pkt_buf.push(apkt); |
55 | } else { | |
56 | asize = 0; | |
57 | } | |
e69b1148 | 58 | let mut buf: Vec<u8> = vec![0; size - asize + 1]; |
9067c1f8 KS |
59 | buf[0] = ctype; |
60 | self.src.read_buf(&mut buf[1..])?; | |
61 | ||
817e4872 KS |
62 | let stream = strmgr.get_stream(self.vid_id).unwrap(); |
63 | let ts = stream.make_ts(Some(self.vpos), None, None); | |
64 | let pkt = NAPacket::new(stream, ts, (ctype & 3) == 3, buf); | |
9067c1f8 KS |
65 | |
66 | self.vpos += 1; | |
67 | return Ok(pkt); | |
68 | } | |
69 | } | |
70 | ||
24d99894 | 71 | fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { |
33b5a8f0 | 72 | Err(DemuxerError::NotPossible) |
9067c1f8 | 73 | } |
a480a0de | 74 | fn get_duration(&self) -> u64 { 0 } |
9067c1f8 KS |
75 | } |
76 | ||
787b8d03 KS |
77 | impl<'a> NAOptionHandler for BMVDemuxer<'a> { |
78 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
79 | fn set_options(&mut self, _options: &[NAOption]) { } | |
80 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
81 | } | |
82 | ||
9067c1f8 KS |
83 | impl<'a> BMVDemuxer<'a> { |
84 | fn new(io: &'a mut ByteReader<'a>) -> Self { | |
85 | Self { | |
86 | src: io, | |
87 | vid_id: 0, | |
88 | aud_id: 0, | |
89 | vpos: 0, | |
90 | apos: 0, | |
91 | pkt_buf: Vec::with_capacity(1), | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | pub struct BMVDemuxerCreator { } | |
97 | ||
98 | impl DemuxerCreator for BMVDemuxerCreator { | |
6011e201 | 99 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
9067c1f8 KS |
100 | Box::new(BMVDemuxer::new(br)) |
101 | } | |
102 | fn get_name(&self) -> &'static str { "bmv" } | |
103 | } | |
104 | ||
ecda1cc1 KS |
105 | struct BMV3Demuxer<'a> { |
106 | src: &'a mut ByteReader<'a>, | |
107 | vid_id: usize, | |
108 | aud_id: usize, | |
109 | vpos: u64, | |
110 | apos: u64, | |
111 | asize: usize, | |
112 | ablob: usize, | |
113 | pkt_buf: Vec<NAPacket>, | |
114 | } | |
115 | ||
116 | impl<'a> DemuxCore<'a> for BMV3Demuxer<'a> { | |
117 | #[allow(unused_variables)] | |
7450554d | 118 | #[allow(clippy::cast_lossless)] |
33b5a8f0 | 119 | fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> { |
ecda1cc1 KS |
120 | let src = &mut self.src; |
121 | ||
122 | let mut magic = [0u8; 4]; | |
123 | src.read_buf(&mut magic)?; | |
124 | validate!(&magic[0..] == b"BMVi"); | |
125 | let size = src.read_u32le()?; | |
126 | validate!(size == 24); | |
127 | let _slot_size = src.read_u32le()? as usize; | |
128 | let nframes = src.read_u32le()? as usize; | |
129 | let _prefetch_slots = src.read_u16le()?; | |
130 | let _cache_size = src.read_u16le()?; | |
131 | let fps = src.read_u16le()? as u32; | |
132 | let audio_size = src.read_u16le()? as usize; | |
133 | let audio_blob_size = src.read_u16le()? as usize; | |
134 | let _audio_id = src.read_byte()?; | |
135 | let _video_id = src.read_byte()?; | |
136 | let width = src.read_u16le()? as usize; | |
137 | let height = src.read_u16le()? as usize; | |
138 | validate!(nframes > 0); | |
139 | validate!((width > 0) && (width <= 640)); | |
140 | validate!((height > 0) && (height <= 432)); | |
141 | validate!((audio_size > audio_blob_size) && (audio_blob_size > 0) && (audio_size % audio_blob_size == 0)); | |
142 | let mut dta = [0u8; 4]; | |
143 | src.read_buf(&mut dta)?; | |
144 | validate!(&dta[0..] == b"DATA"); | |
145 | let data_size = src.read_u32le()? as usize; | |
146 | validate!(data_size > 0); | |
147 | self.asize = audio_size; | |
148 | self.ablob = audio_blob_size; | |
149 | ||
150 | let vhdr = NAVideoInfo::new(width, height, false, RGB565_FORMAT); | |
151 | let vci = NACodecTypeInfo::Video(vhdr); | |
152 | let vinfo = NACodecInfo::new("bmv3-video", vci, None); | |
a480a0de | 153 | self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 256, fps, nframes as u64)).unwrap(); |
ecda1cc1 KS |
154 | |
155 | let ahdr = NAAudioInfo::new(22050, 2, SND_S16_FORMAT, audio_blob_size); | |
156 | let ainfo = NACodecInfo::new("bmv3-audio", NACodecTypeInfo::Audio(ahdr), None); | |
a480a0de | 157 | self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, 22050, 0)).unwrap(); |
ecda1cc1 KS |
158 | |
159 | self.vpos = 0; | |
160 | self.apos = 0; | |
161 | Ok(()) | |
162 | } | |
163 | ||
164 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { | |
e69b1148 | 165 | if !self.pkt_buf.is_empty() { |
ecda1cc1 KS |
166 | return Ok(self.pkt_buf.pop().unwrap()); |
167 | } | |
168 | ||
169 | loop { | |
170 | let ctype = self.src.read_byte()?; | |
171 | if ctype == 0 { // NOP chunk | |
172 | continue; | |
173 | } | |
174 | if ctype == 1 { return Err(DemuxerError::EOF); } | |
175 | let size = self.src.read_u24le()? as usize; | |
176 | if size == 0 { continue; } | |
177 | let asize; | |
178 | if (ctype & 0x20) != 0 { | |
179 | if (ctype & 0x40) != 0 { | |
180 | asize = self.asize - self.ablob; | |
181 | } else { | |
182 | asize = self.asize; | |
183 | } | |
184 | validate!(asize <= size); | |
e69b1148 | 185 | let mut buf: Vec<u8> = vec![0; asize + 1]; |
ecda1cc1 KS |
186 | buf[0] = (self.src.tell() & 1) as u8; |
187 | self.src.read_buf(&mut buf[1..])?; | |
188 | ||
817e4872 KS |
189 | let stream = strmgr.get_stream(self.aud_id).unwrap(); |
190 | let ts = stream.make_ts(Some(self.apos), None, None); | |
191 | let apkt = NAPacket::new(stream, ts, false, buf); | |
ecda1cc1 KS |
192 | |
193 | self.apos += (asize as u64) / 41 * 32; | |
194 | self.pkt_buf.push(apkt); | |
195 | } else { | |
196 | asize = 0; | |
197 | } | |
726973ae | 198 | if size == asize { |
e69b1148 | 199 | if !self.pkt_buf.is_empty() { |
726973ae KS |
200 | return Ok(self.pkt_buf.pop().unwrap()); |
201 | } else { | |
202 | continue; | |
203 | } | |
204 | } | |
e69b1148 | 205 | let mut buf: Vec<u8> = vec![0; size - asize + 1]; |
ecda1cc1 KS |
206 | buf[0] = ctype; |
207 | self.src.read_buf(&mut buf[1..])?; | |
208 | ||
817e4872 KS |
209 | let stream = strmgr.get_stream(self.vid_id).unwrap(); |
210 | let ts = stream.make_ts(Some(self.vpos), None, None); | |
211 | let pkt = NAPacket::new(stream, ts, (ctype & 3) == 3, buf); | |
ecda1cc1 KS |
212 | |
213 | self.vpos += 1; | |
214 | return Ok(pkt); | |
215 | } | |
216 | } | |
217 | ||
24d99894 | 218 | fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { |
33b5a8f0 | 219 | Err(DemuxerError::NotPossible) |
ecda1cc1 | 220 | } |
a480a0de | 221 | fn get_duration(&self) -> u64 { 0 } |
ecda1cc1 KS |
222 | } |
223 | ||
787b8d03 KS |
224 | impl<'a> NAOptionHandler for BMV3Demuxer<'a> { |
225 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
226 | fn set_options(&mut self, _options: &[NAOption]) { } | |
227 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
228 | } | |
229 | ||
ecda1cc1 KS |
230 | impl<'a> BMV3Demuxer<'a> { |
231 | fn new(io: &'a mut ByteReader<'a>) -> Self { | |
232 | Self { | |
233 | src: io, | |
234 | vid_id: 0, | |
235 | aud_id: 0, | |
236 | vpos: 0, | |
237 | apos: 0, | |
238 | asize: 0, | |
239 | ablob: 0, | |
240 | pkt_buf: Vec::with_capacity(1), | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | pub struct BMV3DemuxerCreator { } | |
246 | ||
247 | impl DemuxerCreator for BMV3DemuxerCreator { | |
6011e201 | 248 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
ecda1cc1 KS |
249 | Box::new(BMV3Demuxer::new(br)) |
250 | } | |
251 | fn get_name(&self) -> &'static str { "bmv3" } | |
252 | } | |
253 | ||
9067c1f8 KS |
254 | #[cfg(test)] |
255 | mod test { | |
256 | use super::*; | |
257 | use std::fs::File; | |
258 | ||
886cde48 | 259 | // samples from https://samples.mplayerhq.hu/game-formats/bmv |
9067c1f8 KS |
260 | #[test] |
261 | fn test_bmv_demux() { | |
1678d59a | 262 | let mut file = File::open("assets/Game/DW2-MOUSE.BMV").unwrap(); |
9067c1f8 KS |
263 | let mut fr = FileReader::new_read(&mut file); |
264 | let mut br = ByteReader::new(&mut fr); | |
265 | let mut dmx = BMVDemuxer::new(&mut br); | |
266 | let mut sm = StreamManager::new(); | |
caf0f37e KS |
267 | let mut si = SeekIndex::new(); |
268 | dmx.open(&mut sm, &mut si).unwrap(); | |
9067c1f8 KS |
269 | loop { |
270 | let pktres = dmx.get_frame(&mut sm); | |
271 | if let Err(e) = pktres { | |
272 | if (e as i32) == (DemuxerError::EOF as i32) { break; } | |
273 | panic!("error"); | |
274 | } | |
275 | let pkt = pktres.unwrap(); | |
276 | println!("Got {}", pkt); | |
277 | } | |
278 | } | |
ecda1cc1 KS |
279 | #[test] |
280 | fn test_bmv3_demux() { | |
281 | let mut file = File::open("assets/Game/DW3-Loffnote.bmv").unwrap(); | |
282 | let mut fr = FileReader::new_read(&mut file); | |
283 | let mut br = ByteReader::new(&mut fr); | |
284 | let mut dmx = BMV3Demuxer::new(&mut br); | |
285 | let mut sm = StreamManager::new(); | |
caf0f37e KS |
286 | let mut si = SeekIndex::new(); |
287 | dmx.open(&mut sm, &mut si).unwrap(); | |
ecda1cc1 KS |
288 | loop { |
289 | let pktres = dmx.get_frame(&mut sm); | |
290 | if let Err(e) = pktres { | |
291 | if (e as i32) == (DemuxerError::EOF as i32) { break; } | |
292 | panic!("error"); | |
293 | } | |
294 | let pkt = pktres.unwrap(); | |
295 | println!("Got {}", pkt); | |
296 | } | |
297 | } | |
9067c1f8 | 298 | } |