1use super::levels::*;
5use super::obu::{
6 get_poc_diff, FrameHeader, WarpedMotionParams, WM_AFFINE, WM_IDENTITY, WM_ROT_ZOOM,
7 WM_TRANSLATION,
8};
9use super::refmvs::RefmvsCandidate;
10use super::tables::TxfmInfo;
11
12#[derive(Clone, Default)]
13pub struct BlockContext {
14 pub mode: [u8; 32],
15 pub lcoef: [u8; 32],
16 pub ccoef: [[u8; 32]; 2],
17 pub seg_pred: [u8; 32],
18 pub skip: [u8; 32],
19 pub skip_mode: [u8; 32],
20 pub intra: [u8; 32],
21 pub comp_type: [u8; 32],
22 pub r#ref: [[i8; 32]; 2],
24 pub filter: [[u8; 32]; 2],
26 pub tx_intra: [i8; 32],
27 pub tx: [i8; 32],
28 pub tx_lpf_y: [u8; 32],
29 pub tx_lpf_uv: [u8; 32],
30 pub partition: [u8; 16],
31 pub uvmode: [u8; 32],
32 pub pal_sz: [u8; 32],
33}
34
35type Bc = BlockContext;
36
37pub fn get_intra_ctx(
38 a: &Bc,
39 l: &Bc,
40 yb4: usize,
41 xb4: usize,
42 have_top: bool,
43 have_left: bool,
44) -> usize {
45 if have_left {
46 if have_top {
47 let ctx = (l.intra[yb4] + a.intra[xb4]) as usize;
48 ctx + (ctx == 2) as usize
49 } else {
50 l.intra[yb4] as usize * 2
51 }
52 } else if have_top {
53 a.intra[xb4] as usize * 2
54 } else {
55 0
56 }
57}
58
59pub fn get_tx_ctx(a: &Bc, l: &Bc, max_tx: &TxfmInfo, yb4: usize, xb4: usize) -> usize {
60 (l.tx_intra[yb4] as i32 >= max_tx.lh as i32) as usize
61 + (a.tx_intra[xb4] as i32 >= max_tx.lw as i32) as usize
62}
63
64pub fn get_partition_ctx(a: &Bc, l: &Bc, bl: usize, yb8: usize, xb8: usize) -> usize {
65 ((a.partition[xb8] >> (4 - bl)) & 1) as usize
66 + ((((l.partition[yb8] >> (4 - bl)) & 1) as usize) << 1)
67}
68
69pub fn gather_left_partition_prob(i: &[u16], bl: usize) -> u32 {
70 let i = |k: usize| i[k] as u32;
71 let mut out = i(PARTITION_H - 1).wrapping_sub(i(PARTITION_H));
72 out = out.wrapping_add(i(PARTITION_SPLIT - 1).wrapping_sub(i(PARTITION_T_LEFT_SPLIT)));
73 if bl != BL_128X128 {
74 out = out.wrapping_add(i(PARTITION_H4 - 1).wrapping_sub(i(PARTITION_H4)));
75 }
76 out
77}
78
79pub fn gather_top_partition_prob(i: &[u16], bl: usize) -> u32 {
80 let i = |k: usize| i[k] as u32;
81 let mut out = i(PARTITION_V - 1).wrapping_sub(i(PARTITION_T_TOP_SPLIT));
82 out = out.wrapping_add(i(PARTITION_T_LEFT_SPLIT - 1));
83 if bl != BL_128X128 {
84 out = out.wrapping_add(i(PARTITION_V4 - 1).wrapping_sub(i(PARTITION_T_RIGHT_SPLIT)));
85 }
86 out
87}
88
89pub fn get_uv_inter_txtp(uvt_dim: &TxfmInfo, ytxtp: u8) -> u8 {
90 if uvt_dim.max as usize == TX_32X32 {
91 return if ytxtp == IDTX { IDTX } else { DCT_DCT };
92 }
93 if uvt_dim.min as usize == TX_16X16
94 && ((1u32 << ytxtp)
95 & ((1 << H_FLIPADST) | (1 << V_FLIPADST) | (1 << H_ADST) | (1 << V_ADST)))
96 != 0
97 {
98 return DCT_DCT;
99 }
100 ytxtp
101}
102
103pub fn get_filter_ctx(
104 a: &Bc,
105 l: &Bc,
106 comp: bool,
107 dir: usize,
108 r: i8,
109 yb4: usize,
110 xb4: usize,
111) -> usize {
112 const N: u8 = 3;
113 let a_f = if a.r#ref[0][xb4] == r || a.r#ref[1][xb4] == r {
114 a.filter[dir][xb4]
115 } else {
116 N
117 };
118 let l_f = if l.r#ref[0][yb4] == r || l.r#ref[1][yb4] == r {
119 l.filter[dir][yb4]
120 } else {
121 N
122 };
123 let c = comp as usize * 4;
124 if a_f == l_f {
125 c + a_f as usize
126 } else if a_f == N {
127 c + l_f as usize
128 } else if l_f == N {
129 c + a_f as usize
130 } else {
131 c + N as usize
132 }
133}
134
135pub fn get_comp_ctx(
136 a: &Bc,
137 l: &Bc,
138 yb4: usize,
139 xb4: usize,
140 have_top: bool,
141 have_left: bool,
142) -> usize {
143 if have_top {
144 if have_left {
145 if a.comp_type[xb4] != 0 {
146 if l.comp_type[yb4] != 0 {
147 4
148 } else {
149 2 + (l.r#ref[0][yb4] as u8 as u32 >= 4) as usize
150 }
151 } else if l.comp_type[yb4] != 0 {
152 2 + (a.r#ref[0][xb4] as u8 as u32 >= 4) as usize
153 } else {
154 ((l.r#ref[0][yb4] >= 4) ^ (a.r#ref[0][xb4] >= 4)) as usize
155 }
156 } else if a.comp_type[xb4] != 0 {
157 3
158 } else {
159 (a.r#ref[0][xb4] >= 4) as usize
160 }
161 } else if have_left {
162 if l.comp_type[yb4] != 0 {
163 3
164 } else {
165 (l.r#ref[0][yb4] >= 4) as usize
166 }
167 } else {
168 1
169 }
170}
171
172#[inline]
173fn has_uni_comp(e: &Bc, off: usize) -> bool {
174 (e.r#ref[0][off] < 4) == (e.r#ref[1][off] < 4)
175}
176
177pub fn get_comp_dir_ctx(
178 a: &Bc,
179 l: &Bc,
180 yb4: usize,
181 xb4: usize,
182 have_top: bool,
183 have_left: bool,
184) -> usize {
185 if have_top && have_left {
186 let (ai, li) = (a.intra[xb4] != 0, l.intra[yb4] != 0);
187 if ai && li {
188 return 2;
189 }
190 if ai || li {
191 let (e, off) = if ai { (l, yb4) } else { (a, xb4) };
192 if e.comp_type[off] == COMP_INTER_NONE {
193 return 2;
194 }
195 return 1 + 2 * has_uni_comp(e, off) as usize;
196 }
197 let a_comp = a.comp_type[xb4] != COMP_INTER_NONE;
198 let l_comp = l.comp_type[yb4] != COMP_INTER_NONE;
199 let (a0, l0) = (a.r#ref[0][xb4], l.r#ref[0][yb4]);
200 if !a_comp && !l_comp {
201 1 + 2 * ((a0 >= 4) == (l0 >= 4)) as usize
202 } else if !a_comp || !l_comp {
203 let (e, off) = if a_comp { (a, xb4) } else { (l, yb4) };
204 if !has_uni_comp(e, off) {
205 return 1;
206 }
207 3 + ((a0 >= 4) == (l0 >= 4)) as usize
208 } else {
209 let (au, lu) = (has_uni_comp(a, xb4), has_uni_comp(l, yb4));
210 if !au && !lu {
211 return 0;
212 }
213 if !au || !lu {
214 return 2;
215 }
216 3 + ((a0 == 4) == (l0 == 4)) as usize
217 }
218 } else if have_top || have_left {
219 let (e, off) = if have_left { (l, yb4) } else { (a, xb4) };
220 if e.intra[off] != 0 {
221 return 2;
222 }
223 if e.comp_type[off] == COMP_INTER_NONE {
224 return 2;
225 }
226 4 * has_uni_comp(e, off) as usize
227 } else {
228 2
229 }
230}
231
232#[allow(clippy::too_many_arguments)]
233pub fn get_jnt_comp_ctx(
234 nb: u8,
235 poc: u32,
236 ref0poc: u32,
237 ref1poc: u32,
238 a: &Bc,
239 l: &Bc,
240 yb4: usize,
241 xb4: usize,
242) -> usize {
243 let d0 = get_poc_diff(nb, ref0poc as i32, poc as i32).unsigned_abs();
244 let d1 = get_poc_diff(nb, poc as i32, ref1poc as i32).unsigned_abs();
245 let offset = (d0 == d1) as usize;
246 let a_ctx = (a.comp_type[xb4] >= COMP_INTER_AVG || a.r#ref[0][xb4] == 6) as usize;
247 let l_ctx = (l.comp_type[yb4] >= COMP_INTER_AVG || l.r#ref[0][yb4] == 6) as usize;
248 3 * offset + a_ctx + l_ctx
249}
250
251pub fn get_mask_comp_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize) -> usize {
252 let f = |e: &Bc, o: usize| {
253 if e.comp_type[o] >= COMP_INTER_SEG {
254 1
255 } else if e.r#ref[0][o] == 6 {
256 3
257 } else {
258 0
259 }
260 };
261 (f(a, xb4) + f(l, yb4)).min(5)
262}
263
264fn count_refs<const N: usize>(
267 a: &Bc,
268 l: &Bc,
269 yb4: usize,
270 xb4: usize,
271 have_top: bool,
272 have_left: bool,
273 pick: impl Fn(i8) -> Option<usize>,
274) -> [i32; N] {
275 let mut cnt = [0i32; N];
276 let mut add = |e: &Bc, o: usize| {
277 if let Some(k) = pick(e.r#ref[0][o]) {
278 cnt[k] += 1;
279 }
280 if e.comp_type[o] != 0
281 && let Some(k) = pick(e.r#ref[1][o])
282 {
283 cnt[k] += 1;
284 }
285 };
286 if have_top && a.intra[xb4] == 0 {
287 add(a, xb4);
288 }
289 if have_left && l.intra[yb4] == 0 {
290 add(l, yb4);
291 }
292 cnt
293}
294
295#[inline]
296fn cmp3(a: i32, b: i32) -> usize {
297 if a == b {
298 1
299 } else if a < b {
300 0
301 } else {
302 2
303 }
304}
305
306pub fn get_ref_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
307 let c = count_refs::<2>(a, l, yb4, xb4, ht, hl, |r| Some((r >= 4) as usize));
308 cmp3(c[0], c[1])
309}
310
311pub fn get_fwd_ref_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
312 let c = count_refs::<4>(a, l, yb4, xb4, ht, hl, |r| {
313 if r < 4 {
314 Some(r as usize)
315 } else {
316 None
317 }
318 });
319 cmp3(c[0] + c[1], c[2] + c[3])
320}
321
322pub fn get_fwd_ref_1_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
323 let c = count_refs::<2>(a, l, yb4, xb4, ht, hl, |r| {
324 if r < 2 {
325 Some(r as usize)
326 } else {
327 None
328 }
329 });
330 cmp3(c[0], c[1])
331}
332
333pub fn get_fwd_ref_2_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
334 let c = count_refs::<2>(a, l, yb4, xb4, ht, hl, |r| {
335 if (r as u32 ^ 2) < 2 {
336 Some(r as usize - 2)
337 } else {
338 None
339 }
340 });
341 cmp3(c[0], c[1])
342}
343
344pub fn get_bwd_ref_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
345 let c = count_refs::<3>(a, l, yb4, xb4, ht, hl, |r| {
346 if r >= 4 {
347 Some(r as usize - 4)
348 } else {
349 None
350 }
351 });
352 cmp3(c[1] + c[0], c[2])
353}
354
355pub fn get_bwd_ref_1_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
356 let c = count_refs::<3>(a, l, yb4, xb4, ht, hl, |r| {
357 if r >= 4 {
358 Some(r as usize - 4)
359 } else {
360 None
361 }
362 });
363 cmp3(c[0], c[1])
364}
365
366pub fn get_uni_p1_ctx(a: &Bc, l: &Bc, yb4: usize, xb4: usize, ht: bool, hl: bool) -> usize {
367 let c = count_refs::<3>(a, l, yb4, xb4, ht, hl, |r| {
368 if (r as i32 as u32).wrapping_sub(1) < 3 {
369 Some(r as usize - 1)
370 } else {
371 None
372 }
373 });
374 cmp3(c[0], c[1] + c[2])
375}
376
377pub fn get_drl_context(stack: &[RefmvsCandidate], idx: usize) -> usize {
378 if stack[idx].weight >= 640 {
379 (stack[idx + 1].weight < 640) as usize
380 } else if stack[idx + 1].weight < 640 {
381 2
382 } else {
383 0
384 }
385}
386
387pub fn get_cur_frame_segid(
389 by: usize,
390 bx: usize,
391 have_top: bool,
392 have_left: bool,
393 map: &[u8],
394 stride: usize,
395) -> (u8, usize) {
396 let o = bx + by * stride;
397 if have_left && have_top {
398 let l = map[o - 1];
399 let a = map[o - stride];
400 let al = map[o - stride - 1];
401 let ctx = if l == a && al == l {
402 2
403 } else if l == a || al == l || a == al {
404 1
405 } else {
406 0
407 };
408 (if a == al { a } else { l }, ctx)
409 } else {
410 (
411 if have_left {
412 map[o - 1]
413 } else if have_top {
414 map[o - stride]
415 } else {
416 0
417 },
418 0,
419 )
420 }
421}
422
423#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
424pub struct Mv {
425 pub y: i16,
426 pub x: i16,
427}
428
429impl Mv {
430 pub const ZERO: Mv = Mv { y: 0, x: 0 };
431 pub const INVALID: Mv = Mv {
432 y: i16::MIN,
433 x: i16::MIN,
434 };
435}
436
437#[inline]
438pub fn fix_int_mv_precision(mv: &mut Mv) {
439 let f = |v: i16| ((v as i32 - (v as i32 >> 15) + 3) as u32 & !7u32) as i16;
440 mv.x = f(mv.x);
441 mv.y = f(mv.y);
442}
443
444pub fn fix_mv_precision(hdr: &FrameHeader, mv: &mut Mv) {
445 if hdr.force_integer_mv {
446 fix_int_mv_precision(mv);
447 } else if !hdr.hp {
448 let f = |v: i16| ((v as i32 - (v as i32 >> 15)) as u32 & !1u32) as i16;
449 mv.x = f(mv.x);
450 mv.y = f(mv.y);
451 }
452}
453
454pub fn get_gmv_2d(
455 gmv: &WarpedMotionParams,
456 bx4: i32,
457 by4: i32,
458 bw4: i32,
459 bh4: i32,
460 hdr: &FrameHeader,
461) -> Mv {
462 match gmv.typ {
463 WM_IDENTITY => Mv::ZERO,
464 WM_TRANSLATION => {
465 let mut r = Mv {
466 y: (gmv.matrix[0] >> 13) as i16,
467 x: (gmv.matrix[1] >> 13) as i16,
468 };
469 if hdr.force_integer_mv {
470 fix_int_mv_precision(&mut r);
471 }
472 r
473 }
474 _ => {
475 debug_assert!(gmv.typ == WM_ROT_ZOOM || gmv.typ == WM_AFFINE);
476 let m = &gmv.matrix;
477 let x = bx4 * 4 + bw4 * 2 - 1;
478 let y = by4 * 4 + bh4 * 2 - 1;
479 let xc = (m[2] - (1 << 16))
480 .wrapping_mul(x)
481 .wrapping_add(m[3].wrapping_mul(y))
482 .wrapping_add(m[0]);
483 let yc = (m[5] - (1 << 16))
484 .wrapping_mul(y)
485 .wrapping_add(m[4].wrapping_mul(x))
486 .wrapping_add(m[1]);
487 let shift = 16 - (3 - !hdr.hp as i32);
488 let round = (1 << shift) >> 1;
489 let f = |c: i32| {
490 let v = ((c.unsigned_abs() as i32 + round) >> shift) << (!hdr.hp as i32);
491 (if c < 0 { -v } else { v }) as i16
492 };
493 let mut r = Mv { y: f(yc), x: f(xc) };
494 if hdr.force_integer_mv {
495 fix_int_mv_precision(&mut r);
496 }
497 r
498 }
499 }
500}