2 use std::cell::RefCell;
3 use nihav_core::frame::*;
4 use nihav_core::formats;
5 use nihav_core::formats::{NAChannelType, NAChannelMap};
6 use nihav_core::codecs::*;
7 use nihav_core::io::byteio::*;
9 struct GremlinVideoDecoder {
10 info: Rc<NACodecInfo>,
27 const PREAMBLE_SIZE: usize = 4096;
30 fn new() -> Self { Bits8 { queue: 0, fill: 0 } }
31 fn read_2bits(&mut self, br: &mut ByteReader) -> ByteIOResult<u8> {
33 self.queue = br.read_byte()?;
36 let res = self.queue >> 6;
44 fn new() -> Self { Bits32 { queue: 0, fill: 0 } }
45 fn fill(&mut self, br: &mut ByteReader) -> ByteIOResult<()> {
46 self.queue = br.read_u32le()?;
50 fn read_bits(&mut self, br: &mut ByteReader, nbits: u8) -> ByteIOResult<u32> {
51 let res = self.queue & ((1 << nbits) - 1);
55 self.queue |= (br.read_u16le()? as u32) << self.fill;
62 impl GremlinVideoDecoder {
64 let dummy_info = Rc::new(DUMMY_CODEC_INFO);
66 info: dummy_info, pal: [0; 768], frame: Vec::new(),
67 scale_v: false, scale_h: false
71 fn lz_copy(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> {
72 if idx + len > self.frame.len() { return Err(DecoderError::InvalidData); }
74 let c = self.frame[idx - 1];
75 for i in 0..len { self.frame[idx + i] = c; }
76 } else if offset < 0 {
77 let start = idx - (-offset as usize);
78 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
80 if idx + (offset as usize) + len > self.frame.len() { return Err(DecoderError::InvalidData); }
81 let start = idx + (offset as usize);
82 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
87 fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
88 if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }
90 if self.scale_h && self.scale_v {
95 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
98 } else if self.scale_h {
102 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w];
105 } else if self.scale_v {
110 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)];
115 if scale_h && scale_v {
118 self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w];
124 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w];
130 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w];
135 self.scale_v = scale_v;
136 self.scale_h = scale_h;
139 fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
140 let bufo = bufinfo.get_vbuf();
141 let mut buf = bufo.unwrap();
142 let paloff = buf.get_offset(1);
143 let stride = buf.get_stride(0);
144 let mut data = buf.get_data_mut();
145 let dst = data.as_mut_slice();
146 let mut sidx = PREAMBLE_SIZE;
149 for i in 0..768 { dst[paloff + i] = self.pal[i]; }
150 if !self.scale_v && !self.scale_h {
152 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
159 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
161 for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; }
163 if !self.scale_h || ((y & 1) == 1) {
164 sidx += if !self.scale_v { w } else { w/2 };
171 fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
172 let mut bits = Bits8::new();
174 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
175 self.info.get_properties().get_video_info().unwrap().get_height();
176 let mut idx = PREAMBLE_SIZE;
177 if self.frame[8] != 0 {
179 for i in 0..16 { self.frame[c * 16 + i] = c as u8; }
183 let tag = bits.read_2bits(br)?;
185 self.frame[idx] = br.read_byte()?;
189 let b = br.read_byte()?;
190 let len = ((b & 0xF) as usize) + 3;
191 let bot = (b >> 4) as isize;
192 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
193 validate!(len <= size);
195 self.lz_copy(idx, off, len)?;
198 let len = (br.read_byte()? as usize) + 2;
199 validate!(len <= size);
209 fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> {
210 let mut bits = Bits8::new();
212 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
213 self.info.get_properties().get_video_info().unwrap().get_height();
214 let mut idx = PREAMBLE_SIZE;
215 validate!(size >= skip);
219 let tag = bits.read_2bits(br)?;
221 self.frame[idx] = br.read_byte()?;
225 let b = br.read_byte()?;
226 let len = ((b & 0xF) as usize) + 3;
227 let bot = (b >> 4) as isize;
228 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
229 validate!(len <= size);
231 self.lz_copy(idx, off, len)?;
234 let b = br.read_byte()?;
236 let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1;
237 validate!(len <= size);
241 let b = br.read_byte()?;
242 let len = ((b & 0x3) as usize) + 2;
243 let off = -((b >> 2) as isize) - 1;
244 validate!(len <= size);
246 self.lz_copy(idx, off, len)?;
253 fn decode_method68(&mut self, br: &mut ByteReader,
254 skip: usize, use8: bool) -> DecoderResult<()> {
255 let mut bits = Bits32::new();
257 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
258 self.info.get_properties().get_video_info().unwrap().get_height();
259 let mut idx = PREAMBLE_SIZE;
260 validate!(size >= skip);
265 let tag = bits.read_bits(br, 2)?;
267 let b = bits.read_bits(br, 1)?;
269 self.frame[idx] = br.read_byte()?;
273 let mut len: usize = 2;
277 let val = bits.read_bits(br, lbits)?;
279 if val != ((1 << lbits) - 1) { break; }
280 validate!(lbits < 16);
282 validate!(len <= size);
283 for i in 0..len { self.frame[idx + i] = br.read_byte()?; }
287 } else if tag == 1 { //skip
288 let b = bits.read_bits(br, 1)?;
291 len = (bits.read_bits(br, 4)? as usize) + 2;
293 let bb = br.read_byte()?;
294 if (bb & 0x80) == 0 {
295 len = (bb as usize) + 18;
297 let top = ((bb & 0x7F) as usize) << 8;
298 len = top + (br.read_byte()? as usize) + 146;
301 validate!(len <= size);
305 let subtag = bits.read_bits(br, 2)? as usize;
307 let top = (bits.read_bits(br, 4)? as usize) << 8;
308 let offs = top + (br.read_byte()? as usize);
309 if (subtag != 0) || (offs <= 0xF80) {
310 let len = (subtag as usize) + 3;
311 self.lz_copy(idx, (offs as isize) - 4096, len)?;
314 if offs == 0xFFF { return Ok(()); }
315 let real_off = ((offs >> 4) & 0x7) + 1;
316 let len = ((offs & 0xF) + 2) * 2;
317 validate!(len <= size);
319 let c1 = self.frame[idx - real_off];
320 let c2 = self.frame[idx - real_off + 1];
322 self.frame[idx + i*2 + 0] = c1;
323 self.frame[idx + i*2 + 1] = c2;
328 let b = br.read_byte()?;
329 let off = ((b & 0x7F) as usize) + 1;
330 let len = if (b & 0x80) == 0 { 2 } else { 3 };
331 validate!(len <= size);
333 self.lz_copy(idx, -(off as isize), len)?;
340 let b = br.read_byte()?;
341 if (b & 0xC0) == 0xC0 {
342 len = ((b & 0x3F) as usize) + 8;
343 let q = bits.read_bits(br, 4)? as isize;
344 off = (q << 8) + (br.read_byte()? as isize) + 1;
348 len = ((b >> 4) as usize) + 6;
349 ofs1 = (b & 0xF) as isize;
351 len = ((b & 0x3F) as usize) + 14;
352 ofs1 = bits.read_bits(br, 4)? as isize;
354 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
357 let b = br.read_byte()?;
359 len = (br.read_byte()? as usize) + 21;
361 len = ((b >> 4) as usize) + 6;
363 let ofs1 = (b & 0xF) as isize;
364 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
366 validate!(len <= size);
368 self.lz_copy(idx, off, len)?;
376 impl NADecoder for GremlinVideoDecoder {
377 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
378 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
379 let w = vinfo.get_width();
380 let h = vinfo.get_height();
381 if !vinfo.get_format().is_paletted() { return Err(DecoderError::NotImplemented); }
382 let fmt = formats::PAL8_FORMAT;
383 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
384 self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()));
386 self.frame.resize(PREAMBLE_SIZE + w * h, 0);
390 self.frame[i * 2048 + j * 8 + k] = j as u8;
394 let edata = info.get_extradata().unwrap();
395 validate!(edata.len() == 768);
398 let cc = edata[c * 3 + i];
399 self.pal[c * 3 + (2 - i)] = (cc << 2) | (cc >> 4);
404 Err(DecoderError::InvalidData)
407 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
408 let src = pkt.get_buffer();
409 let mut mr = MemoryReader::new_read(&src);
410 let mut br = ByteReader::new(&mut mr);
411 let flags = br.read_u32le()?;
412 let w = self.info.get_properties().get_video_info().unwrap().get_width();
413 let h = self.info.get_properties().get_video_info().unwrap().get_height();
415 let cmethod = flags & 0xF;
416 let is_intra = (flags & 0x40) != 0;
417 let scale_v = (flags & 0x10) != 0;
418 let scale_h = (flags & 0x20) != 0;
420 self.rescale(w, h, scale_v, scale_h);
422 if (cmethod == 0) || (cmethod == 1) {
425 let b = br.read_byte()?;
426 self.pal[c * 3 + (2 - i)] = (b << 2) | (b >> 4);
430 for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; }
432 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
433 frm.set_keyframe(false);
434 frm.set_frame_type(FrameType::Skip);
435 return Ok(Rc::new(RefCell::new(frm)))
436 } else if cmethod == 3 {
437 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
438 frm.set_keyframe(false);
439 frm.set_frame_type(FrameType::Skip);
440 return Ok(Rc::new(RefCell::new(frm)))
441 } else if cmethod == 2 {
442 self.decode_method2(&mut br)?;
443 } else if cmethod == 5 {
444 self.decode_method5(&mut br, (flags >> 8) as usize)?;
445 } else if cmethod == 6 {
446 self.decode_method68(&mut br, (flags >> 8) as usize, false)?;
447 } else if cmethod == 8 {
448 self.decode_method68(&mut br, (flags >> 8) as usize, true)?;
450 return Err(DecoderError::NotImplemented);
453 let bufret = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0);
454 if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
455 let mut bufinfo = bufret.unwrap();
457 self.output_frame(&mut bufinfo, w, h);
459 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
460 frm.set_keyframe(is_intra);
461 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
462 Ok(Rc::new(RefCell::new(frm)))
466 pub fn get_decoder_video() -> Box<NADecoder> {
467 Box::new(GremlinVideoDecoder::new())
470 struct GremlinAudioDecoder {
473 delta_tab: [i16; 256],
478 impl GremlinAudioDecoder {
480 let mut delta_tab: [i16; 256] = [0; 256];
488 delta_tab[i * 2 + 1] = delta;
489 delta_tab[i * 2 + 2] = -delta;
491 delta_tab[255] = 32767;//delta + (code >> 5);
492 GremlinAudioDecoder {
493 ainfo: NAAudioInfo::new(0, 1, formats::SND_S16_FORMAT, 0),
494 chmap: NAChannelMap::new(),
502 const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C];
503 const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R];
505 fn get_default_chmap(nch: u8) -> NAChannelMap {
506 let mut chmap = NAChannelMap::new();
508 1 => chmap.add_channels(&CHMAP_MONO),
509 2 => chmap.add_channels(&CHMAP_STEREO),
515 impl NADecoder for GremlinAudioDecoder {
516 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
517 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
518 self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), formats::SND_S16P_FORMAT, ainfo.get_block_len());
519 self.chmap = get_default_chmap(ainfo.get_channels());
520 if self.chmap.num_channels() == 0 { return Err(DecoderError::InvalidData); }
523 Err(DecoderError::InvalidData)
526 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
527 let info = pkt.get_stream().get_info();
528 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
529 let pktbuf = pkt.get_buffer();
530 let samples = pktbuf.len() / self.chmap.num_channels();
531 let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?;
532 let mut adata = abuf.get_abuf_i16().unwrap();
533 let off1 = adata.get_offset(1);
534 let mut buf = adata.get_data_mut();
535 if self.chmap.num_channels() == 2 {
536 for (i, e) in pktbuf.chunks(2).enumerate() {
537 self.state0 = self.state0.wrapping_add(self.delta_tab[e[0] as usize]);
538 buf[i] = self.state0;
539 self.state1 = self.state1.wrapping_add(self.delta_tab[e[1] as usize]);
540 buf[off1 + i] = self.state1;
543 for (i, e) in pktbuf.iter().enumerate() {
544 self.state0 += self.delta_tab[*e as usize];
545 buf[i] = self.state0;
548 let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
549 frm.set_duration(Some(samples as u64));
550 frm.set_keyframe(false);
551 Ok(Rc::new(RefCell::new(frm)))
553 Err(DecoderError::InvalidData)
558 pub fn get_decoder_audio() -> Box<NADecoder> {
559 Box::new(GremlinAudioDecoder::new())
564 use nihav_core::codecs::RegisteredDecoders;
565 use nihav_core::demuxers::RegisteredDemuxers;
566 use nihav_core::test::dec_video::test_file_decoding;
567 use crate::codecs::game_register_all_codecs;
568 use crate::demuxers::game_register_all_demuxers;
571 let mut dmx_reg = RegisteredDemuxers::new();
572 game_register_all_demuxers(&mut dmx_reg);
573 let mut dec_reg = RegisteredDecoders::new();
574 game_register_all_codecs(&mut dec_reg);
576 test_file_decoding("gdv", "assets/Game/intro1.gdv", Some(10), true, false, None, &dmx_reg, &dec_reg);