]> git.nihav.org Git - nihav.git/blob - codecs/h263/decoder.rs
add MPEG-4 ASP decoder
[nihav.git] / 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::*;
8 use nihav_core::formats;
9
10 #[allow(dead_code)]
11 struct MVInfo {
12 mv: Vec<MV>,
13 mb_w: usize,
14 mb_stride: usize,
15 mb_start: usize,
16 top: bool,
17 mvmode: MVMode,
18 }
19
20 impl MVInfo {
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;
24 self.mb_w = mb_w;
25 self.mb_stride = mb_w * 2 + 2;
26 self.mv.resize(self.mb_stride * 3, ZERO_MV);
27 self.mvmode = mvmode;
28 }
29 fn update_row(&mut self) {
30 self.mb_start = self.mb_w + 1;
31 self.top = false;
32 for i in 0..self.mb_stride {
33 self.mv[i] = self.mv[self.mb_stride * 2 + i];
34 }
35 }
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 {
38 let A;
39 let B;
40 let C;
41 let last = mb_x == self.mb_w - 1;
42 match blk_no {
43 0 => {
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 };
48 } else {
49 A = ZERO_MV; B = ZERO_MV; C = ZERO_MV;
50 }
51 },
52 1 => {
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*/ };
56 },
57 2 => {
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];
61 },
62 3 => {
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];
66 },
67 _ => { return ZERO_MV; }
68 }
69 let pred_mv = MV::pred(A, B, C);
70 let new_mv = MV::add_umv(pred_mv, diff, self.mvmode);
71 if !use4 {
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;
76 } else {
77 match blk_no {
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; },
82 _ => {},
83 };
84 }
85
86 new_mv
87 }
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;
93 }
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]
96 }
97 }
98
99 #[allow(dead_code)]
100 #[derive(Clone,Copy)]
101 struct BMB {
102 num_mv: usize,
103 mv_f: [MV; 4],
104 mv_b: [MV; 4],
105 fwd: bool,
106 blk: [[i16; 64]; 6],
107 cbp: u8,
108 }
109
110 impl BMB {
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} }
112 }
113
114 #[derive(Clone,Copy)]
115 struct PredCoeffs {
116 hor: [[i16; 8]; 6],
117 ver: [[i16; 8]; 6],
118 }
119
120 const ZERO_PRED_COEFFS: PredCoeffs = PredCoeffs { hor: [[0; 8]; 6], ver: [[0; 8]; 6] };
121
122 pub struct H263BaseDecoder {
123 w: usize,
124 h: usize,
125 mb_w: usize,
126 mb_h: usize,
127 num_mb: usize,
128 ftype: Type,
129 ipbs: IPBShuffler,
130 next_ts: u16,
131 last_ts: u16,
132 tsdiff: u16,
133 has_b: bool,
134 b_data: Vec<BMB>,
135 pred_coeffs: Vec<PredCoeffs>,
136 is_gob: bool,
137 slice_reset: bool,
138 may_have_b_frames: bool,
139 mv_data: Vec<BlockMVInfo>,
140 }
141
142 #[inline]
143 fn clip_dc(dc: i16) -> i16 {
144 if dc <= 0 { 0 }
145 else if dc > 2046 { 2047 }
146 else { dc | 1 }
147 }
148
149 #[inline]
150 fn clip_ac(ac: i16) -> i16 {
151 if ac < -2048 { -2048 }
152 else if ac > 2047 { 2047 }
153 else { ac }
154 }
155
156 #[allow(dead_code)]
157 impl H263BaseDecoder {
158 pub fn new_with_opts(is_gob: bool, slice_reset: bool, may_have_b_frames: bool) -> Self {
159 H263BaseDecoder{
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(),
166 is_gob, slice_reset,
167 may_have_b_frames,
168 mv_data: Vec::new(),
169 }
170 }
171 pub fn new(is_gob: bool) -> Self {
172 Self::new_with_opts(is_gob, true, false)
173 }
174 pub fn new_b_frames(is_gob: bool) -> Self {
175 Self::new_with_opts(is_gob, true, true)
176 }
177
178 pub fn is_intra(&self) -> bool { self.ftype == Type::I }
179 pub fn get_frame_type(&self) -> FrameType {
180 match self.ftype {
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,
187 }
188 }
189 pub fn get_dimensions(&self) -> (usize, usize) { (self.w, self.h) }
190
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();
196
197 //todo handle res change
198 self.w = pinfo.w;
199 self.h = pinfo.h;
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();
205
206 if self.has_b {
207 self.mv_data.truncate(0);
208 }
209
210 let save_b_data = pinfo.mode.is_ref() && self.may_have_b_frames;
211 if save_b_data {
212 self.mv_data.truncate(0);
213 }
214 let is_b = pinfo.mode == Type::B;
215
216 if is_b && (self.mv_data.len() < self.mb_w * self.mb_h) {
217 return Err(DecoderError::MissingReference);
218 }
219
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 };
224
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();
229
230 let mut slice = if self.is_gob {
231 SliceInfo::get_default_slice(&pinfo)
232 } else {
233 bd.decode_slice_header(&pinfo)?
234 };
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());
238 }
239 cbpi.reset(self.mb_w);
240
241 let mut blk: [[i16; 64]; 6] = [[0; 64]; 6];
242 let mut sstate = SliceState::new(pinfo.mode == Type::I);
243 let mut mb_pos = 0;
244 let apply_acpred = (pinfo.mode == Type::I) && pinfo.plusinfo.is_some() && pinfo.plusinfo.unwrap().aic;
245 if apply_acpred {
246 self.pred_coeffs.truncate(0);
247 self.pred_coeffs.resize(self.mb_w * self.mb_h, ZERO_PRED_COEFFS);
248 }
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; } }
253
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());
260 }
261 cbpi.reset(self.mb_w);
262 sstate.reset_slice(mb_x, mb_y);
263 sstate.quant = slice.quant;
264 }
265 }
266
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() {
273 if save_b_data {
274 self.mv_data.push(BlockMVInfo::Intra);
275 }
276 for i in 0..6 {
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) {
283 (mb_pos, i - 1)
284 } else if i < 4 {
285 (mb_pos - 1, i + 1)
286 } else {
287 (mb_pos - 1, i)
288 }
289 } else { (0, 0) };
290 let (a_mb, a_blk) = if has_a {
291 if (i == 2) || (i == 3) {
292 (mb_pos, i - 2)
293 } else if i < 4 {
294 (mb_pos - self.mb_w, i + 2)
295 } else {
296 (mb_pos - self.mb_w, i)
297 }
298 } else { (0, 0) };
299 match binfo.acpred {
300 ACPredMode::DC => {
301 let dc;
302 if has_a && has_b {
303 dc = (self.pred_coeffs[b_mb].hor[b_blk][0] + self.pred_coeffs[a_mb].ver[a_blk][0]) / 2;
304 } else if has_a {
305 dc = self.pred_coeffs[a_mb].ver[a_blk][0];
306 } else if has_b {
307 dc = self.pred_coeffs[b_mb].hor[b_blk][0];
308 } else {
309 dc = 1024;
310 }
311 blk[i][0] = clip_dc(blk[i][0] + dc);
312 },
313 ACPredMode::Hor => {
314 if has_b {
315 for k in 0..8 {
316 blk[i][k * 8] += self.pred_coeffs[b_mb].hor[b_blk][k];
317 }
318 for k in 1..8 {
319 blk[i][k * 8] = clip_ac(blk[i][k * 8]);
320 }
321 } else {
322 blk[i][0] += 1024;
323 }
324 blk[i][0] = clip_dc(blk[i][0]);
325 },
326 ACPredMode::Ver => {
327 if has_a {
328 for k in 0..8 {
329 blk[i][k] += self.pred_coeffs[a_mb].ver[a_blk][k];
330 }
331 for k in 1..8 {
332 blk[i][k] = clip_ac(blk[i][k]);
333 }
334 } else {
335 blk[i][0] += 1024;
336 }
337 blk[i][0] = clip_dc(blk[i][0]);
338 },
339 ACPredMode::None => {},
340 };
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]; }
343 }
344 bdsp.idct(&mut blk[i]);
345 }
346 blockdsp::put_blocks(&mut buf, mb_x, mb_y, &blk);
347 mvi.set_zero_mv(mb_x);
348 if is_b {
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);
352 }
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);
356 if save_b_data {
357 self.mv_data.push(BlockMVInfo::Inter_1MV(mv));
358 }
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);
361 }
362 if pinfo.is_pb() {
363 mvi2.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
364 }
365 } else {
366 let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV];
367 for blk_no in 0..4 {
368 mv[blk_no] = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
369 }
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);
372 }
373 if pinfo.is_pb() {
374 for blk_no in 0..4 {
375 mvi2.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
376 }
377 }
378 if save_b_data {
379 self.mv_data.push(BlockMVInfo::Inter_4MV(mv));
380 }
381 }
382 for i in 0..6 {
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]);
385 }
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);
389 }
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);
395 }
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);
398 }
399 } else {
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;
403 //todo refactor
404 if has_fwd || has_bwd {
405 let fwd_mv;
406 if has_fwd {
407 fwd_mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
408 } else {
409 fwd_mv = ZERO_MV;
410 mvi.set_zero_mv(mb_x);
411 }
412 let bwd_mv;
413 if has_bwd {
414 bwd_mv = mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
415 } else {
416 bwd_mv = ZERO_MV;
417 mvi2.set_zero_mv(mb_x);
418 }
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);
423 } else if has_fwd {
424 bdsp.copy_blocks(&mut buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, fwd_mv);
425 } else {
426 bdsp.copy_blocks(&mut buf, bck_buf.clone(), mb_x * 16, mb_y * 16, bwd_mv);
427 }
428 }
429 } else {
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];
433 for blk_no in 0..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;
439 }
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);
443 }
444 } else {
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);
448
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);
452 }
453 }
454 mvi.set_zero_mv(mb_x);
455 mvi2.set_zero_mv(mb_x);
456 }
457 if cbp != 0 {
458 for i in 0..6 {
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]);
461 }
462 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
463 }
464 }
465 if pinfo.is_pb() {
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;
469 let bquant;
470 if bq < 1 { bquant = 1; }
471 else if bq > 31 { bquant = 31; }
472 else { bquant = bq as u8; }
473
474 b_mb.cbp = cbp;
475 for i in 0..6 {
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]);
478 }
479
480 let is_fwd = binfo.is_b_fwd();
481 b_mb.fwd = is_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);
488 b_mb.mv_f[0] = mv_f;
489 b_mb.mv_b[0] = mv_b;
490 b_mb.num_mv = 1;
491 } else {
492 for blk_no in 0..4 {
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;
498 }
499 b_mb.num_mv = 4;
500 }
501 self.b_data.push(b_mb);
502 }
503 sstate.next_mb();
504 mb_pos += 1;
505 }
506 if let Some(plusinfo) = pinfo.plusinfo {
507 if plusinfo.deblock {
508 bdsp.filter_row(&mut buf, mb_y, self.mb_w, &cbpi);
509 }
510 }
511 mvi.update_row();
512 if is_b || pinfo.is_pb() {
513 mvi2.update_row();
514 }
515 cbpi.update_row();
516 sstate.new_row();
517 }
518
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;
524 }
525
526 Ok(bufinfo)
527 }
528 pub fn flush(&mut self) {
529 self.ipbs.clear();
530 }
531
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);
535 }
536 self.has_b = false;
537
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();
542
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);
545 }
546
547 self.b_data.truncate(0);
548 Ok(bufinfo)
549 }
550 }
551
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();
555 let mut cur_mb = 0;
556 cbpi.reset(mb_w);
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);
563 if num_mv == 1 {
564 bdsp.copy_blocks(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_b[0]);
565 if !is_fwd {
566 bdsp.avg_blocks(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_f[0]);
567 }
568 } else {
569 bdsp.copy_blocks8x8(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, &b_data[cur_mb].mv_b);
570 if !is_fwd {
571 bdsp.avg_blocks8x8(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, &b_data[cur_mb].mv_f);
572 }
573 }
574 if cbp != 0 {
575 blockdsp::add_blocks(&mut b_buf, mb_x, mb_y, &b_data[cur_mb].blk);
576 }
577 cur_mb += 1;
578 }
579 cbpi.update_row();
580 }
581 }