Skip to content

libobs: Refuse upside-down encoder ROIs and NaN priorities - #1964

Merged
zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:fix/encoder-roi-validation
Oct 23, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:fix/encoder-roi-validation

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1959

What and why

obs_encoder_add_roi() (libobs/obs-encoder.c.h) is meant to refuse an encoder region of interest (ROI) smaller than one 16 × 16 block, or with a priority outside [-1, 1]. Two inputs got past both checks:

  • An upside-down rectangle (bottom above top, or right left of left). The size check subtracts in uint32_t, so it wrapped to a huge size and passed. The encoder then got a rectangle with bottom < top.
  • A NaN priority. NaN < -1.0f and NaN > 1.0f are both false.

obs_encoder_enum_roi() scaled each edge with (uint32_t)((float)edge * scale). In C that cast is undefined when the product is 2^32 or more: a large ROI on an upscaled encoder. On x86_64 with GCC it gives 0.

Only plugins and scripts call these functions. Upstream has the same code. This is a C move plus the fix. The Rust port of the helpers is a separate, later PR (unit 6 of the port plan).

  • Move (libobs: Move the encoder ROI checks and scaling out, no behaviour change):
    • The checks obs_encoder_add_roi() makes before it stores an ROI (the OBS_ENCODER_CAP_ROI cap, the 16 × 16 minimum, the priority range) move verbatim into obs_encoder_roi_admit().
    • The scaling in obs_encoder_enum_roi() moves into obs_encoder_roi_scale_factors() (scaled size / video size; false when the video has no size, so nothing is passed on) and obs_encoder_roi_scale() (the scaled rectangle).
    • They are in libobs/obs-encoder-roi.c.h and are declared in obs-encoder-roi.h without EXPORT. nm shows them as local (t). The file is in the encoder's existing unity group, libobs.core-output. libobs.pch.h includes the header, and source-sets.json lists it.
    • These stay in obs-encoder.c.h: the NULL roi check, roi_mutex, the darray, roi_increment, the reverse walk and enum_proc.
    • The docs/rust-port/naming.md row for rust/obs-output names obs-encoder-roi.c.h.
  • Characterization (test: Characterize the encoder ROI checks and scaling; test_encoder_roi, 10 cases, core suite). It registers two test video encoders, one with OBS_ENCODER_CAP_ROI and one without, and goes through the exported functions only:
    • a NULL roi, and an encoder without the cap, are refused;
    • the minimum at 15, 16 and 17 in each direction, and at the top of the uint32_t range;
    • priorities of -1, 0, -0, 1, the floats just outside [-1, 1], and ±inf;
    • roi_increment after adds, refused adds and clears (clearing an empty list changes nothing);
    • the enumeration order, last added first, without a scaled size and with one (× 2/3, and × 2 by × 1/2: each edge is multiplied in float and truncated);
    • a scaled size with no video size, which passes nothing on.
    • Characterized, not endorsed: a NaN priority and an upside-down rectangle are stored.
    • The scaled cases give the encoder a video output of its own with video_output_open(), so no graphics are needed. The test skips without a display, like the other core tests that call obs_startup().
    • It is registered in test/cmocka/build/test-graph.json, in the core suite (that suite's size is not pinned in rust/obs-build/tests/native.rs). It uses no assert_in_range, so it builds with cmocka 1.x and 2.x.
    • It passes 10/10 against the code before the move as well (origin/master's obs-encoder.c.h, same test).
  • RED (test: Expect upside-down encoder ROIs and NaN priorities to be refused). See the paragraph below. It also adds two cases:
    • test_enum_scaled_saturates: an encoder scaled × 256 from a 16 × 16 video. Edges of 2^24 - 1 scale to 2^32 - 256, which is in range. Edges of 2^24 and up scale to 2^32 or more, and must be UINT32_MAX.
    • test_null_encoder: the five ROI functions with a NULL encoder.
  • Fix (libobs: Refuse upside-down encoder ROIs and NaN priorities):

Test expectation change (maintainer sign-off): test_nan_priority and test_upside_down_rect were added two commits earlier in this PR as characterization. They pinned that a NaN priority and an upside-down rectangle are stored, marked "characterized, not endorsed". The RED commit flips them to refused: the header (obs-encoder.h) defines the priority as a value between -1 and 1, and the edges as distances from the top and left, so neither input is a valid ROI. The stored values only reached the encoder because of a uint32_t wrap and a NaN comparison. No ROI that is valid today changes. The RED commit also adds two upside-down cases (right one pixel left of left, and top at UINT32_MAX).

Encoder plugins that read ROIs. In this repo, nothing calls obs_encoder_add_roi() outside libobs. ROIs come only from third-party plugins and scripts. Three plugins read them, and none relies on the old acceptance:

  • obs-nvenc (nvenc.c.h roi_cb) and AMF (obs-ffmpeg/texture-amf.cpp.hpp roi_cb) loop over their own block map and skip blocks outside [top, bottom - 1] / [left, right - 1]. An upside-down ROI covered no block there, so refusing it changes nothing they draw. An edge of UINT32_MAX is clipped by the same loops.
  • obs-qsv11 (qsv_encoder_add_roi) passed the edges to the SDK as they were, so an upside-down rectangle reached the SDK.
  • All three convert the priority to an integer QP delta ((int8_t)(-51.0f * priority) and similar). That cast was undefined for NaN, and NaN is now refused before it gets there.
  • All three only read through obs_encoder_enum_roi(), obs_encoder_has_roi() and obs_encoder_get_roi_increment(), with a valid encoder.

Seen while reading the code, and not changed here (upstream behaviour): a 16 px ROI scaled down by more than 16× can come out with top == bottom. NVENC and AMF then compute bottom - 1 and right - 1 in uint32_t, so an edge of 0 wraps, and the ROI covers every block from its top or left to the end of the map. This needs an output scaled to under 1/16 of the canvas.

RED → GREEN

  • RED commit: 80093904c (test: Expect upside-down encoder ROIs and NaN priorities to be refused). Against the C libobs, the hybrid libobs and libobs-c-reference, test_encoder_roi passes 8 of 12 and fails these 4:
    • test_nan_priority and test_upside_down_rect: 1 != 0 (stored);
    • test_enum_scaled_saturates: 0 != 4294967295. The undefined cast gives 0 on x86_64 with GCC. Another platform or compiler may give another value, so the test pins the saturated value, not today's;
    • test_null_encoder: a segmentation fault in obs_encoder_add_roi(), which cmocka caught.
  • GREEN: 2cf4a1974. test_encoder_roi passes 12/12 against the C libobs, the hybrid libobs and libobs-c-reference.
  • Local runs used two builds: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON. Headless: private Xvfb with -noreset, private D-Bus (dbus-run-session), fresh HOME, ulimit -c 0, no deep TMPDIR. libobs-c-reference is not in all, so it was built by name (ninja libobs-c-reference) before each c-ref run, and its mtime was checked against the run.
  • The final head is 2cf4a1974, on 8faef9b5c. The full ctest, RED and the cross lane ran on the same series on 773faa051 (RED 0034c5cb2, GREEN a4705215e; same content). The last rebase brought build(libobs): append host public headers from os-<os>.cmake (#888) #1955 (libobs CMake public headers per OS) and ci: add timeout-minutes to every job, drop the WF-001 baseline (#962) #1958 (CI workflow timeouts). After it, these ran again on 2cf4a1974:
    • both builds;
    • the encoder tests on all three libraries (test_encoder_roi 12/12, plus test_encoder_error_stop, test_encoder_group_null, test_encoder_join_keyframe, test_encoder_fallback and test_encoder_video_reset: 6/6 tests);
    • the lint lane, --test-diff origin/master, ci/test_build_groups.py, ci/test_libobs_cmake_facade.py and the export counts.
Lane Library Full ctest test_encoder_roi Exported symbols
OBS_LIBOBS_BACKEND=C libobs 396/396 12/12 1766
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 400/400 12/12 1766, identical
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference (focused set only) 12/12 1766, identical
  • Full ctest skips: none at the ctest level. Three cmocka cases were skipped in both lanes, none of them related to this PR: test_config_hex_float_rounding's comma-locale case (no such locale is installed), and open_crashes_without_obs_core in test_audio_io and in test_video_io.
  • The move commit is checked by the characterization: 10/10 before the move (origin/master's obs-encoder.c.h) and after it.
  • uv run ci/local_gate.py --test-diff origin/master passes: test scope workspace, nextest 2861/2861 with 1 skipped.
  • uv run ci/local_gate.py --lane lint passes. --lane cross passes.
  • ci/test_build_groups.py passes.
  • Real app (UI, protocol output or logging changes): not applicable. Only plugin and script calls change, and nothing in the app adds ROIs.

Windows/macOS: not built there. The new file has no host conditional and uses only isnan and UINT32_MAX, which the core PCH already provides. The test uses C99 hex float literals and NAN/INFINITY, and no libm function.

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 (Fixed upstream bugs, Encoding, streaming and recording)
  • (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)

The checks obs_encoder_add_roi() makes before it stores an ROI (the
encoder has OBS_ENCODER_CAP_ROI, the ROI is at least one 16 x 16 block,
its priority is in [-1, 1]) move verbatim into obs_encoder_roi_admit(),
and the scaling in obs_encoder_enum_roi() into
obs_encoder_roi_scale_factors() (scaled size / video size, false when
the video has no size) and obs_encoder_roi_scale() (the scaled
rectangle). They are in obs-encoder-roi.c.h, declared in
obs-encoder-roi.h without EXPORT, and the file is in the encoder's unity
group, libobs.core-output.

The NULL check, roi_mutex, the list, roi_increment, the walk (last
added first) and the callback stay in obs-encoder.c.h. Same behavior.

The naming.md row for rust/obs-output names the new file.

Refs obs-rust#1959
test_encoder_roi registers two test video encoders, one with
OBS_ENCODER_CAP_ROI and one without, and pins through the exported
ROI functions what they do today:

- a NULL roi, and an encoder without the cap, are refused;
- the 16 x 16 minimum at 15, 16 and 17 in each direction, and at the
  top of the uint32_t range;
- priorities of -1, 0, -0, 1, the floats just outside [-1, 1] and the
  infinities;
- roi_increment after adds, refused adds and clears (a clear of an
  empty list changes nothing);
- the enumeration order, last added first, unscaled and scaled (2/3,
  and 2 x 1/2: each edge multiplied in float and truncated), and a
  scaled size with no video, which passes nothing on.

Characterized, not endorsed: a NaN priority, and an upside-down
rectangle (bottom above top, or right left of left: the uint32_t size
check wraps), are stored.

The scaled cases give the encoder a video output of its own with
video_output_open(), so no graphics are needed. The test is in the
core suite; it skips without a display. No assert_in_range, so it
builds with cmocka 1.x and 2.x.

Refs obs-rust#1959
Test expectation change: test_nan_priority and test_upside_down_rect
now expect a NaN priority and an upside-down rectangle (bottom above
top, or right left of left) to be refused. The characterization pinned
them as stored, not endorsed: the priority range check is false for
NaN, and the size check subtracts in uint32_t and wraps. Two more
upside-down cases are added, right one left of left and top at
UINT32_MAX.

New cases:
- test_enum_scaled_saturates: an encoder scaled x 256 from a 16 x 16
  video. Edges of 2^24 - 1 scale to 2^32 - 256, in range; edges of 2^24
  and up scale to 2^32 or past it, which must be UINT32_MAX. The
  (uint32_t) cast of such a float is undefined in C.
- test_null_encoder: the five ROI functions with a NULL encoder return
  false, 0 or nothing, as the other obs_encoder_* functions do.

On this commit, in all three libraries (C, hybrid and
libobs-c-reference), 8 of 12 pass. test_nan_priority and
test_upside_down_rect fail (1 != 0: stored), test_enum_scaled_saturates
fails with 0 != 4294967295 (x86_64, GCC: the undefined cast gives 0
there; another platform may give another value), and test_null_encoder
fails with a segmentation fault in obs_encoder_add_roi().

Refs obs-rust#1959
@rdoupe
rdoupe requested a review from zackees as a code owner October 23, 2026 19:01
obs_encoder_add_roi() is meant to refuse ROIs smaller than one 16 x 16
block and priorities outside [-1, 1]. Two inputs got past both checks:

- an upside-down rectangle (bottom above top, or right left of left):
  the uint32_t subtractions of the size check wrapped to huge sizes;
- a NaN priority: NaN < -1 and NaN > 1 are both false.

obs_encoder_roi_admit() now refuses bottom <= top, right <= left and
a NaN priority before the size check. The rest of the checks are as
before.

obs_encoder_enum_roi() scaled each edge with a (uint32_t) cast of the
float product, which is undefined in C from 2^32 up (a large ROI on an
upscaled encoder; on x86_64 with GCC it gave 0). Each edge now takes the
nearest uint32_t: 0 for NaN and below 0, UINT32_MAX from 2^32 up, as
obs_scene_scale_crop() does (obs-rust#1840). Every valid ROI, and every scaled
edge in range, gives the value it gave before.

The five ROI functions now check the encoder with obs_encoder_valid(),
as the other obs_encoder_* functions do, and return false, 0 or
nothing for NULL; before, they dereferenced it.

Only plugins and scripts reach these functions; upstream has the same
code. NVENC and AMF clip each ROI to their map, where an upside-down
ROI covered no block, and QSV passed it to the SDK as it was. All three
convert the priority to an integer, which was undefined for NaN.

test_encoder_roi passes 12/12 against the C libobs, the hybrid libobs
and libobs-c-reference.

Fixes obs-rust#1959
@rdoupe
rdoupe force-pushed the fix/encoder-roi-validation branch from a06aeb6 to 2cf4a19 Compare October 23, 2026 19:01
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into obs-rust:master with commit 1856d0c Oct 23, 2026
31 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 24, 2026
* libobs: Move the encoder size and info choices into obs-encoder-size.c

Behaviour-neutral extraction before the Rust port. The choices of
get_audio_info(), get_video_info(), obs_encoder_set_scaled_size(),
obs_encoder_scaling_enabled(), obs_encoder_get_width() / _height() and
obs_encoder_get_sample_rate() move to libobs/obs-encoder-size.c.h, as seven
functions on plain values declared in the new libobs/obs-encoder-size.h
without EXPORT (hidden, as -fvisibility=hidden keeps them):

- obs_encoder_size_audio_defaults(info, mix): get_audio_info()'s default
  fill, an unknown format, a 0 sample rate and unknown speakers each from
  the mix, over struct obs_encoder_audio_fill (the three fields, the enums
  as int);
- obs_encoder_size_cap_override(caps, w, h, voi_w, voi_h): the video's size
  for an encoder with OBS_ENCODER_CAP_SCALING, else the size the plugin
  left;
- obs_encoder_size_scaled_gate(type, active, initialized): the three
  refusals of obs_encoder_set_scaled_size() in their order (not a video
  encoder, active, initialized), or check;
- obs_encoder_size_scaled_match(has_voi, voi_w, voi_h, w, h): disable when
  the size equals the video's, else set;
- obs_encoder_size_scaling(w, h): either scaled axis set;
- obs_encoder_size_video_route(type, has_media, scaled): where one axis of
  obs_encoder_get_width() / _height() comes from: the wrong-type warning,
  0 without a video, the scaled size, or the video's;
- obs_encoder_size_rate_route(type, has_media, samplerate): the same for
  obs_encoder_get_sample_rate() (an audio encoder, the stored rate or the
  mix's).

obs-encoder.c.h keeps obs_encoder_valid(), every warning with the encoder's
name, video_output_get_info() and audio_output_get_info(),
video_output_get_width() / _height() and audio_output_get_sample_rate()
(called only on the media route, so they run exactly when they ran
before), the load of the active flag and every write to the encoder. The
plugin callbacks stay in C in the same order: the default fill, then the
plugin's get_audio_info / get_video_info, then the cap override. No lock
is taken or dropped.

What changes in what the C reads, not in what it does:
- obs_encoder_set_scaled_size() loads the active flag and reads
  initialized before the type is known (an atomic load and a plain read,
  no side effect);
- obs_encoder_get_width() / _height() / _sample_rate() read media and the
  scaled size or rate before the type check;
- get_video_info() reads voi->width / height without the cap (voi is
  already dereferenced for the format) and stores the size back;
- get_audio_info() reads the three mix fields up front. Both callers pass a
  zeroed info, so the original read all three too;
- obs_encoder_scaling_enabled() reads scaled_height when scaled_width is
  set.

Checked in a scratch harness (not committed) that compiles origin/master's
functions verbatim against the moved ones on a stand-in encoder, with
plugin callbacks that record what they see and change the info:
20,000,000 random calls (types 0, 1 and outside the enum, caps with and
without the scaling bit, with and without media, a root size that differs
from the video info, scaled sizes and rates biased to 0 and to the video's,
the active and initialized flags, a NULL encoder) give the same results,
the same warnings, the same media getter calls in the same order, the same
plugin inputs, the same info and the same encoder afterwards. Four planted
mutants were each caught within 300 calls.

The constants are copied into the header, so it needs no core header;
obs-encoder.c.h checks them against obs-encoder.h and audio-io.h with
_Static_assert. The new file joins libobs.core-output, just before
obs-encoder.c.h, with libobs.pch.h, which includes the new header;
source-sets.json lists it. docs/rust-port/naming.md maps it to
rust/obs-output. The exported symbol sets are unchanged (1766).

git log -L on each moved function shows only upstream history. The fork's
changes to this file (obs-rust#1109, obs-rust#1964, obs-rust#2111 and the earlier ones) are all in
code that stays C.

Refs obs-rust#2134

* test: Characterize the encoder size and info choices

test_encoder_size_steps compiles libobs/obs-encoder-size.c.h (ASan and
UBSan) and calls only its seven functions through obs-encoder-size.h, so it
runs on every platform without a display. 28 cases:

- obs_encoder_size_audio_defaults(): a zeroed info (what both callers
  pass) takes every field from the mix; the format, the rate and the
  speakers each unknown on its own; none unknown; an unknown mix field
  copied as unknown; every combination of known and unknown fields against
  a model; values outside the enums (-1, INT_MAX, INT_MIN) counting as
  known, and the rate at the u32 edges;
- obs_encoder_size_cap_override(): without the cap (0 and every other bit)
  the plugin's size is kept; with it the video's size wins, the plugin's
  size equal, different or 0; each of the 32 cap bits alone; every pair of
  eleven edge sizes (0, 1, 2, 720, 1080, 1280, 1920, 2^31 - 1, 2^31,
  UINT32_MAX - 1, UINT32_MAX) with and without the cap;
- obs_encoder_size_scaled_gate(): every refusal. Not a video encoder
  (audio, 2, 3, -1, INT_MAX, INT_MIN) first, active or initialized or not;
  an active video encoder, initialized or not; an initialized one; else
  check;
- obs_encoder_size_scaled_match(): equal sizes disable; one axis or both
  differing, swapped axes and one axis 0 set; no voi sets, the voi's size
  ignored, 0 x 0 included; every pair of edge sizes, equal, swapped and on
  one axis;
- obs_encoder_size_scaling(): 0 x 0 off, either axis on, the u32 edges;
- obs_encoder_size_video_route() and _rate_route(): the wrong type refused
  before the media is looked at, with and without media and at every edge
  size; no media reads 0, scaled or not; with media the scaled size (rate)
  when set, else the video's (mix's); and the width and height end to end,
  with and without a scaled size, a video or one scaled axis.

Characterized, not endorsed:
- obs_encoder_set_scaled_size() with one axis 0, or equal to the video's
  on one axis only, stores it as it is: obs_encoder_scaling_enabled() is
  then true, and the 0 axis reads the video's;
- with OBS_ENCODER_CAP_SCALING, a 0 x 0 video gives 0 x 0, whatever the
  plugin set;
- 0 x 0 asked of a 0 x 0 video goes through the "matches the output"
  warning.

It is in the native test graph, in the core suite (the partition test in
rust/obs-build pins no suite size), and links libm off MSVC. No
assert_in_range or fail_msg, so it builds with cmocka 1.x and 2.x. The
real-libobs tests test_encoder_video_reset (obs-rust#1109),
test_encoding_lag_rescale, test_multitrack_encoder_override,
test_encoder_fallback and test_encoder_roi (obs-rust#1964) cover the callers.

Refs obs-rust#2134

* obs-output: Add Tier 1 tests for the encoder size port

RED for the port of libobs/obs-encoder-size.c.h to the encoder_size module
of rust/obs-output, next to encoder_roi (obs-rust#1974). The safe core is a stub
(todo!()): audio_defaults(AudioFill, AudioFill) -> AudioFill,
cap_override(caps, Size, Size) -> Size, scaled_gate(type, active,
initialized) -> ScaledGate { NotVideo, Active, Initialized, Check },
scaled_match(Option<Size>, Size) -> ScaledMatch { Disable, Set },
scaling_enabled(Size) -> bool, and video_size_route() /
sample_rate_route(type, has_media, stored) -> SizeRoute { WrongType,
NoMedia, Stored, Media }, with the types, the cap and the unknown audio
values as constants.

tests/output/encoder_size.rs ports test_encoder_size_steps 1:1: its 28
cases under the same names (steps_*), in the same order, with the same
values and expectations, the characterized-not-endorsed cases included.

Edge cases (edge_*) check the core against models of the original C: every
pattern of 0 and non-0 audio fields (small, outside the enums, at the
edges) against four mixes; every cap word over the low 8 bits, with and
without every high bit, and each high bit alone; every type from -8 to 8
and the i32 extremes against both flags; every voi and size over 0, 1,
2^31 and u32::MAX with and without a voi; scaling over the edge sizes;
both routes over every type, media and edge size; and the constants.

soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_size::
fails 34 of 35 with "not yet implemented"; edge_constants, which reads only
the constants, passes.

Refs obs-rust#2134

* obs-output: Port the encoder size and info choices

Ports libobs/obs-encoder-size.c.h, the encoder size and info choices of
obs-encoder.c, to the encoder_size module of rust/obs-output.
obs-encoder.c.h stays C around it: obs_encoder_valid(), every warning with
the encoder's name, video_output_get_info(), audio_output_get_info(),
video_output_get_width() / _height(), audio_output_get_sample_rate(), the
plugin's get_audio_info and get_video_info between the default fill and
the cap override, the load of the active flag and every write to the
encoder and the infos. No lock is involved. The hybrid libobs drops the C
file and links the Rust shims in its place.

Safe core (src/encoder_size.rs):
- audio_defaults(): an unknown format, a 0 rate and unknown speakers each
  from the mix, any other value kept;
- cap_override(): the video's size with OBS_ENCODER_CAP_SCALING, else the
  plugin's;
- scaled_gate(): NotVideo, else Active, else Initialized, else Check, in
  the C's order;
- scaled_match(): Disable when a voi is there and equals the size on both
  axes, else Set;
- scaling_enabled(): either axis not 0;
- video_size_route() / sample_rate_route(): WrongType for any type but
  OBS_ENCODER_VIDEO (OBS_ENCODER_AUDIO), else NoMedia, else Stored when the
  scaled size (rate) is not 0, else Media.
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/encoder_size.rs): the seven functions keep the C
signatures. struct obs_encoder_audio_fill and struct obs_encoder_size are
mirrored #[repr(C)]; the shims read the input fills by value, return the
structs by value and the enum values as u32 (the C's enums). The type is a
c_int, as the C passes it. No intentional differences.

Kept, characterized, not endorsed (test_encoder_size_steps): a scaled size
with one axis 0, or equal to the video's on one axis only, is stored as it
is; with the cap a 0 x 0 video gives 0 x 0; 0 x 0 asked of a 0 x 0 video
takes the "matches the output" path.

C side: the file leaves libobs.core-output for its own group,
libobs.ported-encoder-size (LIBOBS_PORTED_SOURCES), whose PCH is only
obs-encoder-size.h; libobs.pch.h keeps the header for obs-encoder.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 seven
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_encoder_size_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 506 undefined references, because the shims' archive member
(obs_output cgu.1) also holds the service lifecycle, the delay and the
interleaver, which need libobs's hidden seams and obs-util (GIO, libuuid),
as for test_output_video_checks_steps (obs-rust#2117). Its tests.d file says so.

soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_size::
passes 35 of 35.

Refs obs-rust#2134

* obs-output: Check the encoder size port against the C

Oracle (oracle/encoder_size.c): the original obs-encoder-size.c.h,
unmodified, with its seven functions renamed to oracle_obs_encoder_size_*,
plus the 15 constants of the header (the types, the cap, the unknown audio
values and the enum values) and the layout of struct obs_encoder_audio_fill
and struct obs_encoder_size (sizes, alignments and every offset) and the
size of the two enums.

Tier 3 (tests/output/encoder_size_parity.rs, 15 tests). Every call runs the
shim, the C and the safe core and compares the results exactly: the audio
fill and the size field by field, the gate, match and routes as values, the
scaling check as a bool. Runs:
- the constants and the layouts against the C;
- the audio defaults over every combination of seven formats, five rates
  and seven speaker values on the info and on the mix (60,025 calls);
- the cap override over every cap word of the low 16 bits, alone and with
  every high bit, and each high bit alone, against three size sets; and
  every size over six edge values (0, 1, 2, 2^31, UINT32_MAX - 1,
  UINT32_MAX) on all four, with no cap, the cap and every cap;
- the gate exhaustively over both flags and every type from -65,536 to
  65,536, the int extremes and every 65,521st int;
- the match over every combination of the six edge values on the voi and
  the size, with and without a voi;
- the scaling check over every pair of edge values;
- both routes over a seventh of those types and -8 to 8, media or not, and
  the edge values, each route reached;
- proptests (8192 cases each) over full-range int types (biased to -2..3),
  u32 caps (biased to the scaling bit set and clear), u32 sizes and sample
  rates (biased to 0 and the top of the range, and the asked size to the
  voi's on one axis or both).

The parity module passed 10 runs in a row with fresh seeds. The regression
file holds the seeds proptest saved while the mutants below failed; all
pass on the port.

Mutation check (done once, not committed), with Tier 1 (35 tests) and
Tier 3 (15) in one run: 35 mutants, 24 in the core, 9 in the shims and 2
planted equivalents. All 33 non-equivalent mutants fail.

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| audio_defaults: format filled when known | 9 | 2 |
| audio_defaults: rate never filled | 5 | 2 |
| audio_defaults: speakers from the mix format | 5 | 2 |
| audio_defaults: rate filled from the mix only when the format is unknown | 3 | 2 |
| AUDIO_FORMAT_UNKNOWN 1 | 5 | 3 |
| cap_override: OBS_ENCODER_CAP_SCALING 1 << 4 | 3 | 4 |
| cap_override: inverted | 5 | 3 |
| cap_override: any cap overrides | 3 | 2 |
| cap_override: only the width overridden | 4 | 3 |
| scaled_gate: initialized before active | 2 | 2 |
| scaled_gate: only audio refused | 2 | 2 |
| scaled_gate: no initialized check | 2 | 2 |
| scaled_gate: active ignored | 2 | 2 |
| scaled_match: width only | 3 | 2 |
| scaled_match: no voi compares 0 x 0 | 3 | 2 |
| scaled_match: inverted | 5 | 2 |
| scaling_enabled: both axes needed | 2 | 2 |
| scaling_enabled: width only | 2 | 2 |
| route: media checked before the type | 3 | 2 |
| route: stored when 0 | 4 | 2 |
| route: no media check | 4 | 2 |
| video route: audio type | 5 | 2 |
| video route: any type above 0 is video | 2 | 2 |
| rate route: any type up to 0 is audio | 2 | 2 |
| shim: STORED and MEDIA swapped | 0 | 2 |
| shim: ACTIVE and INITIALIZED swapped | 0 | 2 |
| shim: DISABLE and SET swapped | 0 | 2 |
| shim: format and speakers swapped | 0 | 2 |
| shim: voi width and height swapped | 0 | 3 |
| shim: has_voi always true | 0 | 2 |
| shim: scaling of the width twice | 0 | 2 |
| shim: rate route through the video route | 0 | 2 |
| shim: OBS_ENCODER_SIZE_NO_MEDIA 0 | 0 | 3 |
| equivalent: scaled_match as the C (has_voi && w == && h ==) | 0 | 0 |
| equivalent: route with the stored check first under the media check | 0 | 0 |

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

Refs obs-rust#2134

* docs: Record the encoder size port

The rust/README.md crate table now lists libobs/obs-encoder-size.c.h among
the units rust/obs-output holds, and its port table has a row for it: the
seven functions and their rules, what stays C in obs-encoder.c.h (the
validity checks, the warnings, the media getters, the plugin callbacks
between the default fill and the cap override, the active flag load and
the writes to the encoder), the two mirrored structs, the "characterized,
not endorsed" behaviours kept, why test_encoder_size_steps has no _rust
variant, the real-libobs tests that see the swap, and what Tier 3
compares. No intentional differences. The crate's description and lib.rs
say what it holds. docs/rust-port/naming.md got its entry in the move
commit.

port_comparison gets its own bench file,
benches/port_comparison/encoder_size.rs: 1024 audio default fills (a
zeroed or partly set info against a 48 kHz or 44.1 kHz mix); 1024 rounds of
the cap override, the gate, the match, the scaling check and the video
route over 1080p, 720p and 1440p videos scaled to 720p, to their own size
or not; and 1024 rate routes, each folded into a checksum both sides must
match. On x86_64 Linux (GCC, not pinned, a loaded host; median per 1024
calls): audio_defaults 7.59 us Rust and 10.28 us C (0.74x),
video_override_gate_match_route 8.54 us Rust and 8.54 us C (1.00x),
rate_route 1.63 us Rust and 1.60 us C (1.02x). Each runs once per encoder
start, setting change or getter call, so neither side is on a hot path.
These numbers come from a run of this file's benches alone: a full
port_comparison run currently stops at the pre-existing bmem/count fixture
check, before it reaches them.

Fixes obs-rust#2134
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.

bug: libobs: obs_encoder_add_roi accepts upside-down rectangles and a NaN priority

2 participants