]> git.nihav.org Git - nihav.git/blame_incremental - cabac.rs
add MPEG-4 ASP decoder
[nihav.git] / cabac.rs
... / ...
CommitLineData
1//use nihav_core::codecs::{DecoderResult, DecoderError};
2
3use super::*;
4use super::cabac_coder::*;
5use super::dsp::{CHROMA_DC_SCAN, ZIGZAG, ZIGZAG1, ZIGZAG8X8};
6use super::slice::SliceHeader;
7
8pub fn cabac_decode_mbskip(cabac: &mut CABAC, sstate: &SliceState, slice_hdr: &SliceHeader) -> bool {
9 let skip_idx = if slice_hdr.slice_type.is_p() { 11 } else { 24 };
10 let mut mb_skip_ctx = 0;
11 let left_mbt = sstate.get_left_mb().mb_type;
12 let top_mbt = sstate.get_top_mb().mb_type;
13 if left_mbt != CompactMBType::None && !left_mbt.is_skip() {
14 mb_skip_ctx += 1;
15 }
16 if top_mbt != CompactMBType::None && !top_mbt.is_skip() {
17 mb_skip_ctx += 1;
18 }
19 if !slice_hdr.slice_type.is_intra() {
20 cabac.decode_bit(skip_idx + mb_skip_ctx)
21 } else {
22 false
23 }
24}
25
26fn decode_i_type(cabac: &mut CABAC, start: usize, ctx: usize) -> MBType {
27 if !cabac.decode_bit(start + ctx) {
28 MBType::Intra4x4
29 } else if !cabac.decode_terminate() {
30 let cbpy = if cabac.decode_bit(start + 3) { 0xF } else { 0x0 };
31 let cbpc = cabac.decode_012(start + 4);
32 let ipred = cabac.decode_bits(start + 6, start + 7, 2);
33
34 MBType::Intra16x16(ipred, cbpy, cbpc)
35 } else {
36 MBType::PCM
37 }
38}
39
40fn decode_i_type_inter(cabac: &mut CABAC, start: usize) -> MBType {
41 if !cabac.decode_bit(start) {
42 MBType::Intra4x4
43 } else if !cabac.decode_terminate() {
44 let cbpy = if cabac.decode_bit(start + 1) { 0xF } else { 0x0 };
45 let cbpc = if !cabac.decode_bit(start + 2) {
46 0
47 } else if !cabac.decode_bit(start + 2) {
48 1
49 } else {
50 2
51 };
52 let ipred = cabac.decode_bits(start + 3, start + 3, 2);
53
54 MBType::Intra16x16(ipred, cbpy, cbpc)
55 } else {
56 MBType::PCM
57 }
58}
59
60fn remap_si_mbtype(mbtype: MBType) -> MBType {
61 match mbtype {
62 MBType::Intra16x16(0, 0, 0) => MBType::Intra4x4,
63 MBType::Intra16x16(imode, cbpy, cbpc) => {
64 let idx = imode + if cbpy != 0 { 12 } else { 0 } + cbpc * 4 - 1;
65 let nimode = idx & 3;
66 let (ncbpy, ncbpc) = if (idx >> 2) >= 3 {
67 (0xF, (idx >> 2) - 3)
68 } else {
69 (0x0, idx >> 2)
70 };
71 MBType::Intra16x16(nimode, ncbpy, ncbpc)
72 },
73 MBType::PCM => MBType::Intra16x16(3, 1, 2),
74 _ => mbtype,
75 }
76}
77
78pub fn cabac_decode_mb_type(cabac: &mut CABAC, slice_hdr: &SliceHeader, sstate: &SliceState) -> MBType {
79 match slice_hdr.slice_type {
80 SliceType::I | SliceType::SI => {
81 let mut ctx = 0;
82 if sstate.get_left_mb().mb_type.is_intra16orpcm() {
83 ctx += 1;
84 }
85 if sstate.get_top_mb().mb_type.is_intra16orpcm() {
86 ctx += 1;
87 }
88 let mbtype = decode_i_type(cabac, 3, ctx);
89 if slice_hdr.slice_type == SliceType::I {
90 mbtype
91 } else {
92 remap_si_mbtype(mbtype)
93 }
94 },
95 SliceType::P | SliceType::SP => {
96 if cabac.decode_bit(14) {
97 decode_i_type_inter(cabac, 17)
98 } else if !cabac.decode_bit(15) {
99 if !cabac.decode_bit(16) {
100 MBType::P16x16
101 } else {
102 MBType::P8x8
103 }
104 } else {
105 if !cabac.decode_bit(17) {
106 MBType::P8x16
107 } else {
108 MBType::P16x8
109 }
110 }
111 },
112 SliceType::B => {
113 let mut ctx = 0;
114 if !sstate.get_left_mb().mb_type.is_direct() {
115 ctx += 1;
116 }
117 if !sstate.get_top_mb().mb_type.is_direct() {
118 ctx += 1;
119 }
120 if !cabac.decode_bit(27 + ctx) {
121 MBType::Direct
122 } else if !cabac.decode_bit(30) {
123 if !cabac.decode_bit(32) {
124 MBType::B16x16(BMode::L0)
125 } else {
126 MBType::B16x16(BMode::L1)
127 }
128 } else {
129 let idx = cabac.decode_bits(31, 32, 4);
130 match idx {
131 0x0 => MBType::B16x16(BMode::Bi),
132 0x1 => MBType::B16x8(BMode::L0, BMode::L0),
133 0x2 => MBType::B8x16(BMode::L0, BMode::L0),
134 0x3 => MBType::B16x8(BMode::L1, BMode::L1),
135 0x4 => MBType::B8x16(BMode::L1, BMode::L1),
136 0x5 => MBType::B16x8(BMode::L0, BMode::L1),
137 0x6 => MBType::B8x16(BMode::L0, BMode::L1),
138 0x7 => MBType::B16x8(BMode::L1, BMode::L0),
139 0xE => MBType::B8x16(BMode::L1, BMode::L0),
140 0xF => MBType::B8x8,
141 0xD => decode_i_type_inter(cabac, 32),
142 _ => {
143 let idx = (idx - 8) * 2 + (cabac.decode_bit(32) as u8);
144 match idx {
145 0 => MBType::B16x8(BMode::L0, BMode::Bi),
146 1 => MBType::B8x16(BMode::L0, BMode::Bi),
147 2 => MBType::B16x8(BMode::L1, BMode::Bi),
148 3 => MBType::B8x16(BMode::L1, BMode::Bi),
149 4 => MBType::B16x8(BMode::Bi, BMode::L0),
150 5 => MBType::B8x16(BMode::Bi, BMode::L0),
151 6 => MBType::B16x8(BMode::Bi, BMode::L1),
152 7 => MBType::B8x16(BMode::Bi, BMode::L1),
153 8 => MBType::B16x8(BMode::Bi, BMode::Bi),
154 _ => MBType::B8x16(BMode::Bi, BMode::Bi),
155 }
156 },
157 }
158 }
159 },
160 }
161}
162
163fn decode_sub_mb_type_cabac(cabac: &mut CABAC, slice_hdr: &SliceHeader) -> SubMBType {
164 match slice_hdr.slice_type {
165 SliceType::P | SliceType::SP => {
166 if cabac.decode_bit(21) {
167 SubMBType::P8x8
168 } else if !cabac.decode_bit(22) {
169 SubMBType::P8x4
170 } else if cabac.decode_bit(23) {
171 SubMBType::P4x8
172 } else {
173 SubMBType::P4x4
174 }
175 },
176 SliceType::B => {
177 if !cabac.decode_bit(36) {
178 SubMBType::Direct8x8
179 } else if !cabac.decode_bit(37) {
180 if !cabac.decode_bit(39) {
181 SubMBType::B8x8(BMode::L0)
182 } else {
183 SubMBType::B8x8(BMode::L1)
184 }
185 } else {
186 let idx = cabac.decode_bits(38, 39, 3);
187 match idx {
188 0 => SubMBType::B8x8(BMode::Bi),
189 1 => SubMBType::B8x4(BMode::L0),
190 2 => SubMBType::B4x8(BMode::L0),
191 3 => SubMBType::B8x4(BMode::L1),
192 6 => SubMBType::B4x4(BMode::L1),
193 7 => SubMBType::B4x4(BMode::Bi),
194 _ => {
195 let idx = (idx - 4) * 2 + (cabac.decode_bit(39) as u8);
196 match idx {
197 0 => SubMBType::B4x8(BMode::L1),
198 1 => SubMBType::B8x4(BMode::Bi),
199 2 => SubMBType::B4x8(BMode::Bi),
200 _ => SubMBType::B4x4(BMode::L0),
201 }
202 },
203 }
204 }
205 },
206 _ => unreachable!(),
207 }
208}
209
210fn decode_ref_idx(cabac: &mut CABAC, num_refs: usize, ctx: usize) -> PicRef {
211 if num_refs == 1 {
212 return ZERO_REF;
213 }
214 if !cabac.decode_bit(54 + ctx) {
215 ZERO_REF
216 } else if !cabac.decode_bit(54 + 4) {
217 PicRef::new(1)
218 } else {
219 let mut idx = 2;
220 while cabac.decode_bit(54 + 5) && idx < 32 {
221 idx += 1;
222 }
223 if idx < num_refs {
224 PicRef::new(idx as u8)
225 } else {
226 INVALID_REF
227 }
228 }
229}
230
231fn decode_mv_component(cabac: &mut CABAC, base: usize, ctx: usize) -> i16 {
232 if !cabac.decode_bit(base + ctx) {
233 0
234 } else {
235 let mut val = 1;
236 while val < 9 && cabac.decode_bit(base + (2 + val).min(6)) {
237 val += 1;
238 }
239 if val >= 9 {
240 let mut pfx = 3;
241 while pfx < 16 && cabac.decode_bypass() {
242 val += 1 << pfx;
243 pfx += 1;
244 }
245 val += cabac.decode_bypass_bits(pfx) as usize;
246 }
247 if val == 0 || !cabac.decode_bypass() {
248 val as i16
249 } else {
250 -(val as i16)
251 }
252 }
253}
254
255fn decode_mv(cabac: &mut CABAC, ctx0: usize, ctx1: usize) -> MV {
256 let x = decode_mv_component(cabac, 40, ctx0);
257 let y = decode_mv_component(cabac, 47, ctx1);
258 MV{ x, y }
259}
260
261#[allow(clippy::cognitive_complexity)]
262pub fn decode_mb_pred_cabac(cabac: &mut CABAC, slice_hdr: &SliceHeader, mb_type: MBType, sstate: &mut SliceState, mb_info: &mut CurrentMBInfo) {
263 mb_info.mb_type = mb_type;
264 let num_l0 = slice_hdr.num_ref_idx_l0_active;
265 let num_l1 = slice_hdr.num_ref_idx_l1_active;
266 sstate.reset_mb_mv();
267 match mb_type {
268 MBType::Intra4x4 => {
269 for &(x, y) in I4X4_SCAN.iter() {
270 let x = x as usize;
271 let y = y as usize;
272 let top_pred = sstate.get_top_blk4(x + y * 4).ipred;
273 let left_pred = sstate.get_left_blk4(x + y * 4).ipred;
274
275 let top_idx = top_pred.into_pred_idx();
276 let left_idx = left_pred.into_pred_idx();
277 let pred_mode = top_idx.min(left_idx);
278 let mut pred_mode = if pred_mode != -1 { pred_mode as u8 } else { 2 };
279
280 if !cabac.decode_bit(68) {
281 let m0 = cabac.decode_bit(69) as u8;
282 let m1 = cabac.decode_bit(69) as u8;
283 let m2 = cabac.decode_bit(69) as u8;
284 let new_mode = (m2 << 2) | (m1 << 1) | m0;
285 pred_mode = if new_mode >= pred_mode {
286 new_mode + 1
287 } else { new_mode };
288 }
289 mb_info.ipred[x + y * 4] = pred_mode.into();
290 sstate.get_cur_blk4(x + y * 4).ipred = (pred_mode as u8).into();
291 }
292 let mut ctx = 0;
293 if sstate.get_left_mb().cmode != 0 {
294 ctx += 1;
295 }
296 if sstate.get_top_mb().cmode != 0 {
297 ctx += 1;
298 }
299 mb_info.chroma_ipred = if !cabac.decode_bit(64 + ctx) {
300 0
301 } else if !cabac.decode_bit(67) {
302 1
303 } else if !cabac.decode_bit(67) {
304 2
305 } else {
306 3
307 };
308 },
309 MBType::Intra8x8 => {
310 for part in 0..4 {
311 let blk4 = (part & 1) * 2 + (part & 2) * 4;
312 let top_pred = sstate.get_top_blk4(blk4).ipred;
313 let left_pred = sstate.get_left_blk4(blk4).ipred;
314
315 let top_idx = top_pred.into_pred_idx();
316 let left_idx = left_pred.into_pred_idx();
317 let pred_mode = top_idx.min(left_idx);
318 let mut pred_mode = if pred_mode != -1 { pred_mode as u8 } else { 2 };
319 if !cabac.decode_bit(68) {
320 let m0 = cabac.decode_bit(69) as u8;
321 let m1 = cabac.decode_bit(69) as u8;
322 let m2 = cabac.decode_bit(69) as u8;
323 let new_mode = (m2 << 2) | (m1 << 1) | m0;
324 pred_mode = if new_mode >= pred_mode {
325 new_mode + 1
326 } else { new_mode };
327 }
328 mb_info.ipred[blk4] = pred_mode.into();
329 mb_info.ipred[blk4 + 1] = pred_mode.into();
330 mb_info.ipred[blk4 + 4] = pred_mode.into();
331 mb_info.ipred[blk4 + 5] = pred_mode.into();
332 sstate.get_cur_blk4(blk4).ipred = (pred_mode as u8).into();
333 sstate.get_cur_blk4(blk4 + 1).ipred = (pred_mode as u8).into();
334 sstate.get_cur_blk4(blk4 + 4).ipred = (pred_mode as u8).into();
335 sstate.get_cur_blk4(blk4 + 5).ipred = (pred_mode as u8).into();
336 }
337 let mut ctx = 0;
338 if sstate.get_left_mb().cmode != 0 {
339 ctx += 1;
340 }
341 if sstate.get_top_mb().cmode != 0 {
342 ctx += 1;
343 }
344 mb_info.chroma_ipred = if !cabac.decode_bit(64 + ctx) {
345 0
346 } else if !cabac.decode_bit(67) {
347 1
348 } else if !cabac.decode_bit(67) {
349 2
350 } else {
351 3
352 };
353 },
354 MBType::Intra16x16(_ipred, _, _) => {
355 let mut ctx = 0;
356 if sstate.get_left_mb().cmode != 0 {
357 ctx += 1;
358 }
359 if sstate.get_top_mb().cmode != 0 {
360 ctx += 1;
361 }
362 mb_info.chroma_ipred = if !cabac.decode_bit(64 + ctx) {
363 0
364 } else if !cabac.decode_bit(67) {
365 1
366 } else if !cabac.decode_bit(67) {
367 2
368 } else {
369 3
370 };
371 },
372 MBType::P16x16 | MBType::P16x8 | MBType::P8x16 => {
373 let num_subparts = mb_type.num_parts();
374 let (pw, ph) = mb_type.size();
375 let mut xoff = 0;
376 let mut yoff = 0;
377 for i in 0..num_subparts {
378 let ctx = sstate.get_mv_ref_ctx(xoff, yoff, 0);
379 let ref_idx = decode_ref_idx(cabac, num_l0, ctx);
380 mb_info.ref_l0[i] = ref_idx;
381 sstate.fill_ref(xoff, yoff, pw, ph, 0, ref_idx);
382 xoff += pw;
383 if xoff == 16 {
384 xoff = 0;
385 yoff += ph;
386 }
387 }
388 let mut xoff = 0;
389 let mut yoff = 0;
390 for i in 0..num_subparts {
391 let (ctx0, ctx1) = sstate.get_mv_ctx(xoff, yoff, 0);
392 let mv = decode_mv(cabac, ctx0, ctx1);
393 mb_info.mv_l0[i] = mv;
394 sstate.fill_mvd(xoff, yoff, pw, ph, 0, mv);
395 xoff += pw;
396 if xoff == 16 {
397 xoff = 0;
398 yoff += ph;
399 }
400 }
401 },
402 MBType::B16x16(mode) => {
403 if mode != BMode::L1 {
404 let ctx = sstate.get_mv_ref_ctx(0, 0, 0);
405 let ref_idx = decode_ref_idx(cabac, num_l0, ctx);
406 mb_info.ref_l0[0] = ref_idx;
407 sstate.fill_ref(0, 0, 16, 16, 0, ref_idx);
408 }
409 if mode != BMode::L0 {
410 let ctx = sstate.get_mv_ref_ctx(0, 0, 1);
411 let ref_idx = decode_ref_idx(cabac, num_l1, ctx);
412 mb_info.ref_l1[0] = ref_idx;
413 sstate.fill_ref(0, 0, 16, 16, 1, ref_idx);
414 }
415 if mode != BMode::L1 {
416 let (ctx0, ctx1) = sstate.get_mv_ctx(0, 0, 0);
417 let mv = decode_mv(cabac, ctx0, ctx1);
418 mb_info.mv_l0[0] = mv;
419 sstate.fill_mvd(0, 0, 16, 16, 0, mv);
420 }
421 if mode != BMode::L0 {
422 let (ctx0, ctx1) = sstate.get_mv_ctx(0, 0, 1);
423 let mv = decode_mv(cabac, ctx0, ctx1);
424 mb_info.mv_l1[0] = mv;
425 sstate.fill_mvd(0, 0, 16, 16, 1, mv);
426 }
427 },
428 MBType::B16x8(mode0, mode1) | MBType::B8x16(mode0, mode1) => {
429 let (pw, ph) = mb_info.mb_type.size();
430 let (px, py) = (pw & 8, ph & 8);
431 if mode0 != BMode::L1 {
432 let ctx = sstate.get_mv_ref_ctx(0, 0, 0);
433 let ref_idx = decode_ref_idx(cabac, num_l0, ctx);
434 mb_info.ref_l0[0] = ref_idx;
435 sstate.fill_ref(0, 0, pw, ph, 0, ref_idx);
436 }
437 if mode1 != BMode::L1 {
438 let ctx = sstate.get_mv_ref_ctx(pw & 8, ph & 8, 0);
439 let ref_idx = decode_ref_idx(cabac, num_l0, ctx);
440 mb_info.ref_l0[1] = ref_idx;
441 sstate.fill_ref(px, py, pw, ph, 0, ref_idx);
442 }
443 if mode0 != BMode::L0 {
444 let ctx = sstate.get_mv_ref_ctx(0, 0, 1);
445 let ref_idx = decode_ref_idx(cabac, num_l1, ctx);
446 mb_info.ref_l1[0] = ref_idx;
447 sstate.fill_ref(0, 0, pw, ph, 1, ref_idx);
448 }
449 if mode1 != BMode::L0 {
450 let ctx = sstate.get_mv_ref_ctx(pw & 8, ph & 8, 1);
451 let ref_idx = decode_ref_idx(cabac, num_l1, ctx);
452 mb_info.ref_l1[1] = ref_idx;
453 sstate.fill_ref(px, py, pw, ph, 1, ref_idx);
454 }
455 if mode0 != BMode::L1 {
456 let (ctx0, ctx1) = sstate.get_mv_ctx(0, 0, 0);
457 let mv = decode_mv(cabac, ctx0, ctx1);
458 mb_info.mv_l0[0] = mv;
459 sstate.fill_mvd(0, 0, pw, ph, 0, mv);
460 }
461 if mode1 != BMode::L1 {
462 let (ctx0, ctx1) = sstate.get_mv_ctx(pw & 8, ph & 8, 0);
463 let mv = decode_mv(cabac, ctx0, ctx1);
464 mb_info.mv_l0[1] = mv;
465 sstate.fill_mvd(px, py, pw, ph, 0, mv);
466 }
467 if mode0 != BMode::L0 {
468 let (ctx0, ctx1) = sstate.get_mv_ctx(0, 0, 1);
469 let mv = decode_mv(cabac, ctx0, ctx1);
470 mb_info.mv_l1[0] = mv;
471 sstate.fill_mvd(0, 0, pw, ph, 1, mv);
472 }
473 if mode1 != BMode::L0 {
474 let (ctx0, ctx1) = sstate.get_mv_ctx(pw & 8, ph & 8, 1);
475 let mv = decode_mv(cabac, ctx0, ctx1);
476 mb_info.mv_l1[1] = mv;
477 sstate.fill_mvd(px, py, pw, ph, 1, mv);
478 }
479 },
480 MBType::P8x8 | MBType::B8x8 => {
481 for sub_type in mb_info.sub_mb_type.iter_mut() {
482 *sub_type = decode_sub_mb_type_cabac(cabac, slice_hdr);
483 }
484 let num_l = [num_l0, num_l1];
485 let dst_ref = [&mut mb_info.ref_l0, &mut mb_info.ref_l1];
486 for ref_l in 0..2 {
487 for spart in 0..4 {
488 let stype = mb_info.sub_mb_type[spart];
489 if stype != SubMBType::Direct8x8 && ((ref_l == 0 && !stype.is_l1()) || (ref_l == 1 && !stype.is_l0())) {
490 let ctx = sstate.get_mv_ref_ctx((spart & 1) * 8, (spart & 2) * 4, ref_l);
491 let ref_idx = decode_ref_idx(cabac, num_l[ref_l], ctx);
492 dst_ref[ref_l][spart] = ref_idx;
493 sstate.get_cur_blk8(spart).ref_idx[ref_l] = ref_idx;
494 }
495 }
496 }
497 let dst_mv = [&mut mb_info.mv_l0, &mut mb_info.mv_l1];
498 for ref_l in 0..2 {
499 for spart in 0..4 {
500 let stype = mb_info.sub_mb_type[spart];
501 if stype == SubMBType::Direct8x8 || (ref_l == 0 && stype.is_l1()) || (ref_l == 1 && stype.is_l0()) {
502 continue;
503 }
504 let (pw, ph) = stype.size();
505 let mut xoff = (spart & 1) * 8;
506 let mut yoff = (spart & 2) * 4;
507 let num_sub = stype.num_parts();
508 let orig_x = xoff;
509 for i in 0..num_sub {
510 let (ctx0, ctx1) = sstate.get_mv_ctx(xoff, yoff, ref_l);
511 let mv = decode_mv(cabac, ctx0, ctx1);
512 dst_mv[ref_l][spart * 4 + i] = mv;
513 sstate.fill_mvd(xoff, yoff, pw, ph, ref_l, mv);
514 xoff += pw;
515 if xoff == orig_x + 8 {
516 xoff -= 8;
517 yoff += ph;
518 }
519 }
520 }
521 }
522 },
523 _ => {},
524 };
525}
526
527pub fn decode_cbp_cabac(cabac: &mut CABAC, sstate: &SliceState) -> (u8, u8) {
528 let mbt_a = sstate.get_left_mb().mb_type;
529 let mbt_b = sstate.get_top_mb().mb_type;
530 let left = if mbt_a == CompactMBType::None || mbt_a == CompactMBType::PCM {
531 0x3F
532 } else if !mbt_a.is_skip() {
533 sstate.get_left_mb().cbp
534 } else {
535 0
536 };
537 let top = if mbt_b == CompactMBType::None || mbt_b == CompactMBType::PCM {
538 0x3F
539 } else if !mbt_b.is_skip() {
540 sstate.get_top_mb().cbp
541 } else {
542 0
543 };
544
545 let cbp_ctx = if (left & 2) != 0 { 0 } else { 1 } + if (top & 4) != 0 { 0 } else { 2 };
546 let mut cbpy = cabac.decode_bit(73 + cbp_ctx) as u8;
547 let cbp_ctx = if cbpy != 0 { 0 } else { 1 } + if (top & 8) != 0 { 0 } else { 2 };
548 cbpy |= (cabac.decode_bit(73 + cbp_ctx) as u8) << 1;
549 let cbp_ctx = if (left & 8) != 0 { 0 } else { 1 } + if (cbpy & 1) != 0 { 0 } else { 2 };
550 cbpy |= (cabac.decode_bit(73 + cbp_ctx) as u8) << 2;
551 let cbp_ctx = if (cbpy & 4) != 0 { 0 } else { 1 } + if (cbpy & 2) != 0 { 0 } else { 2 };
552 cbpy |= (cabac.decode_bit(73 + cbp_ctx) as u8) << 3;
553
554 let left = if mbt_a == CompactMBType::PCM {
555 0x2F
556 } else if mbt_a == CompactMBType::None || !mbt_a.is_skip() {
557 sstate.get_left_mb().cbp
558 } else {
559 0
560 };
561 let top = if mbt_b == CompactMBType::PCM {
562 0x2F
563 } else if mbt_b == CompactMBType::None || !mbt_b.is_skip() {
564 sstate.get_top_mb().cbp
565 } else {
566 0
567 };
568 let cleft = left >> 4;
569 let ctop = top >> 4;
570 let cbp_ctx0 = if cleft != 0 { 1 } else { 0 } + if ctop != 0 { 2 } else { 0 };
571 let cbp_ctx1 = if cleft == 2 { 1 } else { 0 } + if ctop == 2 { 2 } else { 0 };
572 let cbpc = if !cabac.decode_bit(77 + cbp_ctx0) {
573 0
574 } else {
575 cabac.decode_bit(81 + cbp_ctx1) as u8 + 1
576 };
577
578 (cbpy, cbpc)
579}
580
581pub fn decode_mb_qp_delta_cabac(cabac: &mut CABAC, ctx: usize) -> i32 {
582 if !cabac.decode_bit(60 + ctx) {
583 0
584 } else if !cabac.decode_bit(62) {
585 1
586 } else {
587 let mut val = 0;
588 while val < 128 && cabac.decode_bit(63) {
589 val += 1;
590 }
591 if (val & 1) != 0 {
592 ((val >> 1) as i32) + 2
593 } else {
594 -((val >> 1) as i32) - 1
595 }
596 }
597}
598
599fn decode_block(cabac: &mut CABAC, coeffs: &mut [i16], cat: usize, ctx_off: usize) -> bool {
600 const CTX_BASE: [(usize, usize); 5] = [
601 (0, 0), (15, 10), (29, 20), (44, 30), (47, 39)
602 ];
603 let (flag_off, coef_off) = CTX_BASE[cat];
604 let scan: &[usize] = match coeffs.len() {
605 4 => &CHROMA_DC_SCAN,
606 15 => &ZIGZAG1,
607 16 => &ZIGZAG,
608 _ => unreachable!(),
609 };
610
611 let coded_block_flag = cabac.decode_bit(85 + ctx_off);
612 let mut coded = [false; 16];
613 if coded_block_flag {
614 let mut last_idx = coeffs.len() - 1;
615 for i in 0..coeffs.len() - 1 {
616 coded[i] = cabac.decode_bit(105 + flag_off + i); // or 277 for interlaced
617 if coded[i] {
618 let last = cabac.decode_bit(166 + flag_off + i); // or 338 for interlaced
619 if last {
620 last_idx = i;
621 break;
622 }
623 }
624 }
625 coded[last_idx] = true;
626 let mut coef_ctx = 0;
627 for i in (0..=last_idx).rev() {
628 if coded[i] {
629 let zero_ctx = if coef_ctx < 4 { coef_ctx + 1 } else { 0 };
630 coeffs[scan[i]] = if !cabac.decode_bit(227 + coef_off + zero_ctx) {
631 if coef_ctx < 3 {
632 coef_ctx += 1;
633 }
634 1
635 } else {
636 let cur_ctx = 227 + coef_off + (coef_ctx + 2).max(5);
637 coef_ctx = (coef_ctx + 1).max(4).min(7);
638
639 let mut coef = 2;
640 while coef < 15 && cabac.decode_bit(cur_ctx) {
641 coef += 1;
642 }
643 if coef == 15 {
644 let mut pfx = 0;
645 while pfx < 15 && cabac.decode_bypass() {
646 pfx += 1;
647 }
648 let mut tail = 1;
649 for _ in 0..pfx {
650 tail = (tail << 1) + (cabac.decode_bypass() as i16);
651 }
652 coef + tail - 1
653 } else {
654 coef
655 }
656 };
657 if cabac.decode_bypass() {
658 coeffs[scan[i]] = -coeffs[scan[i]];
659 }
660 }
661 }
662 }
663 coded_block_flag
664}
665
666fn decode_block8x8(cabac: &mut CABAC, coeffs: &mut [i16; 64], _cat: usize) {
667 const SIG_FLAG_MAP: [usize; 63] = [
668 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
669 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
670 7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11,
671 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11, 14, 10, 12
672 ];
673 const LAST_SIG_FLAG_MAP: [usize; 63] = [
674 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
675 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
676 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
677 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
678 ];
679 let (flag_off, coef_off) = (0, 0);
680 let scan = &ZIGZAG8X8;
681
682 let mut coded = [false; 64];
683 let mut last_idx = coeffs.len() - 1;
684 for i in 0..coeffs.len() - 1 {
685 coded[i] = cabac.decode_bit(402 + flag_off + SIG_FLAG_MAP[i]);
686 if coded[i] {
687 let last = cabac.decode_bit(417 + flag_off + LAST_SIG_FLAG_MAP[i]);
688 if last {
689 last_idx = i;
690 break;
691 }
692 }
693 }
694 coded[last_idx] = true;
695 let mut coef_ctx = 0;
696 for i in (0..=last_idx).rev() {
697 if coded[i] {
698 let zero_ctx = if coef_ctx < 4 { coef_ctx + 1 } else { 0 };
699 coeffs[scan[i]] = if !cabac.decode_bit(426 + coef_off + zero_ctx) {
700 if coef_ctx < 3 {
701 coef_ctx += 1;
702 }
703 1
704 } else {
705 let cur_ctx = 426 + coef_off + (coef_ctx + 2).max(5);
706 coef_ctx = (coef_ctx + 1).max(4).min(7);
707
708 let mut coef = 2;
709 while coef < 15 && cabac.decode_bit(cur_ctx) {
710 coef += 1;
711 }
712 if coef == 15 {
713 let mut pfx = 0;
714 while pfx < 15 && cabac.decode_bypass() {
715 pfx += 1;
716 }
717 let mut tail = 1;
718 for _ in 0..pfx {
719 tail = (tail << 1) + (cabac.decode_bypass() as i16);
720 }
721 coef + tail - 1
722 } else {
723 coef
724 }
725 };
726 if cabac.decode_bypass() {
727 coeffs[scan[i]] = -coeffs[scan[i]];
728 }
729 }
730 }
731}
732
733fn derive_ctx_off(sstate: &mut SliceState, cat: usize, blk_no: usize) -> usize {
734 let mbt = sstate.get_cur_mb().mb_type;
735 let mut mbt_a = sstate.get_left_mb().mb_type;
736 let mut mbt_b = sstate.get_top_mb().mb_type;
737 let (trans_a, trans_b, mut cond_term_a, mut cond_term_b) = match cat {
738 0 => {
739 (mbt_a == CompactMBType::Intra16x16,
740 mbt_b == CompactMBType::Intra16x16,
741 (sstate.get_left_mb().coded_flags & 1) as usize,
742 (sstate.get_top_mb().coded_flags & 1) as usize)
743 },
744 1 | 2 => {
745 if (blk_no & 3) != 0 {
746 mbt_a = mbt;
747 }
748 if blk_no >= 4 {
749 mbt_b = mbt;
750 }
751 let nc_left = sstate.get_left_blk4(blk_no).ncoded;
752 let nc_top = sstate.get_top_blk4(blk_no).ncoded;
753 (nc_left != 0,
754 nc_top != 0,
755 (nc_left != 0) as usize,
756 (nc_top != 0) as usize)
757 },
758 3 => {
759 ((sstate.get_left_mb().cbp & 0x30) != 0,
760 (sstate.get_top_mb().cbp & 0x30) != 0,
761 ((sstate.get_left_mb().coded_flags & (1 << (blk_no + 1 + 16))) != 0) as usize,
762 ((sstate.get_top_mb().coded_flags & (1 << (blk_no + 1 + 16))) != 0) as usize)
763 },
764 4 => {
765 let chroma = blk_no >> 2;
766 if (blk_no & 1) != 0 {
767 mbt_a = mbt;
768 }
769 if (blk_no & 2) != 0 {
770 mbt_b = mbt;
771 }
772 ((blk_no & 1) != 0 || (sstate.get_left_mb().cbp & 0x20) != 0,
773 (blk_no & 2) != 0 || (sstate.get_top_mb().cbp & 0x20) != 0,
774 (sstate.get_left_blk8(blk_no & 3).ncoded_c[chroma] != 0) as usize,
775 (sstate.get_top_blk8(blk_no & 3).ncoded_c[chroma] != 0) as usize)
776 },
777 _ => unreachable!(),
778 };
779 /*let coded_no = match cat {
780 0 => 0,
781 1 | 2 => blk_no + 1,
782 3 => 1 + 16 + blk_no,
783 4 => 1 + 16 + 2 + blk_no,
784 _ => unreachable!(),
785 };*/
786
787 if mbt_a == CompactMBType::None && mbt.is_inter() {
788 cond_term_a = 0;
789 }
790 if !trans_a && mbt_a != CompactMBType::PCM {
791 cond_term_a = 0;
792 }
793 /*if mbt.is_intra() && pps.constrained_intra_pred && mbt_a.is_inter() && slice_partitioning {
794 cond_term_a = 0;
795 }*/
796 if (mbt_a == CompactMBType::PCM) || (mbt_a == CompactMBType::None && mbt.is_intra()) {
797 cond_term_a = 1;
798 }
799
800 if mbt_b == CompactMBType::None && mbt.is_inter() {
801 cond_term_b = 0;
802 }
803 if !trans_b && mbt_b != CompactMBType::PCM {
804 cond_term_b = 0;
805 }
806 /*if mbt.is_intra() && pps.constrained_intra_pred && mbt_b.is_inter() && slice_partitioning {
807 cond_term_b = 0;
808 }*/
809 if (mbt_b == CompactMBType::PCM) || (mbt_b == CompactMBType::None && mbt.is_intra()) {
810 cond_term_b = 1;
811 }
812
813 cat * 4 + cond_term_b * 2 + cond_term_a
814}
815
816pub fn decode_residual_cabac(cabac: &mut CABAC, sstate: &mut SliceState, mb_info: &mut CurrentMBInfo) {
817 sstate.get_cur_mb().mb_type = mb_info.mb_type.into();
818 let mut coded_flags = 0;
819 if mb_info.mb_type.is_intra16x16() {
820 let off = derive_ctx_off(sstate, 0, 0);
821 let coded = decode_block(cabac, &mut mb_info.coeffs[24], 0, off);
822 mb_info.coded[24] = coded;
823 if coded {
824 coded_flags |= 1;
825 }
826 }
827 if !mb_info.transform_size_8x8 {
828 for blk8 in 0..4 {
829 if (mb_info.cbpy & (1 << blk8)) != 0 {
830 for blk4 in 0..4 {
831 let blk_no = (blk8 & 1) * 2 + (blk8 & 2) * 4 + (blk4 & 1) + (blk4 & 2) * 2;
832 let coded = if mb_info.mb_type.is_intra16x16() {
833 let off = derive_ctx_off(sstate, 1, blk_no);
834 decode_block(cabac, &mut mb_info.coeffs[blk_no][1..], 1, off)
835 } else {
836 let off = derive_ctx_off(sstate, 2, blk_no);
837 decode_block(cabac, &mut mb_info.coeffs[blk_no], 2, off)
838 };
839 sstate.get_cur_blk4(blk_no).ncoded = coded as u8;
840 mb_info.coded[blk_no] = coded;
841 if coded {
842 coded_flags |= 1 << (1 + blk_no);
843 }
844 }
845 }
846 }
847 } else {
848 for blk8 in 0..4 {
849 if (mb_info.cbpy & (1 << blk8)) != 0 {
850 let blk4 = (blk8 & 1) * 2 + (blk8 & 2) * 4;
851 decode_block8x8(cabac, &mut mb_info.coeffs8x8[blk8].coeffs, 5);
852 coded_flags |= 0x33 << blk4;
853 mb_info.coded[blk4] = true;
854 mb_info.coded[blk4 + 1] = true;
855 mb_info.coded[blk4 + 4] = true;
856 mb_info.coded[blk4 + 5] = true;
857 sstate.get_cur_blk4(blk4).ncoded = 1;
858 sstate.get_cur_blk4(blk4 + 1).ncoded = 1;
859 sstate.get_cur_blk4(blk4 + 4).ncoded = 1;
860 sstate.get_cur_blk4(blk4 + 5).ncoded = 1;
861 }
862 }
863 }
864 for chroma in 0..2 {
865 if (mb_info.cbpc & 3) != 0 {
866 let off = derive_ctx_off(sstate, 3, chroma);
867 let coded = decode_block(cabac, &mut mb_info.chroma_dc[chroma], 3, off);
868 if coded {
869 coded_flags |= 1 << (16 + 1 + chroma);
870 }
871 }
872 }
873 for chroma in 0..2 {
874 if (mb_info.cbpc & 2) != 0 {
875 for blk4 in 0..4 {
876 let blk_no = 16 + chroma * 4 + blk4;
877 let off = derive_ctx_off(sstate, 4, blk_no - 16);
878 let coded = decode_block(cabac, &mut mb_info.coeffs[blk_no][1..], 4, off);
879 sstate.get_cur_blk8(blk4).ncoded_c[chroma] = coded as u8;
880 mb_info.coded[blk_no] = coded;
881 if coded {
882 coded_flags |= 1 << (1 + 2 + blk_no);
883 }
884 }
885 }
886 }
887 sstate.get_cur_mb().coded_flags = coded_flags;
888}