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 };
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)]
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);
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);
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 {
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 {
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));
}
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() {
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);
}
}
}
mvi.update_row();
- if is_b {
+ if is_b || pinfo.is_pb() {
mvi2.update_row();
}
cbpi.update_row();
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);
gob_no: usize,
mb_w: usize,
is_pb: bool,
+ is_ipb: bool,
}
fn check_marker<'a>(br: &mut BitReader<'a>) -> DecoderResult<()> {
gob_no: 0,
mb_w: 0,
is_pb: false,
+ is_ipb: false,
}
}
Ok(MV::new(xval, yval))
}
-fn decode_b_info(br: &mut BitReader, is_pb: bool, is_intra: bool) -> DecoderResult<BBlockInfo> {
+fn decode_b_info(br: &mut BitReader, is_pb: bool, is_ipb: bool, is_intra: bool) -> DecoderResult<BBlockInfo> {
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))
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)?;
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;
}
let is_4x4 = (cbpc & 0x10) != 0;
if is_intra {
let mut mvec: Vec<MV> = 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 {
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;
use std::fmt;
-use std::ops::{Add, Sub};
+use std::ops::{Add, AddAssign, Sub, SubAssign};
use super::DecoderResult;
use frame::NAVideoBuffer;
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)]
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<PlusInfo> { self.plusinfo }
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)