1 use nihav_core::codecs::*;
2 use nihav_core::io::bitreader::*;
9 const TMRT_DELTA_TAB: [&[i16]; 3] = [
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 ]
16 fn new() -> Self { Self::default() }
17 #[allow(clippy::too_many_arguments)]
18 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<()> {
19 let delta_tab = TMRT_DELTA_TAB[(dbits - 2) as usize];
20 let step = if !hscale { 1 } else { 2 };
23 for x in (0..w).step_by(step) {
24 let delta = delta_tab[br.read(dbits)? as usize];
26 let pred = if y > 0 { dst[off + x - stride].into() } else if !is_chroma { 0 } else { 0x80 };
27 dst[off + x] = (pred + diff).min(255).max(0) as u8;
29 dst[off + x + 1] = dst[off + x];
38 impl NADecoder for TMRTDecoder {
39 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
40 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
41 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, YUV410_FORMAT));
42 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
45 Err(DecoderError::InvalidData)
48 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
49 let src = pkt.get_buffer();
50 validate!(src.len() > 10);
51 let hdr_size = (src[0].rotate_left(3) & 0x7F) as usize;
52 validate!(hdr_size >= 10 && hdr_size < src.len() - 4);
53 let mut hdr: [u8; 127] = [0; 127];
54 for i in 1..hdr_size {
55 hdr[i - 1] = src[i] ^ src[i + 1];
58 validate!(dbits >= 2 && dbits <= 4);
59 let hscale = hdr[3] != 0;
60 let width = (hdr[7] as usize) | ((hdr[8] as usize) << 8);
61 let height = (hdr[5] as usize) | ((hdr[6] as usize) << 8);
63 let myinfo = NAVideoInfo::new(width, height, false, YUV410_FORMAT);
64 let bufinfo = alloc_video_buffer(myinfo, 2)?;
65 let mut buf = bufinfo.get_vbuf().unwrap();
67 let mut br = BitReader::new(&src[hdr_size..], BitReaderMode::LE);
68 let size = br.read(32)? as usize;
69 validate!(size <= src.len() - hdr_size);
71 let (w, h) = buf.get_dimensions(plane);
72 let off = buf.get_offset(plane);
73 let stride = buf.get_stride(plane);
74 let data = buf.get_data_mut().unwrap();
75 let dst = data.as_mut_slice();
76 self.decode_plane(&mut br, dst, off, stride, w, h, hscale, dbits, plane > 0)?;
79 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
80 frm.set_keyframe(true);
81 frm.set_frame_type(FrameType::I);
88 impl NAOptionHandler for TMRTDecoder {
89 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
90 fn set_options(&mut self, _options: &[NAOption]) { }
91 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
94 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
95 Box::new(TMRTDecoder::new())
100 use nihav_core::codecs::RegisteredDecoders;
101 use nihav_core::demuxers::RegisteredDemuxers;
102 use nihav_codec_support::test::dec_video::*;
103 use crate::duck_register_all_decoders;
104 use nihav_commonfmt::generic_register_all_demuxers;
107 let mut dmx_reg = RegisteredDemuxers::new();
108 generic_register_all_demuxers(&mut dmx_reg);
109 let mut dec_reg = RegisteredDecoders::new();
110 duck_register_all_decoders(&mut dec_reg);
112 test_decoding("avi", "truemotionrt", "assets/Duck/tr20_low.avi", Some(10),
114 ExpectedTestResult::MD5([0x24c3d26c, 0x1e8bbdc4, 0xfb0fba5d, 0xaa04be81]));