Skip to content

obs-output: Port the encoder group reconfigure rules - #2919

Merged
vpacher merged 6 commits into
masterfrom
port/2913-encoder-group-reconfig
Oct 29, 2026
Merged

vpacher merged 6 commits into
masterfrom
port/2913-encoder-group-reconfig

Conversation

@vpacher

@vpacher vpacher commented Oct 29, 2026

Copy link
Copy Markdown
Member

Port of the encoder group reconfigure rules of libobs/obs-encoder.c.h to rust/obs-output (encoder_group_reconfig), next to encoder_lifecycle (#2512) and encoder_audio_start (#2320). When an active encoder of a group (the multitrack video output's encoders) gets new settings, every encoder of the group applies them on the same frame, so that their new keyframes line up.

Fixes #2913

Commits

  1. libobs: Move the encoder group reconfigure rules into obs-encoder-group-reconfig.c (behaviour-neutral).
  2. test: Characterize the encoder group reconfigure rules (test_encoder_group_reconfig_steps, test_encoder_group_reconfig_walk).
  3. obs-output: Add Tier 1 tests for the encoder group reconfigure port (RED: 27 tests on todo!() stubs).
  4. obs-output: Port the encoder group reconfigure rules (GREEN: obs-output::encoder_group_reconfig, the shims, group libobs.ported-encoder-group-reconfig).
  5. obs-output: Check the encoder group reconfigure port against the C (oracle, Tier 3 parity, port_comparison).
  6. docs: Record the encoder group reconfigure port.

Unit

Five functions on plain values, declared in libobs/obs-encoder-group-reconfig.h without EXPORT (hidden). They work on struct obs_encoder_group_reconfig and struct obs_encoder_reconfig, which hold the reconfigure fields of the group and the encoder. obs-encoder.c copies these fields in and out around each call under the group mutex, so the layouts of struct obs_encoder_group and struct obs_encoder do not change.

  • obs_encoder_group_reconfig_update(): the group branch of obs_encoder_update(). On the first request, or once every encoder applied the last one, it starts a new request. Otherwise, if this encoder already applied the current request, it sets reconfigure_again.
  • _new_request(): encoder_group_new_reconfigure_request().
  • _frame(): handle_encoder_group_reconfigure_request(), which runs once per video frame of a grouped encoder (receive_video() and the GPU encode thread). On the shared frame it applies the settings; the last apply starts a held request. Otherwise it takes the request and proposes the next shared frame: cur_pts + timebase_num * frame_rate_divisor, rounded up to a multiple of timebase_num * frame_rate_divisors_lcm. The group keeps the largest proposal.
  • _lcm(): gcd() and lcm() of add_connection(). The lifecycle port (obs-output: Port the encoder lifecycle decisions #2521) kept these in C.
  • _reset(): remove_connection() once the group is idle.

obs-encoder.c keeps:

  • obs_encoder_valid(), obs_data_apply(), and the context.data, info.update and active tests;
  • the group mutex and every field read and write;
  • reconfigure_requested and info.update();
  • start_timestamp and the LCM reset to 1;
  • obs_encoder_set_group() and the group create and destroy.

Characterized, not endorsed:

  • the uint32_t products and the counters wrap;
  • the request counter wraps to 0, which the next update then reads as "no request yet";
  • an empty group starts a new request on every update;
  • an idle group's first frame moves next_pts.

Intentional differences, all unreachable (undefined in C):

  • _lcm answers 0 for a 0, takes a value above INT_MAX as unsigned, and wraps a product above it;
  • a timestep of 0 leaves the pts unrounded;
  • next_pts beyond int64_t wraps;
  • a NULL struct pointer changes nothing.

Tests

  • Characterization:
    • test_encoder_group_reconfig_steps (22 cases, ASan and UBSan; every platform) compiles the moved file into itself. It covers:

      • the update request: the first request, one under way, one after this encoder applied it, one after all did, an older handled request, an empty group;
      • the counter wrap;
      • the frame step: the next shared frame at 29.97 fps, on and past the boundary; the largest wins; the recompute on the shared frame; each apply condition; the held request on the last apply and not before; the uint32_t wraps;
      • a group simulator with divisors 1, 2 and 3;
      • gcd()/lcm(), and the idle reset.

      I broke the moved C on purpose with 12 mutants: 11 are caught, and 1 is equivalent (> against >= + 1). There is no _rust variant: the obs-output archive member needs libobs's hidden seams and does not link on its own, as for test_encoder_lifecycle_steps.

    • test_encoder_group_reconfig_walk (4 cases; Linux with OpenGL under xvfb-run) runs two and three test encoders in one group on a multitrack video output, on real frames:

      • one update applies once on each encoder, all on the same pts, after the update;
      • a second update while the first is pending rides on it;
      • an update from inside the first apply makes every encoder apply a second time on a shared pts.
  • Tier 1: the 22 cases 1:1, plus 5 edge tests:
    • proptests of the next shared frame against C's expression in i128 and of lcm() against the exact LCM;
    • random groups of 1 to 4 encoders with random divisors and updates, where every round applies once on each encoder, on the same pts, a multiple of the timestep, in order;
    • any input without a panic;
    • the intentional differences.
  • Tier 3: shim, C and core on every call, comparing the answer and every field written:
    • the layout of both structs;
    • exhaustive over the update (2,500 states), the frame step (46,080 states), the next shared frame (pts, timebases, divisors and LCMs at the edges), and lcm() over every pair in 1..=256 plus the INT_MAX edges;
    • groups of 1 to 4 encoders in lockstep, C next to the shim (fixed cases and 512 random groups);
    • full-range proptests and NULL pointers.
  • Deliberate breaks of the core: 5 of 6 fail Tier 3. The sixth (a saturating LCM product) only changes inputs where C is undefined, and Tier 1's edge_intentional_differences catches it.
  • port_comparison (encoder_group_reconfig/frame/1024: three encoders at 29.97 fps with divisors 1, 2 and 3, an update every 64 frames), macOS arm64: Rust 9.33 us, C 9.08 us, about 5 ns per frame step.

Found on the way (not changed here)

With frame rate divisors above 1, a grouped encoder can fail to start. receive_video() starts a grouped encoder only on the frame whose timestamp equals the group's start_timestamp. An encoder with a divisor gets every n-th frame, counted from the first frame video-io delivers after it connects. If that count skips the start frame, the encoder waits forever. I saw it with divisors 2 and 3 on macOS with the C libobs from master, in about half of the starts. In one process every restart failed. So the walk test runs all encoders at divisor 1; the LCM rounding is covered by the steps test and Tier 3. This is upstream behaviour outside this unit. I'll file it as a separate issue.

Verification

  • Local gate, stamped (build-rust, Debian 13, the pinned ci-lint local-gate run): lint 75 s, rust-test 192 s, tier2-linux 314 s, cross 127 s; 6 min 35 s in all. The head carries the Local-Gate and Ci-Attestation trailers for its exact tree.
  • Linux x86_64 (tier2-linux, hybrid and C reference): 608/608 in both ctest modes, against libobs-c-reference (OFF) and the hybrid libobs (ON); IDENTICAL (1766 symbols) exported. Passing in both modes:
    • test_encoder_group_reconfig_steps and test_encoder_group_reconfig_walk (2.86 s OFF, 2.75 s ON);
    • test_encoder_lifecycle_steps, test_encoder_group_null, test_encoder_gpu_gates_steps and test_encoder_packet_ts_steps.
  • Windows 11 LTSC (VM, CI preset windows-ci-x64, MSVC; on 6a65d2299, the same unit, its C and its Rust; later commits changed only the Linux walk test, a cmake comment and the docs):
    • libobs and the tests build;
    • test_encoder_group_reconfig_steps 22/22, test_encoder_lifecycle_steps 12/12, test_encoder_packet_ts_steps 10/10;
    • obs-tests-core output::encoder_group_reconfig with MSVC (the oracle compiled by MSVC): 39/39.
  • macOS 26.7.1 arm64:
    • obs-tests-core output::encoder_group_reconfig 39/39;
    • test_encoder_group_reconfig_steps 22/22;
    • test_encoder_group_reconfig_walk 4/4 in 5 runs of 5 (about 3 s each) against a macOS libobs (C).
  • Memory and time of the new tests:
    • Rust tests, macOS arm64 debug, 10 runs: peak RSS 18 MB, longest run 0.06 s;
    • steps test, 20 runs: 16 ms on average, 1.4 MB peak;
    • walk test: about 3 s.

🤖 Generated with Claude Code

vpacher and others added 6 commits October 29, 2026 15:02
…up-reconfig.c

Behaviour-neutral extraction before the Rust port (#2913). The encoder
group reconfigure rules of obs-encoder.c move to
libobs/obs-encoder-group-reconfig.c.h, as five functions on plain values
declared in the new libobs/obs-encoder-group-reconfig.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):

- obs_encoder_group_reconfig_update(group, num_encoders,
  last_handled_reconfigure_request): the group branch of
  obs_encoder_update(), a new request or reconfigure_again;
- obs_encoder_group_reconfig_new_request(group):
  encoder_group_new_reconfigure_request();
- obs_encoder_group_reconfig_frame(group, num_encoders,
  num_encoders_started, frame_rate_divisors_lcm, encoder, cur_pts,
  timebase_num, frame_rate_divisor):
  handle_encoder_group_reconfigure_request(), true when the encoder
  applies its settings on this frame, and the group's next shared frame;
- obs_encoder_group_reconfig_lcm(lcm, frame_rate_divisor): gcd() and
  lcm() of add_connection(), in int as before;
- obs_encoder_group_reconfig_reset(group): the reconfigure fields of
  remove_connection() once the group is idle.

struct obs_encoder_group_reconfig and struct obs_encoder_reconfig carry
the reconfigure fields of the group and the encoder. obs-encoder.c copies
them in and out around each call under the group mutex, so struct
obs_encoder_group and struct obs_encoder keep their layout. The
arithmetic is C's, unchanged.

obs-encoder.c keeps obs_encoder_valid(), obs_data_apply(), the
context.data, info.update and active tests, the group mutex, every field
read and write, the reconfigure_requested store, info.update() in
do_encode() and the GPU encode thread, start_timestamp and the LCM reset
to 1, obs_encoder_set_group() and the group create and destroy.

Checked in a scratch harness (not committed): the original functions
against the new ones over 4.9 million random states and inputs without
undefined behaviour (UBSan on), 0 differences.

Refs #2913

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Characterization before the Rust port (#2913), against the C moved to
libobs/obs-encoder-group-reconfig.c:

- test_encoder_group_reconfig_steps (every platform, the moved file on
  its own): the request of obs_encoder_update() for an active grouped
  encoder (the first, one under way, one after this encoder applied it,
  one after all did, an older handled request, an empty group), the
  request counter and its wrap, the frame step (the next shared frame
  rounded up to timebase_num * LCM at 29.97 fps, on and past the
  boundary, the largest wins, the recompute on the shared frame, each
  apply condition, the held request on the last apply and not before,
  the uint32_t wraps), the group over frames with divisors 1, 2 and 3
  (one apply each on the same pts, a second update while pending, an
  update between two encoders' applies), gcd() and lcm(), and the idle
  reset. 22 cases.
- test_encoder_group_reconfig_walk (Linux with OpenGL, core video): two
  and three test encoders in one group on a multitrack video output
  encode real frames. One update applies once on each encoder, all on
  the same pts, after the update; a second update while pending rides on
  it; an update from inside the first apply makes every encoder apply a
  second time on a shared pts. 4 cases. All encoders run at divisor 1:
  with divisors 2 and 3, an encoder can miss the group's start frame
  (start_timestamp must equal a frame it gets) and never start, a libobs
  race outside this unit, seen on macOS with the C libobs.

Deliberate breaks of the moved C (12 mutants of the rounding, the
counters, the apply conditions, the held request, gcd and the reset):
11 caught by the steps test, 1 equivalent (> against >= + 1).

Refs #2913

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for #2913. obs_output::encoder_group_reconfig has its API and docs,
with todo!() bodies: update() (the group branch of obs_encoder_update()),
new_request() (encoder_group_new_reconfigure_request()), frame()
(handle_encoder_group_reconfigure_request(), with next_shared_pts(), the
group's next shared frame), lcm() (gcd() and lcm() of add_connection())
and reset() (remove_connection() once the group is idle). Group and
Encoder carry the reconfigure fields; FrameInput the other values frame()
reads.

tests/output/encoder_group_reconfig.rs:
- steps_*: test_encoder_group_reconfig_steps.c 1:1 (22 cases, same
  names, values and expectations, and its group simulator);
- edge_*: proptests of next_shared_pts() against C's expression in i128
  and of lcm() against the exact LCM, random groups of 1 to 4 encoders
  with random divisors and updates (every round applies once on each
  encoder, all on the same pts, a multiple of the timestep, after the
  first update, in order), any input without a panic, and the
  intentional differences where C is undefined (a 0 or a value above
  INT_MAX in lcm(), a timestep of 0, next_pts beyond i64).

All 27 fail on the stubs (obs-tests-core, encoder_group_reconfig).

Refs #2913

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for #2913. obs_output::encoder_group_reconfig implements the rules
of obs-encoder.c as C computes them:

- update(): the group branch of obs_encoder_update(), a new request on
  the first one or once every encoder applied the last, else
  reconfigure_again when this encoder already applied the current one;
- new_request(): the counter steps by one (wrapping) and the counts of
  the request start again;
- frame(): handle_encoder_group_reconfigure_request(), the apply on the
  shared frame (the last apply starts the held request) or the request
  taken and the next shared frame proposed, the largest kept;
- next_shared_pts(): cur_pts plus timebase_num * frame_rate_divisor,
  rounded up to a multiple of timebase_num * the LCM, the products as
  uint32_t, truncating as C's / and %;
- lcm(): Euclid, as C's gcd() for values in 1..=INT_MAX, the product's
  low 32 bits;
- reset(): every reconfigure field back to 0.

Intentional differences, unreachable (undefined in C): lcm() answers 0
for a 0 and takes a value above INT_MAX as unsigned; a timestep of 0
leaves the pts unrounded; next_pts beyond i64 wraps. The module denies
clippy::todo now too.

C ABI (ffi::encoder_group_reconfig): five shims over #[repr(C)] mirrors
of struct obs_encoder_group_reconfig and struct obs_encoder_reconfig; a
NULL struct pointer changes nothing (the frame step answers false).

libobs: obs-encoder-group-reconfig.c.h moves from libobs.core-output to
its own group libobs.ported-encoder-group-reconfig, whose precompiled
header includes only the unit's header. The hybrid libobs leaves the
group out and links the shims; the five symbols are listed in
rust-exports.map, rust-hidden.txt and rust-unexports-macos.txt and stay
hidden. ci/test_ported_sources.py lists the object. Exported symbols are
unchanged.

test_encoder_group_reconfig_steps.cmake says why there is no _rust
variant: the obs-output archive member needs libobs's hidden output and
service seams and obs-util, so it does not link on its own (as for
test_encoder_lifecycle_steps); the Tier 3 parity tests compare the shims
with the C instead.

Tier 1: 27/27 (obs-tests-core, encoder_group_reconfig). clippy clean.

Refs #2913

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 3 for #2913. obs-c-oracle builds the original
libobs/obs-encoder-group-reconfig.c.h as oracle_obs_encoder_group_reconfig_*
(oracle/encoder_group_reconfig.c), with the layout of both structs.

tests/output/encoder_group_reconfig_parity.rs runs every call on the
shim, the core and the C and compares the answer and every field
written:

- the layout of struct obs_encoder_group_reconfig and struct
  obs_encoder_reconfig against the C;
- exhaustive: the update over 2,500 states, the new request and the
  reset, the frame step over 46,080 states (counters, flags, requests,
  encoders, pts), the next shared frame over pts, timebases, divisors and
  LCMs at the common values and the integer edges, and lcm() over every
  pair in 1..=256 and the INT_MAX edges;
- groups of 1 to 4 encoders at 30 and 29.97 fps over frames and updates,
  C and shim in lockstep, every state compared after every step (fixed
  cases and 512 random groups);
- proptests over the full ranges, and NULL pointers on the shims.

The inputs where C is undefined (an LCM above INT_MAX, a 0 or a value
above INT_MAX in lcm(), a timestep of 0, next_pts beyond int64_t) go to
the shim and the core only. 12 tests.

Deliberate breaks of the core: 5 of 6 caught here (the rounding, the
largest next pts, the held request, the count wrap, the first request);
the sixth (a saturating LCM product) changes only inputs where C is
undefined, and Tier 1's edge_intentional_differences catches it. Their
shrunk cases stay in proptest-regressions.

port_comparison (encoder_group_reconfig/frame/1024: three encoders at
29.97 fps with divisors 1, 2 and 3, an update every 64 frames): Rust
9.33 us, C 9.08 us on mbmax (M-series, about 5 ns per frame step).

Refs #2913

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rust/README.md: the obs-output row names
libobs/obs-encoder-group-reconfig.c.h, and a row for the unit says what
moved, what obs-encoder.c keeps, the C behaviour kept, the intentional
differences and the tests. docs/rust-port/naming.md lists the file under
rust/obs-output.

Refs #2913

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=02e39b32abcf523b9e42d5dee5a6c98edcef6cf3 secs=394 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793282967,"gate":"rust/all/fmt","host":"linux-x86_64","key":"b9a85e6645b9210305571288b7f0aa157b74ea7dad1d33a3eb870cbb19accd43","lane":"lint","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":75,"stamp":"3c38edb9f5c938f7bf778b0273a9ed78","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"b9a85e6645b9210305571288b7f0aa157b74ea7dad1d33a3eb870cbb19accd43","lane":"lint","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":75,"stamp":"37c6cac87c297e9c0633aacf97d970bc","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"cae36882a7e550b81f9c9584fec8b7d34e5266be9ac8fbb372063ecf477812bc","lane":"rust-test","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":192,"stamp":"3259d3e70234da8fad30ca0d39f3ef11","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"95371a76f12b1aa971b48264df43f516e836a20d95fe04a7d3173a0f3313e0b3","lane":"cross","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":127,"stamp":"d1eea0a4842c01852dc4b5e2b9b55b02","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"95371a76f12b1aa971b48264df43f516e836a20d95fe04a7d3173a0f3313e0b3","lane":"cross","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":127,"stamp":"0f748bb3af743b0cafc4ed8ae4923e7a","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"95371a76f12b1aa971b48264df43f516e836a20d95fe04a7d3173a0f3313e0b3","lane":"cross","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":127,"stamp":"68efdd35b0841ed7d87e9cc6d263555a","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"95371a76f12b1aa971b48264df43f516e836a20d95fe04a7d3173a0f3313e0b3","lane":"cross","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":127,"stamp":"7f5e376e0455e04158edb3ea20d76565","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
Ci-Attestation: {"at":1793282967,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"efcd180b9e97c6909ecb19ae5d95e6d5c6ab17326cf80940a896f4937b503388","lane":"tier2-linux","parents":["b62e75a16622b717273ded766102d04c0a233e0b"],"secs":314,"stamp":"8673411dcb2abea1505cc97fa69c3e8b","tree":"02e39b32abcf523b9e42d5dee5a6c98edcef6cf3","v":1,"via":"run"}
@vpacher
vpacher requested a review from zackees as a code owner October 29, 2026 14:11
@vpacher
vpacher enabled auto-merge October 29, 2026 14:11
@vpacher

vpacher commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Handed off to the coordinator

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 encoder group reconfigure rules in obs-encoder.c to obs-output

1 participant