]> git.nihav.org Git - nihav.git/blame_incremental - nihav-realmedia/src/codecs/rv20.rs
avi: fix handling of palette change chunk with 256 colours
[nihav.git] / nihav-realmedia / src / codecs / rv20.rs
... / ...
CommitLineData
1use nihav_core::io::bitreader::*;
2use nihav_core::io::codebook::*;
3use nihav_core::formats;
4use nihav_core::frame::*;
5use nihav_core::codecs::*;
6use nihav_codec_support::codecs::{MV, ZIGZAG};
7use nihav_codec_support::codecs::blockdsp;
8use nihav_codec_support::codecs::h263::*;
9use nihav_codec_support::codecs::h263::code::{H263_INTERP_FUNCS, H263_INTERP_AVG_FUNCS, h263_filter_row};
10use nihav_codec_support::codecs::h263::decoder::*;
11use nihav_codec_support::codecs::h263::data::*;
12
13
14#[allow(dead_code)]
15struct Tables {
16 intra_mcbpc_cb: Codebook<u8>,
17 inter_mcbpc_cb: Codebook<u8>,
18 mbtype_b_cb: Codebook<u8>,
19 cbpy_cb: Codebook<u8>,
20 cbpc_b_cb: Codebook<u8>,
21 rl_cb: Codebook<H263RLSym>,
22 aic_rl_cb: Codebook<H263RLSym>,
23 mv_cb: Codebook<u8>,
24}
25
26#[derive(Clone,Copy)]
27struct RPRInfo {
28 present: bool,
29 bits: u8,
30 widths: [usize; 8],
31 heights: [usize; 8],
32}
33
34struct RealVideo20Decoder {
35 info: NACodecInfoRef,
36 dec: H263BaseDecoder,
37 tables: Tables,
38 w: usize,
39 h: usize,
40 minor_ver: u8,
41 rpr: RPRInfo,
42 bdsp: Box<dyn BlockDSP + Send>,
43 base_ts: u64,
44 last_ts: u16,
45 next_ts: u16,
46}
47
48struct 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,
58 mb_pos_bits: u8,
59 minor_ver: u8,
60 rpr: RPRInfo,
61 pts: u16,
62}
63
64struct RV20SliceInfo {
65 ftype: Type,
66 seq: u32,
67 qscale: u8,
68 mb_x: usize,
69 mb_y: usize,
70 mb_pos: usize,
71 w: usize,
72 h: usize,
73 loop_filter: bool,
74}
75
76#[derive(Default)]
77struct RV20BlockDSP {}
78
79impl RV20SliceInfo {
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 }
82 }
83}
84
85macro_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
125impl 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 }
135 fn copy_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) {
136 let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize;
137 let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 });
138
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, 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 }
163 }
164 fn avg_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) {
165 let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize;
166 let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 });
167
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, 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 }
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
198fn 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
208impl<'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];
214 let mut br = BitReader::new(offs, BitReaderMode::BE);
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;
224 RealVideo20BR {
225 br: BitReader::new(&src[soff..], BitReaderMode::BE),
226 tables,
227 num_slices: nslices,
228 slice_no: 0,
229 slice_off: slice_offs,
230 w: width,
231 h: height,
232 mb_w,
233 mb_h,
234 mb_pos_bits: get_mb_pos_bits(mb_w, mb_h),
235 minor_ver,
236 rpr,
237 pts: 0,
238 }
239 }
240
241#[allow(unused_variables)]
242 fn decode_block(&mut self, sstate: &SliceState, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize, acpred: ACPredMode) -> DecoderResult<()> {
243 let br = &mut self.br;
244 let mut idx = 0;
245 if !sstate.is_iframe && intra {
246 let mut dc = br.read(8)? as i16;
247 if dc == 255 { dc = 128; }
248 blk[0] = dc << 3;
249 idx = 1;
250 }
251 if !coded { return Ok(()); }
252 let scan = match acpred {
253 ACPredMode::Hor => H263_SCAN_V,
254 ACPredMode::Ver => H263_SCAN_H,
255 _ => &ZIGZAG,
256 };
257
258 let rl_cb = if sstate.is_iframe { &self.tables.aic_rl_cb } else { &self.tables.rl_cb };
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 };
261 while idx < 64 {
262 let code = br.read_cb(rl_cb)?;
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();
270 if br.read_bool()? { level = -level; }
271 if level >= 0 {
272 level = (level * q) + q_add;
273 } else {
274 level = (level * q) - q_add;
275 }
276 } else {
277 last = br.read_bool()?;
278 run = br.read(6)? as u8;
279 level = br.read_s(8)? as i16;
280 if level == -128 {
281 let low = br.read(5)? as i16;
282 let top = br.read_s(6)? as i16;
283 level = (top << 5) | low;
284 }
285 if level >= 0 {
286 level = (level * q) + q_add;
287 } else {
288 level = (level * q) - q_add;
289 }
290 if level < -2048 { level = -2048; }
291 if level > 2047 { level = 2047; }
292 }
293 idx += run;
294 validate!(idx < 64);
295 let oidx = scan[idx as usize];
296 blk[oidx] = level;
297 idx += 1;
298 if last { break; }
299 }
300 Ok(())
301 }
302}
303
304fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> {
305 let code = br.read_cb(mv_cb)? as i16;
306 if code == 0 { return Ok(0) }
307 if !br.read_bool()? {
308 Ok(code)
309 } else {
310 Ok(-code)
311 }
312}
313
314fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> {
315 let xval = decode_mv_component(br, mv_cb)?;
316 let yval = decode_mv_component(br, mv_cb)?;
317 Ok(MV::new(xval, yval))
318}
319
320fn 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
328impl<'a> BlockDecoder for RealVideo20BR<'a> {
329
330#[allow(unused_variables)]
331 fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> {
332 self.slice_no = 0;
333 let shdr = self.read_slice_header()?;
334// self.slice_no += 1;
335 validate!((shdr.mb_x == 0) && (shdr.mb_y == 0));
336/* let mb_count;
337 if self.slice_no < self.num_slices {
338 let pos = self.br.tell();
339 let shdr2 = self.read_slice_header()?;
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;
344 }*/
345
346 let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, shdr.loop_filter, false, false));
347 let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, MVMode::Long, false, false, shdr.qscale, shdr.seq as u16, None, plusinfo);
348 Ok(picinfo)
349 }
350
351 #[allow(unused_variables)]
352 fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<SliceInfo> {
353 let shdr = self.read_slice_header()?;
354 self.slice_no += 1;
355 let mb_count;
356 if self.slice_no < self.num_slices {
357 let pos = self.br.tell();
358 let shdr2 = self.read_slice_header()?;
359 validate!(shdr2.mb_pos > shdr.mb_pos);
360 validate!(shdr2.w == shdr.w && shdr2.h == shdr.h);
361 mb_count = shdr2.mb_pos - shdr.mb_pos;
362 self.br.seek(pos as u32)?;
363 } else {
364 mb_count = self.mb_w * self.mb_h - shdr.mb_pos;
365 }
366 let ret = SliceInfo::new(shdr.mb_x, shdr.mb_y, shdr.mb_pos + mb_count, shdr.qscale);
367
368 Ok(ret)
369 }
370
371 fn decode_block_header(&mut self, info: &PicInfo, _slice: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo> {
372 let br = &mut self.br;
373 let mut q = sstate.quant;
374 match info.get_mode() {
375 Type::I => {
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)?; }
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;
383 if acpp {
384 acpred = if br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver };
385 }
386 }
387 }
388 let cbpy = br.read_cb(&self.tables.cbpy_cb)?;
389 let cbp = (cbpy << 2) | (cbpc & 3);
390 let dquant = (cbpc & 4) != 0;
391 if dquant {
392 q = read_dquant(br, q)?;
393 }
394 let mut binfo = BlockInfo::new(Type::I, cbp, q);
395 binfo.set_acpred(acpred);
396 Ok(binfo)
397 },
398 Type::P => {
399 if br.read_bool()? {
400 return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant()));
401 }
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)?; }
404 let is_intra = (cbpc & 0x04) != 0;
405 let dquant = (cbpc & 0x08) != 0;
406 let is_4x4 = (cbpc & 0x10) != 0;
407 if is_intra {
408 let cbpy = br.read_cb(&self.tables.cbpy_cb)?;
409 let cbp = (cbpy << 2) | (cbpc & 3);
410 if dquant {
411 q = read_dquant(br, q)?;
412 }
413 let binfo = BlockInfo::new(Type::I, cbp, q);
414 return Ok(binfo);
415 }
416
417 let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?;
418// if /* !aiv && */(cbpc & 3) != 3 {
419 cbpy ^= 0xF;
420// }
421 let cbp = (cbpy << 2) | (cbpc & 3);
422 if dquant {
423 q = read_dquant(br, q)?;
424 }
425 let mut binfo = BlockInfo::new(Type::P, cbp, q);
426 if !is_4x4 {
427 let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?];
428 binfo.set_mv(&mvec);
429 } else {
430 let mvec: [MV; 4] = [
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)?
435 ];
436 binfo.set_mv(&mvec);
437 }
438 Ok(binfo)
439 },
440 Type::B => { // B
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 };
456
457 if dquant {
458 q = read_dquant(br, q)?;
459 }
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 },
477 _ => { unreachable!(); },
478 }
479 }
480
481 fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> {
482 self.decode_block(sstate, quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }, info.get_acpred())
483 }
484
485 #[allow(unused_variables)]
486 fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> {
487 self.decode_block(sstate, quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }, ACPredMode::None)
488 }
489
490 fn is_slice_end(&mut self) -> bool { false }
491}
492
493impl<'a> RealVideo20BR<'a> {
494#[allow(unused_variables)]
495 fn read_slice_header(&mut self) -> DecoderResult<RV20SliceInfo> {
496 validate!(self.slice_no < self.num_slices);
497
498 let br = &mut self.br;
499 br.seek(self.slice_off[self.slice_no] * 8)?;
500
501 let frm_type = br.read(2)?;
502 let ftype = match frm_type {
503 0 | 1 => { Type::I },
504 2 => { Type::P },
505 _ => { Type::B },
506 };
507
508 let marker = br.read(1)?;
509 validate!(marker == 0);
510 let qscale = br.read(5)? as u8;
511 validate!(qscale > 0);
512 let loop_filter;
513 if self.minor_ver >= 2 {
514 loop_filter = br.read_bool()?;
515 } else {
516 loop_filter = false;
517 }
518 let seq = if self.minor_ver <= 1 {
519 br.read(8)? << 8
520 } else {
521 br.read(13)? << 3
522 };
523 self.pts = seq as u16;
524 let w;
525 let h;
526 if self.rpr.present {
527 let rpr = br.read(self.rpr.bits)? as usize;
528 if rpr == 0 {
529 w = self.w;
530 h = self.h;
531 } else {
532 w = self.rpr.widths[rpr - 1];
533 h = self.rpr.heights[rpr - 1];
534 validate!((w != 0) && (h != 0));
535 }
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);
539 } else {
540 w = self.w;
541 h = self.h;
542 }
543
544 let mb_pos = br.read(self.mb_pos_bits)? as usize;
545 let mb_x = mb_pos % self.mb_w;
546 let mb_y = mb_pos / self.mb_w;
547
548 br.skip(1)?; // no rounding
549
550 if (self.minor_ver <= 1) && (frm_type == 3) {
551 br.skip(5)?;
552 }
553
554 Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h, loop_filter))
555 }
556}
557
558impl 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();
564 let mut coderead = H263ShortCodeReader::new(H263_MBTYPE_B);
565 let mbtype_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
566 let mut coderead = H263ShortCodeReader::new(H263_CBPY);
567 let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
568 let mut coderead = H263ShortCodeReader::new(H263_CBPC_B);
569 let cbpc_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
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();
576
577 let tables = Tables {
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,
586 };
587
588 RealVideo20Decoder{
589 info: NACodecInfoRef::default(),
590 dec: H263BaseDecoder::new_b_frames(false),
591 tables,
592 w: 0,
593 h: 0,
594 minor_ver: 0,
595 rpr: RPRInfo { present: false, bits: 0, widths: [0; 8], heights: [0; 8] },
596 bdsp: Box::new(RV20BlockDSP::default()),
597 base_ts: 0,
598 last_ts: 0,
599 next_ts: 0,
600 }
601 }
602}
603
604impl NADecoder for RealVideo20Decoder {
605#[allow(unused_variables)]
606 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
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));
612 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
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;
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;
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;
634 }
635 }
636 Ok(())
637 } else {
638 Err(DecoderError::InvalidData)
639 }
640 }
641 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
642 let src = pkt.get_buffer();
643
644 let mut ibr = RealVideo20BR::new(&src, &self.tables, self.w, self.h, self.minor_ver, self.rpr);
645
646 let bufinfo = self.dec.parse_frame(&mut ibr, self.bdsp.as_ref())?;
647
648 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
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);
660 frm.set_keyframe(self.dec.is_intra());
661 frm.set_frame_type(ftype);
662 frm.set_pts(Some(ts >> 3));
663 Ok(frm.into_ref())
664 }
665 fn flush(&mut self) {
666 self.dec.flush();
667 }
668}
669
670impl NAOptionHandler for RealVideo20Decoder {
671 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
672 fn set_options(&mut self, _options: &[NAOption]) { }
673 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
674}
675
676struct MBB { blocks: usize, bits: u8 }
677const H263_MBB: &[MBB; 7] = &[
678 MBB{ blocks: 47, bits: 6 },
679 MBB{ blocks: 98, bits: 7 },
680 MBB{ blocks: 395, bits: 9 },
681 MBB{ blocks: 1583, bits: 11 },
682 MBB{ blocks: 6335, bits: 13 },
683 MBB{ blocks: 9215, bits: 14 },
684 MBB{ blocks: 65536, bits: 14 },
685];
686
687pub fn get_decoder() -> Box<dyn NADecoder + Send> {
688 Box::new(RealVideo20Decoder::new())
689}
690
691#[cfg(test)]
692mod test {
693 use nihav_core::codecs::RegisteredDecoders;
694 use nihav_core::demuxers::RegisteredDemuxers;
695 use nihav_codec_support::test::dec_video::*;
696 use crate::realmedia_register_all_decoders;
697 use crate::realmedia_register_all_demuxers;
698 #[test]
699 fn test_rv20() {
700 let mut dmx_reg = RegisteredDemuxers::new();
701 realmedia_register_all_demuxers(&mut dmx_reg);
702 let mut dec_reg = RegisteredDecoders::new();
703 realmedia_register_all_decoders(&mut dec_reg);
704
705 // sample from a private collection
706 test_decoding("realmedia", "realvideo2",
707 "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm",
708 Some(1000), &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![
709 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
710 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
711 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
712 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
713 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
714 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
715 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
716 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
717 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
718 [0x8ce88686, 0x03ca3bb9, 0x0d18347b, 0xccdb0bc5],
719 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
720 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
721 [0x319d142d, 0x607a7c28, 0x526a2794, 0xa6e7864f],
722 [0xa2008d4c, 0xf4684b3a, 0xecd0526c, 0xf0742a77],
723 [0x5eef6cfd, 0x4eb49f19, 0x4d760b7a, 0x741ccd0c],
724 [0x0e0529df, 0xf1cc3f03, 0x03986b0d, 0xd2033c08],
725 [0xdb9aa091, 0xf2c6345c, 0xde9deae8, 0x71f51a67],
726 [0x22c978cf, 0x6887a9ba, 0xe74c9316, 0x8cbdd29b],
727 [0x7c83c784, 0x15b20881, 0x74798f24, 0x2096573c],
728 [0x84a68aed, 0x4cfcefb1, 0x78d1b66b, 0x21b1860a],
729 [0x78ed094a, 0x8df72434, 0x58bcd64d, 0x8d725dfd]]));
730 }
731}