Skip to content

obs-source: Port the async video GPU conversion plan - #2113

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-conversion-plan
Oct 24, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-conversion-plan

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2062

What and why

Ports the async video GPU conversion plan of libobs/obs-source.c.h to rust/obs-source, as a new module conversion_plan, next to async_pacing (#2046), audio_input (#2021), color_space (#1990) and deinterlace_pacing. Every frame of every async source (media, cameras, capture cards, PipeWire) goes through it. It decides the convert type, the textures the frame is uploaded to (how many, their sizes and formats), the shader technique that turns them into RGBA, and whether that draw is linear. A wrong entry gives a green or garbled picture for one format, so the characterization pins the whole table, and Tier 3 compares it exhaustively. The output-side plan stays separate in rust/obs-core (video_plan, #1714), because the tables differ.

Six commits:

  1. Move (behaviour-neutral): get_convert_type(), the nineteen set_*_sizes() of init_gpu_conversion(), select_conversion_technique() and need_linear_output() go to libobs/obs-source-conversion-plan.c.h. They sit behind one function, obs_source_conversion_plan_fill(), declared in the new obs-source-conversion-plan.h without EXPORT (hidden). It takes the frame's format, range, transfer and size, and fills struct obs_source_conversion_plan { convert_type, channel_count, width[4], height[4], format[4], technique, linear }.

    • The bodies move as they were. The setters write the plan instead of the source, and the RGB limited plane still calls the convert_video_format() inline of obs-internal.h, which stays there for its other callers. Planes a setter does not write stay zero.
    • obs-source.c.h keeps set_async_texture_size() (the change check, the textures and texrenders, the graphics context), upload_raw_frame(), update_async_texrender() and its shader parameters.
    • get_convert_type(), select_conversion_technique() and need_linear_output() keep their names and signatures and read the plan.
    • init_gpu_conversion() fills the plan and copies it into async_convert_width, async_convert_height and async_texture_formats for the plan's planes, then sets async_channel_count. These are the same fields the setters wrote, in the same order.
    • The two asserts for a frame that needs no conversion stay in obs-source.c.h: CONVERT_NONE in init_gpu_conversion(), and full-range RGB in select_conversion_technique(). So the moved function never asserts, and returns "no conversion" in every build.
    • enum convert_type keeps its names, set from the header's enum obs_source_convert_type. Both files check at compile time that enum video_format and enum gs_color_format are int-sized, and that the plan's planes fit MAX_AV_PLANES.
    • Checked against the unchanged functions. A scratch harness (not committed) compiles origin/master's functions verbatim on a real struct obs_source, next to the moved file. It covered every format from -3 to 40, both ranges, every transfer 0 to 255, and 22 widths × 22 heights around 0, the wraps and UINT32_MAX: 2,261,248 frames. Both give the same convert type, plane sizes and formats, channel count, technique and linear flag. Master's copy of these functions is unchanged at the final base.

    The file stays in libobs.core-source for this commit. Exports are unchanged (1766).

  2. Characterization: test_async_conversion_plan, 13 cases. It compiles the moved file (ASan and UBSan) and pins the whole table:

    • every enum video_format plus two values outside it (26 and -1);
    • both ranges;
    • every enum video_trc, plus 4 (outside it) and 258 (PQ modulo 256, because the frame's field is a uint8_t);
    • 14 sizes: 0 × 0 to 3 × 3, 1281 × 721, 1920 × 1080, 0x55555555, 0x55555556, UINT32_MAX - 5, - 4 and - 1, UINT32_MAX, and UINT32_MAX by 1 both ways.

    The two tables in the test (336 format × range × transfer rows, and the plane sizes of the 21 convert types at the 14 sizes) were generated from the moved C at this commit. To regenerate them, build the file with -DCONVERSION_PLAN_GENERATE and run it. The generator stops if two rows of one convert type disagree on a size. full_table checks all 4704 combinations, every field, with the technique compared as a string. Hand-written cases spell the rules out:

    • NV12 at 1080p and half sizes rounding up;
    • the RGB limited plane (convert_video_format()'s format, RGBA16F for PQ and HLG) and full-range RGB;
    • I420's PQ convert type next to the PQ/HLG techniques, and the transfer taken modulo 256;
    • the range techniques of Y800, BGR3 and R10L;
    • no conversion for P216, P416 and values outside the enum;
    • the five linear formats, and the alpha and 16-bit planes.
  3. RED: Tier 1 in the crate, against todo!().

  4. GREEN: the safe core and the #[no_mangle] shim, in a new libobs.ported-source-conversion-plan group with its own PCH (obs-internal.h, <string.h> and the header). The export lists, source-sets.json, the unity lists and ci/test_ported_sources.py are updated, and a _rust variant of the characterization is added (see below).

  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, and a port_comparison bench file.

Kept, characterized, not endorsed: the sizes wrap in uint32_t near UINT32_MAX:

  • (width + 1) / 2 is 0 at UINT32_MAX;
  • width * 3 (BGR3) is 2 at 0x55555556;
  • (width + 5) / 6 * 4 (v210) is 0 from UINT32_MAX - 4 on.

The port uses wrapping_add, wrapping_mul and wrapping_div for each step. No real frame is that wide. gs_texture_set_image() copies at most the texture's rows, so a wrong size gives a wrong picture, not an overrun.

  • How values cross. The format and the transfer cross as raw int, so values outside the enums take the C's default: paths (no conversion, no technique, GS_BGRX for the RGB plane). The transfer is the frame's uint8_t, so the plan takes it modulo 256 in both implementations.
  • The technique crosses as a pointer to one of the core's c"..." literals (a &'static CStr), or as NULL. Nothing is allocated or freed, as with the C's string literals. Tier 1 checks that every technique the plan can give is defined in libobs/data/format_conversion.effect.
  • Intentional difference: a null plan is left alone by the shim, where the C writes through it. libobs always passes a plan on its stack.
  • The _rust variant links the archive alone. test_async_conversion_plan_rust builds the unchanged test source against libobs_rust-static, with libm except on MSVC. The test calls only obs_source_conversion_plan_fill(), and includes only its header, graphics/graphics.h and media-io/video-io.h. None of those needs SIMDe or obsconfig.h, so it needs no libobs include directories. This variant runs the shim, including the struct layout and the technique pointers.
  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets. obs_source_conversion_plan_fill is local (t) in all three. In the hybrid it comes from liblibobs_rust.a, with the core's obs_source::conversion_plan::plan next to it, and in libobs-c-reference from obs-source-conversion-plan.c.h.
  • Performance (port_comparison, x86_64 Linux, GCC, not pinned, a loaded machine; median): 32.18 µs Rust, 20.78 µs C per 1024 plans (1.55x). That is about 11 ns more per plan: the core builds a Plan and the shim copies it out, where the C writes the caller's struct in place. obs-source.c.h asks for about six plans per frame of each async source (set_async_texture_size(), upload_raw_frame(), update_async_texrender() twice and async_texture_changed() twice), so this is about 65 ns per frame.
  • No user-visible change, so no docs/features.md line.

Observed, not changed:

  • need_linear_output() lists I010, P010, I210, I412 and YA2L. The V210 and R10L shaders also return linear light (srgb_nonlinear_to_linear() or the PQ/HLG EOTFs), but those two formats are not in the list. All seven render into an RGBA16F texrender (convert_video_format()), where the sRGB framebuffer switch changes nothing, so no picture differs. Upstream has the same list. The port keeps it as it is.
  • Only I420 has a PQ convert type (CONVERT_420_PQ). The rest of obs-source.c.h treats it exactly like CONVERT_420.

RED → GREEN

  • RED commit: 37fceb7cf. soldr cargo test -p obs-data --test obs-tests-core source::conversion_plan fails 22 of 23, each with "not yet implemented": the 13 ported steps_* cases except one, and the 10 edge_* cases, four of them proptests. The one that passes, steps_table_covers_every_input, checks the copied table's order and count and calls nothing. The same result was recorded on 7e6550a99, the same commit before the rebases.
  • GREEN: c16d46754. Tier 1 passes 23/23. Tier 2 ran on the head, 9c8cfa5d6 (based on cf56f125f), under a private Xvfb -noreset with dbus-run-session, a fresh HOME, ulimit -c 0 and TMPDIR unset. libobs-c-reference was built with ninja libobs-c-reference before each run, and is newer than the head commit.
Test C backend hybrid libobs-c-reference
test_media_restart_first_frame 4/4 4/4 4/4
test_deinterlace_2x_field_order 73/73 73/73 73/73
test_pipewire_camera_nv12 9/9 9/9 9/9
test_deinterlace_pacing_steps 23/23 23/23 23/23
test_deinterlace_pacing_steps_rust (not built: needs the Rust archive) 23/23 23/23
test_async_frame_pacing_steps 12/12 12/12 12/12
test_async_frame_pacing_steps_rust (not built) 12/12 12/12
test_async_conversion_plan 13/13 13/13 13/13
test_async_conversion_plan_rust (not built) 13/13 13/13
full ctest 426/426, 0 skipped 437/437, 0 skipped 437/437, 0 skipped

The full ctest ran on the head with ctest -j4. ctest skipped nothing. Inside the tests, three cmocka cases skip themselves on all three builds, as they do on master on this machine. Two are open_crashes_without_obs_core, in two tests. The third is test_hex_float_values_ignore_comma_locale, because no comma-decimal locale is installed here. No PipeWire or Pulse test skipped (TMPDIR unset).

Two reruns to report, both in test_deinterlace_2x_field_order (#456, the load-induced hold), with the machine at a load average of 15 to 27 on 16 cores from other builds:

  • On the C backend at the characterization commit (43155b38a, before the rebases), discard_2x_bottom_first_60i_into_60 skipped one field in the middle of the run, with a hold of 2 frames ("1 fields skipped, up to 1 in a row (allowed 0)"). The two reruns right after passed 73/73.
  • On the hybrid at the GREEN commit before the rebases (5b1371022), after_pause_yadif_2x_60i_into_60 did the same once. The three reruns right after passed 73/73.

Every later run, on all three builds, passed. The C backend compiles the same C at the move and characterization commits as before them (checked by the harness above). A wrong plan would garble every frame of the format, not skip one field. The test is unchanged.

  • Tier 3, acfa8b7da (source::conversion_plan_parity, 7 tests). Both sides fill a plan through the C ABI, starting from one prefilled with 0xA5 bytes, and every field is compared, the technique as a string:

    • the plan's layout and every enum value the port uses, against the C headers;
    • format × range × transfer, exhaustively: 47 formats (-3 to 40 and the ends of i32), both ranges and 270 transfers (-2 to 260, 511 to 514 and the ends of i32), each at 22 widths and 22 heights with the other side cycling. That is 1,116,720 plans;
    • every pair of the 22 sizes for formats 0 to 26, both ranges and transfers 0 to 4: 130,680 plans;
    • a proptest (20,000 cases) over any u32 width and height (biased to the ends), any i32 format and transfer (half of them inside their enums) and both ranges;
    • the technique is the core's static pointer, the same for the same input at any size;
    • the null plan, on the shim only.

    Tier 1 and the parity module passed 10 runs in a row with fresh seeds. No regression file came up. The only proptest-regressions files that appeared were from the todo!() stub and from the mutants, and they were deleted.

  • Mutation check (once, not committed): 38 mutants, all killed.

Mutant Tier 1 (23) Tier 3 (7)
half without the wrap (saturating) / rounds down 4 / 6 3 / 3
BGR3 width saturating / times 4 3 / 3 3 / 3
v210 + 5 saturating / * 4 before / 6 / + 6 3 / 3 / 3 3 / 3 / 3
I420 PQ convert type dropped / HLG is PQ too 3 / 2 3 / 3
RGB range inverted 4 3
P216 planned as I422 / YVYU not packed 4:2:2 3 / 2 3 / 3
NV12 chroma full height / I422 chroma half height 3 / 2 3 / 3
I420A alpha plane half size / I412 planes R8 / P010 chroma R16 3 / 1 / 2 3 / 3 / 3
Y800 two planes / packed 4:2:2 full width 2 / 4 3 / 3
RGB limited plane: HLG is SDR / RGBA as BGRA 4 / 3 3 / 3
YUY2 PQ and HLG swapped / Y800 full and limited swapped 1 / 2 3 / 3
R10L PQ full and limited swapped 2 3
full-range RGB gets RGB_Limited / P216 gets a technique 3 / 3 3 / 3
P010 SDR technique misnamed (not in the effect) 3 3
linear drops YA2L / adds V210 2 / 2 3 / 3
transfer saturated instead of modulo 256 / the high byte 4 / 7 2 / 3
convert type values I420 and I420_PQ swapped 1 4
shim: widths and heights swapped / technique always null 0 / 0 3 / 4
shim: linear always false / transfer passed as 0 0 / 0 3 / 4
shim: channel count off by one / full range ignored 0 / 0 3 / 4

Tier 1 does not reach the shim; test_async_conversion_plan_rust and Tier 3 do.

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

Other runs, on the head 9c8cfa5d6:

  • uv run ci/local_gate.py --test-diff origin/master: ok. Workspace scope, 3162 of 3162 passed; the 4 skipped are tests ignored by default. --lane lint: ok. --lane cross: ok.
  • 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_includes.py and ci/test_cmocka_windows_diagnostics.py: OK.
  • After each rebase (onto obs-source: Port the async video frame pacing #2046, obs-source: Port the transition clock and fit #2055 and later master), the unity lists, source-sets.json, test-graph.json, the three export lists and the other shared lists were diffed by hand against origin/master: each is master's list plus this PR's lines. Master's copy of the moved functions is unchanged at the final base.
  • Two fixes came from the gates and were folded into their commits. First, the characterization used fail_msg(), which Windows cmocka cannot link (ci/test_cmocka_windows_diagnostics.py); it now prints and calls fail(). Second, the moved file stored convert_video_format()'s enum in an int without a cast, which the macOS oracle build rejects under -Wsign-conversion -Werror (the enum is unsigned there); the store now has an explicit (int) cast, with no change in behaviour. The Tier 1 tables became static for clippy's large_const_arrays.

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 test-diff, lint and cross lanes 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 once more right before posting, after #2089 (the push-to-mute port) merged. The conflicts were only the PCH include list and the obs-source crate description, lib.rs docs and README row; both entries kept, in sorted order. After it, the five list checks, source:: in obs-tests-core (311 passed, 3 ignored), the lint lane and a hybrid rebuild with test_async_conversion_plan and its _rust variant (2/2) pass. The full ctest above ran before this rebase.

…on-plan.c

Behaviour-neutral extraction before the Rust port. The async video GPU
conversion plan of obs-source.c.h moves to
libobs/obs-source-conversion-plan.c.h, declared in the new
libobs/obs-source-conversion-plan.h without EXPORT (hidden, as
-fvisibility=hidden keeps it), behind one function:

- obs_source_conversion_plan_fill() takes the frame's format, range,
  transfer and size and fills struct obs_source_conversion_plan: the
  convert type (get_convert_type()), the channel count and each plane's
  width, height and texture format (the nineteen set_*_sizes() of
  init_gpu_conversion()), the technique (select_conversion_technique(),
  a string literal or NULL) and the linear flag (need_linear_output()).

The bodies move as they were: the setters write the plan instead of the
source, and the RGB limited plane still calls the convert_video_format()
inline of obs-internal.h, which stays there for its other callers. Planes
the setters do not write stay zero, as does every plane of a frame that
needs no conversion. The format and transfer cross as int: the transfer
is stored back into the frame's uint8_t (so it is taken modulo 256, as the
frame's field already is), and the format into enum video_format, so a
value outside either enum takes the same default paths.

obs-source.c.h keeps set_async_texture_size(), upload_raw_frame(),
update_async_texrender() and its shader parameters. get_convert_type(),
select_conversion_technique() and need_linear_output() keep their names
and signatures and read the plan. init_gpu_conversion() fills the plan and
copies it into async_convert_width, async_convert_height and
async_texture_formats for the plan's planes, then async_channel_count, the
same fields the setters wrote, in their order. The two asserts for a frame
that needs no conversion stay here (CONVERT_NONE in init_gpu_conversion(),
full-range RGB in select_conversion_technique()), so the moved function
returns "no conversion" instead in every build. enum convert_type keeps
its names, set from the header's enum obs_source_convert_type.

obs-source.c.h and the moved file check at compile time that enum
video_format and enum gs_color_format are int-sized, and obs-source.c.h
that the plan's 4 planes fit MAX_AV_PLANES.

Checked against the unchanged functions of origin/master (outside the
tree, not committed): every format from -3 to 40, both ranges, every
transfer 0 to 255 and 22 widths x 22 heights around 0, the half and
multiply wraps and UINT32_MAX (a stride over the heights past the first 8
transfers), 2,261,248 frames: the same convert type, plane sizes and
formats, channel count, technique and linear flag.

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

Refs obs-rust#2062
test_async_conversion_plan compiles libobs/obs-source-conversion-plan.c.h
(ASan and UBSan) and calls only obs_source_conversion_plan_fill(). It pins
the whole table: every enum video_format and two values outside it (26
and -1), both ranges, every enum video_trc, 4 (outside it) and 258 (PQ
modulo 256, as the frame's uint8_t field holds it), at 14 sizes: 0 x 0
to 3 x 3, 1281 x 721, 1920 x 1080, 0x55555555 and 0x55555556 (where
width * 3 wraps), UINT32_MAX - 5, - 4 and - 1, UINT32_MAX, and
UINT32_MAX by 1 both ways.

The two tables in the test were generated from the moved C at this
commit (build the file with -DCONVERSION_PLAN_GENERATE and run it):
- conversion_table, 336 rows: the convert type, channel count, plane
  formats, technique and linear flag of each format, range and transfer;
- size_table: the plane sizes of each of the 21 convert types at each
  size. The generator stops if two rows of one convert type disagree on
  a size, so the sizes depend on the convert type alone.

13 cases:
- full_table checks every row at every size (4704 plans), every field
  and the technique as a string; table_covers_every_input checks the
  table's order and count; unused_planes_zero, that planes past the
  channel count (and all of a frame needing no conversion) are zero
  whatever the plan held;
- hand-written cases spell the rules out: NV12 at 1080p, half sizes
  rounding up, the size wraps, the RGB limited plane (convert_video_format()'s
  format, RGBA16F for PQ and HLG) and full-range RGB, I420's PQ convert
  type and the PQ/HLG techniques, the transfer modulo 256, the range
  techniques of Y800, BGR3 and R10L, no conversion (P216, P416, values
  outside the enum), the five linear formats, and the alpha and 16-bit
  planes.

Characterized, not endorsed: the sizes wrap in uint32_t. (width + 1) / 2
is 0 at UINT32_MAX; width * 3 is 2 at 0x55555556; (width + 5) / 6 * 4 is
0 from UINT32_MAX - 4 on.

The moved function has no assert, so no row needs NDEBUG: the asserts
for a frame needing no conversion stayed in obs-source.c.h.

The test is in the native test graph in name order, with libm except on
MSVC. On the C backend it passes 13 of 13, and
test_media_restart_first_frame, test_deinterlace_2x_field_order,
test_pipewire_camera_nv12 and test_deinterlace_pacing_steps are unchanged
and pass.

Refs obs-rust#2062
Tests for the port of libobs/obs-source-conversion-plan.c.h to the
conversion_plan module of rust/obs-source, next to audio_input,
audio_timing, color_space and deinterlace_pacing. The safe core is a stub
(todo!()):

- plan(), obs_source_conversion_plan_fill() on plain values: the format
  and transfer as the C's int, the size as u32, a Plan back (the convert
  type, the channel count, four Planes of width, height and gs format,
  the technique as an Option<&'static CStr>, the linear flag);
- the pieces it is made of: convert_type() (get_convert_type()),
  planes() (the set_*_sizes()), rgb_limited_format()
  (convert_video_format() for the four RGB formats), half() ((n + 1) / 2
  in u32), technique() (select_conversion_technique()) and linear()
  (need_linear_output());
- the video_format, video_trc and gs_color_format values, and
  ConvertType, enum obs_source_convert_type in header order.

tests/source/conversion_plan.rs ports test_async_conversion_plan 1:1: its
13 cases under the same names (steps_*), in the same order, with the same
inputs and expectations, its two generated tables copied unchanged (336
format x range x transfer rows, and the plane sizes of the 21 convert
types at its 14 sizes).

Edge cases (edge_*): the convert type values; half() at its ends; plan()
is its pieces for formats -2 to 27, both ranges and every transfer byte;
a technique exactly when there is a conversion, and planes set exactly
up to the channel count; every technique is one of
libobs/data/format_conversion.effect's; convert_video_format()'s RGB
formats for every transfer byte. Proptests (4000 cases each): any value
outside enum video_format is no conversion, at any range, transfer and
size; the transfer counts modulo 256 for any i32; the sizes of every
format at any u32 size (biased to the ends) against a model in u64
arithmetic reduced modulo 2^32; the size changes only the sizes.

soldr cargo test -p obs-data --test obs-tests-core source::conversion_plan
fails 22 of 23 with "not yet implemented". The one that passes,
steps_table_covers_every_input, checks the copied table's order and
count, and calls nothing.

Refs obs-rust#2062
Ports libobs/obs-source-conversion-plan.c.h, the async video GPU
conversion plan of obs-source.c, to the conversion_plan module of
rust/obs-source. obs-source.c.h stays C: set_async_texture_size(),
upload_raw_frame(), update_async_texrender() and its shader parameters,
the convert_video_format() inline for its other callers, the two asserts
for a frame that needs no conversion, and the copy of the plan into the
source. The hybrid libobs drops the C file and links the Rust shim in its
place.

Safe core (src/conversion_plan.rs):
- convert_type(): get_convert_type(), a match over the format's int with
  the C's default (no conversion) for P216, P416 and any value outside
  the enum; I420 with PQ is its own type.
- planes(): the set_*_sizes(), one arm per convert type: the count and
  four Planes (width, height, gs format), the unused ones Plane::NONE.
  half() is (n + 1) / 2 with wrapping_add, BGR3's width wrapping_mul(3),
  v210's (n + 5) / 6 * 4 with wrapping_add, wrapping_div and
  wrapping_mul, so the sizes wrap exactly as the C's u32 arithmetic
  does. rgb_limited_format() is convert_video_format() for the four RGB
  formats that reach the RGB limited plane (RGBA16F for PQ and HLG, else
  RGBA, BGRA or BGRX).
- technique(): select_conversion_technique(), each name a c"..." literal,
  so the plan holds a &'static CStr and nothing is allocated; None for
  full-range RGB, P216, P416 and values outside the enum.
- linear(): need_linear_output().
- plan(): the transfer's low byte (the frame's uint8_t), then the four.
The module denies clippy's arithmetic, indexing, as (one allowed cast,
the enum's discriminant), unwrap, expect, panic, todo, unimplemented and
unreachable lints, keeps no state and takes no lock.

Shim (src/ffi/source_conversion_plan.rs): obs_source_conversion_plan_fill()
writes the C's struct obs_source_conversion_plan (mirrored, #[repr(C)]),
the technique as the literal's pointer or NULL. Intentional difference: a
null plan is left alone (the C writes through it; libobs passes a plan on
its stack).

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-conversion-plan (LIBOBS_PORTED_SOURCES), whose PCH
is obs-internal.h (the enums and convert_video_format()), <string.h> and
the header. libobs.pch.h keeps the header for obs-source.c. The function
is not EXPORT, so the shim is 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 function is
local in all three, and the hybrid's comes from the Rust archive.
ci/test_ported_sources.py checks the group on every host.

test_async_conversion_plan still compiles the C file into itself,
unchanged. The new test_async_conversion_plan_rust builds the same test
source against libobs_rust-static (and libm except on MSVC): the test
calls only obs_source_conversion_plan_fill() and includes its header,
graphics/graphics.h and media-io/video-io.h, none of which needs SIMDe,
so this variant runs the shim, the struct's layout and the technique
pointers included. test_media_restart_first_frame,
test_deinterlace_2x_field_order and test_pipewire_camera_nv12 see the swap
through the hybrid.

Tier 1 source::conversion_plan passes 23/23.

Refs obs-rust#2062
Oracle (oracle/source_conversion_plan.c): the original
obs-source-conversion-plan.c.h, unmodified, with its function renamed to
oracle_*, plus the plan's layout (size, alignment, the seven offsets),
the plane count and the enum and int sizes, and every enum value the port
uses (the 26 video formats, the 4 transfers, the 11 gs formats and the 21
convert types). The plan is mirrored on the Rust side.

Tier 3 (tests/source/conversion_plan_parity.rs, 7 tests): both sides fill
a plan through the C ABI from one prefilled with 0xA5 bytes, and every
field is compared, the technique as a string (both null or the same
bytes):
- the layout and the values against the C headers;
- format x range x transfer exhaustively: 47 formats (-3 to 40 and the
  ends of i32), both ranges and 270 transfers (-2 to 260, 511 to 514 and
  the ends of i32: every uint8_t value and values that wrap onto each enum
  value), each at 22 widths and 22 heights (around 0, the half and
  multiply wraps and UINT32_MAX), the other side cycling: 1,116,720 plans;
- every pair of the 22 sizes for formats 0 to 26, both ranges and
  transfers 0 to 4: 130,680 plans;
- a proptest (20,000 cases) over any u32 width and height (biased to the
  ends), any i32 format and transfer (half in their enums) and both
  ranges;
- the technique is static: the core's pointer, the same for the same
  input at any size;
- the intentional difference on the shim only: a null plan is left alone.

Tier 1 and the parity module passed 10 runs in a row with fresh seeds. No
regression file came up.

Mutation check (once, not committed): 38 mutants, Tier 1 (23 tests) and
Tier 3 (7), failures per mutant. All are killed.

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| half without the wrap (saturating) / rounds down | 4 / 6 | 3 / 3 |
| BGR3 width saturating / times 4 | 3 / 3 | 3 / 3 |
| v210 + 5 saturating / * 4 before / 6 / + 6 | 3 / 3 / 3 | 3 / 3 / 3 |
| I420 PQ convert type dropped / HLG is PQ too | 3 / 2 | 3 / 3 |
| RGB range inverted | 4 | 3 |
| P216 planned as I422 / YVYU not packed 4:2:2 | 3 / 2 | 3 / 3 |
| NV12 chroma full height / I422 chroma half height | 3 / 2 | 3 / 3 |
| I420A alpha plane half size / I412 planes R8 / P010 chroma R16 | 3 / 1 / 2 | 3 / 3 / 3 |
| Y800 two planes / packed 4:2:2 full width | 2 / 4 | 3 / 3 |
| RGB limited: HLG is SDR / RGBA as BGRA | 4 / 3 | 3 / 3 |
| YUY2 PQ and HLG swapped / Y800 full and limited swapped | 1 / 2 | 3 / 3 |
| R10L PQ full and limited swapped | 2 | 3 |
| full-range RGB gets RGB_Limited / P216 gets a technique | 3 / 3 | 3 / 3 |
| P010 SDR technique misnamed | 3 | 3 |
| linear drops YA2L / adds V210 | 2 / 2 | 3 / 3 |
| transfer saturates instead of wrapping / high byte | 4 / 7 | 2 / 3 |
| convert type values I420 and I420_PQ swapped | 1 | 4 |
| shim: widths and heights swapped / technique always null | 0 / 0 | 3 / 4 |
| shim: linear always false / transfer passed as 0 | 0 / 0 | 3 / 4 |
| shim: channel count off by one / full range ignored | 0 / 0 | 3 / 4 |

Tier 1 does not reach the shim; test_async_conversion_plan_rust does.

Refs obs-rust#2062
The rust/README.md crate table now lists
libobs/obs-source-conversion-plan.c.h among the rust/obs-source units, and
its port table has a row for it: the one function and what it fills (the
convert type, the planes, the technique and the linear flag), what stays
C (set_async_texture_size(), upload_raw_frame(),
update_async_texrender() and its shader parameters, the
convert_video_format() inline, the asserts and the copy into the
source), the format and transfer as int (values outside the enums take
the default paths, the transfer modulo 256), the technique as a static
C string or null, the size wraps it keeps, the intentional difference (a
null plan is left alone), the _rust variant of the characterization, the
real-libobs tests and what Tier 3 compares. The crate's description and
lib.rs say what it holds. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_conversion_plan.rs: 1024 plans (every
video format with both ranges and the four transfers, at 1920 x 1080,
1281 x 721 and 3840 x 2160), every field folded into a checksum both
sides must match, the technique by its bytes. On x86_64 Linux (GCC, not
pinned, a loaded machine; median): 32.18 us Rust and 20.78 us C per 1024
plans (1.55x), about 11 ns more a plan, as the core builds a Plan and the
shim copies it out. obs-source.c.h asks for about six plans a frame of
each async source (set_async_texture_size(), upload_raw_frame(),
update_async_texrender() twice and async_texture_changed() twice), so
about 65 ns a frame.

Fixes obs-rust#2062
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 async video GPU conversion plan in obs-source.c to obs-source

2 participants