Skip to content

rust: Port media-io/format-conversion.c to obs-media-io - #184

Merged
zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/format-conversion
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/format-conversion

Conversation

@swilk19

@swilk19 swilk19 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #183

What and why

Ports the six functions libobs/media-io/format-conversion.c exports (compress_uyvx_to_i420, compress_uyvx_to_nv12, convert_uyvx_to_i444, decompress_420, decompress_nv12, decompress_422) to obs-media-io (obs_media_io::format_conversion). This is item 6 of the media-io part 2 order on #45 (#45 (comment)), claimed on #183. No intentional differences.

With ENABLE_RUST_LIBOBS=ON, libobs builds without format-conversion.c and exports the Rust shims under the same names, listed in rust-exports.d/obs-media-io-format_conversion.txt.

Scalar, byte-exact. The C's SSE2 is integer only, and every lane reduces to plain integer math: byte extraction, and (a + b + c + d) >> 2 for 2x2 chroma. So the core is scalar with no core::arch, and parity is exact byte equality with no ULP allowance. Offsets use the C's u32 arithmetic, wrapping included. Each loop keeps the C's order of reads and writes, which decides the result where output rows overlap. uint16_t/uint32_t values are stored native-endian, as in the C. That includes the I420 chroma, which the C packs into one uint32_t and splits into two uint16_t stores.

Planes as traits. The safe core takes its planes as Source/Sink traits, addressed by byte offset. Byte slices implement both and panic where the C would read or write out of bounds. The shims implement them over the caller's raw pointers, so the shims convert nothing and the core sees the C's offsets. Each shim reads only the output/input and linesize entries the C reads.

Oracle __m128i stand-ins. As noted on #45, the oracle's sse-shim/simde/x86/sse2.h had only the float ops for non-x86. This PR adds the integer ops format-conversion.c uses, lane by lane: _mm_load_si128, _mm_set1_epi32/epi16, _mm_and_si128, _mm_srli_si128, _mm_packs_epi32, _mm_packus_epi16, _mm_add_epi64, _mm_shuffle_epi32, _mm_shufflelo_epi16 and _mm_srai_epi16. __m128i stores its bytes as uint32_t, because the C reads lanes with *(uint32_t *)&val.

The oracle builds format-conversion.c twice:

  • oracle_* uses native SSE2 on x86.
  • oracle_scalar_* (oracle/format_conversion_scalar.c) defines OBS_ORACLE_SCALAR_SSE, so it uses the stand-ins on x86 too.

Parity compares four implementations: C, C with the stand-ins, the Rust shim and the safe core. That way the x86 jobs check the stand-ins before the arm64 job relies on them.

Characterized, not endorsed (pinned in the cmocka test and kept by the port):

  • compress_uyvx_to_i420 addresses the V plane with out_linesize[1].
  • convert_uyvx_to_i444 addresses all three planes with out_linesize[0].
  • decompress_420 and decompress_nv12 round start_y and end_y down to row pairs.
  • decompress_420 takes its width from in_linesize[0] alone.
  • decompress_422 reads min(in_linesize, out_linesize) / 2 dwords per row, which is twice the row, and writes twice as many. Each row spills into the next, and the next row overwrites the spill.

RED → GREEN

  • Characterization: 61bfc212d adds test/cmocka/test_format_conversion.c (tests.d/test_format_conversion.cmake). Every buffer is canary-filled and compared whole. It passes OFF and ON before any Rust.
  • RED: dc583bee0. Against the todo!() core, format_conversion fails 22/22 and format_conversion_parity fails 8/8.
  • GREEN: 9e5171627. format_conversion passes 22/22 and format_conversion_parity 8/8. Tier 2 Linux: 53/53 with ENABLE_RUST_LIBOBS OFF and ON, test_format_conversion included, and libobs exports are identical (1752 symbols).

How has this been tested?

  • Tier 1, tests/format_conversion.rs: ports test_format_conversion.c case for case. Edge cases:
    • zero widths
    • chroma extremes (four 255s average to 255)
    • overlapping output rows following the C's write order
    • a u32 row-offset wrap (row 2 of a 2^31 linesize starts at 0)
    • a short output slice panicking
  • Tier 3, tests/format_conversion_parity.rs: proptest, 512 cases per function, over linesizes, row ranges (including empty and odd) and random contents, plus the cmocka inputs and the u32 wrap.
    • Buffers are 16-byte aligned and sized to exactly the bytes a call touches, plus canary slack.
    • Outputs are compared whole, so writes outside the expected area show up.
    • The excluded inputs are listed in the file header: misaligned loads and stores, which are UB in the C; loops that wrap past u32::MAX; and overlapping planes.
  • Mutation check: each of these breaks the core and fails parity: halving the chroma divisor, swapping decompress_420's row write order, changing decompress_422's luma duplication, and changing the I420 chroma packing. Breaking _mm_packus_epi16, _mm_shuffle_epi32, _mm_srai_epi16 or _mm_shufflelo_epi16 in the stand-ins fails parity as CScalar differs from C on x86. On arm64, the _mm_shuffle_epi32 break fails as Safe differs from C.
    • Note for anyone repeating this: soldr's rustc cache can reuse a stale obs-c-oracle rlib when only oracle C changes. Use SOLDR_RUSTC_WRAPPER=none, or also touch an oracle .rs file.
  • Linux x86_64 (WSL2): ./lint, ./unittest, and the stamped head.
  • Windows MSVC (x86_64-pc-windows-msvc) via cargo-xwin: all obs-media-io tests pass, including format_conversion 22/22 and format_conversion_parity 8/8. The oracle there is native SSE2 for both builds, and cmocka Tier 2 wasn't run locally.
  • macOS 26.7.1 on Apple Silicon (Xcode 26.5, Apple clang 21), where both oracle builds use the stand-ins:
    • Workspace tests: 742 passed, 0 failed. Clippy and fmt are clean.
    • The focus tests pass in debug and release.
    • rust/tools/tier2-validate/validate.sh passes OFF and ON (48/48 each, test_format_conversion included) with identical exports (1863 symbols).

Checklist

🤖 Generated with Claude Code

@swilk19
swilk19 requested a review from zackees as a code owner October 11, 2026 02:25
swilk19 and others added 3 commits October 10, 2026 19:35
Pins the current behaviour of the six functions media-io/format-conversion.c
exports. Nothing in the tree calls them, so this is their only C coverage
and the Tier 2 check for the port (obs-rust#183).

Every buffer starts filled with a canary byte and each test compares the
whole buffer, so writes outside the expected area fail too. Inputs follow
the functions' contracts: 16-byte-aligned UYVX rows with in_linesize a
multiple of 16 and widths a multiple of 4, and uint16_t/uint32_t-aligned
linesizes for decompress_*.

Characterized, not endorsed:
- compress_uyvx_to_i420 addresses the V plane with out_linesize[1];
  convert_uyvx_to_i444 addresses all three planes with out_linesize[0].
- decompress_420 and decompress_nv12 round start_y and end_y down to row
  pairs, so start_y 1, end_y 3 writes rows 0 and 1.
- decompress_420 takes its width from in_linesize[0] alone.
- decompress_422 reads min(in_linesize, out_linesize) / 2 dwords per row,
  twice the row, and writes twice as many dwords, so each row spills into
  the next.

Refs obs-rust#183

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 1 and Tier 3 parity tests for the six functions
media-io/format-conversion.c exports, against a todo!() core.

The safe core takes its planes as Source/Sink traits addressed by byte
offset. Byte slices implement both and panic where the C would read or
write out of bounds; the shims implement them over the caller's raw
pointers, so they convert nothing and the core sees the C's offsets.
Each shim reads only the output/input and linesize entries the C reads.

The oracle builds format-conversion.c twice. oracle_* uses native SSE2
on x86. oracle_scalar_* defines OBS_ORACLE_SCALAR_SSE, so even on x86
it uses the plain-C __m128i intrinsics this commit adds to the oracle's
sse-shim (_mm_load_si128, _mm_set1_epi32/epi16, _mm_and_si128,
_mm_srli_si128, _mm_packs_epi32, _mm_packus_epi16, _mm_add_epi64,
_mm_shuffle_epi32, _mm_shufflelo_epi16, _mm_srai_epi16), which non-x86
builds use for both. Parity compares all four implementations byte for
byte, so the x86 jobs check the stand-ins before the arm64 job relies on
them.

Parity buffers are 16-byte aligned, sized to exactly the bytes a call
touches plus canary slack, and compared whole. The test file lists the
excluded inputs: misaligned loads and stores, loops that wrap past
u32::MAX, and overlapping planes.

RED: soldr cargo test -p obs-media-io --test format_conversion fails 22
of 22, --test format_conversion_parity fails 8 of 8 (not yet
implemented).

Refs obs-rust#183

Co-Authored-By: Claude Opus 5.5 <[email protected]>
compress_uyvx_to_i420, compress_uyvx_to_nv12, convert_uyvx_to_i444,
decompress_420, decompress_nv12 and decompress_422 move to Rust. With
ENABLE_RUST_LIBOBS=ON libobs builds without media-io/format-conversion.c
and exports the Rust shims under the same names. No intentional
differences.

The C's SSE2 is integer only, and every lane reduces to byte extraction
or a (a + b + c + d) >> 2 chroma average, so the core is scalar with no
core::arch and parity is byte-exact. Offsets use the C's u32 arithmetic,
wrapping included, and each loop keeps the C's order of reads and writes,
which decides the result where output rows overlap. uint16_t/uint32_t
values are stored native-endian as in the C, including the I420 chroma
the C packs into one uint32_t and splits into two uint16_t stores.

Mutation checks: halving the chroma divisor, swapping decompress_420's
row write order, changing decompress_422's luma duplication and the I420
chroma packing each fail parity. Breaking _mm_packus_epi16,
_mm_shuffle_epi32, _mm_srai_epi16 or _mm_shufflelo_epi16 in the oracle's
plain-C sse-shim fails parity as "CScalar differs from C" on x86.

Local gate: rust-test and tier2-linux pass (test_format_conversion passes
OFF and ON; exported symbols identical, 1752). obs-media-io tests also
pass for x86_64-pc-windows-msvc via cargo-xwin.

Fixes obs-rust#183

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=e2a36762c7a5157c323e2754690ab6ba7213dcfa secs=150 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791686476,"gate":"rust/all/fmt","host":"linux-x86_64","key":"05801aa46783e55e6e04153c3347c6417abd35835e74561121b5cb9604ea5039","lane":"lint","parents":["07e455b799ee5447efaa23ccab9600dc6da880bf"],"secs":31,"stamp":"44a6d04f5eeaa235b4dd078dbaf7578c","tree":"e2a36762c7a5157c323e2754690ab6ba7213dcfa","v":1,"via":"run"}
Ci-Attestation: {"at":1791686476,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"05801aa46783e55e6e04153c3347c6417abd35835e74561121b5cb9604ea5039","lane":"lint","parents":["07e455b799ee5447efaa23ccab9600dc6da880bf"],"secs":31,"stamp":"771bc902bc2e221cfb589516aa23773f","tree":"e2a36762c7a5157c323e2754690ab6ba7213dcfa","v":1,"via":"run"}
Ci-Attestation: {"at":1791686476,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"d3822cf976ffc59edf870d9b5a02c2b1d2bc85be9b739e0035b8b38881bde94f","lane":"rust-test","parents":["07e455b799ee5447efaa23ccab9600dc6da880bf"],"secs":38,"stamp":"17639da348f0ad7ca81887a113cfa0c4","tree":"e2a36762c7a5157c323e2754690ab6ba7213dcfa","v":1,"via":"run"}
Ci-Attestation: {"at":1791686476,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"33a162da37eb409489461f876489e604e62c9e2da8e8281e6d484002c7c5e56a","lane":"tier2-linux","parents":["07e455b799ee5447efaa23ccab9600dc6da880bf"],"secs":81,"stamp":"cbc3595ea12a68316fc9465a0037c43a","tree":"e2a36762c7a5157c323e2754690ab6ba7213dcfa","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is 5ad7973cd.

  • Characterization (61bfc212d): Tier 2 passed 53 of 53 with ENABLE_RUST_LIBOBS OFF and 53 of 53 with it ON. The C still builds in both, so test_format_conversion describes the original.
  • RED (dc583bee0): tests/format_conversion.rs failed 22 of 22 and tests/format_conversion_parity.rs failed 8 of 8 against the stub.
  • GREEN (9e5171627): 22 of 22 and 8 of 8 passed, and export_list passed 3 of 3.
  • Local gate on the rebased head: lint, rust-test and tier2-linux all passed. tier2-linux covers cmocka with format-conversion.c swapped out and the OFF/ON exported symbol check.

The RED run leaves a format_conversion_parity.proptest-regressions file in the working tree. I didn't commit it. Merging once CI is green.

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 libobs/media-io/format-conversion.c to obs-media-io

2 participants