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