Skip to content

obs-source: Port the source colour space query routing - #1990

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-color-space
Oct 23, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-color-space

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1979

What and why

Ports the colour space query routing of obs_source_get_color_space() in libobs/obs-source.c.h to rust/obs-source, as a new module color_space next to audio_timing (#1936) and deinterlace_pacing (#1954). Every source and filter is asked its colour space when it renders, and the routing decides who answers: the source's own callback, an async source's frame, or the next source down the filter chain. #1493 fixed this for real users: an audio filter on an HDR source answered GS_CS_SRGB for the whole chain, so HDR Tone Mapping above it saw SDR. That rule is kept exactly, pinned in the characterization, and checked by a mutant that reverts it.

Six commits:

  1. Move (behaviour-neutral): the decisions go to libobs/obs-source-color-space.c.h as two pure functions, declared in the new obs-source-color-space.h without EXPORT, so they are hidden:

    • obs_source_color_space_route(): which source answers, with the C's rules in the C's order. A non-filter with no video asks its filter parent. A filter with no video asks its target (libobs: Fix color space query through non-video filters #1493). A source with no data, or a disabled one, asks its target. An async source picks. Anything else asks its callback.
    • obs_source_color_space_pick(): the async pick, the same loop. It returns the first preferred space equal to the frame's space, else the last preferred space, else (with an empty list) the frame's space.

    obs_source_get_color_space() keeps data_valid(), and reads info.type, info.output_flags, context.data != NULL, enabled, filter_parent != NULL and filter_target != NULL into struct obs_source_color_space_node, passed by value. It follows the route itself: the recursion into the parent or the target, convert_video_space() of async_format and async_trc (the result is passed to the pick), and video_get_color_space or GS_CS_SRGB, with the assert, as before. Colour spaces cross as int, so the header needs no graphics header. obs-source.c.h checks at compile time that enum gs_color_space and enum obs_source_type are int-sized, and that the header's filter type and VIDEO and ASYNC flags match obs-source.h. The file stays in libobs.core-source for this commit.

  2. Characterization: test_source_color_space_steps, 12 cases, compiles the moved file. It covers:

    • every route for every combination of the inputs. The six fields give seven inputs, because output_flags carries both VIDEO and ASYNC, so the table has 128 rows (the 64 combinations of the other six inputs, for a filter and for a non-filter). Each row's route is written out;
    • only OBS_SOURCE_TYPE_FILTER counts as a filter (transitions, scenes and out-of-enum types route as inputs), and only the VIDEO and ASYNC bits count;
    • the chains of test_filter_color_space and libobs: Fix color space query through non-video filters #1493;
    • a filter that is also flagged async;
    • the pick with an empty list (NULL, or a count of 0), with a match first, last or in the middle, with no match, and with values outside enum gs_color_space.

    It is in the core suite of the test graph, so no pinned suite count changes.

  3. RED: Tier 1 in the crate against todo!(). 17 of 17 fail.

  4. GREEN: the safe core and #[no_mangle] shims, in a new libobs.ported-source-color-space group with its own PCH (only the new header). This commit updates the export lists, source-sets.json, the unity lists and ci/test_ported_sources.py, and adds a _rust variant of the steps test (see below).

  5. Tier 3: a C oracle and parity, with the mutation check.

  6. Docs: the README rows, the crate's description and lib.rs, and a port_comparison bench file. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source.

  • What changed in the C. obs_source_get_color_space() now reads the six fields into the node, then switches on the route. The route's checks are the same expressions, in the same order. The pick's loop is unchanged; it reads the list as int (a cast of the enum gs_color_space pointer, checked to be the same size) and its result is cast back.

  • Kept, characterized, not endorsed:

    • a filter flagged async picks from its own async frame's space unless an earlier rule sends it to its target. Filters have no async frames, so in libobs that is GS_CS_SRGB;
    • with no data and nowhere to go, the route is the callback or the pick. data_valid() runs first, so libobs never gets there;
    • a value outside enum gs_color_space in the preferred list is compared and returned as it is.
  • Intentional difference: a null preferred list with a count above 0 is read as empty, so the pick returns the frame's space (the C reads through the null pointer). libobs passes NULL only with a count of 0, which both sides read as empty. The difference is listed in the parity test header and in rust/README.md.

  • The _rust variant links and runs the shims. test_source_color_space_steps_rust builds the unchanged test source against libobs_rust-static, as test_source_audio_timing_steps_rust does. Unlike obs-output: Port the encoder ROI checks and scaling #1974, where the test only called the exported API, this test calls the two hidden functions directly. So the variant runs the shims, including the node passed by value from C. It passes 12 of 12 on the hybrid tree.

  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets. The two functions are local (t) in all three. In the hybrid they come from liblibobs_rust.a (the group's object is not on the hybrid link line), and the ported group is compiled only into libobs-c-reference.

  • Performance (port_comparison, x86_64, GCC, not pinned; median per 1024 calls):

    • route: 2.05 µs Rust, 2.26 µs C (0.91x);
    • async_pick: 2.21 µs Rust, 2.69 µs C (0.82x; 2.54 µs Rust in an earlier run).

    That is about 2 ns per call either way, within the run's noise.

  • No user-visible change, so no docs/features.md line.

Observed, not changed (upstream behaviour, nothing unsafe):

  • The parent rule applies only to a non-filter source that has a filter_parent. obs_source_filter_add() does not check the filter's type, so this happens only when a non-filter source is added as a filter through the API. It is pinned, not relied on.

RED → GREEN

  • RED commit: 064baf1d7. soldr cargo test -p obs-data --test obs-tests-core source::color_space fails 17 of 17, every failure "not yet implemented": the 12 ported steps_* cases and 5 edge_* cases, three of them proptests. This was checked again on the rebased commit.
  • GREEN: f50a43724. Tier 1 passes 17/17. Tier 2 ran under a private Xvfb -noreset with dbus-run-session, a fresh HOME and ulimit -c 0, with no TMPDIR set:
Test C backend hybrid libobs-c-reference
test_filter_color_space 5/5 5/5 5/5
test_source_color_space_steps 12/12 12/12 12/12
test_source_color_space_steps_rust (not built: needs the Rust archive) 12/12 12/12
full ctest 399/399 405/405 (not run)

All of these ran on the head, 19c6c25cb, on 01843e7c5. The targeted tests also passed on all three builds before the rebase (GREEN on 92a52ff7a). No ctest test was skipped. Inside the tests, three cases skip on each lane, as on master: open_crashes_without_obs_core in test_audio_io and test_video_io, and test_hex_float_values_ignore_comma_locale, which needs a comma-decimal locale this host does not have. Each c-ref run checked that the loader used libobs-c-reference/libobs.so.30, which was rebuilt (ninja libobs-c-reference) before every c-ref run.

  • Tier 3, 452e4ca56 (source::color_space_parity, 7 tests). Each run compares the route's value and the picked space. Runs:

    • the node's layout, the route values and the type and flag constants, against the C header;
    • the route over every combination of the VIDEO and ASYNC flags, data, enabled, filter parent and filter target, with eight types (one filter, seven not, so all 128 rows) and sixteen sets of other flags: 8,192 nodes. A proptest covers any i32 type, any u32 flags and the four bools (20,000 cases);
    • the pick over every list of 0 to 5 values from -1..=4 against each frame space: 55,986 picks. A proptest covers lists of 0 to 8 arbitrary i32 values, with the frame's space drawn from the list half the time (20,000 cases). Neither uses a float strategy;
    • a count of 0 on both sides, with a null pointer and without one, and the intentional difference on the shim.

    The parity module passed 20 runs in a row with fresh seeds, and no regression file came up.

  • Mutation check (once, not committed; the two test modules in obs-tests-core): 32 of 33 mutants fail, and the one that survives is equivalent.

Mutant Tier 1 (of 17) Tier 3 (of 7)
parent: rule removed / filters ask it too / sources with video ask it too / asked without one 4 / 5 / 4 / 5 2 / 2 / 2 / 2
parent: after the disabled rule 4 2
#1493 reverted (filter rule removed) 5 2
#1493: any source without video asks its target / filters with video ask it / asked without a target 3 / 5 / 5 2 / 2 / 2
disabled: rule removed / data only / enabled only / both needed / asked without a target 5 / 5 / 4 / 5 / 7 2 / 2 / 2 / 2 / 2
async: rule removed / VIDEO bit instead / before the disabled rule / filters never pick 6 / 7 / 4 / 3 2 / 2 / 2 / 2
filter: any non-input type 2 2
video: any flag counts 7 2
fallback: pick instead of callback 6 2
pick: no break / frame space on no match / first entry on no match 6 / 3 / 3 2 / 2 / 2
pick: empty list gives GS_CS_SRGB / last entry skipped / stops one late 3 / 5 / 6 4 / 2 / 2
shim: parent and target routes swapped / parent and target flags swapped / type read as 0 / count less one 0 (Tier 1 does not reach the shims) 2 each
shim: null check dropped 0 aborts (from_raw_parts precondition, in pick_null_list_is_empty_in_the_shim)
equivalent: has_data and enabled swapped in the shim (the route reads them only as !has_data || !enabled) 0 0
  • Real app: no UI, protocol or logging change.

Other runs:

  • uv run ci/local_gate.py --test-diff origin/master (the CI changed-crates step) on the head 19c6c25cb: 2959/2959 passed, 1 skipped (an existing ignored test).
  • uv run ci/local_gate.py --lane lint: ok. --lane cross: ok. Both ran on 19c6c25cb. cargo clippy -p obs-source -p obs-c-oracle -p obs-data --all-targets -D warnings is clean.
  • ci/test_build_groups.py, ci/test_ported_sources.py and ci/test_oracle_standins.py: OK on the head.
  • Exported symbols: 1766 on all three builds, identical sets, checked on 19c6c25cb.
  • soldr cargo test -p obs-data --test obs-tests-core source::: 110/110.
  • Rebased once, onto 01843e7c5. Only the port_comparison bench list conflicted: obs-output: Port the encoder ROI checks and scaling #1974 added encoder_roi, and both entries are kept. After the rebase, source-sets.json, test-graph.json, the unity lists, libobs.pch.h and the three export lists were compared by hand against master: only this PR's lines differ.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite (the lint and cross lanes and --test-diff passed locally; nothing is stamped)
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

…ace.c

Behaviour-neutral extraction before the Rust port. The decisions of
obs_source_get_color_space() in obs-source.c.h move to
libobs/obs-source-color-space.c.h as two pure functions, declared in the
new libobs/obs-source-color-space.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_color_space_route(): which source answers. A non-filter
  source with no video asks its filter parent; a filter with no video asks
  its target (obs-rust#1493); a source with no data, or a disabled one, asks its
  target; an async source picks; any other asks its callback. The checks
  run in the same order as before.
- obs_source_color_space_pick(): the async pick, the same loop: the first
  preferred space equal to the frame's space, else the last preferred
  space, else (none) the frame's space.

obs_source_get_color_space() keeps data_valid(), reads info.type,
info.output_flags, context.data != NULL, enabled, filter_parent != NULL
and filter_target != NULL into struct obs_source_color_space_node, and
follows the route: the recursion into the parent or the target,
convert_video_space() of async_format and async_trc (passed to the pick),
and video_get_color_space or GS_CS_SRGB, with the assert, as before.

The colour spaces cross as int, so the header needs no graphics header.
obs-source.c.h checks at compile time that enum gs_color_space and enum
obs_source_type are int-sized, and that the header's filter type and VIDEO
and ASYNC flags are OBS_SOURCE_TYPE_FILTER, OBS_SOURCE_VIDEO and
OBS_SOURCE_ASYNC. The obs-rust#1493 comment moves with its check.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#1979
test_source_color_space_steps compiles libobs/obs-source-color-space.c.h
(ASan and UBSan, no libobs core) and calls only its two hidden functions,
so it runs on every platform. 12 cases:

- obs_source_color_space_route(), every combination of the inputs: a
  filter or not, the VIDEO and ASYNC flags (output_flags carries both, so
  the six fields are seven inputs and 128 rows), data, enabled, a filter
  parent and a filter target. Each row's route is written out.
- Only OBS_SOURCE_TYPE_FILTER is a filter: transitions, scenes and values
  outside the enum route as inputs, over all 64 input rows. Only the VIDEO
  and ASYNC bits count: every other flag set, or OBS_SOURCE_AUDIO, changes
  no route.
- The chains of test_filter_color_space and obs-rust#1493: the HDR async source
  picks; an audio filter above it asks its target, enabled or not; a
  video filter asks its callback, and its target when disabled or without
  data; a detached audio filter asks its callback; an audio-only input
  asks its filter parent before its target.
- obs_source_color_space_pick(): no preferred spaces (NULL, or a count of
  0) gives the frame's space; a match first, last or in the middle stops
  there, a later match included; no match gives the last preferred space,
  with the HDR Tone Mapping filter's list among them.

Characterized, not endorsed:
- a filter flagged async picks from its own async frame's space, unless
  obs-rust#1493's rule (no VIDEO) or the disabled rule sends it to its target;
  filters have no async frames, so in libobs that is GS_CS_SRGB;
- with no data and nowhere to go the route is the callback or the pick
  (obs_source_get_color_space() checks data_valid() first, so libobs
  never gets there);
- a value outside enum gs_color_space in the list is compared and
  returned as it is.

It is in the native test graph, in the core suite. The real-libobs
test_filter_color_space is unchanged.

Refs obs-rust#1979
RED for the port of libobs/obs-source-color-space.c.h to the color_space
module of rust/obs-source, next to audio_timing and deinterlace_pacing.
The safe core is a stub (todo!()): route() over a Node (the source's
type, output flags, and whether it has data, is enabled and has a filter
parent and a filter target) giving a Route (AskParent, AskTarget,
AsyncPick, Callback), and pick() of the frame's space from a slice of
preferred spaces, one per hidden C function, with TYPE_FILTER, VIDEO and
ASYNC.

tests/source/color_space.rs ports test_source_color_space_steps 1:1: its
12 cases under the same names (steps_*), in the same order, with the same
inputs and expectations, the 128-row route table and the
characterized-not-endorsed cases included.

Edge cases (edge_*): the route's invariants over every row and four
types (a filter never asks its parent, nothing asks a parent or target it
does not have, an enabled video source with data asks no one, only ASYNC
picks); obs-rust#1493's rule with any other flags; and three proptests (4000
cases each): any type but OBS_SOURCE_TYPE_FILTER routes as an input and
only the VIDEO and ASYNC bits count, over any i32 type and any u32 flags;
the pick against a model written another way (the frame's space when the
list is empty or holds it, the last entry otherwise) over lists of 0 to 8
values; and a list holding the frame's space anywhere gives it back.

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as audio_timing's are.

soldr cargo test -p obs-data --test obs-tests-core source::color_space
fails 17 of 17 with "not yet implemented".

Refs obs-rust#1979
Ports libobs/obs-source-color-space.c.h, the colour space query routing
of obs_source_get_color_space(), to the color_space module of
rust/obs-source. obs_source_get_color_space() stays C in obs-source.c.h:
data_valid(), the field reads into the node, the recursion into the
filter parent or target, convert_video_space() of the async frame's
format and transfer, and the video_get_color_space callback (GS_CS_SRGB
without one). The hybrid libobs drops the C file and links the Rust shims
in its place.

Safe core (src/color_space.rs): route() tries the C's rules in the C's
order: a non-filter without VIDEO and with a filter parent asks the
parent; a filter without VIDEO and with a target asks the target
(obs-rust#1493's rule, kept exactly); a source without data, or disabled, with a
target asks the target; an ASYNC source picks; anything else asks its
callback. Only OBS_SOURCE_TYPE_FILTER is a filter and only the VIDEO and
ASYNC bits count. pick() is the C's loop: each preferred space in turn,
stopping at one equal to the frame's space, so no match leaves the last
one and an empty list the frame's space. Values outside enum
gs_color_space are compared and returned as they are. The module denies
clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented
and unreachable lints, keeps no state and takes no lock.

Shim (src/ffi/source_color_space.rs): obs_source_color_space_route()
takes struct obs_source_color_space_node by value (mirrored, #[repr(C)])
and returns the C's enum values; obs_source_color_space_pick() takes the
count and pointer. No source pointer crosses. Intentional difference: a
null list with a count above 0 is read as empty (the C reads through the
null pointer); libobs passes NULL only with a count of 0.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-color-space (LIBOBS_PORTED_SOURCES), whose PCH is
only obs-source-color-space.h; libobs.pch.h keeps the header for
obs-source.c. The two functions are not EXPORT, so the shims are hidden:
rust-hidden.txt, the local: list of rust-exports.map and
rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The
exported symbol sets are unchanged and equal: 1766 on the C backend, the
hybrid and libobs-c-reference; the two are local in all three, and the
hybrid's come from the Rust archive. ci/test_ported_sources.py checks the
group on every host.

test_source_color_space_steps still compiles the C file into itself,
unchanged. The new test_source_color_space_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_audio_timing_steps_rust does: the test calls the two hidden
functions directly, so this variant runs the shims, the node passed by
value from C included. It passes 12 of 12 on the hybrid tree.
test_filter_color_space sees the swap through the hybrid.

Tier 1 source::color_space passes 17/17.

Refs obs-rust#1979
Oracle (oracle/source_color_space.c): the original
obs-source-color-space.c.h, unmodified, with its two functions renamed
to oracle_*, plus the node's layout (size, alignment, the six offsets),
the size and values of the route enum and the filter type and VIDEO and
ASYNC constants. The node is mirrored on the Rust side and passed by
value, as obs-source.c.h passes it.

Tier 3 (tests/source/color_space_parity.rs, 7 tests), comparing the
route's value and the picked space:
- the layout and constants against the C header, and the shim's mirror
  against the oracle's;
- the route over every combination of the VIDEO and ASYNC flags, data,
  enabled, a filter parent and a filter target (64), with eight types
  (the four enum obs_source_type values, -1, 4 and the ends of i32: so
  all 128 rows of the characterization, the filter type and seven
  others) and sixteen sets of other flags (OBS_SOURCE_AUDIO, bits 3, 10
  and 31, every other bit): 8,192 nodes, each route reached more than
  100 times; and a proptest over any i32 type, any u32 flags and the four
  bools (20,000 cases);
- the pick over every list of 0 to 5 values from -1..=4 (the four
  gs_color_space values and one past each end) against each of those
  frame spaces: 9,331 lists, 55,986 picks; and a proptest over lists of
  0 to 8 arbitrary i32 values, the frame's space drawn from the list half
  the time so matches land first, last and in the middle (20,000 cases);
  both sides read the list through a pointer;
- a count of 0 reads nothing on either side, with a null pointer or not;
- the intentional difference, on the shim only: a null list with a count
  above 0 gives the frame's space.

The parity module passed 20 runs in a row with fresh random seeds; no
regression file came up.

Mutation check (done once, not committed), with the two test modules in
obs-tests-core: 33 mutants. The 27 core mutants all fail Tier 1 (2 to 7
of 17) and Tier 3 (2 or 4 of 7): each rule removed, each rule widened
(filters asking their parent, sources with video asking it, any source
without video asking its target, filters with video asking it, a parent
or target asked without one), the disabled rule on data only, enabled
only or both, the rules reordered, the async rule on the VIDEO bit or
skipped for filters, any non-input type a filter, any flag counted as
video, the fallback picking, and the pick without its break, giving the
frame's space or the first entry with no match, GS_CS_SRGB for an empty
list, skipping the last entry or stopping one late. obs-rust#1493's rule
reverted fails 5 of 17 and 2 of 7. Of the 6 shim mutants, 5 fail Tier 3
(the parent and target routes swapped, the parent and target flags
swapped, the type read as 0, the count less one, and the null check
dropped, which aborts in from_raw_parts); Tier 1 does not reach the
shims. One survives and is equivalent: has_data and enabled swapped in
the shim, which the route only reads as !has_data || !enabled.

Refs obs-rust#1979
The rust/README.md crate table now lists libobs/obs-source-color-space.c.h
among the rust/obs-source units, and its port table has a row for it: the
two functions and their rules in order (obs-rust#1493's included), what stays C
(data_valid(), the field reads, the recursion, convert_video_space() and
the callback), the node passed by value, the int-sized colour spaces and
the compile-time checks, what the characterization pins and the port
keeps, the intentional difference (a null list with a count above 0 is
empty), the _rust variant of the steps test, test_filter_color_space and
what Tier 3 compares. The crate's description and lib.rs say what it
holds. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to
rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_color_space.rs: 1024 routes over every
combination of the inputs, with and without OBS_SOURCE_AUDIO, and 1024
async picks of the four frame spaces from lists of 0 to 4 preferred
spaces, matched first, last, in the middle or not at all, each folded
into a checksum both sides must match. On x86_64 Linux (GCC, not pinned;
median per 1024 calls): route 2.05 us Rust and 2.26 us C (0.91x),
async_pick 2.21 us Rust and 2.69 us C (0.82x), about 2 ns a call either
way.

Fixes obs-rust#1979
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 the source colour space query routing in obs-source.c to obs-source

2 participants