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 {
25 const PREAMBLE_SIZE: usize = 4096;
28 fn new() -> Self { Bits8 { queue: 0, fill: 0 } }
29 fn read_2bits(&mut self, br: &mut ByteReader) -> ByteIOResult<u8> {
31 self.queue = br.read_byte()?;
34 let res = self.queue >> 6;
42 fn new() -> Self { Bits32 { queue: 0, fill: 0 } }
43 fn fill(&mut self, br: &mut ByteReader) -> ByteIOResult<()> {
44 self.queue = br.read_u32le()?;
48 fn read_bits(&mut self, br: &mut ByteReader, nbits: u8) -> ByteIOResult<u32> {
49 let res = self.queue & ((1 << nbits) - 1);
53 self.queue |= (br.read_u16le()? as u32) << self.fill;
60 impl GremlinVideoDecoder {
63 info: NACodecInfoRef::default(), pal: [0; 768], frame: Vec::new(),
64 scale_v: false, scale_h: false
68 fn lz_copy(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> {
69 if idx + len > self.frame.len() { return Err(DecoderError::InvalidData); }
71 let c = self.frame[idx - 1];
72 for i in 0..len { self.frame[idx + i] = c; }
73 } else if offset < 0 {
74 let start = idx - (-offset as usize);
75 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
77 if idx + (offset as usize) + len > self.frame.len() { return Err(DecoderError::InvalidData); }
78 let start = idx + (offset as usize);
79 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
84 fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
85 if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }
87 if self.scale_h && self.scale_v {
92 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
95 } else if self.scale_h {
99 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w];
102 } else if self.scale_v {
107 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)];
112 if scale_h && scale_v {
115 self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w];
121 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w];
127 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w];
132 self.scale_v = scale_v;
133 self.scale_h = scale_h;
136 fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
137 let bufo = bufinfo.get_vbuf();
138 let mut buf = bufo.unwrap();
139 let paloff = buf.get_offset(1);
140 let stride = buf.get_stride(0);
141 let data = buf.get_data_mut().unwrap();
142 let dst = data.as_mut_slice();
143 let mut sidx = PREAMBLE_SIZE;
146 for i in 0..768 { dst[paloff + i] = self.pal[i]; }
147 if !self.scale_v && !self.scale_h {
149 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
156 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
158 for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; }
160 if !self.scale_h || ((y & 1) == 1) {
161 sidx += if !self.scale_v { w } else { w/2 };
168 fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
169 let mut bits = Bits8::new();
171 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
172 self.info.get_properties().get_video_info().unwrap().get_height();
173 let mut idx = PREAMBLE_SIZE;
174 if self.frame[8] != 0 {
176 for i in 0..16 { self.frame[c * 16 + i] = c as u8; }
180 let tag = bits.read_2bits(br)?;
182 self.frame[idx] = br.read_byte()?;
186 let b = br.read_byte()?;
187 let len = ((b & 0xF) as usize) + 3;
188 let bot = (b >> 4) as isize;
189 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
190 validate!(len <= size);
192 self.lz_copy(idx, off, len)?;
195 let len = (br.read_byte()? as usize) + 2;
196 validate!(len <= size);
206 fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> {
207 let mut bits = Bits8::new();
209 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
210 self.info.get_properties().get_video_info().unwrap().get_height();
211 let mut idx = PREAMBLE_SIZE;
212 validate!(size >= skip);
216 let tag = bits.read_2bits(br)?;
218 self.frame[idx] = br.read_byte()?;
222 let b = br.read_byte()?;
223 let len = ((b & 0xF) as usize) + 3;
224 let bot = (b >> 4) as isize;
225 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
226 validate!(len <= size);
228 self.lz_copy(idx, off, len)?;
231 let b = br.read_byte()?;
233 let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1;
234 validate!(len <= size);
238 let b = br.read_byte()?;
239 let len = ((b & 0x3) as usize) + 2;
240 let off = -((b >> 2) as isize) - 1;
241 validate!(len <= size);
243 self.lz_copy(idx, off, len)?;
250 fn decode_method68(&mut self, br: &mut ByteReader,
251 skip: usize, use8: bool) -> DecoderResult<()> {
252 let mut bits = Bits32::new();
254 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
255 self.info.get_properties().get_video_info().unwrap().get_height();
256 let mut idx = PREAMBLE_SIZE;
257 validate!(size >= skip);
262 let tag = bits.read_bits(br, 2)?;
264 let b = bits.read_bits(br, 1)?;
266 self.frame[idx] = br.read_byte()?;
270 let mut len: usize = 2;
274 let val = bits.read_bits(br, lbits)?;
276 if val != ((1 << lbits) - 1) { break; }
277 validate!(lbits < 16);
279 validate!(len <= size);
280 for i in 0..len { self.frame[idx + i] = br.read_byte()?; }
284 } else if tag == 1 { //skip
285 let b = bits.read_bits(br, 1)?;
288 len = (bits.read_bits(br, 4)? as usize) + 2;
290 let bb = br.read_byte()?;
291 if (bb & 0x80) == 0 {
292 len = (bb as usize) + 18;
294 let top = ((bb & 0x7F) as usize) << 8;
295 len = top + (br.read_byte()? as usize) + 146;
298 validate!(len <= size);
302 let subtag = bits.read_bits(br, 2)? as usize;
304 let top = (bits.read_bits(br, 4)? as usize) << 8;
305 let offs = top + (br.read_byte()? as usize);
306 if (subtag != 0) || (offs <= 0xF80) {
307 let len = (subtag as usize) + 3;
308 self.lz_copy(idx, (offs as isize) - 4096, len)?;
311 if offs == 0xFFF { return Ok(()); }
312 let real_off = ((offs >> 4) & 0x7) + 1;
313 let len = ((offs & 0xF) + 2) * 2;
314 validate!(len <= size);
316 let c1 = self.frame[idx - real_off];
317 let c2 = self.frame[idx - real_off + 1];
319 self.frame[idx + i*2 + 0] = c1;
320 self.frame[idx + i*2 + 1] = c2;
325 let b = br.read_byte()?;
326 let off = ((b & 0x7F) as usize) + 1;
327 let len = if (b & 0x80) == 0 { 2 } else { 3 };
328 validate!(len <= size);
330 self.lz_copy(idx, -(off as isize), len)?;
337 let b = br.read_byte()?;
338 if (b & 0xC0) == 0xC0 {
339 len = ((b & 0x3F) as usize) + 8;
340 let q = bits.read_bits(br, 4)? as isize;
341 off = (q << 8) + (br.read_byte()? as isize) + 1;
345 len = ((b >> 4) as usize) + 6;
346 ofs1 = (b & 0xF) as isize;
348 len = ((b & 0x3F) as usize) + 14;
349 ofs1 = bits.read_bits(br, 4)? as isize;
351 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
354 let b = br.read_byte()?;
356 len = (br.read_byte()? as usize) + 21;
358 len = ((b >> 4) as usize) + 6;
360 let ofs1 = (b & 0xF) as isize;
361 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
363 validate!(len <= size);
365 self.lz_copy(idx, off, len)?;
373 impl NADecoder for GremlinVideoDecoder {
374 fn init(&mut self, info: NACodecInfoRef) -> DecoderResult<()> {
375 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
376 let w = vinfo.get_width();
377 let h = vinfo.get_height();
378 if !vinfo.get_format().is_paletted() { return Err(DecoderError::NotImplemented); }
379 let fmt = formats::PAL8_FORMAT;
380 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
381 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
383 self.frame.resize(PREAMBLE_SIZE + w * h, 0);
387 self.frame[i * 2048 + j * 8 + k] = j as u8;
391 let edata = info.get_extradata().unwrap();
392 validate!(edata.len() == 768);
395 let cc = edata[c * 3 + i];
396 self.pal[c * 3 + (2 - i)] = (cc << 2) | (cc >> 4);
401 Err(DecoderError::InvalidData)
404 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
405 let src = pkt.get_buffer();
406 let mut mr = MemoryReader::new_read(&src);
407 let mut br = ByteReader::new(&mut mr);
408 let flags = br.read_u32le()?;
409 let w = self.info.get_properties().get_video_info().unwrap().get_width();
410 let h = self.info.get_properties().get_video_info().unwrap().get_height();
412 let cmethod = flags & 0xF;
413 let is_intra = (flags & 0x40) != 0;
414 let scale_v = (flags & 0x10) != 0;
415 let scale_h = (flags & 0x20) != 0;
417 self.rescale(w, h, scale_v, scale_h);
419 if (cmethod == 0) || (cmethod == 1) {
422 let b = br.read_byte()?;
423 self.pal[c * 3 + (2 - i)] = (b << 2) | (b >> 4);
427 for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; }
429 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
430 frm.set_keyframe(false);
431 frm.set_frame_type(FrameType::Skip);
432 return Ok(frm.into_ref())
433 } else if cmethod == 3 {
434 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
435 frm.set_keyframe(false);
436 frm.set_frame_type(FrameType::Skip);
437 return Ok(frm.into_ref())
438 } else if cmethod == 2 {
439 self.decode_method2(&mut br)?;
440 } else if cmethod == 5 {
441 self.decode_method5(&mut br, (flags >> 8) as usize)?;
442 } else if cmethod == 6 {
443 self.decode_method68(&mut br, (flags >> 8) as usize, false)?;
444 } else if cmethod == 8 {
445 self.decode_method68(&mut br, (flags >> 8) as usize, true)?;
447 return Err(DecoderError::NotImplemented);
450 let bufret = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0);
451 if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
452 let mut bufinfo = bufret.unwrap();
454 self.output_frame(&mut bufinfo, w, h);
456 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
457 frm.set_keyframe(is_intra);
458 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
463 pub fn get_decoder_video() -> Box<NADecoder> {
464 Box::new(GremlinVideoDecoder::new())
467 struct GremlinAudioDecoder {
470 delta_tab: [i16; 256],
475 impl GremlinAudioDecoder {
477 let mut delta_tab: [i16; 256] = [0; 256];
485 delta_tab[i * 2 + 1] = delta;
486 delta_tab[i * 2 + 2] = -delta;
488 delta_tab[255] = 32767;//delta + (code >> 5);
489 GremlinAudioDecoder {
490 ainfo: NAAudioInfo::new(0, 1, formats::SND_S16_FORMAT, 0),
491 chmap: NAChannelMap::new(),
499 const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C];
500 const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R];
502 fn get_default_chmap(nch: u8) -> NAChannelMap {
503 let mut chmap = NAChannelMap::new();
505 1 => chmap.add_channels(&CHMAP_MONO),
506 2 => chmap.add_channels(&CHMAP_STEREO),
512 impl NADecoder for GremlinAudioDecoder {
513 fn init(&mut self, info: NACodecInfoRef) -> DecoderResult<()> {
514 if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
515 self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), formats::SND_S16P_FORMAT, ainfo.get_block_len());
516 self.chmap = get_default_chmap(ainfo.get_channels());
517 if self.chmap.num_channels() == 0 { return Err(DecoderError::InvalidData); }
520 Err(DecoderError::InvalidData)
523 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
524 let info = pkt.get_stream().get_info();
525 if let NACodecTypeInfo::Audio(_) = info.get_properties() {
526 let pktbuf = pkt.get_buffer();
527 let samples = pktbuf.len() / self.chmap.num_channels();
528 let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?;
529 let mut adata = abuf.get_abuf_i16().unwrap();
530 let off1 = adata.get_offset(1);
531 let buf = adata.get_data_mut().unwrap();
532 if self.chmap.num_channels() == 2 {
533 for (i, e) in pktbuf.chunks(2).enumerate() {
534 self.state0 = self.state0.wrapping_add(self.delta_tab[e[0] as usize]);
535 buf[i] = self.state0;
536 self.state1 = self.state1.wrapping_add(self.delta_tab[e[1] as usize]);
537 buf[off1 + i] = self.state1;
540 for (i, e) in pktbuf.iter().enumerate() {
541 self.state0 += self.delta_tab[*e as usize];
542 buf[i] = self.state0;
545 let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
546 frm.set_duration(Some(samples as u64));
547 frm.set_keyframe(false);
550 Err(DecoderError::InvalidData)
555 pub fn get_decoder_audio() -> Box<NADecoder> {
556 Box::new(GremlinAudioDecoder::new())
561 use nihav_core::codecs::RegisteredDecoders;
562 use nihav_core::demuxers::RegisteredDemuxers;
563 use nihav_core::test::dec_video::test_file_decoding;
564 use crate::codecs::game_register_all_codecs;
565 use crate::demuxers::game_register_all_demuxers;
568 let mut dmx_reg = RegisteredDemuxers::new();
569 game_register_all_demuxers(&mut dmx_reg);
570 let mut dec_reg = RegisteredDecoders::new();
571 game_register_all_codecs(&mut dec_reg);
573 test_file_decoding("gdv", "assets/Game/intro1.gdv", Some(10), true, false, None, &dmx_reg, &dec_reg);