rv10: fix coefficient quantisation
[nihav.git] / nihav-codec-support / src / codecs / h263 / decoder.rs
CommitLineData
32007ad9 1//use std::mem;
b4d5b851
KS
2use nihav_core::codecs::DecoderError;
3use nihav_core::frame::*;
88c58b1b
KS
4use super::super::*;
5use super::super::blockdsp;
cf64af13 6use super::*;
7b6e074c 7//use super::code::*;
b4d5b851 8use nihav_core::formats;
cf64af13 9
cf64af13
KS
10#[allow(dead_code)]
11struct MVInfo {
12 mv: Vec<MV>,
13 mb_w: usize,
14 mb_stride: usize,
15 mb_start: usize,
16 top: bool,
2324f30b 17 mvmode: MVMode,
cf64af13
KS
18}
19
20impl MVInfo {
2324f30b
KS
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) {
cf64af13
KS
23 self.mb_start = mb_start;
24 self.mb_w = mb_w;
7588f4f2 25 self.mb_stride = mb_w * 2 + 2;
cf64af13 26 self.mv.resize(self.mb_stride * 3, ZERO_MV);
2324f30b 27 self.mvmode = mvmode;
cf64af13
KS
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)]
32007ad9 37 fn predict(&mut self, mb_x: usize, blk_no: usize, use4: bool, diff: MV, first_line: bool, first_mb: bool) -> MV {
cf64af13
KS
38 let A;
39 let B;
40 let C;
41 let last = mb_x == self.mb_w - 1;
cf64af13
KS
42 match blk_no {
43 0 => {
44 if mb_x != self.mb_start {
32007ad9
KS
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 };
cf64af13
KS
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];
32007ad9 54 B = if !first_line { self.mv[mb_x * 2 + 1] } else { A };
effbebce 55 C = if !first_line && !last { self.mv[mb_x * 2 + 2] } else { ZERO_MV/*A*/ };
cf64af13
KS
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 }
cf64af13 69 let pred_mv = MV::pred(A, B, C);
2324f30b 70 let new_mv = MV::add_umv(pred_mv, diff, self.mvmode);
cf64af13
KS
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 }
1a151e53 85
cf64af13
KS
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 }
effbebce
KS
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 }
cf64af13
KS
97}
98
21c16d5f
KS
99#[allow(dead_code)]
100#[derive(Clone,Copy)]
101struct 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
110impl 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
ef3209c7
KS
114#[derive(Clone,Copy)]
115struct PredCoeffs {
116 hor: [[i16; 8]; 6],
117 ver: [[i16; 8]; 6],
118}
119
120const ZERO_PRED_COEFFS: PredCoeffs = PredCoeffs { hor: [[0; 8]; 6], ver: [[0; 8]; 6] };
121
88c58b1b 122pub struct H263BaseDecoder {
cf64af13
KS
123 w: usize,
124 h: usize,
125 mb_w: usize,
126 mb_h: usize,
127 num_mb: usize,
128 ftype: Type,
32007ad9
KS
129 ipbs: IPBShuffler,
130 next_ts: u16,
131 last_ts: u16,
132 tsdiff: u16,
21c16d5f
KS
133 has_b: bool,
134 b_data: Vec<BMB>,
ef3209c7 135 pred_coeffs: Vec<PredCoeffs>,
42a59352 136 is_gob: bool,
b20ff290 137 slice_reset: bool,
32007ad9
KS
138 may_have_b_frames: bool,
139 mv_data: Vec<BlockMVInfo>,
ef3209c7
KS
140}
141
142#[inline]
143fn clip_dc(dc: i16) -> i16 {
acf6187c
KS
144 if dc <= 0 { 0 }
145 else if dc > 2046 { 2047 }
146 else { dc | 1 }
ef3209c7
KS
147}
148
149#[inline]
150fn clip_ac(ac: i16) -> i16 {
151 if ac < -2048 { -2048 }
152 else if ac > 2047 { 2047 }
153 else { ac }
cf64af13
KS
154}
155
156#[allow(dead_code)]
88c58b1b 157impl H263BaseDecoder {
b20ff290 158 pub fn new_with_opts(is_gob: bool, slice_reset: bool, may_have_b_frames: bool) -> Self {
88c58b1b 159 H263BaseDecoder{
cf64af13
KS
160 w: 0, h: 0, mb_w: 0, mb_h: 0, num_mb: 0,
161 ftype: Type::Special,
32007ad9
KS
162 ipbs: IPBShuffler::new(),
163 last_ts: 0, next_ts: 0, tsdiff: 0,
21c16d5f 164 has_b: false, b_data: Vec::new(),
ef3209c7 165 pred_coeffs: Vec::new(),
e243ceb4
KS
166 is_gob, slice_reset,
167 may_have_b_frames,
32007ad9 168 mv_data: Vec::new(),
cf64af13
KS
169 }
170 }
32007ad9 171 pub fn new(is_gob: bool) -> Self {
b20ff290 172 Self::new_with_opts(is_gob, true, false)
32007ad9
KS
173 }
174 pub fn new_b_frames(is_gob: bool) -> Self {
b20ff290 175 Self::new_with_opts(is_gob, true, true)
32007ad9 176 }
cf64af13
KS
177
178 pub fn is_intra(&self) -> bool { self.ftype == Type::I }
2eb21477
KS
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 }
cf64af13
KS
189 pub fn get_dimensions(&self) -> (usize, usize) { (self.w, self.h) }
190
2a2aa421 191 pub fn parse_frame(&mut self, bd: &mut BlockDecoder, bdsp: &BlockDSP) -> DecoderResult<NABufferType> {
cf64af13 192 let pinfo = bd.decode_pichdr()?;
21c16d5f 193 let mut mvi = MVInfo::new();
15facd16 194 let mut mvi2 = MVInfo::new();
f2f1afb2 195 let mut cbpi = CBPInfo::new();
cf64af13
KS
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;
21c16d5f
KS
204 self.has_b = pinfo.is_pb();
205
206 if self.has_b {
32007ad9 207 self.mv_data.truncate(0);
21c16d5f 208 }
cf64af13 209
32007ad9
KS
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 }
15facd16 214 let is_b = pinfo.mode == Type::B;
cf64af13 215
df05c438
KS
216 if is_b && (self.mv_data.len() < self.mb_w * self.mb_h) {
217 return Err(DecoderError::MissingReference);
218 }
219
88e9abd4
KS
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 };
32007ad9
KS
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 };
21c16d5f 224
cf64af13
KS
225 let fmt = formats::YUV420_FORMAT;
226 let vinfo = NAVideoInfo::new(self.w, self.h, false, fmt);
e243ceb4 227 let bufinfo = alloc_video_buffer(vinfo, 4)?;
cf64af13
KS
228 let mut buf = bufinfo.get_vbuf().unwrap();
229
42a59352 230 let mut slice = if self.is_gob {
2a2aa421
KS
231 SliceInfo::get_default_slice(&pinfo)
232 } else {
233 bd.decode_slice_header(&pinfo)?
234 };
2324f30b 235 mvi.reset(self.mb_w, 0, pinfo.get_mvmode());
effbebce 236 if is_b || pinfo.is_pb() {
15facd16
KS
237 mvi2.reset(self.mb_w, 0, pinfo.get_mvmode());
238 }
f2f1afb2 239 cbpi.reset(self.mb_w);
cf64af13
KS
240
241 let mut blk: [[i16; 64]; 6] = [[0; 64]; 6];
2a2aa421
KS
242 let mut sstate = SliceState::new(pinfo.mode == Type::I);
243 let mut mb_pos = 0;
ef3209c7
KS
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 }
43c8b55e 249 sstate.quant = slice.quant;
cf64af13
KS
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
2a2aa421 254 if slice.is_at_end(mb_pos) || (slice.needs_check() && mb_pos > 0 && bd.is_slice_end()) {
cf64af13 255 slice = bd.decode_slice_header(&pinfo)?;
b20ff290 256 if !self.is_gob && self.slice_reset {
6f21f5f4 257 mvi.reset(self.mb_w, mb_x, pinfo.get_mvmode());
effbebce 258 if is_b || pinfo.is_pb() {
15facd16
KS
259 mvi2.reset(self.mb_w, mb_x, pinfo.get_mvmode());
260 }
6f21f5f4 261 cbpi.reset(self.mb_w);
32007ad9 262 sstate.reset_slice(mb_x, mb_y);
43c8b55e 263 sstate.quant = slice.quant;
6f21f5f4 264 }
cf64af13
KS
265 }
266
2a2aa421 267 let binfo = bd.decode_block_header(&pinfo, &slice, &sstate)?;
cf64af13 268 let cbp = binfo.get_cbp();
f2f1afb2
KS
269 cbpi.set_cbp(mb_x, cbp);
270 cbpi.set_q(mb_x, binfo.get_q());
43c8b55e 271 sstate.quant = binfo.get_q();
cf64af13 272 if binfo.is_intra() {
32007ad9
KS
273 if save_b_data {
274 self.mv_data.push(BlockMVInfo::Intra);
275 }
cf64af13 276 for i in 0..6 {
2a2aa421 277 bd.decode_block_intra(&binfo, &sstate, binfo.get_q(), i, (cbp & (1 << (5 - i))) != 0, &mut blk[i])?;
ef3209c7 278 if apply_acpred && (binfo.acpred != ACPredMode::None) {
2eb21477 279 let has_b = (i == 1) || (i == 3) || !sstate.first_mb;
6f21f5f4 280 let has_a = (i == 2) || (i == 3) || !sstate.first_line;
ef3209c7
KS
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 }
7b6e074c 344 bdsp.idct(&mut blk[i]);
cf64af13
KS
345 }
346 blockdsp::put_blocks(&mut buf, mb_x, mb_y, &blk);
347 mvi.set_zero_mv(mb_x);
15facd16
KS
348 if is_b {
349 mvi2.set_zero_mv(mb_x);
effbebce
KS
350 } else if pinfo.is_pb() {
351 mvi2.predict(mb_x, 0, false, binfo.get_mv2(0), sstate.first_line, sstate.first_mb);
15facd16 352 }
2eb21477 353 } else if (binfo.mode != Type::B) && !binfo.is_skipped() {
cf64af13 354 if binfo.get_num_mvs() == 1 {
32007ad9
KS
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() {
401b2b60 360 bdsp.copy_blocks(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, mv);
cf64af13 361 }
effbebce
KS
362 if pinfo.is_pb() {
363 mvi2.predict(mb_x, 0, false, binfo.get_mv(0), sstate.first_line, sstate.first_mb);
364 }
cf64af13 365 } else {
32007ad9 366 let mut mv: [MV; 4] = [ZERO_MV, ZERO_MV, ZERO_MV, ZERO_MV];
cf64af13 367 for blk_no in 0..4 {
32007ad9 368 mv[blk_no] = mvi.predict(mb_x, blk_no, true, binfo.get_mv(blk_no), sstate.first_line, sstate.first_mb);
401b2b60
KS
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);
cf64af13 372 }
effbebce
KS
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 }
32007ad9
KS
378 if save_b_data {
379 self.mv_data.push(BlockMVInfo::Inter_4MV(mv));
380 }
cf64af13
KS
381 }
382 for i in 0..6 {
2a2aa421 383 bd.decode_block_inter(&binfo, &sstate, binfo.get_q(), i, ((cbp >> (5 - i)) & 1) != 0, &mut blk[i])?;
7b6e074c 384 bdsp.idct(&mut blk[i]);
cf64af13
KS
385 }
386 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
effbebce 387 if is_b && !pinfo.is_pb() {
d609e5d5
KS
388 mvi2.set_zero_mv(mb_x);
389 }
2eb21477 390 } else if binfo.mode != Type::B {
32007ad9 391 self.mv_data.push(BlockMVInfo::Inter_1MV(ZERO_MV));
cf64af13 392 mvi.set_zero_mv(mb_x);
effbebce 393 if is_b || pinfo.is_pb() {
d609e5d5
KS
394 mvi2.set_zero_mv(mb_x);
395 }
32007ad9 396 if let Some(ref srcbuf) = self.ipbs.get_lastref() {
401b2b60 397 bdsp.copy_blocks(&mut buf, srcbuf.clone(), mb_x * 16, mb_y * 16, ZERO_MV);
cf64af13 398 }
2eb21477 399 } else {
32007ad9
KS
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;
32007ad9 403//todo refactor
15facd16
KS
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 {
401b2b60
KS
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);
15facd16 423 } else if has_fwd {
401b2b60 424 bdsp.copy_blocks(&mut buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, fwd_mv);
15facd16 425 } else {
401b2b60 426 bdsp.copy_blocks(&mut buf, bck_buf.clone(), mb_x * 16, mb_y * 16, bwd_mv);
32007ad9
KS
427 }
428 }
429 } else {
15facd16 430 if let BlockMVInfo::Inter_4MV(mvs) = ref_mv_info {
401b2b60
KS
431 let mut mv_f = [ZERO_MV; 4];
432 let mut mv_b = [ZERO_MV; 4];
15facd16
KS
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;
401b2b60
KS
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);
15facd16
KS
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()) {
401b2b60
KS
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);
32007ad9
KS
452 }
453 }
15facd16
KS
454 mvi.set_zero_mv(mb_x);
455 mvi2.set_zero_mv(mb_x);
32007ad9 456 }
2eb21477
KS
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])?;
7b6e074c 460 bdsp.idct(&mut blk[i]);
2eb21477 461 }
32007ad9 462 blockdsp::add_blocks(&mut buf, mb_x, mb_y, &blk);
2eb21477 463 }
cf64af13 464 }
21c16d5f
KS
465 if pinfo.is_pb() {
466 let mut b_mb = BMB::new();
cf64af13 467 let cbp = binfo.get_cbp_b();
21c16d5f
KS
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;
cf64af13 475 for i in 0..6 {
2a2aa421 476 bd.decode_block_inter(&binfo, &sstate, bquant, i, (cbp & (1 << (5 - i))) != 0, &mut b_mb.blk[i])?;
7b6e074c 477 bdsp.idct(&mut b_mb.blk[i]);
cf64af13 478 }
21c16d5f 479
effbebce 480 let is_fwd = binfo.is_b_fwd();
21c16d5f 481 b_mb.fwd = is_fwd;
effbebce
KS
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);
21c16d5f
KS
488 b_mb.mv_f[0] = mv_f;
489 b_mb.mv_b[0] = mv_b;
effbebce 490 b_mb.num_mv = 1;
21c16d5f
KS
491 } else {
492 for blk_no in 0..4 {
effbebce 493 let src_mv = if is_fwd { ZERO_MV } else { mvi2.get_mv(mb_x, blk_no).scale(bsdiff, tsdiff) };
2324f30b 494 let mv_f = MV::add_umv(src_mv, binfo.get_mv2(0), pinfo.get_mvmode());
effbebce 495 let mv_b = MV::b_sub(mvi2.get_mv(mb_x, blk_no), mv_f, binfo.get_mv2(0), bsdiff, tsdiff);
21c16d5f
KS
496 b_mb.mv_f[blk_no] = mv_f;
497 b_mb.mv_b[blk_no] = mv_b;
cf64af13 498 }
effbebce 499 b_mb.num_mv = 4;
cf64af13 500 }
21c16d5f 501 self.b_data.push(b_mb);
cf64af13 502 }
2a2aa421
KS
503 sstate.next_mb();
504 mb_pos += 1;
cf64af13 505 }
2a2aa421
KS
506 if let Some(plusinfo) = pinfo.plusinfo {
507 if plusinfo.deblock {
508 bdsp.filter_row(&mut buf, mb_y, self.mb_w, &cbpi);
509 }
f2f1afb2 510 }
cf64af13 511 mvi.update_row();
effbebce 512 if is_b || pinfo.is_pb() {
d609e5d5
KS
513 mvi2.update_row();
514 }
f2f1afb2 515 cbpi.update_row();
2a2aa421 516 sstate.new_row();
cf64af13 517 }
32007ad9
KS
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
cf64af13
KS
526 Ok(bufinfo)
527 }
d8ee06da
KS
528 pub fn flush(&mut self) {
529 self.ipbs.clear();
530 }
cf64af13 531
2a2aa421 532 pub fn get_bframe(&mut self, bdsp: &BlockDSP) -> DecoderResult<NABufferType> {
fdb4b2fb 533 if !self.has_b || self.ipbs.get_lastref().is_none() || self.ipbs.get_nextref().is_none() {
21c16d5f
KS
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);
e243ceb4 540 let bufinfo = alloc_video_buffer(vinfo, 4)?;
401b2b60 541 let b_buf = bufinfo.get_vbuf().unwrap();
21c16d5f 542
32007ad9 543 if let (Some(ref bck_buf), Some(ref fwd_buf)) = (self.ipbs.get_nextref(), self.ipbs.get_lastref()) {
401b2b60 544 recon_b_frame(b_buf, fwd_buf.clone(), bck_buf.clone(), self.mb_w, self.mb_h, self.b_data.as_slice(), bdsp);
21c16d5f
KS
545 }
546
547 self.b_data.truncate(0);
548 Ok(bufinfo)
549 }
550}
551
401b2b60 552fn recon_b_frame(mut b_buf: NAVideoBufferRef<u8>, bck_buf: NAVideoBufferRef<u8>, fwd_buf: NAVideoBufferRef<u8>,
fdb4b2fb 553 mb_w: usize, mb_h: usize, b_data: &[BMB], bdsp: &BlockDSP) {
f2f1afb2 554 let mut cbpi = CBPInfo::new();
21c16d5f 555 let mut cur_mb = 0;
f2f1afb2 556 cbpi.reset(mb_w);
21c16d5f
KS
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;
f2f1afb2
KS
561 let cbp = b_data[cur_mb].cbp;
562 cbpi.set_cbp(mb_x, cbp);
563 if num_mv == 1 {
401b2b60 564 bdsp.copy_blocks(&mut b_buf, fwd_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_b[0]);
21c16d5f 565 if !is_fwd {
401b2b60 566 bdsp.avg_blocks(&mut b_buf, bck_buf.clone(), mb_x * 16, mb_y * 16, b_data[cur_mb].mv_f[0]);
21c16d5f
KS
567 }
568 } else {
401b2b60
KS
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);
21c16d5f
KS
572 }
573 }
f2f1afb2 574 if cbp != 0 {
401b2b60 575 blockdsp::add_blocks(&mut b_buf, mb_x, mb_y, &b_data[cur_mb].blk);
21c16d5f
KS
576 }
577 cur_mb += 1;
cf64af13 578 }
f2f1afb2 579 cbpi.update_row();
cf64af13
KS
580 }
581}