From patchwork Sat Dec 3 12:50:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 39568 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp1970143pzb; Sat, 3 Dec 2022 04:50:21 -0800 (PST) X-Google-Smtp-Source: AA0mqf7nCJwNAjhL9Cd+WKJeEHMqNxdVvazTUc7H2XwNgKs25tPtMKV9HZiVkvkkruLaIjgoffIB X-Received: by 2002:a17:906:f90:b0:7ba:9c18:11e7 with SMTP id q16-20020a1709060f9000b007ba9c1811e7mr40799250ejj.198.1670071820966; Sat, 03 Dec 2022 04:50:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670071820; cv=none; d=google.com; s=arc-20160816; b=BYeq31hpYvrTXrVjvF27lE+YFHTP4GmCNlew1oI0/6l313w/pe+B8+DvnWuRAheeyD IGsTW4B3zvhdg9AC7f1E448FbdahSmrgp8hVAHvus8xNL1++8wNRK2tPB8U/fFF9qTB0 2dXpNY0M7CDnbbdO/kbA9A9ZfU31hK6BXkf2zuazE7iQgVrvP4J4/eElwPsLVuSTo1vG MPRM6EpGp6PuVmtOxyikyHjYkb0ZtEGV2WfVlNhHf4umpDNnyfWsGG+jlnEX5XthClmG EH9XXjEdI3mD3+joj0nxlAlKKygcoLC2jGocjLATIjKoYcUyGzmv8FTNmgQS/ZFPdL5u 3Gow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=oqdudP8Kwgtg4iU85kfXrtCEjal8Hx3iekV0Cc/KEps=; b=vMWva78xx2bWfGSSnR3O1rF6XWdPxdAGLEq47WiqipcofJ8uEM1K9blR1JSP/irThB G5HYzaDdvPA73YLTDGyvT54icDblEAPiD9WCVTh63P6TFSyfUgYbeYwXMgz1jExCwzVr cUX8tZMuukwORtd26uiTogDfrRwIaWVpvkf7SeXSA+aopCv/Uz9hF89tEuEvQYO5ylB/ utuqyDVl1Hq7C2iHZcf/fhXJ3hhTQ9KC9uME0139/gzVmiP02/j2S3Jwy+yO5G4xlrE4 0e+XO/YV9LOAskeT/LuWCICXZpwXTypels9frvEC1rapY06mRLutF+/SNOm87/na2aW6 p6/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Sz3rEeE6; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id js4-20020a17090797c400b0073d8830e4c7si8770961ejc.954.2022.12.03.04.50.19; Sat, 03 Dec 2022 04:50:20 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Sz3rEeE6; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3A36868A778; Sat, 3 Dec 2022 14:50:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vs1-f45.google.com (mail-vs1-f45.google.com [209.85.217.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BCDAA68ADCE for ; Sat, 3 Dec 2022 14:50:09 +0200 (EET) Received: by mail-vs1-f45.google.com with SMTP id t5so7059042vsh.8 for ; Sat, 03 Dec 2022 04:50:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=KUQqk5mJnOP5yVbAkWSfQNwIL8fm1YZdND8uOLRMULg=; b=Sz3rEeE6AV+Y4VUndl3r0ENTGtGAkoYD8zDFTe4jDda/41HDfGh0TOpcCgKmY2J20l ZGq5SF8umefVHCaARM4DWdVuY6I10/iMrLE8NuHtXXt5HPN6Fa8P2zbcjrpXeNvyUN27 rtvqXhTdreinYacBwG2+rM+byNC7JYpBt5t1biDHbdFo6r4wcMPQSQB4zMDS/d4qnQcE 2LcY0g8bgdwrZyW9um5nDgk4LuZ8PkINzI6PAwUQ1dauyx9ObskJUaJStsAsnGp9mK0i HB/AYSypBKClzModuv0bbHo/6bVJVb84+yeeM1F8TSVhTXP2eHiWGJbJDqqVhDe8myik 988w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=KUQqk5mJnOP5yVbAkWSfQNwIL8fm1YZdND8uOLRMULg=; b=Va9oV/OIKmdzyPCf+wxwVuMA93C+F8PLKHPczEq9pvC4Bujx8ziP/MzCoIPUV8WG91 /UHKbl77hct2L9y53074SW9S79UIDw5qdyrUntS5UJ9u+Z0aNJjJzhoY0htBSbLwwP5w VMMJ++Bddc1y5qZwoxaOqmFay2Mn37RCOr2LXELX7guwJXbblTytnHVn7waZ1kBW00WJ uT4/Qf20JfXyK3yQ6BZ79ADBL6N93PtBX/dFhmVZ+OkRcoWkisbuOEw0syFnDN5C7cPX VSyE3FrYjHt+duJD73tbyu5uexYPGR5flUvo4Jo6yaf5cSYi2scnWR/p/BR6Xx4xpW2N CbiQ== X-Gm-Message-State: ANoB5pmUDVs6RStGtRkInLbiY5wqfrt4v7Io4f8vw/tvhX8C79GMw7cE DKxk/u6BOy0tyY4nHS8TjCgaHsgCzv9B2cTi/ZO0YmOT X-Received: by 2002:a67:c906:0:b0:3aa:f64:fbfd with SMTP id w6-20020a67c906000000b003aa0f64fbfdmr33843268vsk.15.1670071807887; Sat, 03 Dec 2022 04:50:07 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:612c:2428:b0:32a:5eaa:92e4 with HTTP; Sat, 3 Dec 2022 04:50:06 -0800 (PST) From: Paul B Mahol Date: Sat, 3 Dec 2022 13:50:06 +0100 Message-ID: To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] avfilter: add adrc filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: usOXMyl6bCYa Patch attached. TODO: add documentation and examples with various transfer expressions (compressor, expander, limiter, gate, etc). From da98ffba037499825e52305ba23f21083e436e4a Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Mon, 7 Nov 2022 21:00:50 +0100 Subject: [PATCH] avfilter: add adrc filter Signed-off-by: Paul B Mahol --- libavfilter/Makefile | 1 + libavfilter/af_adrc.c | 468 +++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 3 files changed, 470 insertions(+) create mode 100644 libavfilter/af_adrc.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5726ad0c76..2791b6a950 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -48,6 +48,7 @@ OBJS-$(CONFIG_ADECORRELATE_FILTER) += af_adecorrelate.o OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o OBJS-$(CONFIG_ADENORM_FILTER) += af_adenorm.o OBJS-$(CONFIG_ADERIVATIVE_FILTER) += af_aderivative.o +OBJS-$(CONFIG_ADRC_FILTER) += af_adrc.o OBJS-$(CONFIG_ADYNAMICEQUALIZER_FILTER) += af_adynamicequalizer.o OBJS-$(CONFIG_ADYNAMICSMOOTH_FILTER) += af_adynamicsmooth.o OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o diff --git a/libavfilter/af_adrc.c b/libavfilter/af_adrc.c new file mode 100644 index 0000000000..7f5c6b3528 --- /dev/null +++ b/libavfilter/af_adrc.c @@ -0,0 +1,468 @@ +/* + * Copyright (c) 2022 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "libavutil/eval.h" +#include "libavutil/ffmath.h" +#include "libavutil/opt.h" +#include "libavutil/tx.h" +#include "audio.h" +#include "avfilter.h" +#include "filters.h" +#include "internal.h" + +static const char * const var_names[] = { + "ch", ///< the value of the current channel + "sn", ///< number of samples + "nb_channels", + "t", ///< timestamp expressed in seconds + "sr", ///< sample rate + "g", ///< input gain + "f", ///< frequency + NULL +}; + +enum var_name { + VAR_CH, + VAR_SN, + VAR_NB_CHANNELS, + VAR_T, + VAR_SR, + VAR_G, + VAR_F, + VAR_VARS_NB +}; + +typedef struct AudioDRCContext { + const AVClass *class; + + double attack_ms; + double release_ms; + char *expr_str; + + double attack; + double release; + + int fft_size; + int overlap; + int channels; + + float *window; + + AVFrame *drc_frame; + AVFrame *energy; + AVFrame *envelope; + AVFrame *factors; + AVFrame *in; + AVFrame *in_buffer; + AVFrame *in_frame; + AVFrame *out_dist_frame; + AVFrame *spectrum_buf; + AVFrame *target_gain; + AVFrame *windowed_frame; + + AVTXContext **tx_ctx; + av_tx_fn tx_fn; + AVTXContext **itx_ctx; + av_tx_fn itx_fn; + + AVExpr *expr; + double var_values[VAR_VARS_NB]; +} AudioDRCContext; + +#define OFFSET(x) offsetof(AudioDRCContext, x) +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption adrc_options[] = { + { "transfer", "set the transfer expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str="g"}, 0, 0, FLAGS }, + { "attack", "set the attack", OFFSET(attack_ms), AV_OPT_TYPE_DOUBLE, {.dbl=50.}, 1, 1000, FLAGS }, + { "release", "set the release", OFFSET(release_ms), AV_OPT_TYPE_DOUBLE, {.dbl=100.}, 5, 2000, FLAGS }, + {NULL} +}; + +AVFILTER_DEFINE_CLASS(adrc); + +static void generate_hann_window(float *window, int size) +{ + for (int i = 0; i < size; i++) { + float value = 0.5f * (1.f - cosf(2.f * M_PI * i / size)); + + window[i] = value; + } +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + AudioDRCContext *s = ctx->priv; + float scale; + int ret; + + s->fft_size = inlink->sample_rate > 100000 ? 1024 : inlink->sample_rate > 50000 ? 512 : 256; + s->overlap = s->fft_size / 4; + + s->window = av_calloc(s->fft_size, sizeof(*s->window)); + if (!s->window) + return AVERROR(ENOMEM); + + s->drc_frame = ff_get_audio_buffer(inlink, s->fft_size * 2); + s->energy = ff_get_audio_buffer(inlink, s->fft_size / 2 + 1); + s->envelope = ff_get_audio_buffer(inlink, s->fft_size / 2 + 1); + s->factors = ff_get_audio_buffer(inlink, s->fft_size / 2 + 1); + s->in_buffer = ff_get_audio_buffer(inlink, s->fft_size * 2); + s->in_frame = ff_get_audio_buffer(inlink, s->fft_size * 2); + s->out_dist_frame = ff_get_audio_buffer(inlink, s->fft_size * 2); + s->spectrum_buf = ff_get_audio_buffer(inlink, s->fft_size * 2); + s->target_gain = ff_get_audio_buffer(inlink, s->fft_size / 2 + 1); + s->windowed_frame = ff_get_audio_buffer(inlink, s->fft_size * 2); + if (!s->in_buffer || !s->in_frame || !s->target_gain || + !s->out_dist_frame || !s->windowed_frame || !s->envelope || + !s->drc_frame || !s->spectrum_buf || !s->energy || !s->factors) + return AVERROR(ENOMEM); + + generate_hann_window(s->window, s->fft_size); + + s->channels = inlink->ch_layout.nb_channels; + + s->tx_ctx = av_calloc(s->channels, sizeof(*s->tx_ctx)); + s->itx_ctx = av_calloc(s->channels, sizeof(*s->itx_ctx)); + if (!s->tx_ctx || !s->itx_ctx) + return AVERROR(ENOMEM); + + for (int ch = 0; ch < s->channels; ch++) { + scale = 1.f; + ret = av_tx_init(&s->tx_ctx[ch], &s->tx_fn, AV_TX_FLOAT_RDFT, 0, s->fft_size, &scale, 0); + if (ret < 0) + return ret; + + scale = 1.f / s->fft_size; + ret = av_tx_init(&s->itx_ctx[ch], &s->itx_fn, AV_TX_FLOAT_RDFT, 1, s->fft_size, &scale, 0); + if (ret < 0) + return ret; + } + + s->var_values[VAR_SR] = inlink->sample_rate; + s->var_values[VAR_NB_CHANNELS] = s->channels; + + return av_expr_parse(&s->expr, s->expr_str, var_names, NULL, NULL, + NULL, NULL, 0, ctx); +} + +static void apply_window(AudioDRCContext *s, + const float *in_frame, float *out_frame, const int add_to_out_frame) +{ + const float *window = s->window; + const int fft_size = s->fft_size; + + if (add_to_out_frame) { + for (int i = 0; i < fft_size; i++) + out_frame[i] += in_frame[i] * window[i]; + } else { + for (int i = 0; i < fft_size; i++) + out_frame[i] = in_frame[i] * window[i]; + } +} + +static float sqrf(float x) +{ + return x * x; +} + +static void get_energy(AVFilterContext *ctx, + int len, + float *energy, + const float *spectral) +{ + for (int n = 0; n < len; n++) + energy[n] = 10.f * log10f(sqrf(spectral[2 * n]) + sqrf(spectral[2 * n + 1])); +} + +static void get_target_gain(AVFilterContext *ctx, + int len, + float *gain, + const float *energy, + double *var_values, + float fx) +{ + AudioDRCContext *s = ctx->priv; + + for (int n = 0; n < len; n++) { + const float Xg = energy[n]; + + var_values[VAR_G] = Xg; + var_values[VAR_F] = n * fx; + + gain[n] = av_expr_eval(s->expr, var_values, s); + } +} + +static void get_envelope(AVFilterContext *ctx, + int len, + float *envelope, + const float *energy, + const float *gain) +{ + AudioDRCContext *s = ctx->priv; + const float release = s->release; + const float attack = s->attack; + + for (int n = 0; n < len; n++) { + const float Bg = gain[n] - energy[n]; + const float Vg = envelope[n]; + + if (Bg > Vg) { + envelope[n] = attack * Vg + (1.f - attack) * Bg; + } else if (Bg <= Vg) { + envelope[n] = release * Vg + (1.f - release) * Bg; + } else { + envelope[n] = 0.f; + } + } +} + +static void get_factors(AVFilterContext *ctx, + int len, + float *factors, + const float *envelope) +{ + for (int n = 0; n < len; n++) + factors[n] = sqrtf(ff_exp10f(envelope[n] / 10.f)); +} + +static void apply_factors(AVFilterContext *ctx, + int len, + float *spectrum, + const float *factors) +{ + for (int n = 0; n < len; n++) { + spectrum[2*n+0] *= factors[n]; + spectrum[2*n+1] *= factors[n]; + } +} + +static void feed(AVFilterContext *ctx, int ch, + const float *in_samples, float *out_samples, + float *in_frame, float *out_dist_frame, + float *windowed_frame, float *drc_frame, + float *spectrum_buf, float *energy, + float *target_gain, float *envelope, + float *factors) +{ + AudioDRCContext *s = ctx->priv; + double var_values[VAR_VARS_NB]; + const int fft_size = s->fft_size; + const int nb_coeffs = s->fft_size / 2 + 1; + const int overlap = s->overlap; + const float fx = s->var_values[VAR_SR] * 0.5f / nb_coeffs; + + memcpy(var_values, s->var_values, sizeof(var_values)); + + var_values[VAR_CH] = ch; + + // shift in/out buffers + memmove(in_frame, in_frame + overlap, (fft_size - overlap) * sizeof(*in_frame)); + memmove(out_dist_frame, out_dist_frame + overlap, (fft_size - overlap) * sizeof(*out_dist_frame)); + + memcpy(in_frame + fft_size - overlap, in_samples, sizeof(*in_frame) * overlap); + memset(out_dist_frame + fft_size - overlap, 0, sizeof(*out_dist_frame) * overlap); + + apply_window(s, in_frame, windowed_frame, 0); + s->tx_fn(s->tx_ctx[ch], spectrum_buf, windowed_frame, sizeof(float)); + + get_energy(ctx, nb_coeffs, energy, spectrum_buf); + get_target_gain(ctx, nb_coeffs, target_gain, energy, var_values, fx); + get_envelope(ctx, nb_coeffs, envelope, energy, target_gain); + get_factors(ctx, nb_coeffs, factors, envelope); + apply_factors(ctx, nb_coeffs, spectrum_buf, factors); + + s->itx_fn(s->itx_ctx[ch], drc_frame, spectrum_buf, sizeof(AVComplexFloat)); + + apply_window(s, drc_frame, out_dist_frame, 1); + + // 4 times overlap with squared hanning window results in 1.5 time increase in amplitude + if (!ctx->is_disabled) { + for (int i = 0; i < overlap; i++) + out_samples[i] = out_dist_frame[i] / 1.5f; + } else { + memcpy(out_samples, in_frame, sizeof(*out_samples) * overlap); + } +} + +static int drc_channel(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int ch) +{ + AudioDRCContext *s = ctx->priv; + const float *src = (const float *)in->extended_data[ch]; + float *in_buffer = (float *)s->in_buffer->extended_data[ch]; + float *dst = (float *)out->extended_data[ch]; + + memcpy(in_buffer, src, sizeof(*in_buffer) * s->overlap); + + feed(ctx, ch, in_buffer, dst, + (float *)(s->in_frame->extended_data[ch]), + (float *)(s->out_dist_frame->extended_data[ch]), + (float *)(s->windowed_frame->extended_data[ch]), + (float *)(s->drc_frame->extended_data[ch]), + (float *)(s->spectrum_buf->extended_data[ch]), + (float *)(s->energy->extended_data[ch]), + (float *)(s->target_gain->extended_data[ch]), + (float *)(s->envelope->extended_data[ch]), + (float *)(s->factors->extended_data[ch])); + + return 0; +} + +static int drc_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + AudioDRCContext *s = ctx->priv; + AVFrame *out = arg; + const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs; + const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs; + + for (int ch = start; ch < end; ch++) + drc_channel(ctx, s->in, out, ch); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + AudioDRCContext *s = ctx->priv; + AVFrame *out; + int ret; + + out = ff_get_audio_buffer(outlink, s->overlap); + if (!out) { + ret = AVERROR(ENOMEM); + goto fail; + } + + s->var_values[VAR_SN] = outlink->sample_count_in; + s->var_values[VAR_T] = s->var_values[VAR_SN] * (double)1/outlink->sample_rate; + + s->in = in; + av_frame_copy_props(out, in); + ff_filter_execute(ctx, drc_channels, out, NULL, + FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); + + out->pts = in->pts; + out->nb_samples = in->nb_samples; + ret = ff_filter_frame(outlink, out); +fail: + av_frame_free(&in); + s->in = NULL; + return ret < 0 ? ret : 0; +} + +static int activate(AVFilterContext *ctx) +{ + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + AudioDRCContext *s = ctx->priv; + AVFrame *in = NULL; + int ret = 0, status; + int64_t pts; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + ret = ff_inlink_consume_samples(inlink, s->overlap, s->overlap, &in); + if (ret < 0) + return ret; + + if (ret > 0) { + s->attack = expf(-1.f / (s->attack_ms * inlink->sample_rate / 1000.f)); + s->release = expf(-1.f / (s->release_ms * inlink->sample_rate / 1000.f)); + + return filter_frame(inlink, in); + } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { + ff_outlink_set_status(outlink, status, pts); + return 0; + } else { + if (ff_inlink_queued_samples(inlink) >= s->overlap) { + ff_filter_set_ready(ctx, 10); + } else if (ff_outlink_frame_wanted(outlink)) { + ff_inlink_request_frame(inlink); + } + return 0; + } +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + AudioDRCContext *s = ctx->priv; + + av_expr_free(s->expr); + s->expr = NULL; + + av_freep(&s->window); + + av_frame_free(&s->drc_frame); + av_frame_free(&s->energy); + av_frame_free(&s->envelope); + av_frame_free(&s->factors); + av_frame_free(&s->in_buffer); + av_frame_free(&s->in_frame); + av_frame_free(&s->out_dist_frame); + av_frame_free(&s->spectrum_buf); + av_frame_free(&s->target_gain); + av_frame_free(&s->windowed_frame); + + for (int ch = 0; ch < s->channels; ch++) { + if (s->tx_ctx) + av_tx_uninit(&s->tx_ctx[ch]); + if (s->itx_ctx) + av_tx_uninit(&s->itx_ctx[ch]); + } + + av_freep(&s->tx_ctx); + av_freep(&s->itx_ctx); +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_input, + }, +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + }, +}; + +const AVFilter ff_af_adrc = { + .name = "adrc", + .description = NULL_IF_CONFIG_SMALL("Audio Spectral Dynamic Range Controller."), + .priv_size = sizeof(AudioDRCContext), + .priv_class = &adrc_class, + .uninit = uninit, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), + FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP), + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | + AVFILTER_FLAG_SLICE_THREADS, + .activate = activate, + .process_command = ff_filter_process_command, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 21f25da499..3ff20e76ce 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -35,6 +35,7 @@ extern const AVFilter ff_af_adecorrelate; extern const AVFilter ff_af_adelay; extern const AVFilter ff_af_adenorm; extern const AVFilter ff_af_aderivative; +extern const AVFilter ff_af_adrc; extern const AVFilter ff_af_adynamicequalizer; extern const AVFilter ff_af_adynamicsmooth; extern const AVFilter ff_af_aecho; -- 2.37.2