Repository navigation
obs-source: Port the source audio timestamp placement - #1936
Merged
Merged
Conversation
github-merge-queue
Bot
removed this pull request from the merge queue due to a conflict with the base branch
Oct 23, 2026
…io-timing.c Behaviour-neutral extraction before the Rust port. The decisions of source_output_audio_data() in obs-source.c.h, which place each audio packet of a source in time, move to libobs/obs-source-audio-timing.c.h as two pure steps around audio_buf_mutex and the buffer start rule, declared in the new libobs/obs-source-audio-timing.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_audio_timing_pre_lock(): the direct timestamp check, then reset (timing not set), jump (more than MAX_TS_VAR off and not direct), smooth (within TS_SMOOTHING_THRESHOLD, with the async decoupled re-anchor and the drift error) or log, and next_audio_ts_min; - obs_source_audio_timing_under_lock(): push back or place, the reset when the system time jumped and the audio was not continuous (obs-rust#522), the sync and resampler offsets, next_audio_sys_ts_min and the "sync offset changed, so place" rule (obs-rust#333); - obs_source_audio_timing_buffer_start(): the audio_ts rule of reset_audio_data_at_sys_time() (obs-rust#286). source_output_audio_data() reads the fields into a struct, calls a step, and writes the results back itself, in the order the stores happened before: timing_adjust = 0 then timing_set for a direct timestamp, timing_set then timing_adjust for a reset, the re-anchor, then next_audio_ts_min; under the lock the reset, next_audio_sys_ts_min and last_sync_offset. The volatile timing_adjust is still read before the lock for the packet and again under it for next_audio_sys_ts_min, as before. handle_ts_jump() (the lock and the buffer clear), audio_resampler_set_compensation_error(), conv_frames_to_time() (the caller passes the packet's duration, so the steps never divide by the sample rate), the deque place and push-back, the signal, the muting and both log lines, with the same messages, stay in obs-source.c.h. All arithmetic is the same uint64_t arithmetic, wrapping where it wrapped. What differs is only when the fields are read: each step's inputs are read once, just before it, where the code read some of them a few statements later (timing_set after the direct store, sync_offset after the reset branch). That is visible only to a thread writing them in that window without the lock, which was already a data race. reset_audio_timing() stays for handle_ts_jump() and obs_source_show_preloaded_video(), and uint64_diff() for the resampler restart check of obs-rust#546; the steps have their own copy of the distance. The thresholds are named in the header, and obs-source.c.h checks them against TS_SMOOTHING_THRESHOLD and MAX_TS_VAR at compile time. 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#1932
test_source_audio_timing_steps compiles libobs/obs-source-audio-timing.c.h (ASan and UBSan, no libobs core) and calls only its three hidden functions, so it runs on every platform. Each case pins every output of a step, that the step writes its numeric outputs (they start as a garbage byte pattern) and that it leaves its inputs alone. 29 cases: - obs_source_audio_timing_pre_lock(): direct timestamps on either side of the clock; direct at MAX_TS_VAR - 1, MAX_TS_VAR and MAX_TS_VAR + 1 (it is strict); the reset when the timing is not set, which ignores the expected timestamp and the async flags; next_audio_ts_min == 0 keeps the packet's timestamp; smoothing with a positive, zero and negative drift error; the 70 ms threshold at 70 ms - 1 ns (smoothed), 70 ms and 70 ms + 1 ns (logged), either side; MAX_TS_VAR at 2 s - 1 ns and 2 s (logged) and 2 s + 1 ns (a jump, which resets the timing to the packet); a direct timestamp never jumps; the re-anchor of an unbuffered, decoupled async source (to os_time minus the packet's own timestamp, after the direct store) and none with only one of the flags. - obs_source_audio_timing_under_lock(): appended when contiguous, placed when next_audio_sys_ts_min is 0; the 70 ms threshold either side; the reset at 2 s + 1 ns and none at 2 s; no reset when the audio was continuous (obs-rust#522), and the reset using the packet's own timestamp; the sync and resampler offsets applied after the comparison (41fa9c1); next_audio_sys_ts_min from the timing_adjust read under the lock, not the packet's; any sync offset change places the packet and is kept, from 0 too (obs-rust#333). - obs_source_audio_timing_buffer_start(): os_time plus the sync offset minus the resampler offset, 0 for 0 (obs-rust#286). - Seven packet sequences through a fake source (the fields, and a model of handle_ts_jump() and of the buffer start of source_output_audio_place()): contiguous packets with jitter, a system-clock source, the sync offset changed between packets (up, down and negative, before the buffer's start), a timestamp jump keeping the sync offset, a Video Delay (Async) timing move of 4 s followed rather than reset (obs-rust#522), the decoupled re-anchor, and next_audio_ts_min 0 followed by an audio-mix reset. Characterized, not endorsed: a timestamp ahead of the clock gives a timing_adjust near UINT64_MAX; the packet's end, timestamp + timing_adjust, a negative sync offset larger than the packet's system time and a resampler offset larger than it all wrap; the distance is not modular, so timestamps on either side of UINT64_MAX are a jump; a direct timestamp more than 2 s from the expected one is logged as exceeding the smoothing threshold, not treated as a jump. The sequences also show that the first placement sets next_audio_sys_ts_min to the packet's time with its sync offset, so with a non-zero offset the second packet is placed too, right after the first. It is in the native test graph. The five real-libobs tests are unchanged. Refs obs-rust#1932
… port RED for the port of libobs/obs-source-audio-timing.c.h to rust/obs-source, a new crate for the units extracted from libobs/obs-source.c (one crate per libobs area, as rust/obs-scene is for obs-scene.c, obs-rust#444). The safe core, audio_timing, is a stub (todo!()): Action (keep, reset, jump, smooth, exceeded), PreLockInput and PreLock (obs_source_audio_timing_pre_lock()), UnderLockInput and UnderLock (obs_source_audio_timing_under_lock()), buffer_start() (obs_source_audio_timing_buffer_start()), distance() (uint64_diff()), TS_SMOOTHING_THRESHOLD and MAX_TS_VAR. The crate depends on nothing: plain u64, i64 and bool in and out. tests/source/audio_timing.rs ports test_source_audio_timing_steps 1:1: its 29 cases under the same names (steps_*), in the same order, with the same inputs and expectations, the characterized-not-endorsed wraps and the fake source of the sequences included. The C test's checks that a step writes its outputs and leaves its inputs alone have nothing to check in the safe API, which takes its inputs by value and returns every output. Edge cases (edge_*): the distance (not modular, at both ends of the range); every flag combination with the timestamp, clock, expected timestamp and duration over the special values of the u64 range and the thresholds +-1, checking no panic and what each action implies (direct exactly when within MAX_TS_VAR, the reset and jump adjust os_time - timestamp, a smoothed drift under 70 ms, no diff or adjust outside their actions, the end is timestamp + duration wrapped); the same for the under-lock step over special u64 and i64 values (the reset exactly when over MAX_TS_VAR and not continuous, the placed time with wrapped offsets, push back exactly when contiguous and the offset unchanged); the buffer start over special values; and two proptests of the same over full-range values (4000 cases each). The tests are a module of the existing core category binary, obs-tests-core (rust/obs-data/tests/core.rs), as obs-scene's are; no new test binary. The crate joins the workspace through its rust/* glob. soldr cargo test -p obs-data --test obs-tests-core source:: fails 35 of 35 with "not yet implemented". Refs obs-rust#1932
Ports libobs/obs-source-audio-timing.c.h, the audio timestamp placement of source_output_audio_data(), to the audio_timing module of the new rust/obs-source. source_output_audio_data() and reset_audio_data_at_sys_time() stay C in obs-source.c.h: the field reads and the stores in their order (the volatile timing_set and timing_adjust included), audio_buf_mutex, handle_ts_jump(), the resampler's compensation, conv_frames_to_time(), the deque, the signal, the muting and the log lines. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/audio_timing.rs): pre_lock() decides direct (strictly within MAX_TS_VAR of the clock), then reset (timing not set), jump (more than MAX_TS_VAR from the expected timestamp and not direct), smooth (strictly within TS_SMOOTHING_THRESHOLD; the re-anchor of an unbuffered, decoupled async source; the drift error) or exceeded, and the packet's end. under_lock() appends when the packet's system time is the buffer's end or within 70 ms of it, resets the timing to the packet's own timestamp when it is more than 2 s off and the audio was not continuous (obs-rust#522), adds the sync offset and takes off the resampler offset after the comparison, sets next_audio_sys_ts_min from the timing_adjust read under the lock, and places the packet whenever the sync offset changed (obs-rust#333). buffer_start() is the obs-rust#286 rule. Every sum and difference is wrapping_add/wrapping_sub where the C's uint64_t wraps; the drift error is cast_signed() of the wrapped difference, the C's (int64_t), and the sync offset is cast_unsigned(), the C's conversion of int64_t to uint64_t. The module denies clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented and unreachable lints, keeps no state and takes no lock. Shim (src/ffi/source_audio_timing.rs): the three functions keep the C signatures. The two structs are mirrored #[repr(C)], the enum as u32 (C stores it as an int or unsigned int; every value fits). Each shim reads the input fields and writes the output fields through the pointer one field at a time, so the inputs and the padding keep their bytes, as with the C, and it never reads an output field. Intentional difference: a null pointer does nothing, where the C dereferences it; libobs passes a struct on its stack. Kept, characterized, not endorsed (test_source_audio_timing_steps): a timestamp ahead of the clock gives a timing_adjust near u64::MAX; timestamp + timing_adjust, the packet's end, a negative sync offset larger than the packet's system time and a resampler offset larger than it wrap; the distance is not modular; a direct timestamp more than 2 s from the expected one is logged, not a jump. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-audio-timing (LIBOBS_PORTED_SOURCES), whose PCH is only obs-source-audio-timing.h; libobs.pch.h now includes the header for obs-source.c. The three 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 links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference. ci/test_ported_sources.py checks the group on every host. test_source_audio_timing_steps still compiles the C file into itself, unchanged. The new test_source_audio_timing_steps_rust builds the same test source against libobs_rust-static instead, as test_scene_bounds_place_rust does, and links: the test includes only the header and calls only the three functions, and the archive's obs-source member needs nothing from libobs or the other crates (it pulls in only libgcc_s and libm). It passes 29 of 29. The five real-libobs tests see the swap through the hybrid. Tier 1 source::audio_timing passes 35/35. Refs obs-rust#1932
…he C Oracle (oracle/source_audio_timing.c): the original obs-source-audio-timing.c.h, unmodified, with its three functions renamed to oracle_*, plus the layout of both structs, the size of the enum, the action values and the two thresholds. Like the calldata and audio controls oracles, it turns off -Wsign-conversion around the include: the C adds the int64_t sync offset to a uint64_t, as libobs always did, and libobs builds without that warning. Tier 3 (tests/source/audio_timing_parity.rs). The structs cross the ABI as bytes: each run fills a buffer with a random byte pattern (padding and output fields included), writes the inputs, runs the C step on one copy and the shim on another, and compares every byte, so the outputs must be equal and neither side may write an input, the padding or anything the other does not write. Runs: - the layout of both structs (size, alignment, every offset), the enum's size, the action values and the thresholds against the C header; - each step over the special values of the u64 and i64 ranges (0, 1, 70 ms and 2 s +-1, 2^63 +-1, the ends, the thresholds below u64::MAX) with every flag: 186624 pre-lock runs, 349920 under-lock runs, and 2304 buffer starts; - proptests of each step over full-range values with the expected timestamp, the system time and next_audio_sys_ts_min drawn near the thresholds as often as anywhere (20000 cases each); - 10000 random sequences of 2 to 50 packets through a model of source_output_audio_data() (the stores in their order, handle_ts_jump(), the buffer start of source_output_audio_place()), driven once by the C steps and once by the shims, from a random or empty source state, with the sync offset changed, the async flags toggled, the timing moved by the video thread (before the packet and between the two reads of timing_adjust) and timing_set cleared by the audio mix between packets. The step structs, the buffer start and the written-back state must be equal after every packet. A second test checks that the sequences reach every action and both under-lock outcomes, the reset, the re-anchor, direct timestamps and sync offset changes, more than 100 times each in 2000 sequences. The whole parity module passed 26 runs in a row with fresh random seeds (obs-rust#1930); no regression file came up. Excluded, the intentional difference: a null struct pointer, which the C dereferences and the shims ignore (checked on the Rust side only). Mutation check (done once, not committed): 51 of 51 mutants fail. The 43 core mutants fail Tier 1 and Tier 3: each comparison at its threshold (direct, jump, smooth, push back and the reset, < and <= or > and >=), direct not setting the timing, a direct timestamp allowed to jump, the reset adjust reversed, next_audio_ts_min 0 or next_audio_sys_ts_min 0 compared like other values, the re-anchor with either flag or to the smoothed timestamp, the drift error reversed or clamped, the smoothed timestamp or continuity dropped, diff or the jump's adjust not reported, the end from the unsmoothed timestamp, no push back on an exact match, obs-rust#522 reverted (a reset when continuous), the reset to the smoothed timestamp or keeping the old system time, the sync offset not applied or in the comparison, the resampler offset added, next_audio_sys_ts_min from the packet's adjust, obs-rust#333 reverted, the pre-obs-rust#333 rule (a change from 0 does not place), last_sync_offset not kept, the buffer start for 0, the distance not symmetric, and every wrapping operation (twelve of them, the (int64_t) included) made saturating. The 8 shim mutants (an output not written, an input written, two actions swapped, the two adjusts swapped, an argument changed) fail Tier 3; Tier 1 does not reach the shims. Refs obs-rust#1932
docs/rust-port/naming.md maps libobs/obs-source*.c.h to the new rust/obs-source, as libobs/obs-scene*.c.h maps to rust/obs-scene. The rust/README.md crate table lists rust/obs-source, and its port table has a row for libobs/obs-source-audio-timing.c.h: what stays C, the seam (the field reads and the stores in their order), the wrapping arithmetic, what the characterization pins and the port keeps, the intentional difference (a null struct pointer), the _rust variant of the steps test, the five real-libobs tests and what Tier 3 compares. The crate's description and lib.rs say what it holds. port_comparison gets its own bench file, benches/port_comparison/source_audio_timing.rs: each step over 1024 packets that reach every branch, the outputs folded into a checksum both sides must match. On x86_64 Linux (GCC, not pinned; median per 1024 packets): pre_lock 5.12 us Rust and 4.78 us C (1.07x), under_lock 4.50 us Rust and 3.95 us C (1.14x), under 0.6 ns more per packet. Fixes obs-rust#1932
rdoupe
force-pushed
the
port/source-audio-timing
branch
from
October 23, 2026 14:52
2d0f59c to
982fe2a
Compare
zackees
enabled auto-merge
October 23, 2026 15:12
6 of 7 tasks
This was referenced Oct 23, 2026
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 23, 2026
RED for the port of libobs/obs-source-deinterlace-pacing.c.h to the deinterlace_pacing module of rust/obs-source, next to audio_timing (obs-rust#1936). The safe core is a stub (todo!()): hold(), wait_for_next(), waiting_frame_ts(), half_duration(), buffered_offset() and reanchor(), one per hidden C function, on plain u64 and u32 values, and FRAME_TOLERANCE, TWOX_TOLERANCE and TS_JUMP_THRESHOLD. tests/source/deinterlace_pacing.rs ports test_deinterlace_pacing_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 wraps, the first frame's half duration and the model of the unbuffered sequences included. Edge cases (edge_*) check each function against a model of the C's uint64_t arithmetic written in i128 with the modulo explicit (the guard's end as next + offset, the waiting timestamp as 2 * timestamp - next, the lateness negative from 2^63): over the special values of the u64 range and the thresholds +-1 (every combination), the hold at TS_JUMP_THRESHOLD +-1 before every end, the re-anchor at its threshold and at the int64_t sign from every offset, and four proptests over full-range values with the clock drawn near the end and the lateness near the thresholds (4000 cases each). The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs), as audio_timing's are. soldr cargo test -p obs-data --test obs-tests-core source::deinterlace_pacing fails 30 of 30 with "not yet implemented". Refs obs-rust#1950
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 23, 2026
* libobs: Move the 2x deinterlace frame pacing into obs-source-deinterlace-pacing.c Behaviour-neutral extraction before the Rust port. The pacing decisions of double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x) in obs-source-deinterlace.c.h move to libobs/obs-source-deinterlace-pacing.c.h as six pure functions on plain integers, declared in the new libobs/obs-source-deinterlace-pacing.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_deinterlace_pacing_hold(): an unbuffered source keeps its frame while the frame's end (frame_ts + offset + 2 * half_duration - TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD; - obs_source_deinterlace_pacing_wait_for_next() and obs_source_deinterlace_pacing_waiting_frame_ts(): the "don't skip ahead" guard of ready_deinterlace_frames() for an unbuffered source with two frames queued, and the timestamp it then stores; - obs_source_deinterlace_pacing_half_duration(): half the time from the frame before, truncated to uint32_t; - obs_source_deinterlace_pacing_buffered_offset(): the offset of a buffered source, from the source clock that picks frames (obs-rust#395); - obs_source_deinterlace_pacing_reanchor(): the offset of an unbuffered source, kept unless the frame comes early or misses its tick and its first field (obs-rust#395). obs-source-deinterlace.c.h keeps everything else: the async_frames queue, the buffered walk, first_frame(), deinterlace_process_last_frame(), the textures and the render. It still tests async_unbuffered and deinterlace_offset before the hold and the guard, reads the same fields and obs->video values, and stores deinterlace_frame_ts, deinterlace_half_duration and deinterlace_offset in the same order. The re-anchor now stores the offset it returns every time; when it keeps the offset, that is the value already there. All arithmetic is the same uint64_t arithmetic, wrapping where it wrapped, the (int64_t) cast of the re-anchor's lateness included; TWOX_TOLERANCE is now 1000000ULL, which every use already converted to uint64_t. The thresholds are named in the header. obs-source-deinterlace.c.h keeps FRAME_TOLERANCE (the buffered walk) and TWOX_TOLERANCE (the render) and checks them against the header at compile time; TS_JUMP_THRESHOLD was used only by the hold and moves. The obs-rust#395 comments move with the code. 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#1950 * test: Characterize the 2x deinterlace frame pacing test_deinterlace_pacing_steps compiles libobs/obs-source-deinterlace-pacing.c.h (ASan and UBSan, no libobs core) and calls only its six hidden functions, so it runs on every platform. 20 cases: - obs_source_deinterlace_pacing_hold(): held until the frame's end and not at it; TS_JUMP_THRESHOLD - 1 (held), TS_JUMP_THRESHOLD and TS_JUMP_THRESHOLD + 1 (moves on, the clock jumped: strict); the half duration widened before it is doubled (UINT32_MAX gives 8.6 s, no 32-bit wrap); a half duration of 0; the end wrapping past UINT64_MAX and below 0. - obs_source_deinterlace_pacing_wait_for_next() and _waiting_frame_ts(): wait until timestamp + offset + duration (no tolerance, no threshold), the waiting timestamp one duration back, so its half duration is half the duration; a next frame before the first. - obs_source_deinterlace_pacing_half_duration(): 50i and 59.94i, rounding down, the largest that fits. - obs_source_deinterlace_pacing_buffered_offset(): video_time - last_frame_ts + FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395). - obs_source_deinterlace_pacing_reanchor(): no offset yet; a frame early by 1 ns; on time and later by less than the larger of the field and the tick, less TWOX_TOLERANCE (kept); at it and past it (re-anchored), for 50i into 60 fps (the field is longer) and into 30 fps (the tick is), ties, a field and a tick of 0, a tick over UINT32_MAX. - Unbuffered sequences of 100 frames through a model of deinterlace_get_closest_frames() (hold, newest arrived frame, half duration, re-anchor), on exact rational clocks: 25 and 50 frames a second into 30 and 60 fps never re-anchor and never pick early (obs-rust#395), 50 into 30 drops 76 frames; a frame a field late re-anchors once and an early one re-anchors back. Characterized, not endorsed: - half a duration of 2^32 ns or more is truncated to 32 bits (a 10 s gap gives 705 ms); - a timestamp going backwards wraps: the half duration of a frame 40 ms back is 2^32 - 20 ms, the guard's end is the next frame's timestamp plus the offset, the waiting timestamp lands after the first frame; - the first frame with no second one queued takes its half duration against deinterlace_frame_ts, which is 0 for a source that never showed a frame: half its own timestamp truncated (1.78 s for a capture timestamp 1000 s after boot), or against the last frame shown before a queue reset. An unbuffered source then moves on at the next tick as if the clock jumped, or, when the truncation leaves 30 ms, holds the frame to 59 ms where a 50i frame ends at 39 ms; - the buffered offset and the re-anchored offset wrap when the frame's clock is ahead of the canvas, and the re-anchor's lateness is an int64_t, so 2^63 late or more counts as early; - 29.97 frames a second into 30 and 60 fps, and 59.94 into 60, re-anchor on every frame: the canvas ticks a little faster than the frames, so a tick lands within TWOX_TOLERANCE before the frame's time and the frame counts as early; the offset follows the canvas, 33 or 17 us a frame. It is in the native test graph, in the core suite. The real-libobs test_deinterlace_2x_field_order is unchanged. Refs obs-rust#1950 * obs-source: Add Tier 1 tests for the 2x deinterlace frame pacing port RED for the port of libobs/obs-source-deinterlace-pacing.c.h to the deinterlace_pacing module of rust/obs-source, next to audio_timing (obs-rust#1936). The safe core is a stub (todo!()): hold(), wait_for_next(), waiting_frame_ts(), half_duration(), buffered_offset() and reanchor(), one per hidden C function, on plain u64 and u32 values, and FRAME_TOLERANCE, TWOX_TOLERANCE and TS_JUMP_THRESHOLD. tests/source/deinterlace_pacing.rs ports test_deinterlace_pacing_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 wraps, the first frame's half duration and the model of the unbuffered sequences included. Edge cases (edge_*) check each function against a model of the C's uint64_t arithmetic written in i128 with the modulo explicit (the guard's end as next + offset, the waiting timestamp as 2 * timestamp - next, the lateness negative from 2^63): over the special values of the u64 range and the thresholds +-1 (every combination), the hold at TS_JUMP_THRESHOLD +-1 before every end, the re-anchor at its threshold and at the int64_t sign from every offset, and four proptests over full-range values with the clock drawn near the end and the lateness near the thresholds (4000 cases each). The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs), as audio_timing's are. soldr cargo test -p obs-data --test obs-tests-core source::deinterlace_pacing fails 30 of 30 with "not yet implemented". Refs obs-rust#1950 * obs-source: Port the 2x deinterlace frame pacing Ports libobs/obs-source-deinterlace-pacing.c.h, the frame pacing of double-rate deinterlacing, to the deinterlace_pacing module of rust/obs-source. deinterlace_get_closest_frames() and ready_deinterlace_frames() stay C in obs-source-deinterlace.c.h: the async_unbuffered and deinterlace_offset tests before the hold and the guard, the field reads and the stores in their order, the async_frames queue, the buffered walk, first_frame(), the textures and the render. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/deinterlace_pacing.rs): hold() keeps an unbuffered frame while its end (frame_ts + offset + twice the widened half duration, less TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD; wait_for_next() waits while the clock is before timestamp + offset + the frame's duration, and waiting_frame_ts() is the timestamp one duration back; half_duration() is half the difference, its low 32 bits as the C's (uint32_t); buffered_offset() is video_time - last_frame_ts + FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395); reanchor() returns video_time - frame_ts when there is no offset, when the frame is early (the lateness, cast_signed() of the wrapped difference as the C's (int64_t), is negative) or when the lateness plus TWOX_TOLERANCE reaches the larger of the half duration and the canvas interval, and the old offset otherwise (obs-rust#395). Every sum and difference is wrapping_* where the C's uint64_t wraps. The module denies clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented and unreachable lints, keeps no state and takes no lock. Shim (src/ffi/source_deinterlace_pacing.rs): the six functions keep the C signatures, plain integers in and out, so nothing is mirrored and no pointer is checked. No intentional differences. Kept, characterized, not endorsed (test_deinterlace_pacing_steps): half a duration of 2^32 ns or more is truncated; a timestamp going backwards wraps the half duration, the guard's end and the waiting timestamp; the first frame with no second one queued takes its half duration against a deinterlace_frame_ts of 0; the offsets wrap when the frame's clock is ahead of the canvas; a lateness of 2^63 or more counts as early; at 29.97 and 59.94 frames a second into a slightly faster canvas the offset re-anchors on every frame and follows the canvas. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-deinterlace-pacing (LIBOBS_PORTED_SOURCES), whose PCH is only obs-source-deinterlace-pacing.h; libobs.pch.h now includes the header for obs-source-deinterlace.c. The six 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 six 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_deinterlace_pacing_steps still compiles the C file into itself, unchanged. The new test_deinterlace_pacing_steps_rust builds the same test source against libobs_rust-static instead, as test_source_audio_timing_steps_rust does, and links: the test includes only the header and calls only the six functions. test_deinterlace_2x_field_order sees the swap through the hybrid. Tier 1 source::deinterlace_pacing passes 30/30. Refs obs-rust#1950 * obs-source: Check the 2x deinterlace frame pacing port against the C Oracle (oracle/source_deinterlace_pacing.c): the original obs-source-deinterlace-pacing.c.h, unmodified, with its six functions renamed to oracle_*, plus the three thresholds. The bool results are declared as u8 on the Rust side, the byte the C returns. Tier 3 (tests/source/deinterlace_pacing_parity.rs). Every function takes and returns plain integers, so each run compares the bytes of the two results: the C's bool as its byte, the uint32_t and uint64_t results as their bytes. Runs: - the three thresholds against the C header; - each function over the special values of the u64 range (0, 1, the thresholds +-1, 50i and 60 fps durations, 2^32, 2^63 +-1, the end of the range) and ten half durations, every combination: 138240 holds, 331776 guards, 576 half durations and buffered offsets, 3317760 re-anchors; - proptests of each function over full-range u64 and u32 values, with the clock drawn near the frame's end and the lateness near the thresholds as often as anywhere (20000 cases each); - 3000 random sequences of 2 to 300 canvas ticks through a model of deinterlace_get_closest_frames(), ready_deinterlace_frames() (the unbuffered queue trim and guard, and the buffered walk with frame_out_of_bounds() and the MAX_TS_VAR jump) and first_frame(), with the stores in the C's order, driven once by the C functions and once by the shims: sources of 25, 29.97, 50 and 59.94 frames a second into 30 and 60 fps canvases, buffered and unbuffered, with timestamp and arrival jitter, late deliveries, late ticks, timestamps that jump or go backwards or land anywhere, queue resets (last_frame_ts 0, the first-frame path), a full queue and the buffering toggled. The bytes of every call's result and the model's state after every tick must be equal; - a check that 2000 sequences reach every branch of every function more than 100 times (held, moved on, the clock jumped; waited or not; the half duration from a pair and from deinterlace_frame_ts, truncated; the buffered offset; the re-anchor with no offset, early, late and kept; the first frame) and every rate pair. The parity module passed 20 runs in a row with fresh random seeds; no regression file came up. Mutation check (done once, not committed), with the two test modules built into a scratch test binary: 53 mutants. The 45 non-equivalent core mutants fail Tier 1 (2 to 10 of 30) and Tier 3 (1 to 4 of 11): each comparison at its threshold (< and <=, >= and >), the hold without its jump threshold or its "before the end" test, one half duration instead of two, the half duration doubled in 32 bits, TWOX_TOLERANCE or FRAME_TOLERANCE dropped, the guard without its duration or offset, the waiting timestamp ahead or unchanged, the half duration not halved, rounded up, saturated to UINT32_MAX or from the high bits, the buffered offset reversed, the re-anchor without its no-offset, early or late rule, early only past TWOX_TOLERANCE, the smaller of field and tick or only one of them, the pre-obs-rust#395 re-anchor at every pick, the lateness clamped or computed without the modulo, and every wrapping operation made saturating. Two mutants survive and are equivalent: end - sys_time is computed only when sys_time is before the end, and the lateness plus TWOX_TOLERANCE is reached only for a lateness of at most INT64_MAX, so neither can wrap. The 6 shim mutants (a result negated, arguments swapped) fail Tier 3; Tier 1 does not reach the shims. No intentional differences: there is no pointer to check. Refs obs-rust#1950 * docs: Record the 2x deinterlace frame pacing port The rust/README.md crate table now says rust/obs-source also holds the units extracted from libobs/obs-source-deinterlace.c, and its port table has a row for libobs/obs-source-deinterlace-pacing.c.h: the six functions, what stays C, the seam (the async_unbuffered and deinterlace_offset tests, the field and obs->video reads and the stores in their order), the wrapping arithmetic and the (int64_t) lateness, what the characterization pins and the port keeps, no intentional differences, the _rust variant of the steps test, test_deinterlace_2x_field_order 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_deinterlace_pacing.rs: 1024 ticks of 50i and 59.94i sources into 60 fps that reach every branch, as an unbuffered source runs the hold, the guard, the half duration and the re-anchor, and as a buffered one runs the half duration and its offset, the results folded into a checksum both sides must match. On x86_64 Linux (GCC, not pinned; median per 1024 ticks): unbuffered_tick 8.67 us Rust and 7.59 us C (1.14x), buffered_pick 3.20 us Rust and 3.13 us C (1.02x), about 1 ns more per tick. Fixes obs-rust#1950
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1932
What and why
Ports the audio timestamp placement of
source_output_audio_data()(libobs/obs-source.c.h) to a new crate,rust/obs-source: the crate forlibobs/obs-source*.c.hunits, asrust/obs-sceneis forobs-scene*.c.h(#444). This is where every audio packet of every source goes in time, so it is A/V sync for all audio. The code was fixed four times for real users: #522 (a Video Delay (Async) over 2 s), the two sync offset fixes (f7c446e, an offset changed from 0, and bf3c8ea, the offset kept after a buffer reset) and #546 (the resampler tail, which shares the 70 ms threshold). Each rule those fixes added is pinned in the characterization and checked by a mutant.Six commits:
Move (behaviour-neutral): the decisions go to
libobs/obs-source-audio-timing.c.h, in two pure steps aroundaudio_buf_mutexplus the buffer-start rule ofreset_audio_data_at_sys_time(). They are declared in the newobs-source-audio-timing.hwithoutEXPORT, so they are hidden:obs_source_audio_timing_pre_lock(): the direct timestamp check, then reset, jump, smooth (with the drift error and the async decoupled re-anchor) or log, andnext_audio_ts_min;obs_source_audio_timing_under_lock(): append or place, the reset when the system time jumped and the audio was not continuous (libobs: Let audio follow a video timing move of more than 2 s #522), the sync and resampler offsets,next_audio_sys_ts_min, and the rule that a changed sync offset places the packet (bug: libobs: a sync offset changed from 0 is ignored on a running source (upstream #7912) #333);obs_source_audio_timing_buffer_start(): where an emptied buffer starts (bug: libobs: Media Source loses its audio sync offset when it restarts (upstream #11640) #286).source_output_audio_data()reads the fields into a struct, calls a step and writes the results back, so the stores of the volatiletiming_setandtiming_adjuststay where they were, in the same order. The volatiletiming_adjustis still read twice, as before: before the lock for the packet, and under it fornext_audio_sys_ts_min. These stay C:handle_ts_jump()(the lock and the buffer clear),audio_resampler_set_compensation_error()(it gets the step's drift error),conv_frames_to_time()(the caller passes the duration, so the steps never divide), the deque, the signal, the muting and both log lines, with the same messages. The file stays inlibobs.core-sourcefor this commit.Characterization:
test_source_audio_timing_steps, 29 cases, compiles the moved file with a fake source. It covers:next_audio_ts_min == 0;RED: Tier 1 in the new crate against
todo!(). 35 of 35 fail.GREEN: the safe core and
#[no_mangle]shims, in a newlibobs.ported-source-audio-timinggroup with its own PCH (only the new header). The export lists,source-sets.json, the unity lists andci/test_ported_sources.pyare updated, and a_rustvariant of the steps test is added (see below).Tier 3: a C oracle and byte-level parity, with the mutation check.
Docs: the naming.md row, the README rows and a
port_comparisonbench file.Every sum and difference is
wrapping_*where the C'suint64_twraps. The drift error iscast_signed()of the wrapped difference, the C's(int64_t). The sync offset iscast_unsigned(), the C's conversion ofint64_ttouint64_t. The crate depends on no other crate.When fields are read. Each step's inputs are read once, just before it. Before,
timing_setwas read just after the direct store, andsync_offsetjust after the reset branch. Only another thread writing those fields without the lock, in that window of a few statements, could see a difference, and that was already a data race. The stores and their order are unchanged.Moved, but not deleted from
obs-source.c.h.reset_audio_timing()stays forhandle_ts_jump()andobs_source_show_preloaded_video(), anduint64_diff()for the resampler restart check of libobs: Drop audio held in the resampler when a new stream starts #546. The steps have their own copy of the distance. The thresholds are named in the header, andobs-source.c.hchecks them againstTS_SMOOTHING_THRESHOLDandMAX_TS_VARwith_Static_assert.Kept, characterized, not endorsed:
timing_adjustnearu64::MAX;timestamp + timing_adjust, the packet's end, a negative sync offset larger than the packet's system time, and a resampler offset larger than it all wrap;u64::MAXcount as a jump;Intentional difference: a null struct pointer does nothing in the shims; the C dereferences it. libobs passes a struct on its stack.
The
_rustvariant links.test_source_audio_timing_steps_rustbuilds the unchanged test source againstlibobs_rust-static, astest_scene_bounds_place_rustdoes. The test includes only the new header and calls only the three functions, and the archive'sobs-sourcemember needs nothing from libobs or the other crates (it pulls in onlylibgcc_sandlibm). It passes 29 of 29 on the hybrid tree. It is the first port sincetest_scene_bounds_place_rustwhose_rustvariant links.Exported symbols: 1766 on the C backend, the hybrid and
libobs-c-reference, with identical sets. The three functions are local (t) in all three, and in the hybrid they come fromliblibobs_rust.a. The ported group is compiled only intolibobs-c-reference.Performance (port_comparison, x86_64, GCC, not pinned; median per 1024 packets):
pre_lock: 5.12 µs Rust, 4.78 µs C (1.07x);under_lock: 4.50 µs Rust, 3.95 µs C (1.14x).That is under 0.6 ns more per packet, and a source sends a packet roughly every 21 ms.
No user-visible change, so no
docs/features.mdline.Observed, not changed (upstream behaviour, outside the moved code).
source_output_audio_place()empties the buffer withreset_audio_data(source, in->timestamp)when there is noaudio_tsyet, or when the packet lands before it. That stores the placed time, which includes the sync offset and the resampler offset, innext_audio_sys_ts_min. Everywhere else, that field holds a time without the offsets. So with a non-zero sync offset, the packet after such a placement is compared against a time shifted by the offset:seq_jump_keeps_sync_offsetandseq_sync_offset_changed;This is not fixed here. Changing it would change behaviour, and it can be a separate issue.
RED → GREEN
9a155e99f.soldr cargo test -p obs-data --test obs-tests-core source::fails 35 of 35, every failure "not yet implemented" (the 29 portedsteps_*cases and 6edge_*cases, two of them proptests).f97394500. Tier 1 passes 35/35. Tier 2 ran on the branch head when it was based oned71dc387(the same tree as982fe2a66, except fortest_viewfrom test: characterize obs-view before the Rust port (#1933) #1934, which came with the final rebase ontodc5b192c9). It ran under a privateXvfb -noresetwithdbus-run-sessionand a fresh HOME:test_async_delay_audiotest_audio_sync_offset_changetest_media_sync_offset_restarttest_audio_restart_resampler_tailtest_audio_clock_drifttest_source_audio_timing_stepstest_source_audio_timing_steps_rustNo case was skipped and no rerun was needed. The five real-libobs tests and the steps test also passed on all three builds at commit 1 (the move) and at commit 4 (GREEN), before the rebases. The full ctest also passed before the rebase onto
ed71dc387(391/391, 394/394 and 394/394, 0 skipped).Tier 3,
359f2ac22(source::audio_timing_parity, 10 tests). The structs cross the ABI as bytes. Each run fills a buffer with a random byte pattern (padding and output fields included), writes the inputs, runs the C step on one copy and the shim on the other, and compares every byte. Runs:u64andi64ranges with every flag: 186,624 pre-lock runs, 349,920 under-lock runs and 2,304 buffer starts;timing_adjust(before the packet and between its two reads), and the audio mix clearstiming_set. The step structs, the buffer start and the written-back state must match after every packet;The parity module passed 26 runs in a row with fresh seeds (test: source_profiler_parity::parity_random_sequences finds Rust/C differences in about half of runs #1930), and no regression file came up.
Mutation check (once, not committed): 51 of 51 mutants fail.
<=MAX_TS_VARnext_audio_ts_min0 compared like others>=2 s<=70 msnext_audio_sys_ts_min0 compared like others<=70 ms>=2 snext_audio_sys_ts_minfrom the packet's adjustlast_sync_offsetnot keptwrapping_subbefore the(int64_t)), drift clamped instead of cast,timestamp + timing_adjust, sync add, resampler sub,next_audio_sys_ts_min, reset adjust under the lock, reset system time, buffer start sync add, buffer start resampler sub (12 mutants)last_sync_offsetnot writtencontinuousread wrong / buffer start argument changedRebased onto
b4a58944c(#1935, the obs-view port); only the port_comparison bench list conflicted. After that rebase,ci/test_build_groups.py,ci/test_ported_sources.py,ci/test_oracle_standins.py,source::in obs-tests-core (45/45) and the lint lane were re-run and pass.Other runs on the head before that rebase (on
dc5b192c9):soldr cargo test -p obs-data --test obs-tests-core: 1033/1033 before the final rebase;source::45/45 on the final head.soldr cargo test -p obs-source: 0 tests; as forobs-scene, its tests are a module ofobs-tests-core.ci/test_build_groups.py,ci/test_ported_sources.pyandci/test_oracle_standins.py: OK. Theobs-utilexport-list tests: 3/3.uv run ci/local_gate.py --lane lint: ok.--lane cross: ok.cargo clippy --workspace --all-targets -D warningsis clean on every commit.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)