From: Wang Cao <wangcao-at-google.com@ffmpeg.org> To: ffmpeg-devel@ffmpeg.org Cc: Wang Cao <wangcao@google.com> Subject: [FFmpeg-devel] [PATCH] avfilter/alimiter: Add "flush_buffer" option to flush the remaining valid data to the output Date: Sat, 26 Mar 2022 23:08:01 -0700 Message-ID: <20220327060800.3732289-1-wangcao@google.com> (raw) The change in the commit will add some samples to the end of the audio stream. The intention is to add a "zero_delay" option eventually to not have the delay in the begining the output from alimiter due to lookahead. Signed-off-by: Wang Cao <wangcao@google.com> --- doc/filters.texi | 5 ++++ libavfilter/af_alimiter.c | 57 ++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 9 deletions(-) If the intention is clear to you, do you prefer us add the "zero_delay" option to the same patch or it needs to go in a separate patch? Thanks! diff --git a/doc/filters.texi b/doc/filters.texi index d70ac3e237..bb8f7c1a0b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1978,6 +1978,11 @@ in release time while 1 produces higher release times. @item level Auto level output signal. Default is enabled. This normalizes audio back to 0dB if enabled. + +@item flush_buffer +Flushes the internal buffer so that all the input audio samples to the limiter +will appear to the output. Currently due to lookahead buffer, the total number +of output samples will be larger than the input. @end table Depending on picked setting it is recommended to upsample input 2x or 4x times diff --git a/libavfilter/af_alimiter.c b/libavfilter/af_alimiter.c index 133f98f165..ba0a1361ac 100644 --- a/libavfilter/af_alimiter.c +++ b/libavfilter/af_alimiter.c @@ -55,6 +55,9 @@ typedef struct AudioLimiterContext { int *nextpos; double *nextdelta; + int flush_buffer; + int total_samples_to_flush; + double delta; int nextiter; int nextlen; @@ -65,14 +68,15 @@ typedef struct AudioLimiterContext { #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM static const AVOption alimiter_options[] = { - { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, AF }, - { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, AF }, - { "limit", "set limit", OFFSET(limit), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0625, 1, AF }, - { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=5}, 0.1, 80, AF }, - { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 8000, AF }, - { "asc", "enable asc", OFFSET(auto_release), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AF }, - { "asc_level", "set asc level", OFFSET(asc_coeff), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, AF }, - { "level", "auto level", OFFSET(auto_level), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF }, + { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, AF }, + { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, AF }, + { "limit", "set limit", OFFSET(limit), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0625, 1, AF }, + { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=5}, 0.1, 80, AF }, + { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 8000, AF }, + { "asc", "enable asc", OFFSET(auto_release), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AF }, + { "asc_level", "set asc level", OFFSET(asc_coeff), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, AF }, + { "level", "auto level", OFFSET(auto_level), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF }, + { "flush_buffer","flush the samples in the lookahead buffer", OFFSET(flush_buffer), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AF }, { NULL } }; @@ -275,6 +279,39 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) return ff_filter_frame(outlink, out); } + +static int request_frame(AVFilterLink* outlink) { + AVFilterContext* ctx = outlink->src; + AudioLimiterContext* s = (AudioLimiterContext*)ctx->priv; + int ret; + AVFilterLink *inlink; + AVFrame *silence_frame; + + ret = ff_request_frame(ctx->inputs[0]); + + if (!s->flush_buffer) { + return 0; + } + + if (ret != AVERROR_EOF || s->total_samples_to_flush) { + // Not necessarily an error, just not EOF.s + return ret; + } + + // We reach here when input filters have finished producing data (i.e. EOF), + // but because of the attac param, s->buffer still has meaningful + // audio content that needs flushing. The amount of remaining audio to flush + // is the same as the amount of lookahead that was trimmed from the beginning. + inlink = ctx->inputs[0]; + // Pushes silence frame to flush valid audio in the s->buffer + silence_frame = ff_get_audio_buffer(inlink, s->total_samples_to_flush); + ret = filter_frame(inlink, silence_frame); + if (ret < 0) { + return ret; + } + return AVERROR_EOF; +} + static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; @@ -292,7 +329,8 @@ static int config_input(AVFilterLink *inlink) return AVERROR(ENOMEM); memset(s->nextpos, -1, obuffer_size * sizeof(*s->nextpos)); - s->buffer_size = inlink->sample_rate * s->attack * inlink->ch_layout.nb_channels; + s->total_samples_to_flush = inlink->sample_rate * s->attack; + s->buffer_size = s->total_samples_to_flush * inlink->ch_layout.nb_channels; s->buffer_size -= s->buffer_size % inlink->ch_layout.nb_channels; if (s->buffer_size <= 0) { @@ -325,6 +363,7 @@ static const AVFilterPad alimiter_outputs[] = { { .name = "default", .type = AVMEDIA_TYPE_AUDIO, + .request_frame = request_frame, }, }; -- 2.35.1.1021.g381101b075-goog _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2022-03-27 6:08 UTC|newest] Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-03-27 6:08 Wang Cao [this message] 2022-03-27 21:40 ` Marton Balint 2022-03-30 21:03 ` [FFmpeg-devel] [PATCH] avfilter/alimiter: Remove the delay introduced by lookahead buffer Wang Cao 2022-04-04 15:46 ` Paul B Mahol 2022-04-04 15:49 ` [FFmpeg-devel] [PATCH] avfilter/alimiter: Add "flush_buffer" option to flush the remaining valid data to the output Paul B Mahol 2022-04-04 22:28 ` Marton Balint 2022-04-05 18:56 ` Wang Cao 2022-04-06 11:49 ` Paul B Mahol 2022-04-07 7:46 ` Paul B Mahol 2022-04-07 21:56 ` Wang Cao 2022-04-08 18:42 ` Paul B Mahol 2022-04-08 20:41 ` Wang Cao 2022-04-09 12:37 ` Paul B Mahol 2022-04-11 20:59 ` Wang Cao 2022-04-12 19:42 ` Paul B Mahol 2022-04-13 4:11 ` Wang Cao 2022-04-15 18:49 ` [FFmpeg-devel] [PATCH] avfilter/alimiter: Add an option "comp_delay" that removes the delay introduced by lookahead buffer Wang Cao 2022-04-29 20:48 ` Wang Cao 2022-05-02 8:26 ` Paul B Mahol 2022-05-05 21:14 ` [FFmpeg-devel] [PATCH] avfilter/alimiter:add latency compensation Wang Cao 2022-05-10 22:45 ` Wang Cao 2022-05-12 12:05 ` Paul B Mahol 2022-05-05 21:17 ` [FFmpeg-devel] [PATCH] avfilter/alimiter: Add an option "comp_delay" that removes the delay introduced by lookahead buffer Wang Cao 2022-04-15 18:51 ` [FFmpeg-devel] [PATCH] avfilter/alimiter: Add "flush_buffer" option to flush the remaining valid data to the output Wang Cao
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220327060800.3732289-1-wangcao@google.com \ --to=wangcao-at-google.com@ffmpeg.org \ --cc=ffmpeg-devel@ffmpeg.org \ --cc=wangcao@google.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git