forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBdfeffbcTahinli2 months ago3180 commits

pred.rs759 lines, 26.3 KiBHistoryRawDownload
1//! Intra prediction (spec 8.3.1, 8.3.3, 8.3.4), 8-bit.
2//!
3//! Kernels operate on a small gathered neighbour set so the hot loop touches
4//! the picture plane only twice per block: one gather, one write. All
5//! arithmetic is in `i32` with a final `clamp(0, 255)`.
6
7/// Neighbour samples of a 4x4 block, gathered once (spec 8.3.1.2's 13
8/// samples). `top[4..8]` already has the top-right substitution rule applied:
9/// when p[4..7, -1] is unavailable but p[3, -1] is, it is replicated.
10#[derive(Debug, Clone, Copy)]
11pub struct Nbr4 {
12 /// p[0..7, -1].
13 pub top: [u8; 8],
14 /// p[-1, 0..3].
15 pub left: [u8; 4],
16 /// p[-1, -1].
17 pub top_left: u8,
18 /// p[x, -1] available.
19 pub have_top: bool,
20 /// p[-1, y] available.
21 pub have_left: bool,
22}
23
24/// Predict a 4x4 block (spec 8.3.1.2.1-9) into `out` in raster order.
25///
26/// Modes 0..=8; conformant streams only signal modes whose required samples
27/// are available, but unavailable inputs still produce defined output (DC
28/// fallbacks) rather than reading junk.
29pub fn pred_4x4(mode: u8, n: &Nbr4, out: &mut [u8; 16]) {
30 let t = |x: usize| i32::from(n.top[x]);
31 let l = |y: usize| i32::from(n.left[y]);
32 let tl = i32::from(n.top_left);
33 match mode {
34 // Vertical.
35 0 => {
36 for y in 0..4 {
37 for x in 0..4 {
38 out[y * 4 + x] = n.top[x];
39 }
40 }
41 }
42 // Horizontal.
43 1 => {
44 for y in 0..4 {
45 out[y * 4..y * 4 + 4].fill(n.left[y]);
46 }
47 }
48 // DC (8-48..8-51).
49 2 => {
50 let v = match (n.have_top, n.have_left) {
51 (true, true) => (t(0) + t(1) + t(2) + t(3) + l(0) + l(1) + l(2) + l(3) + 4) >> 3,
52 (false, true) => (l(0) + l(1) + l(2) + l(3) + 2) >> 2,
53 (true, false) => (t(0) + t(1) + t(2) + t(3) + 2) >> 2,
54 (false, false) => 128,
55 };
56 out.fill(v as u8);
57 }
58 // Diagonal down-left (8-52, 8-53).
59 3 => {
60 for y in 0..4 {
61 for x in 0..4 {
62 let v = if x == 3 && y == 3 {
63 (t(6) + 3 * t(7) + 2) >> 2
64 } else {
65 (t(x + y) + 2 * t(x + y + 1) + t(x + y + 2) + 2) >> 2
66 };
67 out[y * 4 + x] = v as u8;
68 }
69 }
70 }
71 // Diagonal down-right (8-54..8-56). Index -1 is the top-left sample.
72 4 => {
73 let te = |i: i32| if i < 0 { tl } else { t(i as usize) };
74 let le = |i: i32| if i < 0 { tl } else { l(i as usize) };
75 for y in 0..4i32 {
76 for x in 0..4i32 {
77 let v = if x > y {
78 (te(x - y - 2) + 2 * te(x - y - 1) + te(x - y) + 2) >> 2
79 } else if x < y {
80 (le(y - x - 2) + 2 * le(y - x - 1) + le(y - x) + 2) >> 2
81 } else {
82 (t(0) + 2 * tl + l(0) + 2) >> 2
83 };
84 out[(y * 4 + x) as usize] = v as u8;
85 }
86 }
87 }
88 // Vertical-right (8-57..8-60).
89 5 => {
90 let te = |i: i32| if i < 0 { tl } else { t(i as usize) };
91 let le = |i: i32| if i < 0 { tl } else { l(i as usize) };
92 for y in 0..4i32 {
93 for x in 0..4i32 {
94 let z = 2 * x - y;
95 let i = x - (y >> 1);
96 let v = if z >= 0 && z % 2 == 0 {
97 (te(i - 1) + te(i) + 1) >> 1
98 } else if z >= 0 {
99 (te(i - 2) + 2 * te(i - 1) + te(i) + 2) >> 2
100 } else if z == -1 {
101 (l(0) + 2 * tl + t(0) + 2) >> 2
102 } else {
103 (le(y - 1) + 2 * le(y - 2) + le(y - 3) + 2) >> 2
104 };
105 out[(y * 4 + x) as usize] = v as u8;
106 }
107 }
108 }
109 // Horizontal-down (8-61..8-64).
110 6 => {
111 let te = |i: i32| if i < 0 { tl } else { t(i as usize) };
112 let le = |i: i32| if i < 0 { tl } else { l(i as usize) };
113 for y in 0..4i32 {
114 for x in 0..4i32 {
115 let z = 2 * y - x;
116 let i = y - (x >> 1);
117 let v = if z >= 0 && z % 2 == 0 {
118 (le(i - 1) + le(i) + 1) >> 1
119 } else if z >= 0 {
120 (le(i - 2) + 2 * le(i - 1) + le(i) + 2) >> 2
121 } else if z == -1 {
122 (l(0) + 2 * tl + t(0) + 2) >> 2
123 } else {
124 (te(x - 1) + 2 * te(x - 2) + te(x - 3) + 2) >> 2
125 };
126 out[(y * 4 + x) as usize] = v as u8;
127 }
128 }
129 }
130 // Vertical-left (8-65, 8-66).
131 7 => {
132 for y in 0..4usize {
133 for x in 0..4usize {
134 let i = x + (y >> 1);
135 let v = if y % 2 == 0 {
136 (t(i) + t(i + 1) + 1) >> 1
137 } else {
138 (t(i) + 2 * t(i + 1) + t(i + 2) + 2) >> 2
139 };
140 out[y * 4 + x] = v as u8;
141 }
142 }
143 }
144 // Horizontal-up (8-67..8-70).
145 _ => {
146 for y in 0..4usize {
147 for x in 0..4usize {
148 let z = x + 2 * y;
149 let i = y + (x >> 1);
150 let v = if z < 5 && z % 2 == 0 {
151 (l(i) + l(i + 1) + 1) >> 1
152 } else if z < 5 {
153 (l(i) + 2 * l(i + 1) + l(i + 2) + 2) >> 2
154 } else if z == 5 {
155 (l(2) + 3 * l(3) + 2) >> 2
156 } else {
157 l(3)
158 };
159 out[y * 4 + x] = v as u8;
160 }
161 }
162 }
163 }
164}
165
166/// Neighbour samples of an 8x8 luma block (spec 8.3.2.2's 25 samples), already
167/// low-pass filtered by [`filter_nbr8`]. `top[8..16]` carries the top-right
168/// substitution rule.
169#[derive(Debug, Clone, Copy)]
170pub struct Nbr8 {
171 /// p'[0..15, -1].
172 pub top: [u8; 16],
173 /// p'[-1, 0..7].
174 pub left: [u8; 8],
175 /// p'[-1, -1].
176 pub top_left: u8,
177 /// p[x, -1] available.
178 pub have_top: bool,
179 /// p[-1, y] available.
180 pub have_left: bool,
181 /// p[-1, -1] available. Its own neighbour (the above-left macroblock for
182 /// block 0), not the conjunction of the two runs.
183 pub have_tl: bool,
184}
185
186/// Reference sample filtering for Intra_8x8 prediction (spec 8.3.2.2.1).
187///
188/// Input is the unfiltered neighbourhood; each of the three runs (top row,
189/// corner, left column) is filtered only when it is wholly available, exactly
190/// as the clause states, so an edge block keeps the raw samples the prediction
191/// modes it is allowed to signal actually read.
192pub fn filter_nbr8(n: &Nbr8) -> Nbr8 {
193 let mut out = *n;
194 let have_tl = n.have_tl;
195 if n.have_top {
196 let t = |i: usize| i32::from(n.top[i]);
197 out.top[0] = if have_tl {
198 ((i32::from(n.top_left) + 2 * t(0) + t(1) + 2) >> 2) as u8
199 } else {
200 ((3 * t(0) + t(1) + 2) >> 2) as u8
201 };
202 for x in 1..15 {
203 out.top[x] = ((t(x - 1) + 2 * t(x) + t(x + 1) + 2) >> 2) as u8;
204 }
205 out.top[15] = ((t(14) + 3 * t(15) + 2) >> 2) as u8;
206 }
207 if have_tl {
208 // 8-82 to 8-84: which taps the corner takes depends on which of its two
209 // adjacent samples exist.
210 let (tl, t0, l0) = (
211 i32::from(n.top_left),
212 i32::from(n.top[0]),
213 i32::from(n.left[0]),
214 );
215 out.top_left = match (n.have_top, n.have_left) {
216 (true, true) => ((t0 + 2 * tl + l0 + 2) >> 2) as u8,
217 (true, false) => ((3 * tl + t0 + 2) >> 2) as u8,
218 (false, true) => ((3 * tl + l0 + 2) >> 2) as u8,
219 (false, false) => n.top_left,
220 };
221 }
222 if n.have_left {
223 let l = |i: usize| i32::from(n.left[i]);
224 out.left[0] = if have_tl {
225 ((i32::from(n.top_left) + 2 * l(0) + l(1) + 2) >> 2) as u8
226 } else {
227 ((3 * l(0) + l(1) + 2) >> 2) as u8
228 };
229 for y in 1..7 {
230 out.left[y] = ((l(y - 1) + 2 * l(y) + l(y + 1) + 2) >> 2) as u8;
231 }
232 out.left[7] = ((l(6) + 3 * l(7) + 2) >> 2) as u8;
233 }
234 out
235}
236
237/// Predict an 8x8 luma block (spec 8.3.2.2.2 - 8.3.2.2.10) into `out` in raster
238/// order. `n` must already be filtered by [`filter_nbr8`].
239// x and y are sample coordinates that also index the neighbour runs; the
240// clause's index arithmetic is what the loops spell out.
241#[allow(clippy::needless_range_loop)]
242pub fn pred_8x8(mode: u8, n: &Nbr8, out: &mut [u8; 64]) {
243 let t = |x: i32| i32::from(n.top[x as usize]);
244 let l = |y: i32| i32::from(n.left[y as usize]);
245 let tl = i32::from(n.top_left);
246 // Index -1 of either run is the corner sample.
247 let te = |i: i32| if i < 0 { tl } else { t(i) };
248 let le = |i: i32| if i < 0 { tl } else { l(i) };
249 match mode {
250 // Vertical (8-89).
251 0 => {
252 for y in 0..8 {
253 out[y * 8..y * 8 + 8].copy_from_slice(&n.top[..8]);
254 }
255 }
256 // Horizontal (8-90).
257 1 => {
258 for y in 0..8 {
259 out[y * 8..y * 8 + 8].fill(n.left[y]);
260 }
261 }
262 // DC (8-91..8-94).
263 2 => {
264 let sum_t: i32 = (0..8).map(t).sum();
265 let sum_l: i32 = (0..8).map(l).sum();
266 let v = match (n.have_top, n.have_left) {
267 (true, true) => (sum_t + sum_l + 8) >> 4,
268 (true, false) => (sum_t + 4) >> 3,
269 (false, true) => (sum_l + 4) >> 3,
270 (false, false) => 128,
271 };
272 out.fill(v as u8);
273 }
274 // Diagonal down-left (8-95, 8-96).
275 3 => {
276 for y in 0..8i32 {
277 for x in 0..8i32 {
278 let v = if x == 7 && y == 7 {
279 (t(14) + 3 * t(15) + 2) >> 2
280 } else {
281 (t(x + y) + 2 * t(x + y + 1) + t(x + y + 2) + 2) >> 2
282 };
283 out[(y * 8 + x) as usize] = v as u8;
284 }
285 }
286 }
287 // Diagonal down-right (8-97..8-99).
288 4 => {
289 for y in 0..8i32 {
290 for x in 0..8i32 {
291 let v = if x > y {
292 (te(x - y - 2) + 2 * te(x - y - 1) + t(x - y) + 2) >> 2
293 } else if x < y {
294 (le(y - x - 2) + 2 * le(y - x - 1) + l(y - x) + 2) >> 2
295 } else {
296 (t(0) + 2 * tl + l(0) + 2) >> 2
297 };
298 out[(y * 8 + x) as usize] = v as u8;
299 }
300 }
301 }
302 // Vertical-right (8-100..8-103).
303 5 => {
304 for y in 0..8i32 {
305 for x in 0..8i32 {
306 let z = 2 * x - y;
307 let i = x - (y >> 1);
308 let v = if z >= 0 && z % 2 == 0 {
309 (te(i - 1) + te(i) + 1) >> 1
310 } else if z >= 0 {
311 (te(i - 2) + 2 * te(i - 1) + te(i) + 2) >> 2
312 } else if z == -1 {
313 (l(0) + 2 * tl + t(0) + 2) >> 2
314 } else {
315 (l(y - 2 * x - 1) + 2 * l(y - 2 * x - 2) + le(y - 2 * x - 3) + 2) >> 2
316 };
317 out[(y * 8 + x) as usize] = v as u8;
318 }
319 }
320 }
321 // Horizontal-down (8-104..8-107).
322 6 => {
323 for y in 0..8i32 {
324 for x in 0..8i32 {
325 let z = 2 * y - x;
326 let i = y - (x >> 1);
327 let v = if z >= 0 && z % 2 == 0 {
328 (le(i - 1) + le(i) + 1) >> 1
329 } else if z >= 0 {
330 (le(i - 2) + 2 * le(i - 1) + le(i) + 2) >> 2
331 } else if z == -1 {
332 (l(0) + 2 * tl + t(0) + 2) >> 2
333 } else {
334 (t(x - 2 * y - 1) + 2 * t(x - 2 * y - 2) + te(x - 2 * y - 3) + 2) >> 2
335 };
336 out[(y * 8 + x) as usize] = v as u8;
337 }
338 }
339 }
340 // Vertical-left (8-108, 8-109).
341 7 => {
342 for y in 0..8i32 {
343 for x in 0..8i32 {
344 let i = x + (y >> 1);
345 let v = if y % 2 == 0 {
346 (t(i) + t(i + 1) + 1) >> 1
347 } else {
348 (t(i) + 2 * t(i + 1) + t(i + 2) + 2) >> 2
349 };
350 out[(y * 8 + x) as usize] = v as u8;
351 }
352 }
353 }
354 // Horizontal-up (8-110..8-113).
355 _ => {
356 for y in 0..8i32 {
357 for x in 0..8i32 {
358 let z = x + 2 * y;
359 let i = y + (x >> 1);
360 let v = if z < 13 && z % 2 == 0 {
361 (l(i) + l(i + 1) + 1) >> 1
362 } else if z < 13 {
363 (l(i) + 2 * l(i + 1) + l(i + 2) + 2) >> 2
364 } else if z == 13 {
365 (l(6) + 3 * l(7) + 2) >> 2
366 } else {
367 l(7)
368 };
369 out[(y * 8 + x) as usize] = v as u8;
370 }
371 }
372 }
373 }
374}
375
376/// A borrowed window into a picture plane for whole-MB prediction: `data`
377/// spans the plane, `stride` its pitch, `origin` the index of this MB's
378/// top-left sample.
379pub struct PlaneWindow<'a> {
380 /// The full plane.
381 pub data: &'a mut [u8],
382 /// Row pitch.
383 pub stride: usize,
384 /// Index of the block's top-left sample.
385 pub origin: usize,
386}
387
388/// Intra_16x16 prediction (spec 8.3.3), writing predictions straight into the
389/// plane; residuals are added on top afterwards.
390pub fn pred_16x16(mode: u8, w: &mut PlaneWindow<'_>, have_top: bool, have_left: bool) {
391 pred_nxn::<16>(mode, w, have_top, have_left, 5, 5);
392}
393
394/// Chroma 8x8 prediction for 4:2:0 (spec 8.3.4). Chroma mode numbering
395/// differs from luma 16x16: 0 = DC, 1 = Horizontal, 2 = Vertical, 3 = Plane.
396pub fn pred_chroma_8x8(mode: u8, w: &mut PlaneWindow<'_>, have_top: bool, have_left: bool) {
397 match mode {
398 0 => pred_chroma_dc(w, have_top, have_left),
399 1 => pred_nxn::<8>(1, w, have_top, have_left, 0, 0),
400 2 => pred_nxn::<8>(0, w, have_top, have_left, 0, 0),
401 _ => pred_nxn::<8>(3, w, have_top, have_left, 0, 34),
402 }
403}
404
405/// Shared V/H/DC/Plane engine for NxN whole-block prediction. `mode` uses the
406/// 16x16 numbering (0 V, 1 H, 2 DC, 3 Plane). `dc_shift` = log2(2N),
407/// `plane_scale` = 5 for luma, 34 for 4:2:0 chroma.
408fn pred_nxn<const N: usize>(
409 mode: u8,
410 w: &mut PlaneWindow<'_>,
411 have_top: bool,
412 have_left: bool,
413 dc_shift: u32,
414 plane_scale: i32,
415) {
416 let stride = w.stride;
417 let o = w.origin;
418 match mode {
419 0 => {
420 // Vertical: replicate the row above.
421 let (top, rest) = w.data.split_at_mut(o);
422 let top_row = &top[o - stride..o - stride + N];
423 for y in 0..N {
424 rest[y * stride..y * stride + N].copy_from_slice(top_row);
425 }
426 }
427 1 => {
428 for y in 0..N {
429 let v = w.data[o + y * stride - 1];
430 w.data[o + y * stride..o + y * stride + N].fill(v);
431 }
432 }
433 2 => {
434 let mut sum = 0i32;
435 if have_top {
436 for x in 0..N {
437 sum += i32::from(w.data[o - stride + x]);
438 }
439 }
440 if have_left {
441 for y in 0..N {
442 sum += i32::from(w.data[o + y * stride - 1]);
443 }
444 }
445 let v = match (have_top, have_left) {
446 (true, true) => (sum + (1 << (dc_shift - 1))) >> dc_shift,
447 (true, false) | (false, true) => (sum + (1 << (dc_shift - 2))) >> (dc_shift - 1),
448 (false, false) => 128,
449 } as u8;
450 for y in 0..N {
451 w.data[o + y * stride..o + y * stride + N].fill(v);
452 }
453 }
454 _ => {
455 // Plane (8-122..8-127 / 8-144..8-149). half = N/2.
456 let half = (N / 2) as i32;
457 let px = |x: i32, y: i32| -> i32 {
458 let idx = (o as i32 + y * stride as i32 + x) as usize;
459 i32::from(w.data[idx])
460 };
461 let mut h = 0i32;
462 let mut v = 0i32;
463 for i in 0..half {
464 h += (i + 1) * (px(half + i, -1) - px(half - 2 - i, -1));
465 v += (i + 1) * (px(-1, half + i) - px(-1, half - 2 - i));
466 }
467 let a = 16 * (px(-1, N as i32 - 1) + px(N as i32 - 1, -1));
468 let b = (plane_scale * h + 32) >> 6;
469 let c = (plane_scale * v + 32) >> 6;
470 for y in 0..N as i32 {
471 for x in 0..N as i32 {
472 let p = (a + b * (x - (half - 1)) + c * (y - (half - 1)) + 16) >> 5;
473 w.data[o + y as usize * stride + x as usize] = p.clamp(0, 255) as u8;
474 }
475 }
476 }
477 }
478}
479
480/// Chroma DC prediction (spec 8.3.4.1): each 4x4 sub-block averages specific
481/// neighbour runs — corner blocks prefer their adjacent edge.
482fn pred_chroma_dc(w: &mut PlaneWindow<'_>, have_top: bool, have_left: bool) {
483 let stride = w.stride;
484 let o = w.origin;
485 let sum_top = |data: &[u8], x0: usize| -> i32 {
486 (0..4).map(|i| i32::from(data[o - stride + x0 + i])).sum()
487 };
488 let sum_left = |data: &[u8], y0: usize| -> i32 {
489 (0..4)
490 .map(|i| i32::from(data[o + (y0 + i) * stride - 1]))
491 .sum()
492 };
493 for by in 0..2 {
494 for bx in 0..2 {
495 // (0,0) and (1,1) use both edges; (1,0) prefers top; (0,1) left.
496 let v = if bx == by {
497 match (have_top, have_left) {
498 (true, true) => (sum_top(w.data, bx * 4) + sum_left(w.data, by * 4) + 4) >> 3,
499 (true, false) => (sum_top(w.data, bx * 4) + 2) >> 2,
500 (false, true) => (sum_left(w.data, by * 4) + 2) >> 2,
501 (false, false) => 128,
502 }
503 } else if bx == 1 {
504 match (have_top, have_left) {
505 (true, _) => (sum_top(w.data, 4) + 2) >> 2,
506 (false, true) => (sum_left(w.data, 0) + 2) >> 2,
507 (false, false) => 128,
508 }
509 } else {
510 match (have_left, have_top) {
511 (true, _) => (sum_left(w.data, 4) + 2) >> 2,
512 (false, true) => (sum_top(w.data, 0) + 2) >> 2,
513 (false, false) => 128,
514 }
515 } as u8;
516 for y in 0..4 {
517 let row = o + (by * 4 + y) * stride + bx * 4;
518 w.data[row..row + 4].fill(v);
519 }
520 }
521 }
522}
523
524/// Add a 4x4 residual to the plane with clamping (picture construction,
525/// spec 8.5.14 path for 8-bit).
526#[inline]
527pub fn add_residual_4x4(data: &mut [u8], stride: usize, origin: usize, r: &[i32; 16]) {
528 for y in 0..4 {
529 let row = origin + y * stride;
530 for x in 0..4 {
531 let p = i32::from(data[row + x]) + r[y * 4 + x];
532 data[row + x] = p.clamp(0, 255) as u8;
533 }
534 }
535}
536
537/// Add an 8x8 residual to the plane with clamping (spec 8.5.13 / picture
538/// construction).
539#[inline]
540pub fn add_residual_8x8(data: &mut [u8], stride: usize, origin: usize, r: &[i32; 64]) {
541 for y in 0..8 {
542 let row = origin + y * stride;
543 for x in 0..8 {
544 let p = i32::from(data[row + x]) + r[y * 8 + x];
545 data[row + x] = p.clamp(0, 255) as u8;
546 }
547 }
548}
549
550#[cfg(test)]
551mod tests {
552 use super::*;
553
554 /// A constant neighbourhood survives the reference filter and every 8x8
555 /// mode reproduces it: the filters are normalised (1+2+1)/4 and the modes
556 /// are weighted averages of the same samples.
557 #[test]
558 fn constant_neighbourhood_is_a_fixed_point() {
559 let n = Nbr8 {
560 top: [77; 16],
561 left: [77; 8],
562 top_left: 77,
563 have_top: true,
564 have_left: true,
565 have_tl: true,
566 };
567 let f = filter_nbr8(&n);
568 assert_eq!(f.top, [77; 16]);
569 assert_eq!(f.left, [77; 8]);
570 assert_eq!(f.top_left, 77);
571 for mode in 0..9u8 {
572 let mut out = [0u8; 64];
573 pred_8x8(mode, &f, &mut out);
574 assert!(out.iter().all(|&v| v == 77), "mode {mode}: {out:?}");
575 }
576 }
577
578 /// The reference filter of 8.3.2.2.1 with the corner unavailable uses the
579 /// 3:1 end taps, and never runs over a run that is not wholly available.
580 #[test]
581 fn reference_filter_edges() {
582 let mut top = [0u8; 16];
583 for (x, t) in top.iter_mut().enumerate() {
584 *t = (x * 4) as u8;
585 }
586 let n = Nbr8 {
587 top,
588 left: [100; 8],
589 top_left: 9,
590 have_top: true,
591 have_left: false,
592 have_tl: false,
593 };
594 let f = filter_nbr8(&n);
595 // No left run: p'[0, -1] takes 8-79, and the left column is untouched.
596 // 8-79 with p[0, -1] = 0 and p[1, -1] = 4.
597 assert_eq!(f.top[0], ((4 + 2) >> 2) as u8);
598 // 8-80 over p[0..2, -1] = 0, 4, 8.
599 assert_eq!(f.top[1], ((2 * 4 + 8 + 2) >> 2) as u8);
600 assert_eq!(f.top[15], ((56 + 3 * 60 + 2) >> 2) as u8);
601 assert_eq!(f.left, [100; 8]);
602 assert_eq!(f.top_left, 9);
603 // DC with no left run averages the top run alone (8-93).
604 let mut out = [0u8; 64];
605 pred_8x8(2, &f, &mut out);
606 let sum: i32 = (0..8).map(|i| i32::from(f.top[i])).sum();
607 assert!(out.iter().all(|&v| i32::from(v) == (sum + 4) >> 3));
608 }
609
610 /// Vertical and horizontal 8x8 copy the filtered runs.
611 #[test]
612 fn vertical_horizontal_8x8() {
613 let mut top = [0u8; 16];
614 for (x, t) in top.iter_mut().enumerate() {
615 *t = (x * 3) as u8;
616 }
617 let n = Nbr8 {
618 top,
619 left: [1, 2, 3, 4, 5, 6, 7, 8],
620 top_left: 0,
621 have_top: true,
622 have_left: true,
623 have_tl: true,
624 };
625 let mut out = [0u8; 64];
626 pred_8x8(0, &n, &mut out);
627 assert_eq!(&out[..8], &n.top[..8]);
628 assert_eq!(&out[56..], &n.top[..8]);
629 pred_8x8(1, &n, &mut out);
630 assert_eq!(out[0], 1);
631 assert_eq!(out[63], 8);
632 }
633
634 #[test]
635 fn dc_4x4_fallbacks() {
636 let n = Nbr4 {
637 top: [10; 8],
638 left: [20; 4],
639 top_left: 0,
640 have_top: true,
641 have_left: true,
642 };
643 let mut out = [0u8; 16];
644 pred_4x4(2, &n, &mut out);
645 assert!(out.iter().all(|&v| v == 15)); // (40 + 80 + 4) >> 3
646 let n2 = Nbr4 {
647 have_left: false,
648 ..n
649 };
650 pred_4x4(2, &n2, &mut out);
651 assert!(out.iter().all(|&v| v == 10));
652 let n3 = Nbr4 {
653 have_top: false,
654 have_left: false,
655 ..n
656 };
657 pred_4x4(2, &n3, &mut out);
658 assert!(out.iter().all(|&v| v == 128));
659 }
660
661 #[test]
662 fn vertical_and_horizontal_4x4() {
663 let n = Nbr4 {
664 top: [1, 2, 3, 4, 5, 6, 7, 8],
665 left: [9, 10, 11, 12],
666 top_left: 0,
667 have_top: true,
668 have_left: true,
669 };
670 let mut out = [0u8; 16];
671 pred_4x4(0, &n, &mut out);
672 assert_eq!(&out[0..4], &[1, 2, 3, 4]);
673 assert_eq!(&out[12..16], &[1, 2, 3, 4]);
674 pred_4x4(1, &n, &mut out);
675 assert_eq!(out[0], 9);
676 assert_eq!(out[15], 12);
677 }
678
679 /// Diagonal down-left of a constant edge stays constant; the (3,3)
680 /// special tap agrees.
681 #[test]
682 fn ddl_constant_edge() {
683 let n = Nbr4 {
684 top: [50; 8],
685 left: [0; 4],
686 top_left: 0,
687 have_top: true,
688 have_left: false,
689 };
690 let mut out = [0u8; 16];
691 pred_4x4(3, &n, &mut out);
692 assert!(out.iter().all(|&v| v == 50));
693 }
694
695 /// Plane prediction of a linear ramp reproduces the ramp.
696 #[test]
697 fn plane_16x16_ramp() {
698 let stride = 24usize;
699 let mut data = vec![0u8; stride * 24];
700 // Fill neighbours: top row and left column of a 16x16 at (1, 1).
701 let origin = stride + 1;
702 for (x, d) in data.iter_mut().enumerate().take(17) {
703 *d = (10 + 2 * x) as u8; // includes top-left at x=0
704 }
705 for y in 0..17 {
706 data[y * stride] = (10 + 3 * y) as u8;
707 }
708 let mut w = PlaneWindow {
709 data: &mut data,
710 stride,
711 origin,
712 };
713 pred_16x16(3, &mut w, true, true);
714 // A perfect plane through those edges: value at (x, y) approximately
715 // 12 + 2x + 3y. Check corners within rounding of the spec filter.
716 let v = |x: usize, y: usize| i32::from(data[origin + y * stride + x]);
717 assert!((v(0, 0) - 17).abs() <= 3, "{}", v(0, 0));
718 assert!((v(15, 0) - (12 + 30)).abs() <= 3);
719 assert!((v(0, 15) - (12 + 45)).abs() <= 3);
720 assert!((v(15, 15) - (12 + 30 + 45)).abs() <= 3);
721 }
722
723 #[test]
724 fn chroma_dc_corner_preferences() {
725 let stride = 16usize;
726 let mut data = vec![0u8; stride * 16];
727 let origin = stride + 1;
728 // Top neighbours 40, left neighbours 80.
729 for x in 0..8 {
730 data[1 + x] = 40;
731 }
732 for y in 0..8 {
733 data[(1 + y) * stride] = 80;
734 }
735 let mut w = PlaneWindow {
736 data: &mut data,
737 stride,
738 origin,
739 };
740 pred_chroma_8x8(0, &mut w, true, true);
741 let v = |x: usize, y: usize| data[origin + y * stride + x];
742 assert_eq!(v(0, 0), 60); // both edges
743 assert_eq!(v(7, 0), 40); // top preferred
744 assert_eq!(v(0, 7), 80); // left preferred
745 assert_eq!(v(7, 7), 60); // both edges
746 }
747
748 #[test]
749 fn add_residual_clamps() {
750 let mut data = vec![250u8; 16];
751 let mut r = [0i32; 16];
752 r[0] = 100;
753 r[1] = -300;
754 add_residual_4x4(&mut data, 4, 0, &r);
755 assert_eq!(data[0], 255);
756 assert_eq!(data[1], 0);
757 assert_eq!(data[2], 250);
758 }
759}