feat(linux): add zero-copy v4l2 encoder - #5717
Open
GenshinImpactStarts wants to merge 7 commits into
Open
GenshinImpactStarts wants to merge 7 commits into
GenshinImpactStarts wants to merge 7 commits into
Conversation
GenshinImpactStarts
force-pushed
the
feat-v4l2-encoder
branch
from
September 26, 2026 17:05
b19ba1e to
56d825b
Compare
|
GenshinImpactStarts
marked this pull request as ready for review
September 26, 2026 17:24
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Description
This PR introduces a zero-copy V4L2 hardware encoder path on Linux, targeting embedded devices such as Raspberry Pi. It currently supports baseline YUV420 8-bit encoding, integrates with existing capture backends, and syncs with the Web UI settings. The implementation follows the VAAPI approach: exporting DMA-BUF fds and using EGL for color conversion.
Implementation
Required AVCodec integration changes
The V4L2 M2M path differs from the existing AVCodec initialization and usage paths, so several supporting changes were necessary:
avcodec_encode_device_t:load_opened_context: retrieves the AVCodec context during encoder initialization to extract V4L2 DMA-BUFs.send_frame: V4L2 buffers are managed by FFmpeg, so additional metadata must be passed during encoding to interact with the FFmpeg patches.validate_configpath now manually passes a terminating packet (nullptr) to flush the encoder.validate_confignow records the packet count of the first frame and concatenates them when the actual packets are produced.eglCreateImage,eglDestroyImage,FramebufferTexture). Fallbacks are provided for these older drivers.Testing and limitations
Tested only on Raspberry Pi 4B running Debian 12, wayland. The impact on other codecs has not been tested. It would be helpful if CI could generate artifacts for further testing.
Performance on Raspberry Pi 4B
On Raspberry Pi 4B under Wayland, 1080p encoding currently reaches approximately 15–20 fps with around 50% CPU utilization.
Based on
perfprofiling, roughly 60% of the measured CPU time is spent in the wlroots capture path, primarily inwl::dmabuf_t::create_and_copy_dmabuf, which currently creates a new buffer for every captured frame. The encoder itself accounts for only around 20% of the measured CPU time.This suggests that the next major performance improvement should come from optimizing the wlroots capture path and avoiding per-frame buffer allocation/copying. That work is outside the scope of this PR.
In the end
This PR is quite large, so I've roughly split the changes into logical commits; it's best reviewed commit by commit.
Note: This PR depends on LizardByte/build-deps#781, which adds the two required FFmpeg patches for V4L2 zero-copy support.
Screenshot
Issues Fixed or Closed
Roadmap Issues
Type of Change
Checklist
AI Usage
See our AI usage policy.