X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fcodecs%2Fh263%2Fmod.rs;h=4f066a6e551c6f452a835ebd1c44c91a37428be5;hb=9e8a5d2d663e9e88cac9cff09d12385320331c3d;hp=90170e3cf6a440011fa35c752df0b4bdcb66cb38;hpb=88c58b1b958a5478dca6595e2efc3a1a6b5f7e19;p=nihav.git diff --git a/src/codecs/h263/mod.rs b/src/codecs/h263/mod.rs index 90170e3..4f066a6 100644 --- a/src/codecs/h263/mod.rs +++ b/src/codecs/h263/mod.rs @@ -9,21 +9,40 @@ pub mod decoder; #[cfg(feature="decoder_intel263")] pub mod intel263; +#[cfg(feature="decoder_realvideo1")] +pub mod rv10; +#[cfg(feature="decoder_realvideo2")] +pub mod rv20; pub trait BlockDecoder { fn decode_pichdr(&mut self) -> DecoderResult; - fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult; - fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &Slice) -> DecoderResult; - fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; - fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; + fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult; + fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &SliceInfo, sstate: &SliceState) -> DecoderResult; + fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; + fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; fn is_slice_end(&mut self) -> bool; +} - fn filter_row(&mut self, buf: &mut NAVideoBuffer, mb_y: usize, mb_w: usize, cbpi: &CBPInfo); +pub trait BlockDSP { + fn idct(&self, blk: &mut [i16; 64]); + fn copy_blocks(&self, dst: &mut NAVideoBuffer, src: &NAVideoBuffer, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV); + fn avg_blocks(&self, dst: &mut NAVideoBuffer, src: &NAVideoBuffer, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV); + fn filter_row(&self, buf: &mut NAVideoBuffer, mb_y: usize, mb_w: usize, cbpi: &CBPInfo); } +#[allow(dead_code)] #[derive(Debug,Clone,Copy,PartialEq)] pub enum Type { - I, P, Skip, Special + 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)] @@ -44,64 +63,135 @@ impl PBInfo { #[allow(dead_code)] #[derive(Debug,Clone,Copy)] pub struct PicInfo { - w: usize, - h: usize, - mode: Type, - quant: u8, - apm: bool, - umv: bool, - pb: Option, - ts: u8, - deblock: bool, + 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: u16, + pub plusinfo: Option, } #[allow(dead_code)] impl PicInfo { - pub fn new(w: usize, h: usize, mode: Type, quant: u8, apm: bool, umv: bool, ts: u8, pb: Option, deblock: bool) -> Self { - PicInfo{ w: w, h: h, mode: mode, quant: quant, apm: apm, umv: umv, ts: ts, pb: pb, deblock: deblock } + 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, mvmode: mvmode, + umv: umv, apm: apm, quant: quant, + pb: pb, ts: ts, plusinfo: plusinfo + } } pub fn get_width(&self) -> usize { self.w } pub fn get_height(&self) -> usize { self.h } pub fn get_mode(&self) -> Type { self.mode } pub fn get_quant(&self) -> u8 { self.quant } pub fn get_apm(&self) -> bool { self.apm } - pub fn get_umv(&self) -> bool { self.umv } 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 { self.mvmode } } +#[allow(dead_code)] #[derive(Debug,Clone,Copy)] -pub struct Slice { - mb_x: usize, - mb_y: usize, - quant: u8, +pub struct PlusInfo { + pub aic: bool, + pub deblock: bool, + pub aiv_mode: bool, + pub mq_mode: bool, } -impl Slice { - pub fn new(mb_x: usize, mb_y: usize, quant: u8) -> Self { - Slice{ mb_x: mb_x, mb_y: mb_y, quant: quant } +impl PlusInfo { + pub fn new(aic: bool, deblock: bool, aiv_mode: bool, mq_mode: bool) -> Self { + PlusInfo { aic: aic, deblock: deblock, aiv_mode: aiv_mode, mq_mode: mq_mode } + } +} + +#[allow(dead_code)] +#[derive(Debug,Clone,Copy)] +pub struct SliceInfo { + pub mb_x: usize, + pub mb_y: usize, + pub mb_end: usize, + pub quant: u8, +} + +#[allow(dead_code)] +#[derive(Debug,Clone,Copy)] +pub struct SliceState { + pub is_iframe: bool, + pub mb_x: usize, + 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; + +impl SliceInfo { + pub fn new(mb_x: usize, mb_y: usize, mb_end: usize, quant: u8) -> Self { + SliceInfo{ mb_x: mb_x, mb_y: mb_y, mb_end: mb_end, quant: quant } + } + pub fn new_gob(mb_x: usize, mb_y: usize, quant: u8) -> Self { + SliceInfo{ mb_x: mb_x, mb_y: mb_y, mb_end: SLICE_NO_END, quant: quant } } pub fn get_default_slice(pinfo: &PicInfo) -> Self { - Slice{ mb_x: 0, mb_y: 0, quant: pinfo.get_quant() } + SliceInfo{ mb_x: 0, mb_y: 0, mb_end: SLICE_NO_END, quant: pinfo.get_quant() } } pub fn get_quant(&self) -> u8 { self.quant } + pub fn is_at_end(&self, mb_pos: usize) -> bool { self.mb_end == mb_pos } + pub fn needs_check(&self) -> bool { self.mb_end == SLICE_NO_END } +} + +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, + 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 new_row(&mut self) { + self.mb_x = 0; self.mb_y += 1; + 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; + } } #[derive(Debug,Clone,Copy)] pub struct BlockInfo { - intra: bool, - skip: bool, - mode: Type, - cbp: u8, - q: u8, - mv: [MV; 4], - num_mv: usize, - bpart: bool, - b_cbp: u8, - mv2: [MV; 2], - num_mv2: usize, - fwd: bool, + pub intra: bool, + pub skip: bool, + pub mode: Type, + pub cbp: u8, + pub q: u8, + pub mv: [MV; 4], + pub num_mv: usize, + pub bpart: bool, + pub b_cbp: u8, + pub mv2: [MV; 2], + pub num_mv2: usize, + pub fwd: bool, + pub acpred: ACPredMode, } #[allow(dead_code)] @@ -113,6 +203,23 @@ 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 { + None, + DC, + Ver, + Hor, +} + #[allow(dead_code)] impl BlockInfo { pub fn new(mode: Type, cbp: u8, q: u8) -> Self { @@ -129,6 +236,7 @@ impl BlockInfo { mv2: [ZERO_MV, ZERO_MV], num_mv2: 0, fwd: false, + acpred: ACPredMode::None, } } pub fn is_intra(&self) -> bool { self.intra } @@ -162,6 +270,8 @@ impl BlockInfo { self.mv2 = mv_arr; } pub fn is_b_fwd(&self) -> bool { self.fwd } + pub fn set_acpred(&mut self, acpred: ACPredMode) { self.acpred = acpred } + pub fn get_acpred(&self) -> ACPredMode { self.acpred } } impl BBlockInfo { @@ -176,6 +286,13 @@ impl BBlockInfo { pub fn get_num_mv(&self) -> usize { self.num_mv } } +#[derive(Debug,Clone,Copy)] +pub enum MVMode { + Old, + Long, + UMV, +} + #[derive(Debug,Clone,Copy)] pub struct MV { x: i16, @@ -215,32 +332,41 @@ impl MV { } MV { x: x, y: y } } - fn add_umv(pred_mv: MV, add: MV, umv: bool) -> Self { + fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> Self { let mut new_mv = pred_mv + add; - if umv { - if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } - if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } - if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } - if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } - } else { - if new_mv.x > 31 { new_mv.x -= 64; } - else if new_mv.x < -32 { new_mv.x += 64; } - if new_mv.y > 31 { new_mv.y -= 64; } - else if new_mv.y < -32 { new_mv.y += 64; } - } + match mvmode { + MVMode::Old => { + if new_mv.x >= 64 { new_mv.x -= 64; } + else if new_mv.x <= -64 { new_mv.x += 64; } + if new_mv.y >= 64 { new_mv.y -= 64; } + else if new_mv.y <= -64 { new_mv.y += 64; } + }, + MVMode::Long => { + if new_mv.x > 31 { new_mv.x -= 64; } + else if new_mv.x < -32 { new_mv.x += 64; } + if new_mv.y > 31 { new_mv.y -= 64; } + else if new_mv.y < -32 { new_mv.y += 64; } + }, + MVMode::UMV => { + if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } + if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } + if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } + if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } + }, + }; 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 { - 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 }; + fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u16, trd: u16) -> Self { + let bscale = (trb as i32) - (trd as i32); + let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { (bscale * (pvec.x as i32) / (trd as i32)) as i16 } else { 0 }; + let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { (bscale * (pvec.y as i32) / (trd as i32)) as i16 } else { 0 }; MV { x: x, y: y } } }