Skip to content

libobs: Refuse an output size that the alignment makes invalid - #1734

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/video-output-size-validation
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/video-output-size-validation

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #1713 (PR 2 of the plan: the C fix of bugs 4 and 6, which #1714 found while extracting the video output plan. The port, step 3, follows and closes the issue.)

What and why

Two fixes in libobs/obs.c.h and libobs/obs-video-plan.c.h, described on #1713. This is C only, and the port copies the fixed C. It lands before the port, so the port starts from the fixed behaviour and the tests that pin it.

  • An output width of 2 or 3 no longer destroys the video (bug 4). obs_reset_video() checked the output size (2..32768) and then, in obs_reset_video_internal(), aligned the width down to a multiple of 4 and the height to a multiple of 2. A width of 2 or 3 passed the check and was aligned to 0. By then the reset had already freed the old video, so video_output_open() failed with no video left: obs_reset_video() returned OBS_VIDEO_FAIL, obs_get_video_info() returned false, and obs_get_video() then crashed on the missing main mix (SIGSEGV in obs_get_video(), seen on master with a small driver against the real libobs). The output size is now checked as given and once aligned, by a new hidden obs_video_output_size_valid() in obs-video-plan.c.h, before anything is freed. A width of 2 or 3 now returns OBS_VIDEO_INVALID_PARAM (already a documented return value of obs_reset_video()), and the old video keeps running.
    • Every other size behaves as before. Heights of 2 and 3 align to 2 and stay valid. The alignment only lowers a size, so no size above 32768 becomes valid (32769..32771 are still refused). The base size is not aligned and keeps its plain check. test_video_output_plan checks the new function against obs_video_size_valid() over 17 x 17 edge sizes: only the widths 2 and 3 differ.
    • Who can reach it. Only plugins and scripts that call obs_reset_video() themselves. The Settings dialog has a minimum of 32, and obs-websocket SetVideoSettings requires at least 8.
  • The video mutex checks catch a failure (bug 6). obs_init_video() checked pthread_mutex_init() for task_mutex, encoder_group_mutex and mixes_mutex with < 0. pthread returns a positive error number, so a failure was never caught. It checks != 0 now, like the rest of obs.c.h. There is no test: pthread_mutex_init() with default attributes does not fail on our platforms.
  • Compatibility. No format, public API, header or exported-symbol change (1766 in each library, identical); obs_video_output_size_valid is local. A caller that passed an output width of 2 or 3 now gets OBS_VIDEO_INVALID_PARAM instead of OBS_VIDEO_FAIL and keeps its video. As before, a refused reset does not touch the caller's obs_video_info (before, the failing reset left its width aligned to 0).
  • Upstream. OBS master (95d5223b6) has the same code in libobs/obs.c: size_valid() with OBS_SIZE_MIN 2 runs before stop_video()/obs_free_video(), and obs_reset_video_internal() then applies the 0xFFFFFFFC mask; the three pthread_mutex_init(...) < 0 checks are there too. I found no user report (searched the obs-studio and obs-websocket trackers for obs_reset_video crashes, small or 2-pixel output resolutions and SetVideoSettings crashes).

Follow-ups, not in this PR.

  • Canvases. obs_canvas_create() and obs_canvas_reset_video() (obs-canvas.c.h) check neither the base nor the output size and do not align it, so they cannot hit bug 4 (a width of 2 stays 2 there), but they accept sizes that obs_reset_video() refuses (0, 1, above 32768). Adding the check is not this one-line fix: obs_canvas_reset_video_internal() clears the canvas mix before it creates the new one, obs_canvas_create() returns a canvas without a mix when the mix fails, nothing characterizes canvas sizes today, and refusing sizes there changes what the canvas API accepts. It wants its own characterization test, then a fix.
  • obs_context_data_init() checks pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0 (not the video init path; upstream has it too). Same one-character class as bug 6, left for its own change.

This deliberately changes an existing test (needs maintainer sign-off). The RED commit changes only the bug-4 assertions that #1714 pinned as "characterized, not endorsed", and adds one real-libobs case. It changes no other case:

  • test/cmocka/test_video_output_plan.c: the header paragraph "Characterized, not endorsed: obs_video_size_valid() accepts an output width of 2 or 3, and obs_video_align_output_size() then masks it to 0, ..." now says that the output size is also checked after the alignment and that obs_reset_video() refuses those widths. The case width_2_and_3_align_to_0 becomes width_2_and_3_refused_after_alignment: its existing assertions stay (the widths 2 and 3 pass obs_video_size_valid() and align to 0; 4x3 aligns to 4x2), and it now also expects obs_video_output_size_valid() to refuse 2 and 3 with heights 2 and 3 and to accept 4x2, 4x3 and 5x3. A new case, output_size_check_edges, expects obs_video_output_size_valid() to agree with obs_video_size_valid() for every pair of 17 edge sizes (0..8, 1366, 32767..32772, 65536, UINT32_MAX) except the widths 2 and 3. size_check_edges and alignment_masks are unchanged: obs_video_size_valid() and the alignment masks do not change.
  • test/cmocka/test_video_output_formats.c: the header paragraph that said this file does not drive the width-2/3 edge because the core cannot be used afterwards now describes the fixed behaviour. A new case, small_output_width_refused_before_teardown, resets to NV12 64x32, then tries output sizes 2x32, 3x32, 2x2 and 3x3 (I420), and expects each to return OBS_VIDEO_INVALID_PARAM, to leave the caller's width as given, and to leave the old video running (obs_get_video() non-null, obs_get_video_info() still NV12 64x32, and a frame arrives). A later reset to I420 64x32 must succeed and produce frames; the case then resets back to NV12 64x32.

No other test changes. The other 8 cases of test_video_output_plan and the other 3 of test_video_output_formats are unchanged and pass.

Headless (private Xvfb, private D-Bus, fresh HOME), OpenGL renderer.

Build Case Result
master + RED, libobs (C backend), hybrid libobs, libobs-c-reference test_video_output_formats 1 of 4 fails in each: small_output_width_refused_before_teardown, -1 != -3 (the 2x32 reset returns OBS_VIDEO_FAIL)
master + RED test_video_output_plan does not build: obs_video_output_size_valid is not declared
master libobs, a small driver: reset 64x32, then 2x32, then obs_get_video() the 2x32 reset returns -1, obs_get_video_info() returns false, then SIGSEGV in obs_get_video()
This branch, the same driver the 2x32 reset returns -3, obs_get_video_info() returns true, obs_get_video() returns the old video, a later 64x32 reset returns 0
This branch, the three libraries test_video_output_formats 4/4, 3 runs each
This branch, the three libraries test_video_output_plan 10/10, 3 runs each

RED → GREEN

  • RED commit: cd7ae81 test: Expect obs_reset_video() to refuse an output width that aligns to 0. On master, test_video_output_formats fails 1 of 4 against libobs with OBS_LIBOBS_BACKEND=C, and against the hybrid libobs and libobs-c-reference (HYBRID, OBS_BUILD_C_REFERENCE=ON): small_output_width_refused_before_teardown gets OBS_VIDEO_FAIL (-1) instead of OBS_VIDEO_INVALID_PARAM (-3). test_video_output_plan does not compile on master, because the function it now tests is new.
  • GREEN: 9f87669. test_video_output_formats passes 4/4 and test_video_output_plan 10/10 in both lanes and against all three libraries, 3 runs each. The exported symbols of libobs (C backend), the hybrid libobs and libobs-c-reference are identical (1766), and obs_video_output_size_valid stays local. Full ctest -j4:
Lane Library ctest
OBS_LIBOBS_BACKEND=C libobs 360/360
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 362/362
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 362/362

None failed, skipped or not run; run on their own in the same environment, test_video_output_formats ran all 4 cases rather than skipping. After the final rebase onto cf4b47b (#1726; it brings #1722, the scene group transform extraction, into libobs), I rebuilt both trees, and test_video_output_formats, test_video_output_plan and test_scene_group_transform passed against all three libraries, (4/4, 10/10 and 7/7), and the symbol comparison passed again (1766, identical).

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. clang-format 23.1 is clean on the changed files.
  • Real app: not applicable. The Settings dialog (minimum 32) and obs-websocket (minimum 8) cannot send these widths; the real-libobs test drives the only path that can, obs_reset_video() from API code.

Windows/macOS: the change is portable C in shared libobs code. test_video_output_formats is Linux only (it needs a display); test_video_output_plan builds everywhere and has no cmocka range assertions.

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 (the characterization is libobs: Move the video output conversion plan into obs-video-plan.c #1714)
  • User-visible change: its line in docs/features.md is added or updated (Fixed upstream bugs, Stability)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • 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) (not applicable: no port here)

…to 0

An output width of 2 or 3 passes the 2..32768 size check of
obs_reset_video(), and the output alignment to a multiple of 4 then makes
it 0. The reset has freed the old video by then, so it fails with no video
left, and obs_get_video() then crashes on the missing main mix. obs-rust#1714
pinned this as characterized, not endorsed.

test_video_output_plan now expects the new obs_video_output_size_valid()
to refuse the widths 2 and 3 (any height) and to agree with
obs_video_size_valid() for every other size, including the sizes just
above the maximum. The alignment assertions are unchanged.

test_video_output_formats gets a real-libobs case: a reset to a valid
size, then to an output width of 2 or 3, which must return
OBS_VIDEO_INVALID_PARAM and leave the old video running, then a later
reset to a valid size, which must work.

Refs obs-rust#1713
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 06:06
obs_reset_video() checked the output size (2..32768) before aligning the
width down to a multiple of 4 and the height to a multiple of 2. An
output width of 2 or 3 passed the check and was aligned to 0. By then the
reset had freed the old video, so video_output_open() failed with no
video left, and obs_get_video() then crashed on the missing main mix.

The output size is now checked as given and once aligned, by the new
hidden obs_video_output_size_valid() in obs-video-plan.c.h, before
anything is freed. A width of 2 or 3 now returns OBS_VIDEO_INVALID_PARAM
and the old video keeps running. Every other size is accepted or refused
exactly as before: heights of 2 and 3 align to 2 and stay valid, and the
alignment only lowers a size, so no size above the maximum becomes valid.
The base size is not aligned and keeps its check. The Settings dialog
(minimum 32) and obs-websocket SetVideoSettings (minimum 8) never send
such a width; plugins and scripts can.

obs_init_video() also checked pthread_mutex_init() for its three mutexes
with < 0. pthread returns a positive error number, so a failure was never
caught; it checks != 0 now, like the rest of obs.c.h.

Refs obs-rust#1713
@rdoupe
rdoupe force-pushed the fix/video-output-size-validation branch from 2c4d545 to 9f87669 Compare October 22, 2026 06:07
@zackees
zackees enabled auto-merge October 22, 2026 06:12
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 22, 2026
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit d3521ed Oct 22, 2026
25 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 22, 2026
…them

The Windows cmocka does not export cm_print_error(), which fail_msg()
expands to there, so a Windows-built test that calls it stops the
Windows Tier 2 build with LNK2019 before CTest runs. obs-rust#1741 converted
three of the four calls in test_video_output_plan; the one in
output_size_check_edges (added by obs-rust#1734) is still there, so the
Windows link still fails. The same pattern as obs-rust#1591 for obs-rust#1580: it is
now fprintf(stderr, ...) with the same format and arguments, a
terminating newline, and then a plain fail(), under the same condition.

test_settings_track_rows (Qt6::Widgets only, so Windows builds it too)
had three such calls. It links on Windows today, but it is the only
other Windows-built test that calls fail_msg(), and the new check holds
every such test to the same rule, so it gets the same change.

No condition, loop, case or assertion changes.

Refs obs-rust#1713
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 22, 2026
…them

The Windows cmocka does not export cm_print_error(), which fail_msg()
expands to there, so a Windows-built test that calls it stops the
Windows Tier 2 build with LNK2019 before CTest runs. obs-rust#1741 converted
three of the four calls in test_video_output_plan; the one in
output_size_check_edges (added by obs-rust#1734) is still there, so the
Windows link still fails. The same pattern as obs-rust#1591 for obs-rust#1580: it is
now fprintf(stderr, ...) with the same format and arguments, a
terminating newline, and then a plain fail(), under the same condition.

test_settings_track_rows (Qt6::Widgets only, so Windows builds it too)
had three such calls. It links on Windows today, but it is the only
other Windows-built test that calls fail_msg(), and the new check holds
every such test to the same rule, so it gets the same change.

Once it links, test_video_output_plan fails on Windows in
techniques_exist_in_the_effect ("technique Planar_Y is not in
format_conversion.effect"): the Windows checkout gives the effect file
CRLF line endings, and the "\ntechnique NAME\n" needles never match
"\r\n". The test now drops the CRs from the text it read before
searching. Converting the effect to CRLF reproduces the failure on
Linux, and the test passes with that file after the change.

No condition, loop, case or assertion changes.

Refs obs-rust#1713
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 22, 2026
…them

The Windows cmocka does not export cm_print_error(), which fail_msg()
expands to there, so a Windows-built test that calls it stops the
Windows Tier 2 build with LNK2019 before CTest runs. obs-rust#1741 converted
three of the four calls in test_video_output_plan; the one in
output_size_check_edges (added by obs-rust#1734) is still there, so the
Windows link still fails. The same pattern as obs-rust#1591 for obs-rust#1580: it is
now fprintf(stderr, ...) with the same format and arguments, a
terminating newline, and then a plain fail(), under the same condition.

test_settings_track_rows (Qt6::Widgets only, so Windows builds it too)
had three such calls. It links on Windows today, but it is the only
other Windows-built test that calls fail_msg(), and the new check holds
every such test to the same rule, so it gets the same change.

Once it links, test_video_output_plan fails on Windows in
techniques_exist_in_the_effect ("technique Planar_Y is not in
format_conversion.effect"): the Windows checkout gives the effect file
CRLF line endings, and the "\ntechnique NAME\n" needles never match
"\r\n". The test now drops the CRs from the text it read before
searching. Converting the effect to CRLF reproduces the failure on
Linux, and the test passes with that file after the change.

No condition, loop, case or assertion changes.

Refs obs-rust#1713
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 22, 2026
Ports libobs/obs-video-plan.c.h to the video_plan module of a new crate,
rust/obs-core, for the units extracted from libobs/obs.c.h. The plan
tells obs_reset_video() which sizes it takes and how it aligns the
output, and tells the mix setup, per output format, the GPU conversion
(the techniques of format_conversion.effect and their texel sizes), the
size and format of each conversion plane, and the render texture format
and color space. obs.c.h (still C) creates the textures and surfaces
from it; the hybrid libobs drops the C file and links the Rust shims.

Safe core (src/video_plan.rs): size_valid() with OBS_SIZE_MIN and
OBS_SIZE_MAX, align_output_size(), output_size_valid() (valid as given
and after the alignment, obs-rust#1734), scale_type_name(), and output_plan()
with the C's three steps: the techniques and texel sizes, the planes,
and the render textures. Technique names are 'static c"..." strings.
The texel sizes are 1.0 / size as f32, the C's 1.f / (float)size bit for
bit (infinite for a zero size, as in C). The sizes come from profiles,
obs-websocket SetVideoSettings and scripts, so the module denies
clippy's indexing_slicing, arithmetic_side_effects, unwrap_used,
expect_used, panic, todo, unimplemented and unreachable; nothing panics
for any input.

Shim (src/ffi/video_plan.rs): the five functions with the C
signatures, and #[repr(C)] struct obs_video_plane and struct
obs_video_output_plan, returned by value. The enums cross as c_ints;
a value outside its enum takes the C's default: branch. Intentional
difference: a null pointer to obs_video_align_output_size is skipped (C
dereferences it); the other pointer is still aligned.

C side: the file leaves libobs.core for its own group,
libobs.ported-video-plan (LIBOBS_PORTED_SOURCES), whose PCH is
obs-internal.h, so the C reference and the C backend keep compiling it.
The functions are declared in obs-internal.h without EXPORT, so the
shims are hidden: rust-hidden.txt, rust-exports.map and
rust-unexports-macos.txt. libobs-rust depends on obs-core and
re-exports its ffi.

Results: test_video_output_plan (10 of 10) and test_video_output_formats
(4 of 4, under a private Xvfb) pass against the C-backend libobs, the
hybrid libobs and libobs-c-reference. The exported symbol sets are
identical, 1766 in each. Tier 1: obs_core:: 18 of 18.

Refs obs-rust#1713
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 22, 2026
* libobs: Add Tier 1 tests for the video output plan port

RED for the port of libobs/obs-video-plan.c.h to a new crate,
rust/obs-core, module video_plan: the crate for the units extracted from
libobs/obs.c.h, as the claim on obs-rust#1713 proposed. The safe core is a stub
(todo!()): size_valid(), align_output_size(), output_size_valid(),
scale_type_name() and output_plan(). The C enums it takes or returns,
gs_color_format, gs_color_space and obs_scale_type, are plain data in
header order (GsColorFormat, GsColorSpace, ObsScaleType); video_format
and video_colorspace are obs-media-io's. A value outside its enum is
None, the C's default: branch.

tests/obs_core/video_plan.rs ports test_video_output_plan 1:1: its 10
cases under the same names, in the same order, with the same inputs and
the same expected values, including the written-out plan per format and
the bit-for-bit float comparison over every format x colorspace x size.
techniques_exist_in_the_effect reads libobs/data/format_conversion.effect
from the source tree, with CR removed as the C test does.

Edge cases: the enum tables in header order, the size bounds, a zero
size (infinite texel size, as 1.f / (float)0), u32::MAX sizes, technique
names as C identifiers, techniques and planes in pairs, and two
proptests over any u32 sizes: the plan against the written-out rules,
and the alignment and the output size check.

The tests are a module of the existing core category binary,
obs-tests-core; no new test binary (8 of 8).

soldr cargo test -p obs-data --test obs-tests-core obs_core::
fails 17 of 18 with "not yet implemented"; only
enum_tables_in_header_order passes.

Refs obs-rust#1713

* libobs: Port the video output plan to obs-core

Ports libobs/obs-video-plan.c.h to the video_plan module of a new crate,
rust/obs-core, for the units extracted from libobs/obs.c.h. The plan
tells obs_reset_video() which sizes it takes and how it aligns the
output, and tells the mix setup, per output format, the GPU conversion
(the techniques of format_conversion.effect and their texel sizes), the
size and format of each conversion plane, and the render texture format
and color space. obs.c.h (still C) creates the textures and surfaces
from it; the hybrid libobs drops the C file and links the Rust shims.

Safe core (src/video_plan.rs): size_valid() with OBS_SIZE_MIN and
OBS_SIZE_MAX, align_output_size(), output_size_valid() (valid as given
and after the alignment, obs-rust#1734), scale_type_name(), and output_plan()
with the C's three steps: the techniques and texel sizes, the planes,
and the render textures. Technique names are 'static c"..." strings.
The texel sizes are 1.0 / size as f32, the C's 1.f / (float)size bit for
bit (infinite for a zero size, as in C). The sizes come from profiles,
obs-websocket SetVideoSettings and scripts, so the module denies
clippy's indexing_slicing, arithmetic_side_effects, unwrap_used,
expect_used, panic, todo, unimplemented and unreachable; nothing panics
for any input.

Shim (src/ffi/video_plan.rs): the five functions with the C
signatures, and #[repr(C)] struct obs_video_plane and struct
obs_video_output_plan, returned by value. The enums cross as c_ints;
a value outside its enum takes the C's default: branch. Intentional
difference: a null pointer to obs_video_align_output_size is skipped (C
dereferences it); the other pointer is still aligned.

C side: the file leaves libobs.core for its own group,
libobs.ported-video-plan (LIBOBS_PORTED_SOURCES), whose PCH is
obs-internal.h, so the C reference and the C backend keep compiling it.
The functions are declared in obs-internal.h without EXPORT, so the
shims are hidden: rust-hidden.txt, rust-exports.map and
rust-unexports-macos.txt. libobs-rust depends on obs-core and
re-exports its ffi.

Results: test_video_output_plan (10 of 10) and test_video_output_formats
(4 of 4, under a private Xvfb) pass against the C-backend libobs, the
hybrid libobs and libobs-c-reference. The exported symbol sets are
identical, 1766 in each. Tier 1: obs_core:: 18 of 18.

Refs obs-rust#1713

* obs-c-oracle: Compare the video output plan port with the original C

The oracle, oracle/video_plan.c, is libobs/obs-video-plan.c.h,
unmodified, with its five functions renamed to oracle_*. The rename also
renames the struct tag obs_video_output_plan, in obs-internal.h and the
file alike, which leaves the layout as it is. It also reports the layout
of struct obs_video_plane and struct obs_video_output_plan, the width of
the enums, and the gs_color_format, gs_color_space, obs_scale_type,
video_format and video_colorspace values.

Tier 3 (tests/obs_core/video_plan_parity.rs), in obs-tests-core:

- the layout and the enum values against the C header;
- exhaustively, every video_format and video_colorspace value, with -1,
  one and two past the end, INT_MIN and INT_MAX, at 24 sizes (the
  characterization's, 0, 32769, 65536, 2^31 - 1, 2^31, 2^32 - 2 and
  2^32 - 1) for each of width and height: 31 x 11 x 24 x 24 plans, every
  field, technique names byte for byte, floats bit for bit;
- every obs_scale_type value and values outside it;
- the size checks and the alignment at those sizes;
- proptests (4096 cases each) over any format and color space value and
  any u32 sizes, sizes up to 65536, and the size checks over any u32 and
  near the bounds.

Excluded, the one intentional difference: a null pointer to
obs_video_align_output_size, which the shim skips and C dereferences; a
shim-only test pins it. 11 of 11 pass.

Deliberate breaks, not committed, each caught: the P216 chroma plane at
half height (3 tests fail), the output size not checked again after the
alignment (the edge sweep fails), the PQ and HLG technique sets of P416
swapped (3), and the texel size from a different rounding (3).

Refs obs-rust#1713

* docs: Add the obs-core rows for the video output plan port

naming.md: libobs/obs.c.h and the units extracted from it
(obs-video-plan.c.h so far) go in rust/obs-core. rust/README.md: the
obs-core layout row and the obs-video-plan.c.h row of the ported table,
with its one intentional difference. testing-policy.md: obs-core's
tests are a module of the core category binary, obs-tests-core.

Refs obs-rust#1713
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.

2 participants