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};
10 struct RealVideo30BR {
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 {
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])
48 (self.width, self.height)
50 let start = br.read(get_slice_start_offset_bits(w, h))? as usize;
53 Ok(RV34SliceHeader{ ftype, quant: q, deblock, pts, width: w, height: h, start, end: 0, set_idx: 0 })
55 fn decode_intra_pred(&mut self, br: &mut BitReader, types: &mut [i8], mut pos: usize, tstride: usize, _has_top: bool) -> DecoderResult<()> {
58 let code = br.read_code(UintCodeType::Gamma)? as usize;
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);
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;
80 let idx = if ftype == FrameType::P { 0 } else { 1 };
81 Ok(MBInfo { mbtype: RV30_MB_TYPES[idx][code], skip_run: 0, dquant: dq })
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;
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];
101 fn quant_dc(&self, _is_intra: bool, q: u8) -> u8 { RV30_QUANT_DC[q as usize] }
104 struct RealVideo30Decoder {
106 info: NACodecInfoRef,
110 impl RealVideo30Decoder {
113 bd: RealVideo30BR::new(),
114 info: NACodecInfoRef::default(),
115 dec: RV34Decoder::new(true, Box::new(RV30DSP::new())),
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();
127 let edata = info.get_extradata().unwrap();
128 let src: &[u8] = &edata;
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);
140 self.bd.width = vinfo.get_width();
141 self.bd.height = vinfo.get_height();
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)?;
149 Err(DecoderError::InvalidData)
152 fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
153 let src = pkt.get_buffer();
155 let (bufinfo, ftype, pts) = self.dec.parse_frame(supp, src.as_slice(), &mut self.bd)?;
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 } );
163 fn flush(&mut self) {
168 impl NAOptionHandler for RealVideo30Decoder {
169 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
170 fn set_options(&mut self, _options: &[NAOption]) { }
171 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
174 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
175 Box::new(RealVideo30Decoder::new())
180 use nihav_core::codecs::RegisteredDecoders;
181 use nihav_core::demuxers::RegisteredDemuxers;
182 use nihav_codec_support::test::dec_video::*;
183 use crate::realmedia_register_all_decoders;
184 use crate::realmedia_register_all_demuxers;
187 let mut dmx_reg = RegisteredDemuxers::new();
188 realmedia_register_all_demuxers(&mut dmx_reg);
189 let mut dec_reg = RegisteredDecoders::new();
190 realmedia_register_all_decoders(&mut dec_reg);
192 // sample from a private collection
193 test_decoding("realmedia", "realvideo3", "assets/RV/rv30_weighted_mc.rm", Some(700),
194 &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![
195 [0x2a4d13bf, 0x2f21f3c9, 0xcbd601be, 0x61a6405c],
196 [0x17ea48c7, 0x68334ff5, 0x6fb9729b, 0x9a93ed12],
197 [0xce42a48c, 0x0b5b7f0d, 0x3f66c4a1, 0x261f08e2],
198 [0x91ca8f5b, 0x1f578a93, 0x44e533f2, 0x83beec8a],
199 [0x8cb256a7, 0xb3889afd, 0x28806114, 0x9bbd5287],
200 [0x694570e2, 0x4b2df948, 0xc7d2e36d, 0xa5eb66b2],
201 [0xb9b68059, 0x0d420917, 0x4e0f33d4, 0x8d3a6b0b],
202 [0xb9d6bfa6, 0x04442a8e, 0x6fafc34e, 0xb418a23e],
203 [0xb94e226d, 0xbf8a5fc5, 0x6d9a03c6, 0x4a0d1a50],
204 [0xa2e76d33, 0x1b6996e4, 0xb6a26052, 0x3f5f6145],
205 [0x3b509515, 0x4aa2f4f9, 0x12a0c73b, 0x5b9b20d1],
206 [0x976e0e06, 0xf6194e6f, 0xe0fefc31, 0xf7587bd3],
207 [0x7b38660e, 0xa46f4080, 0xa493f422, 0x36eaaa3b],
208 [0x6375934a, 0xf2a23087, 0x367f9738, 0xf2251e09],
209 [0x54bcefe7, 0xbbc91dc7, 0x0acec7d7, 0x95cf6d02]]));
210 // sample: https://samples.mplayerhq.hu/real/VC-RV30/simpsons-clip.rm
211 test_decoding("realmedia", "realvideo3", "assets/RV/simpsons-clip.rm", Some(1337),
213 ExpectedTestResult::MD5([0x36604117, 0x415f95cc, 0xec38e776, 0x9818d3be]));
217 const RV30_QUANT_DC: [u8; 32] = [
218 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
219 16, 17, 18, 19, 20, 21, 22, 22, 22, 23, 23, 23, 24, 24, 25, 25
222 const RV30_MB_TYPES: [[MBType; 6]; 2] = [
223 [ MBType::MBSkip, MBType::MBP16x16, MBType::MBP8x8, MBType::Invalid, MBType::MBIntra, MBType::MBIntra16 ],
224 [ MBType::MBSkip, MBType::MBDirect, MBType::MBForward, MBType::MBBackward, MBType::MBIntra, MBType::MBIntra16 ],
227 const RV30_ITYPE_MAP: [i8; 9*9*2] = [
228 0, 0, 0, 1, 1, 0, 1, 1, 0, 2, 2, 0, 0, 3, 3, 0, 1, 2,
229 2, 1, 0, 4, 4, 0, 3, 1, 1, 3, 0, 5, 5, 0, 2, 2, 1, 4,
230 4, 1, 0, 6, 3, 2, 1, 5, 2, 3, 5, 1, 6, 0, 0, 7, 4, 2,
231 2, 4, 3, 3, 6, 1, 1, 6, 7, 0, 0, 8, 5, 2, 4, 3, 2, 5,
232 3, 4, 1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