DAEMON-PPS: synthesise H.264 SPS/PPS NAL units from V4L2 controls

libva-v4l2-request-fourier (and any V4L2-stateless-API consumer)
passes H.264 SPS/PPS as separate V4L2_CID_STATELESS_H264_{SPS,PPS}
controls; only the slice NAL goes into the OUTPUT buffer.  This is
correct per the V4L2 stateless contract.  But libavcodec — which
the daedalus daemon uses for actual decode (Option γ) — wants a
self-contained AnnexB stream including SPS+PPS before any slice.
Result on higgs: "non-existing PPS 0 referenced" + decode_slice_
header errors on every H.264 frame, even after LIBVA-1 and -2
routing correctly delivered the request to the daemon.

Fix splits across kernel + daemon, keeping the kernel module as a
thin transport and putting the actual NAL encoding in userspace:

  include/daedalus_v4l2_proto.h:
    Add struct daedalus_h264_meta (the four v4l2_ctrl_h264_*
    structs the kernel collects) and DAEDALUS_REQ_FLAG_H264_META
    (set in req.flags when the meta block is present between the
    daedalus_req_decode prefix and the slice bitstream).

  kernel/daedalus_v4l2_main.c:
    Add daedalus_collect_h264_meta() — reads the H.264 ctrl values
    from the bound media_request via v4l2_ctrl_find +
    ctrl->p_cur.p_h264_*.  device_run() calls it on H.264 codec_id,
    copies the structs into the REQ_DECODE payload between the
    prefix and bitstream, and sets the flag.  Payload size is
    bounds-checked against DAEDALUS_PROTO_MAX_PAYLOAD so an over-
    sized slice + meta fails loud instead of truncating.

  daemon/src/bitstream_writer.{c,h}:
    New module — MSB-first bit packer with H.264 Exp-Golomb ue(v)
    and se(v) coding + rbsp_trailing_bits alignment.  Sticky
    overflow flag so callers can verify the output buffer wasn't
    truncated.

  daemon/src/h264_nal_synth.{c,h}:
    New module — turns v4l2_ctrl_h264_sps / v4l2_ctrl_h264_pps
    into AnnexB-framed NAL units per ITU-T H.264 7.3.2.1 / 7.3.2.2.
    Emits emulation prevention bytes (0x03 after every 00 00 in the
    EBSP) and the 4-byte start code (0x00000001).  Coverage matches
    what V4L2 stateless surface gives us: VUI parameters and full
    scaling matrices are NOT emitted (V4L2 doesn't carry them — the
    seq_scaling_matrix_present_flag is set to 0 and libavcodec uses
    flat defaults, which matches the de-facto behaviour of most
    H.264 streams libva-v4l2-request drives).

  daemon/src/decoder.c:
    daedalus_decoder_run_request() now takes an optional
    h264_meta parameter.  For codec_id == H264 with meta != NULL,
    synthesises SPS+PPS NAL units, allocates a combined
    [SPS][PPS][slice] buffer (+ AV_INPUT_BUFFER_PADDING_SIZE), and
    feeds that to avcodec_send_packet instead of the raw slice.
    VP9/AV1 path unchanged (frames are self-contained).  Cleanup
    now goes through a unified `out:` label so the assembled
    buffer is always freed on every exit (including the existing
    decoder_open_codec / no-frame / receive_frame failure paths).

  daemon/src/chardev_client.c:
    handle_req_decode() peels off the optional meta block when the
    flag is set, passes it through to the decoder, and updates
    the payload-length consistency check (now allows for an extra
    sizeof(daedalus_h264_meta) when the flag is on).

Build (boltzmann aarch64): clean compile of all daemon sources,
including bitstream_writer + h264_nal_synth + the refactored
decoder.c.  Kernel module compile to be verified via DKMS rebuild
on higgs in the marfrit-packages bump that follows.

Test plan: with this commit + a marfrit-packages daedalus pin
bump, higgs's ffmpeg -hwaccel vaapi -i h264_test.mp4 should
produce a successful decode (vs. the previous "non-existing PPS 0
referenced" failure).  The daemon log should show:
  decoder: opened h264 context
  decoder: h264 prepended SPS=NB PPS=MB slice=KB
  decoder: OK 320x240 fmt=0 (yuv420p) fnv1a=0x...

VP9 / AV1 behaviour unchanged — they don't carry meta and the
existing per-frame self-describing path still applies.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-20 17:35:24 +02:00
parent 481279c9bf
commit 8c1d9960c4
10 changed files with 720 additions and 56 deletions
+108 -33
View File
@@ -190,12 +190,14 @@ static const u32 daedalus_stateless_ctrls[] = {
};
/*
* No-op control op set: daemon ignores all stateless control
* values (FFmpeg re-parses the bitstream). But v4l2-core requires
* ops to be present on a ctrl_handler that processes SET requests
* — without it, S_EXT_CTRLS rejects with EINVAL on validate.
* Always-success s_ctrl is the right shape for "we accept whatever
* you tell us but actually act on the OUTPUT buffer payload alone."
* Always-success s_ctrl op. v4l2-core requires ops to be present on
* a ctrl_handler that processes SET requests — without it,
* S_EXT_CTRLS rejects with EINVAL on validate. We don't act on
* values here at SET time; for H.264, device_run later reads the
* (request-bound) values from p_cur and ships them to the daemon
* via struct daedalus_h264_meta. For VP9/AV1 we still rely on
* FFmpeg re-parsing the bitstream — those formats are self-
* describing per frame.
*/
static int daedalus_s_ctrl_noop(struct v4l2_ctrl *ctrl)
{
@@ -207,6 +209,44 @@ static const struct v4l2_ctrl_ops daedalus_ctrl_ops = {
.s_ctrl = daedalus_s_ctrl_noop,
};
/*
* Copy the current H.264 stateless control values into a
* daedalus_h264_meta scratch buffer. Returns true if all four
* required controls (SPS, PPS, scaling matrix, decode params) had
* data on the ctrl handler — caller then ships the meta block in
* REQ_DECODE. Returns false if any control was missing (caller
* skips the meta block; daemon will likely fail the decode, but
* with a clear "no SPS" error from libavcodec rather than a
* confusing protocol mismatch).
*
* The ctrl_handler's p_cur values are bound to the in-flight
* media_request by v4l2_ctrl_request_setup, which v4l2-m2m calls
* before device_run for stateless decoders.
*/
static bool daedalus_collect_h264_meta(struct daedalus_ctx *ctx,
struct daedalus_h264_meta *meta)
{
struct v4l2_ctrl *c_sps, *c_pps, *c_sm, *c_dp;
c_sps = v4l2_ctrl_find(&ctx->hdl, V4L2_CID_STATELESS_H264_SPS);
c_pps = v4l2_ctrl_find(&ctx->hdl, V4L2_CID_STATELESS_H264_PPS);
c_sm = v4l2_ctrl_find(&ctx->hdl, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
c_dp = v4l2_ctrl_find(&ctx->hdl, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
if (!c_sps || !c_pps || !c_sm || !c_dp)
return false;
if (!c_sps->p_cur.p_h264_sps || !c_pps->p_cur.p_h264_pps ||
!c_sm->p_cur.p_h264_scaling_matrix ||
!c_dp->p_cur.p_h264_decode_params)
return false;
meta->sps = *c_sps->p_cur.p_h264_sps;
meta->pps = *c_pps->p_cur.p_h264_pps;
meta->scaling_matrix = *c_sm->p_cur.p_h264_scaling_matrix;
meta->decode_params = *c_dp->p_cur.p_h264_decode_params;
return true;
}
static int daedalus_register_stateless_ctrls(struct v4l2_ctrl_handler *hdl)
{
size_t i;
@@ -637,36 +677,71 @@ static void daedalus_device_run(void *priv)
goto fail_buf_error;
}
payload_len = sizeof(*req) + blen;
req = kmalloc(payload_len, GFP_KERNEL);
if (!req)
goto fail_buf_error;
memset(req, 0, sizeof(*req));
req->codec_id = daedalus_fourcc_to_codec_id(ctx->src_fmt.pixelformat);
if (!req->codec_id) {
v4l2_err(&dev->v4l2_dev,
"device_run: unsupported OUTPUT pixelformat 0x%08x\n",
ctx->src_fmt.pixelformat);
kfree(req);
req = NULL;
goto fail_buf_error;
}
req->bitstream_len = (u32) blen;
req->capture_width = ctx->dst_fmt.width;
req->capture_height = ctx->dst_fmt.height;
req->capture_pix_fmt = ctx->dst_fmt.pixelformat;
req->capture_num_planes = ctx->dst_fmt.num_planes;
{
unsigned int p;
for (p = 0; p < ctx->dst_fmt.num_planes && p < 3; p++) {
req->capture_plane_size[p] =
ctx->dst_fmt.plane_fmt[p].sizeimage;
req->capture_plane_stride[p] =
ctx->dst_fmt.plane_fmt[p].bytesperline;
u32 cid = daedalus_fourcc_to_codec_id(ctx->src_fmt.pixelformat);
size_t meta_len = 0;
struct daedalus_h264_meta meta_local;
bool have_h264_meta = false;
if (!cid) {
v4l2_err(&dev->v4l2_dev,
"device_run: unsupported OUTPUT pixelformat 0x%08x\n",
ctx->src_fmt.pixelformat);
goto fail_buf_error;
}
/*
* H.264 needs SPS/PPS/scaling-matrix/decode-params shipped
* to the daemon alongside the slice bitstream — libavcodec
* can't decode slices without them. VP9/AV1 are self-
* describing so we skip the meta block for those.
*/
if (cid == DAEDALUS_CODEC_H264) {
memset(&meta_local, 0, sizeof(meta_local));
have_h264_meta = daedalus_collect_h264_meta(ctx, &meta_local);
if (have_h264_meta)
meta_len = sizeof(meta_local);
else
v4l2_warn(&dev->v4l2_dev,
"device_run: H.264 frame without SPS/PPS controls — daemon will fail decode\n");
}
payload_len = sizeof(*req) + meta_len + blen;
if (payload_len > DAEDALUS_PROTO_MAX_PAYLOAD) {
v4l2_err(&dev->v4l2_dev,
"device_run: payload %zu exceeds chardev cap %u\n",
payload_len,
(unsigned int) DAEDALUS_PROTO_MAX_PAYLOAD);
goto fail_buf_error;
}
req = kmalloc(payload_len, GFP_KERNEL);
if (!req)
goto fail_buf_error;
memset(req, 0, sizeof(*req));
req->codec_id = cid;
req->bitstream_len = (u32) blen;
req->capture_width = ctx->dst_fmt.width;
req->capture_height = ctx->dst_fmt.height;
req->capture_pix_fmt = ctx->dst_fmt.pixelformat;
req->capture_num_planes = ctx->dst_fmt.num_planes;
{
unsigned int p;
for (p = 0; p < ctx->dst_fmt.num_planes && p < 3; p++) {
req->capture_plane_size[p] =
ctx->dst_fmt.plane_fmt[p].sizeimage;
req->capture_plane_stride[p] =
ctx->dst_fmt.plane_fmt[p].bytesperline;
}
}
if (have_h264_meta) {
req->flags |= DAEDALUS_REQ_FLAG_H264_META;
memcpy((u8 *) req + sizeof(*req),
&meta_local, sizeof(meta_local));
}
memcpy((u8 *) req + sizeof(*req) + meta_len, bitstream, blen);
}
memcpy((u8 *) req + sizeof(*req), bitstream, blen);
inf = kzalloc(sizeof(*inf), GFP_KERNEL);
if (!inf)