Skip to content

rust: Port media-io/video-frame.c to obs-media-io - #182

Merged
zackees merged 2 commits into
obs-rust:masterfrom
swilk19:port/video-frame
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
swilk19:port/video-frame

Conversation

@swilk19

@swilk19 swilk19 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Description

Ports libobs/media-io/video-frame.c to obs-media-io (obs_media_io::video_frame), as item 5 of the media-io part 2 order on #45. It also covers the video-frame.h inlines under the header-inline rule. No intentional differences.

Exports. Only video_frame_init and video_frame_copy get C symbols, listed in rust-exports.d/obs-media-io-video_frame.txt. video_frame_get_linesizes and video_frame_get_plane_heights are not static, but they have no header declaration and no caller outside video-frame.c. They are safe-core functions (linesizes, plane_heights) with no C symbol, as agreed on #45. video-frame.c joins the ENABLE_RUST_LIBOBS guard.

Header inlines. video_frame_free, video_frame_create and video_frame_destroy stay compiled into their C callers, and the header is unchanged. ffi::video_frame mirrors struct video_frame, with a layout test against the oracle covering size, alignment, both field offsets and both array lengths. It also has plain-Rust (not exported) versions of the three inlines for later ports that handle a struct video_frame, such as video-io.c. These are checked against oracle_* wrappers of the real inlines.

Safe core. The core has these pieces:

  • linesizes / plane_heights, with exhaustive matches.
  • FrameLayout: the allocation size, plus each plane's linesize and offset from data[0].
  • copy_extent / copy_plane.
  • An owned VideoFrame with copy_from.

The shims map a format outside the enum to VIDEO_FORMAT_NONE, which is where the C switches' default: goes.

Characterized, not endorsed. These behaviours are ported as they are:

  • V210 ALIGN by 48. ALIGN masks with ~47, which clears bits 0-3 and bit 5, so width 1 becomes 16, width 47 and 48 become 80, and width 1280 stays 1280 (linesize 3424, not 3456). Unless width + 47 wraps, the result is never below the width.
  • u32 wraps. width * 4, HALF(UINT32_MAX) = 0, V210's * 16, and the u32 linesize * rows length of an equal-linesize copy all wrap as in C. Plane sizes and offsets are usize, matching the C's size_t.
  • Skipped planes. Plane i starts at offsets[i - 1], so a plane after a skipped one lands on plane 0. A skip only happens when a linesize wraps to 0, for example I40A at width UINT32_MAX.
  • bmalloc(0). VIDEO_FORMAT_NONE, an unknown format, or a zero width or height still reaches bmalloc(0), which crashes. test: characterize video-frame.c before media-io port #117's frame_init_none pins this in a forked child, so the shim calls the real bmalloc with the layout's size and does not return early. The safe VideoFrame::new panics instead.

Oracle. oracle/video_frame.c renames the four non-static functions and wraps the inlines. Its bmalloc / bfree / base_get_alignment are libobs's, which under cargo test is the Rust bmem port, so both sides share one allocator.

Motivation and Context

Item 5 of the order approved on #45 (#45 (comment)). It was claimed on #151.

Fixes #151
Refs #45

How Has This Been Tested?

Linux x86_64 (WSL2), Rust via soldr.

  • Commits follow the policy. test/cmocka/test_video_frame.c already landed in test: characterize video-frame.c before media-io port #117, so the PR starts at RED (todo!() core), then GREEN.

  • Tier 1 ports test_video_frame.c 1:1, including its expected_linesizes / expected_heights model. frame_init_none becomes a should_panic. The null-pointer cases have no safe-API counterpart and are in parity instead. Edge cases on top of the port: the V210 quirk, the u32 wraps, the skipped-plane offset, the copy extents, and copies between frames of different widths and formats.

  • Tier 3 sweeps:

    • every enumerator plus out-of-range values (-1, 26, 27, 100, INT_MIN, INT_MAX), with widths and heights at each rounding step and u32 wrap, for the linesizes and heights
    • frame shapes from init and create (linesizes, and plane offsets from data[0] or null), starting from a 0xAB-filled frame
    • free (zeroes the frame), and free / destroy / init on NULL
    • copies into separate, canary-filled destinations with equal, wider, narrower and zero linesizes, comparing every destination byte

    Proptest adds arbitrary formats and sizes, and random per-plane linesizes for copies. The exclusions are the C crashes and UB, and they are listed in the test file:

    • bmalloc(0), decided from the oracle's tables
    • layouts too large to allocate
    • copies past a plane or between overlapping planes
  • RED→GREEN, checked with soldr's cache disabled and a fresh target dir. At the RED commit, video_frame fails 19/20; the one that passes checks the test's own V210 model. The parity binary aborts at its first todo!() inside an extern "C" shim. The layout test passes there. At head, everything passes.

  • Mutation check. Each of these fails Tier 1 and parity, and all were reverted:

    • dropping HALF's rounding
    • shifting each plane one byte past the previous one
    • copying one extra byte per row into a wider dst
    • rounding V210 to a true multiple of 48

    Making the equal-linesize copy length usize fails Tier 1.

  • Windows MSVC (local, cargo-xwin): soldr cargo test -p obs-media-io passes on x86_64-pc-windows-msvc. The oracle is built with clang-cl, and cmocka does not run.

  • Local checks: ./lint and ./unittest pass. tier2-linux ran test_video_frame (the unchanged test: characterize video-frame.c before media-io port #117 test, frame_init_none crash included) with ENABLE_RUST_LIBOBS OFF and ON (50/50 each). The libobs exported symbols are identical OFF vs ON (1752). HEAD carries the local-gate stamp.

  • Not run locally: macOS. The Rust Tests and rust-tier2 CI jobs cover it.

Types of changes

  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.

🤖 Generated with Claude Code

@swilk19
swilk19 requested a review from zackees as a code owner October 11, 2026 01:17
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Reviewed head 9b2bf84. Base is master. Policy is met, with no blocking findings.

Checked:

  • Two commits: RED (05690a1), then GREEN (9b2bf84). No existing C tests or public headers are edited (video-frame.h is unchanged; test_video_frame.c is untouched).
  • libobs/CMakeLists.txt swaps video-frame.c under the ENABLE_RUST_LIBOBS guard like its neighbours. The export list obs-media-io-video_frame.txt and the lib.rs and oracle lib.rs one-liners are the per-unit layout.
  • soldr cargo test -p obs-media-io -p obs-c-oracle, run in a worktree with SOLDR_RUSTC_WRAPPER=none, passes at head.

Must change before merge: none.

Non-blocking:

Optional: none.

The PR is ready to merge once CI is green. I'm not merging it here.

@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified (soldr cache disabled) and rebased onto current master with no conflicts (old head 9b2bf84, replaced with --force-with-lease). Claimed on #151 before the PR, too.

  • RED at the shims/tests commit: video_frame fails 19/20.
  • GREEN: the full suite passes. The unchanged test_video_frame (from test: characterize video-frame.c before media-io port #117) passes with the Rust port OFF and ON, and validate.sh's derived check confirms media-io/video-frame.c is left out when ON (30 ported objects).

The local gate passes and is stamped. I'll merge once CI is green.

swilk19 and others added 2 commits October 10, 2026 18:46
Tier 1, Tier 2 layout and Tier 3 parity tests for video_frame_init,
video_frame_copy and the video-frame.h inlines, against a todo!() core.
test/cmocka/test_video_frame.c (obs-rust#117) is the existing C test, so there
is no characterization commit. Tier 1 ports it 1:1, including its
expected_linesizes / expected_heights model, and adds edge cases: the
V210 ALIGN-by-48 quirk (1 -> 16, 47 and 48 -> 80, 1280 stays 1280), u32
wraps (width * 4, HALF(UINT32_MAX) = 0, the u32 linesize * rows copy
length), and a plane after a skipped one landing on plane 0.

Only video_frame_init and video_frame_copy get C symbols, listed in
rust-exports.d/obs-media-io-video_frame.txt.
video_frame_get_linesizes and video_frame_get_plane_heights have no
header declaration and no caller outside video-frame.c, so they are
safe-core functions only. ffi::video_frame mirrors struct video_frame.
It also has plain-Rust video_frame_free / _create / _destroy for later
ports that handle a struct video_frame. The C inlines stay compiled into
their callers (header-inline rule). A format outside enum video_format
takes the C switches' default, VIDEO_FORMAT_NONE.

video_frame_init still reaches bmalloc(0), which crashes, for
VIDEO_FORMAT_NONE, an unknown format and a zero width or height, as
test_video_frame.c's frame_init_none expects. The safe VideoFrame::new
panics there.

The oracle (oracle/video_frame.c) renames the four non-static functions
and wraps the inlines in oracle_* functions. Its bmalloc and bfree are
libobs's, which under cargo test is the Rust bmem port. Parity sweeps
every enumerator and out-of-range values over widths and heights at each
rounding step and u32 wrap for the linesizes and heights. It compares
frame shapes (linesizes, and plane offsets from data[0]) from init and
create, and checks free and destroy. Copies run into separate
canary-filled destinations, with equal, wider, narrower and zero
linesizes, and every byte is compared. Proptest adds arbitrary formats
and sizes. The exclusions are the C crashes and UB: bmalloc(0), layouts
too large to allocate, and copies past a plane or between overlapping
planes.

RED: 19 of the 20 Tier 1 tests fail on todo!(). The parity binary aborts
at its first todo!() inside an extern "C" shim. The layout test passes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
video_frame_init and video_frame_copy, plus the safe core
(obs_media_io::video_frame) that later media-io ports use: the linesize
and plane-height tables, FrameLayout (where init puts each plane in its
one bmalloc block), copy_extent / copy_plane, and an owned VideoFrame.
video-frame.c joins the ENABLE_RUST_LIBOBS guard. No intentional
differences.

The C switches become exhaustive matches, and the shims map a format
outside the enum to VIDEO_FORMAT_NONE, which is what the C's default
does. The u32 linesize math wraps as in C (width * 4, HALF(UINT32_MAX),
and V210's ALIGN by 48 with a power-of-two mask), and so does the u32
linesize * rows length of an equal-linesize copy. Plane sizes and
offsets are usize, as the C's size_t. FrameLayout records the C's
offsets[i - 1] rule, so a plane after a skipped one starts at offset 0.
video_frame_init calls the real bmalloc with the layout's size, so a
zero-size frame still crashes in bmalloc(0), as test_video_frame.c's
frame_init_none requires.

Mutation check: dropping HALF's rounding, moving each plane one byte
past the previous one, copying one byte more per row into a wider dst,
and rounding V210 to a true multiple of 48 each fail Tier 1 and parity.
Making the equal-linesize copy length usize fails Tier 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=9f2175734fa63710b346cbb1fb7c9f8dea29343d secs=248 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791683468,"gate":"rust/all/fmt","host":"linux-x86_64","key":"e8a0f7af274ad886cbd562c503aab0d219ce962aac5a29651d46b98d19f32ee1","lane":"lint","parents":["07c2c68433edecd5f8b64d9db5c9ccf866304df0"],"secs":115,"stamp":"d52ce43fa01631865319bb175950a38a","tree":"9f2175734fa63710b346cbb1fb7c9f8dea29343d","v":1,"via":"run"}
Ci-Attestation: {"at":1791683468,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"e8a0f7af274ad886cbd562c503aab0d219ce962aac5a29651d46b98d19f32ee1","lane":"lint","parents":["07c2c68433edecd5f8b64d9db5c9ccf866304df0"],"secs":115,"stamp":"822a86d6e9d209a47d1f48905a2b76ac","tree":"9f2175734fa63710b346cbb1fb7c9f8dea29343d","v":1,"via":"run"}
Ci-Attestation: {"at":1791683468,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"fdfc27a308eb2752ec54bce9502273a44ef009ea151ec5a71f9209412b1f99f3","lane":"rust-test","parents":["07c2c68433edecd5f8b64d9db5c9ccf866304df0"],"secs":55,"stamp":"991aaa2f7b5ff40bfc59fdd7c8c03d27","tree":"9f2175734fa63710b346cbb1fb7c9f8dea29343d","v":1,"via":"run"}
Ci-Attestation: {"at":1791683468,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4f0a44c71280f272d6a5d52a74c6417ffb7febaf08408be7379c3942e5d4fb06","lane":"tier2-linux","parents":["07c2c68433edecd5f8b64d9db5c9ccf866304df0"],"secs":77,"stamp":"9ab70169229d9c18172ece7f0a604322","tree":"9f2175734fa63710b346cbb1fb7c9f8dea29343d","v":1,"via":"run"}
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.

rust: Port media-io/video-frame.c to obs-media-io

2 participants