Skip to content

obs-source: Port the transition clock and fit - #2055

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-transition-clock
Oct 24, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-transition-clock

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2040

What and why

Ports the pure clock and fit decisions of the transitions (libobs/obs-source-transition.c.h, our #556 area) to rust/obs-source, as a new module transition_clock, next to audio_input (#2021) and color_space (#1990). Every transition goes through them: calc_time() runs for every frame a transition renders and for every audio sample it mixes, and the fit, the size fold and the colour space run on every tick. A wrong clock ends a transition early or never (the #556 class); a wrong fit misplaces a scene of another size during the transition. The swap handover of #556, the locks and get_sample_time() stay exactly as they are.

Six commits:

  1. Move (behaviour-neutral): the decisions go to libobs/obs-source-transition-clock.c.h, declared in the new obs-source-transition-clock.h without EXPORT, so they are hidden:

    • obs_source_transition_clock_time(): calc_time(), with its long double division as written;
    • obs_source_transition_clock_manual(): the manual step of obs_transition_tick() (hold outside manual mode, snap for a torque of 0, otherwise step; the tick still calls calc_torquef());
    • obs_source_transition_clock_size() and _fold(): get_cx()/get_cy() and the size fold of recalculate_transition_size();
    • obs_source_transition_clock_fit(): the scale of recalculate_transition_matrix(), written through two float * on the same branches as before;
    • obs_source_transition_clock_mix_spaces() and obs_source_transition_clock_min_ts(): mix_spaces() and transition_calc_min_ts() (TSan: audio thread reads source audio_ts / audio_input_buf without audio_buf_mutex #130's snapshot read through obs_source_get_audio_timestamp() stays in the caller).

    obs-source-transition.c.h keeps the locks, the field reads and writes, the child size and timestamp reads, the float casts of the sizes, calc_torquef(), the offsets passed to add_alignment() and their (int) casts, add_alignment(), the matrix build, get_sample_time() (unchanged), the texrenders and the swap handover. Three plain field reads move, under the same lock or none as before: calc_time() reads the manual value, start time and duration whatever the mode; the tick reads the torque outside manual mode too; the transition's aspect is computed after the child size reads. The modes, scale types and colour spaces cross as int; obs-source-transition.c.h checks the values and the enum sizes at compile time. The file stays in libobs.core-source for this commit.

  2. Characterization: test_transition_clock_steps, 20 cases, compiles the moved file (ASan and UBSan) and calls only its seven functions, plus calc_torquef() (EXPORT) for the tick. libm is linked except on MSVC.

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

  4. GREEN: the safe core and #[no_mangle] shims, in a new libobs.ported-source-transition-clock group with its own PCH (only the header). The export lists, source-sets.json, the unity lists and ci/test_ported_sources.py are updated, and a _rust variant of the steps test is added (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.

What the characterization pins:

  • calc_time() at start − 1 and the start (0), start + 1 (0.01f), end − 1 (0.99f), the end and after (1), with a start of 0 and one just below UINT64_MAX; a duration of 0 (0 up to the start, then 1); durations of 2^63, 2^63 + 2 and UINT64_MAX, and UINT32_MAX ms, the longest obs_transition_start() can set; the manual value as it is in manual mode; every other mode (2, −1, the ends of int) runs the clock;
  • the manual step: torque 0 and −0 snap; any other torque steps (with the overshoot cut and EPSILON's snap); the clamp as the least step; NaN targets and torques; outside manual mode nothing changes;
  • the size set or the actual one, and the larger child;
  • the fit for each scale type with a child smaller than, equal to, narrower than and larger than the transition, a transition of size 0, a child of width or height 0 (no matrix, nothing written) and scale types outside the enum;
  • mix_spaces() over all 4 × 4 pairs and values outside the enum;
  • min_ts() with no, one (either place) and two timestamps.

Every pinned float is the same on every platform: the ratios are exact or far from an f32 rounding tie, so a 64-, 80- or 128-bit long double gives the same float (checked for each), and the manual steps use exact products, so a fused multiply-add does not change them.

Kept, characterized, not endorsed:

  • the manual value is not clamped, and a NaN one passes through, so the render never sees t >= 1 for it;
  • any ratio of 1 − 2^-25 or more rounds to 1.0f (the tie rounds to even), so a transition reaches t >= 1 that much early (about 3 µs of a 100 s one);
  • a NaN target snaps to NaN at torque 0, and with a torque the NaN distance passes every clamp compare, so the step is NaN too; a NaN torque steps to NaN; a NaN value only recovers by a snap;
  • a transition of size 0 (only while both children report 0 when the size is folded) makes tr_cx / tr_cy NaN or inf, so every child is fitted to a scale of 0 on that side;
  • a scale type outside the enum gives a matrix but writes no scale (see the bug below).

long double: the platforms already differ below 2^53, so the port gives the x86_64 (x87) result for every u64. #2040 planned an f64 division and bit parity below 2^53 ns, on the basis that double rounding through f64 is harmless there. It is not: that result holds for operands that fit in f32, and these are integers up to 2^53. For a duration over 2^29 ns (0.5 s), a quotient within about 2^-54 of an f32 rounding tie can land exactly on the tie in f64, which then rounds to even, while the x87's 64-bit quotient lies off the tie and rounds the other way. Tier 1's near-tie test found it with the first f64 port: a 33.5 s transition, 33554426999 / 33554431999, is 0x3f7ffffd in x87 and 0x3f7ffffe in f64. Tier 3's sweeps count them: of 754,997,945 near-tie ratios (odd whole-millisecond durations from 30 s to 30 min among them), a plain f64 division rounds 131,451 below 2^53 the other way. So the C already differs by platform here: x86_64 Linux and Intel macOS (x87) give one float, MSVC and Apple silicon (long double is a double) sometimes the other, and aarch64 Linux (128 bits) the nearest, which is the x87's below 2^39 and differs rarely above.

The port matches x86_64, where the oracle, the C backend and libobs-c-reference run: ratio() divides in f64 and uses that unless the f64 quotient is exactly on an f32 tie (all but about 1 in 2^29 ratios); then, and from 2^53 on, x87_ratio() computes the x87 result from integers (u128: the quotient rounded to a 64-bit significand, then to 24 bits, both to nearest even). Off a tie the two cannot differ: below 2^53 both operands and every f32 tie are exact in f64, so the f64 quotient is on the same side of every tie as the exact and the x87 quotients. Tier 3 checks it bit for bit on x86_64 (near-tie sweeps, every small duration, proptests over full-range clocks). On MSVC and Apple silicon the hybrid now gives the x87's float where their C gave the double's, in the last bit of a ratio within about 2^-54 of a tie (a duration over 0.5 s); there Tier 3 checks the C against a model of its width and the shim against the x87 model. If maintainers would rather match MSVC and Apple silicon instead, ratio() becomes a plain f64 division (one line), and the parity test's expectations flip with it.

Found, not fixed (reported here): recalculate_transition_matrix() reads a scale it never set for a scale type outside enum obs_transition_scale_type. obs_transition_load() takes transition_scale_type from the scene collection as it is, and only the three enum values write the scale, so any other value leaves struct vec2 scale uninitialized and the matrix is built from it, on every tick. The move keeps that exactly (the fit writes nothing for such a type, which the characterization pins), and the port keeps it too. Since the move, GCC sees it after inlining the fit in the unity group and warns -Wmaybe-uninitialized (not an error, -Wno-error=maybe-uninitialized); from the GREEN commit the fit is in its own translation unit and the warning is gone. The fix (clamp on load, or a defined scale) changes how such a scene collection renders, so it belongs in its own PR.

  • Intentional difference: a null scale_x or scale_y is not written by the shim; the C writes through it. libobs passes the addresses of its own struct vec2.
  • Tick parity and NaN pairs: calc_torquef() is not part of this port (each side calls its own: the C, or obs-graphics's port, which hybrid libobs calls). Where one of its operations has two NaN operands, which NaN comes out depends on the operand order the compiler picks, as in obs-source: Port the source audio input steps #2021, so in the tick only both sides must give a NaN. The decision itself and everything else is compared bit for bit. The first parity run found that case; its regression file is committed.
  • No new crate dependency. obs-source still depends on no other crate: Rust returns snap or step and C keeps calling calc_torquef(). Tier 1 and Tier 3 use obs-graphics's calc_torquef through the test executable's existing dev-dependency.
  • The _rust variant links. test_transition_clock_steps_rust builds the unchanged test source against libobs_rust-static. The test also calls calc_torquef(), so it links OBS::libobs first (it resolves to libobs's, the Rust one in the hybrid) and the archive after it for the seven hidden functions; the other way round the archive's obs-graphics member is pulled in and needs libobs's hidden graphics symbols. libm is linked under if(NOT MSVC). The test includes only graphics/math-extra.h (no SIMDe) and gets libobs's include directories from OBS::libobs. It passes 20 of 20 on the hybrid tree.
  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets. The seven functions are local (t) in all three; in the hybrid they come from liblibobs_rust.a.
  • Performance (port_comparison, x86_64 Linux, GCC, not pinned; median per 1024 calls):
    • audio clock (1024 samples of a 48 kHz buffer, as mix_child() asks): 2.32 µs Rust, 2.43 µs C (0.95x);
    • ratios that all take the integer path (worst case): 13.38 µs Rust, 2.61 µs C (5.1x; in use about 1 in 2^29 ratios takes it);
    • tick (manual step, size fold, two fits, colour space, timestamps): 21.33 µs Rust, 19.34 µs C (1.10x).
  • No user-visible change, so no docs/features.md line.

Observed, not changed: the offsets passed to add_alignment() are (int) casts of a float difference, undefined in C for a size difference of 2^31 or more; they stay C, so nothing changes there.

RED → GREEN

  • RED commit: edb7282f9. soldr cargo test -p obs-data --test obs-tests-core source::transition_clock fails 34 of 35, every failure "not yet implemented" (the 20 ported steps_* cases and 14 of the 15 edge_* cases, eleven of them proptests). The one that passes, edge_ratio_models, checks the test's own three division models and calls nothing in the port. Run on 774933bb2, the same commit before the rebases (they touched nothing in rust/obs-source, rust/obs-data or rust/obs-c-oracle).
  • GREEN: f7e9afcba. Tier 1 passes 35/35. Tier 2 on 15a5b5568, the same six commits based on b3912c298 (the last rebase onto 6891f6909 changed nothing in libobs, the Rust crates, the test graph or the ci lists; see below), under a private Xvfb -noreset with dbus-run-session, a fresh HOME and ulimit -c 0, no TMPDIR. libobs-c-reference was built with ninja libobs-c-reference before the runs and is newer than the head commit.
Test C backend hybrid libobs-c-reference
test_transition_swap 4/4 4/4 4/4
test_studio_mode_swap 4/4 4/4 4/4
test_transition_recursive_destination 18/18 18/18 18/18
test_fade_to_color_mix 5/5 5/5 5/5
test_transition_audio_rescale 2/2 2/2 2/2
test_stinger_media_duration 5/5 5/5 5/5
test_transition_clock_steps 20/20 20/20 20/20
test_transition_clock_steps_rust (not built: needs the Rust archive) 20/20 20/20
full ctest 416/416, 0 skipped 425/425, 0 skipped 425/425, 0 skipped

Every test passed on its first run; no rerun was needed.

The six real-libobs tests and test_transition_clock_steps also passed on all three builds at the characterization commit (e0a429b05, which differs from 96c1891cf only in the test's header comment and the base).

  • Tier 3, e37f0dab0 (source::transition_clock_parity, 21 tests and two ignored; run on 3973694ed, the same commit before the rebases, and again by the test-diff gate on the head). Runs:

    • the constants against the C header; FLT_EVAL_METHOD 0 and, on x86_64, a 64-bit long double significand;
    • the clock over six modes, 14 special manual values, 6 start times, 12 durations and 24 timestamps around the start, middle and end of each (145,152 clocks); the six ratios where the platforms differ; near-tie sweeps in 40 binades for ten durations from 0.5 s to UINT64_MAX (584,522 ratios; a plain f64 division differs on 702 of them below 2^53); every ratio with a duration up to 2^11 ns;
    • ignored, run once with --release --ignored: time_near_ties_wide (754,997,945 near-tie ratios for 49 durations: all equal) and time_every_duration_to_2_15 (2^29 ratios: all equal);
    • the manual step over the modes and 24 torques, and the tick as obs_transition_tick() runs it over 3 modes × 8 values × 8 targets × 10 torques × 4 clamps × 4 times; the size and fold over special u32;
    • the fit over 8 scale types and 12 special sizes on each of the four sides (165,888 fits; no matrix, a matrix with no scale and a scale each over 10,000 times), and 13 video sizes per side for the three types;
    • mix_spaces() over 12 × 12 values, min_ts() over 9 × 9;
    • proptests (20,000 cases each) over full-range u64 clocks and ratios (half near a tie), f32 torques, values, targets and sizes drawn from bit patterns (never a wide Range<f32>, test: extent_parity proptests can trip proptest's float range assertion (Windows merge group) #1951), any i32 and u64.

    Tier 1 and the parity module passed 10 runs in a row with fresh seeds.

  • Mutation check (once, not committed, on the GREEN and Tier 3 commits before the rebases): 50 mutants, 45 fail, 5 survive and are equivalent.

Mutant Tier 1 (35) Tier 3 (21)
time: manual check inverted / manual value ignored 6 / 2 6 / 2
time: ts == start not "up to the start" (a duration of 0 then gives 1) 1 2
time: ends 1 ns early (the #556 class) / never ends 1 / 5 4 / 2
ratio: a plain f64 division (the plan's port) 2 3
ratio: an f32 division / the tie mask a bit off / f64 up to 2^64 5 / 2 / 1 4 / 3 / 2
x87: 64-bit rounding truncates 2 4
x87: 24-bit rounding ties up / truncates 2 / 5 3 / 5
x87: one rounding (the 128-bit long double result) 2 2
x87: no renormalising after the 24-bit carry 3 3
manual: snap for a torque that is not 0 / in any mode / not for −0 5 / 2 / 1 4 / 4 / 4
size: always the actual / always the set size 2 / 1 2 / 1
fold: the smaller child 2 2
fit: only the width checked for 0 2 2
fit: max-only on both sides larger / a larger child stretched 3 / 3 3 / 3
fit: max-only on >= (differs only for non-finite sizes) 0 2
fit: aspect on <= / as f32::min of the two ratios (differ only when the aspects round equal, or for NaN) 0 / 0 2 / 2
fit: aspect sides swapped / stretch x and y swapped 4 / 3 3 / 4
fit: an unknown type scales by 1 / is aspect / gives no matrix 2 / 2 / 2 2 / 2 / 3
mix_spaces: SCRGB not extended / only a read / 16F before extended 2 each 2 each
min_ts: zeros counted / the later one 2 / 2 2 / 2
shim: start time and duration swapped 0 (Tier 1 does not reach the shims) 6
shim: SNAP and STEP swapped / scale x and y swapped 0 / 0 4 / 4
shim: unset writes 1 / no matrix returns true 0 / 0 2 / 2
shim: transition and child sizes swapped 0 4
shim: min_ts and mix_spaces read a twice / size calls the fold 0 each 2 each
equivalent: the end check as > (at the end the ratio is exactly 1 on either path) 0 0
equivalent: no duration == 0 check (the elapsed time is at least 1 there) 0 0
equivalent: ratio() always through x87_ratio() (same result, slower) 0 0
equivalent: x87 64-bit rounding ties up (a u64 ratio is never exactly half way at 64 bits) 0 0
equivalent: min_ts() on <= 0 0
  • Real app: no UI, protocol or logging change.

Other runs, on the head 8dae197b9 (based on 6891f6909) unless noted:

  • uv run ci/local_gate.py --test-diff origin/master: ok (workspace scope, 3104 of 3104 passed, 4 skipped: the ignored tests, the two above among them), on 15a5b5568 and again on 8dae197b9. --lane lint: ok, --lane cross: ok, on 15a5b5568 (the last rebase brought only obs-ffmpeg, frontend and sanitize-script changes from master).
  • ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py, ci/test_sorted_shared_lists.py, ci/test_test_graph_sorted.py and ci/test_cmocka_rust_static_guard.py: OK.
  • The shared lists (source-sets.json, the unity lists, test-graph.json, the three export lists, ci/test_ported_sources.py) were diffed by hand against origin/master after each rebase: only this PR's entries, in order.
  • port_comparison (on 15a5b5568): this unit's checksums match. The run then stops at util's bmem/count fixture (16 against 0), which fails the same way on origin/master on this machine; it is not this unit's.

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 test-diff, lint and cross lanes 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)

@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 05:17
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
@zackees
zackees force-pushed the port/source-transition-clock branch from 8dae197 to 15d2c48 Compare October 24, 2026 06:13
@zackees

zackees commented Oct 24, 2026

Copy link
Copy Markdown
Member

Coordinator: rebased onto master (clean, no conflicts) after it went UNMERGEABLE in the queue; re-queued.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 24, 2026
…clock.c

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

- obs_source_transition_clock_time(): calc_time(). The manual value in
  manual mode; otherwise 0 up to the start, 1 from the end on and for a
  duration of 0, and the elapsed time over the duration, divided in
  long double as before, in between.
- obs_source_transition_clock_manual(): the manual step of
  obs_transition_tick(): hold outside manual mode, snap to the target for
  a torque of 0, otherwise step. obs_transition_tick() still calls
  calc_torquef() for the step.
- obs_source_transition_clock_size() and _fold(): get_cx() and get_cy()
  (the size set, or the actual size), and the size fold of
  recalculate_transition_size() (the larger of the two children).
- obs_source_transition_clock_fit(): the scale of
  recalculate_transition_matrix(). No matrix for a child of width or
  height 0; max-only keeps 1 for a child that fits and fits a larger one
  as aspect; aspect takes the smaller ratio; stretch each side's. It
  writes the scale on the same branches as before, so a scale type
  outside the enum still writes none.
- obs_source_transition_clock_mix_spaces(): mix_spaces().
- obs_source_transition_clock_min_ts(): transition_calc_min_ts(), the
  earliest of the two timestamps that is not 0.

obs-source-transition.c.h keeps the locks, the field reads and writes,
the child size reads (obs_source_get_width/height), the float casts of
the sizes, calc_torquef(), the offsets passed to add_alignment() and
their (int) casts, add_alignment(), the matrix build, get_sample_time(),
the texrenders and the swap handover (obs-rust#556). transition_calc_min_ts()
still reads each child's timestamp through
obs_source_get_audio_timestamp() (obs-rust#130), only for a child that is there
and whose audio is not pending, and passes 0 for the others.

Three reads move, all plain field reads under the same lock or none as
before: calc_time() now reads the manual value, the start time and the
duration whatever the mode; obs_transition_tick() reads the torque
outside manual mode too; recalculate_transition_matrix() computes the
transition's aspect after the child size reads instead of before.

The modes, scale types and colour spaces cross as int, so the header
needs neither obs.h nor a graphics header. obs-source-transition.c.h
checks at compile time that the header's values are
OBS_TRANSITION_MODE_MANUAL, enum obs_transition_scale_type's and enum
gs_color_space's, and that the three enums are int-sized.

GCC now inlines the fit into recalculate_transition_matrix() in the unity
group and warns (-Wmaybe-uninitialized, not an error) that the scale may
be read unset: it is, for a scale type outside the enum, as before the
move. That is reported in the PR, not changed here.

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#2040
test_transition_clock_steps compiles libobs/obs-source-transition-clock.c.h
(ASan and UBSan) and calls only its seven hidden functions, plus
calc_torquef() (EXPORT) for the manual step as obs_transition_tick() runs
it. 20 cases:

- calc_time(): the manual value as it is, whatever the clock says (-0,
  above 1, below 0, NaN and -inf with their bits); 0 at start - 1 and at
  the start, 0.01f at start + 1, 0.99f at end - 1, 1 at the end and after
  (a duration of 100 ns, a start of 0 and one just below UINT64_MAX); a
  third of a 300 ms fade, rounded once; a duration of 0: 0 up to the
  start, then 1; durations of 2^63, 2^63 + 2 and UINT64_MAX, and the
  longest obs_transition_start() can set (UINT32_MAX ms); every mode but
  OBS_TRANSITION_MODE_MANUAL (2, -1 and the ends of int) runs the clock.
- The manual step: torque 0 and -0 snap, to the target as it is; any
  other torque steps (1, 0.5, -1, 1e-30, +-inf, NaN), with the overshoot
  cut at the target and EPSILON's snap; the clamp as the least step;
  outside manual mode nothing changes.
- get_cx()/get_cy() (the size set, or the actual one) and the size fold
  (the larger child, in either order, up to UINT32_MAX).
- The fit: max-only with a child smaller than, equal to, narrower than
  and larger than the transition on either side; aspect by the width for
  a wider child and by the height for a taller one or the same aspect;
  stretch per side; a child of width or height 0 (and -0): no matrix and
  no scale written, for every scale type.
- mix_spaces() over all 4 x 4 pairs, and values outside the enum.
- transition_calc_min_ts() with no, one (in either place) and two
  timestamps, UINT64_MAX included.

Characterized, not endorsed:
- the manual value is not clamped, and a NaN one passes through, so the
  render never sees t >= 1 for it;
- 1 ns before the end of a long transition rounds to 1.0f: any ratio of
  1 - 2^-25 or more (the tie rounds to even), so t >= 1 a little early;
- a NaN target snaps to NaN at torque 0, and with a torque the NaN
  distance passes every clamp compare, so the step is NaN too; a NaN
  torque steps to NaN; a NaN value only recovers by a snap;
- a transition of size 0 (only while both children report 0 when the size
  is folded) makes tr_cx / tr_cy NaN or inf, so every child is fitted to a
  scale of 0 on that side;
- a scale type outside the enum (a scene collection's
  transition_scale_type is loaded as it is) gives a matrix but writes no
  scale.

The pinned floats are the same on every platform. calc_time() divides in
long double: 64 bits on MSVC and Apple silicon, 80-bit x87 on x86_64 and
128 bits on aarch64 Linux. The widths can round a quotient that lies very
near a float rounding tie differently; the pinned ratios are exact or far
from a tie, and each gives the same float with all three widths (checked).
The manual steps use exact products, so a fused multiply-add gives the
same result.

It is in the native test graph, in the core suite, with libm linked
except on MSVC. test_transition_swap, test_studio_mode_swap,
test_transition_recursive_destination, test_fade_to_color_mix,
test_transition_audio_rescale and test_stinger_media_duration are
unchanged.

Refs obs-rust#2040
Tests for the port of libobs/obs-source-transition-clock.c.h to the
transition_clock module of rust/obs-source, next to audio_input,
audio_timing, color_space and deinterlace_pacing. The safe core is a stub
(todo!()), one function per hidden C function, plus the division they
need:

- time() of a Clock (mode, manual value, start time, duration) at a
  timestamp, and ratio(), the elapsed time over the duration as an f32,
  rounded as the C's long double division is on x86_64;
- manual() of the mode and torque, giving a Step (Hold, Snap or Step);
- size() and fold();
- fit() of the transition's and the child's sizes and the scale type,
  giving a Fit (NoMatrix, Scale { x, y } or Unset), with ScaleType and
  ScaleType::from_c();
- mix_spaces() and min_ts().
The constants (MODE_MANUAL, the scale types and the colour spaces) are
the C's.

tests/source/transition_clock.rs ports test_transition_clock_steps 1:1:
its 20 cases under the same names (steps_*), in the same order, with the
same inputs and expectations, the characterized-not-endorsed cases
included. The manual step runs as obs_transition_tick() runs it, with
obs_graphics::math_extra::calc_torquef, already a dev-dependency of the
test executable (obs-data), so obs-source gains no dependency.

Edge cases (edge_*):
- three models of the division, side by side: x87_ratio(), the x87
  long double then float of x86_64, written as a long division (64
  quotient bits rounded on the remainder, then to 24 bits, each to
  nearest even); nearest_ratio(), the nearest f32 from integers alone
  (the C where long double has 113 bits); and f64_ratio(), a plain double
  division (the C on MSVC and Apple silicon). They are checked on known
  ratios and ties, and on six ratios where the platforms' C give
  different floats: two 33.5 s transitions where the double lands on a
  rounding tie and the x87 does not, and four above 2^39 where the x87
  does and the nearest does not;
- ratio() is the x87's on those six, for every duration up to 2^10 ns,
  for near-ties (ratios a few units of 2^-60 from an f32 tie; the double
  differs on some of them below 2^53), and in a proptest over any u64
  ratio (any bit length, half of them near a tie); it is the nearest
  below 2^39, and never more than one f32 unit from the double;
- proptests (4000 cases each) over full-range u64 clocks: the clock
  against the C's rule written with the x87 model, in [0, 1] outside
  manual mode, and never going back in time; the manual step over any
  mode and any f32 torque bit pattern; the size and the fold over any
  u32; the fit's invariants over any f32 sizes (libobs's u32 sizes and
  any bit pattern, never a wide float range): no matrix exactly for a
  child side of +-0, nothing written for a type outside the enum, stretch
  per side, aspect one of the two ratios on both sides and the smaller for
  whole sizes, max-only 1 for a child that is not larger and aspect
  otherwise; mix_spaces() against a rank model over any i32; min_ts()
  against a filter model, both ways round.

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as the other obs-source modules' are.

soldr cargo test -p obs-data --test obs-tests-core source::transition_clock
fails 34 of 35 with "not yet implemented". The one that passes,
edge_ratio_models, checks the test's own three models and calls nothing
in the port.

Refs obs-rust#2040
Ports libobs/obs-source-transition-clock.c.h, the clock and fit decisions
of the transitions, to the transition_clock module of rust/obs-source.
obs-source-transition.c.h stays C: the locks, the field reads and writes,
the child size and timestamp reads, calc_torquef() (obs_transition_tick()
calls it for a step), the offsets and their (int) casts, add_alignment(),
the matrix build, get_sample_time(), the texrenders and the swap handover
(obs-rust#556). The hybrid libobs drops the C file and links the Rust shims in
its place.

Safe core (src/transition_clock.rs):
- time(): the manual value in manual mode (with its bits); 0 up to and
  at the start; 1 from the end on and for a duration of 0; ratio() in
  between.
- ratio(): the C's (float)((long double)ts / (long double)end) as x86_64
  computes it (x87: the quotient rounded to a 64-bit significand, then to
  24 bits, both to nearest even), for every u64. A plain f64 division
  does not do that below 2^53 either: for a duration over 2^29 ns
  (0.5 s), a quotient within about 2^-54 of an f32 rounding tie can land
  on the tie in f64, which then rounds to even where the x87 quotient
  lies off it (a 33.5 s transition, 33554426999 / 33554431999, gives
  0x3f7ffffd in x87 and 0x3f7ffffe in f64). So ratio() divides in f64 and
  uses that unless the f64 quotient is exactly on an f32 tie (all but
  about 1 in 2^29 ratios); then, and from 2^53 on, x87_ratio() computes
  the x87 result from integers (u128). The two cannot differ off a tie:
  both operands are exact in f64 below 2^53, and so is every f32 tie, so
  an f64 quotient off a tie is on the same side of it as the exact and the
  x87 quotients.
- manual(): Hold outside manual mode, Snap for a torque of +-0, Step for
  any other (NaN included).
- size() and fold(): the size set, or the actual one; the larger child.
- fit(): NoMatrix for a child side of +-0; max-only's 1 for a child that
  is not larger on either side (NaN compares false), else aspect; aspect
  by the width when the transition's aspect is below the child's, else by
  the height; stretch per side; Unset (nothing written) for a scale type
  outside the enum. f32 division and compares, as the C's.
- mix_spaces() and min_ts(): the C's compares.
The module denies clippy's arithmetic, indexing, as (allowed in ratio()
for its u64 to f64 and f64 to f32 casts, which have no From, commented),
unwrap, expect, panic, unimplemented and unreachable lints,
keeps no state and takes no lock. The crate still depends on no other
crate.

Shims (src/ffi/source_transition_clock.rs): plain values in and out; the
step as the C's enum values; the fit writes the scale through the two
float * only for a scale, as the C does. Intentional difference: a null
scale_x or scale_y is not written (the C writes through it; libobs passes
the addresses of its own struct vec2).

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-transition-clock (LIBOBS_PORTED_SOURCES), whose PCH
is only obs-source-transition-clock.h; libobs.pch.h keeps the header for
obs-source-transition.c. In its own translation unit the fit is no
longer inlined into recalculate_transition_matrix(), so GCC's
-Wmaybe-uninitialized from the move commit is gone on the C backend too.
The seven 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 seven 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 and that obs-source-transition.c.h stays C.

test_transition_clock_steps still compiles the C file into itself,
unchanged. The new test_transition_clock_steps_rust builds the same test
source against libobs_rust-static: the test calls the seven hidden
functions directly, so this variant runs the shims. It also calls
calc_torquef(), which is EXPORT, so it links libobs first (calc_torquef()
then resolves to libobs's, the Rust one in the hybrid) and the archive
after it for the hidden functions; the other way round the archive's
obs-graphics member is pulled in and needs libobs's hidden graphics
symbols. libm is linked except on MSVC. It passes 20 of 20 on the hybrid
tree.

Tier 1 source::transition_clock passes 35/35.

Refs obs-rust#2040
Oracle (oracle/source_transition_clock.c): the original
obs-source-transition-clock.c.h, unmodified, with its seven functions
renamed to oracle_*, plus the header's constants, the step enum's size
and values, the int, bool and float sizes, FLT_EVAL_METHOD and
LDBL_MANT_DIG (the width of the long double calc_time() divides in).

Tier 3 (tests/source/transition_clock_parity.rs, 21 tests and two
ignored), every result compared bit for bit, the fit's two floats from a
guard fill on both sides, written or not:
- the constants against the C header; FLT_EVAL_METHOD 0, and on x86_64
  (not MSVC) a long double of 64 significant bits, the x87's;
- the clock over six modes (both of the enum, 2, -1 and the ends of int)
  with 14 special manual values, 6 start times, 12 durations (0 to
  UINT64_MAX, a 33.5 s one, UINT32_MAX ms, 2^53, 2^63) and 24 timestamps
  around the start, the middle and the end of each: 145,152 clocks;
- the six ratios where the platforms' C give different floats;
- near-tie sweeps: ratios one unit either side of 512 f32 rounding ties
  in each of 40 binades, for ten durations from 0.5 s to UINT64_MAX
  (584,522 ratios, all equal; below 2^53 a plain f64 division gives 702 of
  them differently); time_near_ties_wide (ignored, run once with
  --release --ignored) does 2^17 ties a binade for 49 durations, odd whole
  milliseconds from 30 s to 30 min among them: 754,997,945 ratios, all
  equal, where a plain f64 division differs on 131,451 below 2^53;
- every ratio with a duration up to 2^11 ns, and up to 2^15 ns in the
  ignored time_every_duration_to_2_15 (2^29 ratios; passed);
- the manual step over the modes and 24 torques, and the tick as
  obs_transition_tick() runs it (each side's decision, then its own
  calc_torquef(): the oracle's C, or obs-graphics's port as hybrid libobs
  calls it) over 3 modes, 8 values, 8 targets, 10 torques, 4 clamps and 4
  times; the size and the fold over 9 x 9 special u32;
- the fit over 8 scale types (the enum, 3, -1, 100 and the ends of int)
  and 12 special sizes on each of the four sides (0, -0, 1, video sizes,
  UINT32_MAX as float, NaN, inf, the smallest subnormal, -1): 165,888
  fits, each of no matrix, a matrix with no scale and a scale more than
  10,000 times; and 13 video sizes on each side for the three types;
- mix_spaces() over 12 x 12 values, min_ts() over 9 x 9;
- proptests (20,000 cases each): any clock (u64 from bit patterns and any
  bit length, the timestamp near the start or the end), any ratio (half
  of them near a tie), the manual step and the tick over any f32 bit
  pattern, the size and fold over any u32, the fit over libobs's u32
  sizes and any f32 bit pattern (never a wide Range<f32>, obs-rust#1951),
  mix_spaces() over any i32, min_ts() over any u64.
On another long double width (MSVC, Apple silicon, aarch64 Linux) the
ratio can differ in the last bit near a tie, as those platforms' libobs
already differ from x86_64's; there the C is checked against the model
of its width and the shim against the x87 model (Tier 1's models). The
intentional difference is checked on the shim only: a null scale pointer
is not written.

One exception, in the tick only: calc_torquef() is not part of this
port, and where one of its operations has two NaN operands, which NaN
comes out depends on the operand order the compiler picks (as in obs-rust#2021).
There both sides must give a NaN. The first parity run found it (value
0, target NaN, torque 1e-45, clamp 0, t NaN); its regression file is
committed. Tier 1's f64_ratio() and PLATFORM_TIES are now pub, for the
parity module.

Tier 1 and the parity module passed 10 runs in a row with fresh seeds;
no other regression file came up.

Mutation check (once, not committed): 50 mutants, Tier 1 (35 tests) and
Tier 3 (21), failures per mutant:

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| time: manual check inverted / manual value ignored | 6 / 2 | 6 / 2 |
| time: ts == start not "up to the start" (a duration of 0 then gives 1) | 1 | 2 |
| time: ends 1 ns early (the obs-rust#556 class) / never ends | 1 / 5 | 4 / 2 |
| ratio: a plain f64 division (no tie check) | 2 | 3 |
| ratio: an f32 division / the tie mask a bit off / f64 up to 2^64 | 5 / 2 / 1 | 4 / 3 / 2 |
| x87: 64-bit rounding truncates | 2 | 4 |
| x87: 24-bit rounding ties up / truncates | 2 / 5 | 3 / 5 |
| x87: one rounding (the 113-bit result) | 2 | 2 |
| x87: no renormalising after the 24-bit carry | 3 | 3 |
| manual: snap for a torque that is not 0 / in any mode / not for -0 | 5 / 2 / 1 | 4 / 4 / 4 |
| size: always the actual / always the set size | 2 / 1 | 2 / 1 |
| fold: the smaller child | 2 | 2 |
| fit: only the width checked for 0 | 2 | 2 |
| fit: max-only on both sides larger / a larger child stretched | 3 / 3 | 3 / 3 |
| fit: max-only on `>=` (differs only for non-finite sizes) | 0 | 2 |
| fit: aspect on `<=` / as f32::min of the two ratios (differ only when the aspects round equal, or NaN) | 0 / 0 | 2 / 2 |
| fit: aspect sides swapped / stretch x and y swapped | 4 / 3 | 3 / 4 |
| fit: an unknown type scales by 1 / is aspect / gives no matrix | 2 / 2 / 2 | 2 / 2 / 3 |
| mix_spaces: SCRGB not extended / only a read / 16F before extended | 2 each | 2 each |
| min_ts: zeros counted / the later one | 2 / 2 | 2 / 2 |
| shim: start time and duration swapped | 0 | 6 |
| shim: SNAP and STEP swapped / scale x and y swapped | 0 / 0 | 4 / 4 |
| shim: unset writes 1 / no matrix returns true | 0 / 0 | 2 / 2 |
| shim: transition and child sizes swapped | 0 | 4 |
| shim: min_ts and mix_spaces read a twice / size calls the fold | 0 each | 2 each |

Tier 1 does not reach the shims. Five survive and are equivalent:
- the end check as `>`: at the end the ratio is exactly 1 on either path;
- no `duration == 0` check: the elapsed time is at least 1 there, so the
  end check already gives 1;
- ratio() always through x87_ratio(): the same result, slower (the fast
  path is exact off a tie, as above);
- x87: the 64-bit rounding ties up: a u64 ratio is never exactly half
  way at 64 bits (that takes 65 significant bits, and a u64 ratio in
  lowest terms has a numerator below 2^64);
- min_ts() on `<=`: equal timestamps give the same value.

Refs obs-rust#2040
The rust/README.md crate table now lists
libobs/obs-source-transition-clock.c.h among the rust/obs-source units
(and obs-source-transition.c among the files they come from), and its
port table has a row for it: the seven functions and their rules, what
stays C (the locks, the field and child reads, calc_torquef(), the
offsets and their casts, add_alignment(), the matrix build,
get_sample_time(), the texrenders and obs-rust#556's handover), the int-sized
enums and the compile-time checks, the scale written only where the C
writes it, the long double widths and the x87 result the port gives for
every u64 (an f64 division off a tie, an integer division on one and
from 2^53), what the characterization pins and the port keeps, the
scale type outside the enum (reported, not changed), the intentional
difference (a null scale pointer is not written), the _rust variant of
the steps test, the real-libobs tests and what Tier 3 compares, with the
NaN-pair exception of the tick. 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_transition_clock.rs: 1024 samples of a
48 kHz buffer through the clock as mix_child() asks it, a third of the
way into a 300 ms transition; 1024 ratios that all take the integer x87
path (f64 quotients on a tie, durations past 2^53); and 1024 ticks of
the manual step, the size fold, two fits with each scale type, the
colour space and the timestamps, each folded into a checksum both sides
must match. On x86_64 Linux (GCC, not pinned; median per 1024 calls):
audio_clock 2.32 us Rust and 2.43 us C (0.95x), ratio_ties 13.38 us and
2.61 us (5.1x, the u128 division, which only about 1 in 2^29 ratios
takes in use), tick 21.33 us and 19.34 us (1.10x).

Fixes obs-rust#2040
@zackees
zackees force-pushed the port/source-transition-clock branch from 15d2c48 to 07a5686 Compare October 24, 2026 06:43
@zackees
zackees enabled auto-merge October 24, 2026 06:43
@zackees

zackees commented Oct 24, 2026

Copy link
Copy Markdown
Member

Coordinator: rebased after #2046 (async pacing) landed. The conflicts were only the crate description lists in rust/README.md, rust/obs-source/Cargo.toml and src/lib.rs; I kept both units (async_pacing + transition_clock). cargo check --workspace --tests passes. Auto-merge set.

@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 5a22c3d Oct 24, 2026
33 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…ansition-render.c

Behaviour-neutral extraction before the Rust port. The render and audio
step decisions of the transitions in obs-source-transition.c.h move to
libobs/obs-source-transition-render.c.h, declared in the new
libobs/obs-source-transition-render.h without EXPORT (hidden):

- obs_source_transition_render_video_due() and _video_stop(): the stop
  step of obs_transition_video_render2() and
  obs_transition_video_render_direct(). Due at t >= 1 while the video
  transitions (a NaN t is never due). Then the video ends, and the
  transition stops unless its audio runs on; the direct render first
  ends the audio of a transition that is not active (eb35b07). render2
  has no inactive rule and passes active as true.
- obs_source_transition_render_draw(): render2's draw choice: both
  children through the callback while the video transitions with the
  textures locked and a callback set; else source B while the audio
  transitions; else source A.
- obs_source_transition_render_direct_index(): the child the direct
  render draws: B only for OBS_TRANSITION_SOURCE_B while transitioning.
- obs_source_transition_render_audio_stop(): the stop step of
  obs_transition_audio_render() and transition_stop_audio() (removed):
  with a timestamp at t >= 1 while the audio transitions; without one
  only once the video has ended too (7af9c2d). The audio ends, and the
  transition stops unless its video runs on.
- obs_source_transition_render_audio_mix(): mix each child that is there
  while the audio transitions, else copy source A.
- obs_source_transition_render_audio_valid() and _audio_place(): the
  checks of transition_process_audio(): a child, its audio not pending
  and not duplicated; a timestamp that is not 0 (3191f5d); the start
  frame, ns_to_audio_frames(sample_rate, ts - min_ts) written out as its
  util_mul_div64() so the file needs only util_uint64.h, and the
  "> AUDIO_OUTPUT_FRAMES" check.
- obs_source_transition_render_offset(): the scaled child size of
  recalculate_transition_matrix() and the offsets it passes to
  add_alignment(), before the (int) casts: each product rounded to float,
  then subtracted, as before.

Why nine functions and not one step each: every call keeps its place and
its condition. obs_source_active() is asked only once the video step is
due, so the due test is its own function; obs_source_get_audio_mix() is
read between the timestamp check and the start-frame check, so the place
says NONE, LATE or MIX and the C returns before or after the read;
calc_time() is asked only with a timestamp, so the audio stop step takes
whether there is one. The re-read of the children after the audio stop
step stays where it was.

obs-source-transition.c.h keeps the locks, get_video_time() and
calc_time(), copy_transition_state() and the refs, obs_transition_stop(),
handle_stop() and the signals, obs_source_active(), the drop of removed
children (obs_source_removed() is asked only of a child that is there)
and transition_calc_min_ts() with its pending check and obs-rust#130's snapshot
read (as obs-rust#2055 left them), the texture trylock, render_child(), the
callbacks, the cx/cy check, mix_child() and get_sample_time(), the mixer
loop, the memcpy(), the (int) casts, add_alignment() and the matrix
calls. transition_process_audio() now reads audio_is_duplicated also
when audio_pending is set (two plain field reads of a child it holds a
reference to); nothing else is read that was not read before.

The target crosses as int; obs-source-transition.c.h checks at compile
time that the header's values are OBS_TRANSITION_SOURCE_B and
AUDIO_OUTPUT_FRAMES and that enum obs_transition_target is int-sized.

Checked against the unchanged functions with a scratch harness (not
committed): master's and the moved recalculate_transition_matrix(),
obs_transition_video_render2(), obs_transition_video_render_direct(),
transition_process_audio() and obs_transition_audio_render() (with their
helpers) against stubs that log every lock, trylock, clock read, ref,
stop, signal, render, gs_*/matrix call, alignment, timestamp read, mix
and memcpy with its arguments (floats as bits), plus the final state.
4,008,640 scenarios, byte-identical logs under GCC -O2 and clang -O0.

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#2173
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…ansition-render.c

Behaviour-neutral extraction before the Rust port. The render and audio
step decisions of the transitions in obs-source-transition.c.h move to
libobs/obs-source-transition-render.c.h, declared in the new
libobs/obs-source-transition-render.h without EXPORT (hidden):

- obs_source_transition_render_video_due() and _video_stop(): the stop
  step of obs_transition_video_render2() and
  obs_transition_video_render_direct(). Due at t >= 1 while the video
  transitions (a NaN t is never due). Then the video ends, and the
  transition stops unless its audio runs on; the direct render first
  ends the audio of a transition that is not active (eb35b07). render2
  has no inactive rule and passes active as true.
- obs_source_transition_render_draw(): render2's draw choice: both
  children through the callback while the video transitions with the
  textures locked and a callback set; else source B while the audio
  transitions; else source A.
- obs_source_transition_render_direct_index(): the child the direct
  render draws: B only for OBS_TRANSITION_SOURCE_B while transitioning.
- obs_source_transition_render_audio_stop(): the stop step of
  obs_transition_audio_render() and transition_stop_audio() (removed):
  with a timestamp at t >= 1 while the audio transitions; without one
  only once the video has ended too (7af9c2d). The audio ends, and the
  transition stops unless its video runs on.
- obs_source_transition_render_audio_mix(): mix each child that is there
  while the audio transitions, else copy source A.
- obs_source_transition_render_audio_valid() and _audio_place(): the
  checks of transition_process_audio(): a child, its audio not pending
  and not duplicated; a timestamp that is not 0 (3191f5d); the start
  frame, ns_to_audio_frames(sample_rate, ts - min_ts) written out as its
  util_mul_div64() so the file needs only util_uint64.h, and the
  "> AUDIO_OUTPUT_FRAMES" check.
- obs_source_transition_render_offset(): the scaled child size of
  recalculate_transition_matrix() and the offsets it passes to
  add_alignment(), before the (int) casts: each product rounded to float,
  then subtracted, as before.

Why nine functions and not one step each: every call keeps its place and
its condition. obs_source_active() is asked only once the video step is
due, so the due test is its own function; obs_source_get_audio_mix() is
read between the timestamp check and the start-frame check, so the place
says NONE, LATE or MIX and the C returns before or after the read;
calc_time() is asked only with a timestamp, so the audio stop step takes
whether there is one. The re-read of the children after the audio stop
step stays where it was.

obs-source-transition.c.h keeps the locks, get_video_time() and
calc_time(), copy_transition_state() and the refs, obs_transition_stop(),
handle_stop() and the signals, obs_source_active(), the drop of removed
children (obs_source_removed() is asked only of a child that is there)
and transition_calc_min_ts() with its pending check and obs-rust#130's snapshot
read (as obs-rust#2055 left them), the texture trylock, render_child(), the
callbacks, the cx/cy check, mix_child() and get_sample_time(), the mixer
loop, the memcpy(), the (int) casts, add_alignment() and the matrix
calls. transition_process_audio() now reads audio_is_duplicated also
when audio_pending is set (two plain field reads of a child it holds a
reference to); nothing else is read that was not read before.

The target crosses as int; obs-source-transition.c.h checks at compile
time that the header's values are OBS_TRANSITION_SOURCE_B and
AUDIO_OUTPUT_FRAMES and that enum obs_transition_target is int-sized.

Checked against the unchanged functions with a scratch harness (not
committed): master's and the moved recalculate_transition_matrix(),
obs_transition_video_render2(), obs_transition_video_render_direct(),
transition_process_audio() and obs_transition_audio_render() (with their
helpers) against stubs that log every lock, trylock, clock read, ref,
stop, signal, render, gs_*/matrix call, alignment, timestamp read, mix
and memcpy with its arguments (floats as bits), plus the final state.
4,008,640 scenarios, byte-identical logs under GCC -O2 and clang -O0.

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#2173
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…ansition-render.c

Behaviour-neutral extraction before the Rust port. The render and audio
step decisions of the transitions in obs-source-transition.c.h move to
libobs/obs-source-transition-render.c.h, declared in the new
libobs/obs-source-transition-render.h without EXPORT (hidden):

- obs_source_transition_render_video_due() and _video_stop(): the stop
  step of obs_transition_video_render2() and
  obs_transition_video_render_direct(). Due at t >= 1 while the video
  transitions (a NaN t is never due). Then the video ends, and the
  transition stops unless its audio runs on; the direct render first
  ends the audio of a transition that is not active (eb35b07). render2
  has no inactive rule and passes active as true.
- obs_source_transition_render_draw(): render2's draw choice: both
  children through the callback while the video transitions with the
  textures locked and a callback set; else source B while the audio
  transitions; else source A.
- obs_source_transition_render_direct_index(): the child the direct
  render draws: B only for OBS_TRANSITION_SOURCE_B while transitioning.
- obs_source_transition_render_audio_stop(): the stop step of
  obs_transition_audio_render() and transition_stop_audio() (removed):
  with a timestamp at t >= 1 while the audio transitions; without one
  only once the video has ended too (7af9c2d). The audio ends, and the
  transition stops unless its video runs on.
- obs_source_transition_render_audio_mix(): mix each child that is there
  while the audio transitions, else copy source A.
- obs_source_transition_render_audio_valid() and _audio_place(): the
  checks of transition_process_audio(): a child, its audio not pending
  and not duplicated; a timestamp that is not 0 (3191f5d); the start
  frame, ns_to_audio_frames(sample_rate, ts - min_ts) written out as its
  util_mul_div64() so the file needs only util_uint64.h, and the
  "> AUDIO_OUTPUT_FRAMES" check.
- obs_source_transition_render_offset(): the scaled child size of
  recalculate_transition_matrix() and the offsets it passes to
  add_alignment(), before the (int) casts: each product rounded to float,
  then subtracted, as before.

Why nine functions and not one step each: every call keeps its place and
its condition. obs_source_active() is asked only once the video step is
due, so the due test is its own function; obs_source_get_audio_mix() is
read between the timestamp check and the start-frame check, so the place
says NONE, LATE or MIX and the C returns before or after the read;
calc_time() is asked only with a timestamp, so the audio stop step takes
whether there is one. The re-read of the children after the audio stop
step stays where it was.

obs-source-transition.c.h keeps the locks, get_video_time() and
calc_time(), copy_transition_state() and the refs, obs_transition_stop(),
handle_stop() and the signals, obs_source_active(), the drop of removed
children (obs_source_removed() is asked only of a child that is there)
and transition_calc_min_ts() with its pending check and obs-rust#130's snapshot
read (as obs-rust#2055 left them), the texture trylock, render_child(), the
callbacks, the cx/cy check, mix_child() and get_sample_time(), the mixer
loop, the memcpy(), the (int) casts, add_alignment() and the matrix
calls. transition_process_audio() now reads audio_is_duplicated also
when audio_pending is set (two plain field reads of a child it holds a
reference to); nothing else is read that was not read before.

The target crosses as int; obs-source-transition.c.h checks at compile
time that the header's values are OBS_TRANSITION_SOURCE_B and
AUDIO_OUTPUT_FRAMES and that enum obs_transition_target is int-sized.

Checked against the unchanged functions with a scratch harness (not
committed): master's and the moved recalculate_transition_matrix(),
obs_transition_video_render2(), obs_transition_video_render_direct(),
transition_process_audio() and obs_transition_audio_render() (with their
helpers) against stubs that log every lock, trylock, clock read, ref,
stop, signal, render, gs_*/matrix call, alignment, timestamp read, mix
and memcpy with its arguments (floats as bits), plus the final state.
4,008,640 scenarios, byte-identical logs under GCC -O2 and clang -O0.

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#2173
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 24, 2026
* libobs: Move the transition render and audio steps into obs-source-transition-render.c

Behaviour-neutral extraction before the Rust port. The render and audio
step decisions of the transitions in obs-source-transition.c.h move to
libobs/obs-source-transition-render.c.h, declared in the new
libobs/obs-source-transition-render.h without EXPORT (hidden):

- obs_source_transition_render_video_due() and _video_stop(): the stop
  step of obs_transition_video_render2() and
  obs_transition_video_render_direct(). Due at t >= 1 while the video
  transitions (a NaN t is never due). Then the video ends, and the
  transition stops unless its audio runs on; the direct render first
  ends the audio of a transition that is not active (eb35b07). render2
  has no inactive rule and passes active as true.
- obs_source_transition_render_draw(): render2's draw choice: both
  children through the callback while the video transitions with the
  textures locked and a callback set; else source B while the audio
  transitions; else source A.
- obs_source_transition_render_direct_index(): the child the direct
  render draws: B only for OBS_TRANSITION_SOURCE_B while transitioning.
- obs_source_transition_render_audio_stop(): the stop step of
  obs_transition_audio_render() and transition_stop_audio() (removed):
  with a timestamp at t >= 1 while the audio transitions; without one
  only once the video has ended too (7af9c2d). The audio ends, and the
  transition stops unless its video runs on.
- obs_source_transition_render_audio_mix(): mix each child that is there
  while the audio transitions, else copy source A.
- obs_source_transition_render_audio_valid() and _audio_place(): the
  checks of transition_process_audio(): a child, its audio not pending
  and not duplicated; a timestamp that is not 0 (3191f5d); the start
  frame, ns_to_audio_frames(sample_rate, ts - min_ts) written out as its
  util_mul_div64() so the file needs only util_uint64.h, and the
  "> AUDIO_OUTPUT_FRAMES" check.
- obs_source_transition_render_offset(): the scaled child size of
  recalculate_transition_matrix() and the offsets it passes to
  add_alignment(), before the (int) casts: each product rounded to float,
  then subtracted, as before.

Why nine functions and not one step each: every call keeps its place and
its condition. obs_source_active() is asked only once the video step is
due, so the due test is its own function; obs_source_get_audio_mix() is
read between the timestamp check and the start-frame check, so the place
says NONE, LATE or MIX and the C returns before or after the read;
calc_time() is asked only with a timestamp, so the audio stop step takes
whether there is one. The re-read of the children after the audio stop
step stays where it was.

obs-source-transition.c.h keeps the locks, get_video_time() and
calc_time(), copy_transition_state() and the refs, obs_transition_stop(),
handle_stop() and the signals, obs_source_active(), the drop of removed
children (obs_source_removed() is asked only of a child that is there)
and transition_calc_min_ts() with its pending check and obs-rust#130's snapshot
read (as obs-rust#2055 left them), the texture trylock, render_child(), the
callbacks, the cx/cy check, mix_child() and get_sample_time(), the mixer
loop, the memcpy(), the (int) casts, add_alignment() and the matrix
calls. transition_process_audio() now reads audio_is_duplicated also
when audio_pending is set (two plain field reads of a child it holds a
reference to); nothing else is read that was not read before.

The target crosses as int; obs-source-transition.c.h checks at compile
time that the header's values are OBS_TRANSITION_SOURCE_B and
AUDIO_OUTPUT_FRAMES and that enum obs_transition_target is int-sized.

Checked against the unchanged functions with a scratch harness (not
committed): master's and the moved recalculate_transition_matrix(),
obs_transition_video_render2(), obs_transition_video_render_direct(),
transition_process_audio() and obs_transition_audio_render() (with their
helpers) against stubs that log every lock, trylock, clock read, ref,
stop, signal, render, gs_*/matrix call, alignment, timestamp read, mix
and memcpy with its arguments (floats as bits), plus the final state.
4,008,640 scenarios, byte-identical logs under GCC -O2 and clang -O0.

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#2173

* test: Characterize the transition render and audio steps

test_transition_render_steps, 16 cases, compiles
libobs/obs-source-transition-render.c.h (ASan and UBSan) and calls only
its nine functions. It pins:

- the video stop step's due test at 14 values of t: 0, -0, the smallest
  subnormal, 0.5, just below 1, 1, just above 1, 2, +-inf, -1, and quiet,
  negative and signalling NaNs (only t >= 1 is due; a NaN t never ends a
  transition), with and without the video transitioning;
- the stop step for every audio and active flag, and as render2 and the
  direct render apply it at t just below, at and above 1 and NaN, for every
  video, audio (and, direct, active) combination: which flags end, the
  video stop signal and obs_transition_stop();
- render2's draw choice for all 16 flag combinations, including a video
  transition whose textures are busy or which has no callback (it draws B
  only while its audio transitions, else A);
- the direct render's child for both transitioning states and targets A,
  B, 2, -1 and the ends of int (only B while transitioning draws B);
- the audio stop step with a timestamp at the 14 values of t, without one
  (only once the video has ended, 7af9c2d; t is not read), and for every
  pair of children absent, removed, pending, without a timestamp or with
  one, as obs_transition_audio_render() passes them;
- the mix or copy and the validity check for every flag;
- the start frame: a timestamp of 0 (nothing more read, pos untouched);
  the frame edges at 48 kHz and 44.1 kHz around min_ts, 1023, 1024 and
  1025 frames, a start at AUDIO_OUTPUT_FRAMES mixing (">", where the
  scene uses ">="), sample rates of 0, 1 and 1e9; a timestamp below
  min_ts wrapping to a late frame (885443715538058 with a 64-bit size_t),
  and a min_ts near UINT64_MAX wrapping back to frame 0;
- the offsets for each scale type's scale (32x16 in 64x64 as in
  test_transition_scale_type, 1280x720, 1000x1000 and 3840x2160 in
  1920x1080), the product rounded to float before the subtract (3 times
  0x3eaaaaab gives exactly 0; 1053 in 1080 gives -2^-13), transitions of
  size 0, infinite and NaN scales, and u32-sized sizes.

Every pinned float is one rounded multiply and one subtract, the same on
every platform the tests run on.

Kept, characterized, not endorsed:
- a NaN t never ends a transition (render or audio stop step);
- a busy texture lock or a missing callback draws the old scene, A,
  during a video-only transition;
- a start frame of exactly AUDIO_OUTPUT_FRAMES mixes 0 frames;
- the audio re-read after the stop step does not drop removed children;
  transition_process_audio() then checks only pending and duplicated;
- a timestamp below min_ts wraps and is placed late; a min_ts near
  UINT64_MAX wraps back to an early frame.

It is in the test graph in name order, with libm except on MSVC, and the
tests.d fragment says what it pins. Passes 16/16 on the C backend.

Refs obs-rust#2173

* obs-source: Add Tier 1 tests for the transition render and audio steps port

The transition_render module of rust/obs-source, with its types and
todo!() bodies, and its Tier 1 tests (tests/source/transition_render.rs,
run by obs-data's obs-tests-core):

- steps_*: test/cmocka/test_transition_render_steps.c 1:1, the 16 cases
  under the same names and in the same order, with the same inputs and
  expectations, the "characterized, not endorsed" ones included. The stop
  steps run as the render functions apply them under the lock.
- edge_*: the draw choice, the direct index and the mix against their
  rules over every flag and any i32 target; the start frame at the first
  ns of every frame up to 1100 and the ns before it at eight sample rates;
  proptests (20,000 cases each) for the due test and the audio stop step
  over any f32 bit pattern (half near 1), the video stop step over every
  flag, the start frame against an exact 128-bit model over any u64
  min_ts and difference wherever util_mul_div64() cannot overflow (a rate
  up to 1e9), its order in ts, and the offsets against an f64 model of one
  rounded multiply and one rounded subtract over f32 bit patterns and
  whole-number sizes (never a wide float range, obs-rust#1951).

The safe API differs from the C header where Rust says it better: the
audio stop step takes Option<f32> (None: no timestamp, the clock is not
asked), the place carries its frame (Place::Late(pos), Place::Mix(pos)),
the mix is a struct of three bools, and the direct index is a usize.

soldr cargo test -p obs-data --test obs-tests-core source::transition_render
fails 25 of 25, each with "not yet implemented".

Refs obs-rust#2173

* obs-source: Port the transition render and audio steps

Ports libobs/obs-source-transition-render.c.h, the render and audio step
decisions of the transitions, to the transition_render module of
rust/obs-source. obs-source-transition.c.h stays C: the locks, the clock
reads, copy_transition_state() and the refs, obs_transition_stop(),
handle_stop() and the signals, obs_source_active(), the removed and
pending checks and the timestamp reads, the texture trylock, the renders
and callbacks, mix_child() and get_sample_time(), the mixer loop and the
memcpy(), the (int) casts, add_alignment() and the matrix calls. The
hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/transition_render.rs):
- video_due(): t >= 1.0 in f32 while the video transitions (NaN never).
- video_stop(): StopAudio when not active, else Stop without the audio,
  else End.
- draw(): Callback with the video, the lock and a callback; else B while
  the audio transitions; else A.
- direct_index(): 1 only for TARGET_B while transitioning.
- audio_stop(Option<f32>, ...): with a t, t >= 1.0 and the audio; without
  one, the audio with the video ended; then Stop without the video, else
  End.
- audio_mix() and audio_valid(): the C's flags.
- audio_place(): None for a timestamp of 0; else the frame
  ns_to_audio_frames(rate, ts - min_ts) with the difference wrapping, as
  Late past AUDIO_FRAMES and Mix up to it. mul_div64() is the C's
  util_mul_div64(): the wrapping u64 formula, or the exact 128-bit
  quotient on MSVC x64 (the new platform module's MUL_DIV64_IS_UDIV128,
  as obs-scene's), written out so the crate still depends on no other
  crate. The frame is cast to usize as the C casts to size_t (to_size(),
  the one as-conversion, commented).
- offset(): each product rounded to f32, then the subtract (Rust never
  fuses them).
The module denies clippy's arithmetic, indexing, as, unwrap, expect,
panic, unimplemented and unreachable lints, keeps no state and takes no
lock.

Shims (src/ffi/source_transition_render.rs): plain values in and out; the
steps as the C's enum values, the mix as its bits, the offset as the
repr(C) struct. The place writes the frame through the size_t * only for
Late and Mix, as the C does. Intentional difference: a null pos is not
written (the C writes through it; libobs passes its own size_t).

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-transition-render (LIBOBS_PORTED_SOURCES), whose PCH
is only obs-source-transition-render.h and util/util_uint64.h;
libobs.pch.h keeps the header for obs-source-transition.c. The nine
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 nine
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_transition_render_steps still compiles the C file into itself,
unchanged. The new test_transition_render_steps_rust builds the same test
source against libobs_rust-static alone (the test includes only the
header, so no SIMDe and no libobs include directories), with libm except
on MSVC. It passes 16 of 16 on the hybrid tree.

Tier 1 source::transition_render passes 25/25.

Refs obs-rust#2173

* obs-source: Check the transition render and audio steps port against the C

Tier 3 for obs-source::transition_render. The oracle
(rust/obs-c-oracle/oracle/source_transition_render.c) compiles the
unmodified libobs/obs-source-transition-render.c.h with its nine
functions renamed to oracle_*, plus a table of its constants and layouts.
The renames also rename struct obs_source_transition_render_offset and
enum obs_source_transition_render_audio_place, whose tags share their
function's name, so the table names the renamed tags.

source::transition_render_parity (13 tests and one ignored) compares the
shims with the C, the frame through a size_t * pre-filled with the same
guard on both sides (written or not), floats by their bits:

- the header's constants, enum values and sizes, the offset struct's size
  and field offset, int, bool and size_t, and FLT_EVAL_METHOD 0;
- every flag combination of the video stop step, the draw choice, the mix
  and the validity check; the direct index over both states and every
  target from -70000 to 70000, every 65521st i32 and the ends; the due
  test and the audio stop step over every flag at 40 special t; the three
  stop steps as the render functions apply them;
- the due test and the audio stop step over every 4099th f32 bit pattern
  and every pattern within 2^16 of 1.0 (every f32, run once with --release
  --ignored: all equal);
- the place over every combination of 26 special timestamps for ts and
  min_ts and 14 sample rates up to usize::MAX (where util_mul_div64()
  wraps), and around the first ns of every frame up to 1100 at eight
  rates, above and below min_ts;
- the offsets over 14 special sizes and scales on each side and both
  sides at once;
- proptests (20,000 cases each) over full-range u64 timestamps (half near
  min_ts, either side), any usize rate, f32 bit patterns for t, sizes and
  scales (never a wide Range<f32>, obs-rust#1951) and any i32 target.

In the offsets only, where one float operation has two NaN operands, which
NaN comes out depends on the operand order the compiler picks (obs-rust#2021);
there both sides must give a NaN. Everything else is bit for bit. The
intentional difference (a null pos is not written by the shim) is checked
on the shim alone.

Tier 1 and the parity module pass 38 of 38 (one ignored), 10 runs in a row
with fresh seeds. No proptest regression file came up.

Refs obs-rust#2173

* docs: Record the transition render and audio steps port

- rust/README.md: the obs-source crate row names
  libobs/obs-source-transition-render.c.h, and a new ported-unit row says
  what moved, what stays C and in which order, how values cross, what is
  kept and characterized, the intentional difference (a null pos is not
  written), which tests see the swap and what Tier 3 compares.
- rust/obs-source: the crate description and the lib.rs docs list the new
  module.
- rust/obs-c-oracle/benches/port_comparison/source_transition_render.rs,
  in the sorted mod and modules![...] lists: render (1024 frames of the
  video steps, the draw choice, the direct index and two offsets) and
  audio (1024 audio ticks of the stop step, the mix, and two children's
  checks and start frames). Both sides must give the same checksum; the
  fixtures allocate nothing, so the module's bmem check (obs-rust#2159) holds.
  Measured on x86_64 Linux, GCC, load average 6: render 12.46 us Rust,
  12.83 us C (0.97x); audio 10.81 us Rust, 11.11 us C (0.97x).

docs/rust-port/naming.md needs no row: libobs/obs-source*.c.h already
maps to rust/obs-source.

Fixes obs-rust#2173
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 transition clock and fit in obs-source-transition.c to obs-source

2 participants