]> git.nihav.org Git - nihav.git/blame - nihav-duck/src/codecs/truemotionrt.rs
add IVF demuxer for VP8 test samples
[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() }
47933c6d 17 #[allow(clippy::too_many_arguments)]
1b8522d6
KS
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 };
21 for y in 0..h {
22 let mut diff = 0;
23 for x in (0..w).step_by(step) {
24 let delta = delta_tab[br.read(dbits)? as usize];
25 diff += delta;
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;
28 if hscale {
29 dst[off + x + 1] = dst[off + x];
30 }
31 }
32 off += stride;
33 }
34 Ok(())
35 }
36}
37
38impl NADecoder for TMRTDecoder {
01613464 39 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
1b8522d6
KS
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));
2422d969 42 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
1b8522d6
KS
43 Ok(())
44 } else {
45 Err(DecoderError::InvalidData)
46 }
47 }
01613464 48 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
1b8522d6
KS
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];
56 }
57 let dbits = hdr[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);
62
63 let myinfo = NAVideoInfo::new(width, height, false, YUV410_FORMAT);
8a7352c0 64 let bufinfo = alloc_video_buffer(myinfo, 2)?;
1b8522d6
KS
65 let mut buf = bufinfo.get_vbuf().unwrap();
66
fa90ccfb 67 let mut br = BitReader::new(&src[hdr_size..], BitReaderMode::LE);
1b8522d6
KS
68 let size = br.read(32)? as usize;
69 validate!(size <= src.len() - hdr_size);
70 for plane in 0..3 {
71 let (w, h) = buf.get_dimensions(plane);
72 let off = buf.get_offset(plane);
73 let stride = buf.get_stride(plane);
1a967e6b 74 let data = buf.get_data_mut().unwrap();
1b8522d6
KS
75 let dst = data.as_mut_slice();
76 self.decode_plane(&mut br, dst, off, stride, w, h, hscale, dbits, plane > 0)?;
77 }
78
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);
171860fc 82 Ok(frm.into_ref())
1b8522d6 83 }
f9be4e75
KS
84 fn flush(&mut self) {
85 }
1b8522d6
KS
86}
87
7d57ae2f
KS
88impl 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 }
92}
93
08a1fab7 94pub fn get_decoder() -> Box<dyn NADecoder + Send> {
1b8522d6
KS
95 Box::new(TMRTDecoder::new())
96}
97
98#[cfg(test)]
99mod test {
100 use nihav_core::codecs::RegisteredDecoders;
101 use nihav_core::demuxers::RegisteredDemuxers;
ce742854 102 use nihav_codec_support::test::dec_video::*;
78fb6560 103 use crate::duck_register_all_decoders;
e64739f8 104 use nihav_commonfmt::generic_register_all_demuxers;
1b8522d6
KS
105 #[test]
106 fn test_tmrt() {
107 let mut dmx_reg = RegisteredDemuxers::new();
108 generic_register_all_demuxers(&mut dmx_reg);
109 let mut dec_reg = RegisteredDecoders::new();
78fb6560 110 duck_register_all_decoders(&mut dec_reg);
1b8522d6 111
3cee9950
KS
112 test_decoding("avi", "truemotionrt", "assets/Duck/tr20_low.avi", Some(10),
113 &dmx_reg, &dec_reg,
114 ExpectedTestResult::MD5([0x24c3d26c, 0x1e8bbdc4, 0xfb0fba5d, 0xaa04be81]));
1b8522d6
KS
115 }
116}