Skip to content

obs-source: Port the source volume multiply kernels (#3080) - #3093

Merged
zackees merged 6 commits into
masterfrom
rust/3080-source-volume-mul
Oct 30, 2026
Merged

zackees merged 6 commits into
masterfrom
rust/3080-source-volume-mul

Conversation

@vpacher

@vpacher vpacher commented Oct 30, 2026

Copy link
Copy Markdown
Member

Fixes #3080

What

The two volume multiply kernels of libobs/obs-source.c.h move to libobs/obs-source-volume-mul.c.h and are ported to rust/obs-source (module volume_mul). The hybrid libobs links the Rust shims in place of the C (group libobs.ported-source-volume-mul). The kernels are two hidden functions on plain pointers and counts:

  • obs_source_volume_mul_block(): multiply_output_audio(), one volume over a whole mix. apply_audio_volume() passes it as one block of AUDIO_OUTPUT_FRAMES * channels samples from the mix's first channel; a mix's channels are consecutive in allocate_audio_output_buffer()'s one allocation.
  • obs_source_volume_mul_data(): multiply_vol_data()'s inner loop, one channel by a volume per frame, when apply_audio_actions() applies an action inside the tick.

obs-source.c.h keeps the rest, in its order:

  • the action queue and its mutex;
  • get_source_volume();
  • the vol_data fill and the memset;
  • the mix and channel loops.

Vectorization (#3007): the safe core runs loops on slices, which cannot overlap, so LLVM vectorizes them with no std::arch. In the release build on aarch64, both shims multiply 16 samples per iteration (four fmul.4s). GCC 14 at -O2, which builds the Linux release, leaves both C loops scalar. mul_data uses a different loop form on x86_64 and aarch64; see Numbers.

NaN payloads: each sample is the C's one IEEE single multiply. The one case neither side pins is a NaN sample times a NaN volume. There the C itself gives the sample's payload at -O0 and the volume's when vectorized at -O2. The tests accept any NaN there.

No intentional differences.

Commits

  1. Extraction 8f9d516: behaviour-neutral. A scratch harness (not committed) compared master's two loops with the moved functions:

    • 4,000 ticks of 1 to 8 channels and every count from 0 to 70;
    • special values, random bit patterns and ordinary values;
    • clang -O0 and -O2, with and without ASan/UBSan.

    The only differences were NaN payloads where both operands are NaN, which differ between builds of the C itself.

  2. Characterization 90ed333: test_source_volume_mul_steps, 6 cases on buffers laid out as libobs lays them out, with a canary after every count. It catches all 11 planted mutants of the C.

  3. RED b29adbf: the core with todo!() bodies and 10 Tier 1 tests:

    • steps_*: the cmocka cases 1:1;
    • edge_*: slices of different lengths, proptests against one multiply per sample, no panic on any input.

    All 10 fail. GREEN adds an 11th: both loop forms of mul_data agree on every host.

  4. GREEN efbb19f: the core (with the per-target loop form of mul_data), two C ABI shims 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 0800a21: the oracle build of the original C, and 4 parity tests comparing the shim, the C and the core bit for bit, the samples past the count included:

    • every count to 70 and the tick of 1 to 8 channels;
    • 21 special bit patterns as samples and as volumes;
    • random bit patterns;
    • a proptest.

    It also adds the port_comparison bench audio_mix/source_volume_*. All 14 planted mutants of the core are caught by Tier 1 and by Tier 3.

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

Numbers

port_comparison, one mix of one tick. 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) source_volume_block/stereo 243.4 ns 947.1 ns 0.26
source_volume_block/8_channels 942.9 ns 3726.8 ns 0.25
source_volume_data/stereo 279.6 ns 966.1 ns 0.29
Apple M1 Max, Apple clang -O2 (vectorized) source_volume_block/stereo 250 ns 256–395 ns (noisy) ≤ 1
source_volume_block/8_channels 893 ns 892 ns 1.00
source_volume_data/stereo 296.4 ns 302.0 ns 0.98
Ryzen 9 5950X, Windows VM, MSVC /O2 (vectorized) source_volume_block/stereo 293.8 ns 272.9 ns 1.08
source_volume_block/8_channels 1059 ns 1027 ns 1.03
source_volume_data/stereo 323.3 ns 306.2 ns 1.06

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

Case Rust C Rust/C
source_volume_block/stereo 243.4 ns 256.3 ns 0.95
source_volume_block/8_channels 942.9 ns 955.9 ns 0.99
source_volume_data/stereo 279.6 ns 272.5 ns 1.03

x86: taskset -c 27, functions and loops aligned on both sides, two runs within 1 %. macOS and Windows: not pinned; Windows is the mean of two runs in a VM, which vary by up to 7 %.

Why mul_data has a different loop on x86_64 and aarch64. LLVM vectorizes both loop forms but unrolls them differently, and the faster form differs per target: on x86_64 the zip loop was 17 % behind GCC's -O3 C and 4-sample groups are 3 %; on aarch64 the groups run one vector per iteration and were 49 % behind clang's C (437.9 ns), the zip loop level with it. So mul_data 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). Both forms are #[doc(hidden)] pub, and Tier 1 checks on every host that they agree. mul_block is one stream and a zip loop everywhere. Same finding as #3079.

Tests

  • macOS (aarch64): volume_mul Tier 1 and Tier 3, 15/15 in 0.5 s. The cmocka test passes 6/6 with ASan/UBSan at -O0 and -O2.
  • Linux gate host, stamped head 1eae853: lint, rust-test, cross (Windows and macOS clippy and Dylint) and tier2-linux passed.
  • Windows (MSVC 14.51, CI preset): test_source_volume_mul_steps 6/6, source::volume_mul Tier 1 and Tier 3 15/15 against the MSVC oracle.
  • Real app: N/A. This is a behaviour-neutral port. test_source_volume_mix runs the real libobs mix on the hybrid libobs in Tier 2.
  • Test cost: Tier 1 and Tier 3 together 0.55 s; 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

@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. Each sample is still one IEEE single multiply, bit-exact with the C (no FMA or reassociation, so #3007's SIMD parity holds). The commits show RED (b29adbf) then GREEN (efbb19f). There's Tier 2 cmocka coverage (6 cases) and Tier 3 parity against the new 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 source 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
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 30, 2026
vpacher and others added 6 commits October 30, 2026 12:13
Behaviour-neutral extraction before the Rust port (#3080). The two volume
multiply loops of obs-source.c move to libobs/obs-source-volume-mul.c.h,
as two functions on plain pointers and counts declared in the new
libobs/obs-source-volume-mul.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_volume_mul_block(out, count, vol): multiply_output_audio(),
  one volume over count samples; apply_audio_volume() still passes the
  mix's first channel buffer and AUDIO_OUTPUT_FRAMES * channels, the
  mix's channel buffers being consecutive in
  allocate_audio_output_buffer()'s one allocation;
- obs_source_volume_mul_data(out, vol_data, count): multiply_vol_data()'s
  inner loop, one channel by a volume per frame; the channel loop stays
  in obs-source.c.

The loop bodies are the C's, unchanged. obs-source.c keeps the action
queue and its mutex, get_source_volume(), the vol_data fill, the memset
and the mix and channel loops.

Checked in a scratch harness (not committed): master's two loops against
the moved functions on the same buffers, 4,000 ticks of 1 to 8 channels
and 71 short counts (0 to 70), samples, volumes and vol_data drawn from
NaN, -NaN, infinities, -0.0, 0, 1, random bit patterns and ordinary
values; clang -O0 and -O2, with and without ASan/UBSan: 0 differences
other than NaN payloads where both operands are NaN. Those differ
between builds of the C itself: at -O0 the result carries the sample's
payload, vectorized at -O2 the volume's (the multiply is commutative and
the compiler orders it), so the payload is not a property of the source.

Refs #3080.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Characterization before the Rust port, against the C moved to
libobs/obs-source-volume-mul.c: test_source_volume_mul_steps (every
platform, the moved file on its own, ASan and UBSan), 6 cases, on
buffers laid out as allocate_audio_output_buffer() lays them out (8
consecutive channels of 1024 frames per mix, then the next mix), with a
canary after every count:

- one volume (0, 1, 0.5, 20, -10 dB) over 1, 2 and 8 channels as one
  block: those channels multiplied, the rest of the mix and the next mix
  untouched;
- counts 0, 1 to 17 and around a tick: nothing past the count;
- special values: 0 times infinity is NaN, -0.0 for negative samples
  times 0, -0.0 and -1 flip signs, a NaN or infinite volume spreads to
  every sample (characterized, not endorsed: obs_source_volume_snap()
  keeps those volumes out of the mix since #2108), a NaN sample keeps its
  payload;
- a vol_data fade across the tick, channel by channel the way
  multiply_vol_data() calls it, for 1, 2 and 8 channels;
- an action's step (0.75, then mute at frame 300), negative samples after
  it -0.0;
- vol_data with special values in every position and counts 0 to 1023.

Not covered: a NaN sample times a NaN volume, whose payload depends on
how the C is compiled (operand order, scalar or vector).

Deliberate breaks of the moved C, 11 mutants (both ends of each loop,
the stride, the operator, the vol_data start and step, a rounding nudge,
-0.0 lost, the volume's sign): all 11 caught.

Refs #3080.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for #3080. obs_source::volume_mul has its API and docs, with todo!()
bodies: mul_block() (one volume over a slice, multiply_output_audio())
and mul_data() (a volume per sample, multiply_vol_data(); the shorter
slice counts).

tests/source/volume_mul.rs:
- steps_*: test_source_volume_mul_steps.c 1:1 (6 cases, same names,
  buffers, values and expectations);
- edge_*: mul_data() with slices of different lengths, empty slices, and
  proptests of both kernels against one multiply per sample (special
  values, any bit pattern, ordinary values) and of any input without a
  panic. A NaN sample times a NaN volume takes any NaN, as the C does
  not pin its payload.

All 10 fail on the stubs (obs-tests-core, source::volume_mul).

Refs #3080.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for #3080. obs_source::volume_mul implements the two kernels:

- mul_block(): `*sample *= vol` for every sample of the slice;
- mul_data(): `*sample *= *v` over the slices, so the shorter one counts
  and the rest of `out` stays.

Loops on slices, which cannot overlap, so LLVM vectorizes them without
std::arch (#3007); GCC at -O2, as libobs ships, leaves the C loops
scalar.

The loop form of mul_data() 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 was 17 % slower than GCC's
  vectorized C at -O3 (Ryzen 9 5950X); the grouped loop is 3 %.
- aarch64: a plain zip loop. The grouped loop is one vector per
  iteration and was 49 % slower than clang's C (Apple M1 Max); the zip
  loop is level with it.
Both forms are #[doc(hidden)] pub (mul_data_grouped, mul_data_zipped),
and Tier 1 checks on every host that they agree. The module denies clippy::todo now too.
No intentional differences: each sample is the C's one IEEE single
multiply. A NaN sample times a NaN volume may carry either payload, as
in the C, where -O0 and vectorized -O2 builds already differ.

C ABI (ffi::source_volume_mul): obs_source_volume_mul_block() and
obs_source_volume_mul_data() build slices of exactly `count` samples
(nothing for a count of 0, so a null pointer with 0 is fine, as in the C).

libobs: obs-source-volume-mul.c.h moves from libobs.core-source to its
own group libobs.ported-source-volume-mul (registered in
build/ported-source-sets/source-volume-mul.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: 10/10 (obs-tests-core, source::volume_mul).

Refs #3080.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 3 for #3080. obs-c-oracle builds the original
libobs/obs-source-volume-mul.c.h as oracle_obs_source_volume_mul_*
(oracle/source_volume_mul.c).

tests/source/volume_mul_parity.rs runs every call on the shim, the C and
the core, on copies of two mixes laid out as
allocate_audio_output_buffer() lays them out, and compares the whole
buffer bit for bit, the samples past the count included:

- every count from 0 to 70, the ticks of 1 to 8 channels and both mixes;
- 21 special and edge bit patterns (zeros, ones, infinities, NaNs with
  payloads and signs, denormals, the largest finite, powers of two, -10
  dB) as samples against each as a volume, at a whole tick and a count
  of 37, and as rotating vol_data;
- 64 rounds of random bit patterns at random counts;
- a proptest over any samples, volumes and count.

No intentional differences. A NaN sample times a NaN volume matches any
NaN: the C's payload there depends on how it is compiled. 4 tests.

Deliberate breaks of the core (14: a skipped first, last or second
sample, the volume's sign, add for multiply, -0.0 lost, a clamp, the
vol_data offset or start, one volume for all, a short count): all 14
caught here and by Tier 1.

port_comparison (audio_mix, one mix of one tick, scaled down and back
up), Apple M1 Max, Apple clang (which vectorizes the C too): block
stereo Rust 250 ns, C 319 ns (noisy, 256-395); block 8 channels Rust
893 ns, C 892 ns; vol_data stereo Rust 291 ns, C 294 ns. Release
assembly on aarch64: both shims multiply 16 samples per iteration (four
fmul.4s).

Refs #3080.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rust/README.md: the obs-source crate row lists obs-source-volume-mul.c.h,
and its own row says what moved, what obs-source.c keeps, why the block
call spans the mix's channels, the vectorization (#3007), the NaN-times-
NaN payload neither side pins, and what Tier 3 and the bench cover.
docs/rust-port/naming.md already maps libobs/obs-source*.c.h to
rust/obs-source.

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

Refs #3080.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=d44e83ee7c436b1a2a9d8e33d7d94430f1de1944 secs=420 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793383895,"gate":"rust/all/fmt","host":"linux-x86_64","key":"4efe63ebe3acdca441691c6fe67100f8b9b074a7384e83d5dde403b821f4d23a","lane":"lint","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":78,"stamp":"18661d2739b33c2cd30792ce69114fd5","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"4efe63ebe3acdca441691c6fe67100f8b9b074a7384e83d5dde403b821f4d23a","lane":"lint","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":78,"stamp":"430ac319a12b0fc4ac0800ea1c1181be","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"18e7c01990b58fe5c5f66445c47ac04133216aaf0352061309711a3144734a5a","lane":"rust-test","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":202,"stamp":"19711bd13be0639fadef7855fb15027e","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"c28cbafd5ecf8d5dcccae323557643b1e9acb8d80ba4d821cdc9bdd9ef592f2e","lane":"cross","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":140,"stamp":"905dcaa2b70cb39f72c2bcb47537c0e2","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"c28cbafd5ecf8d5dcccae323557643b1e9acb8d80ba4d821cdc9bdd9ef592f2e","lane":"cross","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":140,"stamp":"a2bee8c86fbe7345263c33077d426e4b","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"c28cbafd5ecf8d5dcccae323557643b1e9acb8d80ba4d821cdc9bdd9ef592f2e","lane":"cross","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":140,"stamp":"94a80a286613c93493c122c1566fd3f2","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"c28cbafd5ecf8d5dcccae323557643b1e9acb8d80ba4d821cdc9bdd9ef592f2e","lane":"cross","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":140,"stamp":"80d8472b2f8fdd2d00ce3daf6dc16e09","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
Ci-Attestation: {"at":1793383895,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"19dd1496d0d1de3fd404eed1e529c68502949eb24f42f7b470062428d7540ad8","lane":"tier2-linux","parents":["0800a21922050b2ef3e23505bb586f7eabc181c1"],"secs":243,"stamp":"7ac1b592455347ef04d2437c5ad9db59","tree":"d44e83ee7c436b1a2a9d8e33d7d94430f1de1944","v":1,"via":"run"}
@zackees
zackees force-pushed the rust/3080-source-volume-mul branch from 1eae853 to 0cd6f5a Compare October 30, 2026 19:13
@zackees

zackees commented Oct 30, 2026

Copy link
Copy Markdown
Member

Coordinator: rebased onto master after #3092 landed. The only conflict was the rust/README.md table row, where I kept both the scene audio-mix and source volume-mul entries. Pushed 0cd6f5a; it re-queues via auto-merge once CI is green.

@zackees
zackees enabled auto-merge October 30, 2026 19:14
@zackees
zackees added this pull request to the merge queue Oct 30, 2026
Merged via the queue into master with commit f2e76ff Oct 30, 2026
80 of 82 checks passed
@zackees
zackees deleted the rust/3080-source-volume-mul branch October 30, 2026 20:32
MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
…#3103)

edge_downmix_model ran 4000 full-size cases unscaled and hit nextest's
30 s limit on master b083524 (run 41121995484) and in the merge
queue for obs-rust#3075 and obs-rust#3093. Route the block through
obs_c_oracle::parity::cases() like the other proptest modules, so PR,
queue and master runs take the reduced count (obs-rust#2593); local, nightly
and release runs keep 4000.
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In review 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 source volume multiply kernels

2 participants