]> git.nihav.org Git - nihav.git/blame_incremental - nihav-qt/src/codecs/rpza.rs
aac: fix intensity stereo reconstruction for ms_mask_present=0 case
[nihav.git] / nihav-qt / src / codecs / rpza.rs
... / ...
CommitLineData
1use nihav_core::codecs::*;
2use nihav_core::io::byteio::*;
3use nihav_codec_support::codecs::HAMShuffler;
4
5const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton {
6 model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
7 comp_info: [
8 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 10, comp_offs: 0, next_elem: 2 }),
9 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 5, comp_offs: 0, next_elem: 2 }),
10 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 0, comp_offs: 0, next_elem: 2 }),
11 None, None],
12 elem_size: 2, be: false, alpha: false, palette: false };
13
14#[derive(Default)]
15struct RpzaDecoder {
16 info: NACodecInfoRef,
17 hams: HAMShuffler<u16>,
18 width: usize,
19 height: usize,
20}
21
22impl RpzaDecoder {
23 fn new() -> Self {
24 Self::default()
25 }
26 fn put_block(dst: &mut [u16], dstride: usize, block: &[u16; 16]) {
27 for (line, src) in dst.chunks_mut(dstride).take(4).zip(block.chunks(4)) {
28 line[..4].copy_from_slice(src);
29 }
30 }
31}
32
33fn div3(a: u16, b: u16) -> u16 {
34 let r0 = (a >> 10) & 0x1F;
35 let g0 = (a >> 5) & 0x1F;
36 let b0 = a & 0x1F;
37 let r1 = (b >> 10) & 0x1F;
38 let g1 = (b >> 5) & 0x1F;
39 let b1 = b & 0x1F;
40
41 let r = (r0 * 21 + r1 * 11) / 32;
42 let g = (g0 * 21 + g1 * 11) / 32;
43 let b = (b0 * 21 + b1 * 11) / 32;
44
45 (r << 10) | (g << 5) | b
46}
47
48impl NADecoder for RpzaDecoder {
49 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
50 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
51 self.width = (vinfo.get_width() + 3) & !3;
52 self.height = (vinfo.get_height() + 3) & !3;
53 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(self.width, self.height, false, RGB555_FORMAT));
54 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
55
56 Ok(())
57 } else {
58 Err(DecoderError::InvalidData)
59 }
60 }
61 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
62 let src = pkt.get_buffer();
63 validate!(src.len() >= 2);
64 let mut mr = MemoryReader::new_read(src.as_slice());
65 let mut br = ByteReader::new(&mut mr);
66
67 let id = br.read_byte()?;
68 validate!(id == 0xE1);
69 let length = br.read_u24be()? as usize;
70 validate!(length == src.len());
71
72 let bufret = self.hams.clone_ref();
73 let mut buf;
74 if let Some(bbuf) = bufret {
75 buf = bbuf;
76 } else {
77 let bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 2)?;
78 buf = bufinfo.get_vbuf16().unwrap();
79 self.hams.add_frame(buf);
80 buf = self.hams.get_output_frame().unwrap();
81 }
82 let frm = NASimpleVideoFrame::from_video_buf(&mut buf).unwrap();
83 let stride = frm.stride[0];
84 let mut doff = frm.offset[0];
85
86 let mut x = 0;
87 let nblocks = (self.width / 4) * (self.height / 4);
88 let mut blockpos = 0;
89 let mut has_skips = false;
90 let mut block = [0u16; 16];
91 while blockpos < nblocks {
92 let opcode = br.read_byte()?;
93 if (opcode & 0x80) == 0 {
94 let b2 = br.read_byte()?;
95 let clr0 = u16::from(opcode) * 256 + u16::from(b2);
96 if (br.peek_byte()? & 0x80) == 0 {
97 block[0] = clr0;
98 for el in block.iter_mut().skip(1) {
99 *el = br.read_u16be()?;
100 }
101 } else {
102 let clr3 = br.read_u16be()?;
103 let clr1 = div3(clr0, clr3);
104 let clr2 = div3(clr3, clr0);
105 let clr = [clr3, clr2, clr1, clr0];
106 let flags = br.read_u32be()? as usize;
107 for i in 0..16 {
108 block[i] = clr[(flags >> (30 - i * 2)) & 3];
109 }
110 }
111 Self::put_block(&mut frm.data[doff + x..], stride, &block);
112 x += 4;
113 if x == self.width {
114 x = 0;
115 doff += stride * 4;
116 }
117 blockpos += 1;
118 continue;
119 }
120 let len = ((opcode & 0x1F) as usize) + 1;
121 validate!(blockpos + len <= nblocks);
122 match opcode >> 5 {
123 4 => {
124 has_skips = true;
125 for _ in 0..len {
126 x += 4;
127 if x == self.width {
128 x = 0;
129 doff += stride * 4;
130 }
131 }
132 },
133 5 => {
134 let clr = br.read_u16be()?;
135 block = [clr; 16];
136 for _ in 0..len {
137 Self::put_block(&mut frm.data[doff + x..], stride, &block);
138 x += 4;
139 if x == self.width {
140 x = 0;
141 doff += stride * 4;
142 }
143 }
144 },
145 6 => {
146 let clr0 = br.read_u16be()?;
147 let clr3 = br.read_u16be()?;
148 let clr1 = div3(clr0, clr3);
149 let clr2 = div3(clr3, clr0);
150 let clr = [clr3, clr2, clr1, clr0];
151 for _ in 0..len {
152 let flags = br.read_u32be()? as usize;
153 for i in 0..16 {
154 block[i] = clr[(flags >> (30 - i * 2)) & 3];
155 }
156 Self::put_block(&mut frm.data[doff + x..], stride, &block);
157 x += 4;
158 if x == self.width {
159 x = 0;
160 doff += stride * 4;
161 }
162 }
163 },
164 _ => {
165 return Err(DecoderError::InvalidData);
166 },
167 };
168 blockpos += len;
169 }
170
171 let buftype = NABufferType::Video16(buf);
172
173 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), buftype);
174 frm.set_keyframe(!has_skips);
175 frm.set_frame_type(if !has_skips { FrameType::I } else { FrameType::P });
176 Ok(frm.into_ref())
177 }
178 fn flush(&mut self) {
179 self.hams.clear();
180 }
181}
182
183impl NAOptionHandler for RpzaDecoder {
184 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
185 fn set_options(&mut self, _options: &[NAOption]) { }
186 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
187}
188
189pub fn get_decoder() -> Box<dyn NADecoder + Send> {
190 Box::new(RpzaDecoder::new())
191}
192
193#[cfg(test)]
194mod test {
195 use nihav_core::codecs::RegisteredDecoders;
196 use nihav_core::demuxers::RegisteredDemuxers;
197 use nihav_codec_support::test::dec_video::*;
198 use crate::qt_register_all_decoders;
199 use nihav_commonfmt::generic_register_all_demuxers;
200 #[test]
201 fn test_rpza() {
202 let mut dmx_reg = RegisteredDemuxers::new();
203 generic_register_all_demuxers(&mut dmx_reg);
204 let mut dec_reg = RegisteredDecoders::new();
205 qt_register_all_decoders(&mut dec_reg);
206
207 // sample: https://samples.mplayerhq.hu/V-codecs/RPZA/aletrek-rpza.mov
208 test_decoding("mov", "apple-video", "assets/QT/aletrek-rpza.mov", Some(6), &dmx_reg, &dec_reg,
209 ExpectedTestResult::MD5Frames(vec![
210 [0xe7cfc941, 0xa448841b, 0x75afc888, 0x94aa064b],
211 [0xadaaec50, 0xff7319ff, 0xa1f3f64a, 0xc40c2985],
212 [0xdfa5c4b9, 0xdac2d22b, 0x14f9f281, 0x7295eae7],
213 [0x3f3420b9, 0xbe48b885, 0x91b0fb51, 0xd71462ac],
214 [0xcaae1580, 0x16eecce3, 0x2ca0dd4b, 0x7f9c62e3],
215 [0xb51fc759, 0xe1cfc171, 0xda854767, 0x878f6e17],
216 [0x8d3abf6d, 0xeecec7a2, 0x5832e5d6, 0x86145fc9]]));
217 }
218}