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 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 };
22 for x in (0..w).step_by(step) {
23 let delta = delta_tab[br.read(dbits)? as usize];
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;
28 dst[off + x + 1] = dst[off + x];
37 impl NADecoder for TMRTDecoder {
38 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
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));
41 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
44 Err(DecoderError::InvalidData)
47 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
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];
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);
62 let myinfo = NAVideoInfo::new(width, height, false, YUV410_FORMAT);
63 let bufinfo = alloc_video_buffer(myinfo, 2)?;
64 let mut buf = bufinfo.get_vbuf().unwrap();
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);
70 let (w, h) = buf.get_dimensions(plane);
71 let off = buf.get_offset(plane);
72 let stride = buf.get_stride(plane);
73 let data = buf.get_data_mut().unwrap();
74 let dst = data.as_mut_slice();
75 self.decode_plane(&mut br, dst, off, stride, w, h, hscale, dbits, plane > 0)?;
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);
85 pub fn get_decoder() -> Box<dyn NADecoder> {
86 Box::new(TMRTDecoder::new())
91 use nihav_core::codecs::RegisteredDecoders;
92 use nihav_core::demuxers::RegisteredDemuxers;
93 use nihav_core::test::dec_video::*;
94 use crate::codecs::duck_register_all_codecs;
95 use nihav_commonfmt::demuxers::generic_register_all_demuxers;
98 let mut dmx_reg = RegisteredDemuxers::new();
99 generic_register_all_demuxers(&mut dmx_reg);
100 let mut dec_reg = RegisteredDecoders::new();
101 duck_register_all_codecs(&mut dec_reg);
103 test_file_decoding("avi", "assets/Duck/tr20_low.avi", Some(10), true, false, None/*Some("tmrt")*/, &dmx_reg, &dec_reg);