2 use nihav_core::codecs::DecoderError;
3 use nihav_core::frame::*;
5 use super::super::blockdsp;
8 use nihav_core::formats;
21 fn new() -> Self { MVInfo{ mv: Vec::new(), mb_w: 0, mb_stride: 0, mb_start: 0, top: true, mvmode: MVMode::Old } }
22 fn reset(&mut self, mb_w: usize, mb_start: usize, mvmode: MVMode) {
23 self.mb_start = mb_start;
25 self.mb_stride = mb_w * 2 + 2;
26 self.mv.resize(self.mb_stride * 3, ZERO_MV);
29 fn update_row(&mut self) {
30 self.mb_start = self.mb_w + 1;
32 for i in 0..self.mb_stride {
33 self.mv[i] = self.mv[self.mb_stride * 2 + i];
36 #[allow(non_snake_case)]
37 fn predict(&mut self, mb_x: usize, blk_no: usize, use4: bool, diff: MV, first_line: bool, first_mb: bool) -> MV {
41 let last = mb_x == self.mb_w - 1;
44 if mb_x != self.mb_start {
45 A = if !first_mb { self.mv[self.mb_stride + mb_x * 2 - 1] } else { ZERO_MV };
46 B = if !first_line { self.mv[ mb_x * 2] } else { A };
47 C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV };
49 A = ZERO_MV; B = ZERO_MV; C = ZERO_MV;
53 A = self.mv[self.mb_stride + mb_x * 2];
54 B = if !first_line { self.mv[mb_x * 2 + 1] } else { A };
55 C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV/*A*/ };
58 A = if mb_x != self.mb_start { self.mv[self.mb_stride * 2 + mb_x * 2 - 1] } else { ZERO_MV };
59 B = self.mv[self.mb_stride + mb_x * 2];
60 C = self.mv[self.mb_stride + mb_x * 2 + 1];
63 A = self.mv[self.mb_stride * 2 + mb_x * 2];
64 B = self.mv[self.mb_stride * 1 + mb_x * 2 + 1];
65 C = self.mv[self.mb_stride * 1 + mb_x * 2];
67 _ => { return ZERO_MV; }
69 let pred_mv = MV::pred(A, B, C);
70 let new_mv = MV::add_umv(pred_mv, diff, self.mvmode);
72 self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = new_mv;
73 self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = new_mv;
74 self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = new_mv;
75 self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = new_mv;
78 0 => { self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = new_mv; },
79 1 => { self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = new_mv; },
80 2 => { self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = new_mv; },
81 3 => { self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = new_mv; },
88 fn set_zero_mv(&mut self, mb_x: usize) {
89 self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = ZERO_MV;
90 self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = ZERO_MV;
91 self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = ZERO_MV;
92 self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = ZERO_MV;
94 fn get_mv(&self, mb_x: usize, blk_no: usize) -> MV {
95 self.mv[self.mb_stride + mb_x * 2 + (blk_no & 1) + (blk_no >> 1) * self.mb_stride]
100 #[derive(Clone,Copy)]
111 fn new() -> Self { BMB {blk: [[0; 64]; 6], cbp: 0, fwd: false, mv_f: [ZERO_MV; 4], mv_b: [ZERO_MV; 4], num_mv: 0} }
114 #[derive(Clone,Copy)]
120 const ZERO_PRED_COEFFS: PredCoeffs = PredCoeffs { hor: [[0; 8]; 6], ver: [[0; 8]; 6] };
122 pub struct H263BaseDecoder {
135 pred_coeffs: Vec<PredCoeffs>,
138 may_have_b_frames: bool,
139 mv_data: Vec<BlockMVInfo>,
143 fn clip_dc(dc: i16) -> i16 {
145 else if dc > 2046 { 2047 }
150 fn clip_ac(ac: i16) -> i16 {
151 if ac < -2048 { -2048 }
152 else if ac > 2047 { 2047 }
157 impl H263BaseDecoder {
158 pub fn new_with_opts(is_gob: bool, slice_reset: bool, may_have_b_frames: bool) -> Self {
160 w: 0, h: 0, mb_w: 0, mb_h: 0, num_mb: 0,
161 ftype: Type::Special,
162 ipbs: IPBShuffler::new(),
163 last_ts: 0, next_ts: 0, tsdiff: 0,
164 has_b: false, b_data: Vec::new(),
165 pred_coeffs: Vec::new(),
171 pub fn new(is_gob: bool) -> Self {
172 Self::new_with_opts(is_gob, true, false)
174 pub fn new_b_frames(is_gob: bool) -> Self {
175 Self::new_with_opts(is_gob, true, true)
178 pub fn is_intra(&self) -> bool { self.ftype == Type::I }
179 pub fn get_frame_type(&self) -> FrameType {
181 Type::I => FrameType::I,
182 Type::P => FrameType::P,
183 Type::B => FrameType::B,
184 Type::PB => FrameType::P,
185 Type::Skip => FrameType::Skip,
186 Type::Special => FrameType::Skip,
189 pub fn get_dimensions(&self) -> (usize, usize) { (self.w, self.h) }
191 pub fn parse_frame(&mut self, bd: &mut BlockDecoder, bdsp: &BlockDSP) -> DecoderResult<NABufferType> {
192 let pinfo = bd.decode_pichdr()?;
193 let mut mvi = MVInfo::new();
194 let mut mvi2 = MVInfo::new();
195 let mut cbpi = CBPInfo::new();
197 //todo handle res change
200 self.mb_w = (pinfo.w + 15) >> 4;
201 self.mb_h = (pinfo.h + 15) >> 4;
202 self.num_mb = self.mb_w * self.mb_h;
203 self.ftype = pinfo.mode;
204 self.has_b = pinfo.is_pb();
207 self.mv_data.truncate(0);
210 let save_b_data = pinfo.mode.is_ref() && self.may_have_b_frames;
212 self.mv_data.truncate(0);
214 let is_b = pinfo.mode == Type::B;
216 if is_b && (self.mv_data.len() < self.mb_w * self.mb_h) {
217 return Err(DecoderError::MissingReference);
220 let tsdiff = if pinfo.is_pb() { pinfo.ts.wrapping_sub(self.last_ts) >> 1 }
221 else { self.last_ts.wrapping_sub(self.next_ts) >> 1 };
222 let bsdiff = if pinfo.is_pb() { (pinfo.get_pbinfo().get_trb() as u16) << 7 }
223 else { pinfo.ts.wrapping_sub(self.next_ts) >> 1 };
225 let fmt = formats::YUV420_FORMAT;
226 let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
227 let bufinfo = alloc_video_buffer(vinfo, 4)?;
228 let mut buf = bufinfo.get_vbuf().unwrap();
230 let mut slice = if self.is_gob {
231 SliceInfo::get_default_slice(&pinfo)
233 bd.decode_slice_header(&pinfo)?
235 mvi.reset(self.mb_w, 0, pinfo.get_mvmode());
236 if is_b || pinfo.is_pb() {
237 mvi2.reset(self.mb_w, 0, pinfo.get_mvmode());
239 cbpi.reset(self.mb_w);
241 let mut blk: [[i16; 64]; 6] = [[0; 64]; 6];
242 let mut sstate = SliceState::new(pinfo.mode == Type::I);
244 let apply_acpred = (pinfo.mode == Type::I) && pinfo.plusinfo.is_some() && pinfo.plusinfo.unwrap().aic;
246 self.pred_coeffs.truncate(0);
247 self.pred_coeffs.resize(self.mb_w * self.mb_h, ZERO_PRED_COEFFS);
249 sstate.quant = slice.quant;
250 for mb_y in 0..self.mb_h {
251 for mb_x in 0..self.mb_w {
252 for i in 0..6 { for j in 0..64 { blk[i][j] = 0; } }
254 if slice.is_at_end(mb_pos) || (slice.needs_check() && mb_pos > 0 && bd.is_slice_end()) {
255 slice = bd.decode_slice_header(&pinfo)?;
256 if !self.is_gob && self.slice_reset {
257 mvi.reset(self.mb_w, mb_x, pinfo.get_mvmode());
258 if is_b || pinfo.is_pb() {
259 mvi2.reset(self.mb_w, mb_x, pinfo.get_mvmode());
261 cbpi.reset(self.mb_w);
262 sstate.reset_slice(mb_x, mb_y);
263 sstate.quant = slice.quant;
267 let binfo = bd.decode_block_header(&pinfo, &slice, &sstate)?;
268 let cbp = binfo.get_cbp();
269 cbpi.set_cbp(mb_x, cbp);
270 cbpi.set_q(mb_x, binfo.get_q());
271 sstate.quant = binfo.get_q();
272 if binfo.is_intra() {
274 self.mv_data.push(BlockMVInfo::Intra);
277 bd.decode_block_intra(&binfo, &sstate, binfo.get_q(), i, (cbp & (1 << (5 - i))) != 0, &mut blk[i])?;
278 if apply_acpred && (binfo.acpred != ACPredMode::None) {
279 let has_b = (i == 1) || (i == 3) || !sstate.first_mb;
280 let has_a = (i == 2) || (i == 3) || !sstate.first_line;
281 let (b_mb, b_blk) = if has_b {
282 if (i == 1) || (i == 3) {
290 let (a_mb, a_blk) = if has_a {
291 if (i == 2) || (i == 3) {
294 (mb_pos - self.mb_w, i + 2)
296 (mb_pos - self.mb_w, i)
303 dc = (self.pred_coeffs[b_mb].hor[b_blk][0] + self.pred_coeffs[a_mb].ver[a_blk][0]) / 2;
305 dc = self.pred_coeffs[a_mb].ver[a_blk][0];
307 dc = self.pred_coeffs[b_mb].hor[b_blk][0];
311 blk[i][0] = clip_dc(blk[i][0] + dc);
316 blk[i][k * 8] += self.pred_coeffs[b_mb].hor[b_blk][k];
319 blk[i][k * 8] = clip_ac(blk[i][k * 8]);
324 blk[i][0] = clip_dc(blk[i][0]);
329 blk[i][k] += self.pred_coeffs[a_mb].ver[a_blk][k];
332 blk[i][k] = clip_ac(blk[i][k]);
337 blk[i][0] = clip_dc(blk[i][0]);
339 ACPredMode::None => {},
341 for t in 0..8 { self.pred_coeffs[mb_pos].hor[i][t] = blk[i][t * 8]; }
342 for t in 0..8 { self.pred_coeffs[mb_pos].ver[i][t] = blk[i][t]; }
344 bdsp.idct(&mut blk[i]);
346 blockdsp::put_blocks(&mut buf, mb_x, mb_y, &blk);
347 mvi.set_zero_mv(mb_x);
349 mvi2.set_zero_mv(mb_x);
350 } else if pinfo.is_pb() {
351 mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
353 } else if (binfo.mode != Type::B) && !binfo.is_skipped() {
354 if binfo.get_num_mvs() == 1 {
355 let mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
357 self.mv_data.push(BlockMVInfo::Inter_1MV(mv));
359 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
360 bdsp.copy_blocks(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, mv);
363 mvi2.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
366 let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV];
368 mv[blk_no] = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
370 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
371 bdsp.copy_blocks8x8(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, &mv);
375 mvi2.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
379 self.mv_data.push(BlockMVInfo::Inter_4MV(mv));
383 bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?;
384 bdsp.idct(&mut blk[i]);
386 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
387 if is_b && !pinfo.is_pb() {
388 mvi2.set_zero_mv(mb_x);
390 } else if binfo.mode != Type::B {
391 self.mv_data.push(BlockMVInfo::Inter_1MV(ZERO_MV));
392 mvi.set_zero_mv(mb_x);
393 if is_b || pinfo.is_pb() {
394 mvi2.set_zero_mv(mb_x);
396 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
397 bdsp.copy_blocks(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, ZERO_MV);
400 let ref_mv_info = self.mv_data[mb_pos];
401 let has_fwd = binfo.get_num_mvs() > 0;
402 let has_bwd = binfo.get_num_mvs2() > 0;
404 if has_fwd || has_bwd {
407 fwd_mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
410 mvi.set_zero_mv(mb_x);
414 bwd_mv = mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
417 mvi2.set_zero_mv(mb_x);
419 if let (Some(ref fwd_buf), Some(ref bck_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
420 if has_fwd && has_bwd {
421 bdsp.copy_blocks(&mut buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, fwd_mv);
422 bdsp.avg_blocks (&mut buf, bck_buf.clone(), mb_x * 16, mb_y * 16, bwd_mv);
424 bdsp.copy_blocks(&mut buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, fwd_mv);
426 bdsp.copy_blocks(&mut buf, bck_buf.clone(), mb_x * 16, mb_y * 16, bwd_mv);
430 if let BlockMVInfo::Inter_4MV(mvs) = ref_mv_info {
431 let mut mv_f = [ZERO_MV; 4];
432 let mut mv_b = [ZERO_MV; 4];
434 let ref_mv = mvs[blk_no];
435 let ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff);
436 let ref_mv_bwd = ref_mv - ref_mv_fwd;
437 mv_f[blk_no] = ref_mv_fwd;
438 mv_b[blk_no] = ref_mv_bwd;
440 if let (Some(ref fwd_buf), Some(ref bck_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
441 bdsp.copy_blocks8x8(&mut buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, &mv_f);
442 bdsp.avg_blocks8x8 (&mut buf, bck_buf.clone(), mb_x * 16, mb_y * 16, &mv_b);
445 let ref_mv = if let BlockMVInfo::Inter_1MV(mv_) = ref_mv_info { mv_ } else { ZERO_MV };
446 let ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff);
447 let ref_mv_bwd = MV::b_sub(ref_mv, ref_mv_fwd, ZERO_MV, bsdiff, tsdiff);
449 if let (Some(ref fwd_buf), Some(ref bck_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
450 bdsp.copy_blocks(&mut buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, ref_mv_fwd);
451 bdsp.avg_blocks (&mut buf, bck_buf.clone(), mb_x * 16, mb_y * 16, ref_mv_bwd);
454 mvi.set_zero_mv(mb_x);
455 mvi2.set_zero_mv(mb_x);
459 bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?;
460 bdsp.idct(&mut blk[i]);
462 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
466 let mut b_mb = BMB::new();
467 let cbp = binfo.get_cbp_b();
468 let bq = (((pinfo.get_pbinfo().get_dbquant() + 5) as u16) * (binfo.get_q() as u16)) >> 2;
470 if bq < 1 { bquant = 1; }
471 else if bq > 31 { bquant = 31; }
472 else { bquant = bq as u8; }
476 bd.decode_block_inter(&binfo, &sstate, bquant, i, (cbp & (1 << (5 - i))) != 0, &mut b_mb.blk[i])?;
477 bdsp.idct(&mut b_mb.blk[i]);
480 let is_fwd = binfo.is_b_fwd();
482 if binfo.get_num_mvs() != 4 {
483 let ref_mv = mvi2.get_mv(mb_x, 0);
484 let b_mv = if binfo.is_intra() { binfo.get_mv2(1) } else { binfo.get_mv2(0) };
485 let src_mv = if is_fwd { ZERO_MV } else { ref_mv.scale(bsdiff, tsdiff) };
486 let mv_f = MV::add_umv(src_mv, b_mv, pinfo.get_mvmode());
487 let mv_b = MV::b_sub(ref_mv, mv_f, b_mv, bsdiff, tsdiff);
493 let src_mv = if is_fwd { ZERO_MV } else { mvi2.get_mv(mb_x, blk_no).scale(bsdiff, tsdiff) };
494 let mv_f = MV::add_umv(src_mv, binfo.get_mv2(0), pinfo.get_mvmode());
495 let mv_b = MV::b_sub(mvi2.get_mv(mb_x, blk_no), mv_f, binfo.get_mv2(0), bsdiff, tsdiff);
496 b_mb.mv_f[blk_no] = mv_f;
497 b_mb.mv_b[blk_no] = mv_b;
501 self.b_data.push(b_mb);
506 if let Some(plusinfo) = pinfo.plusinfo {
507 if plusinfo.deblock {
508 bdsp.filter_row(&mut buf, mb_y, self.mb_w, &cbpi);
512 if is_b || pinfo.is_pb() {
519 if pinfo.mode.is_ref() {
520 self.ipbs.add_frame(buf);
521 self.next_ts = self.last_ts;
522 self.last_ts = pinfo.ts;
523 self.tsdiff = tsdiff;
528 pub fn flush(&mut self) {
532 pub fn get_bframe(&mut self, bdsp: &BlockDSP) -> DecoderResult<NABufferType> {
533 if !self.has_b || self.ipbs.get_lastref().is_none() || self.ipbs.get_nextref().is_none() {
534 return Err(DecoderError::MissingReference);
538 let fmt = formats::YUV420_FORMAT;
539 let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
540 let bufinfo = alloc_video_buffer(vinfo, 4)?;
541 let b_buf = bufinfo.get_vbuf().unwrap();
543 if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
544 recon_b_frame(b_buf, fwd_buf.clone(), bck_buf.clone(), self.mb_w, self.mb_h, self.b_data.as_slice(), bdsp);
547 self.b_data.truncate(0);
552 fn recon_b_frame(mut b_buf: NAVideoBufferRef<u8>, bck_buf: NAVideoBufferRef<u8>, fwd_buf: NAVideoBufferRef<u8>,
553 mb_w: usize, mb_h: usize, b_data: &[BMB], bdsp: &BlockDSP) {
554 let mut cbpi = CBPInfo::new();
557 for mb_y in 0..mb_h {
558 for mb_x in 0..mb_w {
559 let num_mv = b_data[cur_mb].num_mv;
560 let is_fwd = b_data[cur_mb].fwd;
561 let cbp = b_data[cur_mb].cbp;
562 cbpi.set_cbp(mb_x, cbp);
564 bdsp.copy_blocks(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_b[0]);
566 bdsp.avg_blocks(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_f[0]);
569 bdsp.copy_blocks8x8(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, &b_data[cur_mb].mv_b);
571 bdsp.avg_blocks8x8(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, &b_data[cur_mb].mv_f);
575 blockdsp::add_blocks(&mut b_buf, mb_x, mb_y, &b_data[cur_mb].blk);