Skip to content

rust: Port media-io/video-matrices.c to obs-media-io - #106

Merged
zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/video-matrices
Oct 10, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/video-matrices

Conversation

@swilk19

@swilk19 swilk19 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Description

Ports libobs/media-io/video-matrices.c (video_format_get_parameters, video_format_get_parameters_for_format) to obs-media-io. With ENABLE_RUST_LIBOBS=ON libobs builds without media-io/video-matrices.c and exports the Rust shims under the same names. No intentional differences.

No lazy table. The C fills a table of every matrix (3 colorspaces × 9 bit depths × 2 ranges) on first use, behind an unsynchronized static bool, so two threads making the first call race on it. The port has no shared state. Each call computes only the requested matrix, using the C's f32 operations in the same order. The black-level offsets go through obs_graphics::vec3::Vec3::rotate, which sums lanes in the SSE vec3_dot order (#75). Parity against the C oracle is bit-exact, so no ULP tolerance is needed.

Enums at the C boundary. enum video_colorspace and enum video_range_type cross the C ABI as c_ints, like enum video_format in #97. This adds the checked *_from_c conversions that #97 anticipated, plus video_format_from_c over VideoFormat::ALL. A colorspace outside its enum returns false and writes nothing, as in C. video-matrices.c only compares range with VIDEO_RANGE_FULL, and gives more than 8 bits only to the formats it lists. So the shims convert out-of-enum range and format values to VIDEO_RANGE_PARTIAL and VIDEO_FORMAT_NONE, which behave the same. The parity sweep covers those values.

Oracle. oracle/video_matrices.c includes graphics_math_names.h, so the oracle's vec3_rotate call resolves to the oracle vec3.c rather than the Rust port. The original source fills its matrix tables lazily, which -Wextra reports as -Wmissing-field-initializers. The wrapper silences that one warning around the #include with a GCC/Clang pragma; the source itself is unmodified. obs-media-io now depends on obs-graphics.

Motivation and Context

The second of the two files claimed on #45, after video-fourcc.c (#97). Unblocked by #75 (vec3).

Refs #45

How Has This Been Tested?

Linux x86_64 (WSL2), Rust via soldr.

  • Commits follow the policy: test/cmocka/test_video_matrices.c (C characterization, passes OFF and ON before any Rust), then RED (todo!() core: Tier 1 fails, parity aborts in the shim, layout and export_list pass), then GREEN.
  • The cmocka test pins the Rec. 601, 709 and 2100 PQ matrices at 8, 10, 12 and 16 bits, in partial and full range. It also covers the DEFAULT/sRGB → 709 and HLG → PQ aliases, the bit depth each format and colorspace selects, NULL range pointers, and an unknown colorspace returning false without writing. Matrix entries are compared with a 1e-5 tolerance, so a platform compiler that fuses multiply-adds cannot fail it. Range limits are single divisions and are compared exactly.
  • soldr cargo test -p obs-media-io: Tier 1 (1:1 port of the cmocka cases plus edge cases), Tier 2 layout for both new enums, and Tier 3 parity. Parity sweeps every enumerator of the three enums plus two values on each side of each, with all four combinations of null range pointers. It compares the return value and every written float bit for bit against sentinel-filled buffers, which also checks that nothing is written on failure. The safe core is checked against the oracle directly too, and proptest adds arbitrary c_ints. The sweep covers every cmocka input.
  • Mutation check: reordering Kg = 1 - Kb - Kr to 1 - Kr - Kb (mathematically equal, differs in the last bit) fails 3 of the 5 parity tests. Dropping R10L from the 10-bit formats fails 2. Both reverted.
  • ./lint and ./unittest: ok. tier2-linux ran test_video_matrices with ENABLE_RUST_LIBOBS OFF and ON (36/36 each). The libobs exported symbols are identical OFF vs ON (1752). HEAD carries the local-gate stamp.
  • macOS and Windows were not run locally; the Rust Tests and rust-tier2 CI jobs cover them. video_format_get_parameters and video_format_get_parameters_for_format are added to rust-exports.txt.

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

@zackees
zackees force-pushed the port/video-matrices branch 4 times, most recently from 2233346 to 9f0379e Compare October 10, 2026 18:24
swilk19 and others added 3 commits October 10, 2026 12:44
Pins video_format_get_parameters and
video_format_get_parameters_for_format from media-io/video-matrices.c
before the Rust port: the Rec. 601, 709 and 2100 PQ matrices at 8, 10,
12 and 16 bits in partial and full range, the DEFAULT/sRGB -> 709 and
HLG -> PQ aliases, the bit depth each format and colorspace selects,
NULL range pointers, and an unknown colorspace returning false without
writing. Range values other than VIDEO_RANGE_FULL, and format values
outside the enum, are characterized as partial range and 8 bits.

Matrix entries are compared with a 1e-5 tolerance so a compiler that
fuses multiply-adds does not fail the test. Range limits are single
divisions and are compared exactly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 1, Tier 2 layout and Tier 3 parity tests for
video_format_get_parameters and video_format_get_parameters_for_format
against a todo!() core. enum video_colorspace and enum video_range_type
cross the C ABI as c_ints, like enum video_format; the safe enums keep
the header's values as discriminants, and the layout test checks each
enum's size and alignment and every enumerator's value against the
oracle.

The shims return false without writing for a colorspace outside the
enum, as the C does. video-matrices.c only tests the range against
VIDEO_RANGE_FULL and lists the formats it gives more than 8 bits, so
range and format values outside their enums convert to
VIDEO_RANGE_PARTIAL and VIDEO_FORMAT_NONE, which behave the same.

The oracle wraps video-matrices.c with graphics_math_names.h, so its
vec3_rotate call resolves to the oracle vec3.c rather than the Rust
port. obs-media-io now depends on obs-graphics for Vec3 and Matrix3.

Parity sweeps every enumerator plus two values on each side of each
enum, with every combination of null range pointers, and compares the
return value and every written float bit for bit against
sentinel-filled buffers. Proptest adds arbitrary c_ints.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
video_format_get_parameters and video_format_get_parameters_for_format
move to Rust. With ENABLE_RUST_LIBOBS=ON libobs builds without
media-io/video-matrices.c and exports the Rust shims under the same
names. No intentional differences.

The C fills a table of every matrix on first use behind an
unsynchronized static bool, so two threads making the first call race
on it. The port has no shared state: each call computes the requested
matrix with the C's f32 operations in the same order, and the
black-level offsets go through obs-graphics' Vec3::rotate, which sums
in the SSE vec3_dot order. Parity against the oracle is bit-exact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=4d942c109ba49365d605b7094f8bdb2358afec1c secs=112 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791661649,"gate":"rust/all/fmt","host":"linux-x86_64","key":"b86babd2e461fed5dec346f4701b477b6950d2956293ed4b7657c3d7a95b6c97","lane":"lint","parents":["6489bbb0cdb7d8ec39840dcd6b7c23fa10570a0a"],"secs":37,"stamp":"c25ca84e9eebd9052748e0915fe19ba8","tree":"4d942c109ba49365d605b7094f8bdb2358afec1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791661649,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"b86babd2e461fed5dec346f4701b477b6950d2956293ed4b7657c3d7a95b6c97","lane":"lint","parents":["6489bbb0cdb7d8ec39840dcd6b7c23fa10570a0a"],"secs":37,"stamp":"ea23036547197f79cb142c88ab78222e","tree":"4d942c109ba49365d605b7094f8bdb2358afec1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791661649,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"2afd643f4c2eeac9268a8927c80c2c561c50f1c33ea519cd91b01f657db5161f","lane":"rust-test","parents":["6489bbb0cdb7d8ec39840dcd6b7c23fa10570a0a"],"secs":25,"stamp":"16ad262f58e4bd3359fda105b7d4a4d0","tree":"4d942c109ba49365d605b7094f8bdb2358afec1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791661649,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"f9e75b65a773a6c527b93a5b6f1a8e8e4637877e3e37c212f4c0778157af1faf","lane":"tier2-linux","parents":["6489bbb0cdb7d8ec39840dcd6b7c23fa10570a0a"],"secs":50,"stamp":"e4aae9753fd797891df03705efe6ba18","tree":"4d942c109ba49365d605b7094f8bdb2358afec1c","v":1,"via":"run"}
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master (after #49, #59 and #78), keeping both sides in the oracle build.rs and src/lib.rs. RED→GREEN, checked with soldr's cache disabled: obs-media-io fails 10/12 at the RED commit and all tests pass at head. The C test already avoids fail_msg (no cm_print_error, so Windows links). The local gate passes and is stamped.

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.

2 participants