Skip to content

obs-media-io: Port the resampler output plan - #3342

Merged
zackees merged 6 commits into
masterfrom
fix/rust-port-the-resampler-s-output-403
Nov 1, 2026
Merged

zackees merged 6 commits into
masterfrom
fix/rust-port-the-resampler-s-output-403

Conversation

@rdoupe

@rdoupe rdoupe commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3332

What and why

Ports the output plan of the libswresample resampler (audio_resampler_create() in libobs/media-io/audio-resampler-ffmpeg.c.h) to rust/obs-media-io, in the audio_resampler_drift module beside the resampler's sample format, upmix row and drift loop (#3139):

  • audio_resampler_layout_mask(): the channel mask of each speaker layout (convert_speaker_layout(), only asked below libswresample 4.5.100);
  • audio_resampler_output_channels() and audio_resampler_planar_output(): the output channels, and whether the output format is planar (the caller still picks the channels or 1 as the plane count);
  • audio_resampler_input_channels() and audio_resampler_is_4point1(): the input channels for the default input layout and the two 4.1 overrides (libswresample 4.5.100 and newer);
  • audio_resampler_upmixes_mono(): more than one output channel, asked only once the input layout is mono.

Each is asked at the line where audio_resampler_create() read its input before, under the same condition (#2153): the same reads, in the same order, on each libswresample path.

audio-resampler-ffmpeg.c keeps every swr_* and av_* call (av_channel_layout_default() and av_channel_layout_compare() among them, so the mono test is still made by libavutil), the plane choice, the 4.1 layout assignments, the output buffer and every log. The masks cross as plain numbers, each checked against its AV_CH_LAYOUT_* with a _Static_assert.

audio-resampler-drift.c.h is already ported, so the hybrid libobs drops it. The Move therefore puts the plan in its own live file and group first (audio-resampler-plan.c.h, no Rust), and the GREEN commit moves that file into the existing ported group, libobs.ported-audio-resampler-drift.

The commits:

  1. libobs: Move the resampler output plan: behaviour-neutral, one function per original read site. A scratch harness built master's audio-resampler-ffmpeg.c and the moved one over logging stand-ins for libswresample and libavutil, on both libswresample paths, over every pair of speaker layouts and every format (values outside both enums too), then one resample. 16 builds (GCC and Clang, -O0 and -O2, with and without ASan and UBSan, old and new libswresample) give identical logs, and all 12 planted mutants differ.
  2. test: Characterize the resampler output plan: test_audio_resampler_plan_steps (9 cases), compiling the moved file on its own. Characterized, not endorsed: 2.1 (FL, FR, LFE) has the surround mask (FL, FR, FC) on old libswresample; an unknown input layout has 0 channels; packed output with no channels has one plane.
  3. RED: Tier 1 against todo!().
  4. GREEN: the core and the shims; the file joins the ported group, its live group goes, the six names join rust-hidden.d/obs-media-io-audio_resampler_drift.txt.
  5. Tier 3 against the oracle build of the C file, with a mutation check (18 mutants, all killed: the 12 core ones by Tier 1 and Tier 3, the 6 shim ones by Tier 3) and a port_comparison case (128 plans: Rust 0.48 µs, C 1.31 µs; asked once per resampler).
  6. Docs: the rust/README.md row, the crate description and the module docs.

Review: the first version asked one audio_resampler_plan() up front, which read the speakers and format before their original lines, ran the input-channel and 4.1 decisions on the old libswresample path too, and read output_ch for the upmix gate even for a non-mono input. It is now one function per read site, folded into the commits above.

No intentional difference from the C, and Tier 3 leaves no input out.

Upstream code by Lain and pkv, credited with Co-authored-by on the Move and GREEN commits.

RED → GREEN

  • RED commit: 63508fe obs-media-io: Test the resampler output plan port: soldr cargo test -p obs-media-io --lib -- audio::audio_resampler_plan:: passes 1 of 15 (steps_constants) and fails 14 with "not yet implemented". The local test-diff on the RED commit failed exactly those 14 and nothing else.
  • GREEN: Tier 1 15/15, Tier 3 7/7 (61/61 for all of audio::audio_resampler_). Tier 2, natively under a private Xvfb, on the C, hybrid and C reference builds: test_audio_resampler_plan_steps 9/9, test_audio_resampler_drift_steps 17/17, test_audio_clock_drift 6/6, test_audio_restart_resampler_tail 4/4, none skipped. The exported symbols are identical on the three Linux builds (1767 each, as on master), the six new functions local (t); only libobs-c-reference compiles the ported group. Local gate lanes: test-diff 5853/5853 (6 skipped, as on master), lint and cross pass, and clippy --target x86_64-pc-windows-msvc --all-targets is clean. Runs were on 2efe011.
  • Real app (UI, protocol output or logging changes): not applicable, a port with no change in behaviour.

Test cost: 22 new Rust tests in the media library harness (no new binary); the whole module takes at most 53 ms per run at the reduced parity level over 25 runs, peak RSS 5.8 MB, no threads of their own.

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 (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite (not stamped: the Tier 2 Docker lane cannot run on this machine; Tier 2 ran natively, above)
  • 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) (none: both say so)

rdoupe and others added 6 commits November 1, 2026 00:32
Behaviour-neutral extraction before the Rust port. The output plan of
audio_resampler_create() in media-io/audio-resampler-ffmpeg.c.h moves
to the new media-io/audio-resampler-plan.c.h, as hidden functions on
plain values declared without EXPORT in audio-resampler-drift.h. Each
is asked at the line where audio_resampler_create() read its input,
under the same condition (#2153):

- audio_resampler_layout_mask(): convert_speaker_layout(), the channel
  mask of each speaker layout (0 for unknown and outside the enum),
  still asked only below libswresample 4.5.100;
- audio_resampler_output_channels(): the output channels, where
  output_ch is set;
- audio_resampler_planar_output(): whether the output format is
  planar; the caller still picks output_ch or 1 for the planes,
  reading output_ch only when it is;
- audio_resampler_input_channels() and audio_resampler_is_4point1():
  the input channels for the default input layout and the two 4.1
  overrides, asked only on libswresample 4.5.100 and newer, src and
  dst each where they were read;
- audio_resampler_upmixes_mono(): more than one output channel, the
  right side of the mono test's &&, so output_ch is read only for a
  mono input.

The header still includes no FFmpeg header: the masks are
AUDIO_RESAMPLER_CH_LAYOUT_* values, and audio-resampler-ffmpeg.c
checks each against its AV_CH_LAYOUT_* with a _Static_assert, as it
does for the sample formats. audio-resampler-ffmpeg.c keeps every
swr_* and av_* call: the mono test is still the mask compare on the
old libswresample and av_channel_layout_compare() on the newer one.
The layout table is now built everywhere.

The new file has its own live group, libobs.audio-resampler-plan, in
the media lib-module, with a precompiled header of
audio-resampler-drift.h only, so the C oracle and a C test can compile
it alone. audio-resampler-drift.c.h is already ported, so the hybrid
libobs would drop it; the plan joins it in the GREEN commit. No Rust:
the hybrid libobs compiles the C too. The C, hybrid and C reference
builds export the same 1767 symbols, the six new functions local (t)
in each.

Move harness (scratch, not committed): origin/master's
audio-resampler-ffmpeg.c and this branch's with the moved file, each
over logging stand-ins for libswresample and libavutil (layouts,
allocation, matrix, init, convert, with injected failures) and for
bmalloc, bfree and blog, on both libswresample paths. Every pair of
speaker layouts (with -1, 8, 9, 100 and INT_MAX) and every output
format (with -1, 9 and 100), then one resample and the planes handed
out. 16 builds (GCC and Clang, -O0 and -O2, with and without ASan and
UBSan without recovery, old and new libswresample): identical logs,
36,672 lines each side on the old path and 48,164 on the new. All 12
planted mutants differ: 5.1 as 5.1 (side), 2.1 as 4.0, unknown as
mono, outside the enum as stereo (old path), one output channel too
many, always planar or planar inverted, an unknown input as two
channels, 4.1 for 4.0 or never, and the gate at one channel or never.

test_audio_clock_drift (6), test_audio_restart_resampler_tail (4) and
test_audio_resampler_drift_steps (17) pass on the C, hybrid and C
reference builds, none skipped.

git blame on the moved lines shows Lain's resampler and pkv's channel
layout API and mono upmix.

Refs #3332.

Co-authored-by: Lain <[email protected]>
Co-authored-by: pkv <[email protected]>
test_audio_resampler_plan_steps compiles the moved
libobs/media-io/audio-resampler-plan.c.h into itself, with ASan and
UBSan, and calls only its hidden functions through
audio-resampler-drift.h, with no swr context and no FFmpeg header, so
it runs on every platform (9 cases). A local helper asks them as
audio_resampler_create() does: the planes from the output channels
when the output is planar, the gate only for a mono input.

- the seven channel masks;
- the mask of every speaker layout, and 0 for SPEAKERS_UNKNOWN and for
  -1, 7 (the gap before 7.1), 9, 100, INT_MAX and INT_MIN;
- the output channels of every layout, 0 outside the enum;
- the planes of every format: the channels when planar, else 1, and 1
  for a format outside the enum;
- the input channels of every layout;
- 4.1 on the input side, the output side, both, and nowhere else;
- mono into each layout: one input channel, and the upmix only into
  two channels or more;
- the upmix gate at 0, 1, 2, 3, 5, 8, 9, 64 and UINT32_MAX channels;
- every pair of layouts from -2 to 10 and every format from -2 to 10
  against the expressions audio_resampler_create() wrote.

Characterized, not endorsed:
- SPEAKERS_2POINT1 (FL, FR and LFE) has the surround mask (FL, FR and
  FC) on the old libswresample path;
- an unknown input layout has 0 channels, which the newer
  libswresample path gives to av_channel_layout_default();
- a packed format with no output channels still has one plane.

It is in the native test graph, in name order, on every platform, with
the header as a watch input. No assert_in_range or fail_msg, and no
comparison of two enum types, so it builds with cmocka 1.x and 2.x and
with MSVC. test_audio_resampler_drift_steps is unchanged. Passes on the
C and Rust build trees (9/9).

Refs #3332.
RED for the port of libobs/media-io/audio-resampler-plan.c.h to the
audio_resampler_drift module of rust/obs-media-io, beside the
resampler's sample format, upmix row and drift loop. The safe core is
a stub (todo!()): layout_mask(), output_channels(), planar_output(),
input_channels(), is_4point1() and upmixes_mono(). The channel masks
(ch_layout) are real.

tests/audio/audio_resampler_plan.rs ports
test_audio_resampler_plan_steps 1:1: its 9 cases under the same names
(steps_*), in the same order, with the same values and expectations,
the characterized-not-endorsed cases included, through the same kind
of local helper that asks the core as audio_resampler_create() does.

Edge cases (edge_*) check the core against a model of the origin/master
C, its expressions inline: the mask of every layout from -1 to 10 and
at the ends of int; the plan of every combination of those values as
input layout, output layout and format; and the upmix gate at the ends
of uint32_t, mono or not. Three proptests: the mask of any int, the
plan of any three ints (half of them drawn from -2 to 10), and the gate
of any channel count.

The tests are a module of the existing obs-media-io library harness,
the media category's binary; no new test binary.

soldr cargo test -p obs-media-io --lib -- audio::audio_resampler_plan::
passes 1 of 15 (the constants) and fails 14 with "not yet
implemented".

Refs #3332.
GREEN for #3332. Ports libobs/media-io/audio-resampler-plan.c.h, the
output plan of the libswresample resampler, to the
audio_resampler_drift module of rust/obs-media-io.
audio-resampler-ffmpeg.c stays C around it: every swr_* and av_* call
(av_channel_layout_default() and av_channel_layout_compare() among
them), the planes' choice, the 4.1 layout assignments, the output
buffer and every log, each read in its place. The hybrid libobs drops
the C file and links the Rust shims in its place.

Safe core (src/audio_resampler_drift.rs):
- layout_mask(): the channel mask of each enum speaker_layout value,
  through audio_io's speaker_layout_from_c(); 0 for unknown and any
  value outside the enum, 2.1 as surround as in the C;
- output_channels() and input_channels(): audio_io's audio_channels()
  (0 outside the enum), the input count as an int;
- planar_output(): audio_io's is_audio_planar(), false for a format
  outside the enum;
- is_4point1(): the 4.1 layout and nothing else;
- upmixes_mono(): more than one output channel.
The module's lints (no arithmetic side effects, casts, indexing,
unwrap or panics) cover the new functions; none keeps state, takes a
lock or allocates.

No intentional difference.

Shims (src/ffi/audio_resampler_drift.rs): the six functions keep the C
signatures; enum speaker_layout and enum audio_format cross as int.
They take no pointers.

C side: the file leaves its live group, libobs.audio-resampler-plan,
for the existing ported group, libobs.ported-audio-resampler-drift,
with the same precompiled header contents; the live group, its
precompiled header and its source-set and lib-module entries go. The
functions are not EXPORT, so the shims are hidden: the
obs-media-io-audio_resampler_drift.txt fragment of rust-hidden.d.

test_audio_resampler_plan_steps still compiles the C file into itself,
unchanged; tests.d says why it has no _rust variant (the shims'
archive member does not link on its own, as for the drift steps
test).

soldr cargo test -p obs-media-io --lib -- audio::audio_resampler_
passes 54 of 54 (15 new). test_audio_resampler_plan_steps (9/9),
test_audio_resampler_drift_steps (17/17), test_audio_clock_drift (6/6)
and test_audio_restart_resampler_tail (4/4) pass against the C, hybrid
and C reference builds, none skipped. The exported symbols are
identical on the Linux C, hybrid and C reference builds (1767 each, as
on master), the six shims local (t); only libobs-c-reference compiles
the ported group.

Refs #3332.

Co-authored-by: Lain <[email protected]>
Co-authored-by: pkv <[email protected]>
Tier 3 for the resampler output plan port. The oracle
(rust/obs-c-oracle/oracle/audio_resampler_plan.c) compiles the
unchanged libobs/media-io/audio-resampler-plan.c.h with its six
functions renamed to oracle_*, and gives its seven channel masks;
bound in src/audio_resampler_plan.rs.

tests/audio/audio_resampler_plan_parity.rs compares the oracle, the
shims and the core exactly, all three sides:
- the masks;
- the mask, the output channels, the input channels and the 4.1 test
  of every speaker layout from -300 to 300 and the ends of int, and by
  proptest of any int;
- whether the output is planar for every format from -300 to 300 and
  the ends of int, and by proptest any int;
- the upmix gate over 0 to 300 channels and the ends of uint32_t, and
  by proptest any count.

No intentional difference, and no input left out: every int is a
defined input to all six functions.

port_comparison gets benches/port_comparison/audio_resampler_plan.rs:
every pair of the eight speaker layouts in packed and planar float
(128 plans), each asked as audio_resampler_create() asks it, folded
into a checksum both sides must match. Linux x86_64: Rust 0.48 us, C
1.31 us; the plan is asked once per resampler. It allocates nothing
with bmalloc.

Mutation check (done once, not committed); failing tests per mutant:

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| mask: 2.1 as stereo | 2 | 1 |
| mask: 5.1 as 5.1 (side) | 2 | 1 |
| mask: unknown as mono | 2 | 1 |
| mask: outside the enum as stereo | 3 | 2 |
| output channels: one too many | 7 | 2 |
| channels: an unknown layout as two | 7 | 2 |
| planar: inverted | 7 | 2 |
| planar: a format outside the enum | 4 | 2 |
| input channels: at least two | 5 | 2 |
| 4.1: for 4.0 | 6 | 1 |
| gate: at one channel | 3 | 1 |
| gate: never | 4 | 2 |
| shim mask: the next layout | 0 | 1 |
| shim output channels: the next layout | 0 | 1 |
| shim planar: inverted | 0 | 2 |
| shim input channels: the next layout | 0 | 1 |
| shim 4.1: inverted | 0 | 2 |
| shim gate: inverted | 0 | 2 |

All 18 are killed; the shim mutants by Tier 3, the tier that reaches
the shims. The proptest regression files keep seeds the mutants failed
on.

Tier 1 and Tier 3 pass (15/15 and 7/7). At the reduced parity level
the whole module, 22 tests, takes at most 53 ms over 25 runs; peak RSS
5.8 MB; no threads of their own.

Refs #3332.
rust/README.md's port table has a row for
libobs/media-io/audio-resampler-plan.c.h, after the drift loop's: the
six functions (the channel mask, the output channels, whether the
output is planar, the input channels, the 4.1 test and the mono upmix
gate), each asked where the C read its input, what stays C in
audio-resampler-ffmpeg.c (every swr_* and av_* call, the planes'
choice, the 4.1 layout assignments and every log), the
_Static_asserts on the masks, what is characterized, and the tests
that cover it. The drift loop's row no longer lists
convert_speaker_layout() among what C keeps. The crate's description
and its lib.rs module docs name the output plan. naming.md's
libobs/media-io/ row already maps the directory to the crate.

No docs/features.md line: a port with no change in behaviour.

Fixes #3332.
@rdoupe
rdoupe force-pushed the fix/rust-port-the-resampler-s-output-403 branch from d405642 to 2efe011 Compare November 1, 2026 05:00
@rdoupe
rdoupe marked this pull request as ready for review November 1, 2026 05:24
@rdoupe
rdoupe requested a review from zackees as a code owner November 1, 2026 05:24
@rdoupe
rdoupe added this pull request to the merge queue Nov 1, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 1, 2026
@rdoupe
rdoupe added this pull request to the merge queue Nov 1, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 1, 2026
@zackees
zackees added this pull request to the merge queue Nov 1, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 1, 2026
@zackees

zackees commented Nov 1, 2026

Copy link
Copy Markdown
Member

Coordinator: this has been ejected from the merge queue twice, so I'm not re-queuing it blind.

  • 2nd ejection (run 41285357452, job 124786472746, Tier 2 Linux tests OFF): test_mp4_mux_multichannel segfaults in flac_51_config_box_follows_entry ("Test failed with exception: Segmentation fault(11)"). The aac and opus cases before it pass.
  • 1st ejection (run 41282595997, job 124777959095): the Tier 2 Linux build job failed.

No other queue run in the recent window fails like this, and this PR changes audio-resampler-ffmpeg.c.h, so please check whether the FLAC 5.1 output path goes through the new drift/plan code. Would you reproduce it with ./unittest (or under ASan)? If it turns out to be unrelated, say so and I'll queue it again.

@rdoupe

rdoupe commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Unrelated to this PR: the FLAC 5.1 case never reaches the resampler, old code or new.

  • test_mp4_mux_multichannel registers stub encoders and an output whose start returns false, then only calls obs_output_initialize_encoders(). libobs creates a resampler only when an encoder connects to audio on start, which never happens here.
  • To check rather than argue it, I ran the test under gdb on this PR's head merged with the same master the second queue run used (63c4084), with breakpoints on all 11 resampler entry points: audio_resampler_create, _resample, _destroy, _sample_format, _upmix_row, and the six plan functions this PR adds. All resolved in the loaded libobs; none fired in any of the 6 cases, on the hybrid libobs or on libobs-c-reference. The control breakpoints did fire (obs_output_initialize_encoders and obs_encoder_initialize, 6 each).
  • On that same merge, the test passed 20 of 20 runs on each of the C, hybrid and C-reference builds (all 6 cases run, none skipped). ./unittest's Tier 2 lane needs Docker, which I can't use on this machine, so these were native ctest runs under Xvfb. I didn't do an ASan build; the gdb trace answers the question directly.

The first ejection (run 41282595997) was the Tier 2 build losing its zccache daemon: all 82 failed build steps are "daemon unavailable ... refusing to run /usr/bin/cc uncached", with no compile error. That's what #3384 addresses.

The segfault looks like a rare intermittent fault in the muxer test or its FLAC stub. If it shows up again on other PRs, I'll file it. This one is fine to queue again.

@zackees
zackees added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit ea5c5ca Nov 1, 2026
41 checks passed
@zackees
zackees deleted the fix/rust-port-the-resampler-s-output-403 branch November 1, 2026 09:52
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 resampler's output plan

2 participants