| 1 | use nihav_core::codecs::DecoderResult; |
| 2 | use super::{MV, ZERO_MV}; |
| 3 | use nihav_core::frame::{NAVideoBuffer, NAVideoBufferRef}; |
| 4 | |
| 5 | #[allow(clippy::erasing_op)] |
| 6 | #[allow(clippy::many_single_char_names)] |
| 7 | pub mod code; |
| 8 | pub mod data; |
| 9 | #[allow(clippy::needless_range_loop)] |
| 10 | pub mod decoder; |
| 11 | |
| 12 | pub trait BlockDecoder { |
| 13 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo>; |
| 14 | fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult<SliceInfo>; |
| 15 | fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo>; |
| 16 | fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; |
| 17 | fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; |
| 18 | fn is_slice_end(&mut self) -> bool; |
| 19 | } |
| 20 | |
| 21 | pub trait BlockDSP { |
| 22 | fn idct(&self, blk: &mut [i16; 64]); |
| 23 | fn copy_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV); |
| 24 | fn copy_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]); |
| 25 | fn avg_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV); |
| 26 | fn avg_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]); |
| 27 | fn filter_row(&self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo); |
| 28 | } |
| 29 | |
| 30 | #[allow(dead_code)] |
| 31 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 32 | pub enum Type { |
| 33 | I, P, PB, Skip, B, Special |
| 34 | } |
| 35 | |
| 36 | impl Type { |
| 37 | pub fn is_ref(self) -> bool { |
| 38 | match self { |
| 39 | Type::I | Type::P | Type::PB => true, |
| 40 | _ => false, |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[allow(dead_code)] |
| 46 | #[derive(Debug,Clone,Copy)] |
| 47 | pub struct PBInfo { |
| 48 | trb: u8, |
| 49 | dbquant: u8, |
| 50 | improved: bool, |
| 51 | } |
| 52 | |
| 53 | impl PBInfo { |
| 54 | pub fn new(trb: u8, dbquant: u8, improved: bool) -> Self { |
| 55 | PBInfo{ trb, dbquant, improved } |
| 56 | } |
| 57 | pub fn get_trb(self) -> u8 { self.trb } |
| 58 | pub fn get_dbquant(self) -> u8 { self.dbquant } |
| 59 | pub fn is_improved(self) -> bool { self.improved } |
| 60 | } |
| 61 | |
| 62 | #[allow(dead_code)] |
| 63 | #[derive(Debug,Clone,Copy)] |
| 64 | pub struct PicInfo { |
| 65 | pub w: usize, |
| 66 | pub h: usize, |
| 67 | pub mode: Type, |
| 68 | pub mvmode: MVMode, |
| 69 | pub umv: bool, |
| 70 | pub apm: bool, |
| 71 | pub quant: u8, |
| 72 | pub pb: Option<PBInfo>, |
| 73 | pub ts: u16, |
| 74 | pub plusinfo: Option<PlusInfo>, |
| 75 | } |
| 76 | |
| 77 | #[allow(dead_code)] |
| 78 | impl PicInfo { |
| 79 | pub fn new(w: usize, h: usize, mode: Type, mvmode: MVMode, umv: bool, apm: bool, quant: u8, ts: u16, pb: Option<PBInfo>, plusinfo: Option<PlusInfo>) -> Self { |
| 80 | PicInfo { |
| 81 | w, h, mode, mvmode, |
| 82 | umv, apm, quant, |
| 83 | pb, ts, plusinfo |
| 84 | } |
| 85 | } |
| 86 | pub fn get_width(&self) -> usize { self.w } |
| 87 | pub fn get_height(&self) -> usize { self.h } |
| 88 | pub fn get_mode(&self) -> Type { self.mode } |
| 89 | pub fn get_quant(&self) -> u8 { self.quant } |
| 90 | pub fn get_apm(&self) -> bool { self.apm } |
| 91 | pub fn is_pb(&self) -> bool { self.pb.is_some() } |
| 92 | pub fn is_ipb(&self) -> bool { |
| 93 | if let Some(ref pbi) = self.pb { |
| 94 | pbi.is_improved() |
| 95 | } else { |
| 96 | false |
| 97 | } |
| 98 | } |
| 99 | pub fn get_ts(&self) -> u16 { self.ts } |
| 100 | pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() } |
| 101 | pub fn get_plusifo(&self) -> Option<PlusInfo> { self.plusinfo } |
| 102 | pub fn get_mvmode(&self) -> MVMode { self.mvmode } |
| 103 | } |
| 104 | |
| 105 | #[allow(dead_code)] |
| 106 | #[derive(Debug,Clone,Copy)] |
| 107 | pub struct PlusInfo { |
| 108 | pub aic: bool, |
| 109 | pub deblock: bool, |
| 110 | pub aiv_mode: bool, |
| 111 | pub mq_mode: bool, |
| 112 | } |
| 113 | |
| 114 | impl PlusInfo { |
| 115 | pub fn new(aic: bool, deblock: bool, aiv_mode: bool, mq_mode: bool) -> Self { |
| 116 | PlusInfo { aic, deblock, aiv_mode, mq_mode } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[allow(dead_code)] |
| 121 | #[derive(Debug,Clone,Copy)] |
| 122 | pub struct SliceInfo { |
| 123 | pub mb_x: usize, |
| 124 | pub mb_y: usize, |
| 125 | pub mb_end: usize, |
| 126 | pub quant: u8, |
| 127 | } |
| 128 | |
| 129 | #[allow(dead_code)] |
| 130 | #[derive(Debug,Clone,Copy)] |
| 131 | pub struct SliceState { |
| 132 | pub is_iframe: bool, |
| 133 | pub mb_x: usize, |
| 134 | pub mb_y: usize, |
| 135 | pub first_line: bool, |
| 136 | pub first_mb: bool, |
| 137 | pub slice_mb_x: usize, |
| 138 | pub slice_mb_y: usize, |
| 139 | pub quant: u8, |
| 140 | } |
| 141 | |
| 142 | const SLICE_NO_END: usize = 99999999; |
| 143 | |
| 144 | impl SliceInfo { |
| 145 | pub fn new(mb_x: usize, mb_y: usize, mb_end: usize, quant: u8) -> Self { |
| 146 | SliceInfo{ mb_x, mb_y, mb_end, quant } |
| 147 | } |
| 148 | pub fn new_gob(mb_x: usize, mb_y: usize, quant: u8) -> Self { |
| 149 | SliceInfo{ mb_x, mb_y, mb_end: SLICE_NO_END, quant } |
| 150 | } |
| 151 | pub fn get_default_slice(pinfo: &PicInfo) -> Self { |
| 152 | SliceInfo{ mb_x: 0, mb_y: 0, mb_end: SLICE_NO_END, quant: pinfo.get_quant() } |
| 153 | } |
| 154 | pub fn get_quant(&self) -> u8 { self.quant } |
| 155 | pub fn is_at_end(&self, mb_pos: usize) -> bool { self.mb_end == mb_pos } |
| 156 | pub fn needs_check(&self) -> bool { self.mb_end == SLICE_NO_END } |
| 157 | } |
| 158 | |
| 159 | impl SliceState { |
| 160 | pub fn new(is_iframe: bool) -> Self { |
| 161 | SliceState { |
| 162 | is_iframe, mb_x: 0, mb_y: 0, first_line: true, first_mb: true, |
| 163 | slice_mb_x: 0, slice_mb_y: 0, quant: 0 |
| 164 | } |
| 165 | } |
| 166 | pub fn next_mb(&mut self) { |
| 167 | self.mb_x += 1; self.first_mb = false; |
| 168 | if self.mb_x >= self.slice_mb_x && self.mb_y > self.slice_mb_y { |
| 169 | self.first_line = false; |
| 170 | } |
| 171 | } |
| 172 | pub fn new_row(&mut self) { |
| 173 | self.mb_x = 0; self.mb_y += 1; |
| 174 | if self.mb_x >= self.slice_mb_x && self.mb_y > self.slice_mb_y { |
| 175 | self.first_line = false; |
| 176 | } |
| 177 | self.first_mb = true; |
| 178 | } |
| 179 | pub fn reset_slice(&mut self, smb_x: usize, smb_y: usize) { |
| 180 | self.slice_mb_x = smb_x; |
| 181 | self.slice_mb_y = smb_y; |
| 182 | self.first_line = true; |
| 183 | self.first_mb = true; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[derive(Debug,Clone,Copy)] |
| 188 | pub struct BlockInfo { |
| 189 | pub intra: bool, |
| 190 | pub skip: bool, |
| 191 | pub mode: Type, |
| 192 | pub cbp: u8, |
| 193 | pub q: u8, |
| 194 | pub mv: [MV; 4], |
| 195 | pub num_mv: usize, |
| 196 | pub bpart: bool, |
| 197 | pub b_cbp: u8, |
| 198 | pub mv2: [MV; 2], |
| 199 | pub num_mv2: usize, |
| 200 | pub fwd: bool, |
| 201 | pub acpred: ACPredMode, |
| 202 | } |
| 203 | |
| 204 | #[allow(dead_code)] |
| 205 | #[derive(Debug,Clone,Copy)] |
| 206 | pub struct BBlockInfo { |
| 207 | present: bool, |
| 208 | cbp: u8, |
| 209 | num_mv: usize, |
| 210 | fwd: bool, |
| 211 | } |
| 212 | |
| 213 | #[allow(non_camel_case_types)] |
| 214 | #[derive(Debug,Clone,Copy)] |
| 215 | pub enum BlockMVInfo { |
| 216 | Intra, |
| 217 | Inter_1MV(MV), |
| 218 | Inter_4MV([MV; 4]), |
| 219 | } |
| 220 | |
| 221 | #[allow(dead_code)] |
| 222 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 223 | pub enum ACPredMode { |
| 224 | None, |
| 225 | DC, |
| 226 | Ver, |
| 227 | Hor, |
| 228 | } |
| 229 | |
| 230 | #[allow(dead_code)] |
| 231 | impl BlockInfo { |
| 232 | pub fn new(mode: Type, cbp: u8, q: u8) -> Self { |
| 233 | BlockInfo { |
| 234 | intra: mode == Type::I, |
| 235 | skip: (cbp == 0) && (mode != Type::I), |
| 236 | mode, |
| 237 | cbp, |
| 238 | q, |
| 239 | mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)], |
| 240 | num_mv: 0, |
| 241 | bpart: false, |
| 242 | b_cbp: 0, |
| 243 | mv2: [ZERO_MV, ZERO_MV], |
| 244 | num_mv2: 0, |
| 245 | fwd: false, |
| 246 | acpred: ACPredMode::None, |
| 247 | } |
| 248 | } |
| 249 | pub fn is_intra(&self) -> bool { self.intra } |
| 250 | pub fn is_skipped(&self) -> bool { self.skip } |
| 251 | pub fn get_mode(&self) -> Type { self.mode } |
| 252 | pub fn get_cbp(&self) -> u8 { self.cbp } |
| 253 | pub fn get_q(&self) -> u8 { self.q } |
| 254 | pub fn get_num_mvs(&self) -> usize { self.num_mv } |
| 255 | pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] } |
| 256 | pub fn has_b_part(&self) -> bool { self.bpart } |
| 257 | pub fn get_cbp_b(&self) -> u8 { self.b_cbp } |
| 258 | pub fn get_num_mvs2(&self) -> usize { self.num_mv2 } |
| 259 | pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] } |
| 260 | pub fn set_mv(&mut self, mvs: &[MV]) { |
| 261 | if !mvs.is_empty() { self.skip = false; } |
| 262 | let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)]; |
| 263 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } |
| 264 | self.mv = mv_arr; |
| 265 | self.num_mv = mvs.len(); |
| 266 | } |
| 267 | pub fn set_bpart(&mut self, bbinfo: BBlockInfo) { |
| 268 | self.bpart = bbinfo.present; |
| 269 | self.b_cbp = bbinfo.cbp; |
| 270 | self.fwd = bbinfo.fwd; |
| 271 | self.num_mv2 = bbinfo.get_num_mv(); |
| 272 | } |
| 273 | pub fn set_b_mv(&mut self, mvs: &[MV]) { |
| 274 | if !mvs.is_empty() { self.skip = false; } |
| 275 | let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV]; |
| 276 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } |
| 277 | self.mv2 = mv_arr; |
| 278 | self.num_mv2 = mvs.len(); |
| 279 | } |
| 280 | pub fn is_b_fwd(&self) -> bool { self.fwd } |
| 281 | pub fn set_acpred(&mut self, acpred: ACPredMode) { self.acpred = acpred } |
| 282 | pub fn get_acpred(&self) -> ACPredMode { self.acpred } |
| 283 | } |
| 284 | |
| 285 | impl BBlockInfo { |
| 286 | pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self { |
| 287 | BBlockInfo { |
| 288 | present, |
| 289 | cbp, |
| 290 | num_mv, |
| 291 | fwd, |
| 292 | } |
| 293 | } |
| 294 | pub fn get_num_mv(&self) -> usize { self.num_mv } |
| 295 | } |
| 296 | |
| 297 | #[derive(Debug,Clone,Copy)] |
| 298 | pub enum MVMode { |
| 299 | Old, |
| 300 | Long, |
| 301 | UMV, |
| 302 | } |
| 303 | |
| 304 | pub trait H263MVTrait { |
| 305 | fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> MV; |
| 306 | fn scale(&self, trb: u16, trd: u16) -> MV; |
| 307 | fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u16, trd: u16) -> MV; |
| 308 | } |
| 309 | |
| 310 | impl H263MVTrait for MV { |
| 311 | fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> MV { |
| 312 | let mut new_mv = pred_mv + add; |
| 313 | match mvmode { |
| 314 | MVMode::Old => { |
| 315 | if new_mv.x >= 64 { new_mv.x -= 64; } |
| 316 | else if new_mv.x <= -64 { new_mv.x += 64; } |
| 317 | if new_mv.y >= 64 { new_mv.y -= 64; } |
| 318 | else if new_mv.y <= -64 { new_mv.y += 64; } |
| 319 | }, |
| 320 | MVMode::Long => { |
| 321 | if new_mv.x > 31 { new_mv.x -= 64; } |
| 322 | else if new_mv.x < -32 { new_mv.x += 64; } |
| 323 | if new_mv.y > 31 { new_mv.y -= 64; } |
| 324 | else if new_mv.y < -32 { new_mv.y += 64; } |
| 325 | }, |
| 326 | MVMode::UMV => { |
| 327 | if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } |
| 328 | if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } |
| 329 | if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } |
| 330 | if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } |
| 331 | }, |
| 332 | }; |
| 333 | new_mv |
| 334 | } |
| 335 | fn scale(&self, trb: u16, trd: u16) -> MV { |
| 336 | if (trd == 0) || (trb == 0) { |
| 337 | ZERO_MV |
| 338 | } else { |
| 339 | 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 } |
| 340 | } |
| 341 | } |
| 342 | fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u16, trd: u16) -> MV { |
| 343 | let bscale = (trb as i32) - (trd as i32); |
| 344 | 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 }; |
| 345 | 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 }; |
| 346 | MV { x, y } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | #[allow(dead_code)] |
| 351 | pub struct CBPInfo { |
| 352 | cbp: Vec<u8>, |
| 353 | q: Vec<u8>, |
| 354 | mb_w: usize, |
| 355 | } |
| 356 | |
| 357 | impl CBPInfo { |
| 358 | fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } } |
| 359 | fn reset(&mut self, mb_w: usize) { |
| 360 | self.mb_w = mb_w; |
| 361 | self.cbp.clear(); |
| 362 | self.cbp.resize(self.mb_w * 2, 0); |
| 363 | self.q.clear(); |
| 364 | self.q.resize(self.mb_w * 2, 0); |
| 365 | } |
| 366 | fn update_row(&mut self) { |
| 367 | for i in 0..self.mb_w { |
| 368 | self.cbp[i] = self.cbp[self.mb_w + i]; |
| 369 | self.q[i] = self.q[self.mb_w + i]; |
| 370 | } |
| 371 | } |
| 372 | fn set_cbp(&mut self, mb_x: usize, cbp: u8) { |
| 373 | self.cbp[self.mb_w + mb_x] = cbp; |
| 374 | } |
| 375 | fn set_q(&mut self, mb_x: usize, q: u8) { |
| 376 | self.q[self.mb_w + mb_x] = q; |
| 377 | } |
| 378 | pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] } |
| 379 | pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool { |
| 380 | (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0 |
| 381 | } |
| 382 | pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool { |
| 383 | let cbp = self.cbp[self.mb_w + mb_x]; |
| 384 | let cbp_top = self.cbp[mb_x]; |
| 385 | match blk_no { |
| 386 | 0 => { (cbp_top & 0b001000) != 0 }, |
| 387 | 1 => { (cbp_top & 0b000100) != 0 }, |
| 388 | 2 => { (cbp & 0b100000) != 0 }, |
| 389 | 3 => { (cbp & 0b010000) != 0 }, |
| 390 | 4 => { (cbp_top & 0b000010) != 0 }, |
| 391 | _ => { (cbp_top & 0b000001) != 0 }, |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |