5 struct GremlinVideoDecoder {
23 const PREAMBLE_SIZE: usize = 4096;
26 fn new() -> Self { Bits8 { queue: 0, fill: 0 } }
27 fn read_2bits(&mut self, br: &mut ByteReader) -> ByteIOResult<u8> {
29 self.queue = br.read_byte()?;
32 let res = self.queue >> 6;
40 fn new() -> Self { Bits32 { queue: 0, fill: 0 } }
41 fn fill(&mut self, br: &mut ByteReader) -> ByteIOResult<()> {
42 self.queue = br.read_u32le()?;
46 fn read_bits(&mut self, br: &mut ByteReader, nbits: u8) -> ByteIOResult<u32> {
47 let res = self.queue & ((1 << nbits) - 1);
51 self.queue |= (br.read_u16le()? as u32) << self.fill;
58 impl GremlinVideoDecoder {
60 let dummy_info = Rc::new(DUMMY_CODEC_INFO);
62 info: dummy_info, pal: [0; 768], frame: Vec::new(),
63 scale_v: false, scale_h: false
67 fn lz_copy(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> {
68 if idx + len > self.frame.len() { return Err(DecoderError::InvalidData); }
70 let c = self.frame[idx - 1];
71 for i in 0..len { self.frame[idx + i] = c; }
72 } else if offset < 0 {
73 let start = idx - (-offset as usize);
74 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
76 if idx + (offset as usize) + len > self.frame.len() { return Err(DecoderError::InvalidData); }
77 let start = idx + (offset as usize);
78 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
83 fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
84 if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }
86 if self.scale_h && self.scale_v {
91 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
94 } else if self.scale_h {
98 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w];
101 } else if self.scale_v {
106 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)];
111 if scale_h && scale_v {
114 self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w];
120 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w];
126 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w];
131 self.scale_v = scale_v;
132 self.scale_h = scale_h;
135 fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
136 let bufo = bufinfo.get_vbuf();
137 let mut buf = bufo.unwrap();
138 let paloff = buf.get_offset(1);
139 let stride = buf.get_stride(0);
140 let mut data = buf.get_data_mut();
141 let dst = data.as_mut_slice();
142 let mut sidx = PREAMBLE_SIZE;
145 for i in 0..768 { dst[paloff + i] = self.pal[i]; }
146 if !self.scale_v && !self.scale_h {
148 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
155 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
157 for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; }
159 if !self.scale_h || ((y & 1) == 1) {
160 sidx += if !self.scale_v { w } else { w/2 };
167 fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
168 let mut bits = Bits8::new();
170 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
171 self.info.get_properties().get_video_info().unwrap().get_height();
172 let mut idx = PREAMBLE_SIZE;
173 if self.frame[8] != 0 {
175 for i in 0..16 { self.frame[c * 16 + i] = c as u8; }
179 let tag = bits.read_2bits(br)?;
181 self.frame[idx] = br.read_byte()?;
185 let b = br.read_byte()?;
186 let len = ((b & 0xF) as usize) + 3;
187 let bot = (b >> 4) as isize;
188 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
189 validate!(len <= size);
191 self.lz_copy(idx, off, len)?;
194 let len = (br.read_byte()? as usize) + 2;
195 validate!(len <= size);
205 fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> {
206 let mut bits = Bits8::new();
208 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
209 self.info.get_properties().get_video_info().unwrap().get_height();
210 let mut idx = PREAMBLE_SIZE;
211 validate!(size >= skip);
215 let tag = bits.read_2bits(br)?;
217 self.frame[idx] = br.read_byte()?;
221 let b = br.read_byte()?;
222 let len = ((b & 0xF) as usize) + 3;
223 let bot = (b >> 4) as isize;
224 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
225 validate!(len <= size);
227 self.lz_copy(idx, off, len)?;
230 let b = br.read_byte()?;
232 let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1;
233 validate!(len <= size);
237 let b = br.read_byte()?;
238 let len = ((b & 0x3) as usize) + 2;
239 let off = -((b >> 2) as isize) - 1;
240 validate!(len <= size);
242 self.lz_copy(idx, off, len)?;
249 fn decode_method68(&mut self, br: &mut ByteReader,
250 skip: usize, use8: bool) -> DecoderResult<()> {
251 let mut bits = Bits32::new();
253 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
254 self.info.get_properties().get_video_info().unwrap().get_height();
255 let mut idx = PREAMBLE_SIZE;
256 validate!(size >= skip);
261 let tag = bits.read_bits(br, 2)?;
263 let b = bits.read_bits(br, 1)?;
265 self.frame[idx] = br.read_byte()?;
269 let mut len: usize = 2;
273 let val = bits.read_bits(br, lbits)?;
275 if val != ((1 << lbits) - 1) { break; }
276 validate!(lbits < 16);
278 validate!(len <= size);
279 for i in 0..len { self.frame[idx + i] = br.read_byte()?; }
283 } else if tag == 1 { //skip
284 let b = bits.read_bits(br, 1)?;
287 len = (bits.read_bits(br, 4)? as usize) + 2;
289 let bb = br.read_byte()?;
290 if (bb & 0x80) == 0 {
291 len = (bb as usize) + 18;
293 let top = ((bb & 0x7F) as usize) << 8;
294 len = top + (br.read_byte()? as usize) + 146;
297 validate!(len <= size);
301 let subtag = bits.read_bits(br, 2)? as usize;
303 let top = (bits.read_bits(br, 4)? as usize) << 8;
304 let offs = top + (br.read_byte()? as usize);
305 if (subtag != 0) || (offs <= 0xF80) {
306 let len = (subtag as usize) + 3;
307 self.lz_copy(idx, (offs as isize) - 4096, len)?;
310 if offs == 0xFFF { return Ok(()); }
311 let real_off = ((offs >> 4) & 0x7) + 1;
312 let len = ((offs & 0xF) + 2) * 2;
313 validate!(len <= size);
315 let c1 = self.frame[idx - real_off];
316 let c2 = self.frame[idx - real_off + 1];
318 self.frame[idx + i*2 + 0] = c1;
319 self.frame[idx + i*2 + 1] = c2;
324 let b = br.read_byte()?;
325 let off = ((b & 0x7F) as usize) + 1;
326 let len = if (b & 0x80) == 0 { 2 } else { 3 };
327 validate!(len <= size);
329 self.lz_copy(idx, -(off as isize), len)?;
336 let b = br.read_byte()?;
337 if (b & 0xC0) == 0xC0 {
338 len = ((b & 0x3F) as usize) + 8;
339 let q = bits.read_bits(br, 4)? as isize;
340 off = (q << 8) + (br.read_byte()? as isize) + 1;
344 len = ((b >> 4) as usize) + 6;
345 ofs1 = (b & 0xF) as isize;
347 len = ((b & 0x3F) as usize) + 14;
348 ofs1 = bits.read_bits(br, 4)? as isize;
350 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
353 let b = br.read_byte()?;
355 len = (br.read_byte()? as usize) + 21;
357 len = ((b >> 4) as usize) + 6;
359 let ofs1 = (b & 0xF) as isize;
360 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
362 validate!(len <= size);
364 self.lz_copy(idx, off, len)?;
372 impl NADecoder for GremlinVideoDecoder {
373 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
374 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
375 let w = vinfo.get_width();
376 let h = vinfo.get_height();
377 if !vinfo.get_format().is_paletted() { return Err(DecoderError::NotImplemented); }
378 let fmt = formats::PAL8_FORMAT;
379 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
380 self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()));
382 self.frame.resize(PREAMBLE_SIZE + w * h, 0);
386 self.frame[i * 2048 + j * 8 + k] = j as u8;
390 let edata = info.get_extradata().unwrap();
391 validate!(edata.len() == 768);
394 let cc = edata[c * 3 + i];
395 self.pal[c * 3 + (2 - i)] = (cc << 2) | (cc >> 4);
400 Err(DecoderError::InvalidData)
403 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
404 let src = pkt.get_buffer();
405 let mut mr = MemoryReader::new_read(&src);
406 let mut br = ByteReader::new(&mut mr);
407 let flags = br.read_u32le()?;
408 let w = self.info.get_properties().get_video_info().unwrap().get_width();
409 let h = self.info.get_properties().get_video_info().unwrap().get_height();
411 let cmethod = flags & 0xF;
412 let is_intra = (flags & 0x40) != 0;
413 let scale_v = (flags & 0x10) != 0;
414 let scale_h = (flags & 0x20) != 0;
416 self.rescale(w, h, scale_v, scale_h);
418 if (cmethod == 0) || (cmethod == 1) {
421 let b = br.read_byte()?;
422 self.pal[c * 3 + (2 - i)] = (b << 2) | (b >> 4);
426 for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; }
428 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
429 frm.set_keyframe(false);
430 frm.set_frame_type(FrameType::Skip);
431 return Ok(Rc::new(RefCell::new(frm)))
432 } else if cmethod == 3 {
433 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
434 frm.set_keyframe(false);
435 frm.set_frame_type(FrameType::Skip);
436 return Ok(Rc::new(RefCell::new(frm)))
437 } else if cmethod == 2 {
438 self.decode_method2(&mut br)?;
439 } else if cmethod == 5 {
440 self.decode_method5(&mut br, (flags >> 8) as usize)?;
441 } else if cmethod == 6 {
442 self.decode_method68(&mut br, (flags >> 8) as usize, false)?;
443 } else if cmethod == 8 {
444 self.decode_method68(&mut br, (flags >> 8) as usize, true)?;
446 return Err(DecoderError::NotImplemented);
449 let bufret = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0);
450 if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
451 let mut bufinfo = bufret.unwrap();
453 self.output_frame(&mut bufinfo, w, h);
455 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
456 frm.set_keyframe(is_intra);
457 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
458 Ok(Rc::new(RefCell::new(frm)))
462 pub fn get_decoder() -> Box<NADecoder> {
463 Box::new(GremlinVideoDecoder::new())
468 use test::dec_video::test_file_decoding;
471 test_file_decoding("gdv", "assets/intro1.gdv", Some(10), true, false, None);