591840673f392214b3e3431139713004494d1559
[nihav.git] / nihav-codec-support / src / codecs / h263 / decoder.rs
1 //use std::mem;
2 use nihav_core::codecs::DecoderError;
3 use nihav_core::frame::*;
4 use super::super::*;
5 use super::super::blockdsp;
6 use super::*;
7 use super::code::{H263_INTERP_FUNCS, obmc_filter};
8 use super::data::H263_CHROMA_ROUND;
9 use nihav_core::formats;
10
11 #[allow(dead_code)]
12 struct MVInfo {
13 mv: Vec<MV>,
14 mb_w: usize,
15 mb_stride: usize,
16 mb_start: usize,
17 top: bool,
18 mvmode: MVMode,
19 }
20
21 impl MVInfo {
22 fn new() -> Self { MVInfo{ mv: Vec::new(), mb_w: 0, mb_stride: 0, mb_start: 0, top: true, mvmode: MVMode::Old } }
23 fn reset(&mut self, mb_w: usize, mb_start: usize, mvmode: MVMode) {
24 self.mb_start = mb_start;
25 self.mb_w = mb_w;
26 self.mb_stride = mb_w * 2 + 2;
27 self.mv.resize(self.mb_stride * 3, ZERO_MV);
28 self.mvmode = mvmode;
29 }
30 fn update_row(&mut self) {
31 self.mb_start = self.mb_w + 1;
32 self.top = false;
33 for i in 0..self.mb_stride {
34 self.mv[i] = self.mv[self.mb_stride * 2 + i];
35 }
36 }
37 #[allow(non_snake_case)]
38 fn predict(&mut self, mb_x: usize, blk_no: usize, use4: bool, diff: MV, first_line: bool, first_mb: bool) -> MV {
39 let A;
40 let B;
41 let C;
42 let last = mb_x == self.mb_w - 1;
43 match blk_no {
44 0 => {
45 if mb_x != self.mb_start {
46 A = if !first_mb { self.mv[self.mb_stride + mb_x * 2 - 1] } else { ZERO_MV };
47 B = if !first_line { self.mv[ mb_x * 2] } else { A };
48 C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV };
49 } else {
50 A = ZERO_MV; B = ZERO_MV; C = ZERO_MV;
51 }
52 },
53 1 => {
54 A = self.mv[self.mb_stride + mb_x * 2];
55 B = if !first_line { self.mv[mb_x * 2 + 1] } else { A };
56 C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV/*A*/ };
57 },
58 2 => {
59 A = if mb_x != self.mb_start { self.mv[self.mb_stride * 2 + mb_x * 2 - 1] } else { ZERO_MV };
60 B = self.mv[self.mb_stride + mb_x * 2];
61 C = self.mv[self.mb_stride + mb_x * 2 + 1];
62 },
63 3 => {
64 A = self.mv[self.mb_stride * 2 + mb_x * 2];
65 B = self.mv[self.mb_stride * 1 + mb_x * 2 + 1];
66 C = self.mv[self.mb_stride * 1 + mb_x * 2];
67 },
68 _ => { return ZERO_MV; }
69 }
70 let pred_mv = MV::pred(A, B, C);
71 let new_mv = MV::add_umv(pred_mv, diff, self.mvmode);
72 if !use4 {
73 self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = new_mv;
74 self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = new_mv;
75 self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = new_mv;
76 self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = new_mv;
77 } else {
78 match blk_no {
79 0 => { self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = new_mv; },
80 1 => { self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = new_mv; },
81 2 => { self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = new_mv; },
82 3 => { self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = new_mv; },
83 _ => {},
84 };
85 }
86
87 new_mv
88 }
89 fn set_zero_mv(&mut self, mb_x: usize) {
90 self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = ZERO_MV;
91 self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = ZERO_MV;
92 self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = ZERO_MV;
93 self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = ZERO_MV;
94 }
95 fn get_mv(&self, mb_x: usize, blk_no: usize) -> MV {
96 self.mv[self.mb_stride + mb_x * 2 + (blk_no & 1) + (blk_no >> 1) * self.mb_stride]
97 }
98 }
99
100 #[allow(dead_code)]
101 #[derive(Clone,Copy)]
102 struct BMB {
103 num_mv: usize,
104 mv_f: [MV; 4],
105 mv_b: [MV; 4],
106 fwd: bool,
107 blk: [[i16; 64]; 6],
108 cbp: u8,
109 }
110
111 impl BMB {
112 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} }
113 }
114
115 #[derive(Clone,Copy)]
116 struct PredCoeffs {
117 hor: [[i16; 8]; 6],
118 ver: [[i16; 8]; 6],
119 }
120
121 const ZERO_PRED_COEFFS: PredCoeffs = PredCoeffs { hor: [[1024, 0, 0, 0, 0, 0, 0, 0]; 6], ver: [[1024, 0, 0, 0, 0, 0, 0, 0]; 6] };
122 const ZERO_PRED_COEFFS2: PredCoeffs = PredCoeffs { hor: [[0x7FFF, 0, 0, 0, 0, 0, 0, 0]; 6], ver: [[0x7FFF, 0, 0, 0, 0, 0, 0, 0]; 6] };
123
124 pub const H263DEC_OPT_USES_GOB: u32 = 0x0001;
125 pub const H263DEC_OPT_SLICE_RESET: u32 = 0x0002;
126 pub const H263DEC_OPT_HAS_B_FRAMES: u32 = 0x0004;
127 pub const H263DEC_OPT_HAS_OBMC: u32 = 0x0008;
128 pub const H263DEC_OPT_PRED_QUANT: u32 = 0x0010;
129
130 pub struct H263BaseDecoder {
131 w: usize,
132 h: usize,
133 mb_w: usize,
134 mb_h: usize,
135 num_mb: usize,
136 ftype: Type,
137 ipbs: IPBShuffler,
138 next_ts: u16,
139 last_ts: u16,
140 tsdiff: u16,
141 has_b: bool,
142 b_data: Vec<BMB>,
143 pred_coeffs: Vec<PredCoeffs>,
144 is_gob: bool,
145 slice_reset: bool,
146 may_have_b_frames: bool,
147 has_obmc: bool,
148 mv_data: Vec<BlockMVInfo>,
149 blk: [[i16; 64]; 6],
150 obmc_buf: NAVideoBufferRef<u8>,
151 obmc_blk: Vec<(Type, BMB)>,
152 pred_quant: bool,
153 }
154
155 #[inline]
156 fn clip_dc(dc: i16) -> i16 {
157 if dc <= 0 { 0 }
158 else if dc > 2046 { 2047 }
159 else { dc | 1 }
160 }
161
162 #[inline]
163 fn clip_ac(ac: i16) -> i16 {
164 if ac < -2048 { -2048 }
165 else if ac > 2047 { 2047 }
166 else { ac }
167 }
168
169 #[allow(dead_code)]
170 impl H263BaseDecoder {
171 pub fn new_with_opts(options: u32) -> Self {
172 let is_gob = (options & H263DEC_OPT_USES_GOB) != 0;
173 let slice_reset = (options & H263DEC_OPT_SLICE_RESET) != 0;
174 let may_have_b_frames = (options & H263DEC_OPT_HAS_B_FRAMES) != 0;
175 let has_obmc = (options & H263DEC_OPT_HAS_OBMC) != 0;
176 let pred_quant = (options & H263DEC_OPT_PRED_QUANT) != 0;
177
178 let vbuf = alloc_video_buffer(NAVideoInfo::new(64, 64, false, YUV420_FORMAT), 4).unwrap();
179 let obmc_buf = vbuf.get_vbuf().unwrap();
180
181 H263BaseDecoder{
182 w: 0, h: 0, mb_w: 0, mb_h: 0, num_mb: 0,
183 ftype: Type::Special,
184 ipbs: IPBShuffler::new(),
185 last_ts: 0, next_ts: 0, tsdiff: 0,
186 has_b: false, b_data: Vec::new(),
187 pred_coeffs: Vec::new(),
188 is_gob, slice_reset,
189 may_have_b_frames, has_obmc,
190 pred_quant,
191 mv_data: Vec::new(),
192 blk: [[0; 64]; 6],
193 obmc_buf,
194 obmc_blk: Vec::new(),
195 }
196 }
197 pub fn new(is_gob: bool) -> Self {
198 Self::new_with_opts(H263DEC_OPT_SLICE_RESET | (if is_gob { H263DEC_OPT_USES_GOB } else { 0 }))
199 }
200 pub fn new_b_frames(is_gob: bool) -> Self {
201 Self::new_with_opts(H263DEC_OPT_SLICE_RESET | H263DEC_OPT_HAS_B_FRAMES | (if is_gob { H263DEC_OPT_USES_GOB } else { 0 }))
202 }
203
204 pub fn is_intra(&self) -> bool { self.ftype == Type::I }
205 pub fn get_frame_type(&self) -> FrameType {
206 match self.ftype {
207 Type::I => FrameType::I,
208 Type::P => FrameType::P,
209 Type::B => FrameType::B,
210 Type::PB => FrameType::P,
211 Type::Skip => FrameType::Skip,
212 Type::Special => FrameType::Skip,
213 }
214 }
215 pub fn get_dimensions(&self) -> (usize, usize) { (self.w, self.h) }
216
217 fn decode_intra_mb(&mut self, bd: &mut dyn BlockDecoder, bdsp: &dyn BlockDSP, mb_pos: usize, binfo: &BlockInfo, sstate: &SliceState, apply_acpred: bool) -> DecoderResult<()> {
218 for i in 0..6 {
219 bd.decode_block_intra(&binfo, &sstate, binfo.get_q(), i, (binfo.cbp & (1 << (5 - i))) != 0, &mut self.blk[i])?;
220 if apply_acpred && (binfo.acpred != ACPredMode::None) {
221 let has_b = (i == 1) || (i == 3) || !sstate.first_mb;
222 let has_a = (i == 2) || (i == 3) || !sstate.first_line;
223 let (b_mb, b_blk) = if has_b {
224 if (i == 1) || (i == 3) {
225 (mb_pos, i - 1)
226 } else if i < 4 {
227 (mb_pos - 1, i + 1)
228 } else {
229 (mb_pos - 1, i)
230 }
231 } else { (0, 0) };
232 let (a_mb, a_blk) = if has_a {
233 if (i == 2) || (i == 3) {
234 (mb_pos, i - 2)
235 } else if i < 4 {
236 (mb_pos - self.mb_w, i + 2)
237 } else {
238 (mb_pos - self.mb_w, i)
239 }
240 } else { (0, 0) };
241 match binfo.acpred {
242 ACPredMode::DC => {
243 let dc;
244 if has_a && has_b {
245 dc = (self.pred_coeffs[b_mb].hor[b_blk][0] + self.pred_coeffs[a_mb].ver[a_blk][0]) / 2;
246 } else if has_a {
247 dc = self.pred_coeffs[a_mb].ver[a_blk][0];
248 } else if has_b {
249 dc = self.pred_coeffs[b_mb].hor[b_blk][0];
250 } else {
251 dc = 1024;
252 }
253 self.blk[i][0] = clip_dc(self.blk[i][0] + dc);
254 },
255 ACPredMode::Hor => {
256 if has_b {
257 for k in 0..8 {
258 self.blk[i][k * 8] += self.pred_coeffs[b_mb].hor[b_blk][k];
259 }
260 for k in 1..8 {
261 self.blk[i][k * 8] = clip_ac(self.blk[i][k * 8]);
262 }
263 } else {
264 self.blk[i][0] += 1024;
265 }
266 self.blk[i][0] = clip_dc(self.blk[i][0]);
267 },
268 ACPredMode::Ver => {
269 if has_a {
270 for k in 0..8 {
271 self.blk[i][k] += self.pred_coeffs[a_mb].ver[a_blk][k];
272 }
273 for k in 1..8 {
274 self.blk[i][k] = clip_ac(self.blk[i][k]);
275 }
276 } else {
277 self.blk[i][0] += 1024;
278 }
279 self.blk[i][0] = clip_dc(self.blk[i][0]);
280 },
281 ACPredMode::None => {},
282 };
283 for t in 0..8 { self.pred_coeffs[mb_pos].hor[i][t] = self.blk[i][t * 8]; }
284 for t in 0..8 { self.pred_coeffs[mb_pos].ver[i][t] = self.blk[i][t]; }
285 }
286 bdsp.idct(&mut self.blk[i]);
287 }
288 Ok(())
289 }
290 fn decode_intra_mb_pred_quant(&mut self, bd: &mut dyn BlockDecoder, bdsp: &dyn BlockDSP, mb_pos: usize, binfo: &BlockInfo, sstate: &SliceState, apply_acpred: bool) -> DecoderResult<()> {
291 for i in 0..6 {
292 bd.decode_block_intra(&binfo, &sstate, binfo.get_q(), i, (binfo.cbp & (1 << (5 - i))) != 0, &mut self.blk[i])?;
293 let q = if binfo.get_q() < 8 {
294 8
295 } else {
296 i16::from(binfo.get_q() * 2)
297 };
298 let quant_gray = 1024 / q;
299 if apply_acpred && (binfo.acpred != ACPredMode::None) {
300 let has_b = (i == 1) || (i == 3) || !sstate.first_mb;
301 let has_a = (i == 2) || (i == 3) || !sstate.first_line;
302 let (b_mb, b_blk) = if has_b {
303 if (i == 1) || (i == 3) {
304 (mb_pos, i - 1)
305 } else if i < 4 {
306 (mb_pos - 1, i + 1)
307 } else {
308 (mb_pos - 1, i)
309 }
310 } else { (0, 0) };
311 let (a_mb, a_blk) = if has_a {
312 if (i == 2) || (i == 3) {
313 (mb_pos, i - 2)
314 } else if i < 4 {
315 (mb_pos - self.mb_w, i + 2)
316 } else {
317 (mb_pos - self.mb_w, i)
318 }
319 } else { (0, 0) };
320 let dc_a = if has_a && self.pred_coeffs[a_mb].ver[a_blk][0] != 0x7FFF {
321 self.pred_coeffs[a_mb].ver[a_blk][0]
322 } else {
323 quant_gray
324 };
325 let dc_b = if has_b && self.pred_coeffs[b_mb].hor[b_blk][0] != 0x7FFF {
326 self.pred_coeffs[b_mb].hor[b_blk][0]
327 } else {
328 quant_gray
329 };
330 let dc = match binfo.acpred {
331 ACPredMode::DC => {
332 if has_a && has_b {
333 (dc_a + dc_b + 1) >> 1
334 } else if has_a {
335 dc_a
336 } else if has_b {
337 dc_b
338 } else {
339 quant_gray
340 }
341 },
342 ACPredMode::Hor => {
343 if has_b {
344 for k in 1..8 {
345 self.blk[i][k * 8] += self.pred_coeffs[b_mb].hor[b_blk][k];
346 }
347 for k in 1..8 {
348 self.blk[i][k * 8] = clip_ac(self.blk[i][k * 8]);
349 }
350 dc_b
351 } else {
352 quant_gray
353 }
354 },
355 ACPredMode::Ver => {
356 if has_a {
357 for k in 1..8 {
358 self.blk[i][k] += self.pred_coeffs[a_mb].ver[a_blk][k];
359 }
360 for k in 1..8 {
361 self.blk[i][k] = clip_ac(self.blk[i][k]);
362 }
363 dc_a
364 } else {
365 quant_gray
366 }
367 },
368 ACPredMode::None => { 0 },
369 };
370 self.blk[i][0] += dc;
371 for t in 0..8 { self.pred_coeffs[mb_pos].hor[i][t] = self.blk[i][t * 8]; }
372 for t in 0..8 { self.pred_coeffs[mb_pos].ver[i][t] = self.blk[i][t]; }
373 }
374 if apply_acpred {
375 self.blk[i][0] *= q;
376 }
377 bdsp.idct(&mut self.blk[i]);
378 }
379 Ok(())
380 }
381 fn decode_inter_mb(&mut self, bd: &mut dyn BlockDecoder, bdsp: &dyn BlockDSP, binfo: &BlockInfo, sstate: &SliceState) -> DecoderResult<()> {
382 for i in 0..6 {
383 bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((binfo.cbp >> (5 - i)) & 1) != 0, &mut self.blk[i])?;
384 bdsp.idct(&mut self.blk[i]);
385 }
386 Ok(())
387 }
388 fn get_obmc_mv(&self, mb_idx: usize, blk_no: usize, cur_mv: MV) -> MV {
389 let (mbt, ref bi) = self.obmc_blk[mb_idx];
390 if mbt == Type::I {
391 cur_mv
392 } else if mbt == Type::Skip {
393 ZERO_MV
394 } else if bi.num_mv == 1 {
395 bi.mv_f[0]
396 } else {
397 bi.mv_f[blk_no]
398 }
399 }
400 #[allow(clippy::cyclomatic_complexity)]
401 fn reconstruct_obmc(&mut self, buf: &mut NAVideoBuffer<u8>, slice_start: usize, start: usize, end: usize, slice_end: bool) -> usize {
402 let mut mb_x = start % self.mb_w;
403 let mut mb_y = start / self.mb_w;
404 let mut mb_pos = start;
405 while mb_pos < end {
406 let has_top = mb_pos >= slice_start + self.mb_w;
407 let has_left = (mb_x > 0) && (mb_pos > slice_start);
408 let has_right = (mb_x + 1 < self.mb_w) && (mb_pos + 1 < end);
409 let has_bottom = mb_pos + self.mb_w < end;
410
411 if !has_bottom && !slice_end {
412 break;
413 }
414
415 let (mbt, ref bi) = self.obmc_blk[mb_pos];
416 if mbt == Type::P || mbt == Type::Skip {
417 let single_mv = bi.num_mv != 4;
418 for blk in 0..4 {
419 let cur_mv = if single_mv { bi.mv_f[0] } else { bi.mv_f[blk] };
420 let top_mv = if (blk & 2) == 0 {
421 if has_top {
422 self.get_obmc_mv(mb_pos - self.mb_w, blk + 2, cur_mv)
423 } else { cur_mv }
424 } else {
425 if single_mv { cur_mv } else { bi.mv_f[blk - 2] }
426 };
427 let left_mv = if (blk & 1) == 0 {
428 if has_left {
429 self.get_obmc_mv(mb_pos - 1, blk + 1, cur_mv)
430 } else { cur_mv }
431 } else {
432 if single_mv { cur_mv } else { bi.mv_f[blk - 1] }
433 };
434 let bottom_mv = if (blk & 2) != 0 {
435 if has_bottom {
436 self.get_obmc_mv(mb_pos + self.mb_w, blk - 2, cur_mv)
437 } else { cur_mv }
438 } else {
439 if single_mv { cur_mv } else { bi.mv_f[blk + 2] }
440 };
441 let right_mv = if (blk & 1) != 0 {
442 if has_right {
443 self.get_obmc_mv(mb_pos + 1, blk - 1, cur_mv)
444 } else { cur_mv }
445 } else {
446 if single_mv { cur_mv } else { bi.mv_f[blk + 1] }
447 };
448
449 let mut obmcbuf = NASimpleVideoFrame::from_video_buf(&mut self.obmc_buf).unwrap();
450
451 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
452 let dx = (mb_x * 16 + (blk & 1) * 8) as i16;
453 let dy = (mb_y * 16 + (blk & 2) * 4) as i16;
454
455 let block_params = [(8, 0, top_mv), (0, 8, left_mv), (8, 8, cur_mv), (16, 8, right_mv), (8, 16, bottom_mv)];
456
457 for (off_x, off_y, mv) in block_params.iter() {
458 let mx = dx + (mv.x >> 1) - off_x;
459 let my = dy + (mv.y >> 1) - off_y;
460 let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize;
461 blockdsp::copy_block(&mut obmcbuf, srcbuf.clone(), 0,
462 *off_x as usize, *off_y as usize, mx, my,
463 8, 8, 0, 1, mode, H263_INTERP_FUNCS);
464 }
465 let stride = buf.get_stride(0);
466 let off = buf.get_offset(0) + (dx as usize) + (dy as usize) * stride;
467 let data = buf.get_data_mut().unwrap();
468 obmc_filter(&mut data[off..], stride, obmcbuf.data, obmcbuf.stride[0]);
469 }
470 }
471 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
472 let mut dst = NASimpleVideoFrame::from_video_buf(buf).unwrap();
473 let xpos = mb_x * 8;
474 let ypos = mb_y * 8;
475
476 let (mvx, mvy, cmode) = if single_mv {
477 let mv = bi.mv_f[0];
478 let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 });
479 (mv.x >> 2, mv.y >> 2, cmode)
480 } else {
481 let sum_mv = bi.mv_f[0] + bi.mv_f[1] + bi.mv_f[2] + bi.mv_f[3];
482 let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize];
483 let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize];
484 let cmode = ((cmx & 1) + (cmy & 1) * 2) as usize;
485 (cmx, cmy, cmode)
486 };
487 blockdsp::copy_block(&mut dst, srcbuf.clone(), 1, xpos, ypos, mvx, mvy, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS);
488 blockdsp::copy_block(&mut dst, srcbuf.clone(), 2, xpos, ypos, mvx, mvy, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS);
489 }
490 if mbt != Type::Skip {
491 blockdsp::add_blocks(buf, mb_x, mb_y, &bi.blk);
492 }
493 }
494
495 mb_pos += 1;
496 mb_x += 1;
497 if mb_x == self.mb_w {
498 mb_x = 0;
499 mb_y += 1;
500 }
501 }
502 mb_pos
503 }
504 #[allow(clippy::cyclomatic_complexity)]
505 pub fn parse_frame(&mut self, bd: &mut dyn BlockDecoder, bdsp: &dyn BlockDSP) -> DecoderResult<NABufferType> {
506 let pinfo = bd.decode_pichdr()?;
507 let mut mvi = MVInfo::new();
508 let mut mvi2 = MVInfo::new();
509 let mut cbpi = CBPInfo::new();
510
511 //todo handle res change
512 self.w = pinfo.w;
513 self.h = pinfo.h;
514 self.mb_w = (pinfo.w + 15) >> 4;
515 self.mb_h = (pinfo.h + 15) >> 4;
516 self.num_mb = self.mb_w * self.mb_h;
517 self.ftype = pinfo.mode;
518 self.has_b = pinfo.is_pb();
519
520 let do_obmc = self.has_obmc && (pinfo.mode == Type::P);
521
522 if do_obmc {
523 let capacity = self.obmc_blk.capacity();
524 if capacity < self.num_mb {
525 self.obmc_blk.reserve(self.num_mb - capacity);
526 }
527 self.obmc_blk.truncate(0);
528 }
529
530 if self.has_b {
531 self.mv_data.truncate(0);
532 }
533
534 let save_b_data = pinfo.mode.is_ref() && self.may_have_b_frames;
535 if save_b_data {
536 self.mv_data.truncate(0);
537 }
538 let is_b = pinfo.mode == Type::B;
539
540 if is_b && (self.mv_data.len() < self.mb_w * self.mb_h) {
541 return Err(DecoderError::MissingReference);
542 }
543
544 let tsdiff = if pinfo.is_pb() { pinfo.ts.wrapping_sub(self.last_ts) >> 1 }
545 else { self.last_ts.wrapping_sub(self.next_ts) >> 1 };
546 let bsdiff = if pinfo.is_pb() { (pinfo.get_pbinfo().get_trb() as u16) << 7 }
547 else { pinfo.ts.wrapping_sub(self.next_ts) >> 1 };
548
549 let fmt = formats::YUV420_FORMAT;
550 let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
551 let bufinfo = alloc_video_buffer(vinfo, 4)?;
552 let mut buf = bufinfo.get_vbuf().unwrap();
553
554 let mut slice = if self.is_gob {
555 SliceInfo::get_default_slice(&pinfo)
556 } else {
557 bd.decode_slice_header(&pinfo)?
558 };
559 mvi.reset(self.mb_w, 0, pinfo.get_mvmode());
560 if is_b || pinfo.is_pb() {
561 mvi2.reset(self.mb_w, 0, pinfo.get_mvmode());
562 }
563 cbpi.reset(self.mb_w);
564
565 let mut sstate = SliceState::new(pinfo.mode == Type::I);
566 let mut mb_pos = 0;
567 let apply_acpred = /*(pinfo.mode == Type::I) && */pinfo.plusinfo.is_some() && pinfo.plusinfo.unwrap().aic;
568 if apply_acpred {
569 self.pred_coeffs.truncate(0);
570 if !self.pred_quant {
571 self.pred_coeffs.resize(self.mb_w * self.mb_h, ZERO_PRED_COEFFS);
572 } else {
573 self.pred_coeffs.resize(self.mb_w * self.mb_h, ZERO_PRED_COEFFS2);
574 }
575 }
576 sstate.quant = slice.quant;
577 let mut obmc_start = 0;
578 let mut slice_start = 0;
579 for mb_y in 0..self.mb_h {
580 for mb_x in 0..self.mb_w {
581 self.blk = [[0; 64]; 6];
582
583 if slice.is_at_end(mb_pos) || (slice.needs_check() && mb_pos > 0 && bd.is_slice_end()) {
584 if do_obmc {
585 self.reconstruct_obmc(&mut buf, slice_start, obmc_start, mb_pos, true);
586 obmc_start = mb_pos;
587 slice_start = mb_pos;
588 }
589 slice = bd.decode_slice_header(&pinfo)?;
590 if self.is_gob || self.slice_reset {
591 mvi.reset(self.mb_w, mb_x, pinfo.get_mvmode());
592 if is_b || pinfo.is_pb() {
593 mvi2.reset(self.mb_w, mb_x, pinfo.get_mvmode());
594 }
595 cbpi.reset(self.mb_w);
596 sstate.reset_slice(mb_x, mb_y);
597 sstate.quant = slice.quant;
598 }
599 }
600
601 let binfo = bd.decode_block_header(&pinfo, &slice, &sstate)?;
602 let cbp = binfo.get_cbp();
603 cbpi.set_cbp(mb_x, cbp);
604 cbpi.set_q(mb_x, binfo.get_q());
605 sstate.quant = binfo.get_q();
606 if binfo.is_intra() {
607 if save_b_data {
608 self.mv_data.push(BlockMVInfo::Intra);
609 }
610 if !self.pred_quant {
611 self.decode_intra_mb(bd, bdsp, mb_pos, &binfo, &sstate, apply_acpred)?;
612 } else {
613 self.decode_intra_mb_pred_quant(bd, bdsp, mb_pos, &binfo, &sstate, apply_acpred)?;
614 }
615 blockdsp::put_blocks(&mut buf, mb_x, mb_y, &self.blk);
616 mvi.set_zero_mv(mb_x);
617 if is_b {
618 mvi2.set_zero_mv(mb_x);
619 } else if pinfo.is_pb() {
620 mvi.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
621 mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
622 }
623 if do_obmc {
624 //todo: use MV from PB-part if available
625 self.obmc_blk.push((Type::I, BMB::new()));
626 }
627 } else if (binfo.mode != Type::B) && !binfo.is_skipped() {
628 if binfo.get_num_mvs() == 1 {
629 let mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
630 if save_b_data {
631 self.mv_data.push(BlockMVInfo::Inter_1MV(mv));
632 }
633 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
634 bdsp.copy_blocks(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, mv);
635 }
636 if pinfo.is_pb() {
637 mvi2.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
638 }
639 if do_obmc {
640 let mut blki = BMB::new();
641 blki.mv_f[0] = mv;
642 blki.num_mv = 1;
643 self.obmc_blk.push((Type::P, blki));
644 }
645 } else {
646 let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV];
647 for blk_no in 0..4 {
648 mv[blk_no] = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
649 }
650 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
651 bdsp.copy_blocks8x8(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, &mv);
652 }
653 if pinfo.is_pb() {
654 for blk_no in 0..4 {
655 mvi2.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
656 }
657 }
658 if save_b_data {
659 self.mv_data.push(BlockMVInfo::Inter_4MV(mv));
660 }
661 if do_obmc {
662 let mut blki = BMB::new();
663 blki.mv_f = mv;
664 blki.num_mv = 4;
665 self.obmc_blk.push((Type::P, blki));
666 }
667 }
668 self.decode_inter_mb(bd, bdsp, &binfo, &sstate)?;
669 if !do_obmc {
670 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &self.blk);
671 } else {
672 let (_, ref mut bi) = self.obmc_blk[mb_pos];
673 bi.blk = self.blk;
674 }
675 if is_b && !pinfo.is_pb() {
676 mvi2.set_zero_mv(mb_x);
677 }
678 } else if binfo.mode != Type::B {
679 self.mv_data.push(BlockMVInfo::Inter_1MV(ZERO_MV));
680 mvi.set_zero_mv(mb_x);
681 if is_b || pinfo.is_pb() {
682 mvi2.set_zero_mv(mb_x);
683 }
684 if !do_obmc {
685 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
686 bdsp.copy_blocks(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, ZERO_MV);
687 }
688 } else {
689 self.obmc_blk.push((Type::Skip, BMB::new()));
690 }
691 } else {
692 recon_b_mb(&mut buf, &mut self.ipbs, bdsp, &mut mvi, &mut mvi2, mb_pos, self.mb_w, &sstate, &binfo, &self.mv_data, bsdiff, tsdiff);
693 if cbp != 0 {
694 self.decode_inter_mb(bd, bdsp, &binfo, &sstate)?;
695 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &self.blk);
696 }
697 }
698 if pinfo.is_pb() {
699 let mut b_mb = BMB::new();
700 let cbp = binfo.get_cbp_b();
701 let bq = (((pinfo.get_pbinfo().get_dbquant() + 5) as u16) * (binfo.get_q() as u16)) >> 2;
702 let bquant;
703 if bq < 1 { bquant = 1; }
704 else if bq > 31 { bquant = 31; }
705 else { bquant = bq as u8; }
706
707 b_mb.cbp = cbp;
708 for i in 0..6 {
709 bd.decode_block_inter(&binfo, &sstate, bquant, i, (cbp & (1 << (5 - i))) != 0, &mut b_mb.blk[i])?;
710 bdsp.idct(&mut b_mb.blk[i]);
711 }
712
713 let is_fwd = binfo.is_b_fwd();
714 b_mb.fwd = is_fwd;
715 if binfo.get_num_mvs() != 4 {
716 let ref_mv = mvi2.get_mv(mb_x, 0);
717 let b_mv = if binfo.is_intra() { binfo.get_mv2(1) } else { binfo.get_mv2(0) };
718 let src_mv = if is_fwd { ZERO_MV } else { ref_mv.scale(bsdiff, tsdiff) };
719 let mv_f = MV::add_umv(src_mv, b_mv, pinfo.get_mvmode());
720 let mv_b = MV::b_sub(ref_mv, mv_f, b_mv, bsdiff, tsdiff);
721 b_mb.mv_f[0] = mv_f;
722 b_mb.mv_b[0] = mv_b;
723 b_mb.num_mv = 1;
724 } else {
725 for blk_no in 0..4 {
726 let src_mv = if is_fwd { ZERO_MV } else { mvi2.get_mv(mb_x, blk_no).scale(bsdiff, tsdiff) };
727 let mv_f = MV::add_umv(src_mv, binfo.get_mv2(0), pinfo.get_mvmode());
728 let mv_b = MV::b_sub(mvi2.get_mv(mb_x, blk_no), mv_f, binfo.get_mv2(0), bsdiff, tsdiff);
729 b_mb.mv_f[blk_no] = mv_f;
730 b_mb.mv_b[blk_no] = mv_b;
731 }
732 b_mb.num_mv = 4;
733 }
734 self.b_data.push(b_mb);
735 }
736 sstate.next_mb();
737 mb_pos += 1;
738 }
739 if do_obmc && (mb_pos > obmc_start + self.mb_w) {
740 obmc_start = self.reconstruct_obmc(&mut buf, slice_start, obmc_start, mb_pos - self.mb_w, false);
741 } else if let Some(plusinfo) = pinfo.plusinfo {
742 if plusinfo.deblock {
743 bdsp.filter_row(&mut buf, mb_y, self.mb_w, &cbpi);
744 }
745 }
746 mvi.update_row();
747 if is_b || pinfo.is_pb() {
748 mvi2.update_row();
749 }
750 cbpi.update_row();
751 sstate.new_row();
752 }
753 if do_obmc {
754 self.reconstruct_obmc(&mut buf, slice_start, obmc_start, mb_pos, true);
755 }
756
757 if pinfo.mode.is_ref() {
758 self.ipbs.add_frame(buf);
759 self.next_ts = self.last_ts;
760 self.last_ts = pinfo.ts;
761 self.tsdiff = tsdiff;
762 }
763
764 Ok(bufinfo)
765 }
766 pub fn flush(&mut self) {
767 self.ipbs.clear();
768 }
769
770 pub fn get_bframe(&mut self, bdsp: &dyn BlockDSP) -> DecoderResult<NABufferType> {
771 if !self.has_b || self.ipbs.get_lastref().is_none() || self.ipbs.get_nextref().is_none() {
772 return Err(DecoderError::MissingReference);
773 }
774 self.has_b = false;
775
776 let fmt = formats::YUV420_FORMAT;
777 let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
778 let bufinfo = alloc_video_buffer(vinfo, 4)?;
779 let b_buf = bufinfo.get_vbuf().unwrap();
780
781 if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_b_bwdref(), self.ipbs.get_b_fwdref()) {
782 recon_b_frame(b_buf, bck_buf.clone(), fwd_buf.clone(), self.mb_w, self.mb_h, self.b_data.as_slice(), bdsp);
783 }
784
785 self.b_data.truncate(0);
786 Ok(bufinfo)
787 }
788 }
789
790 fn recon_b_mb(buf: &mut NAVideoBuffer<u8>, ipbs: &mut IPBShuffler, bdsp: &dyn BlockDSP, mvi: &mut MVInfo, mvi2: &mut MVInfo, mb_pos: usize, mb_w: usize, sstate: &SliceState, binfo: &BlockInfo, mv_data: &[BlockMVInfo], bsdiff: u16, tsdiff: u16) {
791 let mb_x = mb_pos % mb_w;
792 let mb_y = mb_pos / mb_w;
793
794 let ref_mv_info = mv_data[mb_pos];
795 let has_fwd = binfo.get_num_mvs() > 0;
796 let has_bwd = binfo.get_num_mvs2() > 0;
797
798 if has_fwd || has_bwd {
799 let fwd_mv;
800 if has_fwd {
801 fwd_mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
802 } else {
803 fwd_mv = ZERO_MV;
804 mvi.set_zero_mv(mb_x);
805 }
806 let bwd_mv;
807 if has_bwd {
808 bwd_mv = mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
809 } else {
810 bwd_mv = ZERO_MV;
811 mvi2.set_zero_mv(mb_x);
812 }
813 if let (Some(ref fwd_buf), Some(ref bck_buf)) = (ipbs.get_nextref(), ipbs.get_lastref()) {
814 if has_fwd && has_bwd {
815 bdsp.copy_blocks(buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, fwd_mv);
816 bdsp.avg_blocks (buf, bck_buf.clone(), mb_x * 16, mb_y * 16, bwd_mv);
817 } else if has_fwd {
818 bdsp.copy_blocks(buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, fwd_mv);
819 } else {
820 bdsp.copy_blocks(buf, bck_buf.clone(), mb_x * 16, mb_y * 16, bwd_mv);
821 }
822 }
823 } else {
824 if let BlockMVInfo::Inter_4MV(mvs) = ref_mv_info {
825 let mut mv_f = [ZERO_MV; 4];
826 let mut mv_b = [ZERO_MV; 4];
827 for blk_no in 0..4 {
828 let ref_mv = mvs[blk_no];
829 let ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff);
830 let ref_mv_bwd = ref_mv - ref_mv_fwd;
831 mv_f[blk_no] = ref_mv_fwd;
832 mv_b[blk_no] = ref_mv_bwd;
833 }
834 if let (Some(ref fwd_buf), Some(ref bck_buf)) = (ipbs.get_nextref(), ipbs.get_lastref()) {
835 bdsp.copy_blocks8x8(buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, &mv_f);
836 bdsp.avg_blocks8x8 (buf, bck_buf.clone(), mb_x * 16, mb_y * 16, &mv_b);
837 }
838 } else {
839 let ref_mv = if let BlockMVInfo::Inter_1MV(mv_) = ref_mv_info { mv_ } else { ZERO_MV };
840 let ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff);
841 let ref_mv_bwd = MV::b_sub(ref_mv, ref_mv_fwd, ZERO_MV, bsdiff, tsdiff);
842
843 if let (Some(ref fwd_buf), Some(ref bck_buf)) = (ipbs.get_nextref(), ipbs.get_lastref()) {
844 bdsp.copy_blocks(buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, ref_mv_fwd);
845 bdsp.avg_blocks (buf, bck_buf.clone(), mb_x * 16, mb_y * 16, ref_mv_bwd);
846 }
847 }
848 mvi.set_zero_mv(mb_x);
849 mvi2.set_zero_mv(mb_x);
850 }
851 }
852
853 fn recon_b_frame(mut b_buf: NAVideoBufferRef<u8>, bck_buf: NAVideoBufferRef<u8>, fwd_buf: NAVideoBufferRef<u8>,
854 mb_w: usize, mb_h: usize, b_data: &[BMB], bdsp: &dyn BlockDSP) {
855 let mut cbpi = CBPInfo::new();
856 let mut cur_mb = 0;
857 cbpi.reset(mb_w);
858 for mb_y in 0..mb_h {
859 for mb_x in 0..mb_w {
860 let num_mv = b_data[cur_mb].num_mv;
861 let is_fwd = b_data[cur_mb].fwd;
862 let cbp = b_data[cur_mb].cbp;
863 cbpi.set_cbp(mb_x, cbp);
864 if num_mv == 1 {
865 bdsp.copy_blocks(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_f[0]);
866 if !is_fwd {
867 bdsp.avg_blocks(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_b[0]);
868 }
869 } else {
870 bdsp.copy_blocks8x8(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, &b_data[cur_mb].mv_f);
871 if !is_fwd {
872 bdsp.avg_blocks8x8(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, &b_data[cur_mb].mv_b);
873 }
874 }
875 if cbp != 0 {
876 blockdsp::add_blocks(&mut b_buf, mb_x, mb_y, &b_data[cur_mb].blk);
877 }
878 cur_mb += 1;
879 }
880 cbpi.update_row();
881 }
882 }