| 1 | use std::fmt; |
| 2 | use std::ops::{Add, Sub}; |
| 3 | use super::DecoderResult; |
| 4 | use frame::NAVideoBuffer; |
| 5 | |
| 6 | pub mod code; |
| 7 | pub mod data; |
| 8 | pub mod decoder; |
| 9 | |
| 10 | #[cfg(feature="decoder_intel263")] |
| 11 | pub mod intel263; |
| 12 | |
| 13 | pub trait BlockDecoder { |
| 14 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo>; |
| 15 | fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult<Slice>; |
| 16 | fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &Slice) -> DecoderResult<BlockInfo>; |
| 17 | fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; |
| 18 | fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; |
| 19 | fn is_slice_end(&mut self) -> bool; |
| 20 | |
| 21 | fn filter_row(&mut self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo); |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 25 | pub enum Type { |
| 26 | I, P, Skip, Special |
| 27 | } |
| 28 | |
| 29 | #[allow(dead_code)] |
| 30 | #[derive(Debug,Clone,Copy)] |
| 31 | pub struct PBInfo { |
| 32 | trb: u8, |
| 33 | dbquant: u8, |
| 34 | } |
| 35 | |
| 36 | impl PBInfo { |
| 37 | pub fn new(trb: u8, dbquant: u8) -> Self { |
| 38 | PBInfo{ trb: trb, dbquant: dbquant } |
| 39 | } |
| 40 | pub fn get_trb(&self) -> u8 { self.trb } |
| 41 | pub fn get_dbquant(&self) -> u8 { self.dbquant } |
| 42 | } |
| 43 | |
| 44 | #[allow(dead_code)] |
| 45 | #[derive(Debug,Clone,Copy)] |
| 46 | pub struct PicInfo { |
| 47 | w: usize, |
| 48 | h: usize, |
| 49 | mode: Type, |
| 50 | quant: u8, |
| 51 | apm: bool, |
| 52 | umv: bool, |
| 53 | pb: Option<PBInfo>, |
| 54 | ts: u8, |
| 55 | deblock: bool, |
| 56 | } |
| 57 | |
| 58 | #[allow(dead_code)] |
| 59 | impl PicInfo { |
| 60 | pub fn new(w: usize, h: usize, mode: Type, quant: u8, apm: bool, umv: bool, ts: u8, pb: Option<PBInfo>, deblock: bool) -> Self { |
| 61 | PicInfo{ w: w, h: h, mode: mode, quant: quant, apm: apm, umv: umv, ts: ts, pb: pb, deblock: deblock } |
| 62 | } |
| 63 | pub fn get_width(&self) -> usize { self.w } |
| 64 | pub fn get_height(&self) -> usize { self.h } |
| 65 | pub fn get_mode(&self) -> Type { self.mode } |
| 66 | pub fn get_quant(&self) -> u8 { self.quant } |
| 67 | pub fn get_apm(&self) -> bool { self.apm } |
| 68 | pub fn get_umv(&self) -> bool { self.umv } |
| 69 | pub fn is_pb(&self) -> bool { self.pb.is_some() } |
| 70 | pub fn get_ts(&self) -> u8 { self.ts } |
| 71 | pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() } |
| 72 | } |
| 73 | |
| 74 | #[derive(Debug,Clone,Copy)] |
| 75 | pub struct Slice { |
| 76 | mb_x: usize, |
| 77 | mb_y: usize, |
| 78 | quant: u8, |
| 79 | } |
| 80 | |
| 81 | impl Slice { |
| 82 | pub fn new(mb_x: usize, mb_y: usize, quant: u8) -> Self { |
| 83 | Slice{ mb_x: mb_x, mb_y: mb_y, quant: quant } |
| 84 | } |
| 85 | pub fn get_default_slice(pinfo: &PicInfo) -> Self { |
| 86 | Slice{ mb_x: 0, mb_y: 0, quant: pinfo.get_quant() } |
| 87 | } |
| 88 | pub fn get_quant(&self) -> u8 { self.quant } |
| 89 | } |
| 90 | |
| 91 | #[derive(Debug,Clone,Copy)] |
| 92 | pub struct BlockInfo { |
| 93 | intra: bool, |
| 94 | skip: bool, |
| 95 | mode: Type, |
| 96 | cbp: u8, |
| 97 | q: u8, |
| 98 | mv: [MV; 4], |
| 99 | num_mv: usize, |
| 100 | bpart: bool, |
| 101 | b_cbp: u8, |
| 102 | mv2: [MV; 2], |
| 103 | num_mv2: usize, |
| 104 | fwd: bool, |
| 105 | } |
| 106 | |
| 107 | #[allow(dead_code)] |
| 108 | #[derive(Debug,Clone,Copy)] |
| 109 | pub struct BBlockInfo { |
| 110 | present: bool, |
| 111 | cbp: u8, |
| 112 | num_mv: usize, |
| 113 | fwd: bool, |
| 114 | } |
| 115 | |
| 116 | #[allow(dead_code)] |
| 117 | impl BlockInfo { |
| 118 | pub fn new(mode: Type, cbp: u8, q: u8) -> Self { |
| 119 | BlockInfo { |
| 120 | intra: mode == Type::I, |
| 121 | skip: (cbp == 0) && (mode != Type::I), |
| 122 | mode: mode, |
| 123 | cbp: cbp, |
| 124 | q: q, |
| 125 | mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)], |
| 126 | num_mv: 0, |
| 127 | bpart: false, |
| 128 | b_cbp: 0, |
| 129 | mv2: [ZERO_MV, ZERO_MV], |
| 130 | num_mv2: 0, |
| 131 | fwd: false, |
| 132 | } |
| 133 | } |
| 134 | pub fn is_intra(&self) -> bool { self.intra } |
| 135 | pub fn is_skipped(&self) -> bool { self.skip } |
| 136 | pub fn get_mode(&self) -> Type { self.mode } |
| 137 | pub fn get_cbp(&self) -> u8 { self.cbp } |
| 138 | pub fn get_q(&self) -> u8 { self.q } |
| 139 | pub fn get_num_mvs(&self) -> usize { self.num_mv } |
| 140 | pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] } |
| 141 | pub fn has_b_part(&self) -> bool { self.bpart } |
| 142 | pub fn get_cbp_b(&self) -> u8 { self.b_cbp } |
| 143 | pub fn get_num_mvs2(&self) -> usize { self.num_mv2 } |
| 144 | pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] } |
| 145 | pub fn set_mv(&mut self, mvs: &[MV]) { |
| 146 | if mvs.len() > 0 { self.skip = false; } |
| 147 | let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)]; |
| 148 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } |
| 149 | self.mv = mv_arr; |
| 150 | self.num_mv = mvs.len(); |
| 151 | } |
| 152 | pub fn set_bpart(&mut self, bbinfo: BBlockInfo) { |
| 153 | self.bpart = bbinfo.present; |
| 154 | self.b_cbp = bbinfo.cbp; |
| 155 | self.fwd = bbinfo.fwd; |
| 156 | self.num_mv2 = bbinfo.get_num_mv(); |
| 157 | } |
| 158 | pub fn set_b_mv(&mut self, mvs: &[MV]) { |
| 159 | if mvs.len() > 0 { self.skip = false; } |
| 160 | let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV]; |
| 161 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } |
| 162 | self.mv2 = mv_arr; |
| 163 | } |
| 164 | pub fn is_b_fwd(&self) -> bool { self.fwd } |
| 165 | } |
| 166 | |
| 167 | impl BBlockInfo { |
| 168 | pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self { |
| 169 | BBlockInfo { |
| 170 | present: present, |
| 171 | cbp: cbp, |
| 172 | num_mv: num_mv, |
| 173 | fwd: fwd, |
| 174 | } |
| 175 | } |
| 176 | pub fn get_num_mv(&self) -> usize { self.num_mv } |
| 177 | } |
| 178 | |
| 179 | #[derive(Debug,Clone,Copy)] |
| 180 | pub struct MV { |
| 181 | x: i16, |
| 182 | y: i16, |
| 183 | } |
| 184 | |
| 185 | impl MV { |
| 186 | pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } } |
| 187 | pub fn pred(a: MV, b: MV, c: MV) -> Self { |
| 188 | let x; |
| 189 | if a.x < b.x { |
| 190 | if b.x < c.x { |
| 191 | x = b.x; |
| 192 | } else { |
| 193 | if a.x < c.x { x = c.x; } else { x = a.x; } |
| 194 | } |
| 195 | } else { |
| 196 | if b.x < c.x { |
| 197 | if a.x < c.x { x = a.x; } else { x = c.x; } |
| 198 | } else { |
| 199 | x = b.x; |
| 200 | } |
| 201 | } |
| 202 | let y; |
| 203 | if a.y < b.y { |
| 204 | if b.y < c.y { |
| 205 | y = b.y; |
| 206 | } else { |
| 207 | if a.y < c.y { y = c.y; } else { y = a.y; } |
| 208 | } |
| 209 | } else { |
| 210 | if b.y < c.y { |
| 211 | if a.y < c.y { y = a.y; } else { y = c.y; } |
| 212 | } else { |
| 213 | y = b.y; |
| 214 | } |
| 215 | } |
| 216 | MV { x: x, y: y } |
| 217 | } |
| 218 | fn add_umv(pred_mv: MV, add: MV, umv: bool) -> Self { |
| 219 | let mut new_mv = pred_mv + add; |
| 220 | if umv { |
| 221 | if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } |
| 222 | if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } |
| 223 | if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } |
| 224 | if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } |
| 225 | } else { |
| 226 | if new_mv.x > 31 { new_mv.x -= 64; } |
| 227 | else if new_mv.x < -32 { new_mv.x += 64; } |
| 228 | if new_mv.y > 31 { new_mv.y -= 64; } |
| 229 | else if new_mv.y < -32 { new_mv.y += 64; } |
| 230 | } |
| 231 | new_mv |
| 232 | } |
| 233 | fn scale(&self, trb: u8, trd: u8) -> Self { |
| 234 | if (trd == 0) || (trb == 0) { |
| 235 | ZERO_MV |
| 236 | } else { |
| 237 | MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) } |
| 238 | } |
| 239 | } |
| 240 | fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self { |
| 241 | let bscale = (trb as i16) - (trd as i16); |
| 242 | let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 }; |
| 243 | let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 }; |
| 244 | MV { x: x, y: y } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | pub const ZERO_MV: MV = MV { x: 0, y: 0 }; |
| 249 | |
| 250 | impl Add for MV { |
| 251 | type Output = MV; |
| 252 | fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } } |
| 253 | } |
| 254 | |
| 255 | impl Sub for MV { |
| 256 | type Output = MV; |
| 257 | fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } } |
| 258 | } |
| 259 | |
| 260 | impl fmt::Display for MV { |
| 261 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 262 | write!(f, "{},{}", self.x, self.y) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #[allow(dead_code)] |
| 267 | pub struct CBPInfo { |
| 268 | cbp: Vec<u8>, |
| 269 | q: Vec<u8>, |
| 270 | mb_w: usize, |
| 271 | } |
| 272 | |
| 273 | impl CBPInfo { |
| 274 | fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } } |
| 275 | fn reset(&mut self, mb_w: usize) { |
| 276 | self.mb_w = mb_w; |
| 277 | self.cbp.truncate(0); |
| 278 | self.cbp.resize(self.mb_w * 2, 0); |
| 279 | self.q.truncate(0); |
| 280 | self.q.resize(self.mb_w * 2, 0); |
| 281 | } |
| 282 | fn update_row(&mut self) { |
| 283 | for i in 0..self.mb_w { |
| 284 | self.cbp[i] = self.cbp[self.mb_w + i]; |
| 285 | self.q[i] = self.q[self.mb_w + i]; |
| 286 | } |
| 287 | } |
| 288 | fn set_cbp(&mut self, mb_x: usize, cbp: u8) { |
| 289 | self.cbp[self.mb_w + mb_x] = cbp; |
| 290 | } |
| 291 | fn set_q(&mut self, mb_x: usize, q: u8) { |
| 292 | self.q[self.mb_w + mb_x] = q; |
| 293 | } |
| 294 | pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] } |
| 295 | pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool { |
| 296 | (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0 |
| 297 | } |
| 298 | pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool { |
| 299 | let cbp = self.cbp[self.mb_w + mb_x]; |
| 300 | let cbp_top = self.cbp[mb_x]; |
| 301 | match blk_no { |
| 302 | 0 => { (cbp_top & 0b001000) != 0 }, |
| 303 | 1 => { (cbp_top & 0b000100) != 0 }, |
| 304 | 2 => { (cbp & 0b100000) != 0 }, |
| 305 | 3 => { (cbp & 0b010000) != 0 }, |
| 306 | 4 => { (cbp_top & 0b000010) != 0 }, |
| 307 | _ => { (cbp_top & 0b000001) != 0 }, |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |