Repository navigation
Description
Unit
libobs/media-io/audio-resampler-ffmpeg.c.h: the clock drift compensation loop and the output size plan of the libswresample resampler, which every audio source and output converts through.
The decisions:
- Sample format:
convert_audio_format(),enum audio_formatto the libswresample sample format (unknown, and anything past the last value, map to S16). - Mono upmix: the matrix row
audio_resampler_create()handsswr_set_matrix()for a mono input with more than one output channel (front pair and centre take the signal, LFE doesn't). - Compensation error:
audio_resampler_set_compensation_error(): the first call turns compensation on and clears the loop filter; an error within the 10 ms dead band is 0, a larger one is moved towards 0 by the band. - Loop filter step:
clamp_drift()andupdate_compensation(): one step of the lag-lead filter (charge pump into C1 in parallel with 1 ohm and C2), both states advanced by the frame count and clamped to 1000 ppm of the input rate; it engages the first time the error is non-zero, then gives thesample_deltaforswr_set_compensation(). - Output frames: the padding of the estimated output frames while compensation is engaged (
estimated / 512 + 2). - Counters and report: the input and output sample counts while engaged, and the drift report's gate and estimated input rate in
audio_resampler_destroy().
They move to libobs/media-io/audio-resampler-drift.c.h (hidden functions on plain values and the compensation state, declared in audio-resampler-drift.h without EXPORT) and are ported to rust/obs-media-io (module audio_resampler_drift, next to audio_io and format_conversion). The C keeps every libswresample and libavutil call (swr_*, av_channel_layout_*, av_rescale_rnd, av_samples_alloc, av_freep), the output buffer and its size, convert_speaker_layout() (old libswresample only), the output pointer copies and every log. The compensation state stays in the resampler, as a #[repr(C)] mirror the C checks with _Static_asserts.
Phase and position
Phase 2 libobs core, media-io (media unity group). Earlier media-io units: audio-io, format-conversion, video-io, video-frame, media-remux. Tracking: #4.
Dependencies and existing tests
- Depends on nothing open.
- Existing tests that run this code through the real libobs:
test_audio_clock_drift(a simulated capture device a few hundred ppm off, through the source's resampler) andtest_audio_restart_resampler_tail. - No characterization of the moved functions on their own yet.
test_audio_resampler_drift_steps(cmocka, the moved file on its own, no swr context) lands first: every format and one past the last, the upmix row for 1 to 8 channels, the dead band at its edges and at the ends ofint64_t, 10,000 filter steps at 48 kHz with constant +-50 ms errors (the engage step, the 1000 ppm clamp, each step's delta), a zero error that never engages, the padding, the counters and the report.
Notes
- Floating point: the parity test compares doubles exactly. The C oracle is built with
-ffp-contract=off; the moved file gets the same flag so GCC and Clang never fusevc1 + ic1 / C1 * in_framesinto an FMA (Rust never fuses). lround()isf64::round()then a cast; the delta is at most about +-16,777, so it never overflows a 32-bitlong.- A zero input rate would make the states NaN;
swr_init()rejects it before any step runs. Tier 3 rates start at 8,000. - Per-call cost on the audio thread: a
port_comparisoncase for the filter step is added.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 31, 2026
Claiming: the resampler clock drift loop port (#3139).
Plan, one PR of six commits:
media-io/audio-resampler-ffmpeg.c.hintomedia-io/audio-resampler-drift.c.h(hidden functions, no change in behaviour, export sets unchanged), checked by a scratch harness over master's code and the moved code (GCC and Clang, -O0 and -O2, ASan/UBSan) with planted mutants.test_audio_resampler_drift_steps.obs_media_io::audio_resampler_driftagainsttodo!()stubs.-ffp-contract=offon the moved file.port_comparisoncase.Draft PR once the RED commit exists, today.
libobs: Move the resampler drift decisions
test: Characterize the resampler drift loop
obs-media-io: Test the resampler drift port
obs-media-io: Port the resampler drift loop
obs-media-io: Compare resampler drift to C
docs: Record the resampler drift loop port
libobs: Read the drift delta after its log
rdoupe commentedon Oct 31, 2026
Fixed by the port of the resampler drift loop (PR #3143, merged through the merge queue): the seven decisions listed above moved into
libobs/media-io/audio-resampler-drift.c.hand are ported torust/obs-media-ioasaudio_resampler_drift, with Tier 1, Tier 3 (against the C oracle) andtest_audio_resampler_drift_steps.Left: nothing in this unit. The
swr_*andav_*calls, the output buffer and the logs stay inaudio-resampler-ffmpeg.cby design; replacing libswresample itself is the separate issue about SIMD for the swresample and swscale replacements (#3008). No follow-up issue was needed. The tracking issue (#4) has no checkbox for this unit, so there was nothing to tick.obs-media-io: Port the resampler drift loop (obs-rust#3143)