-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
libobs: Implement asynchronous sample rate conversion for audio #6351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
norihiro
wants to merge
8
commits into
obsproject:master
Choose a base branch
from
norihiro:async-sample-rate-conversion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7de322
libobs/media-io: Add compensation to audio-resampler
norihiro c81368c
libobs: Implement async compensation in source
norihiro a976a76
libobs/media-io: Report estimated frequency of sample rate compensation
norihiro 98fa88d
linux-pulseaudio: Add option to activate async compensation
norihiro cc8e642
win-wasapi: Add option to activate async compensation
norihiro c88f145
mac-capture: Add option to activate async compensation
norihiro 1646bb7
aja: Add option to activate async compensation
norihiro c88796e
decklink: Add option to activate async compensation
norihiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,9 @@ | |
| #include <libavutil/avutil.h> | ||
| #include <libavformat/avformat.h> | ||
| #include <libswresample/swresample.h> | ||
| #include "lag_lead_filter.h" | ||
|
|
||
| // #define DEBUG_COMPENSATION | ||
|
|
||
| struct audio_resampler { | ||
| struct SwrContext *context; | ||
|
|
@@ -41,6 +44,12 @@ struct audio_resampler { | |
| AVChannelLayout input_ch_layout; | ||
| AVChannelLayout output_ch_layout; | ||
| #endif | ||
|
|
||
| struct lag_lead_filter compensation_filter; | ||
| bool compensation_filter_configured; | ||
|
|
||
| uint64_t total_input_samples; | ||
| uint64_t total_output_samples; | ||
| }; | ||
|
|
||
| static inline enum AVSampleFormat convert_audio_format(enum audio_format format) | ||
|
|
@@ -169,6 +178,14 @@ audio_resampler_t *audio_resampler_create(const struct resample_info *dst, const | |
| void audio_resampler_destroy(audio_resampler_t *rs) | ||
| { | ||
| if (rs) { | ||
| uint64_t total_output_samples = rs->total_output_samples + swr_get_delay(rs->context, rs->output_freq); | ||
| if (rs->compensation_filter_configured) | ||
| blog(LOG_INFO, | ||
| "audio_resampler (asynchronous compensation): input %" PRIu64 " samples, " | ||
| "output %" PRIu64 " samples, estimated input frequency %f Hz", | ||
| rs->total_input_samples, rs->total_output_samples, | ||
| (double)rs->total_input_samples / total_output_samples * rs->output_freq); | ||
|
|
||
| if (rs->context) | ||
| swr_free(&rs->context); | ||
| if (rs->output_buffer[0]) | ||
|
|
@@ -178,6 +195,27 @@ void audio_resampler_destroy(audio_resampler_t *rs) | |
| } | ||
| } | ||
|
|
||
| void audio_resampler_set_compensation_error(audio_resampler_t *rs, int error_ns) | ||
| { | ||
| if (!rs->compensation_filter_configured) { | ||
| // Parameters are determined experimentally by SPICE simulation | ||
| // to have these characteristics. | ||
| // - Unity gain frequency: 1/180 Hz (inverse of 3 minutes) | ||
| // - Phase margin: 60 degrees | ||
| lag_lead_filter_set_parameters(&rs->compensation_filter, 3.756e-2, 8.250, 107.1); | ||
| lag_lead_filter_reset(&rs->compensation_filter); | ||
| rs->compensation_filter_configured = true; | ||
| } | ||
|
|
||
| lag_lead_filter_set_error_ns(&rs->compensation_filter, error_ns); | ||
| } | ||
|
|
||
| void audio_resampler_disable_compensation(audio_resampler_t *rs) | ||
| { | ||
| rs->compensation_filter_configured = false; | ||
| swr_set_compensation(rs->context, 0, 0); | ||
| } | ||
|
|
||
| bool audio_resampler_resample(audio_resampler_t *rs, uint8_t *output[], uint32_t *out_frames, uint64_t *ts_offset, | ||
| const uint8_t *const input[], uint32_t in_frames) | ||
| { | ||
|
|
@@ -187,6 +225,17 @@ bool audio_resampler_resample(audio_resampler_t *rs, uint8_t *output[], uint32_t | |
| struct SwrContext *context = rs->context; | ||
| int ret; | ||
|
|
||
| if (rs->compensation_filter_configured) { | ||
| lag_lead_filter_tick(&rs->compensation_filter, rs->input_freq, in_frames); | ||
| double drift = lag_lead_filter_get_drift(&rs->compensation_filter); | ||
|
|
||
| ret = swr_set_compensation(rs->context, -(int)(drift * 65536 / rs->input_freq), 65536); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where does that figure come from ? |
||
| if (ret < 0) { | ||
| blog(LOG_ERROR, "swr_set_compensation failed: %d", ret); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| int64_t delay = swr_get_delay(context, rs->input_freq); | ||
| int estimated = (int)av_rescale_rnd(delay + (int64_t)in_frames, (int64_t)rs->output_freq, | ||
| (int64_t)rs->input_freq, AV_ROUND_UP); | ||
|
|
@@ -210,9 +259,20 @@ bool audio_resampler_resample(audio_resampler_t *rs, uint8_t *output[], uint32_t | |
| return false; | ||
| } | ||
|
|
||
| #ifdef DEBUG_COMPENSATION | ||
| if (rs->compensation_filter_configured) | ||
| blog(LOG_INFO, | ||
| "async-compensation in_frames=%d out_frames=%d error_ns=%" PRIi64 " internal-condition=(%f %f)", | ||
| in_frames, ret, rs->compensation_filter.error_ns, rs->compensation_filter.vc1, | ||
| rs->compensation_filter.vc2); | ||
| #endif | ||
|
|
||
| for (uint32_t i = 0; i < rs->output_planes; i++) | ||
| output[i] = rs->output_buffer[i]; | ||
|
|
||
| rs->total_input_samples += in_frames; | ||
| rs->total_output_samples += ret; | ||
|
|
||
| *out_frames = (uint32_t)ret; | ||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Lag lead filter for asynchronous sample rate converter | ||
| * Copyright (C) 2022-2026 Norihiro Kamae <norihiro@nagater.net> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along | ||
| * with this program; if not, write to the Free Software Foundation, Inc., | ||
| * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| struct lag_lead_filter { | ||
| int64_t error_ns; | ||
| double vc1; | ||
| double vc2; | ||
|
|
||
| double gain; | ||
| double c1_inv; | ||
| double c2_inv; | ||
| }; | ||
|
|
||
| static void lag_lead_filter_reset(struct lag_lead_filter *f) | ||
| { | ||
| f->vc1 = 0.0; | ||
| f->vc2 = 0.0; | ||
| } | ||
|
|
||
| static void lag_lead_filter_set_parameters(struct lag_lead_filter *f, double gain, double c1, double c2) | ||
| { | ||
| f->gain = gain; | ||
| f->c1_inv = 1.0 / c1; | ||
| f->c2_inv = 1.0 / c2; | ||
| } | ||
|
|
||
| static inline void lag_lead_filter_set_error_ns(struct lag_lead_filter *f, int32_t error_ns) | ||
| { | ||
| f->error_ns = error_ns; | ||
| } | ||
|
|
||
| static void lag_lead_filter_tick(struct lag_lead_filter *f, uint32_t fs, uint32_t frames) | ||
| { | ||
| const double icp = f->gain * f->error_ns * 1e-9; | ||
| const double ic2 = (f->vc1 - f->vc2) / fs; | ||
| const double ic1 = icp - ic2; | ||
|
|
||
| f->vc1 += ic1 * f->c1_inv * frames; | ||
| f->vc2 += ic2 * f->c2_inv * frames; | ||
| } | ||
|
|
||
| static inline double lag_lead_filter_get_drift(const struct lag_lead_filter *f) | ||
| { | ||
| return f->vc1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you experiment with shorter and longer times ? say 1 min so 1/60 Hz and 5 min so 1/300 Hz ?
Regarding this curve:
https://user-images.githubusercontent.com/780600/180902267-e95694da-7aed-46c2-839b-36c5fe097f15.png
how is it modified ?
Is the sample/sec negative compensation due to input lag ? have you tested the reverse condition (through a simulation for instance)
More generally, can you explain why the filter converges to a plateau ? it seems to mean that the input source is intrinsically lagging if the filter must always compensate ?