]>
Commit | Line | Data |
---|---|---|
1b8522d6 KS |
1 | use nihav_core::codecs::*; |
2 | use nihav_core::io::bitreader::*; | |
3 | ||
4 | #[derive(Default)] | |
5 | struct TMRTDecoder { | |
2422d969 | 6 | info: NACodecInfoRef, |
1b8522d6 KS |
7 | } |
8 | ||
9 | const TMRT_DELTA_TAB: [&[i16]; 3] = [ | |
10 | &[ 5, -7, 36, -36 ], | |
11 | &[ 2, -3, 8, -8, 18, -18, 36, -36 ], | |
12 | &[ 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144 ] | |
13 | ]; | |
14 | ||
15 | impl TMRTDecoder { | |
16 | fn new() -> Self { Self::default() } | |
17 | fn decode_plane(&self, br: &mut BitReader, dst: &mut [u8], mut off: usize, stride: usize, w: usize, h: usize, hscale: bool, dbits: u8, is_chroma: bool) -> DecoderResult<()> { | |
18 | let delta_tab = TMRT_DELTA_TAB[(dbits - 2) as usize]; | |
19 | let step = if !hscale { 1 } else { 2 }; | |
20 | for y in 0..h { | |
21 | let mut diff = 0; | |
22 | for x in (0..w).step_by(step) { | |
23 | let delta = delta_tab[br.read(dbits)? as usize]; | |
24 | diff += delta; | |
25 | let pred = if y > 0 { dst[off + x - stride].into() } else if !is_chroma { 0 } else { 0x80 }; | |
26 | dst[off + x] = (pred + diff).min(255).max(0) as u8; | |
27 | if hscale { | |
28 | dst[off + x + 1] = dst[off + x]; | |
29 | } | |
30 | } | |
31 | off += stride; | |
32 | } | |
33 | Ok(()) | |
34 | } | |
35 | } | |
36 | ||
37 | impl NADecoder for TMRTDecoder { | |
01613464 | 38 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
1b8522d6 KS |
39 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { |
40 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, YUV410_FORMAT)); | |
2422d969 | 41 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); |
1b8522d6 KS |
42 | Ok(()) |
43 | } else { | |
44 | Err(DecoderError::InvalidData) | |
45 | } | |
46 | } | |
01613464 | 47 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
1b8522d6 KS |
48 | let src = pkt.get_buffer(); |
49 | validate!(src.len() > 10); | |
50 | let hdr_size = (src[0].rotate_left(3) & 0x7F) as usize; | |
51 | validate!(hdr_size >= 10 && hdr_size < src.len() - 4); | |
52 | let mut hdr: [u8; 127] = [0; 127]; | |
53 | for i in 1..hdr_size { | |
54 | hdr[i - 1] = src[i] ^ src[i + 1]; | |
55 | } | |
56 | let dbits = hdr[1]; | |
57 | validate!(dbits >= 2 && dbits <= 4); | |
58 | let hscale = hdr[3] != 0; | |
59 | let width = (hdr[7] as usize) | ((hdr[8] as usize) << 8); | |
60 | let height = (hdr[5] as usize) | ((hdr[6] as usize) << 8); | |
61 | ||
62 | let myinfo = NAVideoInfo::new(width, height, false, YUV410_FORMAT); | |
8a7352c0 | 63 | let bufinfo = alloc_video_buffer(myinfo, 2)?; |
1b8522d6 KS |
64 | let mut buf = bufinfo.get_vbuf().unwrap(); |
65 | ||
66 | let mut br = BitReader::new(&src[hdr_size..], src.len() - hdr_size, BitReaderMode::LE); | |
67 | let size = br.read(32)? as usize; | |
68 | validate!(size <= src.len() - hdr_size); | |
69 | for plane in 0..3 { | |
70 | let (w, h) = buf.get_dimensions(plane); | |
71 | let off = buf.get_offset(plane); | |
72 | let stride = buf.get_stride(plane); | |
1a967e6b | 73 | let data = buf.get_data_mut().unwrap(); |
1b8522d6 KS |
74 | let dst = data.as_mut_slice(); |
75 | self.decode_plane(&mut br, dst, off, stride, w, h, hscale, dbits, plane > 0)?; | |
76 | } | |
77 | ||
78 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
79 | frm.set_keyframe(true); | |
80 | frm.set_frame_type(FrameType::I); | |
171860fc | 81 | Ok(frm.into_ref()) |
1b8522d6 | 82 | } |
f9be4e75 KS |
83 | fn flush(&mut self) { |
84 | } | |
1b8522d6 KS |
85 | } |
86 | ||
08a1fab7 | 87 | pub fn get_decoder() -> Box<dyn NADecoder + Send> { |
1b8522d6 KS |
88 | Box::new(TMRTDecoder::new()) |
89 | } | |
90 | ||
91 | #[cfg(test)] | |
92 | mod test { | |
93 | use nihav_core::codecs::RegisteredDecoders; | |
94 | use nihav_core::demuxers::RegisteredDemuxers; | |
95 | use nihav_core::test::dec_video::*; | |
96 | use crate::codecs::duck_register_all_codecs; | |
97 | use nihav_commonfmt::demuxers::generic_register_all_demuxers; | |
98 | #[test] | |
99 | fn test_tmrt() { | |
100 | let mut dmx_reg = RegisteredDemuxers::new(); | |
101 | generic_register_all_demuxers(&mut dmx_reg); | |
102 | let mut dec_reg = RegisteredDecoders::new(); | |
103 | duck_register_all_codecs(&mut dec_reg); | |
104 | ||
3cee9950 KS |
105 | test_decoding("avi", "truemotionrt", "assets/Duck/tr20_low.avi", Some(10), |
106 | &dmx_reg, &dec_reg, | |
107 | ExpectedTestResult::MD5([0x24c3d26c, 0x1e8bbdc4, 0xfb0fba5d, 0xaa04be81])); | |
1b8522d6 KS |
108 | } |
109 | } |