Repository navigation
obs-core: Port the output frame copy plan and scale effect - #2133
Merged
Merged
Conversation
…eo-copy-plan.c Behaviour-neutral extraction before the Rust port. The decisions of set_gpu_converted_plane(), set_gpu_converted_data(), copy_rgbx_frame(), get_scale_effect_internal(), resolution_close(), get_scale_effect() and the per-mix match of can_reuse_mix_texture() move to libobs/obs-video-copy-plan.c.h, as five functions on plain values declared in the new libobs/obs-video-copy-plan.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_video_gpu_copy_plan(format, width, height, in_linesize, out_linesize): the copies of set_gpu_converted_data(), one per plane: one memcpy() of width * height when the row size and both line sizes match, else height rows of the row size at the two strides. The switch is moved as it was, with its uint32_t row sizes (width * 2, width * 4, width / 2) and its size_t product; the UV copy of a packed NV12 or P010 frame (input linesize[1] of 0) is marked to start where plane 0's source ended, at linesize[0]; - obs_video_rgbx_copy_plan(width, height, in_linesize, out_linesize): copy_rgbx_frame()'s one copy of linesize * height when the line sizes match, else rows of (size_t)width * 4; - obs_video_scale_resolution_close(): resolution_close(), still in long; - obs_video_scale_effect_choice(input): get_scale_effect() with get_scale_effect_internal(): the default effect when close, else the bilinear low resolution effect when the output is under half the base size on both axes, else the scale type's, with the bicubic-else-default fallback when that effect did not load. Which effects loaded comes in as a bit mask; the result is an enum; - obs_video_mix_reuse_match(other, mix): one comparison of can_reuse_mix_texture() (view, render space, base size, other rendered). obs-video.c.h keeps every memcpy(): set_gpu_converted_plane() now runs one copy of the plan and returns where its source ended, set_gpu_converted_data() gets the plan and runs its copies in order (copy i from input plane i or from plane 0's end, into output plane i), and copy_rgbx_frame() runs its one copy. It also keeps the frame lock and unlock, video_output_get_info(), the effect pointers (scale_effect_pointer() maps the enum to obs->video's, loaded_scale_effects() builds the mask), and the walk of the mix list. What changes in what the C reads, not in what it does: - get_scale_effect() reads which effects loaded (five pointer reads) before deciding. video_output_get_info() is still called only when the target is not close to the base size, as before; - can_reuse_mix_texture() reads the five fields of each earlier mix (and of this mix's texture_rendered, which the match ignores) before comparing, instead of stopping at the first that differs. Checked in a scratch harness (not committed) that compiles origin/master's functions verbatim against the moved ones and the new callers, on stand-in mixes and frames: 1,200,000 GPU and 1,200,000 RGBX copies on real buffers (under ASan) write the same bytes; 6,000,000 of each with full-range u32 sizes and line sizes (the width * 2 and * 4 wraps included) make the same memcpy() calls (destination, source, size, in order); 24,000,000 scale effect choices return the same pointer with the same video_output_get_info() calls, resolution_close() agrees on the same 24,000,000, and 21,000,000 mix walks return the same result and index. Deliberately broken moves (P416's width * 4, the RGBX row size, the packed UV stride, the close bound, the fallback, the rendered check) all fail it. The scale types are copied into the header, so it needs only media-io/video-io.h; obs-video.c.h checks them against obs.h, and OBS_VIDEO_COPY_MAX against NUM_CHANNELS, 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. docs/rust-port/naming.md maps it to rust/obs-core. The exported symbol sets are unchanged (1766); the five functions are local. git log -L on each moved function shows only upstream history. The fork's obs-rust#581 changed draw_mix_texture() (the reused texture is drawn without blending), which stays in obs-video.c.h unchanged; the reuse match it depends on is moved as it was. Refs obs-rust#2118
test_video_copy_plan_steps compiles libobs/obs-video-copy-plan.c.h into itself (ASan and UBSan) and calls only its five hidden functions, through obs-video-copy-plan.h. 22 cases pin: - every video_format: the seven with a GPU conversion (I420, NV12, I444, I010, P010, P216, P416) make their 3 or 2 copies, every other format and two values outside the enum (VIDEO_FORMAT_R10L + 1, 0x7fffffff) none; - each format's copies by value: row sizes (width, width / 2, width * 2, width * 4), rows (height, height / 2), the strides, and one copy of row * rows only when the row size equals both line sizes; - the packed NV12 and P010 frame (input linesize[1] of 0): the UV copy starts where plane 0 ended and uses input linesize[0]; linesize[2] does not matter, and P216 with linesize[1] of 0 is not packed; - each input and output line size moved off the row size alone, for every format, turning only the copies that read it into rows; - widths 0, 1, 2, 3, 639, 640, 1919, 1921 against heights 0, 1, 2, 3, 359, 1081, tight and padded (the halves round down); - characterized, not endorsed: widths near the wrap (0x40000000, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff) with heights up to 0xffffffff: width * 2 and width * 4 wrap in uint32_t (P416's plane 1 of a 0x40000000-wide frame is 0 bytes, and with line sizes of 0 it is one copy of 0), while the single copy's size is a size_t product; - copy_rgbx_frame(): matching line sizes are one copy of line size * height, even under width * 4 (characterized, not endorsed); otherwise rows of (size_t)width * 4, which does not wrap at 2^32; - resolution_close() at -17, -16, -15, 0, 15, 16, 17 on each axis and both, one close axis not being enough, and the ends of the sizes obs_reset_video() allows (2 and 32768); - the scale effect: close is the default for every type, output size and loaded mask; the type table (bilinear is the default effect; disable, point and values outside the enum are bicubic); the low resolution rule (strictly under half on both axes of the output's size, the halves rounding down, never for a base of 0 or 1); each type x low resolution or not x each of the five effects missing; and each type x low resolution or not x close or not x all 32 loaded masks; - the mix reuse match: each of view, render space, base width and height and the other mix's rendered flag, with this mix's flag ignored. The test is in the core suite of the test graph and links libm except on MSVC. test_video_output_formats, test_scaled_mix_alpha, test_encoding_lag_rescale and test_video_output_plan cover the callers through the real libobs. Refs obs-rust#2118
The safe API of obs_core::video_copy_plan, with todo!() bodies: FrameCopy and CopyPlan (struct obs_video_copy and struct obs_video_copy_plan), ScaleEffect and its loaded bits, ScaleInput, MixReuseKey, and gpu_copy_plan(), rgbx_copy_plan(), resolution_close(), scale_effect() and mix_reuse_match(). Formats and scale types outside their C enums are None, which takes the C's default. tests/obs_core/video_copy_plan.rs ports test_video_copy_plan_steps.c 1:1: its 22 cases as steps_*, under the same names, in the same order, with the same values, the "characterized, not endorsed" ones included. Eight new edge_* cases add: - resolution_close() against a 32-bit long (MSVC: the conversion and the subtraction emulated in i32, overflow detected) for every size obs_video_size_valid() allows (2 to 32768) against every size within 40 of it and the two ends, and for every size below 2^31 in a proptest; and against an i64 subtraction for full-range u32 sizes; - proptests (4096 cases each) over every format with full-range u32 sizes and line sizes, biased to the row sizes so the single copy is taken, the packed NV12 and P010 included; values outside the enum; the RGBX copy; the scale effect over full-range sizes, every type and loaded mask, and targets near the base size; and the reuse match over full-range keys with each field flipped. soldr cargo test -p obs-data --test obs-tests-core -- obs_core::video_copy_plan:: fails 29 of 30 with "not yet implemented"; steps_constants, which reads only the constants, passes. Refs obs-rust#2118
Ports libobs/obs-video-copy-plan.c.h, the output frame copy plan and the scale effect choice of obs-video.c, to the video_copy_plan module of rust/obs-core, next to video_plan (which sizes the conversion textures this copies from; the two stay separate). obs-video.c.h stays C around it: every memcpy() (set_gpu_converted_plane() runs each copy), the frame lock and unlock, video_output_get_info() (still only when the target is not close to the base size), the effect pointers and the walk of the mix list. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/video_copy_plan.rs): - gpu_copy_plan(): per format, the copies of each plane: one copy of row * rows when the row size equals both line sizes, else rows of the row size at the two strides; the packed NV12 / P010 UV copy (input linesize[1] of 0) continues input plane 0 at its line size. The row sizes are u32 with wrapping_mul (width * 2 and * 4 wrap, as in C); the single copy's size is a usize wrapping product, as size_t. Every format is matched by name, so a new one must be placed; - rgbx_copy_plan(): one copy of linesize * height when the line sizes match, else rows of width * 4 as a usize product; - resolution_close(): u32::abs_diff <= 16 on both axes. That is |(i64)a - (i64)b| for every u32 pair, which is the C's long on LP64, and equals a 32-bit long (MSVC) for every size below 2^31: Tier 1 checks it for every size obs_video_size_valid() allows against every size near it, and a scratch check (not committed) compared all 1,073,676,289 pairs of sizes from 2 to 32768 (and all of 0 to 65536) per axis; - scale_effect(): Default when close; else BilinearLowres when the output is under half the base size on both axes, else the type's (Bilinear is Default; Bicubic, Disable, Point and None are Bicubic); then Bicubic, else Default, when the pick did not load; - mix_reuse_match(): view, render space and base size equal, and the other mix rendered. The module denies clippy's arithmetic, indexing, unwrap, expect, panic, todo, unimplemented and unreachable lints, keeps no state and takes no lock. Shims (src/ffi/video_copy_plan.rs): the five functions keep the C signatures. struct obs_video_copy, struct obs_video_copy_plan, struct obs_video_scale_input and struct obs_video_mix_reuse_key are mirrored #[repr(C)]; the enums cross as c_int (outside the enum is None, the C's default:) and the effect returns as u32. The line sizes are read as the C reads them, the first three of each array. No intentional differences. C side: the file leaves libobs.core-output for its own group, libobs.ported-video-copy-plan (LIBOBS_PORTED_SOURCES), whose PCH is stdlib.h and obs-video-copy-plan.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. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference; the five functions are local in all three, and the hybrid's come from the Rust archive (no C object of the group in the hybrid libobs). ci/test_ported_sources.py checks the group on every host. test_video_copy_plan_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 404 undefined references, because the shims' archive member (an obs_core codegen unit) also holds the context names, the module locale and source rules, the audio mix and the source profiler, which need libobs (blog, bmalloc, the graphics timers, obs_queue_task). The real-libobs tests (test_video_output_formats, test_scaled_mix_alpha, test_encoding_lag_rescale, test_video_output_plan) see the swap through the hybrid. Tier 1 obs_core::video_copy_plan passes 30/30. Refs obs-rust#2118
rust/obs-c-oracle/oracle/video_copy_plan.c compiles the unchanged libobs/obs-video-copy-plan.c.h with its five functions renamed oracle_*, plus its constants and the layout of its four structs. tests/obs_core/video_copy_plan_parity.rs (13 tests) runs the shim, the C and the safe core on every input and compares the results exactly: each copy of a plan field by field with the count and the unused slots, the effect as its C value, the close and reuse answers as bools. - the 16 constants, and the size, alignment and every offset of the four structs (and the two enums' sizes), against the C; - exhaustive over format x line-size equality: every C format value (the 26 of the enum, VIDEO_FORMAT_R10L + 1, -1, INT_MIN, INT_MAX) against every pattern of the six line sizes, each the plane's row size, off it or 0 (729), over 14 widths (0, 1, odd, even and the u32 wrap edges) and 5 heights: 1,530,900 frames, every kind of copy reached; and every format with the line sizes real frames have over 14 x 14 sizes; - the RGBX copy over 14^3 sizes with equal and unequal line sizes; - the scale effect over every scale type from -2 to 7 and the int ends, all 64 six-bit loaded masks and two with high bits, targets at +-15, 16, 17 and far, and outputs at the half edges, for six base sizes (266,112 calls, each of the five effects chosen); resolution_close around every edge; the reuse match with each field flipped and both rendered flags; - proptests (8192 cases each) over full-range u32 sizes and line sizes biased to the row sizes and 0, full-range scale inputs (types as any int) and full-range reuse keys. With a 32-bit long (MSVC) the oracle's resolution_close() is compared only for sizes below 2^31: the C makes a size of 2^31 or more a negative long and its subtraction can overflow. The port's i64 answer equals the C's for every size below that, so for every size obs_video_size_valid() allows; the shim is still compared with the core there. That is the one intentional difference, MSVC only and unreachable. The parity module passed 10 runs in a row with fresh seeds. test_video_copy_plan_steps passed 22/22 run once on its own under ASan and UBSan (detect_leaks, halt_on_error). The committed regression files hold the seeds proptest saved while mutants failed; all pass on the port. Mutation check (not committed): 49 mutants, 41 in the core and 8 in the shims, each run against Tier 1 (30 tests) and Tier 3 (13). 45 fail; the 4 that survive are equivalent (a size_t product that cannot wrap on a 64-bit host saturating instead, the one-copy size taken from the other of two equal line sizes, twice, and a scale type outside the enum read as bicubic, which is what outside the enum means). Tier 1 does not reach the shims; Tier 3 kills every shim mutant. A resolution_close() done as a 32-bit long is killed by Tier 3 only (the oracle is LP64 here). Refs obs-rust#2118
rust/README.md gets the obs-core crate row and a port row for libobs/obs-video-copy-plan.c.h: the five hidden functions, what obs-video.c.h keeps (every memcpy(), the frame lock, video_output_get_info(), the effect pointers, the mix walk, obs-rust#581's draw_mix_texture()), the C's wrapping arithmetic kept, the MSVC-only resolution_close() note, why there is no _rust variant, and what Tier 2 and Tier 3 cover. The crate's description and lib.rs list the module; video_copy_plan.rs and the shims carry the intentional difference: with a 32-bit long (MSVC) the C's resolution_close() overflows for sizes of 2^31 and up, where the port answers as i64; no texture or video size comes near. port_comparison gets its own bench file, port_comparison/video_copy_plan.rs: 1024 copy plans over the GPU formats and BGRA at 1080p, 720p and 4K, tight and padded, NV12 and P010 packed now and then; 1024 scale effect choices; 1024 RGBX plans with reuse matches. Each side folds its results into a checksum both must match. Fixes obs-rust#2118
This was referenced Oct 24, 2026
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
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
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
* libobs: Move the per-frame video output step decisions into obs-video-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: Characterize the per-frame video output step decisions 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 * obs-core: Add Tier 1 tests for the video output step port 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 * obs-core: Port the per-frame video output step decisions 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 * obs-core: Check the video output step port against the C 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 * docs: Record the video output step port - 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
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 #2118
What and why
Ports the output frame copy plan and the scale effect choice of
libobs/obs-video.c.htorust/obs-core, as a new modulevideo_copy_plan. It sits next tovideo_plan(#1714), which sizes the conversion textures that this plan copies from. The two stay separate.These decisions are what
output_video_data()copies into every output frame and which effectrender_output_texture()scales with:set_gpu_converted_plane()andset_gpu_converted_data(): per format, each plane's row size, rows and strides. One copy is made when the row size equals both line sizes. The UV plane of a packed NV12 or P010 frame follows plane 0;copy_rgbx_frame();get_scale_effect_internal(),resolution_close()andget_scale_effect();can_reuse_mix_texture().The plan drives
memcpy()into output frames, so the port only computes numbers. It keeps the C's arithmetic wrap for wrap:u32row sizes (width * 2andwidth * 4wrap), andusizeproducts where the C hassize_t. Everymemcpy(), the frame lock and unlock,video_output_get_info(), the effect pointers and the walk of the mix list stay in C.Six commits:
Move (behaviour-neutral): the decisions go to
libobs/obs-video-copy-plan.c.has five functions on plain values. They are declared in the newobs-video-copy-plan.hwithoutEXPORT, so they are hidden:obs_video_gpu_copy_plan(format, width, height, in_linesize, out_linesize)returns up to three copies. Each copy hassrc_after_plane0,whole,bytes,rows,src_strideanddst_stride. Copyiwrites output planeiand reads input planei, or plane 0's continuation. The switch moved as it was;obs_video_rgbx_copy_plan();obs_video_scale_resolution_close(), still inlong;obs_video_scale_effect_choice(&input)returns an enum. Which effects loaded comes in as a bit mask;obs_video_mix_reuse_match(&other, &mix).set_gpu_converted_data()now gets the plan and runs the copies.set_gpu_converted_plane()runs one copy and returns where its source ended, as before.scale_effect_pointer()maps the enum toobs->video's effect pointers.can_reuse_mix_texture()keeps the walk and theother == mixstop. The runner bounds the copy index byOBS_VIDEO_COPY_MAX, and_Static_asserts check that againstNUM_CHANNELSand the copied scale types againstobs.h.What the C now reads that it did not read before. None of these reads changes a result:
get_scale_effect()reads the five effect pointers up front to build the mask.video_output_get_info()is still called only when the target is not close to the base size;can_reuse_mix_texture()reads all five compared fields of each earlier mix, and this mix'stexture_rendered, which the match ignores.This was checked against the original code. A scratch harness (not committed) compiles origin/master's functions verbatim against the moved ones and the new callers, on stand-in mixes and frames:
u32sizes and line sizes make the samememcpy()calls (destination, source, size, in order), withmemcpyrecorded;video_output_get_info()calls;resolution_close()agrees on those same inputs;Six deliberately broken moves all fail the harness.
git log -Lon each moved function shows only upstream history. The fork's libobs: Copy a reused canvas texture without blending #581 changeddraw_mix_texture(): the reused canvas is drawn without blending. That function stays inobs-video.c.hunchanged, and the match it depends on moved as it was.Characterization:
test_video_copy_plan_steps, 22 cases. It compiles the moved file (ASan and UBSan) and calls only the five functions through their header. It pins:video_format, and two values outside the enum (no copy);linesize[1] == 0). P216 withlinesize[1] == 0is not packed;0xffffffff;resolution_close()at ±15, ±16 and ±17 on each axis, and at the ends of the sizesobs_reset_video()allows;The test is in the core suite of the test graph. It links libm except on MSVC.
RED: Tier 1 in the crate against
todo!(): the 22 C cases ported 1:1, and 8 new edge cases.GREEN: the safe core and five
#[no_mangle]shims, in a newlibobs.ported-video-copy-plangroup. Its PCH isstdlib.hand the new header. This commit updates the three export lists,source-sets.json, the unity lists andci/test_ported_sources.py(every host), each in sorted place.Tier 3: a C oracle of the unchanged file, parity, the regression files, and the mutation check.
Docs: the README rows, the crate's description,
lib.rs, and aport_comparisonbench file.The core denies clippy's arithmetic, indexing, unwrap, expect, panic, todo, unimplemented and unreachable lints. It keeps no state and takes no lock. Every
video_formatis matched by name, so a new format must be placed.resolution_close()andlong: the C subtracts inlong, which is 32-bit on MSVC. The port usesu32::abs_diff, which is|(i64)a - (i64)b|for everyu32pair.long, the two agree for every size below 2^31. A scratch check (not committed) emulated MSVC'slong(the conversion wrapping, overflow detected). It compared all 1,073,676,289 pairs of sizes from 2 to 32768 per axis, every sizeobs_video_size_valid()allows, and all pairs from 0 to 65536: 0 differ. Tier 1 repeats the check for every valid size against every size near it.obs_video_size_valid()gates onlyobs_reset_video(). Canvases, views and encoder-only mixes are bounded by texture creation instead, which is still far below 2^31.Kept, characterized, not endorsed:
width * 2andwidth * 4wrap inuint32_t. For example, P416's plane 1 of a0x40000000-wide frame is 0 bytes.linesize * height, even when the line size is underwidth * 4.Intentional differences: one, on MSVC only and unreachable. For sizes of 2^31 and up, the C's
resolution_close()turns the size negative, and its subtraction can overflow, which is undefined. The port answers as ani64subtraction does. Tier 3 compares the oracle there only on hosts with a 64-bitlong, and still compares the shim with the core everywhere.No
_rustvariant.test_video_copy_plan_steps_rustwas wired againstlibobs_rust-staticand fails to link with 404 undefined references. The shims share their archive member (obs_corecgu.2) with the context names, the module locale and source rules, the audio mix and the source profiler, which need libobs (blog,bmalloc, the graphics timers,obs_queue_task). The target was removed, and itstests.dfile says why. 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, with identical sets, unchanged from master. This was checked on74c378953and again on88cd87020. The five functions are local (t) in all three. In the hybrid they come from the Rust archive: no C object of the group is in the hybrid libobs.Performance (
port_comparison, x86_64 Linux, GCC, not pinned, a heavily loaded host with load average 19; median per 1024 calls):gpu_copy_planscale_effectrgbx_and_reuseThese run a few times per frame per mix at 60 fps, so they cost microseconds a second. The full
port_comparisonrun stops earlier at thebmem/countfixture check, afterservice 'bench_live'fails to be created. obs-output: Port the output video size, conversion and capture checks #2117 saw the same; it is not caused by this bench. These numbers come from a run with only this file's benchmarks enabled. That edit was not committed.No user-visible change, so no
docs/features.mdline.RED → GREEN
c5af8d739(6c38e8c3abefore the last rebases).soldr cargo test -p obs-data --test obs-tests-core -- obs_core::video_copy_plan::fails 29 of 30, every failure "not yet implemented".steps_constants, which reads only the constants, passes. The run was on the same commit before the rebases, which applied without conflicts.7f5584fe5(023aee168before the last rebases). Tier 1 passes 30/30; with Tier 3, 43/43.Xvfb -noresetwithdbus-run-session, a fresh HOME,ulimit -c 0and no TMPDIR. The loader was checked to load each lane'slibobs.so.30.ninja libobs-c-referencewas run explicitly, and its mtime was checked to be after the hybrid's.41b845325, on6bae29730. The table's focused runs are on88cd87020(on94c6b6491), on all three lanes, and were re-run (all passing, plustest_graphics_ffmpeg_host) after rebasing onto7b39b1e95(libobs: image loader's WIC fallback behind graphics-ffmpeg-host.h, portable obs_bswap16 (#872) #2125). The last rebase took in only libobs: Keep source volumes finite and non-negative #2128 (source volume), which touches none of these files. The full ctest ran on74c378953, on2288b404f. That has the same code as the final head; the last rebase took in master's next five commits (obs-output: Port the output video size, conversion and capture checks #2117, obs-source: Port the async video GPU conversion plan #2113, obs-outputs: module host behind obs-outputs-host.h, portable bit helpers and min_size (#851) #2096, libobs: Compute a scene item's transform under the parent's video lock (#2115) #2126 and test: run the screencast portal teardown test verbosely (#2099 diagnostics) #2123). obs-output: Port the output video size, conversion and capture checks #2117 and obs-source: Port the async video GPU conversion plan #2113 add their own entries to the shared lists, and neither touches these functions:test_video_output_formatstest_scaled_mix_alpha(#581)test_encoding_lag_rescaletest_video_output_plantest_video_copy_plan_stepsNo ctest test was skipped or not run on either full lane. Without
-V, the in-test skip lines of passing tests are not shown. The focused runs above were run with-Vand contain none.Tier 3 on
b5210b3f7(now7507249e0) (first run on7a6915b2d, before the rebases) (obs_core::video_copy_plan_parity, 13 tests). Every call runs the shim, the C and the safe core, and compares each copy field by field, the effect as its C value, and the close and reuse answers as bools:VIDEO_FORMAT_R10L + 1, -1,INT_MIN,INT_MAX) against all 729 patterns of the six line sizes, each the row size, off it or 0, at 14 widths including theu32wrap edges and 5 heights. That is 1,530,900 frames, with every kind of copy reached. Also the line sizes real frames have, over 14 × 14 sizes;intends, all 64 six-bit loaded masks and two with high bits, close and far targets, and outputs at the half edges (266,112 calls, each effect chosen);resolution_closearound every edge, and the reuse match with each field flipped;u32sizes and line sizes, scale inputs and reuse keys.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 of them pass on the port.
test_video_copy_plan_stepswas also run once on its own under ASan and UBSan, with leak detection and halt on error: 22/22, no reports.Mutation check (once, not committed; Tier 1 with 30 tests and Tier 3 with 13 in one run): 49 mutants, 41 in the core and 8 in the shims. 45 fail; the 4 that survive are equivalent.
width/ plane 2 rowsheightin1/ not marked after plane 0in2out1width/ chroma rowsheightheightheight / 2width * 2width * 2/width * 4saturate instead of wrapu32< 16/ either axis / one-sidedlong(as i32)<=half / either axis / a quarter / the target's size0x8u32×u32product fits a 64-bitusize)Tier 1 does not reach the shims; Tier 3 kills every shim mutant. The 32-bit
longmutant fails Tier 3 only, because the oracle has a 64-bitlonghere.Other runs:
uv run ci/local_gate.py --test-diff origin/masteron the final head88cd87020: 3333/3333 passed, 4 skipped (the existing#[ignore]tests). On74c378953: 3254/3254, 4 skipped. All 43obs_core::video_copy_plan*tests are among them in both.uv run ci/local_gate.py --lane lintand--lane cross: ok on74c378953. The first clippy run of the bench asked for a type alias andis_multiple_of; both are folded into the docs commit.88cd87020:ci/test_build_groups.py,ci/test_ported_sources.py,ci/test_oracle_standins.py,ci/test_sorted_shared_lists.py,ci/test_test_graph_sorted.py,ci/test_cmocka_rust_static_guard.py,ci/test_cmocka_windows_diagnostics.py,ci/test_naming.pyandci/test_cmocka_includes.py..cmakeformatted with gersemi 0.25.0. C was formatted with clang-format 23.1.3 and passes the lint lane's pinned 22.1.3.obs-video.c.h. obs-output: Port the output video size, conversion and capture checks #2117, obs-source: Port the async video GPU conversion plan #2113 and obs-graphics: Port the sprite and texture upload geometry #2107 meet this PR only in the sorted shared lists. Since then, obs-graphics: Port the sprite and texture upload geometry #2107, libobs: Copy the encoders' last error under a lock for the stop signal (#2110) #2111, obs-output: Port the output video size, conversion and capture checks #2117 and obs-source: Port the async video GPU conversion plan #2113 merged. The branch was rebased over them twice, without conflicts.source-sets.json,test-graph.json, the unity lists,libobs.pch.h, the three export lists,ci/test_ported_sources.py, the bench list and the oracle'slib.rswere then compared by hand against master: only this PR's lines differ.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)--test-diffpassed locally; nothing is stamped)-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)