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