daemon: filter tiny pause-time bitstreams (closes #17) #18

Merged
marfrit merged 1 commits from noether/issue-17-tiny-bitstream-filter into main 2026-05-22 16:14:56 +00:00
Owner

Fixes #17.

Symptom

Firefox YouTube on higgs (Pi CM5 / trixie) plays H.264 at ~46 fps through libva → daedalus. Pause for ≥ 1 s; resume. Daemon goes silent forever after resume; Firefox routes the rest of the session through libmozavcodec SW. Stats for nerds still reports avc1.

Root cause

Last daemon log before silence:

REQ_DECODE cookie=N codec=3 bitstream=3 bytes meta=h264 capture=1280x720 1 planes
[h264 @ ...] no frame!
[ERR] decoder: avcodec_send_packet failed: -1094995529  (AVERROR_INVALIDDATA)

3 bytes is below the structural minimum for any valid H.264 NAL (3-byte start code + 1-byte NAL header = 4 bytes minimum). libva-v4l2-request-fourier flushes this stub into the OUTPUT_MPLANE queue at the pause boundary. avcodec_send_packet correctly rejects it; the daemon's handle_req_decode propagated the negative rc to the kernel without RESP_FRAME; libva returned failure to Firefox; Firefox marked H.264-via-VAAPI as broken for the session.

Fix

At the REQ_DECODE entry in daemon/src/chardev_client.c::handle_req_decode, short-circuit any bitstream below the minimum-parseable H.264 NAL threshold. Log INFO, skip daedalus_decoder_run_request, return RESP_FRAME with status=DAEDALUS_DECODE_NO_FRAME so libva's V4L2 surface pool stays healthy and Firefox doesn't see a failure.

if (req.bitstream_len < 4) {
    log_info("REQ_DECODE cookie=%u: tiny bitstream %u bytes — dropping as no-op (pause-time sentinel)",
             hdr->cookie, req.bitstream_len);
    memset(&resp, 0, sizeof(resp));
    resp.status = DAEDALUS_DECODE_NO_FRAME;
    return send_response(cli, DAEDALUS_MSG_RESP_FRAME,
                         hdr->cookie, &resp, sizeof(resp));
}

Scope NOT covered (deferred)

  • A more general "tolerate AVERROR_INVALIDDATA mid-stream" path (drop bad packet, keep going). Worth doing later but would mask unrelated bugs.
  • Investigating WHY libva-v4l2-request-fourier sends the 3-byte sentinel on pause. Likely an upstream issue in that driver; tracked separately if this filter is not enough.

Wire protocol

Unchanged. No DAEDALUS_PROTO_VERSION bump. No daedalus-v4l2-dkms bump needed.

Verify

After merging + bumping marfrit/marfrit-packages/debian/daedalus-v4l2 to this tip + deploying on higgs:

  1. Open YouTube avc1 stream in Firefox.
  2. Confirm decoder OK lines while playing.
  3. Pause ≥ 1 s.
  4. Resume.
  5. sudo journalctl -u daedalus-v4l2 --since '10s' | grep -c 'decoder: OK' → > 0 (HW path still active).
  6. Journal should show one tiny bitstream 3 bytes — dropping as no-op line per pause cycle.
Fixes #17. ## Symptom Firefox YouTube on higgs (Pi CM5 / trixie) plays H.264 at ~46 fps through libva → daedalus. Pause for ≥ 1 s; resume. Daemon goes silent forever after resume; Firefox routes the rest of the session through libmozavcodec SW. Stats for nerds still reports `avc1`. ## Root cause Last daemon log before silence: ``` REQ_DECODE cookie=N codec=3 bitstream=3 bytes meta=h264 capture=1280x720 1 planes [h264 @ ...] no frame! [ERR] decoder: avcodec_send_packet failed: -1094995529 (AVERROR_INVALIDDATA) ``` 3 bytes is below the structural minimum for any valid H.264 NAL (3-byte start code + 1-byte NAL header = 4 bytes minimum). libva-v4l2-request-fourier flushes this stub into the OUTPUT_MPLANE queue at the pause boundary. `avcodec_send_packet` correctly rejects it; the daemon's `handle_req_decode` propagated the negative rc to the kernel without RESP_FRAME; libva returned failure to Firefox; Firefox marked H.264-via-VAAPI as broken for the session. ## Fix At the REQ_DECODE entry in `daemon/src/chardev_client.c::handle_req_decode`, short-circuit any bitstream below the minimum-parseable H.264 NAL threshold. Log INFO, skip `daedalus_decoder_run_request`, return RESP_FRAME with `status=DAEDALUS_DECODE_NO_FRAME` so libva's V4L2 surface pool stays healthy and Firefox doesn't see a failure. ```c if (req.bitstream_len < 4) { log_info("REQ_DECODE cookie=%u: tiny bitstream %u bytes — dropping as no-op (pause-time sentinel)", hdr->cookie, req.bitstream_len); memset(&resp, 0, sizeof(resp)); resp.status = DAEDALUS_DECODE_NO_FRAME; return send_response(cli, DAEDALUS_MSG_RESP_FRAME, hdr->cookie, &resp, sizeof(resp)); } ``` ## Scope NOT covered (deferred) - A more general "tolerate AVERROR_INVALIDDATA mid-stream" path (drop bad packet, keep going). Worth doing later but would mask unrelated bugs. - Investigating WHY `libva-v4l2-request-fourier` sends the 3-byte sentinel on pause. Likely an upstream issue in that driver; tracked separately if this filter is not enough. ## Wire protocol Unchanged. No `DAEDALUS_PROTO_VERSION` bump. No daedalus-v4l2-dkms bump needed. ## Verify After merging + bumping `marfrit/marfrit-packages/debian/daedalus-v4l2` to this tip + deploying on higgs: 1. Open YouTube avc1 stream in Firefox. 2. Confirm decoder OK lines while playing. 3. Pause ≥ 1 s. 4. Resume. 5. `sudo journalctl -u daedalus-v4l2 --since '10s' | grep -c 'decoder: OK'` → > 0 (HW path still active). 6. Journal should show one `tiny bitstream 3 bytes — dropping as no-op` line per pause cycle.
marfrit added 1 commit 2026-05-22 15:27:00 +00:00
libva-v4l2-request-fourier flushes a stub packet into the V4L2
OUTPUT_MPLANE queue at playback-pause boundaries.  The payload is
shorter than any parseable H.264 NAL (3-byte start code + 1-byte
NAL header = 4 bytes minimum); avcodec_send_packet returns
AVERROR_INVALIDDATA (-1094995529), which propagated to the kernel
as a decode failure.  Firefox then marked H.264-via-VAAPI as
broken for the session and routed every subsequent frame to
libmozavcodec SW — pause never recovered to HW.

At the REQ_DECODE entry in chardev_client.c::handle_req_decode,
short-circuit any bitstream below the minimum-parseable threshold:
log INFO, skip daedalus_decoder_run_request, and reply RESP_FRAME
with status=DAEDALUS_DECODE_NO_FRAME so libva's V4L2 surface pool
stays healthy and Firefox doesn't see a failure.

Repro: Pi CM5 trixie, daedalus-v4l2 0.1.0+r41 + ffmpeg-v4l2-
request-fourier 2:8.1+rfourier+gb57fbbe-9, Firefox YouTube avc1.
Play → daemon decodes at ~46 fps.  Pause ≥ 1s.  Resume → daemon
silent; sudo journalctl -u daedalus-v4l2 --since '10s' | grep -c
'decoder: OK' = 0.  Last entry before silence:

    REQ_DECODE cookie=N codec=3 bitstream=3 bytes ...
    [h264 @ ...] no frame!
    [ERR] decoder: avcodec_send_packet failed: -1094995529

After this fix the 3-byte sentinel logs as 'tiny bitstream 3
bytes — dropping as no-op' and the libavcodec context is
untouched; the next real REQ_DECODE proceeds normally.

Scope NOT covered (intentionally deferred):
- A more general "tolerate AVERROR_INVALIDDATA mid-stream" path.
  Worth doing later but masks unrelated bugs.
- Investigating WHY libva sends the 3-byte sentinel on pause.
  Likely an upstream libva-v4l2-request-fourier issue; tracked
  separately if this filter is not enough.

Wire protocol unchanged.  No DAEDALUS_PROTO_VERSION bump.
marfrit merged commit 1d8f5af164 into main 2026-05-22 16:14:56 +00:00
marfrit deleted branch noether/issue-17-tiny-bitstream-filter 2026-05-22 16:14:56 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: reauktion/daedalus-v4l2#18