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