1 use nihav_core::io::byteio::{ByteReader,MemoryReader};
2 use nihav_core::formats::YUV420_FORMAT;
3 use nihav_core::codecs::*;
4 use nihav_codec_support::codecs::HAMShuffler;
7 #[derive(Clone,Copy,PartialEq)]
15 struct CinepakDecoder {
17 frmmgr: HAMShuffler<u8>,
18 cb_v1: Vec<[[u8; 6]; 256]>,
19 cb_v4: Vec<[[u8; 6]; 256]>,
23 fn put_block(block: &[u8; 24], x: usize, y: usize, frm: &mut NASimpleVideoFrame<u8>) {
24 let mut yoff = frm.offset[0] + x + y * frm.stride[0];
27 frm.data[yoff + j] = block[j + i * 4];
29 yoff += frm.stride[0];
31 let mut uoff = frm.offset[1] + x / 2 + y / 2 * frm.stride[1];
34 frm.data[uoff + j] = block[j + i * 2 + 16];
36 uoff += frm.stride[1];
38 let mut voff = frm.offset[2] + x / 2 + y / 2 * frm.stride[2];
41 frm.data[voff + j] = block[j + i * 2 + 20];
43 voff += frm.stride[2];
47 fn put_block_gray(block: &[u8; 24], x: usize, y: usize, frm: &mut NASimpleVideoFrame<u8>) {
48 let mut yoff = frm.offset[0] + x + y * frm.stride[0];
51 frm.data[yoff + j] = block[j + i * 4];
53 yoff += frm.stride[0];
60 info: NACodecInfo::new_dummy(),
61 frmmgr: HAMShuffler::new(),
62 cb_v1: Vec::with_capacity(1),
63 cb_v4: Vec::with_capacity(1),
64 mode: DecodeMode::Unknown,
67 fn read_cb(br: &mut ByteReader, size: usize, cb: &mut [[u8; 6]; 256], is_yuv: bool) -> DecoderResult<()> {
68 let cb_elem = if is_yuv { 6 } else { 4 };
69 let cb_size = (size - 4) / cb_elem;
70 validate!(size - 4 == cb_size * cb_elem);
71 validate!(cb_size <= 256);
73 br.read_buf(&mut cb[i][..cb_elem])?;
84 fn read_cb_upd(br: &mut ByteReader, size: usize, cb: &mut [[u8; 6]; 256], is_yuv: bool) -> DecoderResult<()> {
85 let cb_elem = if is_yuv { 6 } else { 4 };
86 let end = br.tell() + (size as u64) - 4;
87 for i in (0..256).step_by(32) {
91 let upd = br.read_u32be()?;
93 if ((upd >> (31 - j)) & 1) != 0 {
94 br.read_buf(&mut cb[i + j][..cb_elem])?;
100 cb[i + j][5] ^= 0x80;
105 validate!(br.tell() == end);
108 fn decode_strip(&mut self, src: &[u8], sno: usize, is_intra: bool, is_intra_strip: bool, xoff: usize, yoff: usize, xend: usize, yend: usize, frm: &mut NASimpleVideoFrame<u8>) -> DecoderResult<()> {
109 let mut mr = MemoryReader::new_read(src);
110 let mut br = ByteReader::new(&mut mr);
112 let mut idx_size = 0;
113 let mut v1_only = false;
114 while br.left() > 0 {
115 let id = br.read_byte()?;
116 if (id & 0xF0) == 0x20 && is_intra_strip {
117 validate!((id & 1) == 0);
119 let size = br.read_u24be()? as usize;
120 validate!(size >= 4 && (size - 4 <= (br.left() as usize)));
122 0x20 => Self::read_cb (&mut br, size, &mut self.cb_v4[sno], true)?,
123 0x21 => Self::read_cb_upd(&mut br, size, &mut self.cb_v4[sno], true)?,
124 0x22 => Self::read_cb (&mut br, size, &mut self.cb_v1[sno], true)?,
125 0x23 => Self::read_cb_upd(&mut br, size, &mut self.cb_v1[sno], true)?,
126 0x24 => Self::read_cb (&mut br, size, &mut self.cb_v4[sno], false)?,
127 0x25 => Self::read_cb_upd(&mut br, size, &mut self.cb_v4[sno], false)?,
128 0x26 => Self::read_cb (&mut br, size, &mut self.cb_v1[sno], false)?,
129 0x27 => Self::read_cb_upd(&mut br, size, &mut self.cb_v1[sno], false)?,
130 0x30 => { // intra indices
131 validate!(idx_pos == 0);
132 idx_pos = br.tell() as usize;
134 br.read_skip(idx_size)?;
136 0x31 => { // inter indices
137 validate!(!is_intra);
138 validate!(idx_pos == 0);
139 idx_pos = br.tell() as usize;
141 br.read_skip(idx_size)?;
143 0x32 => { // V1-only blocks
144 validate!(idx_pos == 0);
145 idx_pos = br.tell() as usize;
148 br.read_skip(idx_size)?;
150 _ => return Err(DecoderError::InvalidData),
153 validate!(idx_pos != 0);
154 let mut mr = MemoryReader::new_read(&src[idx_pos..][..idx_size]);
155 let mut br = ByteReader::new(&mut mr);
159 let mut block = [0u8; 24];
160 while br.left() > 0 {
161 let mut flags = if !v1_only { br.read_u32be()? } else { 0x00000000 };
162 let mut mask = 1 << 31;
165 let skip = (flags & mask) == 0;
179 flags = br.read_u32be()?;
183 if (flags & mask) == 0 {
184 let idx = br.read_byte()? as usize;
185 let cb = &self.cb_v1[sno][idx];
186 block[ 0] = cb[0]; block[ 1] = cb[0]; block[ 2] = cb[1]; block[ 3] = cb[1];
187 block[ 4] = cb[0]; block[ 5] = cb[0]; block[ 6] = cb[1]; block[ 7] = cb[1];
188 block[ 8] = cb[2]; block[ 9] = cb[2]; block[10] = cb[3]; block[11] = cb[3];
189 block[12] = cb[2]; block[13] = cb[2]; block[14] = cb[3]; block[15] = cb[3];
190 block[16] = cb[4]; block[17] = cb[4];
191 block[18] = cb[4]; block[19] = cb[4];
192 block[20] = cb[5]; block[21] = cb[5];
193 block[22] = cb[5]; block[23] = cb[5];
195 let idx0 = br.read_byte()? as usize;
196 let cb0 = &self.cb_v4[sno][idx0];
197 let idx1 = br.read_byte()? as usize;
198 let cb1 = &self.cb_v4[sno][idx1];
199 let idx2 = br.read_byte()? as usize;
200 let cb2 = &self.cb_v4[sno][idx2];
201 let idx3 = br.read_byte()? as usize;
202 let cb3 = &self.cb_v4[sno][idx3];
203 block[ 0] = cb0[0]; block[ 1] = cb0[1]; block[ 2] = cb1[0]; block[ 3] = cb1[1];
204 block[ 4] = cb0[2]; block[ 5] = cb0[3]; block[ 6] = cb1[2]; block[ 7] = cb1[3];
205 block[ 8] = cb2[0]; block[ 9] = cb2[1]; block[10] = cb3[0]; block[11] = cb3[1];
206 block[12] = cb2[2]; block[13] = cb2[3]; block[14] = cb3[2]; block[15] = cb3[3];
207 block[16] = cb0[4]; block[17] = cb1[4];
208 block[18] = cb2[4]; block[19] = cb3[4];
209 block[20] = cb0[5]; block[21] = cb1[5];
210 block[22] = cb2[5]; block[23] = cb3[5];
213 if self.mode == DecodeMode::YUV {
214 put_block(&block, x, y, frm);
216 put_block_gray(&block, x, y, frm);
232 impl NADecoder for CinepakDecoder {
233 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
234 if let NACodecTypeInfo::Video(_vinfo) = info.get_properties() {
235 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, YUV420_FORMAT));
236 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, None).into_ref();
240 Err(DecoderError::InvalidData)
243 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
244 let src = pkt.get_buffer();
245 if src.len() <= 10 { return Err(DecoderError::ShortData); }
247 let mut mr = MemoryReader::new_read(src.as_slice());
248 let mut br = ByteReader::new(&mut mr);
250 let flags = br.read_byte()?;
251 let size = br.read_u24be()? as usize;
252 validate!(src.len() >= size);
253 let width = br.read_u16be()? as usize;
254 let height = br.read_u16be()? as usize;
255 let nstrips = br.read_u16be()? as usize;
257 let is_intra = (flags & 1) == 0;
259 let mut mode = DecodeMode::Unknown;
261 let mut stripsize = br.read_u24be()?;
263 while stripsize > 0 {
264 let ctype = br.read_byte()?;
265 let csize = br.read_u24be()?;
267 0x20 | 0x21 | 0x22 | 0x23 => {
268 mode = DecodeMode::YUV;
271 0x24 | 0x25 | 0x26 | 0x27 => {
272 mode = DecodeMode::Gray;
276 br.read_skip(csize as usize)?;
277 validate!(stripsize >= csize);
282 validate!(mode != DecodeMode::Unknown);
283 br.seek(SeekFrom::Start(10))?;
284 for sd in pkt.side_data.iter() {
286 NASideData::Palette(_, _) => {
287 mode = DecodeMode::Palette;
293 self.cb_v1.resize(nstrips, [[0; 6]; 256]);
294 self.cb_v4.resize(nstrips, [[0; 6]; 256]);
296 if let Some(ref vinfo) = self.info.get_properties().get_video_info() {
297 if vinfo.width != width || vinfo.height != height || self.mode != mode {
299 let fmt = match mode {
300 DecodeMode::YUV => YUV420_FORMAT,
301 DecodeMode::Gray => NAPixelFormaton {
302 model: ColorModel::YUV(YUVSubmodel::YUVJ),
304 comp_info: [Some(NAPixelChromaton{h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}), None, None, None, None],
310 DecodeMode::Palette => PAL8_FORMAT,
313 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(width, height, false, fmt));
314 self.info = NACodecInfo::new_ref(self.info.get_name(), myinfo, None).into_ref();
320 let vinfo = self.info.get_properties().get_video_info().unwrap();
321 let bufinfo = alloc_video_buffer(vinfo, 2)?;
322 buf = bufinfo.get_vbuf().unwrap();
325 validate!(self.mode == mode);
326 let bufret = self.frmmgr.clone_ref();
327 if let Some(vbuf) = bufret {
330 return Err(DecoderError::MissingReference);
333 if self.mode == DecodeMode::Palette {
334 let paloff = buf.get_offset(1);
335 let data = buf.get_data_mut().unwrap();
336 let dpal = &mut data[paloff..];
337 for sd in pkt.side_data.iter() {
339 NASideData::Palette(_, ref pal) => {
340 for (dst, src) in dpal.chunks_mut(3).zip(pal.chunks(4)) {
351 let mut frm = NASimpleVideoFrame::from_video_buf(&mut buf).unwrap();
354 for i in 0..nstrips {
355 let flags = br.read_byte()?;
356 validate!(flags == 0x10 || flags == 0x11);
357 let is_intra_strip = (flags & 1) == 0;
358 let size = br.read_u24be()? as usize;
359 validate!(size > 12 && (size - 4) <= (br.left() as usize));
360 let yoff = br.read_u16be()? as usize;
361 let xoff = br.read_u16be()? as usize;
362 if xoff != 0 || yoff != 0 {
363 return Err(DecoderError::NotImplemented);
365 let yend = br.read_u16be()? as usize;
366 let xend = br.read_u16be()? as usize;
367 if i == 0 && is_intra && !is_intra_strip {
368 return Err(DecoderError::InvalidData);
370 let start = br.tell() as usize;
371 let end = start + size - 12;
372 let strip_data = &src[start..end];
373 if is_intra && i > 0 {
374 self.cb_v1[i] = self.cb_v1[i - 1];
375 self.cb_v4[i] = self.cb_v4[i - 1];
377 self.decode_strip(strip_data, i, is_intra, is_intra_strip, 0, last_y, xend, last_y + yend, &mut frm)?;
378 br.read_skip(size - 12)?;
382 self.frmmgr.add_frame(buf.clone());
383 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::Video(buf));
384 frm.set_keyframe(is_intra);
385 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
388 fn flush(&mut self) {
393 impl NAOptionHandler for CinepakDecoder {
394 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
395 fn set_options(&mut self, _options: &[NAOption]) { }
396 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
399 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
400 Box::new(CinepakDecoder::new())
405 use nihav_core::codecs::RegisteredDecoders;
406 use nihav_core::demuxers::RegisteredDemuxers;
407 use nihav_codec_support::test::dec_video::*;
408 use crate::generic_register_all_decoders;
409 use crate::generic_register_all_demuxers;
412 let mut dmx_reg = RegisteredDemuxers::new();
413 generic_register_all_demuxers(&mut dmx_reg);
414 let mut dec_reg = RegisteredDecoders::new();
415 generic_register_all_decoders(&mut dec_reg);
416 // sample: https://samples.mplayerhq.hu/V-codecs/CVID/ot171.avi
417 test_decoding("avi", "cinepak", "assets/Misc/ot171.avi", Some(10), &dmx_reg,
418 &dec_reg, ExpectedTestResult::MD5Frames(vec![
419 [0xd58326b0, 0xdbfc1dcc, 0x6d66a04c, 0x08a21bbb],
420 [0x9b2cb5c5, 0x69b5f261, 0xcaccaaaf, 0xff2a807d],
421 [0x55c322d5, 0xf76f81ce, 0x923ada8c, 0x4925a5c8],
422 [0x2d1a537a, 0x62233cb6, 0xc1d39c2f, 0xeec9ccf3],
423 [0xf3cc841d, 0x56603c01, 0x34f521cf, 0x61f8a0c9],
424 [0xd75c0802, 0x9e786186, 0xc7a05cdf, 0x52ddc59d],
425 [0xde19733b, 0x29633d17, 0x507e9f82, 0x94c09158],
426 [0x1ea11919, 0x133a282c, 0x8cee485c, 0x150cb3f4],
427 [0x55a6d8fb, 0x2ea287c0, 0x36b3083b, 0x954cfc64],
428 [0xfb8be1fb, 0x84ad10aa, 0xa00ee55c, 0x9e191e5b],
429 [0x9c090a08, 0x43071726, 0x26236b5a, 0x79595848]]));
432 fn test_cinepak_gray() {
433 let mut dmx_reg = RegisteredDemuxers::new();
434 generic_register_all_demuxers(&mut dmx_reg);
435 let mut dec_reg = RegisteredDecoders::new();
436 generic_register_all_decoders(&mut dec_reg);
437 // sample: https://samples.mplayerhq.hu/V-codecs/CVID/grayscale/dday.mov
438 test_decoding("mov", "cinepak", "assets/Misc/dday.mov", Some(10), &dmx_reg,
439 &dec_reg, ExpectedTestResult::MD5Frames(vec![
440 [0x2ab229bc, 0xb71308aa, 0x979511c6, 0xcef3ea92],
441 [0x94f227d5, 0xbaa646ef, 0xab78f751, 0x8e1f50da],
442 [0x555de93a, 0x625e77f0, 0x95611bae, 0xbd715e9d],
443 [0xb31b9ba7, 0xba6327f8, 0x5698954f, 0xc16fad2a],
444 [0xda86ffb6, 0x58deb79d, 0x59f62c5b, 0x1bd2a2c5],
445 [0x2f46c7eb, 0x8950ac76, 0xbc68c470, 0x12e3247a],
446 [0x77d73950, 0xf76b28b0, 0x3552bb52, 0x38900a51],
447 [0xf4f45bef, 0x91146af2, 0xdcf4d44e, 0x713bf36e],
448 [0x8e06d350, 0x787f245e, 0x32426903, 0xf35f7dd3],
449 [0x0e35ebc1, 0xfdb6c520, 0x2bf484dc, 0xcec78b63],
450 [0xb8411fa4, 0x3a35f646, 0x85e8e04a, 0xfff58785]]));
453 fn test_cinepak_pal() {
454 let mut dmx_reg = RegisteredDemuxers::new();
455 generic_register_all_demuxers(&mut dmx_reg);
456 let mut dec_reg = RegisteredDecoders::new();
457 generic_register_all_decoders(&mut dec_reg);
458 // sample: https://samples.mplayerhq.hu/V-codecs/CVID/palette/catfight%20Tag%20team%20DT.mov
459 test_decoding("mov", "cinepak", "assets/Misc/catfight Tag team DT.mov", Some(10), &dmx_reg,
460 &dec_reg, ExpectedTestResult::MD5Frames(vec![
461 [0x3f7ec8ea, 0x873a2bc6, 0xcc58336e, 0xe88c4ffd],
462 [0x9665feab, 0xc035fb92, 0x5e4b8718, 0xd1c68877],
463 [0x804f8838, 0x7f4b126e, 0x9efab284, 0xee62d451],
464 [0xbb1930dd, 0x62d4a5d1, 0xca34d891, 0x31236269],
465 [0xc23ec739, 0xbe683ffd, 0xecbc337b, 0x73a96b63],
466 [0xa2fa75f2, 0x1dd937a8, 0x44e2074e, 0x1ac24467],
467 [0x9ba0f1e5, 0xadbe5357, 0x4cfa785b, 0x16181d41],
468 [0xe126c340, 0x6ceaac41, 0x64992bff, 0x8d4bc3c4],
469 [0xba6b2510, 0xc40c2b85, 0x1c7d0199, 0x333d4860],
470 [0x293fe1c2, 0x9f358a7e, 0x4fef6450, 0x8477a4ff],
471 [0x4509095a, 0x65575fdd, 0x3a17ecc4, 0x37821bf9]]));