Skip to content

libobs: Hand over a running transition when transitions are swapped - #556

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/transition-override-lock
Oct 13, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/transition-override-lock

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Description

Switching scenes quickly no longer locks the program when one of the scenes has a transition override. Before, outside Studio Mode with the default transition Fade 1000 ms and a Cut override on scene A, switching A, B, A quickly left the program stuck on one scene: later switches were logged and the scene list changed, but the program output and the recording did not, until a third scene was picked or Studio Mode was toggled.

  • The frontend swaps the program transition for a scene's override with obs_transition_swap_begin() / obs_transition_swap_end() and swaps the default back when the override's transition stops. obs_transition_swap_begin() copied the old transition's two sources to the new one but not that a transition was running. Swapped while Fade was half way from A to B, Cut held A and B without transitioning: obs_transition_start() towards A returned false ("A is already shown"), the frontend swapped Fade back in the same state, and every later start towards B found B already set as the destination and did not transition.
  • obs_transition_swap_begin() now also hands over the running state (video and audio transitioning, start time, duration, mode and manual position), so the new transition finishes the one in progress and a new start interrupts it as usual. Picking another transition while one runs goes through the same swap and is fixed too.
  1. test/cmocka: Add failing test for swapping a transition mid-transition: test_transition_swap drives libobs the way OBSBasic::TransitionToScene, OverrideTransition and TransitionFullyStopped do (core video with the OpenGL module built in the tree). The cases are:

    • swap a running Fade for Cut: Cut has to be transitioning;
    • then start Cut towards A: has to start;
    • swap Fade back and start it towards B: has to transition (the lock);
    • guard: swapping an idle transition hands over the scene shown and the new one starts.

    3 of 4 fail today (the guard passes).

  2. libobs: Hand over a running transition when transitions are swapped: the fix. 4 of 4 pass.

Motivation and Context

Fixes #555 (upstream obsproject/obs-studio#11914, reported by millerdq; Fenrirthviti confirmed the steps; jnortheastfilms reported it still happens on the latest version on 2026-10-10).

Reproduced in the current release (AppImage 33.0.0-beta6-799), headless over obs-websocket, recording the program (A red with a Cut override, B green without, Fade 1000 ms): slow switches are correct; after 12 quick A/B switches ending on B the recording shows A, and the next four slow switches (A, B, A, B) all show A, while GetCurrentProgramScene says B. Same with 0.15 s and 0.3 s between switches.

Why this one: the program output, and so the stream and the recording, silently stops following scene changes; reported by users, confirmed by a maintainer, still reported this month; a small libobs fix, reproduced in our release.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_transition_swap: 3 of 4 cases fail at the test commit and 4 of 4 pass at the fix (3 runs each per lane), under a private Xvfb with software GL and the system OpenGL module masked (as in CI). ctest -V shows every case run, none skipped; without a display they skip. Registered on Linux only, like the other core-video tests.
  • Full ctest at the fix, rebased on current master: 161 of 161 in each lane.
  • clang-format and gersemi are clean.

End to end, a build of this branch (frontend + obs-websocket, Rust libobs) driven like the release repro: every recorded switch is correct, 0 wrong of 9 checks, with 0.05, 0.15 (2 runs), 0.3 and 0.5 s between quick switches; the release locks with 0.15 and 0.3 s.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 13, 2026 12:22
rdoupe and others added 2 commits October 13, 2026 05:24
Outside Studio Mode, with the default transition Fade and a Cut override
on scene A, switching A, B, A quickly locks the program on one scene:
later switches are logged but the program output does not change until
a third scene is picked or Studio Mode is toggled.

The frontend swaps the program transition with obs_transition_swap_begin()
and obs_transition_swap_end() for transition overrides. Swapping Fade,
half way from A to B, for Cut copies both scenes to Cut but not that a
transition is running, so Cut holds A and B without transitioning.
obs_transition_start() towards A then returns false because A is already
its first scene. The frontend swaps Fade back in, again holding A and B
without transitioning, and every later start towards B finds B already
set as the destination and does not transition.

test_transition_swap drives libobs the way OBSBasic::TransitionToScene,
OverrideTransition and TransitionFullyStopped do. On master:

  swap a running Fade for Cut:          Cut not transitioning
  then start Cut towards A:             returns false
  swap Fade back, start towards B:      Fade not transitioning

3 of 4 cases fail. The guard passes: swapping an idle transition hands
over the scene shown and the new transition starts normally.

The test needs core video for the transitions' render targets, so it
runs under xvfb-run on Linux and skips without a display.

Refs obs-rust#555.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
obs_transition_swap_begin() copied the sources of the old transition to
the new one but not that a transition was running. Swapped in the middle
of a transition, the new transition held both sources without
transitioning: it never finished, and obs_transition_start() towards
either source did nothing (towards the first it returns false, towards
the second it finds it already set).

The frontend swaps transitions for scene transition overrides. Outside
Studio Mode, with the default transition Fade and a Cut override on
scene A, switching A, B, A quickly left Cut and then Fade in that state,
and the program stayed on one scene whatever was picked until a third
scene was picked or Studio Mode was toggled.

Copy the running state too (video and audio transitioning, start time,
duration, mode and manual position), so the new transition finishes the
one in progress and a start interrupts it as usual.

Fixes obs-rust#555.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=7f329387c339fc45ea887e681da77522e4bcfd1c secs=513 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run
Ci-Attestation: {"at":1791894773,"gate":"rust/all/fmt","host":"linux-x86_64","key":"ef87b63af4ba72d8942060758adb157b12e0bccee69f3335ca39f011d84939c3","lane":"lint","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":37,"stamp":"86cc2ef646515f5f0c32fc0192f3cba0","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"ef87b63af4ba72d8942060758adb157b12e0bccee69f3335ca39f011d84939c3","lane":"lint","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":37,"stamp":"2bc53e8c9accafc4d8450bb49a371944","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"e2152bf5fff025358a2fb87abf8de46ff9d71685527c5831c11a9e75be640390","lane":"rust-test","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":176,"stamp":"44356a0de54536477e2dab3ab9fb61af","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"375c22997992d1e8e06348ab695ad9330ac6ae1eae92994f9e9f7f9ec6e0d313","lane":"cross","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":177,"stamp":"07bc55ee3ab5dd0e646a444f7da47431","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"375c22997992d1e8e06348ab695ad9330ac6ae1eae92994f9e9f7f9ec6e0d313","lane":"cross","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":177,"stamp":"ad2a3506b01f904b228192fca24e4940","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"375c22997992d1e8e06348ab695ad9330ac6ae1eae92994f9e9f7f9ec6e0d313","lane":"cross","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":177,"stamp":"c3a22ffbb1853ae424d92cd982b1b6bc","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"375c22997992d1e8e06348ab695ad9330ac6ae1eae92994f9e9f7f9ec6e0d313","lane":"cross","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":177,"stamp":"0a4e22d0f9ad66097b1f9baf5961cdf0","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/x86_64-apple-darwin/clippy","host":"linux-x86_64","key":"375c22997992d1e8e06348ab695ad9330ac6ae1eae92994f9e9f7f9ec6e0d313","lane":"cross","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":177,"stamp":"551930743d6134a80137c35b7bd986f2","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"rust/x86_64-apple-darwin/dylint","host":"linux-x86_64","key":"375c22997992d1e8e06348ab695ad9330ac6ae1eae92994f9e9f7f9ec6e0d313","lane":"cross","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":177,"stamp":"6312313c882a23ccec57284d28f11101","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
Ci-Attestation: {"at":1791894773,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"41cc091ec7a5dca2dee112fa5f28d0350685ade9387c5479f24b241e03f385b5","lane":"tier2-linux","parents":["a139b752524e938496aa36b9644c479ec877cbfc"],"secs":124,"stamp":"1798c834c0ad07b769e31049739ef87a","tree":"7f329387c339fc45ea887e681da77522e4bcfd1c","v":1,"via":"run"}
@zackees
zackees force-pushed the fix/transition-override-lock branch from a066be5 to ef25f1d Compare October 13, 2026 12:32
@zackees

zackees commented Oct 13, 2026

Copy link
Copy Markdown
Member

Verified locally under xvfb-run (Tier 2 image + mesa):

  • RED 65c54a4: test_transition_swap fails 3 of 4 (swap_hands_over_running_transition, override_back_to_previous_scene_starts, switch_after_interrupted_override_transitions)
  • GREEN a066be5: 4/4. It needs a display, so plain CI skips it.
  • Rebased on master, local gate passed; pushed as ef25f1d.

@zackees
zackees merged commit 44b367c into obs-rust:master Oct 13, 2026
19 checks passed
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…clock.c

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

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

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

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

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

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

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

Refs obs-rust#2040
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
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
The rust/README.md crate table now lists
libobs/obs-source-transition-clock.c.h among the rust/obs-source units
(and obs-source-transition.c among the files they come from), and its
port table has a row for it: the seven functions and their rules, what
stays C (the locks, the field and child reads, calc_torquef(), the
offsets and their casts, add_alignment(), the matrix build,
get_sample_time(), the texrenders and obs-rust#556's handover), the int-sized
enums and the compile-time checks, the scale written only where the C
writes it, the long double widths and the x87 result the port gives for
every u64 (an f64 division off a tie, an integer division on one and
from 2^53), what the characterization pins and the port keeps, the
scale type outside the enum (reported, not changed), the intentional
difference (a null scale pointer is not written), the _rust variant of
the steps test, the real-libobs tests and what Tier 3 compares, with the
NaN-pair exception of the tick. The crate's description and lib.rs say
what it holds. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_transition_clock.rs: 1024 samples of a
48 kHz buffer through the clock as mix_child() asks it, a third of the
way into a 300 ms transition; 1024 ratios that all take the integer x87
path (f64 quotients on a tie, durations past 2^53); and 1024 ticks of
the manual step, the size fold, two fits with each scale type, the
colour space and the timestamps, each folded into a checksum both sides
must match. On x86_64 Linux (GCC, not pinned; median per 1024 calls):
audio_clock 2.32 us Rust and 2.43 us C (0.95x), ratio_ties 13.38 us and
2.61 us (5.1x, the u128 division, which only about 1 in 2^29 ratios
takes in use), tick 21.33 us and 19.34 us (1.10x).

Fixes obs-rust#2040
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…clock.c

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

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

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

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

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

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

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

Refs obs-rust#2040
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
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
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
* 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
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.

bug: libobs: switching scenes quickly with a transition override locks the program on one scene (upstream obsproject/obs-studio#11914)

2 participants