1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_codec_support::codecs::HAMShuffler;
6 struct MidividDecoder {
20 fn lz_decompress(src: &[u8], dst: &mut [u8]) -> DecoderResult<()> {
25 let oplo = src[spos] as u16;
27 if spos >= end { return Err(DecoderError::ShortData); }
28 let ophi = src[spos] as u16;
30 let mut op = (ophi << 8) | oplo;
32 if spos >= end { return Ok(()); }
37 validate!(dpos < dst.len());
41 validate!(spos < end);
45 let offset = (((b as usize) & 0xF0) << 4) | (bb as usize);
46 let copy_len = ((b & 0xF) as usize) + 3;
47 validate!(offset <= dpos);
48 validate!(offset > 0);
49 validate!(dpos + copy_len <= dst.len());
50 for _ in 0..copy_len {
51 dst[dpos] = dst[dpos - offset];
61 fn decode_frame(frm: &mut NASimpleVideoFrame<u8>, src: &[u8], width: usize, height: usize) -> DecoderResult<bool> {
62 validate!(src.len() > 8);
63 let num_vec = read_u16le(&src[0..])? as usize;
64 validate!(num_vec <= 512);
65 let is_intra = read_u16le(&src[2..])? == 1;
67 let (vecs, nblocks, idx_start) = if is_intra {
68 (&src[4..], width / 2 * height / 2, num_vec * 12 + 4)
70 let num_blocks = read_u32le(&src[4..])? as usize;
71 let changeset_size = (width >> 5) * (height >> 2);
72 (&src[8+changeset_size..], num_blocks, num_vec * 12 + 8 + changeset_size)
74 validate!(src.len() > idx_start);
76 let src1 = if num_vec > 256 { &src[idx_start + (nblocks + 7)/8..] } else { &src[idx_start..] };
77 let mut mr = MemoryReader::new_read(src1);
78 let mut idx_br = ByteReader::new(&mut mr);
79 let mut mr = MemoryReader::new_read(&src[idx_start..]);
80 let mut idx9_br = ByteReader::new(&mut mr);
83 for y in (0..height).step_by(2) {
84 for x in (0..width).step_by(2) {
87 let flag_b = src[8 + x4/8 + (y/4) * ((width + 31) >> 5)];
88 if ((flag_b >> (x4 & 7)) & 1) == 0 {
92 let idx = if num_vec <= 256 {
93 idx_br.read_byte()? as usize
96 hi9 = idx9_br.read_byte()?;
100 let lo = idx_br.read_byte()? as usize;
102 ((((hi9 >> (7 - bits)) & 1) as usize) << 8) | lo
104 validate!(idx < num_vec);
105 let vec = &vecs[idx * 12..];
108 let dst = &mut frm.data[frm.offset[comp] + x + y * frm.stride[comp]..];
109 dst[0] = vec[0 + comp];
110 dst[1] = vec[3 + comp];
111 dst[frm.stride[comp] + 0] = vec[6 + comp];
112 dst[frm.stride[comp] + 1] = vec[9 + comp];
120 impl NADecoder for MidividDecoder {
121 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
122 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
123 let fmt = NAPixelFormaton::new(ColorModel::YUV(YUVSubmodel::YCbCr),
124 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 0, 1)),
125 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 1, 1)),
126 Some(NAPixelChromaton::new(0, 0, false, 8, 0, 2, 1)),
128 self.width = vinfo.get_width();
129 self.height = vinfo.get_height();
130 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(self.width, self.height, true, fmt));
131 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
132 self.lzbuf = vec![0; self.width * self.height * 3];
136 Err(DecoderError::InvalidData)
139 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
140 let src = pkt.get_buffer();
141 validate!(src.len() > 4);
143 let size = read_u32le(&src[0..])? as usize;
144 validate!(size + 8 == src.len());
146 validate!(src.len() > 12);
147 if read_u32le(&src[8..])? == 0 {
148 lz_decompress(&src[12..], self.lzbuf.as_mut_slice())?;
149 data_ptr = self.lzbuf.as_slice();
151 data_ptr = &src[12..];
155 let bufret = self.hams.clone_ref();
156 if let Some(bbuf) = bufret {
159 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 4)?;
160 buf = bufinfo.get_vbuf().unwrap();
161 self.hams.add_frame(buf);
162 buf = self.hams.get_output_frame().unwrap();
165 let mut frm = NASimpleVideoFrame::from_video_buf(&mut buf).unwrap();
166 let is_intra = decode_frame(&mut frm, data_ptr, self.width, self.height)?;
168 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::Video(buf));
169 frm.set_keyframe(is_intra);
170 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
173 fn flush(&mut self) {
179 pub fn get_decoder_video() -> Box<dyn NADecoder + Send> {
180 Box::new(MidividDecoder::new())
185 use nihav_core::codecs::RegisteredDecoders;
186 use nihav_core::demuxers::RegisteredDemuxers;
187 use nihav_codec_support::test::dec_video::*;
188 use crate::game_register_all_codecs;
189 use nihav_commonfmt::generic_register_all_demuxers;
191 fn test_midivid_video() {
192 let mut dmx_reg = RegisteredDemuxers::new();
193 generic_register_all_demuxers(&mut dmx_reg);
194 let mut dec_reg = RegisteredDecoders::new();
195 game_register_all_codecs(&mut dec_reg);
197 let file = "assets/Game/MVDV.avi";
198 //let file = "assets/Game/bbglogo.avi";
199 //let file = "assets/Game/close.avi";
200 //let file = "assets/Game/inland.avi";
201 //let file = "assets/Game/midway.avi";
202 //let file = "assets/Game/midway.1.avi";
203 //let file = "assets/Game/open.avi";
204 test_file_decoding("avi", file, Some(16), true, false, None, &dmx_reg, &dec_reg);