From patchwork Mon May 14 10:12:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 8949 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp3878427jad; Mon, 14 May 2018 03:18:51 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrwQHlcDjsOvy26C0/ggnAUlFJVRQe2+nt3TLKkRm/yf1em9rwN2JMa5oBhtWW0g6fifQ5z X-Received: by 2002:adf:a219:: with SMTP id p25-v6mr6507819wra.19.1526293131761; Mon, 14 May 2018 03:18:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526293131; cv=none; d=google.com; s=arc-20160816; b=vVYg+ExTFIfg/rfu/Gix5W2Ei+pRfKieKaBQ6TSfnNYCeGalFzU03jzYZqISujnEyK rSs64wsN++03OfVsZvykzXzUNekN/MrG2R7N9JH6b5XSjrw3y0sfxKsrDP2/+9fUzWa7 dr4hfn+PzbYlOn0t7f+2DtERuHritQsdzT7OcDL0ay3vHigNjrrHRd1+DUlu5HI2D8Ol Uest4M8bTUoaoSnXjMOs9bi7u7qVOTY9wqRAwmDna6pAgNZA68x4Jf/+liHyocSoNJP6 P/kSf+Woq1ebz3RPVF9c/dqtfyCE4lET4jIR6za504CrWIgQHe/WguHtyooD+biS3qF2 XheA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=0aPPYBEbaENmZecxjbS72XDcARlGJau/bDROQeeGaPc=; b=AhVQqipwhX9to7uNlICXkdNh/4MA0l5diDXhkwLLLLlPssv15ibQrLEjdL5plsvXHG u7w219qQJTg/gn3Mzgg4/RvbeB9s+VYb7txn/nJFnHHdxeCmsuREj70ICuPpP27mk4Vt JTtG/dNuU7i/hEUZQvvGKtssXCYCY0hSygMpZpzBqoT/xh/Catq4tgbaLT47Vl4qFbgg 9nLCT+Op7mBc7JWA7a1R6dmzm/Usf6Vb61s+Y7lVKQdQQu3zeHZFsEkhZ1oLU21GXdwo knbK8RCULdGMoBDxpv3h3tBnODNQTKjaZuGvzW2pufecHjv8tpiV/TbUFpDuPqylGA/P g4yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=gP80j3No; 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 q76-v6si8115280wrb.269.2018.05.14.03.18.51; Mon, 14 May 2018 03:18:51 -0700 (PDT) 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=20161025 header.b=gP80j3No; 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 5AB68689F16; Mon, 14 May 2018 13:18:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5CF4F689EC0 for ; Mon, 14 May 2018 13:18:05 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id w194-v6so12437294wmf.2 for ; Mon, 14 May 2018 03:18:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=9iOPqWrPdoo3xoJVwtDLEWbvyGBccf4pCxeQMuZZ/64=; b=gP80j3NojN+T6VOgBTKMDFpxX4wkVR33Fom95uYFDC6OgfXZCQiRvxpiiPoG0jPK7W LuDeAohZJcU3WRysPtlydN5ZmxFYmGWsthgkVZ5dUshVCSBEsoPtw7kbfm8nMdll9Pis X+qN9obCdwQNYHEvkkAvf5a8Dd1AX0+lPUV0jcWBnhF2o3u0D/ucAp1AzYM6e0+YIF4P ZLypt+bUPE/bOSJon5mCQQqoxC8NKV3VXHr7lPGorTK3d5QbVD28xX8TJMoG9/m1QnGf y/ynU6Erjj95fy3Vg7dXtbQzSeceNnZ5OokdajazKwHhZ/nGMluXlUbqOLTexMnO3NAX fG7g== 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=9iOPqWrPdoo3xoJVwtDLEWbvyGBccf4pCxeQMuZZ/64=; b=ITjmdlfahKMgCplqEsUAyM2Eo1im7/eCHNkNteFcqIHEu2rk2fKSmbH1g4sW+43NWW dWjutSpv6TOtGmBW3RQWDOpfN6kOOcHeJ78JCd1vwwn9xhdwG+1uXa+/ClplWKX+QUmn ss7/n2pKz+al6irikVmTIc5Fnb4omSbWmrvwtKMNY1AV1DROHZOBMfiJnniiZiymaThI 0sogp+NFxv9jDny8XOWL2Mo0MRekaBBOZttjyArtdg3T2MGpCc1u37DDt+Wii2AVwxBG 3rL3s4IbVmq3cctYj9s/iseZ9WTDc7mtzvJVoAPX7JgabMgMZ86K/YjPGdTsprXqSnOd IRQw== X-Gm-Message-State: ALKqPwflVERS+dYHmyLpXqhc9aQgyZUEla7bh280/MmQftso5jF1D/s0 4CmRhdzHo55q6Dq0K80WAmPc8A== X-Received: by 2002:a1c:3647:: with SMTP id d68-v6mr5044072wma.39.1526292784530; Mon, 14 May 2018 03:13:04 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id z10-v6sm11994958wre.43.2018.05.14.03.13.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 May 2018 03:13:03 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 14 May 2018 12:12:40 +0200 Message-Id: <20180514101240.23801-1-onemda@gmail.com> X-Mailer: git-send-email 2.11.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add aderivative and aintegral 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 | 6 ++ libavfilter/Makefile | 2 + libavfilter/af_aderivative.c | 207 +++++++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 2 + 4 files changed, 217 insertions(+) create mode 100644 libavfilter/af_aderivative.c diff --git a/doc/filters.texi b/doc/filters.texi index 30982cb6ab..ba31ed1316 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -585,6 +585,12 @@ adelay=0|500S|700S @end example @end itemize +@section aderivative, aintegral + +Compute derivative/integral of audio stream. + +Applying both filters one after another produces original audio. + @section aecho Apply echoing to the input audio. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index b2d6756e79..717aa83359 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -35,6 +35,8 @@ OBJS-$(CONFIG_ACOPY_FILTER) += af_acopy.o OBJS-$(CONFIG_ACROSSFADE_FILTER) += af_afade.o OBJS-$(CONFIG_ACRUSHER_FILTER) += af_acrusher.o OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o +OBJS-$(CONFIG_ADERIVATIVE_FILTER) += af_aderivative.o +OBJS-$(CONFIG_AINTEGRAL_FILTER) += af_aderivative.o OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o diff --git a/libavfilter/af_aderivative.c b/libavfilter/af_aderivative.c new file mode 100644 index 0000000000..a591515cbf --- /dev/null +++ b/libavfilter/af_aderivative.c @@ -0,0 +1,207 @@ +/* + * 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 "audio.h" +#include "avfilter.h" +#include "internal.h" + +typedef struct ADerivativeContext { + const AVClass *class; + AVFrame *prev; + void (*filter)(void **dst, void **prv, const void **src, + int nb_samples, int channels); +} ADerivativeContext; + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats = NULL; + AVFilterChannelLayouts *layouts = NULL; + static const enum AVSampleFormat derivative_sample_fmts[] = { + AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_FLTP, + AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_DBLP, + AV_SAMPLE_FMT_NONE + }; + static const enum AVSampleFormat integral_sample_fmts[] = { + AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, + AV_SAMPLE_FMT_NONE + }; + int ret; + + formats = ff_make_format_list(strcmp(ctx->filter->name, "aintegral") ? + derivative_sample_fmts : integral_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); +} + +#define DERIVATIVE(name, type) \ +static void aderivative_## name ##p(void **d, void **p, const void **s, \ + int nb_samples, int channels) \ +{ \ + int n, c; \ + \ + for (c = 0; c < channels; c++) { \ + const type *src = s[c]; \ + type *dst = d[c]; \ + type *prv = p[c]; \ + \ + for (n = 0; n < nb_samples; n++) { \ + const type current = src[n]; \ + \ + dst[n] = current - prv[0]; \ + prv[0] = current; \ + } \ + } \ +} + +DERIVATIVE(flt, float) +DERIVATIVE(dbl, double) +DERIVATIVE(s16, int16_t) +DERIVATIVE(s32, int32_t) + +#define INTEGRAL(name, type) \ +static void aintegral_## name ##p(void **d, void **p, const void **s, \ + int nb_samples, int channels) \ +{ \ + int n, c; \ + \ + for (c = 0; c < channels; c++) { \ + const type *src = s[c]; \ + type *dst = d[c]; \ + type *prv = p[c]; \ + \ + for (n = 0; n < nb_samples; n++) { \ + const type current = src[n]; \ + \ + dst[n] = current + prv[0]; \ + prv[0] = dst[n]; \ + } \ + } \ +} + +INTEGRAL(flt, float) +INTEGRAL(dbl, double) + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ADerivativeContext *s = ctx->priv; + + switch (inlink->format) { + case AV_SAMPLE_FMT_FLTP: s->filter = aderivative_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = aderivative_dblp; break; + case AV_SAMPLE_FMT_S32P: s->filter = aderivative_s32p; break; + case AV_SAMPLE_FMT_S16P: s->filter = aderivative_s16p; break; + } + + if (strcmp(ctx->filter->name, "aintegral")) + return 0; + + switch (inlink->format) { + case AV_SAMPLE_FMT_FLTP: s->filter = aintegral_fltp; break; + case AV_SAMPLE_FMT_DBLP: s->filter = aintegral_dblp; break; + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + ADerivativeContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples); + + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + + if (!s->prev) { + s->prev = ff_get_audio_buffer(inlink, 1); + if (!s->prev) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + } + + s->filter((void **)out->extended_data, (void **)s->prev->extended_data, (const void **)in->extended_data, + in->nb_samples, in->channels); + + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + ADerivativeContext *s = ctx->priv; + + av_frame_free(&s->prev); +} + +static const AVFilterPad aderivative_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad aderivative_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + }, + { NULL } +}; + +AVFilter ff_af_aderivative = { + .name = "aderivative", + .description = NULL_IF_CONFIG_SMALL("Compute derivative of input audio."), + .query_formats = query_formats, + .priv_size = sizeof(ADerivativeContext), + .uninit = uninit, + .inputs = aderivative_inputs, + .outputs = aderivative_outputs, +}; + +AVFilter ff_af_aintegral = { + .name = "aintegral", + .description = NULL_IF_CONFIG_SMALL("Compute integral of input audio."), + .query_formats = query_formats, + .priv_size = sizeof(ADerivativeContext), + .uninit = uninit, + .inputs = aderivative_inputs, + .outputs = aderivative_outputs, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index f28f6e47ee..5d3ed0a8a2 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -30,6 +30,7 @@ extern AVFilter ff_af_acopy; extern AVFilter ff_af_acrossfade; extern AVFilter ff_af_acrusher; extern AVFilter ff_af_adelay; +extern AVFilter ff_af_aderivative; extern AVFilter ff_af_aecho; extern AVFilter ff_af_aemphasis; extern AVFilter ff_af_aeval; @@ -39,6 +40,7 @@ extern AVFilter ff_af_afir; extern AVFilter ff_af_aformat; extern AVFilter ff_af_agate; extern AVFilter ff_af_aiir; +extern AVFilter ff_af_aintegral; extern AVFilter ff_af_ainterleave; extern AVFilter ff_af_alimiter; extern AVFilter ff_af_allpass;