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::*;
6 use nihav_core::codecs::h263::*;
7 use nihav_core::codecs::h263::code::H263BlockDSP;
8 use nihav_core::codecs::h263::decoder::*;
9 use nihav_core::codecs::h263::data::*;
14 intra_mcbpc_cb: Codebook<u8>,
15 inter_mcbpc_cb: Codebook<u8>,
16 mbtype_b_cb: Codebook<u8>,
17 cbpy_cb: Codebook<u8>,
18 cbpc_b_cb: Codebook<u8>,
19 rl_cb: Codebook<H263RLSym>,
20 aic_rl_cb: Codebook<H263RLSym>,
32 struct RealVideo20Decoder {
43 struct RealVideo20BR<'a> {
58 struct RV20SliceInfo {
70 fn new(ftype: Type, seq: u32, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize) -> Self {
71 RV20SliceInfo { ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h }
75 impl<'a> RealVideo20BR<'a> {
76 fn new(src: &'a [u8], tables: &'a Tables, width: usize, height: usize, minor_ver: u8, rpr: RPRInfo) -> Self {
77 let nslices = (src[0] as usize) + 1;
78 let mut slice_offs = Vec::with_capacity(nslices);
80 let offs = &src[1..][..nslices * 8];
81 let mut br = BitReader::new(offs, offs.len(), BitReaderMode::BE);
84 let off = br.read(32).unwrap();
88 let soff = nslices * 8 + 1;
89 let mb_w = (width + 15) >> 4;
90 let mb_h = (height + 15) >> 4;
91 let max_pos = mb_w * mb_h - 1;
93 for i in 0..H263_MBB.len() {
94 if max_pos <= H263_MBB[i].blocks {
95 mbpb = H263_MBB[i].bits;
100 br: BitReader::new(&src[soff..], src.len() - soff, BitReaderMode::BE),
104 slice_off: slice_offs,
115 #[allow(unused_variables)]
116 fn decode_block(&mut self, sstate: &SliceState, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize, acpred: ACPredMode) -> DecoderResult<()> {
117 let br = &mut self.br;
119 if !sstate.is_iframe && intra {
120 let mut dc = br.read(8)? as i16;
121 if dc == 255 { dc = 128; }
125 if !coded { return Ok(()); }
126 let scan = match acpred {
127 ACPredMode::Hor => H263_SCAN_V,
128 ACPredMode::Ver => H263_SCAN_H,
132 let rl_cb = if sstate.is_iframe { &self.tables.aic_rl_cb } else { &self.tables.rl_cb };
133 let q_add = if quant == 0 || sstate.is_iframe { 0i16 } else { ((quant - 1) | 1) as i16 };
134 let q = if plane_no == 0 { (quant * 2) as i16 } else { H263_CHROMA_QUANT[quant as usize] as i16 };
136 let code = br.read_cb(rl_cb)?;
140 if !code.is_escape() {
141 run = code.get_run();
142 level = code.get_level();
143 last = code.is_last();
144 if br.read_bool()? { level = -level; }
145 level = (level * q) + q_add;
147 last = br.read_bool()?;
148 run = br.read(6)? as u8;
149 level = br.read_s(8)? as i16;
151 let low = br.read(5)? as i16;
152 let top = br.read_s(6)? as i16;
153 level = (top << 5) | low;
155 level = (level * q) + q_add;
156 if level < -2048 { level = -2048; }
157 if level > 2047 { level = 2047; }
161 let oidx = scan[idx as usize];
170 fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> {
171 let code = br.read_cb(mv_cb)? as i16;
172 if code == 0 { return Ok(0) }
173 if !br.read_bool()? {
180 fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> {
181 let xval = decode_mv_component(br, mv_cb)?;
182 let yval = decode_mv_component(br, mv_cb)?;
183 Ok(MV::new(xval, yval))
186 fn read_dquant(br: &mut BitReader, q: u8) -> DecoderResult<u8> {
188 Ok(H263_MODIFIED_QUANT[br.read(1)? as usize][q as usize])
190 Ok(br.read(5)? as u8)
194 impl<'a> BlockDecoder for RealVideo20BR<'a> {
196 #[allow(unused_variables)]
197 fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> {
199 let shdr = self.read_slice_header()?;
200 // self.slice_no += 1;
201 validate!((shdr.mb_x == 0) && (shdr.mb_y == 0));
203 if self.slice_no < self.num_slices {
204 let pos = self.br.tell();
205 let shdr2 = self.read_slice_header()?;
206 self.br.seek(pos as u32)?;
207 mb_count = shdr2.mb_pos - shdr.mb_pos;
209 mb_count = self.mb_w * self.mb_h;
212 let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, false, false, false));
213 let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, MVMode::Long, false, false, shdr.qscale, shdr.seq as u16, None, plusinfo);
217 #[allow(unused_variables)]
218 fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<SliceInfo> {
219 let shdr = self.read_slice_header()?;
222 if self.slice_no < self.num_slices {
223 let pos = self.br.tell();
224 let shdr2 = self.read_slice_header()?;
225 mb_count = shdr2.mb_pos - shdr.mb_pos;
226 self.br.seek(pos as u32)?;
228 mb_count = self.mb_w * self.mb_h - shdr.mb_pos;
230 let ret = SliceInfo::new(shdr.mb_x, shdr.mb_y, shdr.mb_pos + mb_count, shdr.qscale);
235 fn decode_block_header(&mut self, info: &PicInfo, _slice: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo> {
236 let br = &mut self.br;
237 let mut q = sstate.quant;
238 match info.get_mode() {
240 let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?;
241 while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; }
242 let mut acpred = ACPredMode::None;
243 if let Some(ref pi) = info.plusinfo {
245 let acpp = br.read_bool()?;
246 acpred = ACPredMode::DC;
248 acpred = if br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver };
252 let cbpy = br.read_cb(&self.tables.cbpy_cb)?;
253 let cbp = (cbpy << 2) | (cbpc & 3);
254 let dquant = (cbpc & 4) != 0;
256 q = read_dquant(br, q)?;
258 let mut binfo = BlockInfo::new(Type::I, cbp, q);
259 binfo.set_acpred(acpred);
264 return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant()));
266 let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?;
267 while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; }
268 let is_intra = (cbpc & 0x04) != 0;
269 let dquant = (cbpc & 0x08) != 0;
270 let is_4x4 = (cbpc & 0x10) != 0;
272 let cbpy = br.read_cb(&self.tables.cbpy_cb)?;
273 let cbp = (cbpy << 2) | (cbpc & 3);
275 q = read_dquant(br, q)?;
277 let binfo = BlockInfo::new(Type::I, cbp, q);
281 let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?;
282 // if /* !aiv && */(cbpc & 3) != 3 {
285 let cbp = (cbpy << 2) | (cbpc & 3);
287 q = read_dquant(br, q)?;
289 let mut binfo = BlockInfo::new(Type::P, cbp, q);
291 let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?];
294 let mvec: [MV; 4] = [
295 decode_mv(br, &self.tables.mv_cb)?,
296 decode_mv(br, &self.tables.mv_cb)?,
297 decode_mv(br, &self.tables.mv_cb)?,
298 decode_mv(br, &self.tables.mv_cb)?
305 let mut mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize;
306 while mbtype == 14 { mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; }
308 let is_coded = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_CODED) != 0;
309 let is_intra = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_INTRA) != 0;
310 let dquant = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_DQUANT) != 0;
311 let is_fwd = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_FORWARD) != 0;
312 let is_bwd = (H263_MBTYPE_B_CAPS[mbtype] & H263_MBB_CAP_BACKWARD) != 0;
314 let cbp = if is_coded {
315 let cbpc = br.read_cb(&self.tables.cbpc_b_cb)?;
316 let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?;
317 if !is_intra { cbpy ^= 0xF; }
318 (cbpy << 2) | (cbpc & 3)
322 q = read_dquant(br, q)?;
326 let binfo = BlockInfo::new(Type::I, cbp, q);
330 let mut binfo = BlockInfo::new(Type::B, cbp, q);
332 let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?];
336 let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?];
337 binfo.set_b_mv(&mvec);
341 _ => { unreachable!(); },
345 fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> {
346 self.decode_block(sstate, quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }, info.get_acpred())
349 #[allow(unused_variables)]
350 fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> {
351 self.decode_block(sstate, quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }, ACPredMode::None)
354 fn is_slice_end(&mut self) -> bool { false }
357 impl<'a> RealVideo20BR<'a> {
358 #[allow(unused_variables)]
359 fn read_slice_header(&mut self) -> DecoderResult<RV20SliceInfo> {
360 validate!(self.slice_no < self.num_slices);
362 let br = &mut self.br;
363 br.seek(self.slice_off[self.slice_no] * 8)?;
365 let frm_type = br.read(2)?;
366 let ftype = match frm_type {
367 0 | 1 => { Type::I },
372 let marker = br.read(1)?;
373 validate!(marker == 0);
374 let qscale = br.read(5)? as u8;
375 validate!(qscale > 0);
376 if self.minor_ver >= 2 {
377 br.skip(1)?; // loop filter
379 let seq = if self.minor_ver <= 1 {
386 if self.rpr.present {
387 let rpr = br.read(self.rpr.bits)? as usize;
392 w = self.rpr.widths[rpr - 1];
393 h = self.rpr.heights[rpr - 1];
394 validate!((w != 0) && (h != 0));
401 let mb_pos = br.read(self.mb_pos_bits)? as usize;
402 let mb_x = mb_pos % self.mb_w;
403 let mb_y = mb_pos / self.mb_w;
405 br.skip(1)?; // no rounding
407 if (self.minor_ver <= 1) && (frm_type == 3) {
411 Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h))
415 impl RealVideo20Decoder {
417 let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC);
418 let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
419 let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC);
420 let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
421 let mut coderead = H263ShortCodeReader::new(H263_MBTYPE_B);
422 let mbtype_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
423 let mut coderead = H263ShortCodeReader::new(H263_CBPY);
424 let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
425 let mut coderead = H263ShortCodeReader::new(H263_CBPC_B);
426 let cbpc_b_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
427 let mut coderead = H263RLCodeReader::new(H263_RL_CODES);
428 let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
429 let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC);
430 let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
431 let mut coderead = H263ShortCodeReader::new(H263_MV);
432 let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
434 let tables = Tables {
446 info: NACodecInfoRef::default(),
447 dec: H263BaseDecoder::new_b_frames(false),
452 rpr: RPRInfo { present: false, bits: 0, widths: [0; 8], heights: [0; 8] },
453 bdsp: H263BlockDSP::new(),
458 impl NADecoder for RealVideo20Decoder {
459 #[allow(unused_variables)]
460 fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
461 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
462 let w = vinfo.get_width();
463 let h = vinfo.get_height();
464 let fmt = formats::YUV420_FORMAT;
465 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
466 self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
470 let edata = info.get_extradata().unwrap();
471 let src: &[u8] = &edata;
472 let ver = ((src[4] as u32) << 12) | ((src[5] as u32) << 4) | ((src[6] as u32) >> 4);
473 let maj_ver = ver >> 16;
474 let min_ver = (ver >> 8) & 0xFF;
475 let mic_ver = ver & 0xFF;
476 validate!(maj_ver == 2);
477 self.minor_ver = min_ver as u8;
478 let rprb = src[1] & 7;
480 self.rpr.present = false;
482 self.rpr.present = true;
483 self.rpr.bits = ((rprb >> 1) + 1) as u8;
484 for i in 4..(src.len()/2) {
485 self.rpr.widths [i - 4] = (src[i * 2] as usize) * 4;
486 self.rpr.heights[i - 4] = (src[i * 2 + 1] as usize) * 4;
491 Err(DecoderError::InvalidData)
494 fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
495 let src = pkt.get_buffer();
497 let mut ibr = RealVideo20BR::new(&src, &self.tables, self.w, self.h, self.minor_ver, self.rpr);
499 let bufinfo = self.dec.parse_frame(&mut ibr, &self.bdsp)?;
501 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
502 frm.set_keyframe(self.dec.is_intra());
503 frm.set_frame_type(self.dec.get_frame_type());
506 fn flush(&mut self) {
511 struct MBB { blocks: usize, bits: u8 }
512 const H263_MBB: &[MBB; 7] = &[
513 MBB{ blocks: 47, bits: 6 },
514 MBB{ blocks: 98, bits: 7 },
515 MBB{ blocks: 395, bits: 9 },
516 MBB{ blocks: 1583, bits: 11 },
517 MBB{ blocks: 6335, bits: 13 },
518 MBB{ blocks: 9215, bits: 14 },
519 MBB{ blocks: 65536, bits: 14 },
522 pub fn get_decoder() -> Box<dyn NADecoder + Send> {
523 Box::new(RealVideo20Decoder::new())
528 use nihav_core::codecs::RegisteredDecoders;
529 use nihav_core::demuxers::RegisteredDemuxers;
530 use nihav_core::test::dec_video::*;
531 use crate::codecs::realmedia_register_all_codecs;
532 use crate::demuxers::realmedia_register_all_demuxers;
535 let mut dmx_reg = RegisteredDemuxers::new();
536 realmedia_register_all_demuxers(&mut dmx_reg);
537 let mut dec_reg = RegisteredDecoders::new();
538 realmedia_register_all_codecs(&mut dec_reg);
540 test_file_decoding("realmedia", "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", /*None*/Some(3000), true, false, None/*Some("rv20")*/, &dmx_reg, &dec_reg);
541 // test_file_decoding("realmedia", "assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm", /*None*/Some(1000), true, false, Some("rv20"));