GremlinVideo audio decoder
[nihav.git] / src / codecs / real / rv30.rs
1 use crate::formats;
2 use crate::io::bitreader::*;
3 use crate::io::intcode::*;
4 use crate::codecs::*;
5 use super::rv3040::*;
6 use super::rv30dsp::*;
7
8 struct RealVideo30BR {
9 rpr_bits: u8,
10 width: usize,
11 height: usize,
12 widths: Vec<usize>,
13 heights: Vec<usize>,
14 }
15
16 impl RealVideo30BR {
17 fn new() -> Self {
18 RealVideo30BR {
19 rpr_bits: 0,
20 width: 0,
21 height: 0,
22 widths: Vec::new(),
23 heights: Vec::new(),
24 }
25 }
26 }
27
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 {
33 0|1 => FrameType::I,
34 2 => FrameType::P,
35 _ => FrameType::B,
36 };
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])
45 } else {
46 (self.width, self.height)
47 };
48 let start = br.read(get_slice_start_offset_bits(w, h))? as usize;
49 br.skip(1)?;
50
51 Ok(RV34SliceHeader{ ftype: ftype, quant: q, deblock: deblock, pts: pts, width: w, height: h, start: start, end: 0, set_idx: 0 })
52 }
53 fn decode_intra_pred(&mut self, br: &mut BitReader, types: &mut [i8], mut pos: usize, tstride: usize, _has_top: bool) -> DecoderResult<()> {
54 for _ in 0..4 {
55 for x in 0..2 {
56 let code = br.read_code(UintCodeType::Gamma)? as usize;
57 validate!(code < 81);
58 for k in 0..2 {
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);
64 }
65 }
66 pos += tstride;
67 }
68 Ok(())
69 }
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;
72 let mut dq = false;
73 validate!(code < 11);
74 if code > 5 {
75 code -= 6;
76 dq = true;
77 }
78 let idx = if ftype == FrameType::P { 0 } else { 1 };
79 Ok(MBInfo { mbtype: RV30_MB_TYPES[idx][code], skip_run: 0, dquant: dq })
80 }
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;
84 let mv_f;
85 let mv_b;
86 match mbtype {
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];
90 mv_b = mv_f;
91 },
92 _ => {
93 mv_f = ZERO_MV;
94 mv_b = ZERO_MV;
95 },
96 };
97 (mv_f, mv_b)
98 }
99 fn quant_dc(&self, _is_intra: bool, q: u8) -> u8 { RV30_QUANT_DC[q as usize] }
100 }
101
102 struct RealVideo30Decoder {
103 bd: RealVideo30BR,
104 info: Rc<NACodecInfo>,
105 dec: RV34Decoder,
106 }
107
108 impl RealVideo30Decoder {
109 fn new() -> Self {
110 RealVideo30Decoder{
111 bd: RealVideo30BR::new(),
112 info: Rc::new(DUMMY_CODEC_INFO),
113 dec: RV34Decoder::new(true, Box::new(RV30DSP::new())),
114 }
115 }
116 }
117
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()));
124
125 let edata = info.get_extradata().unwrap();
126 let src: &[u8] = &edata;
127
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);
136 }
137
138 self.bd.width = vinfo.get_width();
139 self.bd.height = vinfo.get_height();
140 Ok(())
141 } else {
142 println!("???");
143 Err(DecoderError::InvalidData)
144 }
145 }
146 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
147 let src = pkt.get_buffer();
148
149 let (bufinfo, ftype, pts) = self.dec.parse_frame(src.as_slice(), &mut self.bd)?;
150
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)))
156 }
157 }
158
159 pub fn get_decoder() -> Box<NADecoder> {
160 Box::new(RealVideo30Decoder::new())
161 }
162
163 #[cfg(test)]
164 mod test {
165 use crate::test::dec_video::test_file_decoding;
166 #[test]
167 fn test_rv30() {
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"));
171 //panic!("end");
172 }
173 }
174
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
178 ];
179
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 ],
183 ];
184
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, 8, 3, 3, 8, 7, 5, 8, 4, 5, 7, 4, 8, 6, 6, 7, 6,
194 5, 8, 8, 5, 6, 7, 8, 6, 7, 7, 6, 8, 8, 7, 7, 8, 8, 8,
195 ];
196
197 const RV30_ITYPE: [i8; 10*10*9] = [
198 0, 9, 9, 9, 9, 9, 9, 9, 9,
199 0, 2, 9, 9,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