From 32007ad9122cd9be6018dfb66628520d6cf322c8 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sat, 2 Dec 2017 16:13:15 +0100 Subject: [PATCH] h263: improve B-frame decoding; improve RV2 decoding too --- src/codecs/h263/decoder.rs | 140 ++++++++++++++++++++++++++---------- src/codecs/h263/intel263.rs | 5 +- src/codecs/h263/mod.rs | 63 ++++++++++++---- src/codecs/h263/rv10.rs | 2 +- src/codecs/h263/rv20.rs | 36 ++++------ src/codecs/mod.rs | 35 +++++++++ 6 files changed, 206 insertions(+), 75 deletions(-) diff --git a/src/codecs/h263/decoder.rs b/src/codecs/h263/decoder.rs index 13e289b..7d48199 100644 --- a/src/codecs/h263/decoder.rs +++ b/src/codecs/h263/decoder.rs @@ -1,4 +1,4 @@ -use std::mem; +//use std::mem; use frame::*; use super::super::*; use super::super::blockdsp; @@ -22,7 +22,6 @@ impl MVInfo { self.mb_start = mb_start; self.mb_w = mb_w; self.mb_stride = mb_w * 2; - self.top = true; self.mv.resize(self.mb_stride * 3, ZERO_MV); self.mvmode = mvmode; } @@ -34,7 +33,7 @@ impl MVInfo { } } #[allow(non_snake_case)] - fn predict(&mut self, mb_x: usize, blk_no: usize, use4: bool, diff: MV) -> MV { + fn predict(&mut self, mb_x: usize, blk_no: usize, use4: bool, diff: MV, first_line: bool, first_mb: bool) -> MV { let A; let B; let C; @@ -42,17 +41,17 @@ impl MVInfo { match blk_no { 0 => { if mb_x != self.mb_start { - A = if mb_x != 0 { self.mv[self.mb_stride + mb_x * 2 - 1] } else { ZERO_MV }; - B = if !self.top { self.mv[ mb_x * 2] } else { A }; - C = if !self.top && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV }; + A = if !first_mb { self.mv[self.mb_stride + mb_x * 2 - 1] } else { ZERO_MV }; + B = if !first_line { self.mv[ mb_x * 2] } else { A }; + C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV }; } else { A = ZERO_MV; B = ZERO_MV; C = ZERO_MV; } }, 1 => { A = self.mv[self.mb_stride + mb_x * 2]; - B = if !self.top { self.mv[mb_x * 2 + 1] } else { A }; - C = if !self.top && !last { self.mv[mb_x * 2 + 2] } else { A }; + B = if !first_line { self.mv[mb_x * 2 + 1] } else { A }; + C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { A }; }, 2 => { A = if mb_x != self.mb_start { self.mv[self.mb_stride * 2 + mb_x * 2 - 1] } else { ZERO_MV }; @@ -123,13 +122,16 @@ pub struct H263BaseDecoder { mb_h: usize, num_mb: usize, ftype: Type, - prev_frm: Option>, - cur_frm: Option>, - last_ts: u8, + ipbs: IPBShuffler, + next_ts: u16, + last_ts: u16, + tsdiff: u16, has_b: bool, b_data: Vec, pred_coeffs: Vec, is_gob: bool, + may_have_b_frames: bool, + mv_data: Vec, } #[inline] @@ -148,17 +150,25 @@ fn clip_ac(ac: i16) -> i16 { #[allow(dead_code)] impl H263BaseDecoder { - pub fn new(is_gob: bool) -> Self { + pub fn new_with_opts(is_gob: bool, may_have_b_frames: bool) -> Self { H263BaseDecoder{ w: 0, h: 0, mb_w: 0, mb_h: 0, num_mb: 0, ftype: Type::Special, - prev_frm: None, cur_frm: None, - last_ts: 0, + ipbs: IPBShuffler::new(), + last_ts: 0, next_ts: 0, tsdiff: 0, has_b: false, b_data: Vec::new(), pred_coeffs: Vec::new(), is_gob: is_gob, + may_have_b_frames: may_have_b_frames, + mv_data: Vec::new(), } } + pub fn new(is_gob: bool) -> Self { + Self::new_with_opts(is_gob, false) + } + pub fn new_b_frames(is_gob: bool) -> Self { + Self::new_with_opts(is_gob, true) + } pub fn is_intra(&self) -> bool { self.ftype == Type::I } pub fn get_frame_type(&self) -> FrameType { @@ -188,13 +198,17 @@ impl H263BaseDecoder { self.has_b = pinfo.is_pb(); if self.has_b { - self.b_data.truncate(0); + self.mv_data.truncate(0); } - mem::swap(&mut self.cur_frm, &mut self.prev_frm); + let save_b_data = pinfo.mode.is_ref() && self.may_have_b_frames; + if save_b_data { + self.mv_data.truncate(0); + } - let tsdiff = pinfo.ts.wrapping_sub(self.last_ts); - let bsdiff = if pinfo.is_pb() { pinfo.get_pbinfo().get_trb() } else { 0 }; + let tsdiff = pinfo.ts.wrapping_sub(self.last_ts) >> 1; + let bsdiff = if pinfo.is_pb() { (pinfo.get_pbinfo().get_trb() as u16) << 7 } + else { pinfo.ts.wrapping_sub(self.next_ts) >> 1 }; let fmt = formats::YUV420_FORMAT; let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt); @@ -228,8 +242,7 @@ impl H263BaseDecoder { if !self.is_gob { mvi.reset(self.mb_w, mb_x, pinfo.get_mvmode()); cbpi.reset(self.mb_w); - sstate.first_line = true; - sstate.first_mb = true; + sstate.reset_slice(mb_x, mb_y); } } @@ -238,6 +251,9 @@ impl H263BaseDecoder { cbpi.set_cbp(mb_x, cbp); cbpi.set_q(mb_x, binfo.get_q()); if binfo.is_intra() { + if save_b_data { + self.mv_data.push(BlockMVInfo::Intra); + } for i in 0..6 { bd.decode_block_intra(&binfo, &sstate, binfo.get_q(), i, (cbp & (1 << (5 - i))) != 0, &mut blk[i])?; if apply_acpred && (binfo.acpred != ACPredMode::None) { @@ -312,19 +328,26 @@ impl H263BaseDecoder { mvi.set_zero_mv(mb_x); } else if (binfo.mode != Type::B) && !binfo.is_skipped() { if binfo.get_num_mvs() == 1 { - let mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0)); - if let Some(ref srcbuf) = self.prev_frm { + let mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb); + if save_b_data { + self.mv_data.push(BlockMVInfo::Inter_1MV(mv)); + } + if let Some(ref srcbuf) = self.ipbs.get_lastref() { bdsp.copy_blocks(&mut buf, srcbuf, mb_x * 16, mb_y * 16, 16, 16, mv); } } else { + let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV]; for blk_no in 0..4 { - let mv = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no)); - if let Some(ref srcbuf) = self.prev_frm { + mv[blk_no] = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb); + if let Some(ref srcbuf) = self.ipbs.get_lastref() { bdsp.copy_blocks(&mut buf, srcbuf, mb_x * 16 + (blk_no & 1) * 8, - mb_y * 16 + (blk_no & 2) * 4, 8, 8, mv); + mb_y * 16 + (blk_no & 2) * 4, 8, 8, mv[blk_no]); } } + if save_b_data { + self.mv_data.push(BlockMVInfo::Inter_4MV(mv)); + } } for i in 0..6 { bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?; @@ -332,19 +355,61 @@ impl H263BaseDecoder { } blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk); } else if binfo.mode != Type::B { + self.mv_data.push(BlockMVInfo::Inter_1MV(ZERO_MV)); mvi.set_zero_mv(mb_x); - if let Some(ref srcbuf) = self.prev_frm { + if let Some(ref srcbuf) = self.ipbs.get_lastref() { bdsp.copy_blocks(&mut buf, srcbuf, mb_x * 16, mb_y * 16, 16, 16, ZERO_MV); } } else { -//todo + let ref_mv_info = self.mv_data[mb_pos]; + let has_fwd = binfo.get_num_mvs() > 0; + let has_bwd = binfo.get_num_mvs2() > 0; + let is_direct = has_fwd && has_bwd; +//todo refactor + if let BlockMVInfo::Inter_4MV(mvs) = ref_mv_info { + for blk_no in 0..4 { + let ref_mv = mvs[blk_no]; + let mut ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff); + let mut ref_mv_bwd = ref_mv - ref_mv_fwd; + let xoff = mb_x * 16 + (blk_no & 1) * 8; + let yoff = mb_y * 16 + (blk_no & 2) * 4; + if has_fwd { ref_mv_fwd = MV::add_umv(ref_mv_fwd, binfo.get_mv(0), pinfo.get_mvmode()); } + if has_bwd { ref_mv_bwd = MV::add_umv(ref_mv_bwd, binfo.get_mv2(0), pinfo.get_mvmode()); } + if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) { + if is_direct || (!has_fwd && !has_bwd) { + bdsp.copy_blocks(&mut buf, fwd_buf, xoff, yoff, 8, 8, ref_mv_fwd); + bdsp.avg_blocks(&mut buf, bck_buf, xoff, yoff, 8, 8, ref_mv_bwd); + } else if has_fwd { + bdsp.copy_blocks(&mut buf, fwd_buf, xoff, yoff, 8, 8, ref_mv_fwd); + } else { + bdsp.copy_blocks(&mut buf, bck_buf, xoff, yoff, 8, 8, ref_mv_bwd); + } + } + } + } else { + let ref_mv = if let BlockMVInfo::Inter_1MV(mv_) = ref_mv_info { mv_ } else { ZERO_MV }; + let mut ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff); + let mut ref_mv_bwd = ref_mv - ref_mv_fwd; + if has_fwd { ref_mv_fwd = MV::add_umv(ref_mv_fwd, binfo.get_mv(0), pinfo.get_mvmode()); } + if has_bwd { ref_mv_bwd = MV::add_umv(ref_mv_bwd, binfo.get_mv2(0), pinfo.get_mvmode()); } + if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) { + if is_direct || (!has_fwd && !has_bwd) { + bdsp.copy_blocks(&mut buf, fwd_buf, mb_x * 16, mb_y * 16, 16, 16, ref_mv_fwd); + bdsp.avg_blocks(&mut buf, bck_buf, mb_x * 16, mb_y * 16, 16, 16, ref_mv_bwd); + } else if has_fwd { + bdsp.copy_blocks(&mut buf, fwd_buf, mb_x * 16, mb_y * 16, 16, 16, ref_mv_fwd); + } else { + bdsp.copy_blocks(&mut buf, bck_buf, mb_x * 16, mb_y * 16, 16, 16, ref_mv_bwd); + } + } + } if cbp != 0 { for i in 0..6 { bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?; h263_idct(&mut blk[i]); } + blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk); } - blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk); } if pinfo.is_pb() { let mut b_mb = BMB::new(); @@ -397,14 +462,19 @@ impl H263BaseDecoder { cbpi.update_row(); sstate.new_row(); } - - self.cur_frm = Some(buf); - self.last_ts = pinfo.ts; + + if pinfo.mode.is_ref() { + self.ipbs.add_frame(buf); + self.next_ts = self.last_ts; + self.last_ts = pinfo.ts; + self.tsdiff = tsdiff; + } + Ok(bufinfo) } pub fn get_bframe(&mut self, bdsp: &BlockDSP) -> DecoderResult { - if !self.has_b || !self.cur_frm.is_some() || !self.prev_frm.is_some() { + if !self.has_b || !self.ipbs.get_lastref().is_some() || !self.ipbs.get_nextref().is_some() { return Err(DecoderError::MissingReference); } self.has_b = false; @@ -416,10 +486,8 @@ impl H263BaseDecoder { let mut bufinfo = bufret.unwrap(); let mut b_buf = bufinfo.get_vbuf().unwrap(); - if let Some(ref bck_buf) = self.prev_frm { - if let Some(ref fwd_buf) = self.cur_frm { - recon_b_frame(&mut b_buf, bck_buf, fwd_buf, self.mb_w, self.mb_h, &self.b_data, bdsp); - } + if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) { + recon_b_frame(&mut b_buf, bck_buf, fwd_buf, self.mb_w, self.mb_h, &self.b_data, bdsp); } self.b_data.truncate(0); diff --git a/src/codecs/h263/intel263.rs b/src/codecs/h263/intel263.rs index 9bef52d..14ddeac 100644 --- a/src/codecs/h263/intel263.rs +++ b/src/codecs/h263/intel263.rs @@ -140,7 +140,7 @@ impl<'a> BlockDecoder for Intel263BR<'a> { let mut br = &mut self.br; let syncw = br.read(22)?; validate!(syncw == 0x000020); - let tr = br.read(8)? as u8; + let tr = (br.read(8)? << 8) as u16; check_marker(br)?; let id = br.read(1)?; validate!(id == 0); @@ -205,7 +205,8 @@ impl<'a> BlockDecoder for Intel263BR<'a> { let ftype = if is_intra { Type::I } else { Type::P }; let plusinfo = if deblock { Some(PlusInfo::new(false, deblock, false, false)) } else { None }; - let picinfo = PicInfo::new(w, h, ftype, umv, apm, quant as u8, tr, pbinfo, plusinfo); + let mvmode = if umv { MVMode::UMV } else { MVMode::Old }; + let picinfo = PicInfo::new(w, h, ftype, mvmode, umv, apm, quant as u8, tr, pbinfo, plusinfo); Ok(picinfo) } diff --git a/src/codecs/h263/mod.rs b/src/codecs/h263/mod.rs index cf2350d..094307c 100644 --- a/src/codecs/h263/mod.rs +++ b/src/codecs/h263/mod.rs @@ -36,6 +36,15 @@ pub enum Type { I, P, PB, Skip, B, Special } +impl Type { + pub fn is_ref(&self) -> bool { + match *self { + Type::I | Type::P | Type::PB => true, + _ => false, + } + } +} + #[allow(dead_code)] #[derive(Debug,Clone,Copy)] pub struct PBInfo { @@ -57,19 +66,20 @@ pub struct PicInfo { pub w: usize, pub h: usize, pub mode: Type, + pub mvmode: MVMode, pub umv: bool, pub apm: bool, pub quant: u8, pub pb: Option, - pub ts: u8, + pub ts: u16, pub plusinfo: Option, } #[allow(dead_code)] impl PicInfo { - pub fn new(w: usize, h: usize, mode: Type, umv: bool, apm: bool, quant: u8, ts: u8, pb: Option, plusinfo: Option) -> Self { + pub fn new(w: usize, h: usize, mode: Type, mvmode: MVMode, umv: bool, apm: bool, quant: u8, ts: u16, pb: Option, plusinfo: Option) -> Self { PicInfo { - w: w, h: h, mode: mode, + w: w, h: h, mode: mode, mvmode: mvmode, umv: umv, apm: apm, quant: quant, pb: pb, ts: ts, plusinfo: plusinfo } @@ -80,14 +90,10 @@ impl PicInfo { pub fn get_quant(&self) -> u8 { self.quant } pub fn get_apm(&self) -> bool { self.apm } pub fn is_pb(&self) -> bool { self.pb.is_some() } - pub fn get_ts(&self) -> u8 { self.ts } + pub fn get_ts(&self) -> u16 { self.ts } pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() } pub fn get_plusifo(&self) -> Option { self.plusinfo } - pub fn get_mvmode(&self) -> MVMode { - if self.umv { MVMode::UMV } - else if self.apm { MVMode::Long } - else { MVMode::Old } - } + pub fn get_mvmode(&self) -> MVMode { self.mvmode } } #[allow(dead_code)] @@ -122,6 +128,8 @@ pub struct SliceState { pub mb_y: usize, pub first_line: bool, pub first_mb: bool, + pub slice_mb_x: usize, + pub slice_mb_y: usize, } const SLICE_NO_END: usize = 99999999; @@ -143,12 +151,29 @@ impl SliceInfo { impl SliceState { pub fn new(is_iframe: bool) -> Self { - SliceState { is_iframe: is_iframe, mb_x: 0, mb_y: 0, first_line: true, first_mb: true } + SliceState { + is_iframe: is_iframe, mb_x: 0, mb_y: 0, first_line: true, first_mb: true, + slice_mb_x: 0, slice_mb_y: 0 + } + } + pub fn next_mb(&mut self) { + self.mb_x += 1; self.first_mb = false; + if self.mb_x >= self.slice_mb_x && self.mb_y > self.slice_mb_y { + self.first_line = false; + } } - pub fn next_mb(&mut self) { self.mb_x += 1; self.first_mb = false; } pub fn new_row(&mut self) { self.mb_x = 0; self.mb_y += 1; - self.first_line = false; self.first_mb = true; + if self.mb_x >= self.slice_mb_x && self.mb_y > self.slice_mb_y { + self.first_line = false; + } + self.first_mb = true; + } + pub fn reset_slice(&mut self, smb_x: usize, smb_y: usize) { + self.slice_mb_x = smb_x; + self.slice_mb_y = smb_y; + self.first_line = true; + self.first_mb = true; } } @@ -178,6 +203,14 @@ pub struct BBlockInfo { fwd: bool, } +#[allow(non_camel_case_types)] +#[derive(Debug,Clone,Copy)] +pub enum BlockMVInfo { + Intra, + Inter_1MV(MV), + Inter_4MV([MV; 4]), +} + #[allow(dead_code)] #[derive(Debug,Clone,Copy,PartialEq)] pub enum ACPredMode { @@ -323,14 +356,14 @@ impl MV { }; new_mv } - fn scale(&self, trb: u8, trd: u8) -> Self { + fn scale(&self, trb: u16, trd: u16) -> Self { if (trd == 0) || (trb == 0) { ZERO_MV } else { - MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) } + MV { x: (((self.x as i32) * (trb as i32)) / (trd as i32)) as i16, y: (((self.y as i32) * (trb as i32)) / (trd as i32)) as i16 } } } - fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self { + fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u16, trd: u16) -> Self { let bscale = (trb as i16) - (trd as i16); let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 }; let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 }; diff --git a/src/codecs/h263/rv10.rs b/src/codecs/h263/rv10.rs index 0093f87..efca754 100644 --- a/src/codecs/h263/rv10.rs +++ b/src/codecs/h263/rv10.rs @@ -224,7 +224,7 @@ println!("decoding picture header size {}", if self.num_slices > 1 { self.slice_ let mb_end = shdr.mb_x + shdr.mb_y * self.mb_w + shdr.mb_c; let ftype = if !shdr.is_p { Type::I } else { Type::P }; - let picinfo = PicInfo::new(self.w, self.h, ftype, false, false, shdr.qscale, 0, None, None); + let picinfo = PicInfo::new(self.w, self.h, ftype, MVMode::Old, false, false, shdr.qscale, 0, None, None); Ok(picinfo) } diff --git a/src/codecs/h263/rv20.rs b/src/codecs/h263/rv20.rs index a781d1e..177b6f1 100644 --- a/src/codecs/h263/rv20.rs +++ b/src/codecs/h263/rv20.rs @@ -55,6 +55,7 @@ struct RealVideo20BR<'a> { struct RV20SliceInfo { ftype: Type, + seq: u32, qscale: u8, mb_x: usize, mb_y: usize, @@ -64,8 +65,8 @@ struct RV20SliceInfo { } impl RV20SliceInfo { - fn new(ftype: Type, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize) -> Self { - RV20SliceInfo { ftype: ftype, qscale: qscale, mb_x: mb_x, mb_y: mb_y, mb_pos: mb_pos, w: w, h: h } + fn new(ftype: Type, seq: u32, qscale: u8, mb_x: usize, mb_y: usize, mb_pos: usize, w: usize, h: usize) -> Self { + RV20SliceInfo { ftype: ftype, seq: seq, qscale: qscale, mb_x: mb_x, mb_y: mb_y, mb_pos: mb_pos, w: w, h: h } } } @@ -139,11 +140,7 @@ impl<'a> RealVideo20BR<'a> { level = code.get_level(); last = code.is_last(); if br.read_bool()? { level = -level; } - if (idx != 0) || !sstate.is_iframe { - level = (level * q) + q_add; - } else { - level = level * (H263_DC_SCALES[quant as usize] as i16); - } + level = (level * q) + q_add; } else { last = br.read_bool()?; run = br.read(6)? as u8; @@ -153,11 +150,7 @@ impl<'a> RealVideo20BR<'a> { let top = br.read_s(6)? as i16; level = (top << 5) | low; } - if (idx != 0) || !sstate.is_iframe { - level = (level * q) + q_add; - } else { - level = level * (H263_DC_SCALES[quant as usize] as i16); - } + level = (level * q) + q_add; if level < -2048 { level = -2048; } if level > 2047 { level = 2047; } } @@ -210,7 +203,7 @@ println!("slice ends @ {}\n", self.br.tell()); }*/ let plusinfo = Some(PlusInfo::new(shdr.ftype == Type::I, false, false, false)); - let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, false, false, shdr.qscale, 0, None, plusinfo); + let picinfo = PicInfo::new(shdr.w, shdr.h, shdr.ftype, MVMode::Long, false, false, shdr.qscale, shdr.seq as u16, None, plusinfo); Ok(picinfo) } @@ -245,7 +238,7 @@ println!("slice ends @ {}\n", self.br.tell()); if pi.aic { let acpp = br.read_bool()?; acpred = ACPredMode::DC; - println!(" acp {} @ {}", acpp as u8, br.tell()); +//println!(" acp {} @ {}", acpp as u8, br.tell()); if acpp { acpred = if br.read_bool()? { ACPredMode::Hor } else { ACPredMode::Ver }; } @@ -309,7 +302,7 @@ println!(" MB {}.{} cbp = {:X}", sstate.mb_x, sstate.mb_y, cbp); } Ok(binfo) }, - _ => { // B + Type::B => { // B let mut mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; while mbtype == 14 { mbtype = br.read_cb(&self.tables.mbtype_b_cb)? as usize; } @@ -330,7 +323,7 @@ println!(" MB {}.{} cbp = {:X}", sstate.mb_x, sstate.mb_y, cbp); let idx = br.read(2)? as usize; q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; } -println!(" MB B {}.{} cbp = {:X} @ {} ({} {})", sstate.mb_x, sstate.mb_y, cbp, br.tell(), mbtype, is_intra); +//println!(" MB B {}.{} cbp = {:X} @ {} ({} {})", sstate.mb_x, sstate.mb_y, cbp, br.tell(), mbtype, is_intra); if is_intra { let binfo = BlockInfo::new(Type::I, cbp, q); @@ -348,6 +341,7 @@ println!(" MB B {}.{} cbp = {:X} @ {} ({} {})", sstate.mb_x, sstate.mb_y, cbp, b } Ok(binfo) }, + _ => { unreachable!(); }, } } @@ -387,9 +381,9 @@ impl<'a> RealVideo20BR<'a> { br.skip(1)?; // loop filter } let seq = if self.minor_ver <= 1 { - br.read(8)? << 7 + br.read(8)? << 8 } else { - br.read(13)? << 2 + br.read(13)? << 3 }; let w; let h; @@ -419,7 +413,7 @@ impl<'a> RealVideo20BR<'a> { } println!("slice q {} mb {},{}", qscale, mb_x, mb_y); - Ok(RV20SliceInfo::new(ftype, qscale, mb_x, mb_y, mb_pos, w, h)) + Ok(RV20SliceInfo::new(ftype, seq, qscale, mb_x, mb_y, mb_pos, w, h)) } } @@ -455,7 +449,7 @@ impl RealVideo20Decoder { RealVideo20Decoder{ info: Rc::new(DUMMY_CODEC_INFO), - dec: H263BaseDecoder::new(false), + dec: H263BaseDecoder::new_b_frames(false), tables: tables, w: 0, h: 0, @@ -538,7 +532,7 @@ mod test { use test::dec_video::test_file_decoding; #[test] fn test_rv20() { - test_file_decoding("realmedia", "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", None, true, false, Some("rv20")); + test_file_decoding("realmedia", "assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm", /*None*/Some(7000), true, false, Some("rv20")); // test_file_decoding("realmedia", "assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm", /*None*/Some(1000), true, false, Some("rv20")); } } diff --git a/src/codecs/mod.rs b/src/codecs/mod.rs index 6572f26..4bfc503 100644 --- a/src/codecs/mod.rs +++ b/src/codecs/mod.rs @@ -1,6 +1,7 @@ use frame::*; use std::rc::Rc; use std::cell::RefCell; +use std::mem; use io::byteio::ByteIOError; use io::bitreader::BitReaderError; use io::codebook::CodebookError; @@ -102,6 +103,40 @@ impl IPShuffler { } } +#[allow(dead_code)] +struct IPBShuffler { + lastframe: Option>, + nextframe: Option>, +} + +impl IPBShuffler { + #[allow(dead_code)] + fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } } + #[allow(dead_code)] + fn clear(&mut self) { self.lastframe = None; self.nextframe = None; } + #[allow(dead_code)] + fn add_frame(&mut self, buf: NAVideoBuffer) { + mem::swap(&mut self.lastframe, &mut self.nextframe); + self.lastframe = Some(buf); + } + #[allow(dead_code)] + fn get_lastref(&mut self) -> Option> { + if let Some(ref frm) = self.lastframe { + Some(frm.clone()) + } else { + None + } + } + #[allow(dead_code)] + fn get_nextref(&mut self) -> Option> { + if let Some(ref frm) = self.nextframe { + Some(frm.clone()) + } else { + None + } + } +} + pub trait NADecoder { fn init(&mut self, info: Rc) -> DecoderResult<()>; fn decode(&mut self, pkt: &NAPacket) -> DecoderResult; -- 2.39.5