Skip to content

fix(playback): composite bitmap subtitle burn-in on the GPU for QSV/VAAPI - #465

Merged
Quick104 merged 1 commit into
mainfrom
fix/gpu-bitmap-subtitle-overlay
Jul 29, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/gpu-bitmap-subtitle-overlay

Conversation

@CoffeeKnyte

@CoffeeKnyte CoffeeKnyte commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Single commit: move bitmap subtitle burn-in off the CPU and onto the GPU for QSV/VAAPI.

Problem

Movies with burned-in bitmap subtitles buffer forever and sometimes crash the stream.

On the live server, a viewer watching a 1080p movie with PGS ("image") subtitles burned in — e.g. A.I. Artificial Intelligence — never builds up a play buffer. The player sits right at the live edge: it asks for the next 2-second chunk, the server delivers it a beat late, the player asks for the next one, and so on for minutes on end. Any tiny hiccup turns into a visible stall/rebuffer. In the server logs this showed up as a session requesting one segment at a time, each arriving ~1.5–2.5s old against a 3.5s timeout, plus repeated transcode restarts and, on some runs, an outright ffmpeg crash (bus error (core dumped)) that froze playback for up to a minute while it recovered.

This only affects bitmap subtitles (PGS/VOBSUB/DVB) that are burned into the video during a transcode. Text subtitles (SRT/ASS) and direct/remux playback are unaffected.

The root cause is throughput: with the subtitle burn-in running on the CPU, the transcode encodes slower than realtime, so it can physically never get ahead of the viewer.

Measured on the exact file/hardware (jellyfin-ffmpeg 7.1.4, Intel QSV, 25s sample, seek to 10:00):

Filter graph Throughput Stable?
Before — hwdownload → software overlay → hwupload → qsv 0.68× realtime (16 fps) No — SIGBUS bus error (core dumped) on 2 of 3 runs
After — overlay_vaapi → h264_vaapi 7.3× (176 fps) Yes
After — overlay_vaapi → hwmap qsv → h264_qsv (the QSV drop-in) 7.67× (185 fps) Yes

Below 1.0× is starvation by definition; the fix takes it to ~7×, i.e. from "can never keep up" to "roughly 7 seconds of headroom produced per second of playback."

Solution

fix(playback): composite bitmap subtitle burn-in on the GPU for QSV/VAAPI

The burn-in graph is built in appendBitmapSubtitleBurnInArgs (internal/playback/transcode.go:727). Previously every hardware path (qsv/vaapi/nvenc) used the same skeleton it inherited from the text path: pull the entire decoded video frame off the GPU to system RAM (hwdownload,format=yuv420p), composite the subtitle with the software overlay filter, convert, and push the whole frame back to the GPU (hwupload/hwmap). For a 1080p source that's ~70+ MB/s of full-frame PCIe traffic plus a per-frame CPU composite — purely to stamp on a subtitle bitmap that only changes every few seconds. That roundtrip is both the throughput wall and the source of the SIGBUS instability (the container's /dev/shm is only 64M, which the large QSV surface churn tips over).

The fix keeps the video on the GPU and uploads only the small subtitle bitmap:

[0:s:N]format=bgra,hwupload[sub];[0:v:0][sub]overlay_vaapi=eof_action=pass,<scale_vaapi…>[vout]
  • QSV ends the chain with the existing qsvScaleFilter helper, whose tail (scale_vaapi=…,hwmap=derive_device=qsv,format=qsv) maps the composited VAAPI surface to QSV for h264_qsv.
  • VAAPI ends with vaapiScaleFilter; the frames stay on VAAPI straight into h264_vaapi, so no cross-device map is needed.
  • Overlay still happens at native resolution first, then scale, so bitmap subtitle geometry is preserved exactly as before, and eof_action=pass is carried over (verified supported by overlay_vaapi) so the video keeps flowing after the subtitle stream ends.

Deliberately not changed:

  • The libass text path (appendSubtitleBurnInArgs) stays on CPU — libass is a software renderer with no GPU equivalent.
  • NVENC and CPU encodes keep the software overlay. overlay_cuda exists in the build but I have no NVIDIA hardware to verify it on, and shipping an unverified hardware filtergraph could break working (if slow) NVIDIA burn-in. Left as a follow-up.

Tests: updated the VAAPI assertion to the GPU graph and added QSV and NVENC cases (internal/playback/transcode_args_test.go), locking in that QSV/VAAPI composite on the GPU with no hwdownload, and that NVENC still takes the CPU roundtrip.

Risk / follow-ups

  • NVENC still starves on bitmap burn-in — it keeps the CPU roundtrip. A follow-up should validate overlay_cuda on real NVIDIA hardware and switch it over.
  • PGS canvas vs. video size: overlay_vaapi composites at 0,0, identical to the prior software overlay; PGS is authored at the video resolution in practice, so no positioning regression is expected, but it's worth an eyeball on a title with an oddly-authored PGS track.
  • The two GPU graphs are validated by ffmpeg runs on one Intel iHD/QSV host (the production box). Other VAAPI drivers (AMD/older Intel) should be smoke-tested before assuming universal behavior.
  • /dev/shm at 64M is small; the fix removes the pressure that was tipping it into SIGBUS, but the low limit is worth revisiting separately.
  • No existing scope issue cleanly covers this; it was found by direct log analysis of the live deployment. A tracking issue can be filed if wanted (the repro was actually executed — real logs and real ffmpeg benchmarks).

Verification

  • gofmt -l — clean on both files.
  • go vet ./internal/playback/ — clean.
  • go test ./internal/playback/... — pass (ok … 6.096s); all 9 burn-in tests pass, including the 3 new/updated ones.
  • Real ffmpeg benchmarks on the production host against the actual starving file produced the table above (before: 0.68× + SIGBUS; after: 7.3–7.7×, stable), with eof_action=pass and a 720p downscale target both exercised.

AI-use disclosure

  • Tool(s): Claude Code
  • Model(s): claude-opus-4-8[1m]
  • Involvement: AI-assisted (human-directed: diagnosis, benchmarking, implementation, and tests by the agent; reviewed and merged by the maintainer)
  • Adversarial review: Self-review of the diff checked (1) overlay-before-scale ordering is preserved so subtitle geometry is unchanged; (2) the scale helpers return valid no-scale chains when TargetResolution is empty, so an un-scaled 1080p→1080p burn-in still produces a valid graph; (3) eof_action=pass is actually accepted by overlay_vaapi (confirmed via -h filter); (4) NVENC/text/CPU paths are untouched and still covered by tests. Open risk left explicit above: overlay_cuda (NVENC) is unverified and intentionally left on the slow path rather than shipped blind.

Summary by CodeRabbit

  • Performance Improvements

    • Improved bitmap subtitle burn-in for VAAPI and QSV hardware-accelerated playback by compositing subtitles directly on the GPU.
    • Reduced unnecessary video frame transfers between GPU and CPU memory, which can improve playback efficiency.
  • Bug Fixes

    • Preserved the expected CPU-based subtitle overlay behavior for NVENC playback.

…AAPI

Bitmap subtitle burn-in (PGS/VOBSUB/DVB) ran the whole video through a
GPU->CPU->GPU roundtrip: every decoded frame was hwdownload'd to system
memory, the subtitle bitmap composited with the software overlay filter,
then hwupload'd back for the encoder. On a 1080p source that pins the
encode below realtime (~0.68x measured), so the client can never build a
buffer and rides the produced-head edge indefinitely; the same roundtrip
also intermittently crashes the QSV buffer path with SIGBUS.

Composite on the GPU instead via overlay_vaapi for QSV and VAAPI: the
decoded video never leaves its VAAPI surface and only the small,
low-frequency subtitle bitmap is uploaded. Measured ~7x realtime and
crash-free on the same file. The libass text path is unchanged (it must
stay on CPU), and NVENC/CPU keep the software overlay because overlay_cuda
is unverified on the bundled ffmpeg.

Adds QSV and NVENC bitmap burn-in tests and updates the VAAPI test to the
GPU graph.
@coderabbitai

coderabbitai Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f26406a5-54e6-474b-b54e-5b0e7cb6fd94

📥 Commits

Reviewing files that changed from the base of the PR and between 2bc6ebb and ccec06f.

📒 Files selected for processing (2)
  • internal/playback/transcode.go
  • internal/playback/transcode_args_test.go

📝 Walkthrough

Walkthrough

Bitmap subtitle burn-in now uses GPU composition for QSV and VAAPI, while NVENC and CPU encoding retain software overlay paths. Tests cover the updated VAAPI, QSV, and NVENC filter graphs.

Changes

Bitmap subtitle burn-in

Layer / File(s) Summary
Hardware-specific burn-in filter graphs
internal/playback/transcode.go
appendBitmapSubtitleBurnInArgs builds overlay_vaapi graphs for QSV and VAAPI, and retains CPU overlay processing for NVENC and software encoding.
Burn-in path coverage
internal/playback/transcode_args_test.go
Tests verify GPU composition for VAAPI and QSV and CPU overlay with CUDA transfers for NVENC.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TranscodeArgs
  participant FilterGraph
  participant HardwareEncoder
  TranscodeArgs->>FilterGraph: Select graph from opts.HWAccel
  FilterGraph->>HardwareEncoder: Send overlay_vaapi graph for QSV/VAAPI
  FilterGraph->>HardwareEncoder: Send hwdownload/overlay graph for NVENC
Loading

Possibly related PRs

Suggested labels: v1

Suggested reviewers: quick104, zzebrahz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: moving bitmap subtitle burn-in to GPU compositing for QSV/VAAPI.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/gpu-bitmap-subtitle-overlay

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot added the v1.0 Silo v1 scope - auto-adds to the Silo v1 project label Jul 24, 2026
@Quick104
Quick104 merged commit 7907e0c into main Jul 29, 2026
1 check passed
@Quick104
Quick104 deleted the fix/gpu-bitmap-subtitle-overlay branch July 29, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1.0 Silo v1 scope - auto-adds to the Silo v1 project

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants