]>
Commit | Line | Data |
---|---|---|
5641dccf KS |
1 | use nihav_core::io::bitreader::*; |
2 | use nihav_core::io::codebook::*; | |
3 | use nihav_core::formats; | |
4 | use nihav_core::frame::*; | |
5 | use nihav_core::codecs::*; | |
b4d5b851 | 6 | use nihav_codec_support::codecs::{MV, ZIGZAG}; |
5c75af61 | 7 | use nihav_codec_support::codecs::blockdsp; |
b4d5b851 | 8 | use nihav_codec_support::codecs::h263::*; |
5c75af61 | 9 | use nihav_codec_support::codecs::h263::code::{H263_INTERP_FUNCS, H263_INTERP_AVG_FUNCS, h263_filter_row}; |
b4d5b851 KS |
10 | use nihav_codec_support::codecs::h263::decoder::*; |
11 | use nihav_codec_support::codecs::h263::data::*; | |
5641dccf | 12 | |
be91b1c3 KS |
13 | |
14 | #[allow(dead_code)] | |
15 | struct Tables { | |
16 | intra_mcbpc_cb: Codebook<u8>, | |
17 | inter_mcbpc_cb: Codebook<u8>, | |
507f4b11 | 18 | mbtype_b_cb: Codebook<u8>, |
be91b1c3 | 19 | cbpy_cb: Codebook<u8>, |
507f4b11 | 20 | cbpc_b_cb: Codebook<u8>, |
be91b1c3 KS |
21 | rl_cb: Codebook<H263RLSym>, |
22 | aic_rl_cb: Codebook<H263RLSym>, | |
23 | mv_cb: Codebook<u8>, | |
24 | } | |
25 | ||
26 | #[derive(Clone,Copy)] | |
27 | struct RPRInfo { | |
28 | present: bool, | |
29 | bits: u8, | |
30 | widths: [usize; 8], | |
31 | heights: [usize; 8], | |
32 | } | |
33 | ||
34 | struct RealVideo20Decoder { | |
2422d969 | 35 | info: NACodecInfoRef, |
be91b1c3 KS |
36 | dec: H263BaseDecoder, |
37 | tables: Tables, | |
38 | w: usize, | |
39 | h: usize, | |
40 | minor_ver: u8, | |
41 | rpr: RPRInfo, | |
5c75af61 | 42 | bdsp: Box<dyn BlockDSP + Send>, |
5ce3689c KS |
43 | base_ts: u64, |
44 | last_ts: u16, | |
45 | next_ts: u16, | |
be91b1c3 KS |
46 | } |
47 | ||
48 | struct RealVideo20BR<'a> { | |
49 | br: BitReader<'a>, | |
50 | tables: &'a Tables, | |
51 | num_slices: usize, | |
52 | slice_no: usize, | |
53 | slice_off: Vec<u32>, | |
54 | w: usize, | |
55 | h: usize, | |
56 | mb_w: usize, | |
57 | mb_h: usize, | |
be91b1c3 | 58 | mb_pos_bits: u8, |
be91b1c3 KS |
59 | minor_ver: u8, |
60 | rpr: RPRInfo, | |
5ce3689c | 61 | pts: u16, |
be91b1c3 KS |
62 | } |
63 | ||
64 | struct RV20SliceInfo { | |
65 | ftype: Type, | |
32007ad9 | 66 | seq: u32, |
be91b1c3 KS |
67 | qscale: u8, |
68 | mb_x: usize, | |
69 | mb_y: usize, | |
70 | mb_pos: usize, | |
71 | w: usize, | |
72 | h: usize, | |
71b656a2 | 73 | loop_filter: bool, |
be91b1c3 KS |
74 | } |
75 | ||
5c75af61 KS |
76 | #[derive(Default)] |
77 | struct RV20BlockDSP {} | |
78 | ||
be91b1c3 | 79 | impl RV20SliceInfo { |
71b656a2 KS |
80 | fn new(ftype: Type, seq: u32, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize, loop_filter: bool) -> Self { |
81 | RV20SliceInfo { ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h, loop_filter } | |
be91b1c3 KS |
82 | } |
83 | } | |
84 | ||
5c75af61 KS |
85 | macro_rules! idct { |
86 | ($src: expr, $sstep: expr, $dst: expr, $dstep: expr, $bias: expr, $shift: expr, $dtype: tt) => { | |
87 | let s0 = $src[0] as i32; | |
88 | let s1 = $src[$sstep] as i32; | |
89 | let s2 = $src[$sstep * 2] as i32; | |
90 | let s3 = $src[$sstep * 3] as i32; | |
91 | let s4 = $src[$sstep * 4] as i32; | |
92 | let s5 = $src[$sstep * 5] as i32; | |
93 | let s6 = $src[$sstep * 6] as i32; | |
94 | let s7 = $src[$sstep * 7] as i32; | |
95 | ||
96 | let t0 = (s0 + s4).wrapping_mul(1448); | |
97 | let t1 = (s0 - s4).wrapping_mul(1448); | |
98 | let t2 = s2.wrapping_mul(1892) + s6.wrapping_mul(784); | |
99 | let t3 = s2.wrapping_mul(784) - s6.wrapping_mul(1892); | |
100 | let t4 = s1.wrapping_mul(2009) + s3.wrapping_mul(1703) | |
101 | + s5.wrapping_mul(1138) + s7.wrapping_mul(400); | |
102 | let t5 = s1.wrapping_mul(1703) - s3.wrapping_mul(400) | |
103 | - s5.wrapping_mul(2009) - s7.wrapping_mul(1138); | |
104 | let t6 = s1.wrapping_mul(1138) - s3.wrapping_mul(2009) | |
105 | + s5.wrapping_mul(400) + s7.wrapping_mul(1703); | |
106 | let t7 = s1.wrapping_mul(400) - s3.wrapping_mul(1138) | |
107 | + s5.wrapping_mul(1703) - s7.wrapping_mul(2009); | |
108 | ||
109 | let t8 = t0 + t2; | |
110 | let t9 = t0 - t2; | |
111 | let ta = t1 + t3; | |
112 | let tb = t1 - t3; | |
113 | ||
114 | $dst[0] = ((t8 + t4 + $bias) >> $shift) as $dtype; | |
115 | $dst[$dstep] = ((ta + t5 + $bias) >> $shift) as $dtype; | |
116 | $dst[$dstep * 2] = ((tb + t6 + $bias) >> $shift) as $dtype; | |
117 | $dst[$dstep * 3] = ((t9 + t7 + $bias) >> $shift) as $dtype; | |
118 | $dst[$dstep * 4] = ((t9 - t7 + $bias) >> $shift) as $dtype; | |
119 | $dst[$dstep * 5] = ((tb - t6 + $bias) >> $shift) as $dtype; | |
120 | $dst[$dstep * 6] = ((ta - t5 + $bias) >> $shift) as $dtype; | |
121 | $dst[$dstep * 7] = ((t8 - t4 + $bias) >> $shift) as $dtype; | |
122 | } | |
123 | } | |
124 | ||
125 | impl BlockDSP for RV20BlockDSP { | |
126 | fn idct(&self, blk: &mut [i16; 64]) { | |
127 | let mut tmp = [0i32; 64]; | |
128 | for (dst, src) in tmp.chunks_mut(8).zip(blk.chunks(8)) { | |
129 | idct!(src, 1, dst, 1, 0, 4, i32); | |
130 | } | |
131 | for i in 0..8 { | |
132 | idct!(&tmp[i..], 8, &mut blk[i..], 8, 1 << 19, 20, i16); | |
133 | } | |
134 | } | |
401b2b60 | 135 | fn copy_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) { |
5c75af61 | 136 | let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; |
401b2b60 | 137 | let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 }); |
5c75af61 | 138 | |
401b2b60 KS |
139 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); |
140 | ||
141 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_FUNCS); | |
142 | blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS); | |
143 | blockdsp::copy_block(&mut dst, src.clone(), 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS); | |
144 | } | |
145 | fn copy_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]) { | |
146 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); | |
147 | ||
148 | for i in 0..4 { | |
149 | let xadd = (i & 1) * 8; | |
150 | let yadd = (i & 2) * 4; | |
151 | let mode = ((mvs[i].x & 1) + (mvs[i].y & 1) * 2) as usize; | |
152 | ||
153 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mvs[i].x >> 1, mvs[i].y >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS); | |
154 | } | |
155 | ||
156 | let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3]; | |
157 | let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize]; | |
158 | let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize]; | |
159 | let mode = ((cmx & 1) + (cmy & 1) * 2) as usize; | |
160 | for plane in 1..3 { | |
161 | blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS); | |
162 | } | |
5c75af61 | 163 | } |
401b2b60 | 164 | fn avg_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) { |
5c75af61 | 165 | let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize; |
401b2b60 | 166 | let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 }); |
5c75af61 | 167 | |
401b2b60 KS |
168 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); |
169 | ||
170 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_AVG_FUNCS); | |
171 | blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS); | |
172 | blockdsp::copy_block(&mut dst, src.clone(), 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS); | |
173 | } | |
174 | fn avg_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]) { | |
175 | let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap(); | |
176 | ||
177 | for i in 0..4 { | |
178 | let xadd = (i & 1) * 8; | |
179 | let yadd = (i & 2) * 4; | |
180 | let mode = ((mvs[i].x & 1) + (mvs[i].y & 1) * 2) as usize; | |
181 | ||
182 | blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mvs[i].x >> 1, mvs[i].y >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS); | |
183 | } | |
184 | ||
185 | let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3]; | |
186 | let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize]; | |
187 | let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize]; | |
188 | let mode = ((cmx & 1) + (cmy & 1) * 2) as usize; | |
189 | for plane in 1..3 { | |
190 | blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS); | |
191 | } | |
5c75af61 KS |
192 | } |
193 | fn filter_row(&self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) { | |
194 | h263_filter_row(buf, mb_y, mb_w, cbpi) | |
195 | } | |
196 | } | |
197 | ||
b7e1f972 KS |
198 | fn get_mb_pos_bits(mb_w: usize, mb_h: usize) -> u8 { |
199 | let max_pos = mb_w * mb_h - 1; | |
200 | for i in 0..H263_MBB.len() { | |
201 | if max_pos <= H263_MBB[i].blocks { | |
202 | return H263_MBB[i].bits; | |
203 | } | |
204 | } | |
205 | 0 | |
206 | } | |
207 | ||
be91b1c3 KS |
208 | impl<'a> RealVideo20BR<'a> { |
209 | fn new(src: &'a [u8], tables: &'a Tables, width: usize, height: usize, minor_ver: u8, rpr: RPRInfo) -> Self { | |
210 | let nslices = (src[0] as usize) + 1; | |
211 | let mut slice_offs = Vec::with_capacity(nslices); | |
212 | { | |
213 | let offs = &src[1..][..nslices * 8]; | |
fa90ccfb | 214 | let mut br = BitReader::new(offs, BitReaderMode::BE); |
be91b1c3 KS |
215 | for _ in 0..nslices { |
216 | br.skip(32).unwrap(); | |
217 | let off = br.read(32).unwrap(); | |
218 | slice_offs.push(off); | |
219 | } | |
220 | } | |
221 | let soff = nslices * 8 + 1; | |
222 | let mb_w = (width + 15) >> 4; | |
223 | let mb_h = (height + 15) >> 4; | |
be91b1c3 | 224 | RealVideo20BR { |
fa90ccfb | 225 | br: BitReader::new(&src[soff..], BitReaderMode::BE), |
e07387c7 | 226 | tables, |
be91b1c3 KS |
227 | num_slices: nslices, |
228 | slice_no: 0, | |
229 | slice_off: slice_offs, | |
230 | w: width, | |
231 | h: height, | |
e07387c7 KS |
232 | mb_w, |
233 | mb_h, | |
b7e1f972 | 234 | mb_pos_bits: get_mb_pos_bits(mb_w, mb_h), |
e07387c7 KS |
235 | minor_ver, |
236 | rpr, | |
5ce3689c | 237 | pts: 0, |
be91b1c3 KS |
238 | } |
239 | } | |
240 | ||
2a2aa421 | 241 | #[allow(unused_variables)] |
0c3b4923 | 242 | fn decode_block(&mut self, sstate: &SliceState, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize, acpred: ACPredMode) -> DecoderResult<()> { |
9037cf6b | 243 | let br = &mut self.br; |
be91b1c3 | 244 | let mut idx = 0; |
2a2aa421 | 245 | if !sstate.is_iframe && intra { |
fc26b944 | 246 | let mut dc = br.read(8)? as i16; |
be91b1c3 KS |
247 | if dc == 255 { dc = 128; } |
248 | blk[0] = dc << 3; | |
249 | idx = 1; | |
250 | } | |
251 | if !coded { return Ok(()); } | |
0c3b4923 KS |
252 | let scan = match acpred { |
253 | ACPredMode::Hor => H263_SCAN_V, | |
254 | ACPredMode::Ver => H263_SCAN_H, | |
64e8b971 | 255 | _ => &ZIGZAG, |
0c3b4923 | 256 | }; |
be91b1c3 | 257 | |
2a2aa421 | 258 | let rl_cb = if sstate.is_iframe { &self.tables.aic_rl_cb } else { &self.tables.rl_cb }; |
7f73eeee KS |
259 | let q = if plane_no == 0 { (quant * 2) as i16 } else { (H263_CHROMA_QUANT[quant as usize] * 2) as i16 }; |
260 | let q_add = if q == 0 || sstate.is_iframe { 0i16 } else { (((q >> 1) - 1) | 1) as i16 }; | |
be91b1c3 | 261 | while idx < 64 { |
fc26b944 | 262 | let code = br.read_cb(rl_cb)?; |
be91b1c3 KS |
263 | let run; |
264 | let mut level; | |
265 | let last; | |
266 | if !code.is_escape() { | |
267 | run = code.get_run(); | |
268 | level = code.get_level(); | |
269 | last = code.is_last(); | |
fc26b944 | 270 | if br.read_bool()? { level = -level; } |
7f73eeee KS |
271 | if level >= 0 { |
272 | level = (level * q) + q_add; | |
273 | } else { | |
274 | level = (level * q) - q_add; | |
275 | } | |
be91b1c3 | 276 | } else { |
fc26b944 KS |
277 | last = br.read_bool()?; |
278 | run = br.read(6)? as u8; | |
279 | level = br.read_s(8)? as i16; | |
be91b1c3 | 280 | if level == -128 { |
fc26b944 KS |
281 | let low = br.read(5)? as i16; |
282 | let top = br.read_s(6)? as i16; | |
be91b1c3 KS |
283 | level = (top << 5) | low; |
284 | } | |
7f73eeee KS |
285 | if level >= 0 { |
286 | level = (level * q) + q_add; | |
287 | } else { | |
288 | level = (level * q) - q_add; | |
289 | } | |
be91b1c3 KS |
290 | if level < -2048 { level = -2048; } |
291 | if level > 2047 { level = 2047; } | |
292 | } | |
293 | idx += run; | |
294 | validate!(idx < 64); | |
0c3b4923 | 295 | let oidx = scan[idx as usize]; |
be91b1c3 KS |
296 | blk[oidx] = level; |
297 | idx += 1; | |
298 | if last { break; } | |
299 | } | |
300 | Ok(()) | |
301 | } | |
302 | } | |
303 | ||
304 | fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> { | |
fc26b944 | 305 | let code = br.read_cb(mv_cb)? as i16; |
be91b1c3 | 306 | if code == 0 { return Ok(0) } |
fc26b944 | 307 | if !br.read_bool()? { |
be91b1c3 KS |
308 | Ok(code) |
309 | } else { | |
310 | Ok(-code) | |
311 | } | |
312 | } | |
313 | ||
314 | fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> { | |
fc26b944 KS |
315 | let xval = decode_mv_component(br, mv_cb)?; |
316 | let yval = decode_mv_component(br, mv_cb)?; | |
be91b1c3 KS |
317 | Ok(MV::new(xval, yval)) |
318 | } | |
319 | ||
43c8b55e KS |
320 | fn read_dquant(br: &mut BitReader, q: u8) -> DecoderResult<u8> { |
321 | if br.read_bool()? { | |
322 | Ok(H263_MODIFIED_QUANT[br.read(1)? as usize][q as usize]) | |
323 | } else { | |
324 | Ok(br.read(5)? as u8) | |
325 | } | |
326 | } | |
327 | ||
be91b1c3 KS |
328 | impl<'a> BlockDecoder for RealVideo20BR<'a> { |
329 | ||
330 | #[allow(unused_variables)] | |
331 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> { | |
332 | self.slice_no = 0; | |
fc26b944 | 333 | let shdr = self.read_slice_header()?; |
2a2aa421 | 334 | // self.slice_no += 1; |
be91b1c3 | 335 | validate!((shdr.mb_x == 0) && (shdr.mb_y == 0)); |
2a2aa421 | 336 | /* let mb_count; |
be91b1c3 KS |
337 | if self.slice_no < self.num_slices { |
338 | let pos = self.br.tell(); | |
fc26b944 | 339 | let shdr2 = self.read_slice_header()?; |
be91b1c3 KS |
340 | self.br.seek(pos as u32)?; |
341 | mb_count = shdr2.mb_pos - shdr.mb_pos; | |
342 | } else { | |
343 | mb_count = self.mb_w * self.mb_h; | |
2a2aa421 | 344 | }*/ |
be91b1c3 | 345 | |
71b656a2 | 346 | let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, shdr.loop_filter, false, false)); |
32007ad9 | 347 | let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, MVMode::Long, false, false, shdr.qscale, shdr.seq as u16, None, plusinfo); |
be91b1c3 KS |
348 | Ok(picinfo) |
349 | } | |
350 | ||
351 | #[allow(unused_variables)] | |
2a2aa421 | 352 | fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<SliceInfo> { |
fc26b944 | 353 | let shdr = self.read_slice_header()?; |
be91b1c3 KS |
354 | self.slice_no += 1; |
355 | let mb_count; | |
356 | if self.slice_no < self.num_slices { | |
0c3b4923 | 357 | let pos = self.br.tell(); |
fc26b944 | 358 | let shdr2 = self.read_slice_header()?; |
1b6a1780 | 359 | validate!(shdr2.mb_pos > shdr.mb_pos); |
b7e1f972 | 360 | validate!(shdr2.w == shdr.w && shdr2.h == shdr.h); |
be91b1c3 | 361 | mb_count = shdr2.mb_pos - shdr.mb_pos; |
0c3b4923 | 362 | self.br.seek(pos as u32)?; |
be91b1c3 KS |
363 | } else { |
364 | mb_count = self.mb_w * self.mb_h - shdr.mb_pos; | |
365 | } | |
2a2aa421 | 366 | let ret = SliceInfo::new(shdr.mb_x, shdr.mb_y, shdr.mb_pos + mb_count, shdr.qscale); |
be91b1c3 KS |
367 | |
368 | Ok(ret) | |
369 | } | |
370 | ||
43c8b55e | 371 | fn decode_block_header(&mut self, info: &PicInfo, _slice: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo> { |
9037cf6b | 372 | let br = &mut self.br; |
43c8b55e | 373 | let mut q = sstate.quant; |
be91b1c3 KS |
374 | match info.get_mode() { |
375 | Type::I => { | |
fc26b944 KS |
376 | let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; |
377 | while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; } | |
0c3b4923 KS |
378 | let mut acpred = ACPredMode::None; |
379 | if let Some(ref pi) = info.plusinfo { | |
380 | if pi.aic { | |
381 | let acpp = br.read_bool()?; | |
382 | acpred = ACPredMode::DC; | |
0c3b4923 KS |
383 | if acpp { |
384 | acpred = if br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver }; | |
385 | } | |
386 | } | |
0c3b4923 | 387 | } |
fc26b944 | 388 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; |
be91b1c3 KS |
389 | let cbp = (cbpy << 2) | (cbpc & 3); |
390 | let dquant = (cbpc & 4) != 0; | |
391 | if dquant { | |
43c8b55e | 392 | q = read_dquant(br, q)?; |
be91b1c3 | 393 | } |
0c3b4923 KS |
394 | let mut binfo = BlockInfo::new(Type::I, cbp, q); |
395 | binfo.set_acpred(acpred); | |
396 | Ok(binfo) | |
be91b1c3 KS |
397 | }, |
398 | Type::P => { | |
fc26b944 | 399 | if br.read_bool()? { |
be91b1c3 KS |
400 | return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); |
401 | } | |
fc26b944 KS |
402 | let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; |
403 | while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; } | |
be91b1c3 KS |
404 | let is_intra = (cbpc & 0x04) != 0; |
405 | let dquant = (cbpc & 0x08) != 0; | |
406 | let is_4x4 = (cbpc & 0x10) != 0; | |
407 | if is_intra { | |
fc26b944 | 408 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; |
be91b1c3 KS |
409 | let cbp = (cbpy << 2) | (cbpc & 3); |
410 | if dquant { | |
43c8b55e | 411 | q = read_dquant(br, q)?; |
be91b1c3 KS |
412 | } |
413 | let binfo = BlockInfo::new(Type::I, cbp, q); | |
be91b1c3 KS |
414 | return Ok(binfo); |
415 | } | |
416 | ||
fc26b944 | 417 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; |
be91b1c3 KS |
418 | // if /* !aiv && */(cbpc & 3) != 3 { |
419 | cbpy ^= 0xF; | |
420 | // } | |
421 | let cbp = (cbpy << 2) | (cbpc & 3); | |
422 | if dquant { | |
43c8b55e | 423 | q = read_dquant(br, q)?; |
be91b1c3 | 424 | } |
be91b1c3 KS |
425 | let mut binfo = BlockInfo::new(Type::P, cbp, q); |
426 | if !is_4x4 { | |
fc26b944 | 427 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; |
be91b1c3 KS |
428 | binfo.set_mv(&mvec); |
429 | } else { | |
430 | let mvec: [MV; 4] = [ | |
fc26b944 KS |
431 | decode_mv(br, &self.tables.mv_cb)?, |
432 | decode_mv(br, &self.tables.mv_cb)?, | |
433 | decode_mv(br, &self.tables.mv_cb)?, | |
434 | decode_mv(br, &self.tables.mv_cb)? | |
be91b1c3 KS |
435 | ]; |
436 | binfo.set_mv(&mvec); | |
437 | } | |
be91b1c3 KS |
438 | Ok(binfo) |
439 | }, | |
32007ad9 | 440 | Type::B => { // B |
507f4b11 KS |
441 | let mut mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; |
442 | while mbtype == 14 { mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; } | |
443 | ||
444 | let is_coded = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_CODED) != 0; | |
445 | let is_intra = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_INTRA) != 0; | |
446 | let dquant = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_DQUANT) != 0; | |
447 | let is_fwd = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_FORWARD) != 0; | |
448 | let is_bwd = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_BACKWARD) != 0; | |
449 | ||
450 | let cbp = if is_coded { | |
451 | let cbpc = br.read_cb(&self.tables.cbpc_b_cb)?; | |
452 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
453 | if !is_intra { cbpy ^= 0xF; } | |
454 | (cbpy << 2) | (cbpc & 3) | |
455 | } else { 0 }; | |
1a151e53 | 456 | |
507f4b11 | 457 | if dquant { |
43c8b55e | 458 | q = read_dquant(br, q)?; |
507f4b11 | 459 | } |
507f4b11 KS |
460 | |
461 | if is_intra { | |
462 | let binfo = BlockInfo::new(Type::I, cbp, q); | |
463 | return Ok(binfo); | |
464 | } | |
465 | ||
466 | let mut binfo = BlockInfo::new(Type::B, cbp, q); | |
467 | if is_fwd { | |
468 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
469 | binfo.set_mv(&mvec); | |
470 | } | |
471 | if is_bwd { | |
472 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
473 | binfo.set_b_mv(&mvec); | |
474 | } | |
475 | Ok(binfo) | |
476 | }, | |
32007ad9 | 477 | _ => { unreachable!(); }, |
be91b1c3 KS |
478 | } |
479 | } | |
480 | ||
2a2aa421 | 481 | fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { |
0c3b4923 | 482 | self.decode_block(sstate, quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }, info.get_acpred()) |
be91b1c3 KS |
483 | } |
484 | ||
485 | #[allow(unused_variables)] | |
2a2aa421 | 486 | fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { |
0c3b4923 | 487 | self.decode_block(sstate, quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }, ACPredMode::None) |
be91b1c3 KS |
488 | } |
489 | ||
2a2aa421 | 490 | fn is_slice_end(&mut self) -> bool { false } |
be91b1c3 KS |
491 | } |
492 | ||
493 | impl<'a> RealVideo20BR<'a> { | |
2a2aa421 | 494 | #[allow(unused_variables)] |
be91b1c3 KS |
495 | fn read_slice_header(&mut self) -> DecoderResult<RV20SliceInfo> { |
496 | validate!(self.slice_no < self.num_slices); | |
497 | ||
9037cf6b | 498 | let br = &mut self.br; |
fc26b944 | 499 | br.seek(self.slice_off[self.slice_no] * 8)?; |
be91b1c3 | 500 | |
fc26b944 | 501 | let frm_type = br.read(2)?; |
be91b1c3 KS |
502 | let ftype = match frm_type { |
503 | 0 | 1 => { Type::I }, | |
504 | 2 => { Type::P }, | |
507f4b11 | 505 | _ => { Type::B }, |
be91b1c3 KS |
506 | }; |
507 | ||
fc26b944 | 508 | let marker = br.read(1)?; |
be91b1c3 | 509 | validate!(marker == 0); |
fc26b944 | 510 | let qscale = br.read(5)? as u8; |
be91b1c3 | 511 | validate!(qscale > 0); |
71b656a2 | 512 | let loop_filter; |
be91b1c3 | 513 | if self.minor_ver >= 2 { |
71b656a2 KS |
514 | loop_filter = br.read_bool()?; |
515 | } else { | |
516 | loop_filter = false; | |
be91b1c3 KS |
517 | } |
518 | let seq = if self.minor_ver <= 1 { | |
32007ad9 | 519 | br.read(8)? << 8 |
be91b1c3 | 520 | } else { |
32007ad9 | 521 | br.read(13)? << 3 |
be91b1c3 | 522 | }; |
5ce3689c | 523 | self.pts = seq as u16; |
be91b1c3 KS |
524 | let w; |
525 | let h; | |
526 | if self.rpr.present { | |
fc26b944 | 527 | let rpr = br.read(self.rpr.bits)? as usize; |
be91b1c3 KS |
528 | if rpr == 0 { |
529 | w = self.w; | |
530 | h = self.h; | |
531 | } else { | |
7c37f176 KS |
532 | w = self.rpr.widths[rpr - 1]; |
533 | h = self.rpr.heights[rpr - 1]; | |
be91b1c3 KS |
534 | validate!((w != 0) && (h != 0)); |
535 | } | |
b7e1f972 KS |
536 | self.mb_w = (w + 15) >> 4; |
537 | self.mb_h = (h + 15) >> 4; | |
538 | self.mb_pos_bits = get_mb_pos_bits(self.mb_w, self.mb_h); | |
be91b1c3 KS |
539 | } else { |
540 | w = self.w; | |
541 | h = self.h; | |
542 | } | |
543 | ||
fc26b944 | 544 | let mb_pos = br.read(self.mb_pos_bits)? as usize; |
be91b1c3 KS |
545 | let mb_x = mb_pos % self.mb_w; |
546 | let mb_y = mb_pos / self.mb_w; | |
547 | ||
fc26b944 | 548 | br.skip(1)?; // no rounding |
be91b1c3 KS |
549 | |
550 | if (self.minor_ver <= 1) && (frm_type == 3) { | |
fc26b944 | 551 | br.skip(5)?; |
be91b1c3 | 552 | } |
be91b1c3 | 553 | |
71b656a2 | 554 | Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h, loop_filter)) |
be91b1c3 KS |
555 | } |
556 | } | |
557 | ||
be91b1c3 KS |
558 | impl RealVideo20Decoder { |
559 | fn new() -> Self { | |
560 | let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); | |
561 | let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
562 | let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); | |
563 | let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
507f4b11 KS |
564 | let mut coderead = H263ShortCodeReader::new(H263_MBTYPE_B); |
565 | let mbtype_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
be91b1c3 KS |
566 | let mut coderead = H263ShortCodeReader::new(H263_CBPY); |
567 | let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
507f4b11 KS |
568 | let mut coderead = H263ShortCodeReader::new(H263_CBPC_B); |
569 | let cbpc_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
be91b1c3 KS |
570 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES); |
571 | let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
572 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); | |
573 | let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
574 | let mut coderead = H263ShortCodeReader::new(H263_MV); | |
575 | let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
1a151e53 | 576 | |
be91b1c3 | 577 | let tables = Tables { |
e07387c7 KS |
578 | intra_mcbpc_cb, |
579 | inter_mcbpc_cb, | |
580 | mbtype_b_cb, | |
581 | cbpy_cb, | |
582 | cbpc_b_cb, | |
583 | rl_cb, | |
584 | aic_rl_cb, | |
585 | mv_cb, | |
be91b1c3 KS |
586 | }; |
587 | ||
588 | RealVideo20Decoder{ | |
2422d969 | 589 | info: NACodecInfoRef::default(), |
32007ad9 | 590 | dec: H263BaseDecoder::new_b_frames(false), |
e07387c7 | 591 | tables, |
be91b1c3 KS |
592 | w: 0, |
593 | h: 0, | |
594 | minor_ver: 0, | |
595 | rpr: RPRInfo { present: false, bits: 0, widths: [0; 8], heights: [0; 8] }, | |
5c75af61 | 596 | bdsp: Box::new(RV20BlockDSP::default()), |
5ce3689c KS |
597 | base_ts: 0, |
598 | last_ts: 0, | |
599 | next_ts: 0, | |
be91b1c3 KS |
600 | } |
601 | } | |
602 | } | |
603 | ||
604 | impl NADecoder for RealVideo20Decoder { | |
2a2aa421 | 605 | #[allow(unused_variables)] |
01613464 | 606 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
be91b1c3 KS |
607 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { |
608 | let w = vinfo.get_width(); | |
609 | let h = vinfo.get_height(); | |
610 | let fmt = formats::YUV420_FORMAT; | |
611 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); | |
2422d969 | 612 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); |
be91b1c3 KS |
613 | self.w = w; |
614 | self.h = h; | |
615 | ||
616 | let edata = info.get_extradata().unwrap(); | |
617 | let src: &[u8] = &edata; | |
618 | let ver = ((src[4] as u32) << 12) | ((src[5] as u32) << 4) | ((src[6] as u32) >> 4); | |
619 | let maj_ver = ver >> 16; | |
620 | let min_ver = (ver >> 8) & 0xFF; | |
621 | let mic_ver = ver & 0xFF; | |
be91b1c3 KS |
622 | validate!(maj_ver == 2); |
623 | self.minor_ver = min_ver as u8; | |
624 | let rprb = src[1] & 7; | |
625 | if rprb == 0 { | |
626 | self.rpr.present = false; | |
627 | } else { | |
628 | self.rpr.present = true; | |
b7e1f972 KS |
629 | self.rpr.bits = ((rprb >> 1) + 1).min(3) as u8; |
630 | let num_dim = ((src.len() - 8) / 2).min(self.rpr.widths.len() - 1); | |
631 | for i in 0..num_dim { | |
632 | self.rpr.widths [i] = (src[i * 2 + 8] as usize) * 4; | |
633 | self.rpr.heights[i] = (src[i * 2 + 9] as usize) * 4; | |
be91b1c3 KS |
634 | } |
635 | } | |
636 | Ok(()) | |
637 | } else { | |
be91b1c3 KS |
638 | Err(DecoderError::InvalidData) |
639 | } | |
640 | } | |
01613464 | 641 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
be91b1c3 KS |
642 | let src = pkt.get_buffer(); |
643 | ||
be91b1c3 KS |
644 | let mut ibr = RealVideo20BR::new(&src, &self.tables, self.w, self.h, self.minor_ver, self.rpr); |
645 | ||
5c75af61 | 646 | let bufinfo = self.dec.parse_frame(&mut ibr, self.bdsp.as_ref())?; |
be91b1c3 KS |
647 | |
648 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
5ce3689c KS |
649 | let ftype = self.dec.get_frame_type(); |
650 | let pts = ibr.pts; | |
651 | if ftype != FrameType::B { | |
652 | self.last_ts = self.next_ts; | |
653 | self.next_ts = pts; | |
654 | if self.last_ts > self.next_ts { | |
655 | self.base_ts += 1 << 16; | |
656 | } | |
657 | } | |
658 | let ts_diff = self.next_ts.wrapping_sub(pts); | |
659 | let ts = self.base_ts + (self.next_ts as u64) - (ts_diff as u64); | |
be91b1c3 | 660 | frm.set_keyframe(self.dec.is_intra()); |
5ce3689c KS |
661 | frm.set_frame_type(ftype); |
662 | frm.set_pts(Some(ts >> 3)); | |
171860fc | 663 | Ok(frm.into_ref()) |
be91b1c3 | 664 | } |
f9be4e75 KS |
665 | fn flush(&mut self) { |
666 | self.dec.flush(); | |
667 | } | |
be91b1c3 KS |
668 | } |
669 | ||
670 | struct MBB { blocks: usize, bits: u8 } | |
671 | const H263_MBB: &[MBB; 7] = &[ | |
672 | MBB{ blocks: 47, bits: 6 }, | |
673 | MBB{ blocks: 98, bits: 7 }, | |
674 | MBB{ blocks: 395, bits: 9 }, | |
675 | MBB{ blocks: 1583, bits: 11 }, | |
676 | MBB{ blocks: 6335, bits: 13 }, | |
677 | MBB{ blocks: 9215, bits: 14 }, | |
678 | MBB{ blocks: 65536, bits: 14 }, | |
679 | ]; | |
680 | ||
08a1fab7 | 681 | pub fn get_decoder() -> Box<dyn NADecoder + Send> { |
be91b1c3 KS |
682 | Box::new(RealVideo20Decoder::new()) |
683 | } | |
684 | ||
685 | #[cfg(test)] | |
686 | mod test { | |
3167c45c KS |
687 | use nihav_core::codecs::RegisteredDecoders; |
688 | use nihav_core::demuxers::RegisteredDemuxers; | |
ce742854 | 689 | use nihav_codec_support::test::dec_video::*; |
e64739f8 KS |
690 | use crate::realmedia_register_all_codecs; |
691 | use crate::realmedia_register_all_demuxers; | |
be91b1c3 KS |
692 | #[test] |
693 | fn test_rv20() { | |
3167c45c KS |
694 | let mut dmx_reg = RegisteredDemuxers::new(); |
695 | realmedia_register_all_demuxers(&mut dmx_reg); | |
696 | let mut dec_reg = RegisteredDecoders::new(); | |
697 | realmedia_register_all_codecs(&mut dec_reg); | |
698 | ||
39a5835c KS |
699 | test_decoding("realmedia", "realvideo2", |
700 | "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", | |
701 | Some(1000), &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![ | |
702 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
703 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
704 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
705 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
706 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
707 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
708 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
709 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
710 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
711 | [0x8ce88686, 0x03ca3bb9, 0x0d18347b, 0xccdb0bc5], | |
712 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
713 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
714 | [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f], | |
715 | [0xa2008d4c, 0xf4684b3a, 0xecd0526c, 0xf0742a77], | |
71b656a2 | 716 | [0xed26067a, 0x861f385e, 0x3562e478, 0x586d0cb6], |
39a5835c | 717 | [0x0e0529df, 0xf1cc3f03, 0x03986b0d, 0xd2033c08], |
71b656a2 | 718 | [0x9343a151, 0xc7622835, 0x9edb29c2, 0xd96928fa], |
39a5835c | 719 | [0x22c978cf, 0x6887a9ba, 0xe74c9316, 0x8cbdd29b], |
71b656a2 KS |
720 | [0x3ff51b06, 0x1460e151, 0xd8536d37, 0x31121464], |
721 | [0x2912d269, 0x01f00c4b, 0x22f714f8, 0xf81abe8b], | |
722 | [0x875be308, 0x390d5b71, 0xe4108ce3, 0x1f39fed4]])); | |
be91b1c3 KS |
723 | } |
724 | } |