From: Kostya Shishkov Date: Wed, 28 Feb 2018 18:44:59 +0000 (+0100) Subject: h263: better PB-frame reconstruction (for Intel263) X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=effbebce8a7acf0cbdd6075b523bbe0b1c33bb57;p=nihav.git h263: better PB-frame reconstruction (for Intel263) --- diff --git a/src/codecs/h263/decoder.rs b/src/codecs/h263/decoder.rs index a7768ea..b200bd5 100644 --- a/src/codecs/h263/decoder.rs +++ b/src/codecs/h263/decoder.rs @@ -51,7 +51,7 @@ impl MVInfo { 1 => { A = self.mv[self.mb_stride + mb_x * 2]; 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 }; + C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV/*A*/ }; }, 2 => { A = if mb_x != self.mb_start { self.mv[self.mb_stride * 2 + mb_x * 2 - 1] } else { ZERO_MV }; @@ -90,6 +90,9 @@ impl MVInfo { self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = ZERO_MV; self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = ZERO_MV; } + fn get_mv(&self, mb_x: usize, blk_no: usize) -> MV { + self.mv[self.mb_stride + mb_x * 2 + (blk_no & 1) + (blk_no >> 1) * self.mb_stride] + } } #[allow(dead_code)] @@ -226,7 +229,7 @@ impl H263BaseDecoder { bd.decode_slice_header(&pinfo)? }; mvi.reset(self.mb_w, 0, pinfo.get_mvmode()); - if is_b { + if is_b || pinfo.is_pb() { mvi2.reset(self.mb_w, 0, pinfo.get_mvmode()); } cbpi.reset(self.mb_w); @@ -247,7 +250,7 @@ impl H263BaseDecoder { slice = bd.decode_slice_header(&pinfo)?; if !self.is_gob { mvi.reset(self.mb_w, mb_x, pinfo.get_mvmode()); - if is_b { + if is_b || pinfo.is_pb() { mvi2.reset(self.mb_w, mb_x, pinfo.get_mvmode()); } cbpi.reset(self.mb_w); @@ -337,6 +340,8 @@ impl H263BaseDecoder { mvi.set_zero_mv(mb_x); if is_b { mvi2.set_zero_mv(mb_x); + } else if pinfo.is_pb() { + mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb); } } else if (binfo.mode != Type::B) && !binfo.is_skipped() { if binfo.get_num_mvs() == 1 { @@ -347,6 +352,9 @@ impl H263BaseDecoder { if let Some(ref srcbuf) = self.ipbs.get_lastref() { bdsp.copy_blocks(&mut buf, srcbuf, mb_x * 16, mb_y * 16, 16, 16, mv); } + if pinfo.is_pb() { + mvi2.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb); + } } else { let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV]; for blk_no in 0..4 { @@ -357,6 +365,11 @@ impl H263BaseDecoder { mb_y * 16 + (blk_no & 2) * 4, 8, 8, mv[blk_no]); } } + if pinfo.is_pb() { + for blk_no in 0..4 { + mvi2.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb); + } + } if save_b_data { self.mv_data.push(BlockMVInfo::Inter_4MV(mv)); } @@ -366,13 +379,13 @@ impl H263BaseDecoder { bdsp.idct(&mut blk[i]); } blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk); - if is_b { + if is_b && !pinfo.is_pb() { mvi2.set_zero_mv(mb_x); } } else if binfo.mode != Type::B { self.mv_data.push(BlockMVInfo::Inter_1MV(ZERO_MV)); mvi.set_zero_mv(mb_x); - if is_b { + if is_b || pinfo.is_pb() { mvi2.set_zero_mv(mb_x); } if let Some(ref srcbuf) = self.ipbs.get_lastref() { @@ -457,27 +470,26 @@ impl H263BaseDecoder { bdsp.idct(&mut b_mb.blk[i]); } - let is_fwd = !binfo.is_b_fwd(); + let is_fwd = binfo.is_b_fwd(); b_mb.fwd = is_fwd; - b_mb.num_mv = binfo.get_num_mvs(); - if binfo.get_num_mvs() == 0 { - b_mb.num_mv = 1; - b_mb.mv_f[0] = binfo.get_mv2(1); - b_mb.mv_b[0] = binfo.get_mv2(0); - } else if binfo.get_num_mvs() == 1 { - let src_mv = binfo.get_mv(0).scale(bsdiff, tsdiff); - let mv_f = MV::add_umv(src_mv, binfo.get_mv2(0), pinfo.get_mvmode()); - let mv_b = MV::b_sub(binfo.get_mv(0), mv_f, binfo.get_mv2(0), bsdiff, tsdiff); + if binfo.get_num_mvs() != 4 { + let ref_mv = mvi2.get_mv(mb_x, 0); + let b_mv = if binfo.is_intra() { binfo.get_mv2(1) } else { binfo.get_mv2(0) }; + let src_mv = if is_fwd { ZERO_MV } else { ref_mv.scale(bsdiff, tsdiff) }; + let mv_f = MV::add_umv(src_mv, b_mv, pinfo.get_mvmode()); + let mv_b = MV::b_sub(ref_mv, mv_f, b_mv, bsdiff, tsdiff); b_mb.mv_f[0] = mv_f; b_mb.mv_b[0] = mv_b; + b_mb.num_mv = 1; } else { for blk_no in 0..4 { - let src_mv = binfo.get_mv(blk_no).scale(bsdiff, tsdiff); + let src_mv = if is_fwd { ZERO_MV } else { mvi2.get_mv(mb_x, blk_no).scale(bsdiff, tsdiff) }; let mv_f = MV::add_umv(src_mv, binfo.get_mv2(0), pinfo.get_mvmode()); - let mv_b = MV::b_sub(binfo.get_mv(blk_no), mv_f, binfo.get_mv2(0), bsdiff, tsdiff); + let mv_b = MV::b_sub(mvi2.get_mv(mb_x, blk_no), mv_f, binfo.get_mv2(0), bsdiff, tsdiff); b_mb.mv_f[blk_no] = mv_f; b_mb.mv_b[blk_no] = mv_b; } + b_mb.num_mv = 4; } self.b_data.push(b_mb); } @@ -490,7 +502,7 @@ impl H263BaseDecoder { } } mvi.update_row(); - if is_b { + if is_b || pinfo.is_pb() { mvi2.update_row(); } cbpi.update_row(); @@ -521,7 +533,7 @@ impl H263BaseDecoder { let mut b_buf = bufinfo.get_vbuf().unwrap(); 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); + recon_b_frame(&mut b_buf, fwd_buf, bck_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 e47073d..0c5652a 100644 --- a/src/codecs/h263/intel263.rs +++ b/src/codecs/h263/intel263.rs @@ -30,6 +30,7 @@ struct Intel263BR<'a> { gob_no: usize, mb_w: usize, is_pb: bool, + is_ipb: bool, } fn check_marker<'a>(br: &mut BitReader<'a>) -> DecoderResult<()> { @@ -46,6 +47,7 @@ impl<'a> Intel263BR<'a> { gob_no: 0, mb_w: 0, is_pb: false, + is_ipb: false, } } @@ -114,19 +116,25 @@ fn decode_mv(br: &mut BitReader, mv_cb: &Codebook) -> DecoderResult { Ok(MV::new(xval, yval)) } -fn decode_b_info(br: &mut BitReader, is_pb: bool, is_intra: bool) -> DecoderResult { +fn decode_b_info(br: &mut BitReader, is_pb: bool, is_ipb: bool, is_intra: bool) -> DecoderResult { if is_pb { // as improved pb - let pb_mv_add = if is_intra { 1 } else { 0 }; - if br.read_bool()?{ - if br.read_bool()? { - let pb_mv_count = 1 - (br.read(1)? as usize); - let cbpb = br.read(6)? as u8; - Ok(BBlockInfo::new(true, cbpb, pb_mv_count + pb_mv_add, pb_mv_count == 1)) + if is_ipb { + let pb_mv_add = if is_intra { 1 } else { 0 }; + if br.read_bool()?{ + if br.read_bool()? { + let pb_mv_count = 1 - (br.read(1)? as usize); + let cbpb = br.read(6)? as u8; + Ok(BBlockInfo::new(true, cbpb, pb_mv_count + pb_mv_add, pb_mv_count == 1)) + } else { + Ok(BBlockInfo::new(true, 0, 1 + pb_mv_add, true)) + } } else { - Ok(BBlockInfo::new(true, 0, 1 + pb_mv_add, true)) + Ok(BBlockInfo::new(true, 0, pb_mv_add, false)) } } else { - Ok(BBlockInfo::new(true, 0, pb_mv_add, false)) + let mvdb = br.read_bool()?; + let cbpb = if mvdb && br.read_bool()? { br.read(6)? as u8 } else { 0 }; + Ok(BBlockInfo::new(true, cbpb, if mvdb { 1 } else { 0 }, false)) } } else { Ok(BBlockInfo::new(false, 0, 0, false)) @@ -155,19 +163,22 @@ impl<'a> BlockDecoder for Intel263BR<'a> { let apm = br.read_bool()?; self.is_pb = br.read_bool()?; let deblock; + let pbplus; if sfmt == 0b111 { sfmt = br.read(3)?; validate!((sfmt != 0b000) && (sfmt != 0b111)); br.read(2)?; // unknown flags deblock = br.read_bool()?; br.read(1)?; // unknown flag - let pbplus = br.read_bool()?; + pbplus = br.read_bool()?; br.read(5)?; // unknown flags let marker = br.read(5)?; validate!(marker == 1); } else { deblock = false; + pbplus = false; } + self.is_ipb = pbplus; let w; let h; if sfmt == 0b110 { let par = br.read(4)?; @@ -192,7 +203,7 @@ impl<'a> BlockDecoder for Intel263BR<'a> { if self.is_pb { let trb = br.read(3)?; let dbquant = br.read(2)?; - pbinfo = Some(PBInfo::new(trb as u8, dbquant as u8)); + pbinfo = Some(PBInfo::new(trb as u8, dbquant as u8, pbplus)); } else { pbinfo = None; } @@ -250,7 +261,7 @@ impl<'a> BlockDecoder for Intel263BR<'a> { let is_4x4 = (cbpc & 0x10) != 0; if is_intra { let mut mvec: Vec = Vec::new(); - let bbinfo = decode_b_info(br, self.is_pb, true)?; + let bbinfo = decode_b_info(br, self.is_pb, self.is_ipb, true)?; let cbpy = br.read_cb(&self.tables.cbpy_cb)?; let cbp = (cbpy << 2) | (cbpc & 3); if dquant { @@ -268,7 +279,7 @@ impl<'a> BlockDecoder for Intel263BR<'a> { return Ok(binfo); } - let bbinfo = decode_b_info(br, self.is_pb, false)?; + let bbinfo = decode_b_info(br, self.is_pb, self.is_ipb, false)?; let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; // if /* !aiv && */(cbpc & 3) != 3 { cbpy ^= 0xF; diff --git a/src/codecs/h263/mod.rs b/src/codecs/h263/mod.rs index 9973dc7..15ca5a9 100644 --- a/src/codecs/h263/mod.rs +++ b/src/codecs/h263/mod.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::ops::{Add, Sub}; +use std::ops::{Add, AddAssign, Sub, SubAssign}; use super::DecoderResult; use frame::NAVideoBuffer; @@ -50,14 +50,16 @@ impl Type { pub struct PBInfo { trb: u8, dbquant: u8, + improved: bool, } impl PBInfo { - pub fn new(trb: u8, dbquant: u8) -> Self { - PBInfo{ trb: trb, dbquant: dbquant } + pub fn new(trb: u8, dbquant: u8, improved: bool) -> Self { + PBInfo{ trb: trb, dbquant: dbquant, improved: improved } } pub fn get_trb(&self) -> u8 { self.trb } pub fn get_dbquant(&self) -> u8 { self.dbquant } + pub fn is_improved(&self) -> bool { self.improved } } #[allow(dead_code)] @@ -90,6 +92,13 @@ 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 is_ipb(&self) -> bool { + if let Some(ref pbi) = self.pb { + pbi.is_improved() + } else { + false + } + } 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 } @@ -379,11 +388,19 @@ impl Add for MV { fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } } } +impl AddAssign for MV { + fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; } +} + impl Sub for MV { type Output = MV; fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } } } +impl SubAssign for MV { + fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; } +} + impl fmt::Display for MV { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{},{}", self.x, self.y)