1 use nihav_core::frame::*;
2 use nihav_core::formats;
3 use nihav_core::formats::{NAChannelType, NAChannelMap};
4 use nihav_core::codecs::*;
5 use nihav_core::io::byteio::*;
7 struct GremlinVideoDecoder {
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 |= u32::from(br.read_u16le()?) << self.fill;
62 impl GremlinVideoDecoder {
65 info: NACodecInfoRef::default(), pal: [0; 768],
66 frame: Vec::new(), frame16: Vec::new(),
67 scale_v: false, scale_h: false, is_16bit: 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 lz_copy16(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> {
88 if idx + len > self.frame16.len() { return Err(DecoderError::InvalidData); }
90 let c = self.frame16[idx - 1];
91 for i in 0..len { self.frame16[idx + i] = c; }
92 } else if offset < 0 {
93 let start = idx - (-offset as usize);
94 for i in 0..len { self.frame16[idx + i] = self.frame16[start + i]; }
96 if idx + (offset as usize) + len > self.frame16.len() { return Err(DecoderError::InvalidData); }
97 let start = idx + (offset as usize);
98 for i in 0..len { self.frame16[idx + i] = self.frame16[start + i]; }
103 fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
104 if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }
106 if self.scale_h && self.scale_v {
111 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
114 } else if self.scale_h {
118 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w];
121 } else if self.scale_v {
126 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)];
131 if scale_h && scale_v {
134 self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w];
140 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w];
146 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w];
151 self.scale_v = scale_v;
152 self.scale_h = scale_h;
155 fn rescale16(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
156 if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }
158 if self.scale_h && self.scale_v {
163 self.frame16[PREAMBLE_SIZE + x + y * w] = self.frame16[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
166 } else if self.scale_h {
170 self.frame16[PREAMBLE_SIZE + x + y * w] = self.frame16[PREAMBLE_SIZE + x + (y/2) * w];
173 } else if self.scale_v {
178 self.frame16[PREAMBLE_SIZE + x + y * w] = self.frame16[PREAMBLE_SIZE + x/2 + y * (w/2)];
183 if scale_h && scale_v {
186 self.frame16[PREAMBLE_SIZE + x + y * (w/2)] = self.frame16[PREAMBLE_SIZE + x*2 + y*2 * w];
192 self.frame16[PREAMBLE_SIZE + x + y * w] = self.frame16[PREAMBLE_SIZE + x + y*2 * w];
198 self.frame16[PREAMBLE_SIZE + x + y * w] = self.frame16[PREAMBLE_SIZE + x*2 + y * w];
203 self.scale_v = scale_v;
204 self.scale_h = scale_h;
207 fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
208 let bufo = bufinfo.get_vbuf();
209 let mut buf = bufo.unwrap();
210 let paloff = buf.get_offset(1);
211 let stride = buf.get_stride(0);
212 let data = buf.get_data_mut().unwrap();
213 let dst = data.as_mut_slice();
214 let mut sidx = PREAMBLE_SIZE;
217 dst[paloff..][..768].copy_from_slice(&self.pal);
218 if !self.scale_v && !self.scale_h {
220 dst[didx..][..w].copy_from_slice(&self.frame[sidx..][..w]);
227 dst[didx..][..w].copy_from_slice(&self.frame[sidx..][..w]);
229 for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; }
231 if !self.scale_h || ((y & 1) == 1) {
232 sidx += if !self.scale_v { w } else { w/2 };
239 fn output_frame16(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
240 let bufo = bufinfo.get_vbuf16();
241 let mut buf = bufo.unwrap();
242 let stride = buf.get_stride(0);
243 let data = buf.get_data_mut().unwrap();
244 let dst = data.as_mut_slice();
246 if !self.scale_v && !self.scale_h {
247 for (dline, sline) in dst.chunks_mut(stride).zip(self.frame16[PREAMBLE_SIZE..].chunks(w)).take(h) {
248 dline[..w].copy_from_slice(sline);
250 } else if !self.scale_h {
251 for (dline, sline) in dst.chunks_mut(stride).zip(self.frame16[PREAMBLE_SIZE..].chunks(w / 2)).take(h) {
252 for (dst, &src) in dline.chunks_mut(2).zip(sline.iter()) {
257 } else if !self.scale_v {
258 for (dline, sline) in dst.chunks_mut(stride * 2).zip(self.frame16[PREAMBLE_SIZE..].chunks(w)).take(h) {
259 dline[..w].copy_from_slice(sline);
260 dline[stride..][..w].copy_from_slice(sline);
263 for (dline, sline) in dst.chunks_mut(stride).zip(self.frame16[PREAMBLE_SIZE..].chunks(w / 2)).take(h) {
265 dline[x * 2] = sline[x];
266 dline[x * 2 + 1] = sline[x];
267 dline[x * 2 + stride] = sline[x];
268 dline[x * 2 + 1 + stride] = sline[x];
274 fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
275 let mut bits = Bits8::new();
277 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
278 self.info.get_properties().get_video_info().unwrap().get_height();
279 let mut idx = PREAMBLE_SIZE;
280 if self.frame[8] != 0 {
282 for i in 0..16 { self.frame[c * 16 + i] = c as u8; }
286 let tag = bits.read_2bits(br)?;
288 self.frame[idx] = br.read_byte()?;
292 let b = br.read_byte()?;
293 let len = ((b & 0xF) as usize) + 3;
294 let bot = (b >> 4) as isize;
295 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
296 validate!(len <= size);
298 self.lz_copy(idx, off, len)?;
301 let len = (br.read_byte()? as usize) + 2;
302 validate!(len <= size);
312 fn decode_method2_16bit(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
313 let mut bits = Bits8::new();
315 if self.frame16[8] != 64 {
316 for (i, dst) in self.frame16[..PREAMBLE_SIZE].chunks_mut(4).enumerate() {
317 let val = (i as u16) << 5;
318 for el in dst.iter_mut() {
324 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
325 self.info.get_properties().get_video_info().unwrap().get_height();
326 let mut idx = PREAMBLE_SIZE;
328 let tag = bits.read_2bits(br)?;
330 self.frame16[idx] = br.read_u16le()?;
334 let b = br.read_byte()?;
335 let len = ((b & 0xF) as usize) + 3;
336 let bot = (b >> 4) as isize;
337 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
338 validate!(len <= size);
340 self.lz_copy16(idx, off, len)?;
343 let len = (br.read_byte()? as usize) + 2;
344 validate!(len <= size);
354 fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> {
355 let mut bits = Bits8::new();
357 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
358 self.info.get_properties().get_video_info().unwrap().get_height();
359 let mut idx = PREAMBLE_SIZE;
360 validate!(size >= skip);
364 let tag = bits.read_2bits(br)?;
366 self.frame[idx] = br.read_byte()?;
370 let b = br.read_byte()?;
371 let len = ((b & 0xF) as usize) + 3;
372 let bot = (b >> 4) as isize;
373 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
374 validate!(len <= size);
376 self.lz_copy(idx, off, len)?;
379 let b = br.read_byte()?;
381 let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1;
382 validate!(len <= size);
386 let b = br.read_byte()?;
387 let len = ((b & 0x3) as usize) + 2;
388 let off = -((b >> 2) as isize) - 1;
389 validate!(len <= size);
391 self.lz_copy(idx, off, len)?;
398 #[allow(clippy::identity_op)]
399 fn decode_method68(&mut self, br: &mut ByteReader,
400 skip: usize, use8: bool) -> DecoderResult<()> {
401 let mut bits = Bits32::new();
403 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
404 self.info.get_properties().get_video_info().unwrap().get_height();
405 let mut idx = PREAMBLE_SIZE;
406 validate!(size >= skip);
411 let tag = bits.read_bits(br, 2)?;
413 let b = bits.read_bits(br, 1)?;
415 self.frame[idx] = br.read_byte()?;
419 let mut len: usize = 2;
423 let val = bits.read_bits(br, lbits)?;
425 if val != ((1 << lbits) - 1) { break; }
426 validate!(lbits < 16);
428 validate!(len <= size);
429 for i in 0..len { self.frame[idx + i] = br.read_byte()?; }
433 } else if tag == 1 { //skip
434 let b = bits.read_bits(br, 1)?;
437 len = (bits.read_bits(br, 4)? as usize) + 2;
439 let bb = br.read_byte()?;
440 if (bb & 0x80) == 0 {
441 len = (bb as usize) + 18;
443 let top = ((bb & 0x7F) as usize) << 8;
444 len = top + (br.read_byte()? as usize) + 146;
447 validate!(len <= size);
451 let subtag = bits.read_bits(br, 2)? as usize;
453 let top = (bits.read_bits(br, 4)? as usize) << 8;
454 let offs = top + (br.read_byte()? as usize);
455 if (subtag != 0) || (offs <= 0xF80) {
456 let len = (subtag as usize) + 3;
457 self.lz_copy(idx, (offs as isize) - 4096, len)?;
460 if offs == 0xFFF { return Ok(()); }
461 let real_off = ((offs >> 4) & 0x7) + 1;
462 let len = ((offs & 0xF) + 2) * 2;
463 validate!(len <= size);
465 let c1 = self.frame[idx - real_off];
466 let c2 = self.frame[idx - real_off + 1];
468 self.frame[idx + i*2 + 0] = c1;
469 self.frame[idx + i*2 + 1] = c2;
474 let b = br.read_byte()?;
475 let off = ((b & 0x7F) as usize) + 1;
476 let len = if (b & 0x80) == 0 { 2 } else { 3 };
477 validate!(len <= size);
479 self.lz_copy(idx, -(off as isize), len)?;
486 let b = br.read_byte()?;
487 if (b & 0xC0) == 0xC0 {
488 len = ((b & 0x3F) as usize) + 8;
489 let q = bits.read_bits(br, 4)? as isize;
490 off = (q << 8) + (br.read_byte()? as isize) + 1;
494 len = ((b >> 4) as usize) + 6;
495 ofs1 = (b & 0xF) as isize;
497 len = ((b & 0x3F) as usize) + 14;
498 ofs1 = bits.read_bits(br, 4)? as isize;
500 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
503 let b = br.read_byte()?;
505 len = (br.read_byte()? as usize) + 21;
507 len = ((b >> 4) as usize) + 6;
509 let ofs1 = (b & 0xF) as isize;
510 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
512 validate!(len <= size);
514 self.lz_copy(idx, off, len)?;
522 const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
524 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 10, comp_offs: 0, next_elem: 2 }),
525 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 5, comp_offs: 1, next_elem: 2 }),
526 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 0, comp_offs: 2, next_elem: 2 }),
528 elem_size: 2, be: false, alpha: false, palette: false };
530 impl NADecoder for GremlinVideoDecoder {
531 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
532 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
533 let w = vinfo.get_width();
534 let h = vinfo.get_height();
535 self.is_16bit = !vinfo.get_format().is_paletted();
536 let fmt = if !self.is_16bit { PAL8_FORMAT } else { RGB555_FORMAT };
537 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
538 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
541 self.frame.resize(PREAMBLE_SIZE + w * h, 0);
545 self.frame[i * 2048 + j * 8 + k] = j as u8;
549 let edata = info.get_extradata().unwrap();
550 validate!(edata.len() == 768);
553 let cc = edata[c * 3 + i];
554 self.pal[c * 3 + i] = (cc << 2) | (cc >> 4);
558 self.frame16.resize(PREAMBLE_SIZE + w * h, 0);
562 Err(DecoderError::InvalidData)
565 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
566 let src = pkt.get_buffer();
567 let mut mr = MemoryReader::new_read(&src);
568 let mut br = ByteReader::new(&mut mr);
569 let flags = br.read_u32le()?;
570 let w = self.info.get_properties().get_video_info().unwrap().get_width();
571 let h = self.info.get_properties().get_video_info().unwrap().get_height();
573 let cmethod = flags & 0xF;
574 let is_intra = (flags & 0x40) != 0;
575 let scale_v = (flags & 0x10) != 0;
576 let scale_h = (flags & 0x20) != 0;
579 self.rescale(w, h, scale_v, scale_h);
581 self.rescale16(w, h, scale_v, scale_h);
584 if self.is_16bit && (cmethod != 2) && (cmethod != 3) {
585 return Err(DecoderError::NotImplemented);
587 if (cmethod == 0) || (cmethod == 1) {
588 validate!(!self.is_16bit);
591 let b = br.read_byte()?;
592 self.pal[c * 3 + i] = (b << 2) | (b >> 4);
596 for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; }
598 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
599 frm.set_keyframe(false);
600 frm.set_frame_type(FrameType::Skip);
601 return Ok(frm.into_ref())
602 } else if cmethod == 3 {
603 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
604 frm.set_keyframe(false);
605 frm.set_frame_type(FrameType::Skip);
606 return Ok(frm.into_ref())
607 } else if cmethod == 2 {
609 self.decode_method2(&mut br)?;
611 self.decode_method2_16bit(&mut br)?;
613 } else if cmethod == 5 {
614 self.decode_method5(&mut br, (flags >> 8) as usize)?;
615 } else if cmethod == 6 {
616 self.decode_method68(&mut br, (flags >> 8) as usize, false)?;
617 } else if cmethod == 8 {
618 self.decode_method68(&mut br, (flags >> 8) as usize, true)?;
620 return Err(DecoderError::NotImplemented);
623 let mut bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0)?;
626 self.output_frame(&mut bufinfo, w, h);
628 self.output_frame16(&mut bufinfo, w, h);
631 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
632 frm.set_keyframe(is_intra);
633 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
636 fn flush(&mut self) {
640 impl NAOptionHandler for GremlinVideoDecoder {
641 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
642 fn set_options(&mut self, _options: &[NAOption]) { }
643 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
646 pub fn get_decoder_video() -> Box<dyn NADecoder + Send> {
647 Box::new(GremlinVideoDecoder::new())
650 struct GremlinAudioDecoder {
653 delta_tab: [i16; 256],
658 impl GremlinAudioDecoder {
660 let mut delta_tab: [i16; 256] = [0; 256];
668 delta_tab[i * 2 + 1] = delta;
669 delta_tab[i * 2 + 2] = -delta;
671 delta_tab[255] = 32767;//delta + (code >> 5);
672 GremlinAudioDecoder {
673 ainfo: NAAudioInfo::new(0, 1, formats::SND_S16_FORMAT, 0),
674 chmap: NAChannelMap::new(),
682 const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C];
683 const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R];
685 fn get_default_chmap(nch: u8) -> NAChannelMap {
686 let mut chmap = NAChannelMap::new();
688 1 => chmap.add_channels(&CHMAP_MONO),
689 2 => chmap.add_channels(&CHMAP_STEREO),
695 impl NADecoder for GremlinAudioDecoder {
696 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
697 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
698 self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), formats::SND_S16P_FORMAT, ainfo.get_block_len());
699 self.chmap = get_default_chmap(ainfo.get_channels());
700 if self.chmap.num_channels() == 0 { return Err(DecoderError::InvalidData); }
703 Err(DecoderError::InvalidData)
706 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
707 let info = pkt.get_stream().get_info();
708 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
709 let pktbuf = pkt.get_buffer();
710 let samples = pktbuf.len() / self.chmap.num_channels();
711 let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?;
712 let mut adata = abuf.get_abuf_i16().unwrap();
713 let off1 = adata.get_offset(1);
714 let buf = adata.get_data_mut().unwrap();
715 if self.chmap.num_channels() == 2 {
716 for (i, e) in pktbuf.chunks(2).enumerate() {
717 self.state0 = self.state0.wrapping_add(self.delta_tab[e[0] as usize]);
718 buf[i] = self.state0;
719 self.state1 = self.state1.wrapping_add(self.delta_tab[e[1] as usize]);
720 buf[off1 + i] = self.state1;
723 for (i, e) in pktbuf.iter().enumerate() {
724 self.state0 += self.delta_tab[*e as usize];
725 buf[i] = self.state0;
728 let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
729 frm.set_duration(Some(samples as u64));
730 frm.set_keyframe(false);
733 Err(DecoderError::InvalidData)
736 fn flush(&mut self) {
740 impl NAOptionHandler for GremlinAudioDecoder {
741 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
742 fn set_options(&mut self, _options: &[NAOption]) { }
743 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
746 pub fn get_decoder_audio() -> Box<dyn NADecoder + Send> {
747 Box::new(GremlinAudioDecoder::new())
752 use nihav_core::codecs::RegisteredDecoders;
753 use nihav_core::demuxers::RegisteredDemuxers;
754 use nihav_codec_support::test::dec_video::*;
755 use crate::game_register_all_decoders;
756 use crate::game_register_all_demuxers;
759 fn test_gdv_video() {
760 let mut dmx_reg = RegisteredDemuxers::new();
761 game_register_all_demuxers(&mut dmx_reg);
762 let mut dec_reg = RegisteredDecoders::new();
763 game_register_all_decoders(&mut dec_reg);
765 test_decoding("gdv", "gdv-video", "assets/Game/intro1.gdv", Some(10), &dmx_reg, &dec_reg,
766 ExpectedTestResult::MD5([0x7ea302bf, 0xc3e210cf, 0x6e341376, 0x9e976056]));
769 fn test_gdv_video16() {
770 let mut dmx_reg = RegisteredDemuxers::new();
771 game_register_all_demuxers(&mut dmx_reg);
772 let mut dec_reg = RegisteredDecoders::new();
773 game_register_all_decoders(&mut dec_reg);
775 test_decoding("gdv", "gdv-video", "assets/Game/SHELI_S.GDV", Some(10), &dmx_reg, &dec_reg,
776 ExpectedTestResult::MD5([0x9acfdefd, 0x12687f8c, 0x9ef1cd56, 0x019240bb]));
779 fn test_gdv_audio() {
780 let mut dmx_reg = RegisteredDemuxers::new();
781 game_register_all_demuxers(&mut dmx_reg);
782 let mut dec_reg = RegisteredDecoders::new();
783 game_register_all_decoders(&mut dec_reg);
785 test_decoding("gdv", "gdv-audio", "assets/Game/intro1.gdv", None, &dmx_reg, &dec_reg,
786 ExpectedTestResult::MD5([0xd41d8cd9, 0x8f00b204, 0xe9800998, 0xecf8427e]));