2 use std::ops::{Add, Sub};
3 use super::DecoderResult;
4 use frame::NAVideoBuffer;
10 #[cfg(feature="decoder_intel263")]
12 #[cfg(feature="decoder_realvideo1")]
14 #[cfg(feature="decoder_realvideo2")]
17 pub trait BlockDecoder {
18 fn decode_pichdr(&mut self) -> DecoderResult<PicInfo>;
19 fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult<Slice>;
20 fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &Slice) -> DecoderResult<BlockInfo>;
21 fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>;
22 fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>;
23 fn is_slice_end(&mut self) -> bool;
25 fn filter_row(&mut self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo);
28 #[derive(Debug,Clone,Copy,PartialEq)]
34 #[derive(Debug,Clone,Copy)]
41 pub fn new(trb: u8, dbquant: u8) -> Self {
42 PBInfo{ trb: trb, dbquant: dbquant }
44 pub fn get_trb(&self) -> u8 { self.trb }
45 pub fn get_dbquant(&self) -> u8 { self.dbquant }
49 #[derive(Debug,Clone,Copy)]
64 pub fn new(w: usize, h: usize, mode: Type, quant: u8, apm: bool, mvmode: MVMode, ts: u8, pb: Option<PBInfo>, deblock: bool) -> Self {
65 PicInfo{ w: w, h: h, mode: mode, quant: quant, apm: apm, mvmode: mvmode, ts: ts, pb: pb, deblock: deblock }
67 pub fn get_width(&self) -> usize { self.w }
68 pub fn get_height(&self) -> usize { self.h }
69 pub fn get_mode(&self) -> Type { self.mode }
70 pub fn get_quant(&self) -> u8 { self.quant }
71 pub fn get_apm(&self) -> bool { self.apm }
72 pub fn get_mvmode(&self) -> MVMode { self.mvmode }
73 pub fn is_pb(&self) -> bool { self.pb.is_some() }
74 pub fn get_ts(&self) -> u8 { self.ts }
75 pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() }
78 #[derive(Debug,Clone,Copy)]
86 pub fn new(mb_x: usize, mb_y: usize, quant: u8) -> Self {
87 Slice{ mb_x: mb_x, mb_y: mb_y, quant: quant }
89 pub fn get_default_slice(pinfo: &PicInfo) -> Self {
90 Slice{ mb_x: 0, mb_y: 0, quant: pinfo.get_quant() }
92 pub fn get_quant(&self) -> u8 { self.quant }
95 #[derive(Debug,Clone,Copy)]
96 pub struct BlockInfo {
112 #[derive(Debug,Clone,Copy)]
113 pub struct BBlockInfo {
122 pub fn new(mode: Type, cbp: u8, q: u8) -> Self {
124 intra: mode == Type::I,
125 skip: (cbp == 0) && (mode != Type::I),
129 mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)],
133 mv2: [ZERO_MV, ZERO_MV],
138 pub fn is_intra(&self) -> bool { self.intra }
139 pub fn is_skipped(&self) -> bool { self.skip }
140 pub fn get_mode(&self) -> Type { self.mode }
141 pub fn get_cbp(&self) -> u8 { self.cbp }
142 pub fn get_q(&self) -> u8 { self.q }
143 pub fn get_num_mvs(&self) -> usize { self.num_mv }
144 pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] }
145 pub fn has_b_part(&self) -> bool { self.bpart }
146 pub fn get_cbp_b(&self) -> u8 { self.b_cbp }
147 pub fn get_num_mvs2(&self) -> usize { self.num_mv2 }
148 pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] }
149 pub fn set_mv(&mut self, mvs: &[MV]) {
150 if mvs.len() > 0 { self.skip = false; }
151 let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)];
152 for i in 0..mvs.len() { mv_arr[i] = mvs[i]; }
154 self.num_mv = mvs.len();
156 pub fn set_bpart(&mut self, bbinfo: BBlockInfo) {
157 self.bpart = bbinfo.present;
158 self.b_cbp = bbinfo.cbp;
159 self.fwd = bbinfo.fwd;
160 self.num_mv2 = bbinfo.get_num_mv();
162 pub fn set_b_mv(&mut self, mvs: &[MV]) {
163 if mvs.len() > 0 { self.skip = false; }
164 let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV];
165 for i in 0..mvs.len() { mv_arr[i] = mvs[i]; }
168 pub fn is_b_fwd(&self) -> bool { self.fwd }
172 pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self {
180 pub fn get_num_mv(&self) -> usize { self.num_mv }
183 #[derive(Debug,Clone,Copy)]
190 #[derive(Debug,Clone,Copy)]
197 pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } }
198 pub fn pred(a: MV, b: MV, c: MV) -> Self {
204 if a.x < c.x { x = c.x; } else { x = a.x; }
208 if a.x < c.x { x = a.x; } else { x = c.x; }
218 if a.y < c.y { y = c.y; } else { y = a.y; }
222 if a.y < c.y { y = a.y; } else { y = c.y; }
229 fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> Self {
230 let mut new_mv = pred_mv + add;
233 if new_mv.x >= 64 { new_mv.x -= 64; }
234 else if new_mv.x <= -64 { new_mv.x += 64; }
235 if new_mv.y >= 64 { new_mv.y -= 64; }
236 else if new_mv.y <= -64 { new_mv.y += 64; }
239 if new_mv.x > 31 { new_mv.x -= 64; }
240 else if new_mv.x < -32 { new_mv.x += 64; }
241 if new_mv.y > 31 { new_mv.y -= 64; }
242 else if new_mv.y < -32 { new_mv.y += 64; }
245 if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; }
246 if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; }
247 if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; }
248 if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; }
253 fn scale(&self, trb: u8, trd: u8) -> Self {
254 if (trd == 0) || (trb == 0) {
257 MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) }
260 fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self {
261 let bscale = (trb as i16) - (trd as i16);
262 let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 };
263 let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 };
268 pub const ZERO_MV: MV = MV { x: 0, y: 0 };
272 fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
277 fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
280 impl fmt::Display for MV {
281 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
282 write!(f, "{},{}", self.x, self.y)
294 fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } }
295 fn reset(&mut self, mb_w: usize) {
297 self.cbp.truncate(0);
298 self.cbp.resize(self.mb_w * 2, 0);
300 self.q.resize(self.mb_w * 2, 0);
302 fn update_row(&mut self) {
303 for i in 0..self.mb_w {
304 self.cbp[i] = self.cbp[self.mb_w + i];
305 self.q[i] = self.q[self.mb_w + i];
308 fn set_cbp(&mut self, mb_x: usize, cbp: u8) {
309 self.cbp[self.mb_w + mb_x] = cbp;
311 fn set_q(&mut self, mb_x: usize, q: u8) {
312 self.q[self.mb_w + mb_x] = q;
314 pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] }
315 pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool {
316 (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0
318 pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool {
319 let cbp = self.cbp[self.mb_w + mb_x];
320 let cbp_top = self.cbp[mb_x];
322 0 => { (cbp_top & 0b001000) != 0 },
323 1 => { (cbp_top & 0b000100) != 0 },
324 2 => { (cbp & 0b100000) != 0 },
325 3 => { (cbp & 0b010000) != 0 },
326 4 => { (cbp_top & 0b000010) != 0 },
327 _ => { (cbp_top & 0b000001) != 0 },