Skip to content

linux-pipewire: Fix NV12 camera frames - #391

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/13823-pipewire-camera-nv12
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/13823-pipewire-camera-nv12

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

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.c line 663) gave the frame one line size, width * bpp, and formats.c gives NV12 a bpp of 2. NV12's luma rows are width bytes.
  • 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.
  • libobs then copied twice the luma plane, reading past the end of the buffer, and nothing into the chroma plane: chroma left uninitialised (green), luma rows doubled up (bands), and a read that can run off the buffer's mapping (the crash).
  • The stride and offset the producer writes in the buffer's chunk were ignored for every format.

The fix, plugins/linux-pipewire only:

  • The plane setup moves into a small helper, obs_pw_frame_layout() in frame-layout.[ch], with no stream state so it can be tested without PipeWire.
  • NV12 in one block: the luma plane at the chunk offset, the chroma plane right after it, both with the chunk 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 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, encoder or format choice changes.

  1. test/cmocka: Add failing tests for PipeWire cameras sending NV12: moves the plane setup into frame-layout.c unchanged 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 through test_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 way camera-portal.c connects 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).
  2. 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_LIBOBS OFF 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 -V shows all 3 cases run, with no skips, also with the system's libobs-opengl hidden (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 no pipewire binary, so there it skips (77).
  • Full ctest: 126 of 128 at RED (only the two new tests fail) and 128 of 128 at GREEN, in each lane (130 of 130 rebased on current master). test_pipewire_cursor_hdr and test_screencast_portal_teardown, which build pipewire.c too, pass.
  • clang-format, gersemi and ci/test_registration.py are clean.

Checked against the producers in PipeWire 1.6: the V4L2 plugin sends one block with chunk offset 0, size bytesused and stride bytesperline; 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:

  • A physical NV12 camera. The end-to-end test uses a real PipeWire daemon and the camera source's own stream code; the camera portal, which only hands over the PipeWire connection and the device's node, is not in the loop.

Not fixed here (separate causes, own PRs):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

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]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 16:34
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"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally (Tier 2 image + pipewire, wireplumber, dbus, under xvfb-run):

  • RED 4ef0aa0: test_pipewire_frame_layout fails 5 cases (nv12_one_block, nv12_one_block_padded_rows, nv12_one_block_without_stride, nv12_two_blocks, chunk_outside_the_block_is_dropped); test_pipewire_camera_nv12 fails 3/3
  • GREEN 583178d: frame_layout 8/8, camera_nv12 3/3

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: linux-pipewire: cameras sending NV12 show green or crash (upstream #13823)

2 participants