2 use std::ops::{Add, Sub};
3 use super::DecoderResult;
4 use frame::NAVideoBuffer;
10 #[cfg(feature="decoder_intel263")]
12 #[cfg(feature="decoder_realvideo1")]
15 pub trait BlockDecoder {
16 fn decode_pichdr(&mut self) -> DecoderResult<PicInfo>;
17 fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult<Slice>;
18 fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &Slice) -> DecoderResult<BlockInfo>;
19 fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>;
20 fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>;
21 fn is_slice_end(&mut self) -> bool;
23 fn filter_row(&mut self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo);
26 #[derive(Debug,Clone,Copy,PartialEq)]
32 #[derive(Debug,Clone,Copy)]
39 pub fn new(trb: u8, dbquant: u8) -> Self {
40 PBInfo{ trb: trb, dbquant: dbquant }
42 pub fn get_trb(&self) -> u8 { self.trb }
43 pub fn get_dbquant(&self) -> u8 { self.dbquant }
47 #[derive(Debug,Clone,Copy)]
62 pub fn new(w: usize, h: usize, mode: Type, quant: u8, apm: bool, mvmode: MVMode, ts: u8, pb: Option<PBInfo>, deblock: bool) -> Self {
63 PicInfo{ w: w, h: h, mode: mode, quant: quant, apm: apm, mvmode: mvmode, ts: ts, pb: pb, deblock: deblock }
65 pub fn get_width(&self) -> usize { self.w }
66 pub fn get_height(&self) -> usize { self.h }
67 pub fn get_mode(&self) -> Type { self.mode }
68 pub fn get_quant(&self) -> u8 { self.quant }
69 pub fn get_apm(&self) -> bool { self.apm }
70 pub fn get_mvmode(&self) -> MVMode { self.mvmode }
71 pub fn is_pb(&self) -> bool { self.pb.is_some() }
72 pub fn get_ts(&self) -> u8 { self.ts }
73 pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() }
76 #[derive(Debug,Clone,Copy)]
84 pub fn new(mb_x: usize, mb_y: usize, quant: u8) -> Self {
85 Slice{ mb_x: mb_x, mb_y: mb_y, quant: quant }
87 pub fn get_default_slice(pinfo: &PicInfo) -> Self {
88 Slice{ mb_x: 0, mb_y: 0, quant: pinfo.get_quant() }
90 pub fn get_quant(&self) -> u8 { self.quant }
93 #[derive(Debug,Clone,Copy)]
94 pub struct BlockInfo {
110 #[derive(Debug,Clone,Copy)]
111 pub struct BBlockInfo {
120 pub fn new(mode: Type, cbp: u8, q: u8) -> Self {
122 intra: mode == Type::I,
123 skip: (cbp == 0) && (mode != Type::I),
127 mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)],
131 mv2: [ZERO_MV, ZERO_MV],
136 pub fn is_intra(&self) -> bool { self.intra }
137 pub fn is_skipped(&self) -> bool { self.skip }
138 pub fn get_mode(&self) -> Type { self.mode }
139 pub fn get_cbp(&self) -> u8 { self.cbp }
140 pub fn get_q(&self) -> u8 { self.q }
141 pub fn get_num_mvs(&self) -> usize { self.num_mv }
142 pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] }
143 pub fn has_b_part(&self) -> bool { self.bpart }
144 pub fn get_cbp_b(&self) -> u8 { self.b_cbp }
145 pub fn get_num_mvs2(&self) -> usize { self.num_mv2 }
146 pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] }
147 pub fn set_mv(&mut self, mvs: &[MV]) {
148 if mvs.len() > 0 { self.skip = false; }
149 let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)];
150 for i in 0..mvs.len() { mv_arr[i] = mvs[i]; }
152 self.num_mv = mvs.len();
154 pub fn set_bpart(&mut self, bbinfo: BBlockInfo) {
155 self.bpart = bbinfo.present;
156 self.b_cbp = bbinfo.cbp;
157 self.fwd = bbinfo.fwd;
158 self.num_mv2 = bbinfo.get_num_mv();
160 pub fn set_b_mv(&mut self, mvs: &[MV]) {
161 if mvs.len() > 0 { self.skip = false; }
162 let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV];
163 for i in 0..mvs.len() { mv_arr[i] = mvs[i]; }
166 pub fn is_b_fwd(&self) -> bool { self.fwd }
170 pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self {
178 pub fn get_num_mv(&self) -> usize { self.num_mv }
181 #[derive(Debug,Clone,Copy)]
188 #[derive(Debug,Clone,Copy)]
195 pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } }
196 pub fn pred(a: MV, b: MV, c: MV) -> Self {
202 if a.x < c.x { x = c.x; } else { x = a.x; }
206 if a.x < c.x { x = a.x; } else { x = c.x; }
216 if a.y < c.y { y = c.y; } else { y = a.y; }
220 if a.y < c.y { y = a.y; } else { y = c.y; }
227 fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> Self {
228 let mut new_mv = pred_mv + add;
231 if new_mv.x >= 64 { new_mv.x -= 64; }
232 else if new_mv.x <= -64 { new_mv.x += 64; }
233 if new_mv.y >= 64 { new_mv.y -= 64; }
234 else if new_mv.y <= -64 { new_mv.y += 64; }
237 if new_mv.x > 31 { new_mv.x -= 64; }
238 else if new_mv.x < -32 { new_mv.x += 64; }
239 if new_mv.y > 31 { new_mv.y -= 64; }
240 else if new_mv.y < -32 { new_mv.y += 64; }
243 if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; }
244 if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; }
245 if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; }
246 if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; }
251 fn scale(&self, trb: u8, trd: u8) -> Self {
252 if (trd == 0) || (trb == 0) {
255 MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) }
258 fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self {
259 let bscale = (trb as i16) - (trd as i16);
260 let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 };
261 let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 };
266 pub const ZERO_MV: MV = MV { x: 0, y: 0 };
270 fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
275 fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
278 impl fmt::Display for MV {
279 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
280 write!(f, "{},{}", self.x, self.y)
292 fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } }
293 fn reset(&mut self, mb_w: usize) {
295 self.cbp.truncate(0);
296 self.cbp.resize(self.mb_w * 2, 0);
298 self.q.resize(self.mb_w * 2, 0);
300 fn update_row(&mut self) {
301 for i in 0..self.mb_w {
302 self.cbp[i] = self.cbp[self.mb_w + i];
303 self.q[i] = self.q[self.mb_w + i];
306 fn set_cbp(&mut self, mb_x: usize, cbp: u8) {
307 self.cbp[self.mb_w + mb_x] = cbp;
309 fn set_q(&mut self, mb_x: usize, q: u8) {
310 self.q[self.mb_w + mb_x] = q;
312 pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] }
313 pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool {
314 (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0
316 pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool {
317 let cbp = self.cbp[self.mb_w + mb_x];
318 let cbp_top = self.cbp[mb_x];
320 0 => { (cbp_top & 0b001000) != 0 },
321 1 => { (cbp_top & 0b000100) != 0 },
322 2 => { (cbp & 0b100000) != 0 },
323 3 => { (cbp & 0b010000) != 0 },
324 4 => { (cbp_top & 0b000010) != 0 },
325 _ => { (cbp_top & 0b000001) != 0 },