forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

intra.rs724 lines, 27.3 KiBHistoryRawDownload
1//! Intra-frame prediction for VP8 (RFC 6386, Section 12).
2//!
3//! This module implements the spatial predictors a VP8 decoder applies to
4//! intra-coded macroblocks: the 16x16 luma predictors and 8x8 (per-plane)
5//! chroma predictors of Section 12.2/12.3, and the ten 4x4 subblock
6//! predictors of Section 12.3. All functions are dependency-free,
7//! allocate nothing, and are bit-exact against the normative integer math of
8//! the RFC code listings (which are in turn identical to the libvpx oracle in
9//! `vp8/common/reconintra.c`, `vp8/common/reconintra4x4.c` and
10//! `vpx_dsp/intrapred.c`).
11//!
12//! # Edge-pixel conventions (RFC 6386 Section 12, p.50)
13//!
14//! Pixels outside the visible frame are pre-filled by the caller: 127 for the
15//! row above the top row (including the above-left pixel `P`), 129 for the
16//! column left of the leftmost column. V_PRED/H_PRED/TM_PRED consume those
17//! fill values directly; DC_PRED instead decides from the availability flags
18//! which genuinely-visible pixels to average (RFC Section 12.2, "Note that
19//! the averages used in these exceptional cases are not the same as those
20//! that would be arrived at by using the out-of-bounds A and L values").
21//!
22//! # 4x4 above-right synthesis
23//!
24//! The 4x4 predictors use an 8-pixel above row `A[0..8]`: the 4 pixels
25//! immediately above the subblock plus the 4 "above-right" pixels
26//! (frame positions (-1,16)..(-1,19) relative to the macroblock; RFC
27//! Section 12.3, subblocks 3/7/11/15). Producing those extra four pixels is
28//! the caller's job: for a macroblock not on the top frame row they come from
29//! the row above (replicating pixel (-1,15) at the rightmost macroblock of a
30//! row); on the top frame row they are 127. [`predict4`] only consumes
31//! `A[0..8]` as given.
32
33
34/// Weighted average of three adjacent pixels centered at `y`
35/// (RFC 6386 Section 12.3 `avg3`: `(x + 2y + z + 2) >> 2`; libvpx `AVG3`).
36///
37/// Arguments are valid pixel values, so the result always fits in `u8` and no
38/// clamp is needed (RFC Section 12.3, comment above `avg3`).
39#[inline]
40fn avg3(x: u8, y: u8, z: u8) -> u8 {
41 ((u32::from(x) + 2 * u32::from(y) + u32::from(z) + 2) >> 2) as u8
42}
43
44/// Simple average of two pixels (RFC 6386 Section 12.3 `avg2`:
45/// `(x + y + 1) >> 1`; libvpx `AVG2`).
46#[inline]
47fn avg2(x: u8, y: u8) -> u8 {
48 ((u32::from(x) + u32::from(y) + 1) >> 1) as u8
49}
50
51/// `clamp255(v)`: clamp an integer pixel difference into the valid `u8` range
52/// (RFC 6386 Section 12.2 `TMpred` / Section 12.3 `B_TM_PRED`).
53#[inline]
54fn clamp255(v: i32) -> u8 {
55 v.clamp(0, 255) as u8
56}
57
58/// Shared DC/V/H/TM core for the 16x16 luma and 8x8 chroma blocks
59/// (RFC 6386 Section 12.2 semantics; Section 12.3 states 16x16 luma is
60/// "essentially identical" with 16-pixel edges).
61///
62/// DC rounding follows the RFC formula `DCvalue = (sum + (1 << (shf-1))) >> shf`
63/// with `shf` = log2 of the number of averaged pixels: 32/16 pixels for the
64/// 16x16 both/single-edge cases, 16/8 for 8x8.
65fn predict_block<const N: usize>(
66 mode: u8,
67 have_above: bool,
68 have_left: bool,
69 above: &[u8; N],
70 left: &[u8; N],
71 above_left: u8,
72 out: &mut [u8],
73) -> crate::Result<()> {
74 match mode {
75 // DC_PRED: fill the block with the average of the genuinely visible
76 // edge pixels; availability flags alone decide which edges count
77 // (RFC Section 12.2). Neither edge: constant 128.
78 0 => {
79 let sum_above: u32 = above.iter().map(|&p| u32::from(p)).sum();
80 let sum_left: u32 = left.iter().map(|&p| u32::from(p)).sum();
81 let tz = N.trailing_zeros(); // log2(N)
82 let v: u32 = match (have_above, have_left) {
83 (true, true) => (sum_above + sum_left + N as u32) >> (tz + 1),
84 (false, false) => 128,
85 // Above available only: average the above row.
86 (true, false) => (sum_above + (N as u32 >> 1)) >> tz,
87 // Left column available only: average the left column.
88 (false, true) => (sum_left + (N as u32 >> 1)) >> tz,
89 };
90 out.fill(v as u8);
91 }
92 // V_PRED: every row is a copy of the above row A (RFC Section 12.2).
93 1 => {
94 for row in out.as_chunks_mut::<N>().0.iter_mut() {
95 row.copy_from_slice(above);
96 }
97 }
98 // H_PRED: every column is a copy of the left column L (RFC Section 12.2).
99 2 => {
100 for (row, &l) in out.as_chunks_mut::<N>().0.iter_mut().zip(left.iter()) {
101 row.fill(l);
102 }
103 }
104 // TM_PRED: X_ij = clamp255(L_i + A_j - P) (RFC Section 12.2 `TMpred`).
105 // Unlike DC_PRED this mode does use the 127/129 out-of-bounds fills.
106 3 => {
107 for (row, &l) in out.as_chunks_mut::<N>().0.iter_mut().zip(left.iter()) {
108 for (o, &a) in row.iter_mut().zip(above.iter()) {
109 *o = clamp255(i32::from(l) + i32::from(a) - i32::from(above_left));
110 }
111 }
112 }
113 _ => {
114 return Err(crate::Error::corrupt(format!(
115 "intra: predict_block mode {mode} (expected 0..=3)"
116 )));
117 }
118 }
119 Ok(())
120}
121
122/// 16x16 luma prediction (RFC 6386 Section 12.3; predictor math per
123/// Section 12.2).
124///
125/// `mode` is a u8 in `{0=DC, 1=V, 2=H, 3=TM}`. `above` holds the 16
126/// reconstructed pixels of the row above the macroblock, `left` the 16 pixels
127/// of the column to its left (adjacent entries separated by one frame row),
128/// `above_left` is the corner pixel `P`. `have_above`/`have_left` state
129/// whether those edges are genuinely in-frame.
130///
131/// For DC the flags alone decide what is averaged (the out-of-bounds fills
132/// are NOT used); V/H/TM consume the caller-prepared out-of-bounds fills
133/// (127 above/`above_left` on the top frame row, 129 on the left frame
134/// column) written into `above`/`left`/`above_left`.
135///
136/// `out` receives 256 row-major prediction pixels.
137pub fn predict16(
138 mode: u8,
139 have_above: bool,
140 have_left: bool,
141 above: &[u8; 16],
142 left: &[u8; 16],
143 above_left: u8,
144 out: &mut [u8; 256],
145) -> crate::Result<()> {
146 predict_block::<16>(mode, have_above, have_left, above, left, above_left, out)
147}
148
149/// 8x8 chroma prediction for one plane (RFC 6386 Section 12.2).
150///
151/// Identical semantics to [`predict16`] with 8-pixel edges; call it once for
152/// U and once for V. `mode` is a u8 in `{0=DC, 1=V, 2=H, 3=TM}`. DC averages
153/// 16 pixels (both edges: `(sumA + sumL + 8) >> 4`) or 8 pixels (one edge:
154/// `(sum + 4) >> 3`), or fills 128.
155///
156/// `out` receives 64 row-major prediction pixels.
157pub fn predict8(
158 mode: u8,
159 have_above: bool,
160 have_left: bool,
161 above: &[u8; 8],
162 left: &[u8; 8],
163 above_left: u8,
164 out: &mut [u8; 64],
165) -> crate::Result<()> {
166 predict_block::<8>(mode, have_above, have_left, above, left, above_left, out)
167}
168
169/// 4x4 subblock prediction (RFC 6386 Section 12.3 `subblock_intra_predict`).
170///
171/// `mode` is a BMode integer: `0=B_DC 1=B_TM 2=B_VE 3=B_HE 4=B_LD 5=B_RD
172/// 6=B_VR 7=B_VL 8=B_HD 9=B_HU`. `above` is the 8-pixel row `A[0..8]`
173/// (4 above + 4 above-right; see the [module docs](self) for the caller-side
174/// synthesis rule), `left` is `L[0..4]`, `above_left` is `P = A[-1] = L[-1]`.
175///
176/// Both edges are always considered present for subblocks: the caller
177/// pre-fills out-of-bounds pixels (127 above including P, 129 left) before
178/// the call, which is why no availability flags exist here.
179///
180/// `out` receives 16 row-major prediction pixels.
181///
182/// # RFC typo
183///
184/// The RFC listing for `B_HD_PRED` contains the line
185/// `B[2][0] = B[3][2] = svg2p(E + 1)`; `svg2p` is a typo for `avg2p` (there
186/// is no `svg2p` definition; libvpx's `d153_predictor` computes `AVG2(K, J)`
187/// there, confirming the correction implemented here).
188pub fn predict4(
189 mode: u8,
190 above: &[u8; 8],
191 left: &[u8; 4],
192 above_left: u8,
193 out: &mut [u8; 16],
194) -> crate::Result<()> {
195 // The 9 already-constructed edge pixels in RFC scan order:
196 // E[0..4] = L[3], L[2], L[1], L[0]; E[4] = P; E[5..9] = A[0..4].
197 let e: [u8; 9] = [
198 left[3], left[2], left[1], left[0], above_left, above[0], above[1], above[2], above[3],
199 ];
200 // Row-major B[r][c] = b[r * 4 + c].
201 let mut b = [0u8; 16];
202 // avg3p(E + k) = avg3(E[k-1], E[k], E[k+1]); avg2p(E + k) = avg2(E[k], E[k+1]).
203 match mode {
204 // B_DC_PRED: DC value = (sum(A[0..4]) + sum(L[0..4]) + 4) >> 3.
205 0 => {
206 let mut v: u32 = 4;
207 for i in 0..4 {
208 v += u32::from(above[i]) + u32::from(left[i]);
209 }
210 v >>= 3;
211 b.fill(v as u8);
212 }
213 // B_TM_PRED: just like 16x16 TM_PRED (RFC Section 12.3).
214 1 => {
215 for r in 0..4 {
216 for c in 0..4 {
217 b[r * 4 + c] =
218 clamp255(i32::from(left[r]) + i32::from(above[c]) - i32::from(above_left));
219 }
220 }
221 }
222 // B_VE_PRED: all four rows = smoothed top row avg3p(A + c), where
223 // A[-1] = P participates in column 0 (libvpx `vpx_ve_predictor_4x4`).
224 2 => {
225 for c in 0..4usize {
226 let v = if c == 0 {
227 avg3(above_left, above[0], above[1])
228 } else {
229 avg3(above[c - 1], above[c], above[c + 1])
230 };
231 b[c] = v;
232 b[4 + c] = v;
233 b[8 + c] = v;
234 b[12 + c] = v;
235 }
236 }
237 // B_HE_PRED: columns = smoothed left column; bottom row exceptional
238 // because L[4] does not exist: avg3(L[2], L[3], L[3]).
239 3 => {
240 let rows = [
241 avg3(above_left, left[0], left[1]),
242 avg3(left[0], left[1], left[2]),
243 avg3(left[1], left[2], left[3]),
244 avg3(left[2], left[3], left[3]),
245 ];
246 for (r, &v) in rows.iter().enumerate() {
247 b[r * 4..r * 4 + 4].fill(v);
248 }
249 }
250 // B_LD_PRED: southwest (left and down) 45-degree diagonals off the
251 // above row; B[3][3] uses avg3(A[6], A[7], A[7]) because A[8] does
252 // not exist (libvpx `d45e_predictor_4x4`).
253 4 => {
254 b[0] = avg3(above[0], above[1], above[2]);
255 b[1] = avg3(above[1], above[2], above[3]);
256 b[4] = b[1];
257 b[2] = avg3(above[2], above[3], above[4]);
258 b[5] = b[2];
259 b[8] = b[2];
260 b[3] = avg3(above[3], above[4], above[5]);
261 b[6] = b[3];
262 b[9] = b[3];
263 b[12] = b[3];
264 b[7] = avg3(above[4], above[5], above[6]);
265 b[10] = b[7];
266 b[13] = b[7];
267 b[11] = avg3(above[5], above[6], above[7]);
268 b[14] = b[11];
269 b[15] = avg3(above[6], above[7], above[7]);
270 }
271 // B_RD_PRED: southeast 45-degree diagonals off the whole E edge
272 // (libvpx `d135_predictor_4x4`).
273 5 => {
274 b[12] = avg3(e[0], e[1], e[2]);
275 b[13] = avg3(e[1], e[2], e[3]);
276 b[8] = b[13];
277 b[14] = avg3(e[2], e[3], e[4]);
278 b[9] = b[14];
279 b[4] = b[14];
280 b[15] = avg3(e[3], e[4], e[5]);
281 b[10] = b[15];
282 b[5] = b[15];
283 b[0] = b[15];
284 b[11] = avg3(e[4], e[5], e[6]);
285 b[6] = b[11];
286 b[1] = b[11];
287 b[7] = avg3(e[5], e[6], e[7]);
288 b[2] = b[7];
289 b[3] = avg3(e[6], e[7], e[8]);
290 }
291 // B_VR_PRED: vertical-right diagonals mixing avg3/avg2 along E
292 // (libvpx `d117_predictor_4x4`).
293 6 => {
294 b[12] = avg3(e[1], e[2], e[3]);
295 b[8] = avg3(e[2], e[3], e[4]);
296 b[13] = avg3(e[3], e[4], e[5]);
297 b[4] = b[13];
298 b[9] = avg2(e[4], e[5]);
299 b[0] = b[9];
300 b[14] = avg3(e[4], e[5], e[6]);
301 b[5] = b[14];
302 b[10] = avg2(e[5], e[6]);
303 b[1] = b[10];
304 b[15] = avg3(e[5], e[6], e[7]);
305 b[6] = b[15];
306 b[11] = avg2(e[6], e[7]);
307 b[2] = b[11];
308 b[7] = avg3(e[6], e[7], e[8]);
309 b[3] = avg2(e[7], e[8]);
310 }
311 // B_VL_PRED: vertical-left diagonals off the above row; the last two
312 // values do not strictly follow the pattern (RFC Section 12.3;
313 // libvpx `d63e_predictor_4x4`).
314 7 => {
315 b[0] = avg2(above[0], above[1]);
316 b[4] = avg3(above[0], above[1], above[2]);
317 b[8] = avg2(above[1], above[2]);
318 b[1] = b[8];
319 b[5] = avg3(above[1], above[2], above[3]);
320 b[12] = b[5];
321 b[9] = avg2(above[2], above[3]);
322 b[2] = b[9];
323 b[13] = avg3(above[2], above[3], above[4]);
324 b[6] = b[13];
325 b[10] = avg2(above[3], above[4]);
326 b[3] = b[10];
327 b[14] = avg3(above[3], above[4], above[5]);
328 b[7] = b[14];
329 b[11] = avg3(above[4], above[5], above[6]);
330 b[15] = avg3(above[5], above[6], above[7]);
331 }
332 // B_HD_PRED: horizontal-down diagonals; the RFC's `svg2p(E + 1)` is
333 // the avg2p correction noted in this function's docs (libvpx
334 // `d153_predictor_4x4`).
335 8 => {
336 b[12] = avg2(e[0], e[1]);
337 b[13] = avg3(e[0], e[1], e[2]);
338 b[8] = avg2(e[1], e[2]); // corrected svg2p -> avg2p line
339 b[14] = b[8];
340 b[9] = avg3(e[1], e[2], e[3]);
341 b[15] = b[9];
342 b[10] = avg2(e[2], e[3]);
343 b[4] = b[10];
344 b[11] = avg3(e[2], e[3], e[4]);
345 b[5] = b[11];
346 b[6] = avg2(e[3], e[4]);
347 b[0] = b[6];
348 b[7] = avg3(e[3], e[4], e[5]);
349 b[1] = b[7];
350 b[2] = avg3(e[4], e[5], e[6]);
351 b[3] = avg3(e[5], e[6], e[7]);
352 }
353 // B_HU_PRED: horizontal-up diagonals off the left column; the bottom
354 // rows cannot follow the pattern and collapse to L[3] (RFC
355 // Section 12.3; libvpx `d207_predictor_4x4`).
356 9 => {
357 b[0] = avg2(left[0], left[1]);
358 b[1] = avg3(left[0], left[1], left[2]);
359 b[2] = avg2(left[1], left[2]);
360 b[4] = b[2];
361 b[3] = avg3(left[1], left[2], left[3]);
362 b[5] = b[3];
363 b[6] = avg2(left[2], left[3]);
364 b[8] = b[6];
365 b[7] = avg3(left[2], left[3], left[3]);
366 b[9] = b[7];
367 let v = left[3];
368 b[10] = v;
369 b[11] = v;
370 b[12] = v;
371 b[13] = v;
372 b[14] = v;
373 b[15] = v;
374 }
375 _ => {
376 return Err(crate::Error::corrupt(format!(
377 "intra: predict4 mode {mode} (expected 0..=9)"
378 )));
379 }
380 }
381 out.copy_from_slice(&b);
382 Ok(())
383}
384
385#[cfg(test)]
386mod tests {
387 use super::*;
388
389 // ---------------------------------------------------------------- --
390 // Reference model used to derive the expected vectors below. It is a
391 // verbatim Python transcription of RFC 6386 Section 12.3
392 // subblock_intra_predict (with svg2p corrected to avg2p); outputs were
393 // generated with it and then hand-spot-checked:
394 //
395 // def avg2(x, y): return (x + y + 1) >> 1
396 // def avg3(x, y, z): return (x + 2*y + z + 2) >> 2
397 // def clamp255(v): return max(0, min(255, v))
398 // # A = A[0..8], L = L[0..4], P = above-left
399 // E = [L[3], L[2], L[1], L[0], P, A[0], A[1], A[2], A[3]]
400 // B_DC: v = (4 + sum(A[0:4]) + sum(L[0:4])) >> 3
401 // B_TM: B[r][c] = clamp255(L[r] + A[c] - P)
402 // B_VE: col c: avg3(P, A0, A1) then avg3(A[c-1], A[c], A[c+1])
403 // B_HE: rows avg3(P, L0, L1), avg3(L0, L1, L2), avg3(L1, L2, L3),
404 // avg3(L2, L3, L3)
405 // B_LD: B[0][0]=avg3(A0,A1,A2); B[0][1]=B[1][0]=avg3(A1,A2,A3); ...
406 // B[3][3]=avg3(A6,A7,A7)
407 // B_RD: B[3][0]=avg3(E0,E1,E2); B[3][1]=B[2][0]=avg3(E1,E2,E3);
408 // B[3][2]=B[2][1]=B[1][0]=avg3(E2,E3,E4);
409 // diag=avg3(E3,E4,E5); ...; B[0][3]=avg3(E6,E7,E8)
410 // B_VR: B[3][0]=avg3(E1,E2,E3); B[2][0]=avg3(E2,E3,E4);
411 // B[3][1]=B[1][0]=avg3(E3,E4,E5); B[2][1]=B[0][0]=avg2(E4,E5);
412 // B[3][2]=B[1][1]=avg3(E4,E5,E6); B[2][2]=B[0][1]=avg2(E5,E6);
413 // B[3][3]=B[1][2]=avg3(E5,E6,E7); B[2][3]=B[0][2]=avg2(E6,E7);
414 // B[1][3]=avg3(E6,E7,E8); B[0][3]=avg2(E7,E8)
415 // B_VL: B[0][0]=avg2(A0,A1); B[1][0]=avg3(A0,A1,A2);
416 // B[2][0]=B[0][1]=avg2(A1,A2); B[1][1]=B[3][0]=avg3(A1,A2,A3);
417 // B[2][1]=B[0][2]=avg2(A2,A3); B[3][1]=B[1][2]=avg3(A2,A3,A4);
418 // B[2][2]=B[0][3]=avg2(A3,A4); B[3][2]=B[1][3]=avg3(A3,A4,A5);
419 // B[2][3]=avg3(A4,A5,A6); B[3][3]=avg3(A5,A6,A7)
420 // B_HD: B[3][0]=avg2(E0,E1); B[3][1]=avg3(E0,E1,E2);
421 // B[2][0]=B[3][2]=avg2(E1,E2) <- svg2p typo corrected
422 // B[2][1]=B[3][3]=avg3(E1,E2,E3);
423 // B[2][2]=B[1][0]=avg2(E2,E3);
424 // B[2][3]=B[1][1]=avg3(E2,E3,E4);
425 // B[1][2]=B[0][0]=avg2(E3,E4);
426 // B[1][3]=B[0][1]=avg3(E3,E4,E5);
427 // B[0][2]=avg3(E4,E5,E6); B[0][3]=avg3(E5,E6,E7)
428 // B_HU: B[0][0]=avg2(L0,L1); B[0][1]=avg3(L0,L1,L2);
429 // B[0][2]=B[1][0]=avg2(L1,L2); B[0][3]=B[1][1]=avg3(L1,L2,L3);
430 // B[1][2]=B[2][0]=avg2(L2,L3);
431 // B[1][3]=B[2][1]=avg3(L2,L3,L3);
432 // B[2][2..4]=B[3][0..4]=L3
433 // ---------------------------------------------------------------- --
434
435 /// Fixed edge pattern shared by all ten subblock tests:
436 /// above = [10,20,30,40,50,60,70,80], left = [15,25,35,45], P = 5.
437 const EDGE_A: [u8; 8] = [10, 20, 30, 40, 50, 60, 70, 80];
438 const EDGE_L: [u8; 4] = [15, 25, 35, 45];
439 const EDGE_P: u8 = 5;
440
441 fn assert_pred4(mode: u8, expected: [u8; 16]) {
442 let mut out = [0u8; 16];
443 predict4(mode, &EDGE_A, &EDGE_L, EDGE_P, &mut out).unwrap();
444 assert_eq!(out, expected, "predict4 mode {mode}");
445 }
446
447 #[test]
448 fn subblock_dc_constant() {
449 // v = (4 + (10+20+30+40) + (15+25+35+45)) >> 3 = (4+100+120)>>3 = 28.
450 assert_pred4(0, [28; 16]);
451 }
452
453 #[test]
454 fn subblock_tm_gradient() {
455 // B[r][c] = L[r] + A[c] - P = L[r] + A[c] - 5.
456 assert_pred4(
457 1,
458 [
459 20, 30, 40, 50, // row 0: 15 + A[c] - 5
460 30, 40, 50, 60, // row 1: 25 + A[c] - 5
461 40, 50, 60, 70, // row 2
462 50, 60, 70, 80, // row 3
463 ],
464 );
465 }
466
467 #[test]
468 fn subblock_ve_smoothed_row() {
469 // Row = avg3(A[c-1], A[c], A[c+1]) with A[-1] = P:
470 // avg3(5,10,20)=11, avg3(10,20,30)=20, avg3(20,30,40)=30,
471 // avg3(30,40,50)=40.
472 let row = [11, 20, 30, 40];
473 let mut expected = [0u8; 16];
474 for r in 0..4 {
475 expected[r * 4..r * 4 + 4].copy_from_slice(&row);
476 }
477 assert_pred4(2, expected);
478 }
479
480 #[test]
481 fn subblock_he_smoothed_column() {
482 // Rows: avg3(5,15,25)=15, avg3(15,25,35)=25, avg3(25,35,45)=35,
483 // avg3(35,45,45)=43 (bottom-row exception, L[4] absent).
484 assert_pred4(
485 3,
486 [
487 15, 15, 15, 15, 25, 25, 25, 25, 35, 35, 35, 35, 43, 43, 43, 43,
488 ],
489 );
490 }
491
492 #[test]
493 fn subblock_ld_downright_diagonal() {
494 // B[3][3] = avg3(70,80,80) = (70 + 160 + 80 + 2) >> 2 = 78 (A[8]
495 // does not exist).
496 assert_pred4(
497 4,
498 [
499 20, 30, 40, 50, // avg3(10,20,30)=20, avg3(20,30,40)=30, ...
500 30, 40, 50, 60, //
501 40, 50, 60, 70, //
502 50, 60, 70, 78, //
503 ],
504 );
505 }
506
507 #[test]
508 fn subblock_rd_downright_diagonal() {
509 // E = [45,35,25,15,5,10,20,30,40].
510 // B[3][0] = avg3(45,35,25) = 35; B[3][1]=B[2][0] = avg3(35,25,15) = 25;
511 // B[3][2]=B[2][1]=B[1][0] = avg3(25,15,5) = 15;
512 // main diagonal = avg3(15,5,10) = 9; B[2][3]=B[1][2]=B[0][1] =
513 // avg3(5,10,20) = 11; B[1][3]=B[0][2] = avg3(10,20,30) = 20;
514 // B[0][3] = avg3(20,30,40) = 30.
515 assert_pred4(
516 5,
517 [
518 9, 11, 20, 30, //
519 15, 9, 11, 20, //
520 25, 15, 9, 11, //
521 35, 25, 15, 9, //
522 ],
523 );
524 }
525
526 #[test]
527 fn subblock_vr_mixed_diagonal() {
528 // E = [45,35,25,15,5,10,20,30,40].
529 // B[0][0] = avg2(E4,E5) = avg2(5,10) = 8;
530 // B[0][1] = avg2(10,20) = 15; B[0][2] = avg2(20,30) = 25;
531 // B[0][3] = avg2(30,40) = 35; B[1][0] = avg3(15,5,10) = 9;
532 // B[1][1] = avg3(5,10,20) = 11; B[1][2] = avg3(10,20,30) = 20;
533 // B[1][3] = avg3(20,30,40) = 30; B[2][0] = avg3(25,15,5) = 15;
534 // B[2][1] = 8; B[2][2] = 15; B[2][3] = 25;
535 // B[3][0] = avg3(35,25,15) = 25; B[3][1] = 9; B[3][2] = 11; B[3][3] = 20.
536 assert_pred4(
537 6,
538 [
539 8, 15, 25, 35, //
540 9, 11, 20, 30, //
541 15, 8, 15, 25, //
542 25, 9, 11, 20, //
543 ],
544 );
545 }
546
547 #[test]
548 fn subblock_vl_downleft_diagonal() {
549 // avg2/avg3 pairs along the above row:
550 // cols: avg2(10,20)=15, avg2(20,30)=25, avg2(30,40)=35, avg2(40,50)=45;
551 // rows shift by avg3: 20, 30, 40, 50; last column 45/50/60/70 with
552 // B[2][3]=avg3(50,60,70)=60, B[3][3]=avg3(60,70,80)=70.
553 assert_pred4(
554 7,
555 [
556 15, 25, 35, 45, //
557 20, 30, 40, 50, //
558 25, 35, 45, 60, //
559 30, 40, 50, 70, //
560 ],
561 );
562 }
563
564 #[test]
565 fn subblock_hd_uses_avg2p_for_svg2p_typo_line() {
566 // E = [45,35,25,15,5,10,20,30,40].
567 // The RFC's `svg2p(E + 1)` line: B[2][0] = B[3][2] = avg2p(E+1) =
568 // avg2(E[1], E[2]) = avg2(35,25) = 30. A mis-transcription (any
569 // avg3-style or E-shifted variant) would not yield 30 here.
570 // B[3][0] = avg2(45,35) = 40; B[3][1] = avg3(45,35,25) = 35;
571 // B[2][1]=B[3][3] = avg3(35,25,15) = 25; B[2][2]=B[1][0] =
572 // avg2(25,15) = 20; B[2][3]=B[1][1] = avg3(25,15,5) = 15;
573 // B[1][2]=B[0][0] = avg2(15,5) = 10; B[1][3]=B[0][1] =
574 // avg3(15,5,10) = 9; B[0][2] = avg3(5,10,20) = 11;
575 // B[0][3] = avg3(10,20,30) = 20.
576 assert_pred4(
577 8,
578 [
579 10, 9, 11, 20, //
580 20, 15, 10, 9, //
581 30, 25, 20, 15, //
582 40, 35, 30, 25, //
583 ],
584 );
585 // Directly pin the corrected-typo cells.
586 let mut out = [0u8; 16];
587 predict4(8, &EDGE_A, &EDGE_L, EDGE_P, &mut out).unwrap();
588 assert_eq!(
589 (out[8], out[14]),
590 (30, 30),
591 "B[2][0] and B[3][2] = avg2(E1,E2)"
592 );
593 }
594
595 #[test]
596 fn subblock_hu_up_diagonal() {
597 // B[0][0] = avg2(15,25) = 20; B[0][1] = avg3(15,25,35) = 25;
598 // B[0][2]=B[1][0] = avg2(25,35) = 30; B[0][3]=B[1][1] =
599 // avg3(25,35,45) = 35; B[1][2]=B[2][0] = avg2(35,45) = 40;
600 // B[1][3]=B[2][1] = avg3(35,45,45) = 43; bottom rows = L[3] = 45.
601 assert_pred4(
602 9,
603 [
604 20, 25, 30, 35, //
605 30, 35, 40, 43, //
606 40, 43, 45, 45, //
607 45, 45, 45, 45, //
608 ],
609 );
610 }
611
612 #[test]
613 fn dc16_all_availability_cases() {
614 // above = [10; 16] (sum 160), left = [20; 16] (sum 320).
615 let above = [10u8; 16];
616 let left = [20u8; 16];
617 let mut out = [0u8; 256];
618
619 // Both edges: (160 + 320 + 16) >> 5 = 15.
620 predict16(0, true, true, &above, &left, 5, &mut out).unwrap();
621 assert_eq!(out, [15; 256]);
622
623 // Neither edge (top-left macroblock): constant 128. The array
624 // contents are irrelevant here; availability flags decide.
625 predict16(0, false, false, &above, &left, 5, &mut out).unwrap();
626 assert_eq!(out, [128; 256]);
627
628 // Above only (left frame column): (160 + 8) >> 4 = 10.
629 predict16(0, true, false, &above, &left, 5, &mut out).unwrap();
630 assert_eq!(out, [10; 256]);
631
632 // Left only (top frame row): (320 + 8) >> 4 = 20.
633 predict16(0, false, true, &above, &left, 5, &mut out).unwrap();
634 assert_eq!(out, [20; 256]);
635 }
636
637 #[test]
638 fn dc8_all_availability_cases() {
639 // above = [10; 8] (sum 80), left = [20; 8] (sum 160).
640 let above = [10u8; 8];
641 let left = [20u8; 8];
642 let mut out = [0u8; 64];
643
644 // Both: (80 + 160 + 8) >> 4 = 15.
645 predict8(0, true, true, &above, &left, 5, &mut out).unwrap();
646 assert_eq!(out, [15; 64]);
647
648 // Neither: 128.
649 predict8(0, false, false, &above, &left, 5, &mut out).unwrap();
650 assert_eq!(out, [128; 64]);
651
652 // Above only: (80 + 4) >> 3 = 10.
653 predict8(0, true, false, &above, &left, 5, &mut out).unwrap();
654 assert_eq!(out, [10; 64]);
655
656 // Left only: (160 + 4) >> 3 = 20.
657 predict8(0, false, true, &above, &left, 5, &mut out).unwrap();
658 assert_eq!(out, [20; 64]);
659 }
660
661 #[test]
662 fn tm16_clamps_both_directions() {
663 let mut out = [0u8; 256];
664
665 // Negative difference clamps to 0: P high (250), L and A low (0)
666 // gives L + A - P = -250 everywhere.
667 predict16(3, true, true, &[0u8; 16], &[0u8; 16], 250, &mut out).unwrap();
668 assert_eq!(out, [0; 256]);
669
670 // Overflow clamps to 255: P = 0, L = A = 255 gives 510 everywhere.
671 predict16(3, true, true, &[255u8; 16], &[255u8; 16], 0, &mut out).unwrap();
672 assert_eq!(out, [255; 256]);
673
674 // Exact in-range gradient: out[r][c] = 200 + c - 50 = 150 + c,
675 // no clamping, verifies arithmetic (not just the clamps).
676 let above: [u8; 16] = core::array::from_fn(|i| i as u8);
677 predict16(3, true, true, &above, &[200u8; 16], 50, &mut out).unwrap();
678 for r in 0..16 {
679 let row: [u8; 16] = core::array::from_fn(|c| (150 + c) as u8);
680 assert_eq!(out[r * 16..r * 16 + 16], row, "TM16 row {r}");
681 }
682 }
683
684 #[test]
685 fn tm4_clamps_via_predict4() {
686 // Same clamp contract at 4x4 (RFC B_TM_PRED == clamp255(L + A - P)).
687 let mut out = [0u8; 16];
688 predict4(1, &[0u8; 8], &[0u8; 4], 250, &mut out).unwrap();
689 assert_eq!(out, [0; 16]);
690 predict4(1, &[255u8; 8], &[255u8; 4], 0, &mut out).unwrap();
691 assert_eq!(out, [255; 16]);
692 }
693
694 #[test]
695 fn v_and_h_copy_edges() {
696 // V_PRED: every 16x16 row equals the above row.
697 let above: [u8; 16] = core::array::from_fn(|i| (i * 7 + 3) as u8);
698 let mut out = [0u8; 256];
699 predict16(1, true, true, &above, &[0u8; 16], 0, &mut out).unwrap();
700 for row in out.as_chunks::<16>().0.iter() {
701 assert_eq!(*row, above);
702 }
703
704 // H_PRED: every 8x8 row is the constant left[r].
705 let left: [u8; 8] = core::array::from_fn(|i| (i * 11 + 1) as u8);
706 let mut out8 = [0u8; 64];
707 predict8(2, true, true, &[0u8; 8], &left, 0, &mut out8).unwrap();
708 for (r, row) in out8.as_chunks::<8>().0.iter().enumerate() {
709 assert_eq!(*row, [left[r]; 8]);
710 }
711 }
712
713 #[test]
714 fn predict16_rejects_invalid_mode() {
715 let mut out = [0u8; 256];
716 assert!(predict16(4, true, true, &[0u8; 16], &[0u8; 16], 0, &mut out).is_err());
717 }
718
719 #[test]
720 fn predict4_rejects_invalid_mode() {
721 let mut out = [0u8; 16];
722 assert!(predict4(10, &[0u8; 8], &[0u8; 4], 0, &mut out).is_err());
723 }
724}