aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-codec-support/src/codecs/h263/decoder.rs
blob: 929854af4929608baf96cd43b0d471d1760d0be3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//use std::mem;
use nihav_core::codecs::DecoderError;
use nihav_core::frame::*;
use super::super::*;
use super::super::blockdsp;
use super::*;
//use super::code::*;
use nihav_core::formats;

#[allow(dead_code)]
struct MVInfo {
    mv:         Vec<MV>,
    mb_w:       usize,
    mb_stride:  usize,
    mb_start:   usize,
    top:        bool,
    mvmode:     MVMode,
}

impl MVInfo {
    fn new() -> Self { MVInfo{ mv: Vec::new(), mb_w: 0, mb_stride: 0, mb_start: 0, top: true, mvmode: MVMode::Old } }
    fn reset(&mut self, mb_w: usize, mb_start: usize, mvmode: MVMode) {
        self.mb_start  = mb_start;
        self.mb_w      = mb_w;
        self.mb_stride = mb_w * 2;
        self.mv.resize(self.mb_stride * 3, ZERO_MV);
        self.mvmode    = mvmode;
    }
    fn update_row(&mut self) {
        self.mb_start = self.mb_w + 1;
        self.top      = false;
        for i in 0..self.mb_stride {
            self.mv[i] = self.mv[self.mb_stride * 2 + i];
        }
    }
    #[allow(non_snake_case)]
    fn predict(&mut self, mb_x: usize, blk_no: usize, use4: bool, diff: MV, first_line: bool, first_mb: bool) -> MV {
        let A;
        let B;
        let C;
        let last = mb_x == self.mb_w - 1;
        match blk_no {
            0 => {
                    if mb_x != self.mb_start {
                        A = if !first_mb   { self.mv[self.mb_stride + mb_x * 2 - 1] } else { ZERO_MV };
                        B = if !first_line { self.mv[                 mb_x * 2] } else { A };
                        C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV };
                    } else {
                        A = ZERO_MV; B = ZERO_MV; C = ZERO_MV;
                    }
                },
            1 => {
                    A = self.mv[self.mb_stride + mb_x * 2];
                    B = if !first_line { self.mv[mb_x * 2 + 1] } else { A };
                    C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV/*A*/ };
                },
            2 => {
                    A = if mb_x != self.mb_start { self.mv[self.mb_stride * 2 + mb_x * 2 - 1] } else { ZERO_MV };
                    B = self.mv[self.mb_stride + mb_x * 2];
                    C = self.mv[self.mb_stride + mb_x * 2 + 1];
                },
            3 => {
                    A = self.mv[self.mb_stride * 2 + mb_x * 2];
                    B = self.mv[self.mb_stride * 1 + mb_x * 2 + 1];
                    C = self.mv[self.mb_stride * 1 + mb_x * 2];
                },
            _ => { return ZERO_MV; }
        }
        let pred_mv = MV::pred(A, B, C);
        let new_mv = MV::add_umv(pred_mv, diff, self.mvmode);
        if !use4 {
            self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = new_mv;
            self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = new_mv;
            self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = new_mv;
            self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = new_mv;
        } else {
            match blk_no {
                0 => { self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = new_mv; },
                1 => { self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = new_mv; },
                2 => { self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = new_mv; },
                3 => { self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = new_mv; },
                _ => {},
            };
        }

        new_mv
    }
    fn set_zero_mv(&mut self, mb_x: usize) {
        self.mv[self.mb_stride * 1 + mb_x * 2 + 0] = ZERO_MV;
        self.mv[self.mb_stride * 1 + mb_x * 2 + 1] = ZERO_MV;
        self.mv[self.mb_stride * 2 + mb_x * 2 + 0] = ZERO_MV;
        self.mv[self.mb_stride * 2 + mb_x * 2 + 1] = ZERO_MV;
    }
    fn get_mv(&self, mb_x: usize, blk_no: usize) -> MV {
        self.mv[self.mb_stride + mb_x * 2 + (blk_no & 1) + (blk_no >> 1) * self.mb_stride]
    }
}

#[allow(dead_code)]
#[derive(Clone,Copy)]
struct BMB {
    num_mv: usize,
    mv_f:   [MV; 4],
    mv_b:   [MV; 4],
    fwd:    bool,
    blk:    [[i16; 64]; 6],
    cbp:    u8,
}

impl BMB {
    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} }
}

#[derive(Clone,Copy)]
struct PredCoeffs {
    hor: [[i16; 8]; 6],
    ver: [[i16; 8]; 6],
}

const ZERO_PRED_COEFFS: PredCoeffs = PredCoeffs { hor: [[0; 8]; 6], ver: [[0; 8]; 6] };

pub struct H263BaseDecoder {
    w:          usize,
    h:          usize,
    mb_w:       usize,
    mb_h:       usize,
    num_mb:     usize,
    ftype:      Type,
    ipbs:       IPBShuffler,
    next_ts:    u16,
    last_ts:    u16,
    tsdiff:     u16,
    has_b:      bool,
    b_data:     Vec<BMB>,
    pred_coeffs: Vec<PredCoeffs>,
    is_gob:     bool,
    slice_reset: bool,
    may_have_b_frames: bool,
    mv_data:    Vec<BlockMVInfo>,
}

#[inline]
fn clip_dc(dc: i16) -> i16 {
    if dc < 0 { 0 }
    else if dc > 2046 { 2046 }
    else { (dc + 1) & !1 }
}

#[inline]
fn clip_ac(ac: i16) -> i16 {
    if ac < -2048 { -2048 }
    else if ac > 2047 { 2047 }
    else { ac }
}

#[allow(dead_code)]
impl H263BaseDecoder {
    pub fn new_with_opts(is_gob: bool, slice_reset: bool, may_have_b_frames: bool) -> Self {
        H263BaseDecoder{
            w: 0, h: 0, mb_w: 0, mb_h: 0, num_mb: 0,
            ftype: Type::Special,
            ipbs: IPBShuffler::new(),
            last_ts: 0, next_ts: 0, tsdiff: 0,
            has_b: false, b_data: Vec::new(),
            pred_coeffs: Vec::new(),
            is_gob, slice_reset,
            may_have_b_frames,
            mv_data: Vec::new(),
        }
    }
    pub fn new(is_gob: bool) -> Self {
        Self::new_with_opts(is_gob, true, false)
    }
    pub fn new_b_frames(is_gob: bool) -> Self {
        Self::new_with_opts(is_gob, true, true)
    }

    pub fn is_intra(&self) -> bool { self.ftype == Type::I }
    pub fn get_frame_type(&self) -> FrameType {
        match self.ftype {
            Type::I       => FrameType::I,
            Type::P       => FrameType::P,
            Type::B       => FrameType::B,
            Type::PB      => FrameType::P,
            Type::Skip    => FrameType::Skip,
            Type::Special => FrameType::Skip,
        }
    }
    pub fn get_dimensions(&self) -> (usize, usize) { (self.w, self.h) }

    pub fn parse_frame(&mut self, bd: &mut BlockDecoder, bdsp: &BlockDSP) -> DecoderResult<NABufferType> {
        let pinfo = bd.decode_pichdr()?;
        let mut mvi = MVInfo::new();
        let mut mvi2 = MVInfo::new();
        let mut cbpi = CBPInfo::new();

//todo handle res change
        self.w = pinfo.w;
        self.h = pinfo.h;
        self.mb_w = (pinfo.w + 15) >> 4;
        self.mb_h = (pinfo.h + 15) >> 4;
        self.num_mb = self.mb_w * self.mb_h;
        self.ftype = pinfo.mode;
        self.has_b = pinfo.is_pb();

        if self.has_b {
            self.mv_data.truncate(0);
        }

        let save_b_data = pinfo.mode.is_ref() && self.may_have_b_frames;
        if save_b_data {
            self.mv_data.truncate(0);
        }
        let is_b = pinfo.mode == Type::B;

        let tsdiff = if pinfo.is_pb() { pinfo.ts.wrapping_sub(self.last_ts) >> 1 }
                     else { self.last_ts.wrapping_sub(self.next_ts) >> 1 };
        let bsdiff = if pinfo.is_pb() { (pinfo.get_pbinfo().get_trb() as u16) << 7 }
                     else { pinfo.ts.wrapping_sub(self.next_ts) >> 1 };

        let fmt = formats::YUV420_FORMAT;
        let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
        let bufinfo = alloc_video_buffer(vinfo, 4)?;
        let mut buf = bufinfo.get_vbuf().unwrap();

        let mut slice = if self.is_gob {
                SliceInfo::get_default_slice(&pinfo)
            } else {
                bd.decode_slice_header(&pinfo)?
            };
        mvi.reset(self.mb_w, 0, pinfo.get_mvmode());
        if is_b || pinfo.is_pb() {
            mvi2.reset(self.mb_w, 0, pinfo.get_mvmode());
        }
        cbpi.reset(self.mb_w);

        let mut blk: [[i16; 64]; 6] = [[0; 64]; 6];
        let mut sstate = SliceState::new(pinfo.mode == Type::I);
        let mut mb_pos = 0;
        let apply_acpred = (pinfo.mode == Type::I) && pinfo.plusinfo.is_some() && pinfo.plusinfo.unwrap().aic;
        if apply_acpred {
            self.pred_coeffs.truncate(0);
            self.pred_coeffs.resize(self.mb_w * self.mb_h, ZERO_PRED_COEFFS);
        }
        sstate.quant = slice.quant;
        for mb_y in 0..self.mb_h {
            for mb_x in 0..self.mb_w {
                for i in 0..6 { for j in 0..64 { blk[i][j] = 0; } }

                if slice.is_at_end(mb_pos) || (slice.needs_check() && mb_pos > 0 && bd.is_slice_end()) {
                    slice = bd.decode_slice_header(&pinfo)?;
                    if !self.is_gob && self.slice_reset {
                        mvi.reset(self.mb_w, mb_x, pinfo.get_mvmode());
                        if is_b || pinfo.is_pb() {
                            mvi2.reset(self.mb_w, mb_x, pinfo.get_mvmode());
                        }
                        cbpi.reset(self.mb_w);
                        sstate.reset_slice(mb_x, mb_y);
                        sstate.quant = slice.quant;
                    }
                }

                let binfo = bd.decode_block_header(&pinfo, &slice, &sstate)?;
                let cbp = binfo.get_cbp();
                cbpi.set_cbp(mb_x, cbp);
                cbpi.set_q(mb_x, binfo.get_q());
                sstate.quant = binfo.get_q();
                if binfo.is_intra() {
                    if save_b_data {
                        self.mv_data.push(BlockMVInfo::Intra);
                    }
                    for i in 0..6 {
                        bd.decode_block_intra(&binfo, &sstate, binfo.get_q(), i, (cbp & (1 << (5 - i))) != 0, &mut blk[i])?;
                        if apply_acpred && (binfo.acpred != ACPredMode::None) {
                            let has_b = (i == 1) || (i == 3) || !sstate.first_mb;
                            let has_a = (i == 2) || (i == 3) || !sstate.first_line;
                            let (b_mb, b_blk) = if has_b {
                                    if (i == 1) || (i == 3) {
                                        (mb_pos, i - 1)
                                    } else if i < 4 {
                                        (mb_pos - 1, i + 1)
                                    } else {
                                        (mb_pos - 1, i)
                                    }
                                } else { (0, 0) };
                            let (a_mb, a_blk) = if has_a {
                                    if (i == 2) || (i == 3) {
                                        (mb_pos, i - 2)
                                    } else if i < 4 {
                                        (mb_pos - self.mb_w, i + 2)
                                    } else {
                                        (mb_pos - self.mb_w, i)
                                    }
                                } else { (0, 0) };
                            match binfo.acpred {
                                ACPredMode::DC   => {
                                            let dc;
                                            if has_a && has_b {
                                                dc = (self.pred_coeffs[b_mb].hor[b_blk][0] + self.pred_coeffs[a_mb].ver[a_blk][0]) / 2;
                                            } else if has_a {
                                                dc = self.pred_coeffs[a_mb].ver[a_blk][0];
                                            } else if has_b {
                                                dc = self.pred_coeffs[b_mb].hor[b_blk][0];
                                            } else {
                                                dc = 1024;
                                            }
                                            blk[i][0] = clip_dc(blk[i][0] + dc);
                                        },
                                ACPredMode::Hor  => {
                                        if has_b {
                                            for k in 0..8 {
                                                blk[i][k * 8] += self.pred_coeffs[b_mb].hor[b_blk][k];
                                            }
                                            for k in 1..8 {
                                                blk[i][k * 8] = clip_ac(blk[i][k * 8]);
                                            }
                                        } else {
                                            blk[i][0] += 1024;
                                        }
                                        blk[i][0] = clip_dc(blk[i][0]);
                                    },
                                ACPredMode::Ver  => {
                                        if has_a {
                                            for k in 0..8 {
                                                blk[i][k] += self.pred_coeffs[a_mb].ver[a_blk][k];
                                            }
                                            for k in 1..8 {
                                                blk[i][k] = clip_ac(blk[i][k]);
                                            }
                                        } else {
                                            blk[i][0] += 1024;
                                        }
                                        blk[i][0] = clip_dc(blk[i][0]);
                                    },
                                ACPredMode::None => {},
                            };
                            for t in 0..8 { self.pred_coeffs[mb_pos].hor[i][t] = blk[i][t * 8]; }
                            for t in 0..8 { self.pred_coeffs[mb_pos].ver[i][t] = blk[i][t]; }
                        }
                        bdsp.idct(&mut blk[i]);
                    }
                    blockdsp::put_blocks(&mut buf, mb_x, mb_y, &blk);
                    mvi.set_zero_mv(mb_x);
                    if is_b {
                        mvi2.set_zero_mv(mb_x);
                    } else if pinfo.is_pb() {
                        mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
                    }
                } else if (binfo.mode != Type::B) && !binfo.is_skipped() {
                    if binfo.get_num_mvs() == 1 {
                        let mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
                        if save_b_data {
                            self.mv_data.push(BlockMVInfo::Inter_1MV(mv));
                        }
                        if let Some(ref srcbuf) = self.ipbs.get_lastref() {
                            bdsp.copy_blocks(&mut buf, srcbuf, mb_x * 16, mb_y * 16, 16, 16, mv);
                        }
                        if pinfo.is_pb() {
                            mvi2.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
                        }
                    } else {
                        let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV];
                        for blk_no in 0..4 {
                            mv[blk_no] = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
                            if let Some(ref srcbuf) = self.ipbs.get_lastref() {
                                bdsp.copy_blocks(&mut buf, srcbuf,
                                                 mb_x * 16 + (blk_no & 1) * 8,
                                                 mb_y * 16 + (blk_no & 2) * 4, 8, 8, mv[blk_no]);
                            }
                        }
                        if pinfo.is_pb() {
                            for blk_no in 0..4 {
                                mvi2.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
                            }
                        }
                        if save_b_data {
                            self.mv_data.push(BlockMVInfo::Inter_4MV(mv));
                        }
                    }
                    for i in 0..6 {
                        bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?;
                        bdsp.idct(&mut blk[i]);
                    }
                    blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
                    if is_b && !pinfo.is_pb() {
                        mvi2.set_zero_mv(mb_x);
                    }
                } else if binfo.mode != Type::B {
                    self.mv_data.push(BlockMVInfo::Inter_1MV(ZERO_MV));
                    mvi.set_zero_mv(mb_x);
                    if is_b || pinfo.is_pb() {
                        mvi2.set_zero_mv(mb_x);
                    }
                    if let Some(ref srcbuf) = self.ipbs.get_lastref() {
                        bdsp.copy_blocks(&mut buf, srcbuf, mb_x * 16, mb_y * 16, 16, 16, ZERO_MV);
                    }
                } else {
                    let ref_mv_info = self.mv_data[mb_pos];
                    let has_fwd = binfo.get_num_mvs() > 0;
                    let has_bwd = binfo.get_num_mvs2() > 0;
//todo refactor
                    if has_fwd || has_bwd {
                        let fwd_mv;
                        if has_fwd {
                            fwd_mv = mvi.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
                        } else {
                            fwd_mv = ZERO_MV;
                            mvi.set_zero_mv(mb_x);
                        }
                        let bwd_mv;
                        if has_bwd {
                            bwd_mv = mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
                        } else {
                            bwd_mv = ZERO_MV;
                            mvi2.set_zero_mv(mb_x);
                        }
                        if let (Some(ref fwd_buf), Some(ref bck_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
                            if has_fwd && has_bwd {
                                bdsp.copy_blocks(&mut buf, fwd_buf, mb_x * 16, mb_y * 16, 16, 16, fwd_mv);
                                bdsp.avg_blocks (&mut buf, bck_buf, mb_x * 16, mb_y * 16, 16, 16, bwd_mv);
                            } else if has_fwd {
                                bdsp.copy_blocks(&mut buf, fwd_buf, mb_x * 16, mb_y * 16, 16, 16, fwd_mv);
                            } else {
                                bdsp.copy_blocks(&mut buf, bck_buf, mb_x * 16, mb_y * 16, 16, 16, bwd_mv);
                            }
                        }
                    } else {
                        if let BlockMVInfo::Inter_4MV(mvs) = ref_mv_info {
                            for blk_no in 0..4 {
                                let ref_mv = mvs[blk_no];
                                let ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff);
                                let ref_mv_bwd = ref_mv - ref_mv_fwd;
                                let xoff = mb_x * 16 + (blk_no & 1) * 8;
                                let yoff = mb_y * 16 + (blk_no & 2) * 4;
                                if let (Some(ref fwd_buf), Some(ref bck_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
                                    bdsp.copy_blocks(&mut buf, fwd_buf, xoff, yoff, 8, 8, ref_mv_fwd);
                                    bdsp.avg_blocks (&mut buf, bck_buf, xoff, yoff, 8, 8, ref_mv_bwd);
                                }
                            }
                        } else {
                            let ref_mv = if let BlockMVInfo::Inter_1MV(mv_) = ref_mv_info { mv_ } else { ZERO_MV };
                            let ref_mv_fwd = ref_mv.scale(bsdiff, tsdiff);
                            let ref_mv_bwd = MV::b_sub(ref_mv, ref_mv_fwd, ZERO_MV, bsdiff, tsdiff);

                            if let (Some(ref fwd_buf), Some(ref bck_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
                                bdsp.copy_blocks(&mut buf, fwd_buf, mb_x * 16, mb_y * 16, 16, 16, ref_mv_fwd);
                                bdsp.avg_blocks (&mut buf, bck_buf, mb_x * 16, mb_y * 16, 16, 16, ref_mv_bwd);
                            }
                        }
                        mvi.set_zero_mv(mb_x);
                        mvi2.set_zero_mv(mb_x);
                    }
                    if cbp != 0 {
                        for i in 0..6 {
                            bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?;
                            bdsp.idct(&mut blk[i]);
                        }
                        blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
                    }
                }
                if pinfo.is_pb() {
                    let mut b_mb = BMB::new();
                    let cbp = binfo.get_cbp_b();
                    let bq = (((pinfo.get_pbinfo().get_dbquant() + 5) as u16) * (binfo.get_q() as u16)) >> 2;
                    let bquant;
                    if bq < 1 { bquant = 1; }
                    else if bq > 31 { bquant = 31; }
                    else { bquant = bq as u8; }

                    b_mb.cbp = cbp;
                    for i in 0..6 {
                        bd.decode_block_inter(&binfo, &sstate, bquant, i, (cbp & (1 << (5 - i))) != 0, &mut b_mb.blk[i])?;
                        bdsp.idct(&mut b_mb.blk[i]);
                    }

                    let is_fwd = binfo.is_b_fwd();
                    b_mb.fwd = is_fwd;
                    if binfo.get_num_mvs() != 4 {
                        let ref_mv = mvi2.get_mv(mb_x, 0);
                        let b_mv = if binfo.is_intra() { binfo.get_mv2(1) } else { binfo.get_mv2(0) };
                        let src_mv = if is_fwd { ZERO_MV } else { ref_mv.scale(bsdiff, tsdiff) };
                        let mv_f = MV::add_umv(src_mv, b_mv, pinfo.get_mvmode());
                        let mv_b = MV::b_sub(ref_mv, mv_f, b_mv, bsdiff, tsdiff);
                        b_mb.mv_f[0] = mv_f;
                        b_mb.mv_b[0] = mv_b;
                        b_mb.num_mv = 1;
                    } else {
                        for blk_no in 0..4 {
                            let src_mv = if is_fwd { ZERO_MV } else { mvi2.get_mv(mb_x, blk_no).scale(bsdiff, tsdiff) };
                            let mv_f = MV::add_umv(src_mv, binfo.get_mv2(0), pinfo.get_mvmode());
                            let mv_b = MV::b_sub(mvi2.get_mv(mb_x, blk_no), mv_f, binfo.get_mv2(0), bsdiff, tsdiff);
                            b_mb.mv_f[blk_no] = mv_f;
                            b_mb.mv_b[blk_no] = mv_b;
                        }
                        b_mb.num_mv = 4;
                    }
                    self.b_data.push(b_mb);
                }
                sstate.next_mb();
                mb_pos += 1;
            }
            if let Some(plusinfo) = pinfo.plusinfo {
                if plusinfo.deblock {
                    bdsp.filter_row(&mut buf, mb_y, self.mb_w, &cbpi);
                }
            }
            mvi.update_row();
            if is_b || pinfo.is_pb() {
                mvi2.update_row();
            }
            cbpi.update_row();
            sstate.new_row();
        }

        if pinfo.mode.is_ref() {
            self.ipbs.add_frame(buf);
            self.next_ts = self.last_ts;
            self.last_ts = pinfo.ts;
            self.tsdiff  = tsdiff;
        }

        Ok(bufinfo)
    }
    pub fn flush(&mut self) {
        self.ipbs.clear();
    }

    pub fn get_bframe(&mut self, bdsp: &BlockDSP) -> DecoderResult<NABufferType> {
        if !self.has_b || self.ipbs.get_lastref().is_none() || self.ipbs.get_nextref().is_none() {
            return Err(DecoderError::MissingReference);
        }
        self.has_b = false;

        let fmt = formats::YUV420_FORMAT;
        let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
        let bufinfo = alloc_video_buffer(vinfo, 4)?;
        let mut b_buf = bufinfo.get_vbuf().unwrap();

        if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
            recon_b_frame(&mut b_buf, fwd_buf, bck_buf, self.mb_w, self.mb_h, self.b_data.as_slice(), bdsp);
        }

        self.b_data.truncate(0);
        Ok(bufinfo)
    }
}

fn recon_b_frame(b_buf: &mut NAVideoBuffer<u8>, bck_buf: &NAVideoBuffer<u8>, fwd_buf: &NAVideoBuffer<u8>,
                 mb_w: usize, mb_h: usize, b_data: &[BMB], bdsp: &BlockDSP) {
    let mut cbpi = CBPInfo::new();
    let mut cur_mb = 0;
    cbpi.reset(mb_w);
    for mb_y in 0..mb_h {
        for mb_x in 0..mb_w {
            let num_mv = b_data[cur_mb].num_mv;
            let is_fwd = b_data[cur_mb].fwd;
            let cbp    = b_data[cur_mb].cbp;
            cbpi.set_cbp(mb_x, cbp);
            if num_mv == 1 {
                bdsp.copy_blocks(b_buf, fwd_buf, mb_x * 16, mb_y * 16, 16, 16, b_data[cur_mb].mv_b[0]);
                if !is_fwd {
                    bdsp.avg_blocks(b_buf, bck_buf, mb_x * 16, mb_y * 16, 16, 16, b_data[cur_mb].mv_f[0]);
                }
            } else {
                for blk_no in 0..4 {
                    let xpos = mb_x * 16 + (blk_no & 1) * 8;
                    let ypos = mb_y * 16 + (blk_no & 2) * 4;
                    bdsp.copy_blocks(b_buf, fwd_buf, xpos, ypos, 8, 8, b_data[cur_mb].mv_b[blk_no]);
                    if !is_fwd {
                        bdsp.avg_blocks(b_buf, bck_buf, xpos, ypos, 8, 8, b_data[cur_mb].mv_f[blk_no]);
                    }
                }
            }
            if cbp != 0 {
                blockdsp::add_blocks(b_buf, mb_x, mb_y, &b_data[cur_mb].blk);
            }
            cur_mb += 1;
        }
        cbpi.update_row();
    }
}