diff --git a/crates/coreav-kernels-h264/src/intra.rs b/crates/coreav-kernels-h264/src/intra.rs index 3ae2e9a05..015731a5d 100644 --- a/crates/coreav-kernels-h264/src/intra.rs +++ b/crates/coreav-kernels-h264/src/intra.rs @@ -133,20 +133,146 @@ pub(crate) fn pred4x4(v: &mut IntraMut<'_>, mode: u8) { if v.n() != 4 { return; } - let nb = neighbours(v, 8); - let mut block = [0u8; 16]; - match mode { - 0 => copy_rows(&mut block, &nb.top, 4), - 1 => fill_rows(&mut block, &nb.left, 4), - 3..=8 => { - let table = SOURCES_4X4.get(usize::from(mode)).unwrap_or(&[0; 16]); - directional::<4>(&nb, table, &mut block); + let top = v.top(); + let last = top.get(3).copied().unwrap_or(0); + let top: [u16; 8] = core::array::from_fn(|p| u16::from(top.get(p).copied().unwrap_or(last))); + let left: [u16; 4] = core::array::from_fn(|p| u16::from(v.left(p))); + let [a, b, c, d, e, f, g, _] = top; + let [i, j, k, l] = left; + let x = u16::from(v.top_left()); + let half = |a: u16, b: u16| ((a + b + 1) >> 1) as u8; + let quarter = |a: u16, b: u16, c: u16| ((a + 2 * b + c + 2) >> 2) as u8; + let block = match mode { + 0 => [a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d].map(|v| v as u8), + 1 => [i, i, i, i, j, j, j, j, k, k, k, k, l, l, l, l].map(|v| v as u8), + 3 => directional4::<3>(&top, &left, x), + 4 => directional4::<4>(&top, &left, x), + 5 => directional4::<5>(&top, &left, x), + 6 => directional4::<6>(&top, &left, x), + 7 => { + let p0 = half(a, b); + let p1 = half(b, c); + let p2 = half(c, d); + let p3 = half(d, e); + let p4 = half(e, f); + let p5 = quarter(a, b, c); + let p6 = quarter(b, c, d); + let p7 = quarter(c, d, e); + let p8 = quarter(d, e, f); + let p9 = quarter(e, f, g); + [ + p0, p1, p2, p3, p5, p6, p7, p8, p1, p2, p3, p4, p6, p7, p8, p9, + ] } - _ => block.fill(dc(&nb, 4) as u8), - } + 8 => { + let p0 = half(i, j); + let p1 = quarter(i, j, k); + let p2 = half(j, k); + let p3 = quarter(j, k, l); + let p4 = half(k, l); + let p5 = quarter(k, l, l); + let p6 = l as u8; + [ + p0, p1, p2, p3, p2, p3, p4, p5, p4, p5, p6, p6, p6, p6, p6, p6, + ] + } + _ => { + let dc = match (v.avail().top, v.avail().left) { + (true, true) => (a + b + c + d + i + j + k + l + 4) >> 3, + (true, false) => (a + b + c + d + 2) >> 2, + (false, true) => (i + j + k + l + 2) >> 2, + (false, false) => 128, + }; + [dc as u8; 16] + } + }; write_block(v, &block); } +#[inline(always)] +fn directional4(top: &[u16; 8], left: &[u16; 4], top_left: u16) -> [u8; 16] { + let p = |x: i32, y: i32| { + if y < 0 { + if x < 0 { + top_left + } else { + top.get(x as usize).copied().unwrap_or(0) + } + } else { + left.get(y as usize).copied().unwrap_or(0) + } + }; + [ + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + directional4_sample::(&p), + ] +} + +#[inline(always)] +fn directional4_sample( + p: &impl Fn(i32, i32) -> u16, +) -> u8 { + let sample = match MODE { + 3 => { + if X == 3 && Y == 3 { + (p(6, -1) + 3 * p(7, -1) + 2) >> 2 + } else { + (p(X + Y, -1) + 2 * p(X + Y + 1, -1) + p(X + Y + 2, -1) + 2) >> 2 + } + } + 4 => { + if X > Y { + (p(X - Y - 2, -1) + 2 * p(X - Y - 1, -1) + p(X - Y, -1) + 2) >> 2 + } else if X < Y { + (p(-1, Y - X - 2) + 2 * p(-1, Y - X - 1) + p(-1, Y - X) + 2) >> 2 + } else { + (p(0, -1) + 2 * p(-1, -1) + p(-1, 0) + 2) >> 2 + } + } + 5 => { + let z = 2 * X - Y; + if z >= 0 && z % 2 == 0 { + (p(X - (Y >> 1) - 1, -1) + p(X - (Y >> 1), -1) + 1) >> 1 + } else if z >= 0 { + (p(X - (Y >> 1) - 2, -1) + 2 * p(X - (Y >> 1) - 1, -1) + p(X - (Y >> 1), -1) + 2) + >> 2 + } else if z == -1 { + (p(-1, 0) + 2 * p(-1, -1) + p(0, -1) + 2) >> 2 + } else { + (p(-1, Y - 1) + 2 * p(-1, Y - 2) + p(-1, Y - 3) + 2) >> 2 + } + } + _ => { + let z = 2 * Y - X; + if z >= 0 && z % 2 == 0 { + (p(-1, Y - (X >> 1) - 1) + p(-1, Y - (X >> 1)) + 1) >> 1 + } else if z >= 0 { + (p(-1, Y - (X >> 1) - 2) + 2 * p(-1, Y - (X >> 1) - 1) + p(-1, Y - (X >> 1)) + 2) + >> 2 + } else if z == -1 { + (p(-1, 0) + 2 * p(-1, -1) + p(0, -1) + 2) >> 2 + } else { + (p(X - 1, -1) + 2 * p(X - 2, -1) + p(X - 3, -1) + 2) >> 2 + } + } + }; + sample as u8 +} + /// The length of the edge line of an `n` by `n` block: `n` samples left of /// it, the one above and left, and `2n` above. pub(crate) const fn line_len(n: usize) -> usize { @@ -257,7 +383,6 @@ pub(crate) const fn sources() -> [[u8; S]; 9] { out } -static SOURCES_4X4: [[u8; 16]; 9] = sources::<4, 16>(); static SOURCES_8X8: [[u8; 64]; 9] = sources::<8, 64>(); /// A directional prediction of an `N` by `N` block from its neighbours diff --git a/docs/design/h264-intra4-prediction.md b/docs/design/h264-intra4-prediction.md new file mode 100644 index 000000000..8dfda15d4 --- /dev/null +++ b/docs/design/h264-intra4-prediction.md @@ -0,0 +1,11 @@ +# Fixed-size H.264 intra prediction + +The encoder repeatedly calls the shared safe `pred4x4` kernel. Its generic directional path builds neighbours sized for larger blocks and computes every pair and three-tap average before selecting sixteen samples. The exact function's exclusive instruction count on native x86 Actions clears the useful-work ceiling. This change keeps the prediction policy and admitted views unchanged. + +Read eight top samples, four left samples and the top-left sample before writing the block. Preserve the checked view's zero values for missing neighbours and replication of the last available top sample. Evaluate only the averages each directional mode uses, with unsigned 16-bit intermediates. Their maximum rounded numerator is 1,022, so no intermediate can overflow. For modes three through six, instantiate the coordinate equations at fixed mode and sample coordinates. The compiler folds their conditions and shares equal results. The other directional modes retain their fixed output arrangements. Vertical, horizontal and DC modes keep their existing arithmetic. Modes above eight still select DC, and another block size stays untouched. + +This remains safe scalar Rust shared by the decoder and encoder. The frozen test oracle stays unchanged. The 8x8, 16x16, chroma and vector predictors, encoder mode decisions, search, coefficient refinement and buffer ownership are outside the change. No new unsafe code, dispatch policy, public setting or default dependency is needed. + +The first independently written expansion triggered similarity findings in five directional modes. Four used mathematically equivalent commuted endpoint terms or repeated endpoint taps rather than the specification's literal expressions. Those modes were rewritten from H.264 clauses 8.3.1.2.4 through 8.3.1.2.7, Equations 8-52 through 8-64, using compile-time coordinates rather than cached output arrangements. They are not proposed for allowlisting. The unchanged vertical-left averages reproduce clause 8.3.1.2.8, Equations 8-65 and 8-66, and require a separately reviewed, exact-span allowlist entry before this change can ship. No other implementation's source was read. + +Run the existing independent prediction comparisons, both encoder build modes, kernel and encoder Clippy, complete-stream identity and affected conformance CI. Compare main and candidate at explicit source commits on the existing prepared inputs at QP22 and QP28. Require identical streams and reconstructions at one, two, three and eight workers in normal and scalar builds. Native x86 Actions timing uses one and four workers, alternating arms with warmups and retained observations. Ship only a gain above run-to-run variation. A counted function share alone is not a speed result.