Files
daedalus-fourier/tests/vp9_lpf_ref.c
T
marfrit be7ff5587c Cycle 2 (deblocking) Phase 1-3: M3'' = 48.285 Medge/s baseline
Second kernel candidate per phase7_M4.md verdict "next-kernel cycle
authorised". VP9 4-tap inner loop filter, horizontal direction,
8-pixel edge (libavcodec ff_vp9_loop_filter_h_4_8_neon as baseline).
Different workload shape from IDCT - boundary streaming, lighter
compute per unit, per-row conditionals - tests whether QPU win
generalises.

docs/k2_deblock_phase1.md - goal-setting. Same R-band decision rules
as cycle 1 (phase1.md), with the cycle-1 calibration adjustment:
ORANGE band is no longer auto-close because M4 showed mixed > pure
CPU even at modest R when CPU bandwidth-saturates.

docs/k2_deblock_phase2.md - situation analysis. C reference already
in vendored snapshot (vp9dsp_template.c:1780-1898). Fetched
vp9lpf_neon.S fresh (1334 lines, LGPL-2.1+, FFmpeg n7.1.3 pin,
SHA-256 384e49e7...). PROVENANCE.md updated.

docs/k2_deblock_phase3.md - NEON baseline:

  M1''_c bit-exact     100.0000 % (10000 random edges)
  M3'' throughput      48.285 Medge/s  (20.7 ns/edge, single A76)
  per-frame 1080p-eq   748 FPS (worst case 64 530 edges/frame)
  cycles/edge          ~58 (=20.7ns x 2.8GHz), ~7 cycles/row

LPF is 5.9x faster per-unit than IDCT M3 (20.7 vs 122 ns), so the
QPU break-even point moves down. Predicted R''_v1 band ~0.5-0.9
- frame-level batching amortises the same 33us dispatch overhead;
workload becomes bandwidth-bound rather than compute-bound
(~5.7 MB/frame traffic at 64 530 edges x ~88 B per edge).

New artifacts:
- tests/vp9_lpf_ref.c    - standalone bit-exact C ref (8-bit, wd=4
                           inner only; clean transcription)
- tests/bench_neon_lpf.c - M1''_c gate + M3'' time-based bench
                           (5s window, edge-content-biased RNG for
                           realistic fm/hev hit rates)
- external/ffmpeg-snapshot/libavcodec/aarch64/vp9lpf_neon.S
- CMakeLists.txt updated with bench_neon_lpf target

Phase 4 next: plan the QPU LPF compute shader. Cycle 1's phase4.md
+ phase5.md + phase7.md learnings apply directly - bake in the v4
winning patterns from the start (WG=256, edges-per-subgroup
pattern adapted from blocks, uint8_t dst SSBO, oob flag, unrolled
writes).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 12:28:57 +00:00

82 lines
2.8 KiB
C

/*
* Standalone bit-exact C reference for VP9 4-tap inner loop filter
* (horizontal, 8-pixel edge), transcribed from FFmpeg's
* libavcodec/vp9dsp_template.c loop_filter() function (vendored at
* external/ffmpeg-snapshot/, commit f46e514). 8-bit pixels only.
*
* Provided as a self-contained translation unit so the harness
* doesn't need to wrestle FFmpeg's BIT_DEPTH-templated macro
* expansion. Cross-checked against the vendored reference at
* runtime (see bench_neon_lpf.c::correctness_check()).
*
* License: LGPL-2.1-or-later (matches upstream reference).
*
* Spec source: VP9 specification §8.8.1 — Loop filter process.
*/
#include <stdint.h>
#include <stddef.h>
static inline int abs_i(int x) { return x < 0 ? -x : x; }
static inline int clip_intp2_7(int x) /* clamp to int7 = [-128, 127] */
{
return x > 127 ? 127 : x < -128 ? -128 : x;
}
static inline uint8_t clip_u8(int x)
{
return (uint8_t)(x > 255 ? 255 : x < 0 ? 0 : x);
}
static inline int min_i(int a, int b) { return a < b ? a : b; }
/*
* Horizontal-direction 4-tap inner loop filter, 8-pixel edge.
*
* stridea = stride (move down rows between iterations)
* strideb = 1 (neighborhood spans columns -4..+3)
*
* Each of the 8 iterations:
* - reads neighborhood [p3 p2 p1 p0 | q0 q1 q2 q3]
* - tests filter mask `fm` — skip iteration if false
* - tests high-edge-variance `hev` — selects 2-pixel vs 4-pixel
* update path
*
* Matches ff_vp9_loop_filter_h_4_8_neon byte-for-byte on 8-bit input.
*/
void daedalus_vp9_loop_filter_h_4_8_ref(uint8_t *dst, ptrdiff_t stride,
int E, int I, int H)
{
for (int i = 0; i < 8; i++, dst += stride) {
int p3 = dst[-4], p2 = dst[-3], p1 = dst[-2], p0 = dst[-1];
int q0 = dst[ 0], q1 = dst[+1], q2 = dst[+2], q3 = dst[+3];
int fm = abs_i(p3 - p2) <= I && abs_i(p2 - p1) <= I &&
abs_i(p1 - p0) <= I && abs_i(q1 - q0) <= I &&
abs_i(q2 - q1) <= I && abs_i(q3 - q2) <= I &&
abs_i(p0 - q0) * 2 + (abs_i(p1 - q1) >> 1) <= E;
if (!fm) continue;
int hev = abs_i(p1 - p0) > H || abs_i(q1 - q0) > H;
if (hev) {
int f = clip_intp2_7(p1 - q1);
f = clip_intp2_7(3 * (q0 - p0) + f);
int f1 = min_i(f + 4, 127) >> 3;
int f2 = min_i(f + 3, 127) >> 3;
dst[-1] = clip_u8(p0 + f2);
dst[ 0] = clip_u8(q0 - f1);
} else {
int f = clip_intp2_7(3 * (q0 - p0));
int f1 = min_i(f + 4, 127) >> 3;
int f2 = min_i(f + 3, 127) >> 3;
dst[-1] = clip_u8(p0 + f2);
dst[ 0] = clip_u8(q0 - f1);
int fp = (f1 + 1) >> 1;
dst[-2] = clip_u8(p1 + fp);
dst[+1] = clip_u8(q1 - fp);
}
}
}