Skip to content

rust: Port the resampler clock drift loop #3139

Closed

Description

@rdoupe

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:

  1. Sample format: convert_audio_format(), enum audio_format to the libswresample sample format (unknown, and anything past the last value, map to S16).
  2. Mono upmix: the matrix row audio_resampler_create() hands swr_set_matrix() for a mono input with more than one output channel (front pair and centre take the signal, LFE doesn't).
  3. 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.
  4. Loop filter step: clamp_drift() and update_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 the sample_delta for swr_set_compensation().
  5. Output frames: the padding of the estimated output frames while compensation is engaged (estimated / 512 + 2).
  6. 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) and test_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 of int64_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 fuse vc1 + ic1 / C1 * in_frames into an FMA (Rust never fuses).
  • lround() is f64::round() then a cast; the delta is at most about +-16,777, so it never overflows a 32-bit long.
  • 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_comparison case for the filter step is added.

Activity

  1. added
    enhancement
    port
    on Oct 31, 2026
  2. rdoupe commented on Oct 31, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: the resampler clock drift loop port (#3139).

    Plan, one PR of six commits:

    1. Move the sample format map, the mono upmix row, the dead band, the loop filter step, the output frame padding, the counters and the drift report out of media-io/audio-resampler-ffmpeg.c.h into media-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.
    2. Characterize: test_audio_resampler_drift_steps.
    3. RED: Tier 1 tests in obs_media_io::audio_resampler_drift against todo!() stubs.
    4. GREEN: the core and its shims, in a ported group, with -ffp-contract=off on the moved file.
    5. Tier 3 against the oracle build of the moved C, with a mutation check, and a port_comparison case.
    6. Docs: rust/README.md rows, the crate description and module docs.

    Draft PR once the RED commit exists, today.

  3. added
    ready
    on Oct 31, 2026
  4. rdoupe commented on Oct 31, 2026

    @rdoupe
    CollaboratorAuthor

    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.h and are ported to rust/obs-media-io as audio_resampler_drift, with Tier 1, Tier 3 (against the C oracle) and test_audio_resampler_drift_steps.

    Left: nothing in this unit. The swr_* and av_* calls, the output buffer and the logs stay in audio-resampler-ffmpeg.c by 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementportready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions