Skip to content

obs-source: Port the source audio input steps - #2021

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

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-audio-input

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1994

What and why

Ports the audio input steps of process_audio() and reset_resampler() (libobs/obs-source.c.h) to rust/obs-source, as a new module audio_input, next to audio_timing (#1936) and color_space (#1990). Every audio packet of every source goes through these steps: whether the resampler restarts, whether one is needed at all, and the source's balance and Force Mono. Two of the rules are ours and were fixed for real users: #546 (the resampler tail on a Media Source restart) and the clock drift compensation (#270). Each is pinned in the characterization and checked by a mutant, and the real-libobs tests for both pass on every lane.

Six commits:

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

    • obs_source_audio_input_discontinuous(): with a resampler and next_resample_in_ts set, a packet 70 ms or more from it, either way, starts a new stream (libobs: Drop audio held in the resampler when a new stream starts #546);
    • obs_source_audio_input_reset_needed(): the packet's rate, format or speakers differ, the compensation flag changed, or the discontinuity;
    • obs_source_audio_input_resampler_needed(): no resampler only for the output's rate, format and speakers with the compensation off (the clock drift fix);
    • obs_source_audio_input_balance_needed() and _mono_needed(): the balance gate and the Force Mono gate;
    • obs_source_audio_input_balance() and _downmix(): process_audio_balancing() and downmix_to_mono_planar(), the same loops on a plain float ** and counts.

    process_audio() keeps its order: the discontinuity, the reset check and reset_resampler(), then next_resample_in_ts, audio_failed, the resampler or the copy, and the two gates. The next_resample_in_ts computation is unchanged and stays in obs-source.c.h. reset_resampler() keeps its stores, its second load of async_compensation, the resampler's creation and the log line. The clock drift filter and audio_resampler_set_compensation_error() are untouched; the steps read only the compensation flag, as a bool. The file stays in libobs.core-source for this commit.

  2. Characterization: test_source_audio_input_steps, 22 cases, compiles the moved file. Plus test_source_audio_balance_mono, 4 real-libobs cases (see below).

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

  4. GREEN: the safe core and #[no_mangle] shims, in a new libobs.ported-source-audio-input group with its own PCH (the header and <math.h>). 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:

  • the discontinuity at 70 ms - 1 ns (continues), 70 ms and 70 ms + 1 ns (restarts), later and earlier, the restart test's 20 ms, 100 ms and 1 s gaps, no resampler, next_resample_in_ts == 0 and an end of 1;
  • each reset trigger on its own: the rate, the format, the speakers, the compensation turned on and turned off, and the discontinuity, and a zeroed sample_info against each field;
  • the resampler rule: none for the output's format, one with the compensation, one for each difference;
  • the balance gate at 0.49f, 0.50f, 0.51f, 0.495f, the floats just inside and just outside, 0.25, 0.75, 0, -0 and 1; never into a mono output or from a source that is not stereo. The Force Mono gate counts only its bit;
  • each law on known samples, bit for bit (the sine law over nine balances, and one balance for which the double product and a float-only product give different sinf arguments), only the first frames and only planes 0 and 1 written, and an unknown law, which reads nothing;
  • the downmix of 2, 6 and 8 channels, in plane order ((1 + 1e8) - 1e8 is 0) and scaled by the f32 reciprocal, not divided (they differ in the last bit for the sample used), and 0 frames, which read nothing.

Kept, characterized, not endorsed:

  • the distance is not modular, so timestamps on either side of u64::MAX restart;
  • a packet of all zeros against a zeroed sample_info resets nothing;
  • a NaN balance is not balanced (the compares skip it); -1, 2 and ±inf are, giving negative gains, gains above 1 and NaN. Only the API can set them; every UI and obs-websocket value is in 0 to 1 (R5-4 in the plan, not fixed here);
  • the downmix of one channel scales by 1, and of none scales the first plane by +inf. The gate never gets there.

The sine law. sinf((float)((double)(1.0f - balance) * (M_PI / 2.0))) mixes f32 and f64. The port keeps the casts in the same order: 1.0f32 - balance in f32, widened to f64, times the f64 PI / 2.0, rounded to f32 with as, then f32::sin, the platform's sinf (the hybrid calls glibc's sinf, as the C does). Tier 3 compares the gains for all 2^32 balances in a release build, and they are the same bits, so the gains stay in Rust; C does not compute them. The moved file checks that M_PI is <math.h>'s double, not graphics/math-defs.h's float fallback, and its PCH defines _USE_MATH_DEFINES as cmake/pch/obs-c-std.h does for the core.

  • When fields are read. async_compensation is now loaded on every packet, before the reset check. Before, the C's || loaded it only when the rate, format and speakers matched. It is an atomic read with no side effect. The output's channel count is still read twice (the mono check and the downmix), and source->balance twice (the gate and the law).

  • Intentional difference: a null data, or a null plane the C would read, does nothing in the shims; the C dereferences it. libobs passes source->audio_data.data, which copy_audio_data() filled.

  • NaN payloads. Where one multiply or add has two NaN operands (a NaN sample times a NaN gain, or two NaN samples added), which NaN comes out depends on the operand order the compiler picks: x86 returns the first operand's. The C builds already differ there: the same C at -O0 and at -O2 gives a different NaN for a NaN sample times a NaN balance. Tier 3 asks for a NaN in that case and the exact bits everywhere else, including every single-NaN case. This is in the parity test header and the README row.

  • The shims read what the C reads: nothing for zero frames or an unknown law (not even the plane pointers), planes 0 and 1 for a law, and planes 0 to channels - 1 for the downmix (plane 0 alone for 0 channels). The downmix holds two planes at a time, so it allocates nothing for any channel count. The planes must not overlap; libobs's never do.

  • The _rust variant links. test_source_audio_input_steps_rust builds the unchanged test source against libobs_rust-static, as test_source_audio_timing_steps_rust does. The test includes only the new header and calls only the seven functions, so this variant runs the shims, the format passed by value and the plane pointers included. It passes 22 of 22 on the hybrid tree.

  • The real-libobs case. test_source_audio_balance_mono: a stereo source at the output's rate (so no resampler) sends one packet, and an audio capture callback reads it back bit for bit: centred it is unchanged, at 0.25 it is the sine law, with Force Mono both channels are the mean, and with both it is balanced, then the mean. It is in the core suite on Linux, like the other core audio tests, so no pinned suite count changes.

  • 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, and in the hybrid they come from liblibobs_rust.a.

  • Performance (port_comparison, x86_64 Linux, GCC, not pinned, a loaded machine; median):

    • decisions: 9.37 µs Rust, 13.49 µs C per 1024 packets (0.70x);
    • sine law: 1.06 µs Rust, 1.70 µs C per 480-frame stereo packet (0.62x);
    • downmix: 1.13 µs Rust, 2.20 µs C for stereo (0.51x); 3.30 µs Rust, 6.45 µs C for 7.1 (0.51x).

    The Rust loops are vectorized and give the same bits.

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

Observed, not changed: process_audio() always passes the sine law, so the square and linear laws are reachable only from the moved function itself. For a balance above 1, libm's sqrtf of a negative number also sets errno, which f32::sqrt does not; libobs never reads it, and the square law is not reached.

RED → GREEN

  • RED commit: 04f207be7. soldr cargo test -p obs-data --test obs-tests-core source::audio_input fails 32 of 32, every failure "not yet implemented" (the 22 ported steps_* cases and 10 edge_* cases, eight of them proptests). Run on 03c8b650d, the same commit before the last rebase (the rebase touched nothing in rust/obs-source).
  • GREEN: 18da6f659. Tier 1 passes 32/32 (33/33 from 6e8f4ccdb, which adds one case the mutation check asked for). Tier 2 on b5662b08a (the head before the final rebase), based on b55301b7b, under a private Xvfb -noreset with dbus-run-session, a fresh HOME and ulimit -c 0. libobs-c-reference was built with ninja libobs-c-reference before each run and is newer than the head commit.
Test C backend hybrid libobs-c-reference
test_audio_restart_resampler_tail 4/4 4/4 4/4
test_audio_clock_drift 6/6 6/6 6/6
test_media_sync_offset_restart 6/6 6/6 6/6
test_async_delay_audio 6/6 6/6 6/6
test_source_audio_balance_mono 4/4 4/4 4/4
test_source_audio_input_steps 22/22 22/22 22/22
test_source_audio_input_steps_rust (not built: needs the Rust archive) 22/22 22/22
full ctest (on 5a67dea90, see below) 405/405, 0 skipped 412/412, 0 skipped 412/412, 0 skipped

The full ctest ran on 5a67dea90, the same six commits based on 8eb7cfd17; the last rebase onto b55301b7b only moved my entries in test-graph.json, ci/test_ported_sources.py, source-sets.json and the bench list into name order (#2010, #2011). After it, the table's tests, the quick checks and --test-diff were re-run on the head.

Rebased once more onto 446d373e5 right before posting (no conflict), then onto 5db021c7a after #2018 (only the rust/README.md rows conflicted). After it, source:: in obs-tests-core (162 passed, 1 ignored), ci/test_build_groups.py, ci/test_ported_sources.py and the lint lane were re-run and pass.

One rerun to report: at the GREEN commit before the rebases, on the C backend, test_async_delay_audio failed once (tone_continuous_with_3s_video_delay: "the tone was broken up 1 times in 4000 ms", line 466), with the machine at a load average of about 30 on 16 cores. The two reruns right after passed 6/6, and so did every later run on all three builds. The C backend compiles the same C there as at the move commit, and the test does not use balance or Force Mono, so this is the real-time flake class of #838. The test is unchanged.

  • Tier 3, 6e8f4ccdb (source::audio_input_parity, 14 tests and one ignored). Runs:

    • the format's layout and the constants against the C header;
    • the gates exhaustively over their flags: the discontinuity over both resampler states and every pair of 14 special timestamps; the reset condition over the three bools and 64 × 64 formats (each field equal or not, values outside the enums too), the resampler rule over the same; the balance gate over both output kinds, eleven speaker values and 44 special balances; the mono gate over every single bit, none and all;
    • proptests (20,000 cases each) over full-range u64 timestamps, any formats, any flags and balances drawn from bit patterns (finite, ±0, ±inf, NaN);
    • the laws (all three and five unknown values) and the downmix (0 to 8 channels) over two copies of nine planes with guard samples, every sample's bits compared, for the special values and for proptests over random samples, frame counts and balances, all drawn from bit patterns, never from a wide Range<f32> (test: extent_parity proptests can trip proptest's float range assertion (Windows merge group) #1951);
    • the sine gains over 5.2 million strided balances, and all 2^32 in the ignored exhaustive_sine_gains (run once with --release --ignored: it passed, 119 s);
    • zero frames and unknown laws read nothing on either side.

    Tier 1 and the parity module passed 10 runs in a row with fresh seeds. The first parity run found the NaN-pair case (samples [0, 0, NaN], balance NaN, linear law) before the rule above was written; its regression file is committed.

  • Mutation check (once, not committed): 48 mutants, 45 fail, 3 survive and are equivalent.

Mutant Tier 1 Tier 3
discontinuity at > threshold 3/33 fail 2/14 fail
no end-set check / no resampler check 2 / 2 2 / 2
distance one way (modular) 4 2
never discontinuous (#546 reverted) 4 2
reset ignores the rate / format / speakers / compensation / discontinuity 2 / 3 / 2 / 2 / 2 2 each
no resampler for the compensation (clock drift fix reverted) 2 2
resampler rule ignores the rate / speakers / format 2 each 2 each
balance gate at >= 0.51 / <= 0.49 3 / 3 2 / 2
NaN balanced 2 2
balance gate ignores the mono output / the speakers 2 / 2 2 / 2
mono gate on any flag / ignores the mono output 2 / 2 2 / 2
sine law product in f32 only 3 3
sine law sin in f64, then rounded 3 3
sine law 1 - balance in f64 1 3
sine / square / linear gains swapped 4 / 3 / 3 3 / 2 / 2
square and linear laws swapped 5 2
an unknown law is the sine law 2 2
right plane not balanced 5 3
downmix subtracts 3 2
downmix divides instead of the reciprocal 4 2
0 channels scaled as 1 2 2
downmix() sums in reverse / one plane too few / no copy back 2 / 3 / 4 0 (the shim calls the loops directly)
downmix() adds one plane too many 1 (with the case added in 6e8f4ccdb; 0 before) 0
shim: one frame short / planes swapped 0 (Tier 1 does not reach the shims) 3 / 3
shim: balance gate speakers fixed at stereo 0 2
shim: resampler rule reads the source as the output 0 2
shim: downmix scaled by the plane count / last plane skipped / one copy fewer 0 2 each
shim: discontinuity timestamps swapped (moves the end-set check) 0 2
equivalent: the gate's edges as f64 0.49 and 0.51 (no f32 lies between 0.49f and 0.49, or 0.51f and 0.51) 0 0
equivalent: the shim's format and speakers swapped in both formats (only whole formats are compared) 0 0
equivalent: the shim's two compensation flags swapped (!= is symmetric) 0 0
  • Real app: no UI, protocol or logging change.

Other runs, on the head 2d840cf40:

  • uv run ci/local_gate.py --test-diff origin/master: ok (workspace scope, 3013 of 3013 passed, 2 skipped: ignored tests, exhaustive_sine_gains among them). --lane lint: ok. --lane cross: ok.
  • ci/test_build_groups.py, ci/test_ported_sources.py and ci/test_oracle_standins.py: OK.
  • ci/test_sorted_shared_lists.py and ci/test_test_graph_sorted.py (new on master): OK.
  • The real-libobs tests and both steps tests also passed on all three builds at the GREEN commit before the rebases, and on the C backend at the characterization commit (see the note above about test_async_delay_audio).

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 01:39
Behaviour-neutral extraction before the Rust port. The decisions and the
two sample loops of process_audio() and reset_resampler() in obs-source.c.h
move to libobs/obs-source-audio-input.c.h, declared in the new
libobs/obs-source-audio-input.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_audio_input_discontinuous(): the restart rule of obs-rust#546. With a
  resampler and next_resample_in_ts set, a packet 70 ms or more from it,
  either way, starts a new stream.
- obs_source_audio_input_reset_needed(): the reset condition. The
  packet's rate, format or speakers differ from sample_info, the clock
  drift compensation flag changed, or the packet is discontinuous.
- obs_source_audio_input_resampler_needed(): the "no resampler needed" rule
  of the clock drift fix. Same rate, format and speakers as the output and
  the compensation off; otherwise reset_resampler() creates one.
- obs_source_audio_input_balance_needed() and _mono_needed(): the balance
  gate (output not mono, a stereo source, balance above 0.51f or below
  0.49f) and the Force Mono gate (output not mono, the flag set).
- obs_source_audio_input_balance() and _downmix(): process_audio_balancing()
  and downmix_to_mono_planar(), the same loops on a plain float ** and
  counts. The sine law keeps its casts as written.

process_audio() keeps its order: the discontinuity, the reset check and
reset_resampler(), then next_resample_in_ts (unchanged, still computed
here), audio_failed, the resampler or the copy, and the two gates.
reset_resampler() keeps the stores, the second load of async_compensation,
the resampler's creation and the log line. The clock drift filter and
audio_resampler_set_compensation_error() are untouched. The output's
channel count is still read twice, for the mono check and for the
downmix, and source->balance still for the gate and for the law.

One read moves: async_compensation is now loaded on every packet before
the reset check, where the C's || only loaded it when the format matched.
It is an atomic read with no side effect.

obs-source.c.h checks at compile time that the header's threshold,
SPEAKERS_STEREO, OBS_SOURCE_FLAG_FORCE_MONO and balance law values are
obs-source.c's, and that enum audio_format and enum speaker_layout are
int-sized. The moved file checks that M_PI is <math.h>'s double, not
graphics/math-defs.h's float fallback. uint64_diff() goes with its only
user.

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#1994
test_source_audio_input_steps compiles libobs/obs-source-audio-input.c.h
(ASan and UBSan) and calls only its seven hidden functions. 22 cases:

- obs_source_audio_input_discontinuous() (obs-rust#546): 70 ms - 1 ns continues,
  70 ms and 70 ms + 1 ns restart, later and earlier; the restart test's
  20 ms, 100 ms and 1 s gaps; nothing without a resampler or with
  next_resample_in_ts == 0, and an end of 1 is set.
- obs_source_audio_input_reset_needed(): no change, then the rate, the
  format, the speakers, the compensation turned on and off, and the
  discontinuity, each on its own; a zeroed sample_info against each field.
- obs_source_audio_input_resampler_needed() (the clock drift fix): none
  for the output's format without compensation, one with it, and one for
  each difference.
- The balance gate: 0.49f, 0.50f, 0.51f, 0.495f and the floats just inside
  are centred; the floats just outside, 0.25, 0.75, 0, -0 and 1 balance;
  never into a mono output or from a source that is not stereo. The Force
  Mono gate: only its bit counts, never into a mono output.
- The laws, bit for bit: the sine law over nine balances (the gains with
  the C's casts, fully left and right exactly 1 and 0), and a balance for
  which the double product and a float-only product give different sinf
  arguments; the square and linear laws over five balances and on known
  samples; only the first frames and only planes 0 and 1 are written; an
  unknown law and zero frames read nothing, not even the plane pointers.
- The downmix of 2, 6 and 8 channels (the C's sum, times 1 / channels),
  known samples, only the first frames; the sum is in plane order
  ((1 + 1e8) - 1e8 is 0) and scaled by the float reciprocal, not divided
  (they differ in the last bit for the sample used); zero frames read
  nothing.

Characterized, not endorsed:
- the distance is not modular, so timestamps on either side of
  UINT64_MAX restart;
- a packet of all zeros against a zeroed sample_info resets nothing;
- NaN balances are not balanced (the compares skip them); -1, 2 and
  +-inf are, giving negative gains, gains above 1 and NaN;
- the downmix of one channel scales by 1, and of none scales the first
  plane by +inf (the gate never gets there).

test_source_audio_balance_mono is the real-libobs case: a stereo source at
the output's rate (no resampler) sends a packet, and an audio capture
callback reads it back, bit for bit: centred it is unchanged, at 0.25 it is
the sine law, with Force Mono both channels are the mean, and with both it
is balanced, then the mean. 4 cases.

Both are in the native test graph, in the core suite (the real-libobs one
on Linux, like the other core audio tests), so no pinned suite count
changes. On the C backend both pass, and test_audio_restart_resampler_tail,
test_audio_clock_drift, test_media_sync_offset_restart and
test_async_delay_audio are unchanged and pass.

Refs obs-rust#1994
Tests for the port of libobs/obs-source-audio-input.c.h to the
audio_input module of rust/obs-source, next to audio_timing, color_space
and deinterlace_pacing. The safe core is a stub (todo!()), one function
per hidden C function, plus the pieces they are made of:

- discontinuous(), reset_needed() and resampler_needed() over a Format
  (struct obs_source_audio_input_format: format, rate, speakers);
- balance_needed() and mono_needed();
- balance() of a left and a right slice with a Law (Law::from_c() gives
  None for a value outside enum obs_balance_type), and sine_gains(), the
  sine law's two gains with the C's casts;
- downmix() of a slice of planes, from add_plane(), scale_plane() and
  copy_plane(), the C's three loops.

tests/source/audio_input.rs ports test_source_audio_input_steps 1:1: its
22 cases under the same names (steps_*), in the same order, with the same
inputs and expectations, the characterized-not-endorsed cases included.

Edge cases (edge_*): proptests (4000 cases each) of the discontinuity
against "within the threshold either way continues" over full-range u64
timestamps; the reset and resampler rules against "anything differs" over
any formats; the balance gate against "outside [0.49f, 0.51f] and not NaN"
over any f32 bit pattern; the mono gate over any flags; each law and the
downmix against per-sample models over random samples (any bit pattern,
none drawn from a wide float range), frame counts, plane lengths and 1 to
8 channels; the three downmix loops over planes of different lengths and
any channel count. Plus the law values and the gate's float literals.

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::audio_input
fails 32 of 32 with "not yet implemented".

Refs obs-rust#1994
Ports libobs/obs-source-audio-input.c.h, the audio input steps of
process_audio() and reset_resampler(), to the audio_input module of
rust/obs-source. process_audio() and reset_resampler() stay C in
obs-source.c.h: the field reads and stores, next_resample_in_ts (computed
there and passed in), the resampler, copy_audio_data() and its
allocation, the clock drift filter and
audio_resampler_set_compensation_error(). The hybrid libobs drops the C
file and links the Rust shims in its place.

Safe core (src/audio_input.rs):
- discontinuous(): a resampler, an end set, and abs_diff() of the two
  timestamps at or past the 70 ms threshold (obs-rust#546, kept exactly: the
  distance is not modular, as uint64_diff() is not).
- reset_needed(): rate, format, speakers, the compensation flag, or the
  discontinuity. resampler_needed(): not (the output's rate, format and
  speakers and no compensation), the clock drift fix's rule.
- balance_needed() and mono_needed(): the C's compares, so a NaN balance
  is not balanced, and only the Force Mono bit counts.
- balance(): the three laws over the left and right slices; Law::from_c()
  gives None for any other value, which changes nothing. The sine law's
  gains are sine_gains(): (1.0f32 - balance) widened to f64, times the f64
  PI / 2.0, rounded to f32 with `as` (the C's (float), round to nearest),
  then f32::sin, which is the platform's sinf; the same casts in the same
  order as the C. The square law is f32::sqrt, the linear law plain
  products.
- downmix(): add_plane() for each other plane in turn, scale_plane() by
  1.0f32 / channels as f32 (the C's reciprocal, +inf for 0), then
  copy_plane() back. Only the given lengths are touched.
The module denies clippy's arithmetic, indexing, as (two allowed casts,
commented), unwrap, expect, panic, unimplemented and unreachable lints,
keeps no state and takes no lock.

Shims (src/ffi/source_audio_input.rs): the five decisions take plain
values and struct obs_source_audio_input_format by value (mirrored,
C reads: nothing for zero frames or an unknown law, planes 0 and 1 for a
law, planes 0 to channels - 1 for the downmix (plane 0 alone for 0
channels). The downmix holds two planes at a time, so it allocates
nothing. Intentional difference: a null data, or a null plane the C would
read, does nothing (the C dereferences it; libobs passes
source->audio_data.data, which copy_audio_data() filled).

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-audio-input (LIBOBS_PORTED_SOURCES), whose PCH is
the header and <math.h> (with _USE_MATH_DEFINES, as cmake/pch/obs-c-std.h
gives the core); the file still checks that M_PI is a double.
libobs.pch.h keeps the header for obs-source.c. 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, which calls
glibc's sinf as the C does. ci/test_ported_sources.py checks the group on
every host.

test_source_audio_input_steps still compiles the C file into itself,
unchanged. The new test_source_audio_input_steps_rust builds the same
test source against libobs_rust-static instead: the test calls the seven
hidden functions directly, so this variant runs the shims, the format
passed by value and the plane pointers included. It passes 22 of 22 on
the hybrid tree. test_source_audio_balance_mono,
test_audio_restart_resampler_tail and test_audio_clock_drift see the swap
through the hybrid.

Tier 1 source::audio_input passes 32/32.

Refs obs-rust#1994
Oracle (oracle/source_audio_input.c): the original
obs-source-audio-input.c.h, unmodified, with its seven functions renamed
to oracle_*, plus the format's layout (size, alignment, the three
offsets), the threshold, the stereo, Force Mono and law constants, and
the int and bool sizes. The format is mirrored on the Rust side and passed
by value, as obs-source.c.h passes it.

Tier 3 (tests/source/audio_input_parity.rs, 14 tests and one ignored):
- the layout and constants against the C header, and the shim's mirror
  against the oracle's;
- the gates exhaustively over their flags: the discontinuity over both
  resampler states and every pair of 14 special timestamps (around 0, the
  threshold, 2^63 and u64::MAX); the reset condition over the three bools
  and 64 x 64 formats with each field equal or not (values outside the
  enums too), and the resampler rule over the same with the compensation
  on and off; the balance gate over both output kinds, eleven speaker
  values and 44 special balances; the mono gate over every single bit,
  none and all;
- proptests (20,000 cases each) over full-range u64 timestamps (half of
  them within a few thresholds of the end), any formats, any flags and
  balances drawn from bit patterns: finite, +-0, +-inf and NaN;
- the laws (all three and five unknown values) over 0, 1, 7 and 64 frames
  and the special balances, and the downmix over 0 to 8 channels (0
  passes plane 0 alone, as the C reads it): two copies of nine planes with
  guard samples, the C on one, the shim on the other through float **,
  every sample's bits compared; proptests over random samples, frame
  counts, laws and balances, all drawn from bit patterns (never a wide
  Range<f32>, obs-rust#1951);
- the sine law's gains (a plane of ones) over every 4099th u32 pattern
  and every 257th one in [0, 1], 5.2 million balances;
  exhaustive_sine_gains (ignored, run with --release --ignored) covers all
  2^32: it passed, so libm sinf and f32::sin with the C's casts give the
  same bits for every balance on x86_64 Linux. The gains stay in Rust;
- zero frames and unknown laws read nothing on either side (a null
  data), and the intentional difference on the shim only: a null data or
  plane does nothing.

One exception to the bit comparison, written out in the module header:
where one multiply or add of the C had two NaN operands, which NaN comes
out depends on the operand order the compiler picks (x86 returns the
first operand's), and the C builds already differ there: the same C at
-O0 and at -O2 gives a different NaN for a NaN sample times a NaN
balance. There both sides must give a NaN; one NaN operand, a NaN made
from other values, and everything else must match bit for bit. The
proptest regression file is committed: the first run found that case
(samples [0, 0, NaN], balance NaN, linear law) before the rule was
written.

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

Mutation check (once, not committed): 48 mutants, Tier 1 (33 tests) and
Tier 3 (14), failures per mutant:

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| discontinuity at > threshold | 3 | 2 |
| no end-set check / no resampler check | 2 / 2 | 2 / 2 |
| distance one way (modular) | 4 | 2 |
| never discontinuous (obs-rust#546 reverted) | 4 | 2 |
| reset ignores rate / format / speakers / compensation / discontinuity | 2 / 3 / 2 / 2 / 2 | 2 each |
| no resampler rule for compensation (clock drift fix reverted) | 2 | 2 |
| resampler rule ignores rate / speakers / format | 2 each | 2 each |
| balance gate at >= 0.51 / <= 0.49 | 3 / 3 | 2 / 2 |
| NaN balanced | 2 | 2 |
| balance gate ignores mono output / speakers | 2 / 2 | 2 / 2 |
| mono gate on any flag / ignores mono output | 2 / 2 | 2 / 2 |
| sine law product in f32 only | 3 | 3 |
| sine law sin in f64, then rounded | 3 | 3 |
| sine law 1 - balance in f64 | 1 | 3 |
| sine / square / linear gains swapped | 4 / 3 / 3 | 3 / 2 / 2 |
| square and linear laws swapped | 5 | 2 |
| an unknown law is the sine law | 2 | 2 |
| right plane not balanced | 5 | 3 |
| downmix subtracts | 3 | 2 |
| downmix divides instead of the reciprocal | 4 | 2 |
| 0 channels scaled as 1 | 2 | 2 |
| downmix sums in reverse / one plane too few / no copy back | 2 / 3 / 4 | 0 (the shim calls the loops) |
| downmix adds one plane too many | 0, then 1 (see below) | 0 |
| shim: one frame short / planes swapped | 0 | 3 / 3 |
| shim: balance gate speakers fixed at stereo | 0 | 2 |
| shim: resampler rule reads the source as the output | 0 | 2 |
| shim: downmix scaled by the plane count / last plane skipped / one copy fewer | 0 | 2 each |
| shim: discontinuity timestamps swapped (the end-set check moves) | 0 | 2 |

Tier 1 does not reach the shims. Three survive and are equivalent:
- the gate's edges compared as f64 0.49 and 0.51: no f32 lies between
  0.49f and 0.49 or between 0.51f and 0.51;
- the shim's format and speakers swapped in both formats: the rules only
  compare whole formats for equality;
- the shim's two compensation flags swapped: != is symmetric.
"Adds one plane too many" survived at first: downmix() with more planes
than channels added one, and only the core reaches that (the shim calls
the loops). edge_downmix_leaves_planes_past_channels, added to Tier 1
here, now fails on it (1 of 33).

Refs obs-rust#1994
The rust/README.md crate table now lists libobs/obs-source-audio-input.c.h
among the rust/obs-source units, and its port table has a row for it: the
seven functions and their rules (obs-rust#546's restart and the clock drift fix's
resampler rule included), what stays C (the field reads and stores,
next_resample_in_ts, the resampler, copy_audio_data(), the drift filter
and the compensation error), the format passed by value and the
compile-time checks, the sine law's casts and the exhaustive Tier 3 result
for its gains, what the shims read, what the characterization pins and the
port keeps, the intentional difference (a null data or plane does
nothing), the NaN-pair rule of the parity test, the _rust variant of the
steps test, the real-libobs tests 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_audio_input.rs: 1024 packets through the
discontinuity, the reset condition and the two gates (continuing,
jittered and restarting timestamps, formats and flags changing now and
then), the sine law over a stereo packet of 480 frames at 0.25, and the
downmix of 480 frames of stereo and of 7.1, each folded into a checksum
both sides must match. On x86_64 Linux (GCC, not pinned, a loaded
machine; median): decisions 9.37 us Rust and 13.49 us C per 1024 packets
(0.70x), sine_law 1.06 us and 1.70 us per packet (0.62x), downmix_2
1.13 us and 2.20 us (0.51x), downmix_8 3.30 us and 6.45 us (0.51x). The
Rust loops are vectorized and give the same bits.

Fixes obs-rust#1994
@rdoupe
rdoupe force-pushed the port/source-audio-input branch from d2bb974 to 2d840cf Compare October 24, 2026 02:11
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 22a0f79 Oct 24, 2026
33 checks passed
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
* libobs: Move the transition clock and fit into obs-source-transition-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: Characterize the transition clock and fit

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

* obs-source: Add Tier 1 tests for the transition clock and fit port

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

* obs-source: Port the transition clock and fit

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

* obs-source: Check the transition clock and fit port against the C

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

* docs: Record the transition clock and fit port

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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…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
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 source audio input steps in obs-source.c to obs-source

2 participants