Skip to content

obs-core: Port the per-frame video output step decisions - #2181

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/video-output-step
Oct 24, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/video-output-step

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2165

What and why

Ports the per-frame video output step decisions of libobs/obs-video.c.h to rust/obs-core, as a new module video_output_step, next to video_copy_plan (#2133). These are the decisions of update_active_state(), stage_output_texture(), queue_frame(), render_video() and output_frame(): which frame data to clear when an output starts, what is staged for download, whether a frame for the GPU encoders is a duplicate, a copy or a swap, which render steps run, and the texture ring. They run on every frame of every mix. A wrong one outputs the previous or a black frame, or drops or repeats GPU-encoded frames.

Six commits:

  1. Move (behaviour-neutral) into libobs/obs-video-output-step.c.h, ten functions declared in the new obs-video-output-step.h without EXPORT (hidden):

    • obs_video_step_activation(): the base, raw and GPU clears and the new was flags. It takes the two loaded counters as long and decides > 0 itself;
    • obs_video_step_stage(): staged or not, from the output texture (no GPU conversion) or the convert textures (converted), and how many copy surfaces;
    • obs_video_step_queue(), _queue_copy_uv() and _queue_count(): duplicate, copy or swap; the separate UV copy; the count left;
    • obs_video_step_render_targets() and obs_video_step_render(): whether the outputs are rendered and from the encode textures; then convert, encode, stage;
    • obs_video_step_ring(), _download() and _output(): the texture before and after cur_texture; download; output.

    obs-video.c.h keeps every gs_* call in its original order and under its original conditions (a copy surface is still staged only when it is not NULL), the deques (deque_data() and its NULL check when texture-based encoding stops, deque_pop_front(), deque_push_back(), deque_free()), os_sem_post(), obs_host_tex_frame_acquire()/_release(), the profiler and debug markers, download_frame()'s maps, the mix list and its mutex. C still loads both atomics, raw_active first.

    • Why ten calls and not one plan per function. queue_frame() asks obs_host_nv12_planes_share_texture() only for a copy, after the Y copy, so one plan would have to ask it first and on every frame. Instead the C copies Y, then asks the host and passes the answer to _queue_copy_uv(). render_video() reads gpu_conversion after render_output_texture() and obs_host_gpu_encode_surfaces(), so _render_targets() answers before them and _render() after, where the C read the flag. output_frame() computes the previous texture first and advances cur_texture last, so _ring() is called at both points.

    • Checked against the unchanged functions. A scratch harness (not committed) compiles origin/master's update_active_state(), the three clear_*_frame_data(), stage_output_texture(), unmap_last_surface(), queue_frame(), encode_gpu(), output_gpu_encoders(), render_video(), download_frame() and output_frame() verbatim, and the moved ones, as two translation units over shared stubs. The stubs log every gs_*, deque, os_sem_post(), mutex, profiler, debug marker, os_atomic_load_long() and host call with its arguments (pointers by identity). After each run the harness also logs the whole mix: every surface and texture slot, the flags, cur_texture, and each deque frame by frame. The two logs are byte-identical under GCC -O2 and clang -O0: 1,000,000 runs of each of the six functions and 250,000 runs of eight frames, about 250 million logged calls per build. They are also identical under GCC with ASan and UBSan over a tenth of that; that build uses whole-frame queues only, because both sides read a partial frame misaligned. The inputs cover:

      • every activation combination;
      • channel counts 0 to 3 with each surface and texture array;
      • all four behaviours of obs_host_gpu_encode_surfaces() (none, Windows's one channel, two, zero) and both answers of obs_host_nv12_planes_share_texture();
      • empty, partial and full queues;
      • counts from INT_MIN + 1 to INT_MAX;
      • surface maps that fail;
      • outputs switching on and off across frames.

      _ring() equals master's two expressions for every int from -2,000,000 to 2,000,000 and near both ends. 20 planted changes fail the harness. The 2 that survive are equivalent: the output check without raw_active, since frame_ready is only set for raw output, and the base and raw clears swapped, which free the same deque and touch no common field. The harness was rerun on the final base.

    The file joins libobs.core-output for this commit. Exports unchanged (1766).

  2. Characterization: test_video_output_step_steps, 19 cases, compiling the moved file (ASan and UBSan). It pins:

    • every activation combination: the three was flags × both counters at LONG_MIN, -2 to 3 and LONG_MAX (512), plus the single edges;
    • staging with and without GPU conversion for each channel count 0 to NUM_CHANNELS, and before conversion;
    • the queue step for each queue state (no free frame; free with one queued; free with none) and repeat count 1, 2, 3, with the UV copy and the count left;
    • each render-step combination;
    • the ring at 0 and NUM_TEXTURES - 1 (the wrap), and download and output for every pair.

    It is in the test graph in name order, with libm except on MSVC.

  3. RED: Tier 1 in the crate, against todo!().

  4. GREEN: the safe core and the #[no_mangle] shims, in a new libobs.ported-video-output-step group whose PCH is only the header. The three export lists, source-sets.json, the unity list and ci/test_ported_sources.py are updated.

  5. Tier 3: a C oracle and parity tests, with the mutation check.

  6. Docs: the README rows, the crate's description and lib.rs, a naming.md row, and a port_comparison bench file.

How the API differs from the plan's sketch, and why:

  • stage() returns a channel count, not [bool; NUM_CHANNELS]. channel_count comes from obs_host_gpu_encode_surfaces() as a size_t, and the C loops over it as given; a count keeps that loop exactly.
  • queue_step() has no Stop. The C stops when deque_data() gives NULL, which depends on the deque, so that check stays in the C on the Duplicate path. copy_uv is its own call, because the C asks the host only after the Y copy.
  • The activation counters cross as long, not bools. The C loads them, and the > 0 is a decision, so it moved.

Kept, characterized, not endorsed:

  • A channel count past NUM_CHANNELS is passed through; obs-video.c would index past its arrays. The hosts give 1 (Windows) or leave NUM_CHANNELS.

  • Counts of 0 and below are treated as new frames like count 1, and the count left goes to -1 from 0. video_sleep() always gives 1 or more (its clamp keeps the quotient at least 1).

  • Queue sizes are only tested for 0, so a size that is not a whole frame counts as a frame.

  • Outside the ring, only exactly NUM_TEXTURES wraps. cur_texture is only ever 0 or set by the ring.

  • Was flags that disagree with each other are taken as they are.

  • Intentional difference, unreachable: where the C's int arithmetic overflows (undefined: --count at INT_MIN, the ring's prev of INT_MIN and next of INT_MAX), the port wraps. Listed in the parity test header and rust/README.md.

  • How values cross. The six structs as #[repr(C)] mirrors (layout checked by Tier 3), the queue action as the C enum's value, the counters as long, counts and ring values as int, sizes as size_t. Five struct tags share their function's name; the oracle's rename macros rename those tags too, and its layout table names the renamed tags.

  • No _rust variant. I tried linking test_video_output_step_steps against libobs_rust-static, and it fails with 380 undefined references (108 symbols: blog, bcrash, the audio resampler, GIO and D-Bus). The shims' archive member also holds the other obs-core ports, so it does not link on its own; this is the same reason obs-core: Port the output frame copy plan and scale effect #2133 has no _rust variant for test_video_copy_plan_steps. Tier 3 compares the shims with the C instead, and the real-libobs tests see the swap through the hybrid.

  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, identical sets. The ten functions are local (t) in all three; in the hybrid they come from the Rust archive (the group's C object is only in libobs-c-reference).

  • Performance (port_comparison, x86_64 Linux, GCC, not pinned, load average 10 to 20, two runs): video_output_step/frame/1024 took 17.2 and 21.4 µs for Rust and 25.5 and 41.3 µs for C, per 1024 frames of about a dozen decisions each. The difference is within the noise of this host; Rust is not slower.

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

  • Open-PR overlap, checked before the move and again before this draft: no open PR touches obs-video.c.h. obs-core: Port the source load migration and value rules #2157 and obs-source: Port the deinterlace draw decisions #2153 met this one only in the sorted shared lists; both have merged since, and the branch is rebased over them.

RED → GREEN

Test C backend hybrid libobs-c-reference
test_video_output_formats 4/4 4/4 4/4
test_scaled_mix_alpha 2/2 2/2 2/2
test_encoding_lag_rescale 6/6 6/6 6/6
test_video_output_plan 10/10 10/10 10/10
test_video_copy_plan_steps 22/22 22/22 22/22
test_raw_output_video_reset 4/4 4/4 4/4
test_texture_encoder_raw_format 6/6 6/6 6/6
test_encoder_error_stop 8/8 8/8 8/8
test_video_io_hidden 3/3 3/3 3/3
test_output_failed_start_encoders 6/6 6/6 6/6
test_output_video_checks_steps 27/27 27/27 27/27
test_video_output_step_steps 19/19 19/19 19/19
full ctest (-j4, on 231358b86, the same commits based on c0c30e39f) 455/455, 0 skipped 469/469, 0 skipped (not run: the two lanes)

No cmocka case in the table skipped. ctest skipped nothing on either lane. The known flakes (#456, #838) did not fail, so nothing was rerun.

  • Tier 3, 9f5f519be (obs_core::video_output_step_parity, 13 tests). Each call runs the shim, the C and the safe core, and compares each struct field by field, the queue action as its C value, and the bools as bools:

    • the 5 constants, and the size, alignment and every offset of the six structs, the enum's size and sizeof(long), against the C;
    • exhaustive:
      • every was flag against both counters at 14 long and int edges (1,568 activations);
      • both flags against channel counts 0 to 10 and the size_t edges;
      • queue sizes at 0, 1, 47, 48, 49, 96 and the size_t edges, against counts from -3 to 5 and the int edges, with both raw flags (2,600 steps, every action reached);
      • the count left and the ring for every int from -200,000 to 200,000 and the edges;
      • every flag of the render, download, output and UV copy answers;
    • proptests (8192 cases each) over full-range counters, channel counts, sizes, counts and ring values.

    The three inputs where the C is undefined are not given to the C; there the shim is compared with the core, and one test pins the wrapped values. The parity module passed 10 runs in a row with fresh seeds. The committed regression files hold the seeds proptest saved while mutants failed; all pass on the port.

  • Mutation check (once, not committed; Tier 1 with 28 tests and Tier 3 with 13 in one run): 37 mutants, 28 in the core and 9 in the shims. 35 fail; the 2 that survive are equivalent.

Mutant Tier 1 Tier 3
activation: raw counter >= 0 / GPU counter > 1 3 / 3 1 / 1
activation: base clear on raw_was_active / raw clear on was_active 3 / 3 2 / 2
activation: GPU clear when either starts / active needs both 4 / 4 2 / 2
stage: no conversion stages convert texture 0 / count clamped to NUM_CHANNELS 2 / 2 2 / 2
stage: converted flag inverted / three channels without conversion / always staged 4 / 2 / 2 2 / 2 / 2
queue: repeat at count 1 / repeat duplicates with nothing queued 4 / 3 2 / 2
queue: no free frame not a duplicate / first repeat swapped 2 / 3 2 / 2
queue: UV copy inverted / count left - 2 2 / 3 1 / 3
queue: count left saturates (differs only at INT_MIN) 1 1
render: outputs only for raw / encode for raw / convert only for raw 2 / 2 / 2 1 / 1 / 1
ring: prev of 0 is 0 / wraps past NUM_TEXTURES 2 / 2 2 / 2
ring: prev saturates (differs only at INT_MIN) 1 1
frame: always download / output without raw 2 / 2 1 / 1
TEXTURES 3 1 3
shim: counters swapped / clear_raw from clear_gpu / from_output from staged 0 2 / 2 / 2
shim: copy and swap values swapped / render raw and GPU swapped / ring prev and next swapped 0 2 / 1 / 3
shim: stage struct fields reordered / targets ignore GPU 0 1 / 1
equivalent: render targets' encode without outputs && (GPU implies outputs) 0 0
equivalent: shim output arguments swapped (an && of two bools) 0 0

Tier 1 does not reach the shims; Tier 3 kills every shim mutant.

  • Real app: no UI, protocol or logging change.

Other runs, on the head 7f101fb8a unless noted:

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite (the lint and cross lanes and --test-diff passed locally; nothing is stamped)
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Rebased onto master (head 5c8e4245f) to pick up #2186, which fixed the master macOS Tier 2 break from #2170 that failed this PR's macOS job. The rebase was clean and master's new commits touch only the frontend, so the earlier runs above (on the pre-rebase SHAs) still hold.

…-output-step.c

Behaviour-neutral extraction before the Rust port. The decisions of
update_active_state(), stage_output_texture(), queue_frame(),
render_video() and output_frame() move to libobs/obs-video-output-step.c.h,
as ten functions on plain values declared in the new
libobs/obs-video-output-step.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_video_step_activation(input): which frame data to clear (base, raw,
  GPU, in that order) when either output, raw output or GPU encoding
  becomes active, and the new *_was_active flags. It takes the two loaded
  atomics as long and decides "> 0" itself;
- obs_video_step_stage(gpu_conversion, texture_converted, channel_count):
  whether stage_output_texture() stages at all, whether copy surface 0
  stages the output texture (no GPU conversion) or copy surface i convert
  texture i (converted), and how many copy surfaces: 1, or channel_count;
- obs_video_step_queue(avail_size, queue_size, count, raw_active):
  duplicate (no free frame, or a repeat with a frame queued), or a new
  frame copied (raw output, or a repeat) or swapped in;
- obs_video_step_queue_copy_uv(planes_share_texture): whether the UV
  texture is copied on its own;
- obs_video_step_queue_count(count): the count left, which queue_frame()
  stores and returns;
- obs_video_step_render_targets(raw, gpu): whether render_video() renders
  the outputs, and from the encode textures;
- obs_video_step_render(raw, gpu, gpu_conversion): convert, feed the GPU
  encoders, stage;
- obs_video_step_ring(cur_texture): the texture before and after
  cur_texture in the ring of NUM_TEXTURES;
- obs_video_step_download(raw) and obs_video_step_output(raw,
  frame_ready): whether output_frame() downloads, and whether it outputs
  the downloaded frame.

obs-video.c.h keeps every gs_* call in its original order and under its
original conditions (a copy surface is still staged only when it is not
NULL), the deques (deque_data(), and its NULL check when texture-based
encoding stops, deque_pop_front(), deque_push_back(), deque_free()),
os_sem_post(), obs_host_tex_frame_acquire() and _release(), the profiler
and debug markers, download_frame()'s maps, the mix list and its mutex.
The atomics are still loaded by os_atomic_load_long(), raw_active first.

Why several calls and not one plan per function:
- queue_frame() asks obs_host_nv12_planes_share_texture() only for a
  copy, after the Y copy. One queue plan would have to ask it first, and
  for every frame. So obs_video_step_queue() decides the action, the C
  copies Y, then asks the host and passes its answer to
  obs_video_step_queue_copy_uv();
- render_video() reads video->gpu_conversion after render_output_texture()
  and obs_host_gpu_encode_surfaces(). obs_video_step_render_targets()
  answers the two questions before them, obs_video_step_render() the three
  after, at the point the C read the flag;
- output_frame() computes the previous texture first and advances
  video->cur_texture last, from the value it holds then; it calls
  obs_video_step_ring() at both points, so both reads stay where they were.
The duplicate path's "texture-based encoding is stopping" return stays a
NULL check of deque_data() in the C: it depends on the deque, not on a
value the C passes.

Checked in a scratch harness (not committed) that compiles origin/master's
update_active_state(), the three clear_*_frame_data(), stage_output_texture(),
unmap_last_surface(), queue_frame(), encode_gpu(), output_gpu_encoders(),
render_video(), download_frame() and output_frame() verbatim, and the moved
ones with obs-video-output-step.c.h, as two translation units over shared
stubs. The stubs log every gs_*, deque, os_sem_post(), mutex, profiler,
debug marker, os_atomic_load_long() and host call with its arguments
(pointers by identity), and the whole mix is logged after each run (every
surface and texture slot, the flags, cur_texture, and each deque frame by
frame). The two logs are byte-identical under GCC -O2 and clang -O0 over
1,000,000 runs of each of the six functions and 250,000 runs of eight
frames (about 250 million logged calls per build), and under GCC with ASan
and UBSan over a tenth of that (queues of whole frames only there: both
sides read a partial frame misaligned).
The inputs cover every update_active_state() combination (the three was
flags and both atomics at LONG_MIN, -2 to 3 and LONG_MAX), both GPU
conversion and converted flags, channel counts 0 to 3 with each copy
surface set and convert texture array, the four host behaviours of
obs_host_gpu_encode_surfaces() (none, Windows's one channel, two, zero),
both answers of obs_host_nv12_planes_share_texture(), empty, partial and
full queues (sizes that are not whole frames included), repeat counts from
INT_MIN + 1 to INT_MAX, stage surface maps that fail, and runs of eight
frames with the outputs switching on and off. obs_video_step_ring() equals
master's two expressions for every int from -2,000,000 to 2,000,000 and
near both ends. Twenty planted changes to the moved code all fail it; two
more survive and are equivalent (the output check without raw_active,
since frame_ready is set only for raw output, and the base and raw clears
swapped, which free the same deque and touch no common field).

NUM_TEXTURES and NUM_CHANNELS are copied into the header as
OBS_VIDEO_STEP_TEXTURES and OBS_VIDEO_STEP_CHANNELS, so it needs only
stdbool.h and stddef.h; obs-video.c.h checks both with _Static_assert. The
new file joins libobs.core-output, before obs-video.c.h, with
libobs.pch.h, which includes the new header; source-sets.json lists it.
The exported symbol set is unchanged (1766); the ten functions are local.

git log -L on each function shows only upstream history plus the fork's
host interfaces (obs-rust#1051), which turned the Windows ifdefs of queue_frame()
and render_video() into obs_host_tex_frame_acquire() and _release(),
obs_host_nv12_planes_share_texture() and obs_host_gpu_encode_surfaces().
Those calls stay where they are; the rules around them are moved as they
were.

Refs obs-rust#2165
test_video_output_step_steps compiles libobs/obs-video-output-step.c.h
into itself and calls the ten functions through their header (they are
hidden in libobs). 19 cases pin the current C:

- update_active_state(): every combination of the three was flags and of
  both active counters at LONG_MIN, -2, -1, 0, 1, 2, 3 and LONG_MAX (512),
  the single edges (raw starts, GPU starts with raw on and alone, both
  stop, negative counters off), and was flags that disagree with each
  other, which are taken as they are (characterized, not endorsed);
- stage_output_texture(): without GPU conversion, copy surface 0 from the
  output texture for every channel count and both converted flags; with
  it, channel_count surfaces from the convert textures for 0 to
  NUM_CHANNELS once converted, and nothing before; a count past
  NUM_CHANNELS (4, SIZE_MAX) is passed through (characterized, not
  endorsed: the hosts give 1 or NUM_CHANNELS);
- queue_frame(): no free frame is a duplicate for every queue, count and
  raw flag; a free frame with one queued duplicates repeats (counts 2 and
  3) and takes a single frame (count 1), copied for raw output, else
  swapped; with none queued a repeat is copied even without raw output;
  counts of 0 and below act as 1, and sizes are only tested for 0
  (characterized, not endorsed); the UV copy for both host answers; the
  count left for 1, 2 and 3 (that many frames), 0 (-1) and INT_MAX;
- render_video(): the targets for each pair of raw and GPU flags, and the
  convert, encode and stage steps for all eight combinations;
- output_frame(): the ring at 0 and NUM_TEXTURES - 1 (the wrap), two turns
  round it, values outside it (only exactly NUM_TEXTURES wraps;
  characterized, not endorsed), and the download and output for every
  pair.

It is in the test graph in name order, built with ASan and UBSan, with
libm except on MSVC like the other step tests, so it runs on every
platform without a display. tests.d/test_video_output_step_steps.cmake
says what it covers. Passes 19/19 on the C backend.

Refs obs-rust#2165
The safe API of obs_core::video_output_step, with todo!() bodies:
ActivationInput and Activation (the two structs of update_active_state()),
Stage (Skip, Output, or Convert with a channel count, read back as
staged, from_output and channels like the C struct), QueueStep (Duplicate,
Copy, Swap), RenderTargets, RenderSteps and Ring, the constants TEXTURES
and CHANNELS, and activation(), stage(), queue_step(), copy_uv(),
queue_count(), render_targets(), render_steps(), ring(), download() and
output(). The active counters are c_long, as os_atomic_load_long() gives
them; counts and ring values are c_int; sizes are usize.

tests/obs_core/video_output_step.rs ports test_video_output_step_steps.c
1:1: its 19 cases as steps_*, under the same names, in the same order,
with the same values, the "characterized, not endorsed" ones included.
Nine new edge_* cases add:
- the ring at both int ends and queue_count() at INT_MIN, where the C's
  arithmetic overflows (undefined): the port wraps;
- a walk of a thousand frames round the ring, each downloading the texture
  rendered the frame before;
- proptests (4096 cases each) against the C's expressions written out:
  activation over full-range counters and every was flag, stage over
  full-range channel counts, the queue step over full-range sizes and
  counts (biased to 0, one frame and -2 to 4), queue_count() and the ring
  over every int, and the render, download, output and UV copy answers.

soldr cargo test -p obs-data --test obs-tests-core -- video_output_step
fails 27 of 28 with "not yet implemented"; steps_constants, which reads
only the constants, passes.

Refs obs-rust#2165
Ports libobs/obs-video-output-step.c.h, the per-frame video output step
decisions of obs-video.c, to the video_output_step module of
rust/obs-core, next to video_copy_plan. obs-video.c.h stays C around it:
every gs_* call in its order and under its conditions, the deques,
os_sem_post(), the texture-frame host calls, the shared-planes and
GPU-encode-surface host queries (it passes their answers), the maps of
download_frame(), the mix list and its mutex, and the two atomic loads (it
passes the loaded values). The hybrid libobs drops the C file and links the
Rust shims in its place.

Safe core (src/video_output_step.rs):
- activation(): a counter above 0 is active; the base, raw and GPU clears
  for each that was not active and is now; the new was flags;
- stage(): Output without GPU conversion, Convert { channels } once
  converted, else Skip, read back as the C struct's staged, from_output
  and channels;
- queue_step(): Duplicate when no frame is free or a repeat finds one
  queued, else Copy for raw output or a repeat, else Swap; copy_uv(): not
  when the planes share a texture; queue_count(): count - 1;
- render_targets() and render_steps(): outputs for raw or GPU, encode
  textures for GPU; convert with GPU conversion, encode for GPU, stage for
  raw;
- ring(): prev is NUM_TEXTURES - 1 at 0, else cur - 1; next is cur + 1,
  0 at exactly NUM_TEXTURES; download() and output().
The module denies clippy's arithmetic, indexing, unwrap, expect, panic,
todo, unimplemented and unreachable lints, keeps no state and takes no
lock. Intentional difference, unreachable: where the C's int arithmetic
overflows (undefined: queue_count(INT_MIN), the ring's prev of INT_MIN and
next of INT_MAX), the port wraps.

How it differs from the plan's API sketch, and why:
- stage() returns a channel count, not a [bool; NUM_CHANNELS]:
  channel_count comes from obs_host_gpu_encode_surfaces() as a size_t and
  the C loops over it as given, so a count keeps that loop exactly;
- queue_step() has no Stop: the C stops when deque_data() gives NULL,
  which depends on the deque, so that check stays in the C, on the
  Duplicate path; and copy_uv is its own call, because the C asks the host
  for it only after the Y copy;
- the activation counters cross as c_long, not bools: the C loads them and
  the "> 0" is a decision, so it moved;
- render_video() and output_frame() each ask twice (render_targets() then
  render_steps(); ring() at the start and at the end), at the points where
  the C read its values.

Shims (src/ffi/video_output_step.rs): the ten functions keep the C
signatures. The six structs of obs-video-output-step.h are mirrored
texture, surface, deque or mix pointer crosses.

C side: the file leaves libobs.core-output for its own group,
libobs.ported-video-output-step (LIBOBS_PORTED_SOURCES), whose PCH is only
obs-video-output-step.h; libobs.pch.h keeps the header for obs-video.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.

test_video_output_step_steps still compiles the C file into itself,
unchanged. It has no _rust variant: linking it against libobs_rust-static
was tried and fails with 380 undefined references (108 symbols), because
the shims' archive member (an obs_core codegen unit) also holds the other
obs-core ports, which need libobs and obs-util's platform code (blog,
bcrash, the audio resampler, GIO and D-Bus), as for
test_video_copy_plan_steps (obs-rust#2133). The Tier 3 parity tests compare the
shims with this C instead, and the real-libobs tests
(test_video_output_formats, test_raw_output_video_reset,
test_encoding_lag_rescale, test_scaled_mix_alpha, test_video_output_plan
and the texture encoder tests) see the swap through the hybrid.

Tier 1 obs_core::video_output_step passes 28/28.

Refs obs-rust#2165
rust/obs-c-oracle/oracle/video_output_step.c compiles the unchanged
libobs/obs-video-output-step.c.h with its ten functions renamed oracle_*,
plus its constants and the layout of its six structs. Five struct tags
share their function's name, so the renames rename them too and the
layout table names the renamed tags.
tests/obs_core/video_output_step_parity.rs (13 tests) runs the shim, the C
and the safe core on every input and compares the results exactly: each
struct field by field (the size_t channel count and the int ring values
included), the queue action as its C value, the bools as bools.

- the 5 constants, and the size, alignment and every offset of the six
  structs, the enum's size and sizeof(long), against the C;
- exhaustive: every was flag against both counters at 14 long and int
  edges (1,568 activations); both flags against channel counts 0 to 10
  and the size_t edges; every queue size at 0, 1, 47, 48, 49, 96 and the
  size_t edges against counts from -3 to 5 and the int edges and both raw
  flags (2,600 steps, each action reached); the count left and the ring
  for every int from -200,000 to 200,000 and the edges; every flag of the
  render, download, output and UV copy answers;
- proptests (8192 cases each) over full-range counters, channel counts,
  sizes, counts and ring values.

The C is undefined where its int arithmetic overflows:
obs_video_step_queue_count(INT_MIN), and obs_video_step_ring() of INT_MIN
(prev) and INT_MAX (next). Those three inputs are not given to the C;
there the shim is compared with the core, which wraps, and one test pins
the wrapped values. That is the one intentional difference, unreachable.

The parity module passed 10 runs in a row with fresh seeds.
test_video_output_step_steps passed 19/19 under ASan and UBSan.
The committed regression files hold the seeds proptest saved while mutants
failed; all pass on the port.

Mutation check (not committed): 37 mutants, 28 in the core and 9 in the
shims, each run against Tier 1 (28 tests) and Tier 3 (13). 35 fail; the 2
that survive are equivalent (the render targets' encode without
"outputs &&", which gpu_active already implies, and the shim's output
arguments swapped, an && of two bools). Tier 1 does not reach the shims;
Tier 3 kills every shim mutant, the stage struct reordered included (the
layout test). The two mutants that change only the C's undefined INT_MIN
cases (the count left and the ring's prev saturating instead of wrapping)
are killed by the edge test in Tier 1 and the wrap test in Tier 3.

Refs obs-rust#2165
- rust/README.md: the obs-core crate row names
  libobs/obs-video-output-step.c.h, and a row for the file says what is
  swapped in the hybrid, what obs-video.c.h keeps, why the decisions are
  split where they are, the one intentional difference (the C's undefined
  int overflows wrap; unreachable), why there is no _rust variant, and what
  Tier 3 covers;
- rust/obs-core: the crate description and the lib.rs docs list the
  module;
- docs/rust-port/naming.md: obs-video-output-step.c.h, extracted from
  obs-video.c.h, maps to rust/obs-core;
- rust/obs-c-oracle/benches/port_comparison/video_output_step.rs:
  video_output_step/frame/1024, a dozen decisions per frame (activation,
  ring twice, render targets and steps, stage, queue step, UV copy, count
  left, download, output) over 1024 frames on each side, folded into a
  checksum both must match. It allocates nothing, so the per-module
  allocation check holds. It joins the sorted modules![] list.

port_comparison (x86_64 Linux, GCC, not pinned, load average 10 to 20;
median of two runs): 17.2 and 21.4 us Rust, 25.5 and 41.3 us C per 1024
frames, about 10 to 20 ns a frame either way, within the noise of this
host. The Rust shims are not slower.

Fixes obs-rust#2165
@rdoupe
rdoupe force-pushed the port/video-output-step branch from 7f101fb to 5c8e424 Compare October 24, 2026 17:05
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 5c7394e Oct 24, 2026
37 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 25, 2026
…bs-rust#2259)

* libobs: Move the canvas texture draw and display clear decisions into obs-canvas-draw.c

Behaviour-neutral extraction before the Rust port. The decisions of
obs_render_canvas_texture_internal() (obs.c) and render_display_begin()
(obs-display.c) move to libobs/obs-canvas-draw.c.h, as eight functions on
plain values declared in the new libobs/obs-canvas-draw.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):

- obs_canvas_draw_skip(has_mix, texture_rendered): the early return, for a
  canvas with no mix or a mix that has not rendered its texture. obs.c
  passes video && video->texture_rendered, so the field is still read only
  when there is a mix;
- obs_canvas_draw_technique(source_space, current_space): Draw, DrawTonemap
  for GS_CS_709_EXTENDED into SDR, DrawMultiply into GS_CS_709_SCRGB, as
  enum obs_source_draw_technique (obs-source-draw-technique.h), which the
  source draw decisions already use; obs.c maps it to the name with
  obs_source_draw_technique_name();
- obs_canvas_draw_reads_white_level(current_space) and
  obs_canvas_draw_multiplier(sdr_white_level): whether to read
  obs_get_video_sdr_white_level() (GS_CS_709_SCRGB only), and the
  white level / 80.0f;
- obs_display_clear_reads_space(always_srgb) and
  obs_display_clear_linear(current_space): the clear colour is sRGB when
  the host always clears in sRGB, else linear only into GS_CS_SRGB;
- obs_display_clear_flags(use_clear_workaround): depth and stencil, and
  colour without the workaround;
- obs_display_clear_draws_solid(use_clear_workaround): whether the solid
  effect draws the clear colour.

obs.c.h and obs-display.c.h keep every gs_* call in its order and under its
conditions, the effect and param lookups, the blend state, the swapchain,
the resize, the viewport, the solid effect, and the two queries:
obs_get_video_sdr_white_level() is still called only into GS_CS_709_SCRGB,
and gs_get_color_space() for the clear colour only when
obs_host_display_clear_color_always_srgb(), asked first, says no. That is
why each is split into a "reads" question and the answer from the value.

Checked in a scratch harness (not committed) that compiles origin/master's
obs_render_canvas_texture_internal() and render_display_begin() verbatim,
and the moved ones with obs-canvas-draw.c.h, as two translation units over
shared stubs. The stubs log every gs_* call, the white level, the host
query, the colour space queries and vec4_from_rgba() and _srgb() with their
arguments (floats as bits, pointers by identity), and the mix and display
fields after each run. The two logs are byte-identical under GCC and clang
at -O0 and -O2 over 2,050,400 runs each (about 14.5 million logged calls),
and under GCC with ASan and UBSan over 450,400. The inputs cover, for the
canvas, no mix and both texture_rendered values, every pair of the four
colour spaces and five values outside the enum (INT_MIN and INT_MAX
included), white levels of 80, 300, 0, -0, NaN, the infinities, a
denormal and FLT_MAX, and random bit patterns; for the display, equal and
changed sizes, both update_color_space values, present ready or not, both
host answers, every space, five background colours and both workaround
values, plus a million random runs of each. 17 of 18 planted changes fail
it; the one that survives is equivalent (the skip ignoring has_mix, since
obs.c passes texture_rendered false without a mix).

GS_CLEAR_COLOR, GS_CLEAR_DEPTH and GS_CLEAR_STENCIL are copied into the
header as OBS_DISPLAY_CLEAR_*, so it needs only stdbool.h, stdint.h and
obs-source-draw-technique.h; obs-display.c.h checks them with
_Static_assert. The new file joins libobs.core with libobs.pch.h, which
includes the new header; source-sets.json lists it. The exported symbol set
is unchanged (1766); the eight functions are local.

git log -L on both functions shows upstream history (jpark37's colour space
and display clear work, PatTheMav's switch and Apple Silicon clear colour
fixes) plus the fork's host interfaces (obs-rust#1051), which turned the Apple
ifdef into obs_host_display_clear_color_always_srgb(). That call stays
where it is.

Refs obs-rust#2240.

* test: Characterize the canvas texture draw and display clear decisions

test_canvas_draw_steps compiles libobs/obs-canvas-draw.c.h into itself and
calls the eight functions through their header (they are hidden in libobs).
12 cases pin the current C:

- the constants: the three technique values, the copied clear flags against
  GS_CLEAR_*, and the technique names;
- obs_render_canvas_texture_internal(): both early returns (no mix, texture
  not rendered), and no mix with a rendered flag, which obs.c never passes
  (characterized, not endorsed); the technique for every pair of the four
  colour spaces, and spaces outside the enum (-1, 4, 99, INT_MIN, INT_MAX)
  on either side; the white level read into scRGB only; the multiplier at
  SDR white levels of 80, 300 and 0, and for any float (-0, negative, the
  infinities, NaN, a denormal, FLT_MAX, and every multiple of 80 up to
  80 * 2^16); and the name and multiplier obs.c ends up with for every pair
  and level;
- render_display_begin(): whether the space is read for both host answers,
  the linear clear colour into GS_CS_SRGB only, the conversion for every
  combination of host answer and space, and the clear flags and solid
  colour workaround for both workaround values.

It is in the test graph in name order, built with ASan and UBSan, with
libm except on MSVC like the other step tests, so it runs on every
platform without a display. tests.d/test_canvas_draw_steps.cmake says what
it covers. Passes 12/12 on the C backend.

No test drives the two callers through a real graphics device; the canvas
tests (test_canvas_flags, test_scene_private_canvas,
test_canvas_destroy_scene_refs, test_shutdown_canvas_weak_ref) cover the
canvases and their mixes through the real libobs.

Refs obs-rust#2240.

* obs-core: Add Tier 1 tests for the canvas draw port

The safe API of obs_core::canvas_draw, with todo!() bodies: the colour
space constants (GS_CS_*), the clear flags (CLEAR_COLOR, CLEAR_DEPTH,
CLEAR_STENCIL), SCRGB_WHITE, Technique (Draw, DrawMultiply, DrawTonemap,
with the C's values and names), and skip(), technique(),
reads_white_level(), multiplier(), clear_reads_space(), clear_linear(),
clear_flags() and clear_draws_solid(). Colour spaces are c_int, as the C
passes enum gs_color_space.

Technique mirrors only the three values of enum obs_source_draw_technique
that these decisions return: obs-core cannot depend on obs-source, whose
draw_technique module (obs-rust#2206) has all six, with the same numbers and names.

tests/obs_core/canvas_draw.rs ports test_canvas_draw_steps.c 1:1: its 12
cases as steps_*, under the same names, in the same order, with the same
values, the "characterized, not endorsed" ones included. Three new edge_*
proptests (4096 cases each) check the technique, the white level read and
the linear clear colour over any int space against the C's switch written
out, the multiplier over every f32 white level taken as a bit pattern
(obs-rust#1951) against one division by 80, and the skip, clear flags and
workaround over every flag.

soldr cargo test -p obs-data --test obs-tests-core -- canvas_draw fails 14
of 15 with "not yet implemented"; steps_constants, which reads only the
constants and the enum, passes.

Refs obs-rust#2240.

* obs-core: Port the canvas texture draw and display clear decisions

Ports libobs/obs-canvas-draw.c.h, the canvas texture draw decisions of
obs.c and the display clear decisions of obs-display.c, to the canvas_draw
module of rust/obs-core. obs.c.h and obs-display.c.h stay C around it:
every gs_* call in its order and under its conditions, the effect and
param lookups, the blend state, the swapchain, the resize, the viewport,
the solid effect, obs_get_video_sdr_white_level() and
obs_host_display_clear_color_always_srgb() (they pass the values in). The
hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/canvas_draw.rs):
- skip(): no mix, or a texture not rendered;
- technique(): DrawTonemap for GS_CS_709_EXTENDED into GS_CS_SRGB or
  GS_CS_SRGB_16F, DrawMultiply into GS_CS_709_SCRGB, else Draw (a space
  outside the enum included);
- reads_white_level(): into GS_CS_709_SCRGB only; multiplier(): the white
  level over SCRGB_WHITE (80), one f32 division as in the C;
- clear_reads_space(): not when the host always clears in sRGB;
  clear_linear(): into GS_CS_SRGB only;
- clear_flags(): CLEAR_DEPTH | CLEAR_STENCIL, with CLEAR_COLOR without the
  clear workaround; clear_draws_solid(): with the workaround.
The module denies clippy's arithmetic, indexing, as-conversion, unwrap,
expect, panic, todo, unimplemented and unreachable lints, keeps no state
and takes no lock. No intentional differences from the C.

Technique mirrors the three values of enum obs_source_draw_technique these
decisions return, with the C's numbers and names; obs-core cannot depend on
obs-source, whose draw_technique (obs-rust#2206) has all six.

Shims (src/ffi/canvas_draw.rs): the eight functions keep the C signatures.
No canvas, mix or display is mirrored: only colour spaces as c_int, flags
as bool, the white level as f32, the clear flags as u32 and the technique
as the enum's u32, as the obs-source shims return it.

C side: the file leaves libobs.core for its own group,
libobs.ported-canvas-draw (LIBOBS_PORTED_SOURCES), whose PCH is obs.h (for
the gs_color_space values) and obs-canvas-draw.h; libobs.pch.h keeps the
header for obs.c and obs-display.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 set is 1766 on the C, hybrid and c-ref builds, the same set on all
three, with the eight functions local.

test_canvas_draw_steps still compiles the C file into itself, unchanged. It
has no _rust variant: linking it against libobs_rust-static was tried and
fails with 399 undefined references (108 symbols), because the shims'
archive member (an obs_core codegen unit) also holds the other obs-core
ports, which need libobs and obs-util's platform code, as for
test_video_output_step_steps (obs-rust#2181). The Tier 3 parity tests compare the
shims with this C instead.

Tier 1 obs_core::canvas_draw passes 15/15.

Refs obs-rust#2240.

* obs-core: Check the canvas draw port against the C

rust/obs-c-oracle/oracle/canvas_draw.c compiles the unchanged
libobs/obs-canvas-draw.c.h with its eight functions renamed oracle_*, plus
the enum values, the clear flags (the header's copies and graphics.h's) and
the type sizes, and obs_source_draw_technique_name().
tests/obs_core/canvas_draw_parity.rs (7 tests) runs the shim, the C and the
safe core on every input and compares the results exactly: the C's bool as
the byte it returns, the technique as its value, the clear flags as the
uint32_t, the multiplier as its bits (a NaN's payload included).

- the 19 values and sizes against the C headers, and the technique names;
- exhaustive: every flag of the skip, the clear colour, the clear flags and
  the workaround; the technique, the white level read and the linear clear
  colour for every pair of 649 spaces (-320 to 320 and four near each end
  of int); the multiplier for 131,072 bit patterns covering every sign,
  exponent and NaN class, every multiple of 80 to 80 * 2^16 and 24 special
  levels;
- both callers' decisions in their order (the skip, the technique name,
  the white level read and the multiplier only when it is read; the host
  answer, the space only when it is read, the flags and the workaround),
  for every flag, the spaces -3 to 6 and the int ends, and the special
  levels;
- proptests (20,000 cases each) over any space, any white level bit
  pattern (obs-rust#1951: no float range) and every flag, decision by decision and
  both callers at once.

No input reaches an integer division or a 128-bit multiply on either side
(the one division is the f32 multiplier, which cannot trap), so nothing is
skipped on MSVC x64. No intentional differences.

The parity module passed 10 runs in a row with fresh seeds.
test_canvas_draw_steps passed 12/12 under ASan and UBSan. The committed
regression files hold the seeds proptest saved while mutants failed; all
pass on the port.

Mutation check (the mutants are not committed): 28 mutants, 19 in the
core and 9 in the shims, each run against Tier 1 (15 tests) and Tier 3
(7). 27 fail. The one that survives is equivalent: the shim passing
has_mix and texture_rendered swapped, and !a || !b is symmetric. Tier 1
does not reach the shims; Tier 3 kills every other shim mutant. Each
mutant and the tests that fail on it:

 1. core, skip: || becomes &&
    T1: edge_skip_and_clear_any_flags, steps_skip_every_input
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
 2. core, skip ignores has_mix
    T1: edge_skip_and_clear_any_flags, steps_skip_every_input
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
 3. core, technique: GS_CS_SRGB_16F loses the tonemap
    T1: edge_technique_any_space, steps_canvas_plan_every_input,
        steps_technique_space_pairs
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
 4. core, technique: tonemaps a scRGB source, not 709 extended
    T1: edge_technique_any_space, steps_canvas_plan_every_input,
        steps_technique_space_pairs
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
 5. core, technique: Draw into scRGB
    T1: edge_technique_any_space, steps_canvas_plan_every_input,
        steps_technique_outside_enum, steps_technique_space_pairs
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
 6. core, technique: DrawTonemap into 709 extended
    T1: edge_technique_any_space, steps_canvas_plan_every_input,
        steps_technique_outside_enum, steps_technique_space_pairs
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
 7. core, reads_white_level: >= scRGB
    T1: edge_technique_any_space, steps_reads_white_level_spaces
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
 8. core, multiplier: / 81
    T1: edge_multiplier_any_bits, steps_canvas_plan_every_input,
        steps_multiplier_any_level, steps_multiplier_white_levels
    T3: callers_every_input, every_white_level_class, proptest_callers,
        proptest_decisions
 9. core, multiplier: * (1 / 80)
    T1: edge_multiplier_any_bits, steps_multiplier_white_levels
    T3: callers_every_input, every_white_level_class, proptest_callers,
        proptest_decisions
10. core, multiplier: NaN becomes 0
    T1: edge_multiplier_any_bits, steps_multiplier_any_level
    T3: callers_every_input, every_white_level_class, proptest_callers,
        proptest_decisions
11. core, clear_reads_space: not negated
    T1: edge_skip_and_clear_any_flags, steps_clear_colour_every_input,
        steps_clear_reads_space_every_input
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
12. core, clear_linear: <= GS_CS_SRGB_16F
    T1: edge_technique_any_space, steps_clear_colour_every_input,
        steps_clear_linear_spaces
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
13. core, clear_linear: also below 0
    T1: edge_technique_any_space, steps_clear_colour_every_input,
        steps_clear_linear_spaces
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
14. core, clear_flags: no stencil
    T1: edge_skip_and_clear_any_flags, steps_clear_every_input
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
15. core, clear_flags: colour with the workaround
    T1: edge_skip_and_clear_any_flags, steps_clear_every_input
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
16. core, clear_draws_solid: negated
    T1: edge_skip_and_clear_any_flags, steps_clear_every_input
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
17. core, Technique::value: DrawTonemap 3
    T1: steps_constants
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions, values_match_the_c_headers
18. core, Technique::name: DrawTonemap renamed
    T1: steps_canvas_plan_every_input, steps_constants
    T3: values_match_the_c_headers
19. core, CLEAR_STENCIL: 1 << 3
    T1: steps_constants
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions, values_match_the_c_headers
20. shim, skip: arguments swapped: SURVIVES (equivalent)
    T1: none
    T3: none
21. shim, skip: texture_rendered always true
    T1: none
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
22. shim, technique: spaces swapped
    T1: none
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
23. shim, reads_white_level: always scRGB
    T1: none
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
24. shim, multiplier: level negated
    T1: none
    T3: callers_every_input, every_white_level_class, proptest_callers,
        proptest_decisions
25. shim, clear_reads_space: always false
    T1: none
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
26. shim, clear_linear: negated
    T1: none
    T3: callers_every_input, every_space_pair, proptest_callers,
        proptest_decisions
27. shim, clear_flags: workaround negated
    T1: none
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions
28. shim, clear_draws_solid: calls clear_reads_space
    T1: none
    T3: callers_every_input, every_flag, proptest_callers,
        proptest_decisions

Refs obs-rust#2240.

* docs: Record the canvas draw port

- rust/README.md: the obs-core crate row names libobs/obs-canvas-draw.c.h,
  and a row for the file says what is swapped in the hybrid, what obs.c.h
  and obs-display.c.h keep, why the white level and the clear colour's space
  are each split into a "reads" question and an answer, why Technique
  mirrors only three values, why there is no _rust variant, and what Tier 3
  covers;
- rust/obs-core: the crate description and the lib.rs docs list the module;
- docs/rust-port/naming.md: obs-canvas-draw.c.h, extracted from obs.c.h and
  obs-display.c.h, maps to rust/obs-core;
- rust/obs-c-oracle/benches/port_comparison/canvas_draw.rs:
  canvas_draw/canvas_texture and canvas_draw/display_begin, each caller's
  decisions in their order over 1024 calls on each side, folded into a
  checksum both must match. It allocates nothing, so the per-module
  allocation check holds. It joins the sorted modules![] list.

port_comparison (x86_64 Linux, GCC, not pinned, load average 29 to 35;
median of two runs): canvas_texture 10.4 and 8.7 us Rust, 5.6 and 13.4 us
C; display_begin 7.8 and 8.8 us Rust, 5.8 and 13.4 us C, per 1024 calls:
about 5 to 13 ns a call either way, within the noise of this host.

No docs/features.md line: the port changes no behaviour.

Fixes obs-rust#2240.
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 video frame output step in obs-video.c to obs-core

2 participants