From patchwork Mon Nov 2 17:24:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 23339 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2823E44AF1F for ; Mon, 2 Nov 2020 20:24:24 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 043DE68AB33; Mon, 2 Nov 2020 20:24:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8BAFA68A609 for ; Mon, 2 Nov 2020 20:24:17 +0200 (EET) Received: by mail-lj1-f174.google.com with SMTP id x6so16110928ljd.3 for ; Mon, 02 Nov 2020 10:24:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=j1aRzePEwXDykgsBXFeAKToXl5jtwSZ+jTDZNPd7GLw=; b=sjMmGktdlX2ooe7C4Yas7AjuJOoiiqvKMQ3B+mcrSTWPP2NyEU1tXBaTUgmbU12VGK 8RKdCPFJp60HMtJettfoczUX/UZKIHx042SBLk65Sqb77QyhTGwiirfHuua0OvqSYpl4 ACzeIV3SVbYHXiWmJd9VvPAc509gdAU64tXX0gUzsrWvAiJEShNDojdUNKIDMuiMsbNM zeQs75syNNx2d4CMxh3LP1vRQt8F/T2hm0i8qwhXXqvMZqHCBby1sNsJjeyZbXTXpYnF 52u1vtkf9ghaqUdcwBYxjto4ravTPdrwt0rCI7W4Q+XtKG7jUR8goPDHlZGyiGmbFqSv oDnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=j1aRzePEwXDykgsBXFeAKToXl5jtwSZ+jTDZNPd7GLw=; b=W/kAGe7eWCM2T9SMN7wN8NpCvk7fqm33aXdeleOQKC7lPDc9L9LP6Pomi7Wqq62ICR uf+b7PbDzALV25uwsICIhdmkVia2rj2x2XM0uA1ND8EqBPUehyfFTCFNDKlWrleSGq/5 GpjekBp1zkO1qt9rQIOLQVqMUVImBaa8az6/Rj2vaGuz+EaRLNQ13qk9GO5NgypLnvdz ofbMtkI93++XwN7JI8B4ZCaisT6dImsP0qzW+PMmFwvAj0N5hlQC9CUHFFFwhVWAqELX TpDooNDxwpV0M3CbUpHuZ6Bt++oG8aTfpilrQrjk5u5XV4T8xGUvPgtletxjHwV5cXPa jDsw== X-Gm-Message-State: AOAM533KkGkf78wgj6j/YLM2Jr6N+zg2vo3gDNBPx4Sk8z88rJ9jQNzf or5BvQMOuJMsP1AQXMaPi4s0HI3MtNzsUA== X-Google-Smtp-Source: ABdhPJwg1ti5axogyTtAXV2KXqoul/ZUtNlWRkgVffbpdS3YqS4vFKwaXAfn3vqxfzdz2Q6FF/LoeQ== X-Received: by 2002:a17:906:8496:: with SMTP id m22mr3744148ejx.298.1604337906830; Mon, 02 Nov 2020 09:25:06 -0800 (PST) Received: from localhost.localdomain ([77.237.107.67]) by smtp.gmail.com with ESMTPSA id la9sm1299369ejb.121.2020.11.02.09.25.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 09:25:06 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 2 Nov 2020 18:24:52 +0100 Message-Id: <20201102172452.22606-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add adenorm filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 27 ++++ libavfilter/Makefile | 1 + libavfilter/af_adenorm.c | 308 +++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 337 insertions(+) create mode 100644 libavfilter/af_adenorm.c diff --git a/doc/filters.texi b/doc/filters.texi index d98c696f60..777d598c1d 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -731,6 +731,33 @@ adelay=delays=64S:all=1 @end example @end itemize +@section adenorm +Remedy denormals in audio by adding extremely low-level noise. + +A description of the accepted parameters follows. + +@table @option +@item level +Set level of added noise in dB. Default is @code{-351}. +Allowed range is from -451 to -90. + +@item type +Set type of added noise. + +@table @option +@item dc +Add DC signal. +@item ac +Add AC signal. +@item square +Add square signal. +@item pulse +Add pulse signal. +@end table + +@Default is @code{dc}. +@end table + @section aderivative, aintegral Compute derivative/integral of audio stream. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 1e60c55f6f..028fa50d47 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -41,6 +41,7 @@ OBJS-$(CONFIG_ACUE_FILTER) += f_cue.o OBJS-$(CONFIG_ADECLICK_FILTER) += af_adeclick.o OBJS-$(CONFIG_ADECLIP_FILTER) += af_adeclick.o OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o +OBJS-$(CONFIG_ADENORM_FILTER) += af_adenorm.o OBJS-$(CONFIG_ADERIVATIVE_FILTER) += af_aderivative.o OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o diff --git a/libavfilter/af_adenorm.c b/libavfilter/af_adenorm.c new file mode 100644 index 0000000000..e689fe556e --- /dev/null +++ b/libavfilter/af_adenorm.c @@ -0,0 +1,308 @@ +/* + * 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 "libavutil/avassert.h" +#include "libavutil/channel_layout.h" +#include "libavutil/opt.h" +#include "audio.h" +#include "avfilter.h" +#include "internal.h" + +enum FilterType { + DC_TYPE, + AC_TYPE, + SQ_TYPE, + PS_TYPE, + NB_TYPES, +}; + +typedef struct ADenormContext { + const AVClass *class; + + double level; + double level_db; + int type; + int64_t in_samples; + + void (*filter)(AVFilterContext *ctx, void *dst, + const void *src, int nb_samples); +} ADenormContext; + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats = NULL; + AVFilterChannelLayouts *layouts = NULL; + static const enum AVSampleFormat sample_fmts[] = { + AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, + AV_SAMPLE_FMT_NONE + }; + int ret; + + formats = ff_make_format_list(sample_fmts); + if (!formats) + return AVERROR(ENOMEM); + ret = ff_set_common_formats(ctx, formats); + if (ret < 0) + return ret; + + layouts = ff_all_channel_counts(); + if (!layouts) + return AVERROR(ENOMEM); + + ret = ff_set_common_channel_layouts(ctx, layouts); + if (ret < 0) + return ret; + + formats = ff_all_samplerates(); + return ff_set_common_samplerates(ctx, formats); +} + +static void dc_denorm_fltp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const float *src = (const float *)srcp; + float *dst = (float *)dstp; + const float dc = s->level; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc; + } +} + +static void dc_denorm_dblp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const double *src = (const double *)srcp; + double *dst = (double *)dstp; + const double dc = s->level; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc; + } +} + +static void ac_denorm_fltp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const float *src = (const float *)srcp; + float *dst = (float *)dstp; + const float dc = s->level; + const int64_t N = s->in_samples; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc * (((N + n) & 1) ? -1.f : 1.f); + } +} + +static void ac_denorm_dblp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const double *src = (const double *)srcp; + double *dst = (double *)dstp; + const double dc = s->level; + const int64_t N = s->in_samples; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc * (((N + n) & 1) ? -1. : 1.); + } +} + +static void sq_denorm_fltp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const float *src = (const float *)srcp; + float *dst = (float *)dstp; + const float dc = s->level; + const int64_t N = s->in_samples; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc * ((((N + n) >> 8) & 1) ? -1.f : 1.f); + } +} + +static void sq_denorm_dblp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const double *src = (const double *)srcp; + double *dst = (double *)dstp; + const double dc = s->level; + const int64_t N = s->in_samples; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc * ((((N + n) >> 8) & 1) ? -1. : 1.); + } +} + +static void ps_denorm_fltp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const float *src = (const float *)srcp; + float *dst = (float *)dstp; + const float dc = s->level; + const int64_t N = s->in_samples; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc * (((N + n) & 255) ? 0.f : 1.f); + } +} + +static void ps_denorm_dblp(AVFilterContext *ctx, void *dstp, + const void *srcp, int nb_samples) +{ + ADenormContext *s = ctx->priv; + const double *src = (const double *)srcp; + double *dst = (double *)dstp; + const double dc = s->level; + const int64_t N = s->in_samples; + + for (int n = 0; n < nb_samples; n++) { + dst[n] = src[n] + dc * (((N + n) & 255) ? 0. : 1.); + } +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + ADenormContext *s = ctx->priv; + + switch (s->type) { + case DC_TYPE: + switch (outlink->format) { + case AV_SAMPLE_FMT_FLTP: s->filter = dc_denorm_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = dc_denorm_dblp; break; + } + break; + case AC_TYPE: + switch (outlink->format) { + case AV_SAMPLE_FMT_FLTP: s->filter = ac_denorm_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = ac_denorm_dblp; break; + } + break; + case SQ_TYPE: + switch (outlink->format) { + case AV_SAMPLE_FMT_FLTP: s->filter = sq_denorm_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = sq_denorm_dblp; break; + } + break; + case PS_TYPE: + switch (outlink->format) { + case AV_SAMPLE_FMT_FLTP: s->filter = ps_denorm_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = ps_denorm_dblp; break; + } + break; + default: + av_assert0(0); + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + ADenormContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + + if (av_frame_is_writable(in)) { + out = in; + } else { + out = ff_get_audio_buffer(outlink, in->nb_samples); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + } + + s->level = exp(s->level_db / 20. * M_LN10); + for (int ch = 0; ch < inlink->channels; ch++) { + s->filter(ctx, out->extended_data[ch], + in->extended_data[ch], + in->nb_samples); + } + s->in_samples += in->nb_samples; + + if (out != in) + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, + char *res, int res_len, int flags) +{ + AVFilterLink *outlink = ctx->outputs[0]; + int ret; + + ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); + if (ret < 0) + return ret; + + return config_output(outlink); +} + +static const AVFilterPad adenorm_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad adenorm_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_output, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(ADenormContext, x) +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption adenorm_options[] = { + { "level", "set level", OFFSET(level_db), AV_OPT_TYPE_DOUBLE, {.dbl=-351}, -451, -90, FLAGS }, + { "type", "set type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=DC_TYPE}, 0, NB_TYPES-1, FLAGS, "type" }, + { "dc", NULL, 0, AV_OPT_TYPE_CONST, {.i64=DC_TYPE}, 0, 0, FLAGS, "type"}, + { "ac", NULL, 0, AV_OPT_TYPE_CONST, {.i64=AC_TYPE}, 0, 0, FLAGS, "type"}, + { "square",NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQ_TYPE}, 0, 0, FLAGS, "type"}, + { "pulse", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PS_TYPE}, 0, 0, FLAGS, "type"}, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(adenorm); + +AVFilter ff_af_adenorm = { + .name = "adenorm", + .description = NULL_IF_CONFIG_SMALL("Remedy denormals by adding extremely low-level noise."), + .query_formats = query_formats, + .priv_size = sizeof(ADenormContext), + .inputs = adenorm_inputs, + .outputs = adenorm_outputs, + .priv_class = &adenorm_class, + .process_command = process_command, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 801c53f7c0..4c671be329 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -34,6 +34,7 @@ extern AVFilter ff_af_acrusher; extern AVFilter ff_af_adeclick; extern AVFilter ff_af_adeclip; extern AVFilter ff_af_adelay; +extern AVFilter ff_af_adenorm; extern AVFilter ff_af_aderivative; extern AVFilter ff_af_aecho; extern AVFilter ff_af_aemphasis;