From e31ef14c3aa8a6ffe3940389c89bec86690e2f9a Mon Sep 17 00:00:00 2001 From: Gunwoo Kim Date: Sun, 8 Nov 2026 13:10:15 +0900 Subject: [PATCH 1/3] docs(h264): plan fixed-size intra prediction Signed-off-by: Gunwoo Kim --- docs/design/h264-intra4-prediction.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 docs/design/h264-intra4-prediction.md diff --git a/docs/design/h264-intra4-prediction.md b/docs/design/h264-intra4-prediction.md new file mode 100644 index 000000000..d5a8ea6de --- /dev/null +++ b/docs/design/h264-intra4-prediction.md @@ -0,0 +1,9 @@ +# 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 of the six directional modes uses, with unsigned 16-bit intermediates. Their maximum rounded numerator is 1,022, so no intermediate can overflow. Arrange the sixteen results directly according to H.264 clauses 8.3.1.2.4 through 8.3.1.2.9. 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. + +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. From 22345dbbb61ec82dd58cc8e00230625d8eb571a4 Mon Sep 17 00:00:00 2001 From: Gunwoo Kim Date: Sun, 8 Nov 2026 13:13:13 +0900 Subject: [PATCH 2/3] perf(h264): specialize four-by-four prediction Signed-off-by: Gunwoo Kim --- crates/coreav-kernels-h264/src/intra.rs | 112 +++++++++++++++++++++--- 1 file changed, 101 insertions(+), 11 deletions(-) diff --git a/crates/coreav-kernels-h264/src/intra.rs b/crates/coreav-kernels-h264/src/intra.rs index 3ae2e9a05..51f44b7a5 100644 --- a/crates/coreav-kernels-h264/src/intra.rs +++ b/crates/coreav-kernels-h264/src/intra.rs @@ -133,17 +133,108 @@ 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 [a, b, c, d, e, f, g, h]: [u16; 8] = + core::array::from_fn(|p| u16::from(top.get(p).copied().unwrap_or(last))); + let [i, j, k, l]: [u16; 4] = core::array::from_fn(|p| u16::from(v.left(p))); + 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 => { + let p0 = quarter(a, b, c); + let p1 = quarter(b, c, d); + let p2 = quarter(c, d, e); + let p3 = quarter(d, e, f); + let p4 = quarter(e, f, g); + let p5 = quarter(f, g, h); + let p6 = quarter(g, h, h); + [ + p0, p1, p2, p3, p1, p2, p3, p4, p2, p3, p4, p5, p3, p4, p5, p6, + ] } - _ => block.fill(dc(&nb, 4) as u8), - } + 4 => { + let p0 = quarter(i, x, a); + let p1 = quarter(x, a, b); + let p2 = quarter(a, b, c); + let p3 = quarter(b, c, d); + let p4 = quarter(x, i, j); + let p5 = quarter(i, j, k); + let p6 = quarter(j, k, l); + [ + p0, p1, p2, p3, p4, p0, p1, p2, p5, p4, p0, p1, p6, p5, p4, p0, + ] + } + 5 => { + let p0 = half(x, a); + let p1 = half(a, b); + let p2 = half(b, c); + let p3 = half(c, d); + let p4 = quarter(i, x, a); + let p5 = quarter(x, a, b); + let p6 = quarter(a, b, c); + let p7 = quarter(b, c, d); + let p8 = quarter(x, i, j); + let p9 = quarter(i, j, k); + [ + p0, p1, p2, p3, p4, p5, p6, p7, p8, p0, p1, p2, p9, p4, p5, p6, + ] + } + 6 => { + let p0 = half(x, i); + let p1 = half(i, j); + let p2 = half(j, k); + let p3 = half(k, l); + let p4 = quarter(a, x, i); + let p5 = quarter(x, i, j); + let p6 = quarter(i, j, k); + let p7 = quarter(j, k, l); + let p8 = quarter(x, a, b); + let p9 = quarter(a, b, c); + [ + p0, p4, p8, p9, p1, p5, p0, p4, p2, p6, p1, p5, p3, p7, p2, p6, + ] + } + 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, + ] + } + 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); } @@ -257,7 +348,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 From 856119bd087596cdd03fc046be2a3c0e61c2c76e Mon Sep 17 00:00:00 2001 From: Gunwoo Kim Date: Sun, 8 Nov 2026 13:49:00 +0900 Subject: [PATCH 3/3] refactor(h264): instantiate directional equations Signed-off-by: Gunwoo Kim --- crates/coreav-kernels-h264/src/intra.rs | 149 +++++++++++++++--------- docs/design/h264-intra4-prediction.md | 4 +- 2 files changed, 95 insertions(+), 58 deletions(-) diff --git a/crates/coreav-kernels-h264/src/intra.rs b/crates/coreav-kernels-h264/src/intra.rs index 51f44b7a5..015731a5d 100644 --- a/crates/coreav-kernels-h264/src/intra.rs +++ b/crates/coreav-kernels-h264/src/intra.rs @@ -135,69 +135,20 @@ pub(crate) fn pred4x4(v: &mut IntraMut<'_>, mode: u8) { } let top = v.top(); let last = top.get(3).copied().unwrap_or(0); - let [a, b, c, d, e, f, g, h]: [u16; 8] = - core::array::from_fn(|p| u16::from(top.get(p).copied().unwrap_or(last))); - let [i, j, k, l]: [u16; 4] = core::array::from_fn(|p| u16::from(v.left(p))); + 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 => { - let p0 = quarter(a, b, c); - let p1 = quarter(b, c, d); - let p2 = quarter(c, d, e); - let p3 = quarter(d, e, f); - let p4 = quarter(e, f, g); - let p5 = quarter(f, g, h); - let p6 = quarter(g, h, h); - [ - p0, p1, p2, p3, p1, p2, p3, p4, p2, p3, p4, p5, p3, p4, p5, p6, - ] - } - 4 => { - let p0 = quarter(i, x, a); - let p1 = quarter(x, a, b); - let p2 = quarter(a, b, c); - let p3 = quarter(b, c, d); - let p4 = quarter(x, i, j); - let p5 = quarter(i, j, k); - let p6 = quarter(j, k, l); - [ - p0, p1, p2, p3, p4, p0, p1, p2, p5, p4, p0, p1, p6, p5, p4, p0, - ] - } - 5 => { - let p0 = half(x, a); - let p1 = half(a, b); - let p2 = half(b, c); - let p3 = half(c, d); - let p4 = quarter(i, x, a); - let p5 = quarter(x, a, b); - let p6 = quarter(a, b, c); - let p7 = quarter(b, c, d); - let p8 = quarter(x, i, j); - let p9 = quarter(i, j, k); - [ - p0, p1, p2, p3, p4, p5, p6, p7, p8, p0, p1, p2, p9, p4, p5, p6, - ] - } - 6 => { - let p0 = half(x, i); - let p1 = half(i, j); - let p2 = half(j, k); - let p3 = half(k, l); - let p4 = quarter(a, x, i); - let p5 = quarter(x, i, j); - let p6 = quarter(i, j, k); - let p7 = quarter(j, k, l); - let p8 = quarter(x, a, b); - let p9 = quarter(a, b, c); - [ - p0, p4, p8, p9, p1, p5, p0, p4, p2, p6, p1, p5, p3, p7, p2, p6, - ] - } + 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); @@ -238,6 +189,90 @@ pub(crate) fn pred4x4(v: &mut IntraMut<'_>, mode: u8) { 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 { diff --git a/docs/design/h264-intra4-prediction.md b/docs/design/h264-intra4-prediction.md index d5a8ea6de..8dfda15d4 100644 --- a/docs/design/h264-intra4-prediction.md +++ b/docs/design/h264-intra4-prediction.md @@ -2,8 +2,10 @@ 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 of the six directional modes uses, with unsigned 16-bit intermediates. Their maximum rounded numerator is 1,022, so no intermediate can overflow. Arrange the sixteen results directly according to H.264 clauses 8.3.1.2.4 through 8.3.1.2.9. Vertical, horizontal and DC modes keep their existing arithmetic. Modes above eight still select DC, and another block size stays untouched. +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.