From patchwork Sun May 1 13:44:10 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: 35532 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a885:b0:7f:4be2:bd17 with SMTP id ca5csp1341676pzb; Sun, 1 May 2022 06:41:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6Dc/UUEc4yBsbwRoOl/CFoiMP6trzz5pa/E3SqxLemoQmoQfZ9ir6Q3GmXTYVlLdpLJOA X-Received: by 2002:a05:6402:268f:b0:423:e68b:1942 with SMTP id w15-20020a056402268f00b00423e68b1942mr8999864edd.73.1651412514331; Sun, 01 May 2022 06:41:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651412514; cv=none; d=google.com; s=arc-20160816; b=VE2PZY6sNl73pe+7WvZgK91FcyXKvBQNqTJEkG7aLvWmNjx/p1lxNC10g2nbGAAbOu 8Jchljwfmgx6OldGN9uDucM5Ngt9kwJ7lYNbmvzYsOHctc3TuGIqYAEW7AAxsKoZrSzT SnUORPIbwtWLzS1qZUbZnzGgVCyvBy9qnyXmEwUVZzFTRFCPpelb+jOV8asmJ8K2vOld CVaW2vxgaw/kuosKNGNQJP7F1/rJMED2SO97gczV+ApcdmHY1UA6AJVvcNhAkyUqGkJc tOyRUuxmlBF4ptb2KXDNK59rDLWEYX8ffACJJ6cSj2LDVpTpuaJdSdire5ab5v8iRwie 51xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=fslfpKLuXfMt8B0gF8YUP2Y8JHixsnR+p51H0ob7nXM=; b=uuf/pfi73g6ktwF3XWMPgR3dleTp+ReX2XAzv6wKE49EjkUBAT3TwB805TZw2sJBQK 12FYr8/nFUHaKiolyuG1pjSVieGrA9uJN+MMqyhgzKoAKzaAybOqpyqKcgbomwuu16N+ qvZlpL5yS9ysLjVe+1F3MOEA1B+d8GCDILKy3qfjEgZeI9JtcaQvyRw0n4NwmKKjCiuY fDkfieHb/ExCdod5U2JkBtg1Z+x6tYv2lTb5ST/lmjwYiM00egWhzRvEOkkPkMU1h0gb xGPhyzz9+WtXdsKXWtw8UkpV7qj8uC2j1V3AkJan4L1lPcEVu9odS1FFIlokp5F0JTZn YFEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=i87ubz6m; 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 o18-20020a056402039200b00426026f52f0si9498201edv.58.2022.05.01.06.41.54; Sun, 01 May 2022 06:41:54 -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=20210112 header.b=i87ubz6m; 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 3D80968B2BC; Sun, 1 May 2022 16:41:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EABFB68ADD4 for ; Sun, 1 May 2022 16:41:43 +0300 (EEST) Received: by mail-wr1-f42.google.com with SMTP id k2so16582017wrd.5 for ; Sun, 01 May 2022 06:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=V8ngYw/9+suKH2IERxmIWfAf0ZINLyBCzPi3Hma5obY=; b=i87ubz6mmsRw5QFpBX4Ia2t47XNj7BYi2Ij6pZQ3WHJAw5Fo0N92e7LFfHxleKYc1M B0eF2v1ZumS9EiZCluBwJ0WZ8Li4v+D+/gVWYEU1PQ3+3nwENABiEQMvRd/dYXguZIMJ kyNOV2ytuZg6IkCE42bfdOKWzk+XQkwJwKDoFevvhyBMxClMAlZc00I2QKYneUti9KvL p2OnfraAIdll+eThmTNUD993zYJuJan+Egqxq3Ax7OuoyxKRF/KTnA85TrwZL0BexYne sxex6bml2AQutJllNc/qAqAE29WS+V53G1A4vWiTGdokkcZj+A/tG+iCCv3CMTaZhQoN N8gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=V8ngYw/9+suKH2IERxmIWfAf0ZINLyBCzPi3Hma5obY=; b=WeCjJ7ePiNMfOqLMyG59WMDoolX8Pa2G/kJI5hmHCoG6Hi4kqjqTWvGJp92okK4WHA I1Bky2lyH7DIrB5cBGySzTmpyQTR3Jr6ahulR6ZOUhWDcoICHhqiscTxBUVlYyuRp5Bo dPM8qBgsa5b7ElZrjLZDKarqAoZcmClC8gHJ80pnCpG4zDrzFqwddgFy7DfBWD34eUE2 n6vs6R0u/9de8RdlMTzpgXzFhtFVbaweQqXQ30mxvsf2SP9aVo2MoG+LVXoR0T+PsVnT ca43MuoyRhxsdn1ktneOZR3xyL3nJ5+6o8Mo/rIwQNVI1fa5tzb0UDaYriMvqlHNjrId yqyg== X-Gm-Message-State: AOAM531/S98WP5/b/5Xyw8AyL3d1mKBSqwnIAA97yGX0Nhvt94Fpeu7V NFfylRLFdHGamOr8xBhboFJVgcKkesY= X-Received: by 2002:a5d:48c8:0:b0:207:afc8:13fa with SMTP id p8-20020a5d48c8000000b00207afc813famr6354867wrs.487.1651412503297; Sun, 01 May 2022 06:41:43 -0700 (PDT) Received: from localhost.localdomain ([95.168.120.43]) by smtp.gmail.com with ESMTPSA id s2-20020adf8902000000b0020c5253d8f8sm4697249wrs.68.2022.05.01.06.41.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 May 2022 06:41:42 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 1 May 2022 15:44:10 +0200 Message-Id: <20220501134410.319618-1-onemda@gmail.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add multiply video 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: DCmRqz95/dzE Signed-off-by: Paul B Mahol --- doc/filters.texi | 23 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_multiply.c | 253 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 278 insertions(+) create mode 100644 libavfilter/vf_multiply.c diff --git a/doc/filters.texi b/doc/filters.texi index 478a75bd3a..572227161a 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -16635,6 +16635,29 @@ with the reference file @file{ref.mpg}. ffmpeg -i main.mpg -i ref.mpg -lavfi msad -f null - @end example +@section multiply +Multiply first video stream pixels values with second video stream pixels values. + +The filter accepts the following options: + +@table @option +@item scale +Set the scale applied to second video stream. By default is @code{1}. +Allowed range is from @code{0} to @code{9}. + +@item offset +Set the offset applied to second video stream. By default is @code{0.5}. +Allowed range is from @code{-1} to @code{1}. + +@item planes +Specify planes from input video stream that will be processed. +By default all planes are processed. +@end table + +@subsection Commands + +This filter supports same @ref{commands} as options. + @section negate Negate (invert) the input video. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5d95798d70..ad3e77dee1 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -368,6 +368,7 @@ OBJS-$(CONFIG_MIX_FILTER) += vf_mix.o framesync.o OBJS-$(CONFIG_MONOCHROME_FILTER) += vf_monochrome.o OBJS-$(CONFIG_MORPHO_FILTER) += vf_morpho.o OBJS-$(CONFIG_MPDECIMATE_FILTER) += vf_mpdecimate.o +OBJS-$(CONFIG_MULTIPLY_FILTER) += vf_multiply.o OBJS-$(CONFIG_NEGATE_FILTER) += vf_negate.o OBJS-$(CONFIG_NLMEANS_FILTER) += vf_nlmeans.o OBJS-$(CONFIG_NLMEANS_OPENCL_FILTER) += vf_nlmeans_opencl.o opencl.o opencl/nlmeans.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 61c6148695..d9e018f342 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -352,6 +352,7 @@ extern const AVFilter ff_vf_monochrome; extern const AVFilter ff_vf_morpho; extern const AVFilter ff_vf_mpdecimate; extern const AVFilter ff_vf_msad; +extern const AVFilter ff_vf_multiply; extern const AVFilter ff_vf_negate; extern const AVFilter ff_vf_nlmeans; extern const AVFilter ff_vf_nlmeans_opencl; diff --git a/libavfilter/vf_multiply.c b/libavfilter/vf_multiply.c new file mode 100644 index 0000000000..979b885eb1 --- /dev/null +++ b/libavfilter/vf_multiply.c @@ -0,0 +1,253 @@ +/* + * 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 "libavutil/imgutils.h" +#include "libavutil/pixdesc.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" +#include "framesync.h" + +typedef struct MultiplyContext { + const AVClass *class; + + float offset; + float scale; + int planes; + + int linesize[4]; + int nb_planes; + + FFFrameSync fs; +} MultiplyContext; + +#define OFFSET(x) offsetof(MultiplyContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +typedef struct ThreadData { + AVFrame *src, *ref, *dst; +} ThreadData; + +static const AVOption multiply_options[] = { + { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0., 9., FLAGS }, + { "offset", "set offset", OFFSET(offset), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, -1., 1., FLAGS }, + { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=0xF}, 0., 0xF, FLAGS }, + { NULL } +}; + +static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, + AV_PIX_FMT_NONE +}; + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + MultiplyContext *s = ctx->priv; + int ret; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) + return ret; + + return 0; +} + +static void multiply(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst, + float scale, float offset, int w) +{ + const float *src = (const float *)ssrc; + const float *ref = (const float *)rref; + float *dst = (float *)ddst; + + for (int x = 0; x < w; x++) { + const float factor = (ref[x] + offset) * scale; + + dst[x] = src[x] * factor; + } +} + +static int multiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + MultiplyContext *s = ctx->priv; + const float offset = s->offset; + const float scale = s->scale; + ThreadData *td = arg; + + for (int p = 0; p < s->nb_planes; p++) { + const ptrdiff_t src_linesize = td->src->linesize[p]; + const ptrdiff_t ref_linesize = td->ref->linesize[p]; + const ptrdiff_t dst_linesize = td->dst->linesize[p]; + const int w = td->src->width; + const int h = td->src->height; + const int slice_start = (h * jobnr) / nb_jobs; + const int slice_end = (h * (jobnr+1)) / nb_jobs; + const uint8_t *src = td->src->data[p] + slice_start * src_linesize; + const uint8_t *ref = td->ref->data[p] + slice_start * ref_linesize; + uint8_t *dst = td->dst->data[p] + slice_start * dst_linesize; + + if (!((1 << p) & s->planes)) { + av_image_copy_plane(dst, dst_linesize, ref, ref_linesize, + s->linesize[p], slice_end - slice_start); + continue; + } + + for (int y = slice_start; y < slice_end; y++) { + multiply(src, ref, dst, scale, offset, w); + + dst += dst_linesize; + src += src_linesize; + ref += ref_linesize; + } + } + + return 0; +} + +static int process_frame(FFFrameSync *fs) +{ + AVFilterContext *ctx = fs->parent; + MultiplyContext *s = fs->opaque; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out, *src, *ref; + int ret; + + if ((ret = ff_framesync_get_frame(&s->fs, 0, &src, 0)) < 0 || + (ret = ff_framesync_get_frame(&s->fs, 1, &ref, 0)) < 0) + return ret; + + if (ctx->is_disabled) { + out = av_frame_clone(src); + if (!out) + return AVERROR(ENOMEM); + } else { + ThreadData td; + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) + return AVERROR(ENOMEM); + av_frame_copy_props(out, src); + + td.src = src; + td.ref = ref; + td.dst = out; + + ff_filter_execute(ctx, multiply_slice, &td, NULL, + FFMIN(outlink->h, ff_filter_get_nb_threads(ctx))); + } + out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base); + + return ff_filter_frame(outlink, out); +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + MultiplyContext *s = ctx->priv; + AVFilterLink *source = ctx->inputs[0]; + AVFilterLink *ref = ctx->inputs[1]; + FFFrameSyncIn *in; + int ret; + + if (source->w != ref->w || source->h != ref->h) { + av_log(ctx, AV_LOG_ERROR, "First input link %s parameters " + "(size %dx%d) do not match the corresponding " + "second input link %s parameters (%dx%d)\n", + ctx->input_pads[0].name, source->w, source->h, + ctx->input_pads[1].name, ref->w, ref->h); + return AVERROR(EINVAL); + } + + outlink->w = source->w; + outlink->h = source->h; + outlink->sample_aspect_ratio = source->sample_aspect_ratio; + outlink->frame_rate = source->frame_rate; + + if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0) + return ret; + + in = s->fs.in; + in[0].time_base = source->time_base; + in[1].time_base = ref->time_base; + in[0].sync = 1; + in[0].before = EXT_STOP; + in[0].after = EXT_INFINITY; + in[1].sync = 1; + in[1].before = EXT_STOP; + in[1].after = EXT_INFINITY; + s->fs.opaque = s; + s->fs.on_event = process_frame; + + ret = ff_framesync_configure(&s->fs); + outlink->time_base = s->fs.time_base; + + return ret; +} + +static int activate(AVFilterContext *ctx) +{ + MultiplyContext *s = ctx->priv; + return ff_framesync_activate(&s->fs); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + MultiplyContext *s = ctx->priv; + + ff_framesync_uninit(&s->fs); +} + +static const AVFilterPad multiply_inputs[] = { + { + .name = "source", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, + { + .name = "factor", + .type = AVMEDIA_TYPE_VIDEO, + }, +}; + +static const AVFilterPad multiply_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, +}; + +AVFILTER_DEFINE_CLASS(multiply); + +const AVFilter ff_vf_multiply = { + .name = "multiply", + .description = NULL_IF_CONFIG_SMALL("Multiply first video stream with second video stream."), + .priv_class = &multiply_class, + .priv_size = sizeof(MultiplyContext), + .uninit = uninit, + .activate = activate, + FILTER_INPUTS(multiply_inputs), + FILTER_OUTPUTS(multiply_outputs), + FILTER_PIXFMTS_ARRAY(pix_fmts), + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};