forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

intra.rs753 lines, 24.2 KiBHistoryRawDownload
1//! Intra sample prediction, all 35 modes (8.4.2 - 8.4.4.2).
2//!
3//! Reference samples arrive as `corner` (`p[-1][-1]`), `top` (`p[x][-1]`) and
4//! `left` (`p[-1][y]`), each up to `2 * nTbS` long, with a bitmask saying which
5//! of them are real. Substitution, smoothing and the mode itself are then
6//! exactly the decoder's, because the encoder reconstructs with the same code:
7//! a prediction that differs from the decoder's by one sample is a drift that
8//! the picture hash would catch but the eye would not.
9//!
10//! The predictors are generic over the sample type: the encoder runs them on
11//! 8-bit samples (`u8`, the default), the decoder on 16-bit storage for both
12//! 8-bit and 10-bit pictures. The bit depth is an argument wherever the spec
13//! uses `BitDepth`.
14
15/// A sample value: `u8` for 8-bit pictures, `u16` for 10-bit ones.
16pub trait Sample: Copy + Default + Into<i32> {
17 /// The value `v`, which the caller has already clipped to the sample range.
18 fn from_i32(v: i32) -> Self;
19}
20
21impl Sample for u8 {
22 fn from_i32(v: i32) -> u8 {
23 v as u8
24 }
25}
26
27impl Sample for u16 {
28 fn from_i32(v: i32) -> u16 {
29 v as u16
30 }
31}
32
33/// Reference samples for one transform block, before or after filtering.
34#[derive(Clone, Copy)]
35pub struct Refs<T: Sample = u8> {
36 /// `p[-1][-1]`.
37 pub corner: T,
38 /// `p[x][-1]`, `x = 0..2 * nTbS - 1`.
39 pub top: [T; 64],
40 /// `p[-1][y]`, `y = 0..2 * nTbS - 1`.
41 pub left: [T; 64],
42}
43
44impl<T: Sample> Default for Refs<T> {
45 fn default() -> Self {
46 Refs {
47 corner: T::from_i32(128),
48 top: [T::from_i32(128); 64],
49 left: [T::from_i32(128); 64],
50 }
51 }
52}
53
54/// Which reference samples are real, one bit per sample.
55#[derive(Clone, Copy, Default)]
56pub struct Availability {
57 /// Whether `p[-1][-1]` is available.
58 pub corner: bool,
59 /// Bit `x` set when `p[x][-1]` is available.
60 pub top: u64,
61 /// Bit `y` set when `p[-1][y]` is available.
62 pub left: u64,
63}
64
65impl Availability {
66 /// True when nothing at all is available and the whole block predicts flat.
67 pub fn is_empty(&self, n: usize) -> bool {
68 let mask = if 2 * n >= 64 {
69 u64::MAX
70 } else {
71 (1u64 << (2 * n)) - 1
72 };
73 !self.corner && self.top & mask == 0 && self.left & mask == 0
74 }
75}
76
77/// `intraPredAngle` per mode (Table 8-5), indexed by `predModeIntra`.
78#[rustfmt::skip]
79const ANGLE: [i32; 35] = [
80 0, 0, 32, 26, 21, 17, 13, 9, 5, 2, 0, -2, -5, -9, -13, -17, -21, -26,
81 -32, -26, -21, -17, -13, -9, -5, -2, 0, 2, 5, 9, 13, 17, 21, 26, 32,
82];
83
84/// `invAngle` per mode (Table 8-6); only modes 11..25 use it.
85#[rustfmt::skip]
86const INV_ANGLE: [i32; 35] = [
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 -4096, -1638, -910, -630, -482, -390, -315, -256, -315, -390, -482, -630, -910, -1638, -4096,
89 0, 0, 0, 0, 0, 0, 0, 0, 0,
90];
91
92/// Reference sample substitution (8.4.4.2.2): fill the holes from the nearest
93/// available sample, walking up the left edge and then along the top.
94pub fn substitute<T: Sample>(refs: &mut Refs<T>, avail: &Availability, n: usize, bit_depth: u32) {
95 let span = 2 * n;
96 if avail.is_empty(n) {
97 let mid = T::from_i32(1 << (bit_depth - 1));
98 refs.corner = mid;
99 refs.top[..span].fill(mid);
100 refs.left[..span].fill(mid);
101 return;
102 }
103 // Step 1: the bottom-left sample, from the first available going up the
104 // left edge, then across the top.
105 if avail.left & (1 << (span - 1)) == 0 {
106 let mut value = None;
107 for y in (0..span).rev() {
108 if avail.left & (1 << y) != 0 {
109 value = Some(refs.left[y]);
110 break;
111 }
112 }
113 if value.is_none() && avail.corner {
114 value = Some(refs.corner);
115 }
116 if value.is_none() {
117 for x in 0..span {
118 if avail.top & (1 << x) != 0 {
119 value = Some(refs.top[x]);
120 break;
121 }
122 }
123 }
124 refs.left[span - 1] = value.unwrap_or(T::from_i32(1 << (bit_depth - 1)));
125 }
126 // Step 2: up the left edge, each hole taking the sample below it.
127 for y in (0..span - 1).rev() {
128 if avail.left & (1 << y) == 0 {
129 refs.left[y] = refs.left[y + 1];
130 }
131 }
132 if !avail.corner {
133 refs.corner = refs.left[0];
134 }
135 // Step 3: along the top, each hole taking the sample to its left.
136 for x in 0..span {
137 if avail.top & (1 << x) == 0 {
138 refs.top[x] = if x == 0 { refs.corner } else { refs.top[x - 1] };
139 }
140 }
141}
142
143/// `filterFlag` of 8.4.4.2.3.
144fn filter_flag(mode: u8, n: usize, is_luma: bool) -> bool {
145 if !is_luma || mode == 1 || n == 4 {
146 return false;
147 }
148 let min_dist = (i32::from(mode) - 26)
149 .abs()
150 .min((i32::from(mode) - 10).abs());
151 let threshold = match n {
152 8 => 7,
153 16 => 1,
154 _ => 0,
155 };
156 min_dist > threshold
157}
158
159/// Neighbouring sample filtering (8.4.4.2.3), including the strong bilinear
160/// path for flat 32x32 luma blocks. `bit_depth` sets the flatness threshold
161/// `1 << (BitDepthY - 5)`.
162pub fn filter_refs<T: Sample>(
163 refs: &Refs<T>,
164 mode: u8,
165 n: usize,
166 is_luma: bool,
167 strong_smoothing: bool,
168 bit_depth: u32,
169) -> Refs<T> {
170 if !filter_flag(mode, n, is_luma) {
171 return *refs;
172 }
173 let span = 2 * n;
174 let mut out = *refs;
175 let threshold = 1i32 << (bit_depth - 5);
176 let c: i32 = refs.corner.into();
177 let bi_int = strong_smoothing
178 && is_luma
179 && n == 32
180 && (c + Into::<i32>::into(refs.top[span - 1]) - 2 * Into::<i32>::into(refs.top[n - 1]))
181 .abs()
182 < threshold
183 && (c + Into::<i32>::into(refs.left[span - 1]) - 2 * Into::<i32>::into(refs.left[n - 1]))
184 .abs()
185 < threshold;
186 if bi_int {
187 let corner = c;
188 let bottom_left: i32 = refs.left[63].into();
189 let top_right: i32 = refs.top[63].into();
190 for y in 0..63 {
191 out.left[y] =
192 T::from_i32(((63 - y as i32) * corner + (y as i32 + 1) * bottom_left + 32) >> 6);
193 }
194 out.left[63] = refs.left[63];
195 for x in 0..63 {
196 out.top[x] =
197 T::from_i32(((63 - x as i32) * corner + (x as i32 + 1) * top_right + 32) >> 6);
198 }
199 out.top[63] = refs.top[63];
200 return out;
201 }
202 let v = |s: T| -> i32 { s.into() };
203 out.corner = T::from_i32((v(refs.left[0]) + 2 * c + v(refs.top[0]) + 2) >> 2);
204 for y in 0..span - 1 {
205 let below = v(refs.left[y + 1]);
206 let here = v(refs.left[y]);
207 let above = if y == 0 { c } else { v(refs.left[y - 1]) };
208 out.left[y] = T::from_i32((below + 2 * here + above + 2) >> 2);
209 }
210 for x in 0..span - 1 {
211 let right = v(refs.top[x + 1]);
212 let here = v(refs.top[x]);
213 let left = if x == 0 { c } else { v(refs.top[x - 1]) };
214 out.top[x] = T::from_i32((right + 2 * here + left + 2) >> 2);
215 }
216 out
217}
218
219/// Predict an `n x n` block into `out` (row major), for `mode` 0..=34.
220///
221/// `refs` must already be substituted; filtering is applied here so that a
222/// caller trying 35 modes filters once per mode rather than per sample.
223pub fn predict<T: Sample>(
224 refs: &Refs<T>,
225 mode: u8,
226 n: usize,
227 is_luma: bool,
228 strong_smoothing: bool,
229 bit_depth: u32,
230 out: &mut [T],
231) {
232 let flags = PredFlags {
233 smooth: is_luma,
234 strong: strong_smoothing,
235 edge: is_luma,
236 no_boundary: false,
237 };
238 predict_ext(refs, mode, n, flags, bit_depth, out);
239}
240
241/// Which of the component-dependent steps of intra prediction apply to a block
242/// (8.4.4.2.3 to 8.4.4.2.6 with the range extension).
243#[derive(Clone, Copy, Debug)]
244pub struct PredFlags {
245 /// The neighbouring sample filter may run: luma or 4:4:4 chroma, with
246 /// `intra_smoothing_disabled_flag` off.
247 pub smooth: bool,
248 /// The strong bilinear filter may run (`strong_intra_smoothing_enabled_flag`, luma).
249 pub strong: bool,
250 /// The DC and horizontal/vertical edge filters may run (luma blocks below 32x32).
251 pub edge: bool,
252 /// `disableIntraBoundaryFilter`: implicit RDPCM on a transquant-bypass unit.
253 pub no_boundary: bool,
254}
255
256/// [`predict`] with explicit [`PredFlags`].
257pub fn predict_ext<T: Sample>(
258 refs: &Refs<T>,
259 mode: u8,
260 n: usize,
261 flags: PredFlags,
262 bit_depth: u32,
263 out: &mut [T],
264) {
265 if !filter_flag(mode, n, flags.smooth) {
266 predict_filtered(refs, mode, n, flags, bit_depth, out);
267 return;
268 }
269 let filtered = filter_refs(refs, mode, n, flags.smooth, flags.strong, bit_depth);
270 predict_filtered(&filtered, mode, n, flags, bit_depth, out);
271}
272
273/// [`predict`] for a caller that predicts many modes from the same reference samples:
274/// the smoothed copy (the same for every mode that smooths) is built once into `cache`
275/// (set it to `None` when `refs` changes). Bit-identical to [`predict`].
276#[allow(clippy::too_many_arguments)]
277pub fn predict_cached<T: Sample>(
278 refs: &Refs<T>,
279 cache: &mut Option<Refs<T>>,
280 mode: u8,
281 n: usize,
282 is_luma: bool,
283 strong_smoothing: bool,
284 bit_depth: u32,
285 out: &mut [T],
286) {
287 let flags = PredFlags {
288 smooth: is_luma,
289 strong: strong_smoothing,
290 edge: is_luma,
291 no_boundary: false,
292 };
293 predict_cached_ext(refs, cache, mode, n, flags, bit_depth, out);
294}
295
296/// [`predict_cached`] with explicit [`PredFlags`]. The cache is keyed by the caller on the
297/// reference samples and `flags.smooth`/`flags.strong`, which must not change while it is
298/// set; the filtered copy does not depend on the mode beyond `filter_flag`.
299pub fn predict_cached_ext<T: Sample>(
300 refs: &Refs<T>,
301 cache: &mut Option<Refs<T>>,
302 mode: u8,
303 n: usize,
304 flags: PredFlags,
305 bit_depth: u32,
306 out: &mut [T],
307) {
308 if !filter_flag(mode, n, flags.smooth) {
309 predict_filtered(refs, mode, n, flags, bit_depth, out);
310 return;
311 }
312 let filtered = cache
313 .get_or_insert_with(|| filter_refs(refs, mode, n, flags.smooth, flags.strong, bit_depth));
314 predict_filtered(filtered, mode, n, flags, bit_depth, out);
315}
316
317/// Predict from reference samples that already went through the 8.4.4.2.3 filter (or
318/// did not need it).
319pub fn predict_filtered<T: Sample>(
320 refs: &Refs<T>,
321 mode: u8,
322 n: usize,
323 flags: PredFlags,
324 bit_depth: u32,
325 out: &mut [T],
326) {
327 predict_dispatch(refs, mode, n, flags, bit_depth, out);
328}
329
330/// [`predict_body`] compiled twice by `multiversion`: the AVX2 clone has the vector
331/// instructions enabled for the inlined predictors, the baseline clone is the same code.
332#[multiversion::multiversion(targets("x86_64+avx2"))]
333fn predict_dispatch<T: Sample>(
334 refs: &Refs<T>,
335 mode: u8,
336 n: usize,
337 flags: PredFlags,
338 bit_depth: u32,
339 out: &mut [T],
340) {
341 predict_body(refs, mode, n, flags, bit_depth, out);
342}
343
344#[inline(always)]
345fn predict_body<T: Sample>(
346 refs: &Refs<T>,
347 mode: u8,
348 n: usize,
349 flags: PredFlags,
350 bit_depth: u32,
351 out: &mut [T],
352) {
353 match mode {
354 0 => predict_planar(refs, n, out),
355 1 => predict_dc(refs, n, flags.edge, out),
356 _ => predict_angular(
357 refs,
358 mode,
359 n,
360 flags.edge && !flags.no_boundary,
361 bit_depth,
362 out,
363 ),
364 }
365}
366
367#[inline(always)]
368fn predict_planar<T: Sample>(refs: &Refs<T>, n: usize, out: &mut [T]) {
369 let log2n = n.trailing_zeros();
370 let top_right: i32 = refs.top[n].into();
371 let bottom_left: i32 = refs.left[n].into();
372 for y in 0..n {
373 let left: i32 = refs.left[y].into();
374 for x in 0..n {
375 let value = (n as i32 - 1 - x as i32) * left
376 + (x as i32 + 1) * top_right
377 + (n as i32 - 1 - y as i32) * Into::<i32>::into(refs.top[x])
378 + (y as i32 + 1) * bottom_left
379 + n as i32;
380 out[y * n + x] = T::from_i32(value >> (log2n + 1));
381 }
382 }
383}
384
385#[inline(always)]
386fn predict_dc<T: Sample>(refs: &Refs<T>, n: usize, is_luma: bool, out: &mut [T]) {
387 let mut sum = n as u32;
388 for i in 0..n {
389 let t: i32 = refs.top[i].into();
390 let l: i32 = refs.left[i].into();
391 sum += (t + l) as u32;
392 }
393 let dc = (sum >> (n.trailing_zeros() + 1)) as i32;
394 for value in out[..n * n].iter_mut() {
395 *value = T::from_i32(dc);
396 }
397 if is_luma && n < 32 {
398 let l0: i32 = refs.left[0].into();
399 let t0: i32 = refs.top[0].into();
400 out[0] = T::from_i32((l0 + 2 * dc + t0 + 2) >> 2);
401 for (x, sample) in out[1..n].iter_mut().enumerate() {
402 let t: i32 = refs.top[x + 1].into();
403 *sample = T::from_i32((t + 3 * dc + 2) >> 2);
404 }
405 for y in 1..n {
406 let l: i32 = refs.left[y].into();
407 out[y * n] = T::from_i32((l + 3 * dc + 2) >> 2);
408 }
409 }
410}
411
412#[inline(always)]
413fn predict_angular<T: Sample>(
414 refs: &Refs<T>,
415 mode: u8,
416 n: usize,
417 is_luma: bool,
418 bit_depth: u32,
419 out: &mut [T],
420) {
421 let angle = ANGLE[mode as usize];
422 let inv_angle = INV_ANGLE[mode as usize];
423 let max = (1i32 << bit_depth) - 1;
424 // ref[] is indexed from -nTbS to 2 * nTbS; store it shifted by 32.
425 const BIAS: usize = 32;
426 let mut reference = [0i32; 32 + 65];
427 let (main, side): (&[T; 64], &[T; 64]) = if mode >= 18 {
428 (&refs.top, &refs.left)
429 } else {
430 (&refs.left, &refs.top)
431 };
432 reference[BIAS] = refs.corner.into();
433 for x in 1..=n {
434 reference[BIAS + x] = main[x - 1].into();
435 }
436 if angle < 0 {
437 let last = (n as i32 * angle) >> 5;
438 if last < -1 {
439 for x in last..=-1 {
440 let idx = ((x * inv_angle + 128) >> 8) - 1;
441 reference[(BIAS as i32 + x) as usize] = if idx < 0 {
442 refs.corner.into()
443 } else {
444 side[idx as usize].into()
445 };
446 }
447 }
448 } else {
449 for x in n + 1..=2 * n {
450 reference[BIAS + x] = main[x - 1].into();
451 }
452 }
453 // `idx`/`fact` depend only on the "row" index along the prediction
454 // direction (y for the vertical modes, x for the horizontal ones — the
455 // whole difference between the two halves of 8.4.4.2.6), not on the
456 // position across it.
457 if mode >= 18 {
458 for y in 0..n {
459 let idx = ((y as i32 + 1) * angle) >> 5;
460 let fact = ((y as i32 + 1) * angle) & 31;
461 let base = (BIAS as i32 + idx + 1) as usize;
462 let row = &mut out[y * n..y * n + n];
463 if fact != 0 {
464 let (w0, w1) = (32 - fact, fact);
465 for x in 0..n {
466 row[x] = T::from_i32(
467 (w0 * reference[base + x] + w1 * reference[base + x + 1] + 16) >> 5,
468 );
469 }
470 } else {
471 for x in 0..n {
472 row[x] = T::from_i32(reference[base + x]);
473 }
474 }
475 }
476 } else {
477 for x in 0..n {
478 let idx = ((x as i32 + 1) * angle) >> 5;
479 let fact = ((x as i32 + 1) * angle) & 31;
480 let base = (BIAS as i32 + idx + 1) as usize;
481 if fact != 0 {
482 let (w0, w1) = (32 - fact, fact);
483 for y in 0..n {
484 out[y * n + x] = T::from_i32(
485 (w0 * reference[base + y] + w1 * reference[base + y + 1] + 16) >> 5,
486 );
487 }
488 } else {
489 for y in 0..n {
490 out[y * n + x] = T::from_i32(reference[base + y]);
491 }
492 }
493 }
494 }
495 // The vertical and horizontal modes carry a boundary filter on luma
496 // blocks below 32x32 (8-60, 8-68).
497 if is_luma && n < 32 {
498 let top0: i32 = refs.top[0].into();
499 let left0: i32 = refs.left[0].into();
500 let corner: i32 = refs.corner.into();
501 if mode == 26 {
502 for y in 0..n {
503 let value = top0 + ((Into::<i32>::into(refs.left[y]) - corner) >> 1);
504 out[y * n] = T::from_i32(value.clamp(0, max));
505 }
506 } else if mode == 10 {
507 for (x, sample) in out[..n].iter_mut().enumerate() {
508 let value = left0 + ((Into::<i32>::into(refs.top[x]) - corner) >> 1);
509 *sample = T::from_i32(value.clamp(0, max));
510 }
511 }
512 }
513}
514
515/// The three most probable modes (8.4.2 step 3) from the left and above
516/// neighbours' modes; `None` means the neighbour is unavailable or not intra.
517pub fn mpm_list(left: Option<u8>, above: Option<u8>) -> [u8; 3] {
518 let cand_a = left.unwrap_or(1);
519 let cand_b = above.unwrap_or(1);
520 if cand_a == cand_b {
521 if cand_a < 2 {
522 [0, 1, 26]
523 } else {
524 [
525 cand_a,
526 2 + ((u32::from(cand_a) + 29) % 32) as u8,
527 2 + ((u32::from(cand_a) - 2 + 1) % 32) as u8,
528 ]
529 }
530 } else if cand_a != 0 && cand_b != 0 {
531 [cand_a, cand_b, 0]
532 } else if cand_a != 1 && cand_b != 1 {
533 [cand_a, cand_b, 1]
534 } else {
535 [cand_a, cand_b, 26]
536 }
537}
538
539/// `scanIdx` for a residual block (7.4.9.11): mode-dependent for the small
540/// luma and chroma blocks, diagonal everywhere else.
541///
542/// 0 = up-right diagonal, 1 = horizontal, 2 = vertical.
543pub fn scan_index(mode: u8, log2_size: u32, is_luma: bool) -> usize {
544 scan_index_fmt(mode, log2_size, is_luma, false)
545}
546
547/// [`scan_index`] for any chroma format: 4:4:4 chroma blocks of 8x8 are mode-dependent too.
548pub fn scan_index_fmt(mode: u8, log2_size: u32, is_luma: bool, chroma_444: bool) -> usize {
549 let mode_dependent = if is_luma || chroma_444 {
550 log2_size == 2 || log2_size == 3
551 } else {
552 log2_size == 2
553 };
554 if !mode_dependent {
555 return 0;
556 }
557 if (6..=14).contains(&mode) {
558 2
559 } else if (22..=30).contains(&mode) {
560 1
561 } else {
562 0
563 }
564}
565
566#[cfg(test)]
567mod tests {
568 use super::*;
569
570 fn full_refs(value: u8, n: usize) -> (Refs, Availability) {
571 let refs = Refs::<u8> {
572 corner: value,
573 top: [value; 64],
574 left: [value; 64],
575 };
576 let mask = if 2 * n >= 64 {
577 u64::MAX
578 } else {
579 (1u64 << (2 * n)) - 1
580 };
581 (
582 refs,
583 Availability {
584 corner: true,
585 top: mask,
586 left: mask,
587 },
588 )
589 }
590
591 #[test]
592 fn every_mode_predicts_a_flat_block_flat() {
593 // With every neighbour equal, all 35 modes must return that value —
594 // including the boundary filters, which are differences of equals.
595 for &n in &[4usize, 8, 16, 32] {
596 let (refs, _) = full_refs(96, n);
597 let mut out = vec![0u8; n * n];
598 for mode in 0..35u8 {
599 predict(&refs, mode, n, true, true, 8, &mut out);
600 assert!(
601 out.iter().all(|&v| v == 96),
602 "mode {mode} n {n}: {:?}",
603 &out[..8]
604 );
605 }
606 }
607 }
608
609 #[test]
610 fn substitution_fills_from_the_nearest_real_sample() {
611 let n = 4;
612 let mut refs = Refs::<u8> {
613 corner: 0,
614 top: [0; 64],
615 left: [0; 64],
616 };
617 refs.left[5] = 70;
618 let avail = Availability {
619 corner: false,
620 top: 0,
621 left: 1 << 5,
622 };
623 substitute(&mut refs, &avail, n, 8);
624 // Everything takes 70: below it by the bottom-left rule, above it by
625 // the walk up, then the corner and the whole top row.
626 assert!(refs.left[..8].iter().all(|&v| v == 70));
627 assert!(refs.top[..8].iter().all(|&v| v == 70));
628 assert_eq!(refs.corner, 70);
629
630 // Nothing available at all: mid-grey.
631 let mut refs = Refs::<u8>::default();
632 substitute(&mut refs, &Availability::default(), n, 8);
633 assert_eq!(refs.corner, 128);
634 assert!(refs.top[..8].iter().all(|&v| v == 128));
635 }
636
637 #[test]
638 fn vertical_and_horizontal_modes_copy_their_edge() {
639 let n = 8;
640 let mut refs = Refs::<u8>::default();
641 for i in 0..64 {
642 refs.top[i] = (10 + i) as u8;
643 refs.left[i] = (200 - i) as u8;
644 }
645 refs.corner = 100;
646 let mut out = vec![0u8; n * n];
647 // Mode 26 on chroma has no boundary filter: every row is the top row.
648 predict(&refs, 26, n, false, false, 8, &mut out);
649 for y in 0..n {
650 for x in 0..n {
651 assert_eq!(out[y * n + x], refs.top[x], "mode 26 at {x},{y}");
652 }
653 }
654 // Mode 10 likewise copies the left column across.
655 predict(&refs, 10, n, false, false, 8, &mut out);
656 for y in 0..n {
657 for x in 0..n {
658 assert_eq!(out[y * n + x], refs.left[y], "mode 10 at {x},{y}");
659 }
660 }
661 }
662
663 #[test]
664 fn diagonal_mode_2_walks_the_left_column() {
665 // Mode 2 is the 45-degree "bottom-left" direction: angle 32 means one
666 // whole sample of shift per row along the left reference.
667 let n = 4;
668 let mut refs = Refs::<u8>::default();
669 for i in 0..64 {
670 refs.left[i] = i as u8;
671 }
672 let mut out = vec![0u8; n * n];
673 predict(&refs, 2, n, false, false, 8, &mut out);
674 for y in 0..n {
675 for x in 0..n {
676 assert_eq!(out[y * n + x], (y + x + 1) as u8, "at {x},{y}");
677 }
678 }
679 }
680
681 #[test]
682 fn mpm_list_follows_the_spec_cases() {
683 assert_eq!(mpm_list(None, None), [0, 1, 26]);
684 assert_eq!(mpm_list(Some(0), Some(0)), [0, 1, 26]);
685 assert_eq!(mpm_list(Some(10), Some(10)), [10, 9, 11]);
686 assert_eq!(mpm_list(Some(2), Some(2)), [2, 33, 3]);
687 assert_eq!(mpm_list(Some(10), Some(26)), [10, 26, 0]);
688 assert_eq!(mpm_list(Some(0), Some(26)), [0, 26, 1]);
689 assert_eq!(mpm_list(Some(0), Some(1)), [0, 1, 26]);
690 }
691
692 #[test]
693 fn scan_index_is_mode_dependent_only_for_small_blocks() {
694 assert_eq!(scan_index(10, 2, true), 2);
695 assert_eq!(scan_index(26, 3, true), 1);
696 assert_eq!(scan_index(26, 4, true), 0);
697 assert_eq!(scan_index(10, 2, false), 2);
698 assert_eq!(scan_index(10, 3, false), 0);
699 assert_eq!(scan_index(0, 2, true), 0);
700 }
701
702 fn check_all<T: Sample + PartialEq + std::fmt::Debug>(bd: u32) {
703 let mut seed = 0x9e37_79b9u32;
704 let mut next = || {
705 seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
706 (seed >> 8) as i32
707 };
708 let max = (1i32 << bd) - 1;
709 for round in 0..6 {
710 for n in [4usize, 8, 16, 32] {
711 let mut refs = Refs::<T>::default();
712 let mut v = |round: i32| match round {
713 0 => 0,
714 1 => max,
715 _ => next().rem_euclid(max + 1),
716 };
717 refs.corner = T::from_i32(v(round));
718 for i in 0..64 {
719 refs.top[i] = T::from_i32(v(round));
720 refs.left[i] = T::from_i32(v(round));
721 }
722 let mut cache = None;
723 for luma in [true, false] {
724 for mode in 0..35u8 {
725 let mut a = vec![T::default(); n * n];
726 let mut b = a.clone();
727 let mut c = a.clone();
728 predict(&refs, mode, n, luma, true, bd, &mut a);
729 predict_cached(&refs, &mut cache, mode, n, luma, true, bd, &mut b);
730 let f = filter_refs(&refs, mode, n, luma, true, bd);
731 let flags = PredFlags {
732 smooth: luma,
733 strong: true,
734 edge: luma,
735 no_boundary: false,
736 };
737 predict_body(&f, mode, n, flags, bd, &mut c);
738 assert_eq!(a, b, "cached n={n} mode={mode}");
739 assert_eq!(a, c, "scalar n={n} mode={mode}");
740 }
741 cache = None;
742 }
743 }
744 }
745 }
746
747 #[test]
748 fn vector_and_cached_prediction_match_the_plain_one() {
749 check_all::<u8>(8);
750 check_all::<u16>(8);
751 check_all::<u16>(10);
752 }
753}