]> git.nihav.org Git - nihav.git/blob - nihav-qt/src/codecs/smc.rs
allstuff: register multi-threaded decoders as well
[nihav.git] / nihav-qt / src / codecs / smc.rs
1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_codec_support::codecs::HAMShuffler;
4
5 struct SmcDecoder {
6 info: NACodecInfoRef,
7 hams: HAMShuffler<u8>,
8 width: usize,
9 height: usize,
10 pairs: [[u8; 2]; 256],
11 quads: [[u8; 4]; 256],
12 octets: [[u8; 8]; 256],
13 }
14
15 impl SmcDecoder {
16 fn new() -> Self {
17 Self {
18 info: NACodecInfoRef::default(),
19 hams: HAMShuffler::default(),
20 width: 0,
21 height: 0,
22 pairs: [[0; 2]; 256],
23 quads: [[0; 4]; 256],
24 octets: [[0; 8]; 256],
25 }
26 }
27 fn put_block(dst: &mut [u8], dstride: usize, block: &[u8; 16]) {
28 for (line, src) in dst.chunks_mut(dstride).take(4).zip(block.chunks(4)) {
29 (&mut line[..4]).copy_from_slice(src);
30 }
31 }
32 fn put_blocks(&self, dst: &mut [u8], stride: usize, doff: &mut usize, x: &mut usize, len: usize, block: &[u8; 16]) {
33 for _ in 0..len {
34 Self::put_block(&mut dst[*doff + *x..], stride, block);
35 *x += 4;
36 if *x == self.width {
37 *x = 0;
38 *doff += stride * 4;
39 }
40
41 }
42 }
43 }
44
45 impl NADecoder for SmcDecoder {
46 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
47 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
48 self.width = (vinfo.get_width() + 3) & !3;
49 self.height = (vinfo.get_height() + 3) & !3;
50 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(self.width, self.height, false, PAL8_FORMAT));
51 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
52
53 Ok(())
54 } else {
55 Err(DecoderError::InvalidData)
56 }
57 }
58 #[allow(clippy::cognitive_complexity)]
59 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
60 let src = pkt.get_buffer();
61 validate!(src.len() >= 2);
62 let mut mr = MemoryReader::new_read(src.as_slice());
63 let mut br = ByteReader::new(&mut mr);
64
65 let _flags = br.read_byte()?;
66 let length = br.read_u24be()? as usize;
67 validate!(length == src.len());
68
69 let bufret = self.hams.clone_ref();
70 let mut buf;
71 if let Some(bbuf) = bufret {
72 buf = bbuf;
73 } else {
74 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 2)?;
75 buf = bufinfo.get_vbuf().unwrap();
76 self.hams.add_frame(buf);
77 buf = self.hams.get_output_frame().unwrap();
78 }
79 let frm = NASimpleVideoFrame::from_video_buf(&mut buf).unwrap();
80 let stride = frm.stride[0];
81 let mut doff = frm.offset[0];
82
83 self.pairs = [[0; 2]; 256];
84 self.quads = [[0; 4]; 256];
85 self.octets = [[0; 8]; 256];
86
87 let mut x = 0;
88 let nblocks = (self.width / 4) * (self.height / 4);
89 let mut blockpos = 0;
90 let mut pair_idx = 0;
91 let mut quad_idx = 0;
92 let mut oct_idx = 0;
93 let mut has_skips = false;
94 let mut block = [0u8; 16];
95 let mut lblock = [0u8; 16];
96 let mut llblock = [0u8; 16];
97 while blockpos < nblocks {
98 let opcode = br.read_byte()?;
99 let ext_opcode = (opcode & 0x10) != 0;
100 let len = if !ext_opcode || (opcode >= 0x80) {
101 ((opcode & 0xF) as usize) + 1
102 } else {
103 (br.read_byte()? as usize) + 1
104 };
105 validate!(blockpos + len <= nblocks);
106 match opcode >> 5 {
107 0 => {
108 has_skips = true;
109 for _ in 0..len {
110 x += 4;
111 if x == self.width {
112 x = 0;
113 doff += stride * 4;
114 }
115 }
116 },
117 1 => {
118 self.put_blocks(frm.data, stride, &mut doff, &mut x, len, &lblock);
119 llblock = lblock;
120 },
121 2 => {
122 validate!(blockpos + len * 2 <= nblocks);
123 for i in 0..len*2 {
124 if (i & 1) == 0 {
125 Self::put_block(&mut frm.data[doff + x..], stride, &llblock);
126 } else {
127 Self::put_block(&mut frm.data[doff + x..], stride, &lblock);
128 }
129 x += 4;
130 if x == self.width {
131 x = 0;
132 doff += stride * 4;
133 }
134 }
135 blockpos += len;
136 },
137 3 => {
138 let clr = br.read_byte()?;
139 block = [clr; 16];
140 self.put_blocks(frm.data, stride, &mut doff, &mut x, len, &block);
141 if len > 1 {
142 llblock = block;
143 } else {
144 llblock = lblock;
145 }
146 lblock = block;
147 },
148 4 => {
149 let clr;
150 if !ext_opcode {
151 br.read_buf(&mut self.pairs[pair_idx])?;
152 clr = self.pairs[pair_idx];
153 pair_idx = (pair_idx + 1) & 0xFF;
154 } else {
155 let idx = br.read_byte()? as usize;
156 clr = self.pairs[idx];
157 }
158 for _ in 0..len {
159 let flags = br.read_u16be()? as usize;
160 for i in 0..16 {
161 block[i] = clr[(flags >> (15 - i)) & 1];
162 }
163 Self::put_block(&mut frm.data[doff + x..], stride, &block);
164 x += 4;
165 if x == self.width {
166 x = 0;
167 doff += stride * 4;
168 }
169 llblock = lblock;
170 lblock = block;
171 }
172 },
173 5 => {
174 let clr;
175 if !ext_opcode {
176 br.read_buf(&mut self.quads[quad_idx])?;
177 clr = self.quads[quad_idx];
178 quad_idx = (quad_idx + 1) & 0xFF;
179 } else {
180 let idx = br.read_byte()? as usize;
181 clr = self.quads[idx];
182 }
183 for _ in 0..len {
184 let flags = br.read_u32be()? as usize;
185 for i in 0..16 {
186 block[i] = clr[(flags >> (30 - i * 2)) & 3];
187 }
188 Self::put_block(&mut frm.data[doff + x..], stride, &block);
189 x += 4;
190 if x == self.width {
191 x = 0;
192 doff += stride * 4;
193 }
194 llblock = lblock;
195 lblock = block;
196 }
197 },
198 6 => {
199 let clr;
200 if !ext_opcode {
201 br.read_buf(&mut self.octets[oct_idx])?;
202 clr = self.octets[oct_idx];
203 oct_idx = (oct_idx + 1) & 0xFF;
204 } else {
205 let idx = br.read_byte()? as usize;
206 clr = self.octets[idx];
207 }
208 for _ in 0..len {
209 let flg0 = br.read_u16be()? as usize;
210 let flg1 = br.read_u16be()? as usize;
211 let flg2 = br.read_u16be()? as usize;
212
213 let line0 = flg0 >> 4;
214 let line1 = flg1 >> 4;
215 let line2 = flg2 >> 4;
216 let line3 = ((flg0 & 0xF) << 8) | ((flg1 & 0xF) << 4) | (flg2 & 0xF);
217 let flags = [line0, line1, line2, line3];
218 for j in 0..4 {
219 let flg = flags[j];
220 for i in 0..4 {
221 block[i + j * 4] = clr[(flg >> (9 - i * 3)) & 7];
222 }
223 }
224 Self::put_block(&mut frm.data[doff + x..], stride, &block);
225 x += 4;
226 if x == self.width {
227 x = 0;
228 doff += stride * 4;
229 }
230 llblock = lblock;
231 lblock = block;
232 }
233 },
234 _ => {
235 validate!((opcode & 0xF0) != 0xF0);
236 for _ in 0..len {
237 br.read_buf(&mut block)?;
238 Self::put_block(&mut frm.data[doff + x..], stride, &block);
239 x += 4;
240 if x == self.width {
241 x = 0;
242 doff += stride * 4;
243 }
244 llblock = lblock;
245 lblock = block;
246 }
247 },
248 };
249 blockpos += len;
250 }
251
252 let paloff = frm.offset[1];
253 let dpal = &mut frm.data[paloff..];
254 for sd in pkt.side_data.iter() {
255 match *sd {
256 NASideData::Palette(_, ref pal) => {
257 for (dst, src) in dpal.chunks_mut(3).zip(pal.chunks(4)) {
258 dst[0] = src[0];
259 dst[1] = src[1];
260 dst[2] = src[2];
261 }
262 break;
263 },
264 _ => {},
265 };
266 }
267 let buftype = NABufferType::Video(buf);
268
269 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), buftype);
270 frm.set_keyframe(!has_skips);
271 frm.set_frame_type(if !has_skips { FrameType::I } else { FrameType::P });
272 Ok(frm.into_ref())
273 }
274 fn flush(&mut self) {
275 self.hams.clear();
276 }
277 }
278
279 impl NAOptionHandler for SmcDecoder {
280 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
281 fn set_options(&mut self, _options: &[NAOption]) { }
282 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
283 }
284
285 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
286 Box::new(SmcDecoder::new())
287 }
288
289 #[cfg(test)]
290 mod test {
291 use nihav_core::codecs::RegisteredDecoders;
292 use nihav_core::demuxers::RegisteredDemuxers;
293 use nihav_codec_support::test::dec_video::*;
294 use crate::qt_register_all_decoders;
295 use nihav_commonfmt::generic_register_all_demuxers;
296 // samples from https://samples.mplayerhq.hu/V-codecs/SMC
297 #[test]
298 fn test_smc() {
299 let mut dmx_reg = RegisteredDemuxers::new();
300 generic_register_all_demuxers(&mut dmx_reg);
301 let mut dec_reg = RegisteredDecoders::new();
302 qt_register_all_decoders(&mut dec_reg);
303
304 test_decoding("mov", "qt-smc", "assets/QT/aletrek-smc.mov", Some(6), &dmx_reg, &dec_reg,
305 ExpectedTestResult::MD5Frames(vec![
306 [0x4ea351ee, 0x279beedd, 0x71738173, 0x0fba61e8],
307 [0x2c5ea61c, 0x1f539c55, 0x9234aa95, 0xd60be3ef],
308 [0x307955c9, 0x0bc6d4cb, 0x7bebef1e, 0x5f2f3aee],
309 [0x00db3e90, 0x9f57baef, 0x23e6c43b, 0xe9d6dc44],
310 [0x54f2e3eb, 0x6313a0df, 0xb4b52777, 0x10f020f0],
311 [0xdefb0b7d, 0xbdaa77c8, 0xb053d60c, 0xe836b9b0],
312 [0xb820f95b, 0x0ce11d8a, 0xcfd8f623, 0x2b3acb1d]]));
313 }
314 #[test]
315 fn test_smc_gray() {
316 let mut dmx_reg = RegisteredDemuxers::new();
317 generic_register_all_demuxers(&mut dmx_reg);
318 let mut dec_reg = RegisteredDecoders::new();
319 qt_register_all_decoders(&mut dec_reg);
320
321 test_decoding("mov", "qt-smc", "assets/QT/aletrek-smc-gray.mov", Some(6), &dmx_reg, &dec_reg,
322 ExpectedTestResult::MD5Frames(vec![
323 [0x72e87d61, 0x85cde631, 0x780eea35, 0x7dfb8dfb],
324 [0xc41fa1a8, 0x1f038e2e, 0xd67189fd, 0x566e74a8],
325 [0xc2d8e57f, 0x501c5618, 0xdd6cd85a, 0x2c05f0b0],
326 [0x7d530e44, 0xf02646d6, 0xbab960d0, 0x39ea2344],
327 [0x46a2d3dc, 0x93684f3e, 0x88102523, 0x02d19236],
328 [0x78995cd8, 0x3fbcbba2, 0x692d6e19, 0x25334ed0],
329 [0x9632de1a, 0xccbe20a8, 0x4cc5fabe, 0x4dadddbe]]));
330 }
331 }