split nihav-codec-support crate from nihav-core
[nihav.git] / nihav-realmedia / src / codecs / rv30.rs
1 use nihav_core::formats;
2 use nihav_core::io::bitreader::*;
3 use nihav_core::io::intcode::*;
4 use nihav_core::frame::*;
5 use nihav_core::codecs::*;
6 use nihav_codec_support::codecs::{MV, ZERO_MV};
7 use super::rv3040::*;
8 use super::rv30dsp::*;
9
10 struct RealVideo30BR {
11 rpr_bits: u8,
12 width: usize,
13 height: usize,
14 widths: Vec<usize>,
15 heights: Vec<usize>,
16 }
17
18 impl RealVideo30BR {
19 fn new() -> Self {
20 RealVideo30BR {
21 rpr_bits: 0,
22 width: 0,
23 height: 0,
24 widths: Vec::new(),
25 heights: Vec::new(),
26 }
27 }
28 }
29
30 impl RV34BitstreamDecoder for RealVideo30BR {
31 fn decode_slice_header(&mut self, br: &mut BitReader, _old_w: usize, _old_h: usize) -> DecoderResult<RV34SliceHeader> {
32 if br.read(3)? != 0 { return Err(DecoderError::InvalidData); }
33 let ft_idx = br.read(2)?;
34 let ftype = match ft_idx {
35 0|1 => FrameType::I,
36 2 => FrameType::P,
37 _ => FrameType::B,
38 };
39 if br.read(1)? != 0 { return Err(DecoderError::InvalidData); }
40 let q = br.read(5)? as u8;
41 let deblock = !br.read_bool()?;
42 let pts = br.read(13)? as u16;
43 let rpr = br.read(self.rpr_bits)? as usize;
44 let (w, h) = if rpr != 0 {
45 validate!(rpr < self.widths.len());
46 (self.widths[rpr], self.heights[rpr])
47 } else {
48 (self.width, self.height)
49 };
50 let start = br.read(get_slice_start_offset_bits(w, h))? as usize;
51 br.skip(1)?;
52
53 Ok(RV34SliceHeader{ ftype, quant: q, deblock, pts, width: w, height: h, start, end: 0, set_idx: 0 })
54 }
55 fn decode_intra_pred(&mut self, br: &mut BitReader, types: &mut [i8], mut pos: usize, tstride: usize, _has_top: bool) -> DecoderResult<()> {
56 for _ in 0..4 {
57 for x in 0..2 {
58 let code = br.read_code(UintCodeType::Gamma)? as usize;
59 validate!(code < 81);
60 for k in 0..2 {
61 let new = RV30_ITYPE_MAP[code * 2 + k] as usize;
62 let top = (types[pos + x * 2 + k - tstride] + 1) as usize;
63 let left = (types[pos + x * 2 + k - 1] + 1) as usize;
64 types[pos + x * 2 + k] = RV30_ITYPE[top * 90 + left * 9 + new];
65 validate!(types[pos + x * 2 + k] != 9);
66 }
67 }
68 pos += tstride;
69 }
70 Ok(())
71 }
72 fn decode_inter_mb_hdr(&mut self, br: &mut BitReader, ftype: FrameType, _mbtype: MBType) -> DecoderResult<MBInfo> {
73 let mut code = br.read_code(UintCodeType::Gamma)? as usize;
74 let mut dq = false;
75 validate!(code < 11);
76 if code > 5 {
77 code -= 6;
78 dq = true;
79 }
80 let idx = if ftype == FrameType::P { 0 } else { 1 };
81 Ok(MBInfo { mbtype: RV30_MB_TYPES[idx][code], skip_run: 0, dquant: dq })
82 }
83 fn predict_b_mv(&self, sstate: &SState, mvi: &MVInfo, mbtype: MBType, mvs: &[MV], _mbinfo: &[RV34MBInfo]) -> (MV, MV) {
84 let mb_x = sstate.mb_x;
85 let mb_y = sstate.mb_y;
86 let mv_f;
87 let mv_b;
88 match mbtype {
89 MBType::MBForward | MBType::MBBackward => {
90 let mv_pred = mvi.pred_mb_mv(mb_x, mb_y, true, sstate.has_top, sstate.has_left, sstate.has_tr, sstate.has_tl);
91 mv_f = mv_pred + mvs[0];
92 mv_b = mv_f;
93 },
94 _ => {
95 mv_f = ZERO_MV;
96 mv_b = ZERO_MV;
97 },
98 };
99 (mv_f, mv_b)
100 }
101 fn quant_dc(&self, _is_intra: bool, q: u8) -> u8 { RV30_QUANT_DC[q as usize] }
102 }
103
104 struct RealVideo30Decoder {
105 bd: RealVideo30BR,
106 info: NACodecInfoRef,
107 dec: RV34Decoder,
108 }
109
110 impl RealVideo30Decoder {
111 fn new() -> Self {
112 RealVideo30Decoder{
113 bd: RealVideo30BR::new(),
114 info: NACodecInfoRef::default(),
115 dec: RV34Decoder::new(true, Box::new(RV30DSP::new())),
116 }
117 }
118 }
119
120 impl NADecoder for RealVideo30Decoder {
121 fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
122 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
123 let fmt = formats::YUV420_FORMAT;
124 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, fmt));
125 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
126
127 let edata = info.get_extradata().unwrap();
128 let src: &[u8] = &edata;
129
130 if src.len() < 2 { return Err(DecoderError::InvalidData); }
131 let num_rpr = (src[1] & 7) as usize;
132 if src.len() < num_rpr * 2 + 8 { return Err(DecoderError::ShortData); }
133 self.bd.rpr_bits = ((num_rpr >> 1) + 1) as u8;
134 if self.bd.rpr_bits > 3 { self.bd.rpr_bits = 3; }
135 for i in 0..=num_rpr {
136 self.bd.widths.push ((src[6 + i * 2] as usize) << 2);
137 self.bd.heights.push((src[7 + i * 2] as usize) << 2);
138 }
139
140 self.bd.width = vinfo.get_width();
141 self.bd.height = vinfo.get_height();
142
143 supp.pool_u8.set_dec_bufs(3);
144 supp.pool_u8.prealloc_video(NAVideoInfo::new(self.bd.width, self.bd.height, false, fmt), 4)?;
145
146 Ok(())
147 } else {
148 println!("???");
149 Err(DecoderError::InvalidData)
150 }
151 }
152 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
153 let src = pkt.get_buffer();
154
155 let (bufinfo, ftype, pts) = self.dec.parse_frame(supp, src.as_slice(), &mut self.bd)?;
156
157 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
158 frm.set_keyframe(ftype == FrameType::I);
159 frm.set_pts(Some(pts));
160 frm.set_frame_type(ftype);//if ftype == FrameType::B { FrameType::Skip } else { ftype } );
161 Ok(frm.into_ref())
162 }
163 fn flush(&mut self) {
164 self.dec.flush();
165 }
166 }
167
168 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
169 Box::new(RealVideo30Decoder::new())
170 }
171
172 #[cfg(test)]
173 mod test {
174 use nihav_core::codecs::RegisteredDecoders;
175 use nihav_core::demuxers::RegisteredDemuxers;
176 use nihav_core::test::dec_video::*;
177 use crate::realmedia_register_all_codecs;
178 use crate::realmedia_register_all_demuxers;
179 #[test]
180 fn test_rv30() {
181 let mut dmx_reg = RegisteredDemuxers::new();
182 realmedia_register_all_demuxers(&mut dmx_reg);
183 let mut dec_reg = RegisteredDecoders::new();
184 realmedia_register_all_codecs(&mut dec_reg);
185
186 // test_file_decoding("realmedia", "assets/RV/rv30_chroma_drift.rm", Some(1000), true, false, /*None*/Some("rv30"));
187 test_file_decoding("realmedia", "assets/RV/rv30_weighted_mc.rm", Some(400), true, false, None/*Some("rv30")*/, &dmx_reg, &dec_reg);
188 // test_file_decoding("realmedia", "assets/RV/simpsons-clip.rm", Some(1337)/*Some(6666)*/, true, false, /*None*/Some("rv30"));
189 //panic!("end");
190 }
191 }
192
193 const RV30_QUANT_DC: [u8; 32] = [
194 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
195 16, 17, 18, 19, 20, 21, 22, 22, 22, 23, 23, 23, 24, 24, 25, 25
196 ];
197
198 const RV30_MB_TYPES: [[MBType; 6]; 2] = [
199 [ MBType::MBSkip, MBType::MBP16x16, MBType::MBP8x8, MBType::Invalid, MBType::MBIntra, MBType::MBIntra16 ],
200 [ MBType::MBSkip, MBType::MBDirect, MBType::MBForward, MBType::MBBackward, MBType::MBIntra, MBType::MBIntra16 ],
201 ];
202
203 const RV30_ITYPE_MAP: [i8; 9*9*2] = [
204 0, 0, 0, 1, 1, 0, 1, 1, 0, 2, 2, 0, 0, 3, 3, 0, 1, 2,
205 2, 1, 0, 4, 4, 0, 3, 1, 1, 3, 0, 5, 5, 0, 2, 2, 1, 4,
206 4, 1, 0, 6, 3, 2, 1, 5, 2, 3, 5, 1, 6, 0, 0, 7, 4, 2,
207 2, 4, 3, 3, 6, 1, 1, 6, 7, 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