Repository navigation
linux-pipewire: Fix NV12 camera frames - #391
Merged
Merged
Conversation
A PipeWire camera that sends NV12 shows a green, banded picture (obsproject/obs-studio#13823, reported by EberhartLeberhart with an IPU3 camera through libcamera) or crashes OBS as soon as it streams (obsproject/obs-studio#14002, theCarlG, with an Elgato Facecam Pro, a Sony A6700 and a Sony A7V). process_video_async() hands libobs one data pointer per PipeWire data block and a single line size of width * bpp, which formats.c gives as 2 for NV12. NV12 has a luma plane of width bytes per row and an interleaved chroma plane of half the rows. Cameras send both planes in one block, so the chroma plane gets no pointer and no line size and libobs reads twice the luma plane, past the end of the buffer. The stride and offset the producer gives in the buffer's chunk are ignored for every format. Move the plane setup from process_video_async() into frame-layout.c unchanged, and test it two ways: - test_pipewire_frame_layout (no daemon, no display; runs in CI): fake spa_buffers the way producers send them. NV12 in one block (with rows of the frame width, padded rows, no chunk stride), NV12 in one block per plane with a chunk offset, YUY2 with padded rows at an offset, and buffers that must be dropped (luma only; a chunk past the mapped block; a stride shorter than a row). Every plane must start where the producer put it, have its stride and lie inside the buffer. On master 7 of 8 cases fail; the guard (tightly packed YUY2 and RGBA) passes. - test_pipewire_camera_nv12 (end to end, private PipeWire daemon through test_pipewire_cursor_hdr.sh, private Xvfb): a producer stream offering only NV12 sends four coloured quadrants to the camera source's stream code, connected as camera-portal.c connects it, and the output is read back in BGRA. On master all 3 layouts fail and the output is green, as in the report: (0, 135, 0) where the picture is (200, 40, 40). Skips without pipewire or a display. Refs obs-rust#390 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A PipeWire camera that sends NV12 showed a green, banded picture at a fraction of its frame rate, or crashed OBS as soon as it streamed (obsproject/obs-studio#13823, obsproject/obs-studio#14002). The camera source gave libobs a single line size of width * 2 and one data pointer per PipeWire data block. Cameras send NV12 with both planes in one block (PipeWire's V4L2 and libcamera plugins both do), so the chroma plane had no pointer and no line size, and libobs copied twice the luma plane, reading past the end of the buffer: chroma left uninitialised (green), the luma rows doubled up (bands), and with a buffer of exactly the frame's size, a crash. Set up the frame's planes from the buffer: - NV12 in one block: the luma plane at the chunk offset, the chroma plane right after it, both with the chunk's stride. NV12 in one block per plane: each plane from its own block, with its own offset and stride (the luma stride when the block has none). - Every format honours the chunk's stride and offset; without a stride, rows are rounded up to 4 bytes as before, so tightly packed YUY2 and RGBA come out as they did. - The chunk is written by the producer: a buffer whose chunk lies outside the mapped block, whose stride is shorter than a row, or that does not hold every row of every plane is dropped, with one warning per stream, instead of being read out of bounds. Only raw frames are touched; no decoder or format choice changes. Ported from theCarlG's patch in obsproject/obs-studio#14002 (closed upstream unreviewed), which set up both NV12 planes from the chunk stride and checked the buffer size; this version also covers one block per plane with a chunk offset, honours the chunk for the packed formats, bounds the chunk by the mapped block, and logs once instead of every frame. EberhartLeberhart found the missing line size and chroma pointer, wrote the first patch in obsproject/obs-studio#13823 and measured it on IPU3 hardware (11.8 fps at 81% CPU before, 30 fps at 8% after). test_pipewire_frame_layout: 8 of 8 cases pass (7 failed before). test_pipewire_camera_nv12: 3 of 3 layouts show the camera's colours within 1 of 255 (green before), 10 of 10 repeated runs pass. Fixes obs-rust#390 Co-Authored-By: theCarlG <[email protected]> Co-Authored-By: EberhartLeberhart <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=f0b02b03e1ee7f82eac21ab70c9b81b782128366 secs=142 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791823457,"gate":"rust/all/fmt","host":"linux-x86_64","key":"1fa3223c7dd653cc747b2429487b14a395bdb41621528c2c6bba2450c39aa91b","lane":"lint","parents":["4ef0aa0ff8e2564fe16aa7eb110f11b1582262ec"],"secs":34,"stamp":"7c44194e3f2f6f1adaaa59292aa66790","tree":"f0b02b03e1ee7f82eac21ab70c9b81b782128366","v":1,"via":"run"} Ci-Attestation: {"at":1791823457,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"1fa3223c7dd653cc747b2429487b14a395bdb41621528c2c6bba2450c39aa91b","lane":"lint","parents":["4ef0aa0ff8e2564fe16aa7eb110f11b1582262ec"],"secs":34,"stamp":"629872839797ef38a17563f906773c42","tree":"f0b02b03e1ee7f82eac21ab70c9b81b782128366","v":1,"via":"run"} Ci-Attestation: {"at":1791823457,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"ae3f347be532240090e689115b8d031d68d783ceb5dee34907306b6f10aabcb7","lane":"rust-test","parents":["4ef0aa0ff8e2564fe16aa7eb110f11b1582262ec"],"secs":48,"stamp":"c8b1cbe78b02f576eae5d26c0f5e62e5","tree":"f0b02b03e1ee7f82eac21ab70c9b81b782128366","v":1,"via":"run"} Ci-Attestation: {"at":1791823457,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"9735e3f9d6e2f2e881b535a5860a57e6904b2bb6f0ba29e66318dca5e6f9bee7","lane":"tier2-linux","parents":["4ef0aa0ff8e2564fe16aa7eb110f11b1582262ec"],"secs":60,"stamp":"0892badee69feab9b061e351825de25c","tree":"f0b02b03e1ee7f82eac21ab70c9b81b782128366","v":1,"via":"run"}
Member
|
Verified locally (Tier 2 image + pipewire, wireplumber, dbus, under xvfb-run):
The edits to the existing cursor_hdr and portal_teardown cmake files only add frame-layout.c to their sources. Rebased on master, local gate passed; pushed as 623b605. |
zackees
force-pushed
the
fix/13823-pipewire-camera-nv12
branch
from
October 12, 2026 16:44
583178d to
623b605
Compare
9 tasks done
This was referenced Oct 12, 2026
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
A PipeWire camera that sends NV12 now shows its picture. Before, the preview was green, in three horizontal bands, at a fraction of the camera's frame rate, or OBS crashed as soon as the camera streamed.
prepare_obs_frame()(plugins/linux-pipewire/pipewire.cline 663) gave the frame one line size,width * bpp, andformats.cgives NV12 abppof 2. NV12's luma rows arewidthbytes.process_video_async()(lines 694-700) set one data pointer per PipeWire data block and no line size after the first. Cameras send NV12 with both planes in one block (PipeWire's V4L2 and libcamera plugins both do), so the chroma plane had no pointer and line size 0.The fix,
plugins/linux-pipewireonly:obs_pw_frame_layout()inframe-layout.[ch], with no stream state so it can be tested without PipeWire.Only raw frames are touched; no decoder, encoder or format choice changes.
test/cmocka: Add failing tests for PipeWire cameras sending NV12: moves the plane setup intoframe-layout.cunchanged and adds two tests.test_pipewire_frame_layout(Linux, no PipeWire daemon, no display; runs in CI): fake buffers the way producers send them. NV12 in one block with rows of the frame width, with padded rows and an odd height, and without a chunk stride; NV12 in one block per plane with a chunk offset; YUY2 with padded rows at an offset; and buffers that must be dropped (luma only, a chunk past the mapped block, a stride shorter than a row). Every plane must start where the producer put it, have its stride and lie inside the buffer. 7 of 8 fail; the guard (tightly packed YUY2 and RGBA, with and without a chunk stride) passes.test_pipewire_camera_nv12(Linux, private PipeWire daemon throughtest_pipewire_cursor_hdr.sh, private Xvfb; skips without them): a producer stream offering only NV12 sends four coloured quadrants to the camera source's stream code, connected the waycamera-portal.cconnects it (async video source, NV12 selected, the producer's node as target), and the output is read back in BGRA. 3 of 3 layouts fail, green as in the report: (0, 135, 0) where the picture is (200, 40, 40).linux-pipewire: Fix NV12 camera frames: 8 of 8 and 3 of 3 pass; every quadrant within 1 of 255 of its colour.Ported from theCarlG's patch in obsproject/obs-studio#14002 (closed upstream unreviewed; co-author). Differences: it handled NV12 only, ignored the chunk offset in the one-block case and for other formats, did not bound the chunk by the mapped block, assumed an unset stride equals the width, and logged on every dropped frame. EberhartLeberhart found the missing line size and chroma pointer, wrote the first patch in obsproject/obs-studio#13823 and measured it on the affected hardware (co-author).
Motivation and Context
Fixes #390 (upstream obsproject/obs-studio#13823, reported by EberhartLeberhart; the same bug as a crash in theCarlG's obsproject/obs-studio#14002).
Why this one: two people hit it independently with four different cameras (an IPU3 laptop camera, an Elgato Facecam Pro, a Sony A6700 and a Sony A7V), each traced it to the same lines and each tested a patch on real hardware: before, green bands at 11.8 fps and 81% CPU, or a crash; after, a correct picture at 30 fps and 8% CPU, and NV12 1920x1080 at 60 fps without crashes. The issue and the PR were closed upstream without a fix, and the code is unchanged. The fix is small, local to the PipeWire camera's raw-frame path, and leaves tightly packed YUY2 and RGBA as they were. The demand is modest (score 5 in our crawl, plus the PR).
How Has This Been Tested?
Automated, in both libobs lanes (
ENABLE_RUST_LIBOBSOFF and ON), on current master:test_pipewire_frame_layout: 7 of 8 cases fail at the RED commit and 8 of 8 pass at GREEN, in both lanes. It needs no daemon or display, so it runs in every Tier 2 Linux lane.test_pipewire_camera_nv12: 3 of 3 fail at RED (green output) and 3 of 3 pass at GREEN, in both lanes, against a private PipeWire daemon (own runtime dir, no session manager, no devices) under a private Xvfb with software GL.ctest -Vshows all 3 cases run, with no skips, also with the system'slibobs-openglhidden (the test loads the one from the build tree). 10 of 10 repeated runs pass. Without a display, or without the wrapper, it skips; CI's image has nopipewirebinary, so there it skips (77).test_pipewire_cursor_hdrandtest_screencast_portal_teardown, which buildpipewire.ctoo, pass.ci/test_registration.pyare clean.Checked against the producers in PipeWire 1.6: the V4L2 plugin sends one block with chunk offset 0, size
bytesusedand stridebytesperline; the libcamera plugin sends one block per distinct buffer fd (one for NV12 from a single allocation), with the chunk size covering both planes and the stride set. Both pass the new checks.Not tested:
Not fixed here (separate causes, own PRs):
g_hash_table_iter_remove()freed it (camera-portal.clines 1032-1033), a use-after-free on every unplug.formats.c.obs_pw_frame_layout()is where its three planes would go.Types of changes
Checklist:
🤖 Generated with Claude Code