Skip to content

obs-output: Port the GPU encode thread gates - #2481

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/gpu-encode-gates
Oct 26, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/gpu-encode-gates

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2476

What and why

Ports the per-frame and per-encoder gates of the GPU encode thread (gpu_encode_thread() and init_gpu_encoding() in libobs/obs-video-gpu-encode.c.h) to rust/obs-output, as a new module encoder_gpu_gates, next to encoder_audio_start. Every NVENC, QSV, AMF and VA-API texture recording and stream runs these for each frame and each texture encoder:

  • the frames skipped while the thread starts;
  • the frame rate divisor counter (explicit, so encoders started on the same frame encode on the same frames);
  • the keyed mutex key each encoder hands on (0 from the last one, else the next key);
  • whether the frame timing entry is recorded;
  • after the walk, whether the frame goes back to the front of the queue (one interval later) or to the available queue;
  • P010 or NV12 encode textures.

The shared gates are reused, not copied. The group start, the paired audio wait and the pts step are the gates receive_video() already asks (#2331). The thread now calls the same functions, obs_encoder_audio_start_group_ready(), _paired_late() and _video_pts(), so the raw and GPU paths share one implementation. encoder_gpu_gates re-exports them.

One function per decision, asked where the C made it. Eleven functions cross the C ABI: obs_encoder_gpu_gates_warming(), _warm_next(), _divisor_inc(), _divisor_wrap(), _divisor_skip(), _next_key(), _record_timing(), _count_next(), _requeue(), _requeue_ts() and _p010(). This is a hot, concurrent path, so every read stays where it was:

  • the divisor counter is still read, written with the increment, read again and compared, and written 0 at the wrap, in that order;
  • the group's start_timestamp is still read under the group mutex, for each encoder, after the group and start_ts tests;
  • the weak paired-encoder walk still takes and releases each reference and stops at the first encoder not ready; first_raw_ts is read only after first_received.

What stays C, at its point and under its condition:

  • os_sem_wait(), the stop flag, gpu_encoder_mutex and the group mutex (no section merged), the deques, obs_encoder_get_ref() / _release();
  • the weak paired-encoder walk, the failed flag (an atomic load), the group and first_received tests, video_pause_check(), handle_encoder_group_reconfigure_request() and info.update;
  • the start_ts test and write, the NULL test of encode_texture2, both encode calls, os_gettime_ns(), the timing entry, send_off_encoder_packet(), the profiler;
  • the texture creates, obs_host_texture_shared_handle(), stop_gpu_encoding_thread() and free_gpu_encoding().

The truth tests of a single field (the atomic failed flag, the NULL callback, start_ts) stay C, as in receive_video(). That covers the plan's encode_call() and the failed-encoder skip.

The keys are the u64 the C computes, so the Windows keyed mutex code (obs_host_tex_frame_acquire() / _release()) is not touched. The header copies NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT and VIDEO_FORMAT_P010, and obs-video-gpu-encode.c.h checks them with _Static_assert. No pointer crosses and no struct is mirrored.

File name. The plan named the file obs-video-gpu-encode-gates.c.h. docs/rust-port/naming.md maps obs-video-* files to rust/obs-core and the encoder files to rust/obs-output, where this port goes, so it is obs-encoder-gpu-gates.c.h.

Six commits:

  1. Move (behaviour-neutral, 7fb782ac2): the decisions go to libobs/obs-encoder-gpu-gates.c.h, declared in the new obs-encoder-gpu-gates.h without EXPORT, so they are hidden. The file joins libobs.core-output. git blame on the moved lines shows only upstream commits.

    The move was checked against the original code. A scratch harness (not committed) compiles origin/master's gpu_encode_thread() and init_gpu_encoding() verbatim, next to this branch's with the moved file and obs-encoder-audio-start.c.h, as C++:

    • the stand-in video's and encoders' fields log every read and write, with the value;
    • every call is logged in order with its arguments: each lock and unlock, atomic load, os_sem_wait(), deque pop and push (with the frame), reference taken and released, weak reference resolved, video_pause_check(), group reconfigure, info.update, encode call (texture, pts, key, next key), os_gettime_ns(), timing entry, packet send, profiler call, texture create, shared handle and init call;
    • the stubs answer from one seeded stream in call order (an encoder may move next_key, fail or return a packet). Another thread moves the group start, first_received, first_raw_ts, the failed flag, start_ts, the reconfigure flag and the stop flag around each lock and call. Reads with nothing between them are compared as a sorted run (the C leaves operand order unsequenced);
    • the inputs cover 0 to 8 frames, queued frames with counts -1 to 4 and timestamps at 0, near UINT64_MAX, around 2^32 and 2^63 and random; 0 to 5 texture encoders, failed or not, in groups or not, with or without encode_texture2, divisors 0 to 4 and full range, counters below, at and past them, 0 to 3 paired encoders each (dead weak references among them); every video format; failing texture creates and init calls.

    2,600,000 runs (GCC and Clang -O2, 1,000,000 each; GCC -O0, 300,000; GCC -O0 with coverage, 100,000; ASan with UBSan, no recovery, under GCC -O0 and Clang -O2, 100,000 each): all 485,800,492 logged events match, and so does the end state after each run. Every branch of origin/master's functions is reached both ways (gcov). After the rebases onto 4cd953c4e and 38d51e65e, another 300,000 runs on each rebased move commit match too.

    24 deliberately broken moves were tried, and all 24 fail the harness:

    • the group start read before the group mutex; first_raw_ts read before first_received; the paired walk not stopping; start_ts read before the group test;
    • the divisor counter written once, compared without its second read, or stepped before the pause check;
    • the reference loop merged into the walk's lock section;
    • sixteen changed decisions: each comparison moved by one or inverted, the warm-up and counter steps by two, the key not stepped or stepped on the wrong count, the pts only after a successful encode, the count not stored, the timestamp saturating, the P010 test on NV12, the group and paired gates with >=.
  2. Characterization (faca61a6a): test_encoder_gpu_gates_steps, 16 cases. It compiles the moved file and obs-encoder-audio-start.c.h with ASan and UBSan, and calls only their functions. It pins every decision over edge values, and copies the per-frame body of the thread onto stand-in encoders, end to end:

    • two encoders started on the same frame with divisors 1, 2 and 3 encode on the same frames; one a frame in is out of step;
    • the keys when every encoder encodes, with one encoder, and wrapping; the keys when one skips, for each of its four reasons;
    • the group start; the paired audio wait past a dead weak reference and on one with no audio yet;
    • the pts after a failed encode; the timing entry with and without a frame timestamp;
    • frames with counts 1, 2 and 3, near UINT64_MAX, and with no encoder.

    It is in the test graph, sorted, with both headers as watch inputs. No assert_in_range or fail_msg, and no comparison between two enum types (MSVC C5287).

  3. RED (5aabf38d8): Tier 1 against todo!(). It ports the 16 C cases 1:1 and adds 6 edge tests: the int counts around i32::MIN and i32::MAX, every divisor counter and divisor below 64 and the counter sequence of every divisor up to 64 against the C's post-increment, the key over every count and encoder number below 16, every format from -1000 to 1000, and proptests over full-range values and over encoder lists and frame sequences through the stand-in against origin/master's expressions inline.

  4. GREEN (03ede7abb): the safe core (eleven functions) and eleven #[no_mangle] shims, in a new libobs.ported-encoder-gpu-gates group whose PCH is only the new header.

    • The commit updates source-sets.json, the unity lists, the three export lists and ci/test_ported_sources.py, each in sorted place.
    • test_encoder_gpu_gates_steps has no _rust variant, as for test_encoder_audio_start_steps: the shims' archive member also holds the rest of obs-output, which does not link on its own. Its tests.d file says so.
  5. Tier 3 (352ac928c): a C oracle of the unchanged file, the parity tests and a mutation check.

  6. Docs (6fb3f3190): the README row, the crate's description, lib.rs and a port_comparison bench file in the sorted lists. docs/rust-port/naming.md maps the file in the move commit. The port changes nothing a user can see, so docs/features.md has no line.

The core denies clippy's arithmetic, indexing, as, unwrap, expect, panic, todo, unimplemented and unreachable lints. It keeps no state, takes no lock and does not divide. Every sum and difference wraps as the C's does.

  • Kept, characterized, not endorsed (upstream behaviour, unchanged):
    • when an encoder in the list skips the frame (divisor, failed, waiting or paused), lock_count never reaches the number of encoders, so the last key handed on is not 0 and the frame keeps it. The Windows keyed mutex code is written around it;
    • the pts steps after a failed encode, where receive_video() steps it only after a successful one. The failure stops the encoder either way, so it is one step;
    • a divisor of 0 (the setter refuses it) encodes the first frame and then none until the u32 counter wraps.
  • Intentional differences: warm_next() and count_next() at INT_MIN, where the C's int subtraction is undefined, wrap. The thread starts the wait at 1 and a frame's count at 1 or more.

Mutation check (once, not committed), failing tests per tier:

mutant Tier 1 Tier 3
warming: > 0 3 2
warm_next: - 2 3 2
warm_next: saturating 1 0
divisor_inc: saturating 2 1
divisor_wrap: >= 5 4
divisor_skip: > 1 5 3
next_key: >= 3 2
next_key: saturating 3 1
next_key: the last keeps the key 5 5
record_timing: inverted 4 5
count_next: never below 0 4 2
requeue: > 0 4 2
requeue_ts: saturating 4 4
p010: NV12 2 1
WAIT_FRAMES = 2 2 1
shim p010 inverted 0 2
shim next_key: key + 1 0 5
shim requeue_ts: interval - 1 0 4
shim divisor_skip: skip + 1 0 4
shim record_timing: always 0 2
shim divisor_wrap: arguments swapped 0 0

Tier 1 kills all 15 core mutants, Tier 3 19 of 21. The two survivors: warm_next() saturating differs only at INT_MIN, the intentional difference that Tier 3 does not ask the C (Tier 1 pins it); the shim's divisor_wrap() with its arguments swapped compares them the same way.

Bench (x86_64 Linux, GCC, release, not pinned to a core, a loaded host with load average about 10; median per 1024 frames through four encoders with divisors 1 to 4): Rust 51.28 µs, C 47.80 µs (1.07x). That is about 12 ns per frame and encoder against 11.7 ns, next to an encode call.

RED → GREEN

  • RED commit: 5aabf38d8 (obs-output: Add Tier 1 tests for GPU encode gates; it was 1b8e5138c before the rebases onto 4cd953c4e and 38d51e65e).
    • soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_gpu_gates: 2 passed (the constants and the shared gates, already ported), 20 failed, each on a todo!().
    • On that commit (before a formatting-only fixup of the test file), local_gate --test-diff origin/master ran 4394 tests: 4374 passed, 6 skipped, and only those 20 failed. After the fixup, --lane lint was clean.
  • GREEN: 03ede7abb.
    • output::encoder_gpu_gates: 22 of 22. With Tier 3 (352ac928c): 32 of 32.
    • Through ctest (private Xvfb and D-Bus, own HOME, no sound), 12 of 12 against the C libobs, the hybrid libobs and libobs-c-reference (built with ninja libobs-c-reference; ldd shows the test loading it): test_encoder_gpu_gates_steps, test_encoder_audio_start_steps, test_encoder_error_stop (the texture encoder through this thread, with its teardown races), test_texture_encoder_raw_format, test_encoder_join_keyframe (real x264, VA-API H.264 and HEVC, AOM and SVT-AV1), test_encoding_lag_rescale, test_multitrack_encoder_override, test_encoder_video_reset, test_video_output_step_steps, test_output_interleave_e2e, test_encoder_group_null and test_encoder_tex_active_null.
    • The exported symbol sets are identical on the C, hybrid and C reference libobs (1766, unchanged from master); the eleven functions are local.
  • Final, on 6fb3f3190 (rebased onto 38d51e65e): local_gate --lane lint clean; --test-diff origin/master 4433 of 4433 passed (6 skipped); --lane cross clean (clippy -D warnings, the Dylints and the test build for x86_64-pc-windows-msvc and aarch64-apple-darwin, with SOLDR_RUSTC_WRAPPER=none). The 12 unit tests above passed again on all three libobs, and the exported sets are still 1766 and identical. Full Tier 2 ctest, on 33e41ae11 (the same branch before the rebase onto 38d51e65e, which touches no file of this unit): 560 of 560 passed against the hybrid libobs and against libobs-c-reference (3 skipped on this host: test_pulse_default_follow, test_v4l2_vivid_replug, test_v4l2_vivid_stall).
  • Real app: no UI, protocol output or logging change.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Behaviour-neutral extraction before the Rust port. The decisions of
gpu_encode_thread() and init_gpu_encoding() move to
libobs/obs-encoder-gpu-gates.c.h, as eleven functions on plain values
declared in the new libobs/obs-encoder-gpu-gates.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):

- for each frame: _warming() and _warm_next() (the frames skipped
  while the thread starts);
- for each encoder: _divisor_inc(), _divisor_wrap() and
  _divisor_skip() (the frame rate divisor counter), _next_key() (the
  keyed mutex key it hands on) and _record_timing() (the frame timing
  entry);
- after the walk: _count_next(), _requeue() and _requeue_ts();
- init_gpu_encoding(): _p010() (P010 or NV12 textures).

The group start, the paired audio wait and the pts step are the gates
receive_video() already asks (obs-rust#2331), so the thread now asks the same
functions: obs_encoder_audio_start_group_ready(), _paired_late() and
_video_pts(). The raw and GPU paths share one implementation.

One function per decision, asked where the C made it, so every read
stays where it was. The divisor counter is still read, written with
the increment, read again and compared, and written 0 at the wrap, in
that order. The group's start_timestamp is still read under the group
mutex for each encoder, after the group and start_ts tests; the walk
over the weak paired encoders still takes and releases each reference
and stops at the first one not ready, and first_raw_ts is read only
after first_received.

C keeps os_sem_wait(), the stop flag, gpu_encoder_mutex and the group
mutex (no section merged), the deques, obs_encoder_get_ref() and
_release(), the weak paired-encoder walk, the failed flag (an atomic
load), the group and first_received tests, video_pause_check(),
handle_encoder_group_reconfigure_request() and info.update, the
start_ts test and write, the NULL test of encode_texture2, both encode
calls, os_gettime_ns(), the timing entry, send_off_encoder_packet(),
the profiler, the texture creates, obs_host_texture_shared_handle(),
stop_gpu_encoding_thread() and free_gpu_encoding(). The truth tests of
a single field (the atomic failed flag, the NULL callback, start_ts)
stay C as in receive_video(); the plan's encode_call() and the failed
skip are those.

The expressions move as they were: the int counts, the uint32_t
counter sum, the uint64_t key and timestamp sums and the uint32_t pts
product. The pts step still follows every encode, failed or not.

The plan named the file obs-video-gpu-encode-gates.c.h; naming.md maps
obs-video-* to rust/obs-core and the encoder files to rust/obs-output,
where this port goes (encoder_gpu_gates, beside encoder_audio_start),
so it is obs-encoder-gpu-gates.c.h.

Checked in a scratch harness (not committed). It compiles
origin/master's gpu_encode_thread() and init_gpu_encoding() verbatim,
and this branch's with the moved file and obs-encoder-audio-start.c.h,
as C++, over a stand-in video and encoders whose fields log every read
and write with the value. It logs every lock and unlock, atomic load,
os_sem_wait(), deque pop and push (with the frame), reference taken
and released, weak reference resolved, video_pause_check(), group
reconfigure, info.update, encode call (with the texture, pts, key and
next key), os_gettime_ns(), timing entry, packet send, profiler call,
texture create, shared handle and init call, in order. The stubs
answer from one seeded stream in call order (encoders may move
next_key, fail, or return a packet); another thread moves the group
start, first_received, first_raw_ts, the failed flag, start_ts, the
reconfigure flag and the stop flag around each lock and call. Reads
with no write or call between them are compared as a sorted run,
since the C leaves their operand order unsequenced.
- Inputs: 0 to 8 frames, 0 to 5 queued frames with counts -1 to 4,
  keys up to full range and timestamps of 0, near UINT64_MAX, around
  2^32 and 2^63 and random, popped or made up; 0 to 5 texture encoders
  and 0 to 2 audio ones, failed or not, in 0 to 2 groups with any
  start, with or without encode_texture2, divisors 0 to 4 and full
  range, counters below, at and past them, timebases to UINT32_MAX,
  0 to 3 paired encoders each (dead weak references among them);
  intervals of one 60 fps frame, random and near UINT64_MAX; every
  video format 0 to VIDEO_FORMAT_R10L; failing texture creates,
  os_sem_init(), os_event_init() and pthread_create().
- 2,600,000 runs (GCC and Clang -O2, 1,000,000 each; GCC -O0, 300,000;
  GCC -O0 with coverage, 100,000; ASan with UBSan under GCC -O0 and
  Clang -O2, 100,000 each) give equal events and an equal end state
  (every encoder's fields and timing entries, both queues, the video's
  flags and the groups): 485,800,492 events.
- Every branch of origin/master's functions is reached both ways
  (gcov).
- 24 deliberately broken moves were planted and all 24 fail it: the
  group start read before the group mutex, first_raw_ts read before
  first_received, the paired walk not stopping, start_ts read before
  the group test, the divisor counter written once, compared without
  the second read, or stepped before the pause check, the reference
  loop merged into the walk's lock section, and sixteen changed
  decisions (each comparison moved by one or inverted, the warm-up and
  counter steps by two, the key not stepped or stepped on the wrong
  count, the pts only after a successful encode, the count not stored,
  the timestamp saturating, the P010 test on NV12, the group and
  paired gates with >=).

git blame on the moved lines shows only upstream commits (93ba6e7,
6cdfb0a, 6a53b89, c288d42, 7e39ee2, a1fbf10). The fork has
not changed them.

The constants NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT and VIDEO_FORMAT_P010
are copied into the header, so it needs no core header;
obs-video-gpu-encode.c.h checks them with _Static_assert. The new file
joins libobs.core-output, just before obs-encoder.c.h, with
libobs.pch.h, which includes the new header; source-sets.json lists
it. obs-encoder-audio-start.h notes the thread as a caller.
docs/rust-port/naming.md maps the file to rust/obs-output. The
exported symbol sets are unchanged (1766) on the C, hybrid and C
reference libobs; the eleven functions are local.

Refs obs-rust#2476
test_encoder_gpu_gates_steps compiles libobs/obs-encoder-gpu-gates.c.h
and libobs/obs-encoder-audio-start.c.h (the gates the thread shares
with receive_video()) with ASan and UBSan, and calls only their
functions through obs-encoder-gpu-gates.h and obs-encoder-audio-start.h.
16 cases:

- the copied constants (one frame skipped, VIDEO_FORMAT_P010);
- the start-up wait over edge int values, and the count of frames it
  skips;
- the divisor counter's sum (UINT32_MAX wraps to 0), wrap and skip
  over every pair of edge uint32_t values;
- the key over every triple of edge counts and keys (the last encoder
  hands on 0, any other the next key, which wraps);
- the timing entry, the count, the requeue and the requeue timestamp
  over edge values (the sum wraps past UINT64_MAX);
- the texture format over every enum video_format value and the int
  extremes;
- the shared gates as the thread asks them.

The per-frame body of gpu_encode_thread() is also copied onto stand-in
encoders (no thread, lock or texture; a failed encode stops the
encoder, as send_off_encoder_packet()'s full_stop() does), to pin end
to end: two encoders started on the same frame with divisors 1, 2 and
3 encode on the same frames (the reason for the explicit counter), and
one a frame in is out of step; the keys when every encoder encodes,
with one encoder and wrapping; the keys when one skips for each of its
four reasons; the group start; the paired audio wait, past a dead weak
reference and on one with no audio yet; the pts after a failed encode;
the timing entry for a frame with and without a timestamp; and frames
with counts 1, 2 and 3, near UINT64_MAX, and with no encoder.

Characterized, not endorsed:
- when an encoder in the list skips the frame, the last key handed on
  is not 0 and the frame keeps it (upstream; the Windows keyed mutex
  code is written around it);
- the pts steps after a failed encode, where receive_video() steps it
  only after a successful one (the failure stops the encoder either
  way);
- a divisor of 0 (the setter refuses it) encodes the first frame, then
  none until the uint32_t counter wraps.

It is in the native test graph, in name order, with both headers as
watch inputs, and links libm off MSVC. No assert_in_range or fail_msg,
so it builds with cmocka 1.x and 2.x, and no comparison between two
enum types (MSVC C5287). The real-libobs tests test_encoder_error_stop,
test_texture_encoder_raw_format and test_encoder_join_keyframe cover
the thread.

Refs obs-rust#2476
RED for the port of libobs/obs-encoder-gpu-gates.c.h to a new
encoder_gpu_gates module of rust/obs-output, next to
encoder_audio_start. The safe core is a stub (todo!()): warming(),
warm_next(), divisor_inc(), divisor_wrap(), divisor_skip(),
next_key(), record_timing(), count_next(), requeue(), requeue_ts()
and p010(), with WAIT_FRAMES and VIDEO_FORMAT_P010. It re-exports
encoder_audio_start's group_ready(), paired_late() and video_pts(),
the gates the thread shares with receive_video(), already ported.

tests/output/encoder_gpu_gates.rs ports test_encoder_gpu_gates_steps
1:1: its 16 cases under the same names (steps_*), in the same order,
with the same values and expectations, the characterized-not-endorsed
cases included, the stand-in thread pass written in Rust over the
core.

Edge cases (edge_*) check the core against models of the original C:
the int counts at and around i32::MIN and i32::MAX; every divisor
counter and divisor below 64, and the counter sequence of every
divisor up to 64 over 200 frames against the C's post-increment; the
key over every count and encoder number below 16; every format from
-1000 to 1000; a proptest of every decision over full-range values
(4,096 cases); and a proptest of the stand-in over encoder lists and
frame sequences against the origin/master code with its expressions
inline.

soldr cargo test -p obs-data --test obs-tests-core --
output::encoder_gpu_gates passes 2 of 22 (the constants and the
shared gates, already ported) and fails 20 with "not yet
implemented".

Refs obs-rust#2476
Ports libobs/obs-encoder-gpu-gates.c.h, the per-frame and per-encoder
gates of the GPU encode thread, to the encoder_gpu_gates module of
rust/obs-output. obs-video-gpu-encode.c.h stays C around it:
os_sem_wait(), the stop flag, gpu_encoder_mutex and the group mutex,
every read of the video, the frame, the encoders, their groups and
paired encoders, the weak paired-encoder walk, the pause check, the
group reconfigure, both encode calls, os_gettime_ns(), the timing
entry, the packet send, the deques, the references, the profiler and
the texture creates. The hybrid libobs drops the C file and links the
Rust shims in its place.

Safe core (src/encoder_gpu_gates.rs):
- warming(), divisor_skip(), record_timing() and requeue(): not 0;
- warm_next() and count_next(): a wrapping i32 difference;
- divisor_inc(): a wrapping u32 sum; divisor_wrap(): the C's ==;
- next_key(): 0 when lock_count equals the number of encoders, else
  the next key (a wrapping u64 sum);
- requeue_ts(): a wrapping u64 sum;
- p010(): the format is VIDEO_FORMAT_P010 (18);
- group_ready(), paired_late() and video_pts() re-exported from
  encoder_audio_start: the thread calls those shims, as receive_video()
  does, so both paths share one implementation.
The module denies clippy's arithmetic, as, indexing, unwrap, expect,
panic, todo, unimplemented and unreachable lints, keeps no state, takes
no lock and does not divide.

Shims (src/ffi/encoder_gpu_gates.rs): the eleven functions keep the C
signatures (int as c_int). No pointer crosses and no struct is
mirrored. The keys are the u64 the C computes, so the Windows keyed
mutex code (obs_host_tex_frame_acquire/_release) is untouched.

Intentional difference: warm_next() and count_next() at INT_MIN, where
the C's int subtraction is undefined, wrap. The thread starts the wait
at 1 and a frame's count at 1 or more.

C side: the file leaves libobs.core-output for its own group,
libobs.ported-encoder-gpu-gates (LIBOBS_PORTED_SOURCES), whose PCH is
only obs-encoder-gpu-gates.h; libobs.pch.h keeps the header for
obs-video-gpu-encode.c. The 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.
ci/test_ported_sources.py checks the group on every host. The exported
symbol sets are unchanged (1766) on the C, hybrid and C reference
libobs.

test_encoder_gpu_gates_steps still compiles the C file into itself,
unchanged. It has no _rust variant, as for
test_encoder_audio_start_steps: the shims' archive member also holds
the rest of obs-output, which needs libobs's hidden seams and
obs-util, so it does not link on its own. Its tests.d file says so.

soldr cargo test -p obs-data --test obs-tests-core --
output::encoder_gpu_gates passes 22 of 22. Through ctest (private
Xvfb and D-Bus, own HOME, no sound), test_encoder_gpu_gates_steps,
test_encoder_audio_start_steps, test_encoder_error_stop (the texture
encoder and its teardown races), test_texture_encoder_raw_format,
test_encoder_join_keyframe (with real x264, VA-API H.264 and HEVC, AOM
and SVT-AV1), test_encoding_lag_rescale,
test_multitrack_encoder_override, test_encoder_video_reset,
test_video_output_step_steps, test_output_interleave_e2e,
test_encoder_group_null and test_encoder_tex_active_null pass, 12 of
12, against the C, hybrid and C reference libobs.

Refs obs-rust#2476
Tier 3 for the encoder_gpu_gates port. The oracle
(rust/obs-c-oracle/oracle/encoder_gpu_gates.c) compiles the unchanged
libobs/obs-encoder-gpu-gates.c.h with its eleven functions renamed to
oracle_*, plus its two constants, bound in src/encoder_gpu_gates.rs.
Nothing in it divides.

tests/output/encoder_gpu_gates_parity.rs compares the oracle, the
shims and the core exactly, all three sides:
- the two constants;
- the int gates (warming, warm_next, count_next, requeue) over
  -100,000 to 100,000, the 100,000 values above INT_MIN and below
  INT_MAX, and a stride through all of i32 both ways;
- the divisor sum, wrap and skip over every pair of counters and
  divisors below 256 and every pair of 20 special u32 values, and the
  sum over 0 to 1,000,000 and a stride through all u32;
- the key over every count and encoder number below 64 with 16
  special keys, and every pair of 9 special usize values with 24
  special keys;
- the timing entry and the requeue timestamp over every pair of 24
  special u64 values (around 0, 2^32, 2^63, a 60 fps frame and
  u64::MAX); the format over -100,000 to 100,000, the extremes and a
  stride through all i32;
- the per-frame body of gpu_encode_thread() (the C test's stand-in)
  on the oracle, with the oracle's shared group, paired and pts gates,
  and on the shims: every list of one to three encoders with divisors
  and counters 0 to 3 (4,368 lists) through eight frames with counts
  1 to 3 at two intervals, comparing every encode, key, pts, counter,
  timing entry and requeue; each reason a middle encoder skips, fails
  or moves the key;
- proptests (20,000 cases each) of every decision over full-range
  values and of the stand-in over encoder lists and frame sequences
  from any state.

The oracle is called for warm_next() and count_next() only above
INT_MIN, where the C's subtraction is defined; that excludes exactly
the intentional difference. Otherwise no input is excluded.

Mutation check (once, not committed): 21 mutants, 15 in the core and
6 in the shims.

  mutant                                  Tier 1  Tier 3
  warming: > 0                                 3       2
  warm_next: - 2                               3       2
  warm_next: saturating                        1       0
  divisor_inc: saturating                      2       1
  divisor_wrap: >=                             5       4
  divisor_skip: > 1                            5       3
  next_key: >=                                 3       2
  next_key: saturating                         3       1
  next_key: the last keeps the key             5       5
  record_timing: inverted                      4       5
  count_next: never below 0                    4       2
  requeue: > 0                                 4       2
  requeue_ts: saturating                       4       4
  p010: NV12                                   2       1
  WAIT_FRAMES = 2                              2       1
  shim p010 inverted                           0       2
  shim next_key: key + 1                       0       5
  shim requeue_ts: interval - 1                0       4
  shim divisor_skip: skip + 1                  0       4
  shim record_timing: always                   0       2
  shim divisor_wrap: arguments swapped         0       0

(failing tests per tier.) Tier 1 kills all 15 core mutants, Tier 3 19
of 21. The survivors: warm_next() saturating differs only at INT_MIN,
the intentional difference Tier 3 does not ask the C (Tier 1 pins
it), and the shim's divisor_wrap() with the arguments swapped compares
them the same way.

soldr cargo test -p obs-data --test obs-tests-core --
output::encoder_gpu_gates passes 32 of 32 (22 Tier 1, 10 Tier 3).

Refs obs-rust#2476
rust/README.md gets the row for libobs/obs-encoder-gpu-gates.c.h
(obs-output::encoder_gpu_gates), after encoder_audio_start's: what
moved, the three gates shared with receive_video(), what stays C and
why, the characterized-not-endorsed behaviours (the last key not 0
when an encoder skips, the pts step after a failed encode, a divisor
of 0), the one intentional difference and the tests that cover it;
the obs-output crate row, its Cargo description and lib.rs name the
new module. docs/rust-port/naming.md already maps the file to
rust/obs-output (the move commit).

rust/obs-c-oracle/benches/port_comparison/encoder_gpu_gates.rs, in
the sorted module list, runs 1024 frames (counts 1 to 3) through four
encoders with divisors 1 to 4, asking every decision
gpu_encode_thread() asks for each frame and encoder, on the shims and
on the oracle, and checks both checksums match. Release, x86_64
Linux, GCC, a loaded host (load average about 10), median: Rust
51.28 us, C 47.80 us (1.07x), about 12 ns per frame and encoder
against 11.7 ns, next to an encode call.

The port changes nothing a user can see, so docs/features.md has no
line.

Fixes obs-rust#2476
@rdoupe
rdoupe requested a review from zackees as a code owner October 26, 2026 13:40
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: a decision port in the standard shape, like #2475. The GPU encode thread gates move into a steps unit and then to Rust, with a C reference copy, a parity test, a bench and the export maps. The only existing test file touched is the test-graph.json entry. Queuing; it merges when CI is green.

@zackees
zackees enabled auto-merge October 26, 2026 13:42
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
Merged via the queue into obs-rust:master with commit 803889e Oct 26, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port the GPU encode gates in obs-video-gpu-encode.c to Rust

2 participants