28 impl RV34BitstreamDecoder for RealVideo30BR {
29 fn decode_slice_header(&mut self, br: &mut BitReader, _old_w: usize, _old_h: usize) -> DecoderResult<RV34SliceHeader> {
30 if br.read(3)? != 0 { return Err(DecoderError::InvalidData); }
31 let ft_idx = br.read(2)?;
32 let ftype = match ft_idx {
37 if br.read(1)? != 0 { return Err(DecoderError::InvalidData); }
38 let q = br.read(5)? as u8;
39 let deblock = !br.read_bool()?;
40 let pts = br.read(13)? as u16;
41 let rpr = br.read(self.rpr_bits)? as usize;
42 let (w, h) = if rpr != 0 {
43 validate!(rpr < self.widths.len());
44 (self.widths[rpr], self.heights[rpr])
46 (self.width, self.height)
48 let start = br.read(get_slice_start_offset_bits(w, h))? as usize;
51 Ok(RV34SliceHeader{ ftype: ftype, quant: q, deblock: deblock, pts: pts, width: w, height: h, start: start, end: 0, set_idx: 0 })
53 fn decode_intra_pred(&mut self, br: &mut BitReader, types: &mut [i8], mut pos: usize, tstride: usize, _has_top: bool) -> DecoderResult<()> {
56 let code = br.read_code(UintCodeType::Gamma)? as usize;
59 let new = RV30_ITYPE_MAP[code * 2 + k] as usize;
60 let top = (types[pos + x * 2 + k - tstride] + 1) as usize;
61 let left = (types[pos + x * 2 + k - 1] + 1) as usize;
62 types[pos + x * 2 + k] = RV30_ITYPE[top * 90 + left * 9 + new];
63 validate!(types[pos + x * 2 + k] != 9);
70 fn decode_inter_mb_hdr(&mut self, br: &mut BitReader, ftype: FrameType, _mbtype: MBType) -> DecoderResult<MBInfo> {
71 let mut code = br.read_code(UintCodeType::Gamma)? as usize;
78 let idx = if ftype == FrameType::P { 0 } else { 1 };
79 Ok(MBInfo { mbtype: RV30_MB_TYPES[idx][code], skip_run: 0, dquant: dq })
81 fn predict_b_mv(&self, sstate: &SState, mvi: &MVInfo, mbtype: MBType, mvs: &[MV], _mbinfo: &Vec<RV34MBInfo>) -> (MV, MV) {
82 let mb_x = sstate.mb_x;
83 let mb_y = sstate.mb_y;
87 MBType::MBForward | MBType::MBBackward => {
88 let mv_pred = mvi.pred_mb_mv(mb_x, mb_y, true, sstate.has_top, sstate.has_left, sstate.has_tr, sstate.has_tl);
89 mv_f = mv_pred + mvs[0];
99 fn quant_dc(&self, _is_intra: bool, q: u8) -> u8 { RV30_QUANT_DC[q as usize] }
102 struct RealVideo30Decoder {
104 info: Rc<NACodecInfo>,
108 impl RealVideo30Decoder {
111 bd: RealVideo30BR::new(),
112 info: Rc::new(DUMMY_CODEC_INFO),
113 dec: RV34Decoder::new(true, Box::new(RV30DSP::new())),
118 impl NADecoder for RealVideo30Decoder {
119 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
120 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
121 let fmt = formats::YUV420_FORMAT;
122 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, false, fmt));
123 self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()));
125 let edata = info.get_extradata().unwrap();
126 let src: &[u8] = &edata;
128 if src.len() < 2 { return Err(DecoderError::InvalidData); }
129 let num_rpr = (src[1] & 7) as usize;
130 if src.len() < num_rpr * 2 + 8 { return Err(DecoderError::ShortData); }
131 self.bd.rpr_bits = ((num_rpr >> 1) + 1) as u8;
132 if self.bd.rpr_bits > 3 { self.bd.rpr_bits = 3; }
133 for i in 0..num_rpr+1 {
134 self.bd.widths.push ((src[6 + i * 2] as usize) << 2);
135 self.bd.heights.push((src[7 + i * 2] as usize) << 2);
138 self.bd.width = vinfo.get_width();
139 self.bd.height = vinfo.get_height();
143 Err(DecoderError::InvalidData)
146 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
147 let src = pkt.get_buffer();
149 let (bufinfo, ftype, pts) = self.dec.parse_frame(src.as_slice(), &mut self.bd)?;
151 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
152 frm.set_keyframe(ftype == FrameType::I);
153 frm.set_pts(Some(pts));
154 frm.set_frame_type(ftype);//if ftype == FrameType::B { FrameType::Skip } else { ftype } );
155 Ok(Rc::new(RefCell::new(frm)))
159 pub fn get_decoder() -> Box<NADecoder> {
160 Box::new(RealVideo30Decoder::new())
165 use test::dec_video::test_file_decoding;
168 // test_file_decoding("realmedia", "assets/RV/rv30_chroma_drift.rm", Some(1000), true, false, /*None*/Some("rv30"));
169 test_file_decoding("realmedia", "assets/RV/rv30_weighted_mc.rm", Some(400), true, false, None/*Some("rv30")*/);
170 // test_file_decoding("realmedia", "assets/RV/simpsons-clip.rm", Some(1337)/*Some(6666)*/, true, false, /*None*/Some("rv30"));
175 const RV30_QUANT_DC: [u8; 32] = [
176 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
177 16, 17, 18, 19, 20, 21, 22, 22, 22, 23, 23, 23, 24, 24, 25, 25
180 const RV30_MB_TYPES: [[MBType; 6]; 2] = [
181 [ MBType::MBSkip, MBType::MBP16x16, MBType::MBP8x8, MBType::Invalid, MBType::MBIntra, MBType::MBIntra16 ],
182 [ MBType::MBSkip, MBType::MBDirect, MBType::MBForward, MBType::MBBackward, MBType::MBIntra, MBType::MBIntra16 ],
185 const RV30_ITYPE_MAP: [i8; 9*9*2] = [
186 0, 0, 0, 1, 1, 0, 1, 1, 0, 2, 2, 0, 0, 3, 3, 0, 1, 2,
187 2, 1, 0, 4, 4, 0, 3, 1, 1, 3, 0, 5, 5, 0, 2, 2, 1, 4,
188 4, 1, 0, 6, 3, 2, 1, 5, 2, 3, 5, 1, 6, 0, 0, 7, 4, 2,
189 2, 4, 3, 3, 6, 1, 1, 6, 7, 0, 0, 8, 5, 2, 4, 3, 2, 5,
190 3, 4, 1, 7, 4, 4, 7, 1, 8, 0, 6, 2, 3, 5, 5, 3, 2, 6,
191 1, 8, 2, 7, 7, 2, 8, 1, 5, 4, 4, 5, 3, 6, 6, 3, 8, 2,
192 4, 6, 5, 5, 6, 4, 2, 8, 7, 3, 3, 7, 6, 5, 5, 6, 7, 4,
193 4, 7,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