]> git.nihav.org Git - nihav.git/blob - bmv.rs
0bdb6245c07cf354923fa24a628775fcdbf326c2
[nihav.git] / bmv.rs
1 use nihav_core::formats;
2 use nihav_core::codecs::*;
3 use nihav_core::io::byteio::*;
4 use std::str::FromStr;
5
6 const FRAME_W: usize = 640;
7 const FRAME_H: usize = 429;
8
9 const BMV_INTRA: u8 = 0x03;
10 const BMV_SCROLL: u8 = 0x04;
11 const BMV_PAL: u8 = 0x08;
12 const BMV_COMMAND: u8 = 0x10;
13 const BMV_PRINT: u8 = 0x80;
14
15 struct BMVReader<'a> {
16 src: &'a [u8],
17 pos: usize,
18 fwd: bool,
19 nibble: u8,
20 saved: bool,
21 }
22
23 impl<'a> BMVReader<'a> {
24 fn new(src: &'a [u8], fwd: bool) -> Self {
25 let pos = if fwd { 0 } else { src.len() - 1 };
26 Self { src, pos, fwd, nibble: 0, saved: false }
27 }
28 fn advance(&mut self) {
29 if self.fwd {
30 if self.pos < self.src.len() - 1 { self.pos += 1; }
31 } else {
32 if self.pos > 0 { self.pos -= 1; }
33 }
34 }
35 fn get_byte(&mut self) -> u8 {
36 let ret = self.src[self.pos];
37 self.advance();
38 ret
39 }
40 fn get_nibble(&mut self) -> u8 {
41 if self.saved {
42 self.saved = false;
43 self.nibble
44 } else {
45 let val = self.get_byte();
46 self.saved = true;
47 self.nibble = val >> 4;
48 val & 0xF
49 }
50 }
51 }
52
53 struct BMVWriter<'a> {
54 data: &'a mut [u8],
55 pos: usize,
56 fwd: bool,
57 off: isize,
58 }
59
60 impl<'a> BMVWriter<'a> {
61 fn new(data: &'a mut [u8], fwd: bool, off: isize) -> Self {
62 let pos = if fwd { 0 } else { data.len() - 1 };
63 Self { data, pos, fwd, off }
64 }
65 fn is_at_end(&self) -> bool {
66 if self.fwd {
67 self.pos == self.data.len() - 1
68 } else {
69 self.pos == 0
70 }
71 }
72 fn advance(&mut self) {
73 if self.fwd {
74 if self.pos < self.data.len() - 1 { self.pos += 1; }
75 } else {
76 if self.pos > 0 { self.pos -= 1; }
77 }
78 }
79 fn put_byte(&mut self, val: u8) {
80 self.data[self.pos] = val;
81 self.advance();
82 }
83 fn copy(&mut self, len: usize) {
84 for _ in 0..len {
85 let saddr = (self.pos as isize) + self.off;
86 if saddr < 0 { continue; }
87 if self.fwd {
88 self.data[self.pos] = self.data[saddr as usize];
89 } else {
90 self.data[self.pos] = self.data[saddr as usize];
91 }
92 self.advance();
93 }
94 }
95 fn repeat(&mut self, len: usize) {
96 let last = if self.fwd { self.data[self.pos - 1] } else { self.data[self.pos + 1] };
97 for _ in 0..len {
98 self.put_byte(last);
99 }
100 }
101 }
102
103 struct BMVVideoDecoder {
104 info: Rc<NACodecInfo>,
105 pal: [u8; 768],
106 frame: [u8; FRAME_W * FRAME_H],
107 }
108
109 impl BMVVideoDecoder {
110 fn new() -> Self {
111 let dummy_info = Rc::new(DUMMY_CODEC_INFO);
112 Self {
113 info: dummy_info, pal: [0; 768], frame: [0; FRAME_W * FRAME_H],
114 }
115 }
116 fn decode_frame(&mut self, src: &[u8], bufinfo: &mut NABufferType, line: i16) -> DecoderResult<()> {
117 let bufo = bufinfo.get_vbuf();
118 let mut buf = bufo.unwrap();
119 let paloff = buf.get_offset(1);
120 let stride = buf.get_stride(0);
121 let mut data = buf.get_data_mut();
122 let dst = data.as_mut_slice();
123
124 let fwd = (line <= -640) || (line >= 0);
125
126 let mut br = BMVReader::new(src, fwd);
127 let mut bw = BMVWriter::new(&mut self.frame, fwd, line as isize);
128 let mut mode = 0;
129 while !bw.is_at_end() {
130 let mut val = 0;
131 let mut shift = 0;
132 loop {
133 let nib = br.get_nibble() as usize;
134 if (nib & 0xC) != 0 {
135 val |= nib << shift;
136 break;
137 }
138 val |= nib << shift;
139 shift += 2;
140 }
141 if (val & 1) != 0 {
142 mode += 1;
143 }
144 mode += 1;
145 if mode >= 4 {
146 mode -= 3;
147 }
148 validate!(val >= 2);
149 let len = (val >> 1) - 1;
150
151 match mode {
152 1 => bw.copy(len),
153 2 => for _ in 0..len { bw.put_byte(br.get_byte()); },
154 3 => bw.repeat(len),
155 _ => unreachable!(),
156 };
157 }
158
159 for y in 0..FRAME_H {
160 for x in 0..FRAME_W {
161 dst[y * stride + x] = self.frame[y * FRAME_W + x];
162 }
163 }
164
165 let dpal = &mut dst[paloff..][..768];
166 dpal.copy_from_slice(&self.pal[0..]);
167
168 Ok(())
169 }
170 }
171
172 impl NADecoder for BMVVideoDecoder {
173 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
174 if let NACodecTypeInfo::Video(_vinfo) = info.get_properties() {
175 let fmt = NAPixelFormaton::new(ColorModel::RGB(RGBSubmodel::RGB),
176 Some(NAPixelChromaton::new(0, 0, true, 8, 0, 0, 3)),
177 Some(NAPixelChromaton::new(0, 0, true, 8, 0, 1, 3)),
178 Some(NAPixelChromaton::new(0, 0, true, 8, 0, 2, 3)),
179 None, None,
180 FORMATON_FLAG_PALETTE, 3);
181 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(FRAME_W, FRAME_H, false, fmt));
182 self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()));
183
184 Ok(())
185 } else {
186 Err(DecoderError::InvalidData)
187 }
188 }
189 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
190 let src = pkt.get_buffer();
191 validate!(src.len() > 1);
192
193 let mut mr = MemoryReader::new_read(&src);
194 let mut br = ByteReader::new(&mut mr);
195 let flags = br.read_byte()?;
196
197 if (flags & BMV_COMMAND) != 0 {
198 let size = if (flags & BMV_PRINT) != 0 { 8 } else { 10 };
199 br.read_skip(size)?;
200 }
201 if (flags & BMV_PAL) != 0 {
202 br.read_buf(&mut self.pal)?;
203 }
204 let line;
205 if (flags & BMV_SCROLL) != 0 {
206 line = br.read_u16le()? as i16;
207 } else if (flags & BMV_INTRA) == BMV_INTRA {
208 line = -640;
209 } else {
210 line = 0;
211 }
212 let pos = br.tell() as usize;
213
214 let bufret = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0);
215 if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
216 let mut bufinfo = bufret.unwrap();
217
218 self.decode_frame(&src[pos..], &mut bufinfo, line)?;
219
220 let is_intra = (flags & BMV_INTRA) == BMV_INTRA;
221 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
222 frm.set_keyframe(is_intra);
223 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
224 Ok(Rc::new(RefCell::new(frm)))
225 }
226 }
227
228
229 pub fn get_decoder_video() -> Box<NADecoder> {
230 Box::new(BMVVideoDecoder::new())
231 }
232
233 struct BMVAudioDecoder {
234 ainfo: NAAudioInfo,
235 chmap: NAChannelMap,
236 }
237
238 const BMV_AUD_SCALES: [i32; 16] = [ 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32 ];
239
240 impl BMVAudioDecoder {
241 fn new() -> Self {
242 Self {
243 ainfo: NAAudioInfo::new(0, 1, formats::SND_S16P_FORMAT, 0),
244 chmap: NAChannelMap::new(),
245 }
246 }
247 }
248
249 fn scale_sample(samp: u8, scale: i32) -> i16 {
250 let val = (((samp as i8) as i32) * scale) >> 5;
251 if val < -32768 {
252 -32768
253 } else if val > 32767 {
254 32767
255 } else {
256 val as i16
257 }
258 }
259
260 impl NADecoder for BMVAudioDecoder {
261 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
262 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
263 self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), formats::SND_S16P_FORMAT, 32);
264 self.chmap = NAChannelMap::from_str("L,R").unwrap();
265 Ok(())
266 } else {
267 Err(DecoderError::InvalidData)
268 }
269 }
270 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
271 let info = pkt.get_stream().get_info();
272 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
273 let pktbuf = pkt.get_buffer();
274 validate!(pktbuf.len() > 1);
275 let nblocks = pktbuf[0] as usize;
276 validate!(pktbuf.len() == 1 + 65 * nblocks);
277 let samples = nblocks * 32;
278 let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?;
279 let mut adata = abuf.get_abuf_i16().unwrap();
280 let off1 = adata.get_offset(1);
281 let mut dst = adata.get_data_mut();
282 let psrc = &pktbuf[1..];
283 for (n, src) in psrc.chunks_exact(65).enumerate() {
284 let code = src[0].rotate_right(1);
285 let scale0 = BMV_AUD_SCALES[(code & 0xF) as usize];
286 let scale1 = BMV_AUD_SCALES[(code >> 4) as usize];
287 let aoff0 = n * 32;
288 let aoff1 = aoff0 + off1;
289 let data = &src[1..];
290 for (i, samp) in data.chunks_exact(2).enumerate() {
291 dst[aoff0 + i] = scale_sample(samp[0], scale0);
292 dst[aoff1 + i] = scale_sample(samp[1], scale1);
293 }
294 }
295 let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
296 frm.set_duration(Some(samples as u64));
297 frm.set_keyframe(false);
298 Ok(Rc::new(RefCell::new(frm)))
299 } else {
300 Err(DecoderError::InvalidData)
301 }
302 }
303 }
304
305 pub fn get_decoder_audio() -> Box<NADecoder> {
306 Box::new(BMVAudioDecoder::new())
307 }
308
309 #[cfg(test)]
310 mod test {
311 use nihav_core::codecs::RegisteredDecoders;
312 use nihav_core::demuxers::RegisteredDemuxers;
313 use nihav_core::test::dec_video::*;
314 use crate::codecs::game_register_all_codecs;
315 use crate::demuxers::game_register_all_demuxers;
316 #[test]
317 fn test_bmv_video() {
318 let mut dmx_reg = RegisteredDemuxers::new();
319 game_register_all_demuxers(&mut dmx_reg);
320 let mut dec_reg = RegisteredDecoders::new();
321 game_register_all_codecs(&mut dec_reg);
322
323 // let file = "assets/Game/PERFECT.BMV";
324 // let file = "assets/Game/DW2-MOUSE.BMV";
325 let file = "assets/Game/WILDCAT.BMV";
326 test_file_decoding("bmv", file, Some(40), true, false, None, &dmx_reg, &dec_reg);
327 }
328 #[test]
329 fn test_bmv_audio() {
330 let mut dmx_reg = RegisteredDemuxers::new();
331 game_register_all_demuxers(&mut dmx_reg);
332 let mut dec_reg = RegisteredDecoders::new();
333 game_register_all_codecs(&mut dec_reg);
334
335 let file = "assets/Game/PERFECT.BMV";
336 // let file = "assets/Game/DW2-MOUSE.BMV";
337 // let file = "assets/Game/WILDCAT.BMV";
338 test_decode_audio("bmv", file, None, "bmv", &dmx_reg, &dec_reg);
339 }
340 }