Skip to content

obs-scene: Port the scene audio mix kernels (#3079) - #3092

Merged
zackees merged 6 commits into
masterfrom
rust/3079-scene-audio-mix
Oct 30, 2026
Merged

zackees merged 6 commits into
masterfrom
rust/3079-scene-audio-mix

Conversation

@vpacher

@vpacher vpacher commented Oct 30, 2026

Copy link
Copy Markdown
Member

Fixes #3079

What

The two mix kernels of scene_audio_render() in libobs/obs-scene.c.h move to libobs/obs-scene-audio-mix.c.h and are ported to rust/obs-scene (module audio_mix). The hybrid libobs links the Rust shims in place of the C (group libobs.ported-scene-audio-mix). The kernels are two hidden functions on plain pointers and counts:

  • obs_scene_audio_mix_with_buf() (mix_audio_with_buf()): out[pos + i] += in[i] * buf[i], for an item whose visibility change falls in the tick (buf is its 0/1 gain);
  • obs_scene_audio_mix_add() (scene_mix_audio()): out[pos + i] += in[i].

scene_audio_render() keeps the item loop, audio_lock, apply_scene_item_volume(), obs_source_get_audio_mix(), the window and mix decisions and the mix and channel loops.

This is the scene half of #3007 (#3009). The source volume kernels are #3080.

Vectorization: the safe core runs loops on slices, which cannot overlap, so LLVM vectorizes it with no std::arch. On aarch64 it compiles to fmul.4s and fadd.4s per 4 samples, the same as the oracle. GCC 14 at -O2 leaves the C loops scalar, because the C pointers may alias. mix_add uses a different loop form on x86_64 and aarch64; see Numbers.

Fused multiply-add: the multiply and the add stay separate, as in the C oracle, which is built without contraction like every oracle. The shipped C on aarch64 is built with clang's default -ffp-contract=on, which fuses them into fmla, so there it can differ from the port in the last bit. Tier 3 fails if the core fuses them.

No intentional differences.

Commits

  1. Extraction 1607ce0: behaviour-neutral. A scratch harness (not committed) compared master's static functions with the moved ones on 20,000 random calls (any pos and count, random bit patterns and specials), bit for bit, with 0 differences in 7 builds:

    • clang (arm64): -O0 and -O2, with and without ASan/UBSan, and -O2 -ffp-contract=off;
    • gcc 14 (x86_64): -O0 with ASan/UBSan, -O2, and -O2 -march=x86-64-v3.
  2. Characterization ce775ce: test_scene_audio_mix_steps, 6 cases, the moved file on its own, every platform, ASan/UBSan. Every product is exact, so it holds whether or not the compiler fuses. 10 of 10 planted mutants of the C are caught. The gain-from-pos mutant first passed; a step case at pos 301 now catches it.

  3. RED 276c9d2: the core with todo!() bodies, and 9 Tier 1 tests:

    • steps_*: the cmocka cases 1:1;
    • edge_*: proptests against the C's expression, bit for bit.

    All 9 fail. GREEN adds a 10th: both loop forms of mix_add agree on every host.

  4. GREEN 46698f1: the core (with the per-target loop form of mix_add), two C ABI shims that cut the window and return at once for a count of 0, and the ported group. The symbols are in rust-exports.map, rust-hidden.txt and rust-unexports-macos.txt and stay hidden.

  5. Tier 3 and bench 6b9fb27: the oracle build of the original C, and 4 parity tests that compare the whole output buffer bit for bit, any NaN matching any NaN:

    • every triple of 14 specials;
    • over 1,000 windows of a tick;
    • whole ticks of audio;
    • proptests over any window.

    All 9 planted mutants of the Rust core are caught by Tier 1 and by Tier 3, including two that fuse the multiply-add. The commit also adds the port_comparison cases audio_mix/scene_mix_*.

  6. Docs 9eb6f90: the rust/README.md rows. naming.md already maps obs-scene*.c.h. No docs/features.md line: no user-visible change.

Numbers

port_comparison audio_mix/scene_mix_*: one channel of an item into one channel of a scene mix, for a whole tick and for the 724 frames from frame 300. Rust/C below 1: the Rust is faster.

Against the C as it ships (libobs RelWithDebInfo; the Rust staticlib is Cargo release, opt-level = 3):

Machine, compiler of the shipped C Case Rust C Rust/C
Ryzen 9 5950X, Linux, GCC 14 -O2 (scalar) with_buf/whole_tick 106.3 ns 353.3 ns 0.30
with_buf/from_frame_300 76.5 ns 251.3 ns 0.30
add/whole_tick 65.9 ns 237.2 ns 0.28
add/from_frame_300 44.7 ns 169.5 ns 0.26
Apple M1 Max, Apple clang -O2 (vectorized) with_buf/whole_tick 92.2 ns 94.6 ns 0.97
with_buf/from_frame_300 64.4 ns 64.6 ns 1.00
add/whole_tick 65.8 ns 65.1 ns 1.01
add/from_frame_300 47.8 ns 46.6 ns 1.03
Ryzen 9 5950X, Windows VM, MSVC /O2 (vectorized) with_buf/whole_tick 107.5 ns 94.5 ns 1.14
with_buf/from_frame_300 73.9 ns 70.8 ns 1.04
add/whole_tick 70.4 ns 67.1 ns 1.05
add/from_frame_300 46.5 ns 48.5 ns 0.96

Against GCC's vectorized C at -O3 (the oracle's level, not shipped), x86:

Case Rust C Rust/C
with_buf/whole_tick 106.3 ns 94.2 ns 1.13
with_buf/from_frame_300 76.5 ns 69.0 ns 1.11
add/whole_tick 65.9 ns 64.7 ns 1.02
add/from_frame_300 44.7 ns 47.7 ns 0.94

x86: taskset -c 27, functions and loops aligned on both sides (64 and 32 bytes), two runs within 2 % (one run of the C at 76.5 ns for add/whole_tick is an outlier). macOS and Windows: not pinned; Windows is the mean of two runs in a VM, which vary by up to 7 %.

Why mix_add has a different loop on x86_64 and aarch64. LLVM vectorizes both loop forms of the safe core, but unrolls them differently, and the faster form differs per target:

add/whole_tick x86_64 (vs GCC -O3) aarch64 (vs clang)
zip loop 76.3 ns, +18 %: two vectors per iteration, both loads before both stores 67.0 ns, level: four vectors per iteration
4-sample groups 66.0 ns, +2 %: one group stored before the next is loaded 95.2 ns, +46 %: one vector per iteration

So mix_add walks 4-sample groups on x86_64 and is a zip loop on aarch64, chosen by one cfg!(target_arch = "x86_64") constant in src/platform/mod.rs (GROUPED_AUDIO_LOOPS, the crate's platform boundary). Both forms are #[doc(hidden)] pub, and Tier 1 checks on every host that they give the same bits. mix_with_buf is a zip loop everywhere: in 4-sample groups LLVM vectorizes across the groups with gathers (385 ns), and the other forms tried (destructured groups, groups of 8, other zip orders, products first) were slower still; it stays 11–13 % behind GCC's -O3 C on x86 and level with clang's C on aarch64. Buffer placement (4K aliasing), the bench harness's order and code alignment were ruled out first.

Tests

  • macOS (aarch64): scene::audio_mix Tier 1 and Tier 3, 14/14. The cmocka test passes 6/6 at -O1 with ASan/UBSan, at -O2, and at -O2 -ffp-contract=off.
  • Linux gate host, stamped head 9eb6f90: lint, rust-test, cross (Windows and macOS clippy and Dylint) and tier2-linux passed. The first run failed Tier 3 on x86 on a NaN payload where two NaNs meet (shim 0xffc00000, C 0x7fc00000); Tier 1 and Tier 3 now compare any NaN as equal there (see the Tier 3 commit).
  • Windows (MSVC 14.51, CI preset): test_scene_audio_mix_steps 6/6, scene::audio_mix Tier 1 and Tier 3 14/14 against the MSVC oracle.
  • Real app: N/A. This is a behaviour-neutral port; test_scene_audio_actions runs the real scene mix on the hybrid libobs in Tier 2.
  • Test cost: Tier 1 about 3 s and Tier 3 about 4 s (proptests over whole ticks); the cmocka case well under 0.1 s. No own threads.

Checklist

  • Base is master
  • 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
  • User-visible change: not applicable (behaviour-neutral port)
  • (Optional) Local gate passed and the pushed head is stamped
  • Warning or flag changes: not applicable
  • Intentional differences from C: none

🤖 Generated with Claude Code

vpacher and others added 6 commits October 30, 2026 18:14
Behaviour-neutral extraction before the Rust port (#3079). The two mix
kernels of scene_audio_render() in obs-scene.c move to
libobs/obs-scene-audio-mix.c.h, declared in the new
libobs/obs-scene-audio-mix.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- mix_audio_with_buf() is obs_scene_audio_mix_with_buf(out, in, buf,
  pos, count): out[pos + i] += in[i] * buf[i];
- scene_mix_audio() is obs_scene_audio_mix_add(out, in, pos, count):
  out[pos + i] += in[i].

The loops are the C's, unchanged; the inputs are const now.
scene_audio_render() keeps the item loop, audio_lock,
apply_scene_item_volume(), obs_source_get_audio_mix(), the window and
mix decisions and the mix and channel loops, in their order. The file
joins libobs.core-source, before obs-scene.c.

Checked in a scratch harness (not committed): master's two static
functions against the moved ones on 20,000 random calls (count 0 to
1024, pos anywhere in the buffer, samples random, random bit patterns
and the specials 0, -0, 1, -1, 0.5, inf, -inf, NaN, a denormal and
3.4e38; gain buffers of 0, 1 and random values), outputs compared bit
for bit: clang (arm64) -O0 and -O2, with and without ASan/UBSan, -O2
with -ffp-contract=off, and gcc 14 (x86_64) -O0 with ASan/UBSan, -O2
and -O2 -march=x86-64-v3: 0 differences in 7 builds.

Refs #3079.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Characterization before the Rust port, against the C moved to
libobs/obs-scene-audio-mix.c: test_scene_audio_mix_steps (every
platform, the moved file on its own, ASan and UBSan), 6 cases:

- the plain add at the start of the tick, inside it and at its end, for
  counts 0, 1, 3, 724 and a whole tick: only out[pos .. pos + count)
  changes, 16 guard samples on each side keep their values;
- the add through the gain buffer: gains 1, 0, 0.5, 2 and -1 at pos 0
  and 300;
- a hide mid-tick (gain 1 then 0 from the item's frame 512) at pos 0 and
  301: the gain is indexed like the input, not like the output;
- NaN, infinities and -0.0 in every operand of both kernels (inf * 0 is
  NaN, so a hidden item with an infinite sample still poisons the mix);
- a count of 0 at pos 0, 500 and 1024 writes nothing;
- a second add of the same item adds it again.

Every product is exact, so the expectations hold whether or not the
compiler fuses the multiply and the add (clang does on aarch64); checked
on macOS arm64 at -O1 with ASan/UBSan, -O2, and -O2 -ffp-contract=off.

Deliberate breaks of the moved C (10: each kernel's pos offset, the
gain dropped, the add made a store, the gain not advanced, each loop end
one short and one long, the gain indexed from pos, the add made a
subtraction, and an extra multiply-add by 1 and 0): all 10 caught. The
gain-from-pos break first passed (period-2 gains at an even pos); the
step case at pos 301 now catches it.

Refs #3079.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for #3079. obs_scene::audio_mix has its API and docs, with todo!()
bodies: mix_with_buf() (out[i] += input[i] * gain[i]) and mix_add()
(out[i] += input[i]), on slices the shim cuts to the window, the
shortest slice deciding.

tests/scene/audio_mix.rs:
- steps_*: test_scene_audio_mix_steps.c 1:1 (6 cases, same names,
  values and expectations), through the cut the shim makes
  (out[pos .. pos + count], input and gain [..count]);
- edge_*: proptests of both kernels against the C's expression, bit for
  bit with any NaN matching any NaN, on random windows of a tick with any f32 (random bit patterns,
  NaN payloads, both zeros, infinities, extremes), samples outside the
  window unchanged, and slices of different lengths.

All 9 fail on the stubs (obs-tests-core, scene::audio_mix).

Refs #3079.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for #3079. obs_scene::audio_mix implements the two mix kernels of
scene_audio_render():

- mix_with_buf(): out[i] += input[i] * gain[i], a separate multiply and
  add per sample (never fused, as the C oracle);
- mix_add(): out[i] += input[i].

Both run over slices, the shortest deciding, so the compiler vectorizes
them without std::arch (the C's pointers may alias, and GCC at -O2, as
libobs ships, leaves its loops scalar).

The loop form of mix_add() depends on the target, chosen by
platform::GROUPED_AUDIO_LOOPS (cfg!(target_arch = "x86_64")), because
LLVM unrolls the two forms differently there:
- x86_64: 4-sample groups. The plain zip loop, unrolled to two vectors
  with both loads before both stores, was 18 % slower than GCC's
  vectorized C at -O3 (on a Ryzen 9 5950X); the grouped loop is 2 %.
- aarch64: a plain zip loop, unrolled to four vectors. The grouped loop
  is one vector per iteration and was 46 % slower than clang's C (Apple
  M1 Max); the zip loop is level with it.
Both forms are #[doc(hidden)] pub (mix_add_grouped, mix_add_zipped), so
Tier 1 checks on every host that they agree bit for bit.
mix_with_buf() is a zip loop on every target: grouped, LLVM vectorizes
across the groups with gathers, 4 times slower on x86_64. The module denies clippy::todo now
too. No intentional differences.

C ABI (ffi::scene_audio_mix): two shims that cut the scene's channel to
pos .. pos + count and the item's channel and gain buffer to count, and
return at once for a count of 0.

libobs: obs-scene-audio-mix.c.h moves from libobs.core-source to its own
group libobs.ported-scene-audio-mix (registered in
build/ported-source-sets/scene-audio-mix.json), whose precompiled header
includes only the unit's header. The hybrid libobs leaves the group out
and links the shims; the two 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.

Tier 1: 9/9 (obs-tests-core, scene::audio_mix).

Refs #3079.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 3 for #3079. obs-c-oracle builds the original
libobs/obs-scene-audio-mix.c.h as oracle_obs_scene_audio_mix_*
(oracle/scene_audio_mix.c, without contraction like every oracle).

tests/scene/audio_mix_parity.rs runs both kernels on the shim, the C
and the core, on copies of one output buffer, and compares every sample
of the whole buffer bit for bit, any NaN matching any NaN:

- every triple of 14 specials (both zeros, 1, -1, 0.5, 2, the smallest
  normal, a subnormal, both largest floats, both infinities and two NaN
  payloads) as output, input and gain;
- windows at every 64th frame with counts 0 to 70 and to the tick's end,
  and the last 8 frames, on random bit patterns (over 1,000 windows);
- 32 whole ticks of audio in [-1, 1), with a 0/1 gain and a random one;
- proptests over any window of a tick with any f32.

NaN payloads are not compared: where two NaNs meet (a NaN sample and a
NaN product, or the NaN of inf * 0), IEEE 754 leaves the payload open,
the add commutes and each compiler orders its operands as it likes, and
x86 returns the first operand's NaN. The first gate run on x86_64 found
exactly that (shim 0xffc00000, C 0x7fc00000 for NaN + inf * 0); the
Tier 1 proptests treat NaN the same way.

No intentional differences. 4 tests.

Deliberate breaks of the core (9: the gain dropped, the add made a
store, a fused multiply-add twice, the operands swapped with a + 0.0,
the add made a subtraction and a + 0.0, and each loop's input or gain
shifted by one): all 9 caught here and by Tier 1. The fused ones show
that Tier 3 also guards the unfused multiply and add.

port_comparison (audio_mix/scene_mix_*: one channel of an item into one
channel of a scene mix, a whole tick and the 724 frames from frame 300),
Apple M1 Max: with_buf 93.6 / 93.0 ns and 65.5 / 71.8 ns (Rust / C),
add 67.0 / 67.1 ns and 48.8 / 48.7 ns. Apple clang vectorizes the C as
well; the Rust compiles to the same fmul.4s and fadd.4s per 4 samples
(no fmla) as the oracle.

Refs #3079.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rust/README.md: the obs-scene crate row lists obs-scene-audio-mix.c.h,
and its own row says what moved, what obs-scene.c keeps, how the core
vectorizes, why the multiply and add stay unfused (the oracle) and where
the shipped aarch64 C differs (clang fuses them by default), and what
Tier 3 covers. docs/rust-port/naming.md already maps obs-scene*.c.h to
rust/obs-scene.

No docs/features.md line: no user-visible change.

Refs #3079.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=a24f59391e59bc039788653e8b357c436d85938a secs=422 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793383473,"gate":"rust/all/fmt","host":"linux-x86_64","key":"cd937b5d8e00a369eaeaf8ccdab64dacaa55e7e7b6c16e3d26915025a1b7b55f","lane":"lint","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":80,"stamp":"59547544296e86a76adb0a6f0e24ef14","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"cd937b5d8e00a369eaeaf8ccdab64dacaa55e7e7b6c16e3d26915025a1b7b55f","lane":"lint","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":80,"stamp":"8c9dee4e2358f2b800baebecd4535ef8","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"7d0de70b52f5ecec2b58bab2616c967dcaa175c925e1f0f174e12aaf54b7e1e5","lane":"rust-test","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":202,"stamp":"91647d0acb0760557c55abd21ea64086","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"42a85ad55f31dfceb04715cbaea271f9","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"241c32ba4d42ba8c15f14a8566608cc3","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"628ac865da7d476727fc8f65ec2ee568","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"7e6e1de1c5292a690596f65110a657c81244b73bf12835f73d8bd5dcd83955e9","lane":"cross","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":140,"stamp":"bb3ba8081f99536c522706b13aaebcd7","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}
Ci-Attestation: {"at":1793383473,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"18f5e408a881971b6146005dea0b08ff8f43f92450812863a981693f2c67d34e","lane":"tier2-linux","parents":["6b9fb2729a1387bfc16cea1f91aeb39558346c3f"],"secs":247,"stamp":"daa04781f776ebd92068b0a9e570da1a","tree":"a24f59391e59bc039788653e8b357c436d85938a","v":1,"via":"run"}

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: approved. The kernels keep the C's elementwise order with no FMA, and parity is bit-exact apart from NaN payloads, which the tests deliberately treat as equal, as the PR body records. The commits show RED (276c9d2) then GREEN (46698f1). There's Tier 2 cmocka coverage (6 cases) and Tier 3 parity against the C moved unchanged into the oracle. No C tests or public headers were edited. The target_arch cfg stays in platform/mod.rs, and the tests are in the existing scene binary. The Test cost line is present, and it's a behaviour-neutral port, so no ledger line is needed.

@zackees
zackees enabled auto-merge October 30, 2026 18:41
@zackees
zackees added this pull request to the merge queue Oct 30, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this to In merge queue in obs-rust Multiview Oct 30, 2026
Merged via the queue into master with commit d3ce1c8 Oct 30, 2026
42 checks passed
@zackees
zackees deleted the rust/3079-scene-audio-mix branch October 30, 2026 19:02
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In merge queue to Done in obs-rust Multiview Oct 30, 2026
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 scene audio mix kernels

2 participants