forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

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