]> git.nihav.org Git - nihav.git/blame - nihav-duck/src/codecs/truemotionrt.rs
switch NAFrame references to Arc
[nihav.git] / nihav-duck / src / codecs / truemotionrt.rs
CommitLineData
1b8522d6
KS
1use nihav_core::codecs::*;
2use nihav_core::io::bitreader::*;
3
4#[derive(Default)]
5struct TMRTDecoder {
2422d969 6 info: NACodecInfoRef,
1b8522d6
KS
7}
8
9const 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
15impl 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
37impl NADecoder for TMRTDecoder {
2422d969 38 fn init(&mut self, 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 }
47 fn decode(&mut self, 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];
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);
63 let bufret = alloc_video_buffer(myinfo, 2);
64 if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
b70cc006 65 let bufinfo = bufret.unwrap();
1b8522d6
KS
66 let mut buf = bufinfo.get_vbuf().unwrap();
67
68 let mut br = BitReader::new(&src[hdr_size..], src.len() - hdr_size, BitReaderMode::LE);
69 let size = br.read(32)? as usize;
70 validate!(size <= src.len() - hdr_size);
71 for plane in 0..3 {
72 let (w, h) = buf.get_dimensions(plane);
73 let off = buf.get_offset(plane);
74 let stride = buf.get_stride(plane);
1a967e6b 75 let data = buf.get_data_mut().unwrap();
1b8522d6
KS
76 let dst = data.as_mut_slice();
77 self.decode_plane(&mut br, dst, off, stride, w, h, hscale, dbits, plane > 0)?;
78 }
79
80 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
81 frm.set_keyframe(true);
82 frm.set_frame_type(FrameType::I);
171860fc 83 Ok(frm.into_ref())
1b8522d6
KS
84 }
85}
86
87pub fn get_decoder() -> Box<NADecoder> {
88 Box::new(TMRTDecoder::new())
89}
90
91#[cfg(test)]
92mod 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
105 test_file_decoding("avi", "assets/Duck/tr20_low.avi", Some(10), true, false, None/*Some("tmrt")*/, &dmx_reg, &dec_reg);
106 }
107}