From patchwork Wed Sep 12 10:42:44 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: 10303 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp4690156jap; Wed, 12 Sep 2018 03:43:23 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbLHn0s/A+KWHB9ADo7qQ+tpcy46qs1qDJXvVnJ0PzizYpeNSNj8AeTJo9zEIbBVZ5JD4Ng X-Received: by 2002:a1c:8e81:: with SMTP id q123-v6mr1156287wmd.56.1536749003869; Wed, 12 Sep 2018 03:43:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536749003; cv=none; d=google.com; s=arc-20160816; b=uah20aH1FgPbQPjRd9HSK+R4LZpuutGYch9uOhF6+cpI1AHI3F2OCh5XeTE+IT6LG1 Y7wc1nJVfnseTGf70fJPEeG8imT2Yqf55MAmyhTBS1SridfMttb5Zj+4PWj2TszRcovQ nu3OPXbChc2iZjjpoFT0FBoWY/anEJe1jmUZvZzCu7HLJUxy+ANKr4Sw/HG5UPLc4zVI Udxr4B+9RMLw/x99Wusc20Gssjb+T8QfFbOSI+w4utr9Q15NhvzuWEwy1A4hW3RfWduQ 1e8uE7zP/lWRDsMTv4Z9CrKJSwAkfwpGKCwi4oXtm1LC0+cpx1KuA9CgsWOUCZ6+7qfW h9iQ== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=ERMubNO2SB1KjDvIuXijZvXCvU/1Ss2QTjeq88wPn7U=; b=aMqCkiXmaylvLeciiFwRUm9QPmzGiRGa64Z3AMG27je3fTmi7wZfAxVuKpOUTASTXH 7Qyu6n6cGH7cm3WKMroHPu474gCBA0ySRoXVMpStuai4qTURn8y8IW4MrxrxMi0yc2jg AT2kuOotHZfX8juuV83EP2nt61jgyK/97ouW7aEdM3/jvIqT7dQ9h2LU/O+Cehhv4wx4 se9I9niBzxIM5KiAL3q2cBnG0PIFPTvqonNHjGb3Q9ZnwhtHEe2uI6QFOs4ZoDjR7n19 kcWuzGAyQz/aTEDeodWrX24XZFNyXpHiMFFJW3jD1gKnv8dd1S1f+ci4JEajF8p9PnYG 5ZtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=mgGwqXcE; 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 y1-v6si633562wrq.124.2018.09.12.03.43.23; Wed, 12 Sep 2018 03:43:23 -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=mgGwqXcE; 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 6BE8668A544; Wed, 12 Sep 2018 13:43:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 88E2568A539 for ; Wed, 12 Sep 2018 13:42:56 +0300 (EEST) Received: by mail-ed1-f52.google.com with SMTP id h4-v6so1362536edi.6 for ; Wed, 12 Sep 2018 03:43:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=7MkGeXpg9ggcT1go53A1JxdCl6lV5r+vzZr8lsu6INA=; b=mgGwqXcE1gIpmBiFDz/wX+7AtecRu5IbypE4lVsXHqT93FZhY98plinenKrCpWFt5A hrNqSoWpJTdtF7jmm2ULC2OgnjaK+RKiNz2EPbqLRYqmNthi+xOGheuN0wGeljpAlhss yjvCTtLKajHZArvSOJaLr7/hoYOM1BCnwuHXlbh2YY/uTwYCg0W7ZcyxpuhqVelSJmH7 FHWTYxELttfPEBe9PpziVIjTIJO+WoqVJTIlrRQ/ky6xUnh5VKPne4USybdy5xVBldMo 9VToYX2fy15ZFy+u+dfDtT8Ee60W10vaLtryqyvihbMK2hc9sghv6LItKRtbJZelIQG1 q5UA== 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:in-reply-to :references; bh=7MkGeXpg9ggcT1go53A1JxdCl6lV5r+vzZr8lsu6INA=; b=Xnxny4tBcoV3J7Is+bd3/ZlTVdB7XiR0PuOmjuk2tQImE0O0P6bZXwToOZBZfwXNaf VRtn2Z3HpbnoFyVWfTSkvT4W7qGQjw4xmD6C022WDoD9y6OYF+MFP09XzmForfoMg0my B8B9k+9xu9Q3dPQfh7MzabDqT4a3MuDcJtI6yz0bjYUNh+JPsQJtd5AJmkU7+5DfZcUz 8ZS3AOIL0hSUfN5KwlKP1eMoAjfI8/d+zRv1p50BoTe9VfRyVWXS/UnwF2NK7rGpPcd2 gFRzgywUzCRs/ZljW/H9ZQJCCDQOGKDhJDSyFP1LMO80OCQWIYqo04QXEVRQ5Z7/3IYD kBeQ== X-Gm-Message-State: APzg51AGzVucEgWwUuYFylJk8Nx3wsVov4pa+vQBdcTHd1o6A+M36zaS eZisWLlx6sCqPsJiypsq0COMZ4B7 X-Received: by 2002:a50:8141:: with SMTP id 59-v6mr1988833edc.61.1536748986162; Wed, 12 Sep 2018 03:43:06 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id v14-v6sm496043edm.97.2018.09.12.03.43.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Sep 2018 03:43:05 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Sep 2018 12:42:44 +0200 Message-Id: <20180912104245.5326-2-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180912104245.5326-1-onemda@gmail.com> References: <20180912104245.5326-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/2] avfilter: add amultiply audio 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 --- libavfilter/Makefile | 1 + libavfilter/af_amultiply.c | 221 +++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 3 files changed, 223 insertions(+) create mode 100644 libavfilter/af_amultiply.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5b0462692a..f15e520d5d 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -58,6 +58,7 @@ OBJS-$(CONFIG_ALOOP_FILTER) += f_loop.o OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o OBJS-$(CONFIG_AMETADATA_FILTER) += f_metadata.o OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o +OBJS-$(CONFIG_AMULTIPLY_FILTER) += af_amultiply.o OBJS-$(CONFIG_ANEQUALIZER_FILTER) += af_anequalizer.o OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o OBJS-$(CONFIG_APAD_FILTER) += af_apad.o diff --git a/libavfilter/af_amultiply.c b/libavfilter/af_amultiply.c new file mode 100644 index 0000000000..6ec7ec006f --- /dev/null +++ b/libavfilter/af_amultiply.c @@ -0,0 +1,221 @@ +/* + * 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/common.h" +#include "libavutil/float_dsp.h" +#include "libavutil/opt.h" + +#define FF_INTERNAL_FIELDS 1 +#include "framequeue.h" + +#include "audio.h" +#include "avfilter.h" +#include "formats.h" +#include "filters.h" +#include "internal.h" + +typedef struct AudioMultiplyContext { + const AVClass *class; + + AVFrame *frames[2]; + int64_t pts; + int planes; + int channels; + int samples_align; + + AVFloatDSPContext *fdsp; +} AudioMultiplyContext; + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats; + AVFilterChannelLayouts *layouts; + static const enum AVSampleFormat sample_fmts[] = { + AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP, + AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP, + AV_SAMPLE_FMT_NONE + }; + int 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_make_format_list(sample_fmts); + if (!formats) + return AVERROR(ENOMEM); + ret = ff_set_common_formats(ctx, formats); + if (ret < 0) + return ret; + + formats = ff_all_samplerates(); + if (!formats) + return AVERROR(ENOMEM); + return ff_set_common_samplerates(ctx, formats); +} + +static int activate(AVFilterContext *ctx) +{ + AudioMultiplyContext *s = ctx->priv; + int i, ret, status; + int nb_samples; + int64_t pts; + + FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx); + + nb_samples = FFMIN(ff_framequeue_queued_samples(&ctx->inputs[0]->fifo), + ff_framequeue_queued_samples(&ctx->inputs[1]->fifo)); + for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) { + if (s->frames[i]) + continue; + + if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) { + ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]); + if (ret < 0) + return ret; + } + } + + if (nb_samples > 0 && s->frames[0] && s->frames[1]) { + AVFrame *out; + int plane_samples; + + if (av_sample_fmt_is_planar(ctx->inputs[0]->format)) + plane_samples = FFALIGN(nb_samples, s->samples_align); + else + plane_samples = FFALIGN(nb_samples * s->channels, s->samples_align); + + out = ff_get_audio_buffer(ctx->outputs[0], nb_samples); + if (!out) + return AVERROR(ENOMEM); + + out->pts = s->pts; + s->pts += nb_samples; + + if (av_get_packed_sample_fmt(ctx->inputs[0]->format) == AV_SAMPLE_FMT_FLT) { + for (i = 0; i < s->planes; i++) { + s->fdsp->vector_fmul((float *)out->extended_data[i], + (const float *)s->frames[0]->extended_data[i], + (const float *)s->frames[1]->extended_data[i], + plane_samples); + } + } else { + for (i = 0; i < s->planes; i++) { + s->fdsp->vector_dmul((double *)out->extended_data[i], + (const double *)s->frames[0]->extended_data[i], + (const double *)s->frames[1]->extended_data[i], + plane_samples); + } + } + emms_c(); + + av_frame_free(&s->frames[0]); + av_frame_free(&s->frames[1]); + + ret = ff_filter_frame(ctx->outputs[0], out); + if (ret < 0) + return ret; + } + + if (!nb_samples) { + for (i = 0; i < 2; i++) { + if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) { + ff_outlink_set_status(ctx->outputs[0], status, pts); + return 0; + } + } + } + + if (ff_outlink_frame_wanted(ctx->outputs[0])) { + for (i = 0; i < 2; i++) { + if (ff_framequeue_queued_samples(&ctx->inputs[i]->fifo) > 0) + continue; + ff_inlink_request_frame(ctx->inputs[i]); + return 0; + } + } + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + AudioMultiplyContext *s = ctx->priv; + AVFilterLink *inlink = ctx->inputs[0]; + + s->channels = inlink->channels; + s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->channels : 1; + s->samples_align = 16; + + return 0; +} + +static av_cold int init(AVFilterContext *ctx) +{ + AudioMultiplyContext *s = ctx->priv; + + s->fdsp = avpriv_float_dsp_alloc(0); + if (!s->fdsp) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + AudioMultiplyContext *s = ctx->priv; + av_freep(&s->fdsp); +} + +static const AVFilterPad inputs[] = { + { + .name = "multiply0", + .type = AVMEDIA_TYPE_AUDIO, + }, + { + .name = "multiply1", + .type = AVMEDIA_TYPE_AUDIO, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_af_amultiply = { + .name = "amultiply", + .description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."), + .priv_size = sizeof(AudioMultiplyContext), + .init = init, + .uninit = uninit, + .activate = activate, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 10ac52b711..c467064783 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -51,6 +51,7 @@ extern AVFilter ff_af_aloop; extern AVFilter ff_af_amerge; extern AVFilter ff_af_ametadata; extern AVFilter ff_af_amix; +extern AVFilter ff_af_amultiply; extern AVFilter ff_af_anequalizer; extern AVFilter ff_af_anull; extern AVFilter ff_af_apad;