From patchwork Sun Apr 10 23:02:43 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: 35248 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:671c:b0:7c:62c8:b2d1 with SMTP id q28csp769028pzh; Sun, 10 Apr 2022 16:00:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxttxMnq/ck+gtay+xggtJhT0HjmW48Hgd25us58CtGjlS7OtanHalF0q/+Gj+ikpAnn+O7 X-Received: by 2002:a17:907:60d1:b0:6e8:807c:cdd8 with SMTP id hv17-20020a17090760d100b006e8807ccdd8mr4520607ejc.562.1649631635940; Sun, 10 Apr 2022 16:00:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649631635; cv=none; d=google.com; s=arc-20160816; b=UUDKVjn6OMaZ5lS9jjhW03RXOXKY17xTcJGaEzilnpZ25BF1/lNZHgqjTSa+Oz4Y5m /frJs1f7cLa66FK6Ix3NkOFSTWRnHTdXCKxoI8lvLLXDWePy1OqqQ/s8R52kiuDMvx1t FJy9BsUAUYB9kHmOyCipHOvytXYbtZz6+T5Jf/8VplP6Z5ESmA+nIfhoqnv9T+ybygmM jMlBGgoXF+zMv2LIs55noeTuSePQIG/Qu5y1BpCccsXxLbOv1uUDHwC/KM6KSrsoPbN0 4E0DeNx6jvAZiBPqj00DxdkxE5CC3zEbgG9CmJQH0KcV95VUfP4sw7y1NqeiHrIrUjff A68Q== 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=cUC5An4JselpQF6T/QYP1s4jz+j8/fWLHh6tknw1erk=; b=RSLF0YnHX8Z3HCYAZkpjK3lRuugV/ajLrjWx2Dp8kBu6IbNQrRqv37YCC38jLH4PcO OS1Cr0CGZ6LfJjSy40EY2Fuj/TvwCklcGiFr3yWwr16JaYZsmCLXGQp55dLh0lf06+5d eV7jNdT0eBLSX9H8lXCiDN/l4LUnH0FIxz+voQdXhNok/Xn14mlqDuxmrM+w5KPIlqVF kzN+logO8iBtsPppFJ0/m7qrms+L9NmqX/WMueugZSkug4IGhL8DYhyt6iGF/gq/MEkD OwlMry4y5ulakM/XeS3S4gp041lnE8HBfoFZr3KDGcq+ow1a7t5wpijTDENSr4MoISRx DpaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=X2UJouhU; 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 sh41-20020a1709076ea900b006e82db9fd02si6045530ejc.991.2022.04.10.16.00.35; Sun, 10 Apr 2022 16:00:35 -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=X2UJouhU; 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 A3FEA68B367; Mon, 11 Apr 2022 02:00:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3FE4768B0D3 for ; Mon, 11 Apr 2022 02:00:25 +0300 (EEST) Received: by mail-wr1-f46.google.com with SMTP id v2so6875139wrv.6 for ; Sun, 10 Apr 2022 16:00:25 -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=VHqgpuYWdWMOT0JGaDmekp39ogx6pduTuIQ/DzMcMPY=; b=X2UJouhUFu99qwY9CpthRYm93GwXz8bU9RKEggmrimb+YGb4YruzBCP12wQHOFG6Fs GofrNVfGAsezRQTHApgSw1Hx9rFM7B8gk6gS6yocWoRqlRJf1d2UooVAl2dV6EBOoWjB ndAGdz9rieBS6P4u6fxtM/eSlb2isUrlOAPxnXnit4e+6/wWZ4z9ucw9EBO7VqXP5655 Rl2jUYz9E7cwFyx4VysXemi0zYcvgW7Hpzys/rnjAxsYS61TGTY33KYmU0fXYaSCEvWG Fmbgh/PRtg7BGoOBBVo1uMqLlpJkomW8Y+y0bwOGrzlvPtCvHCeecIM6R4lm/b6eCgkS 5J6Q== 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=VHqgpuYWdWMOT0JGaDmekp39ogx6pduTuIQ/DzMcMPY=; b=UMh0QhEpqPNu3EeYCgIdNhPY3XsmqCRViFelD2QLfIbbHyS+9Q0jMFv9t99OVabfJO 48A/FYmz6NL+9zx0heNHk858Zo62Om1nkA9UPHclnMmug78dzIgWxOn0MSmB9JEPYIG5 WScgYkyWPZPCSu3/LzC6UKTN36jGh87vcMSW/NJCOY+EVwaTOufeeg32x6CMVf4a0BHi 6iY0fQOR9HKK2qZEK6rAWU22JcH5vV9x8sIFL/WdaSUGGwWjYPpa0GALF9dPgBQCxDy9 G7JeEqxeyKvRzZbvk3AF5Tl9U2d9E+ZiSA/C9SOGYqD93tJyT1eKPEJs0lQtc2OPEFGf CGrw== X-Gm-Message-State: AOAM530ZKC6W5Ox9vcw8ge4y5GBQM1bl/AF1UySU3EFg3d9Y/eujK6AE FUoKm0dyvVNzjGJwCdPezOOpHRUdXUc= X-Received: by 2002:adf:fc88:0:b0:207:a5f6:dbbf with SMTP id g8-20020adffc88000000b00207a5f6dbbfmr2408189wrr.337.1649631624515; Sun, 10 Apr 2022 16:00:24 -0700 (PDT) Received: from localhost.localdomain ([212.15.177.33]) by smtp.gmail.com with ESMTPSA id m7-20020adfe0c7000000b002060e7bbe49sm25319945wri.45.2022.04.10.16.00.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Apr 2022 16:00:24 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Apr 2022 01:02:43 +0200 Message-Id: <20220410230243.87094-1-onemda@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add feedback 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: 2/VSOMW6IGXz Signed-off-by: Paul B Mahol --- Useful to only filter part of video frame, without using complicated and slow split+crop+overlay combination. --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_feedback.c | 249 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 libavfilter/vf_feedback.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index d69bd59bb6..bdfdfdc04a 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -279,6 +279,7 @@ OBJS-$(CONFIG_ESTDIF_FILTER) += vf_estdif.o OBJS-$(CONFIG_EXPOSURE_FILTER) += vf_exposure.o OBJS-$(CONFIG_EXTRACTPLANES_FILTER) += vf_extractplanes.o OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o +OBJS-$(CONFIG_FEEDBACK_FILTER) += vf_feedback.o OBJS-$(CONFIG_FFTDNOIZ_FILTER) += vf_fftdnoiz.o OBJS-$(CONFIG_FFTFILT_FILTER) += vf_fftfilt.o OBJS-$(CONFIG_FIELD_FILTER) += vf_field.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index abd1fe2367..44fac46521 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -261,6 +261,7 @@ extern const AVFilter ff_vf_estdif; extern const AVFilter ff_vf_exposure; extern const AVFilter ff_vf_extractplanes; extern const AVFilter ff_vf_fade; +extern const AVFilter ff_vf_feedback; extern const AVFilter ff_vf_fftdnoiz; extern const AVFilter ff_vf_fftfilt; extern const AVFilter ff_vf_field; diff --git a/libavfilter/vf_feedback.c b/libavfilter/vf_feedback.c new file mode 100644 index 0000000000..eb3ac4539d --- /dev/null +++ b/libavfilter/vf_feedback.c @@ -0,0 +1,249 @@ +/* + * 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 + */ + +/** + * @file + * feedback video filter + */ + +#include "libavutil/avstring.h" +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/internal.h" +#include "avfilter.h" +#include "filters.h" +#include "internal.h" +#include "video.h" + +typedef struct FeedbackContext { + const AVClass *class; + + int x[2]; + int y[2]; + + int max_step[4]; + int hsub, vsub; + + AVFrame *in[2]; +} FeedbackContext; + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); + FeedbackContext *s = ctx->priv; + + if (s->x[0] >= ctx->inputs[0]->w) + s->x[0] = 0; + + if (s->x[1] >= ctx->inputs[0]->w) + s->x[1] = ctx->inputs[0]->w; + + if (s->y[0] >= ctx->inputs[0]->h) + s->y[0] = 0; + + if (s->y[1] >= ctx->inputs[0]->h) + s->y[1] = ctx->inputs[0]->h; + + s->hsub = pix_desc->log2_chroma_w; + s->vsub = pix_desc->log2_chroma_h; + + av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc); + + ctx->inputs[1]->w = s->x[1] <= s->x[0] ? ctx->inputs[0]->w - s->x[0] : s->x[1] - s->x[0]; + ctx->inputs[1]->h = s->y[1] <= s->y[0] ? ctx->inputs[0]->h - s->y[0] : s->y[1] - s->y[0]; + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + FeedbackContext *s = ctx->priv; + + ctx->outputs[0]->w = ctx->inputs[0]->w; + ctx->outputs[0]->h = ctx->inputs[0]->h; + ctx->outputs[1]->w = s->x[1] <= s->x[0] ? ctx->inputs[0]->w - s->x[0] : s->x[1] - s->x[0]; + ctx->outputs[1]->h = s->y[1] <= s->y[0] ? ctx->inputs[0]->h - s->y[0] : s->y[1] - s->y[0]; + + return 0; +} + +static int query_formats(AVFilterContext *ctx) +{ + return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, AV_PIX_FMT_FLAG_HWACCEL | + AV_PIX_FMT_FLAG_PAL)); +} + +static int activate(AVFilterContext *ctx) +{ + FeedbackContext *s = ctx->priv; + int status, ret; + int64_t pts; + + for (int i = 0; i < ctx->nb_outputs; i++) + FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx); + + if (!s->in[0]) { + ret = ff_inlink_consume_frame(ctx->inputs[0], &s->in[0]); + if (ret < 0) + return ret; + + if (ret > 0) { + AVFrame *frame = av_frame_clone(s->in[0]); + if (!frame) { + av_frame_free(&s->in[0]); + return AVERROR(ENOMEM); + } + + frame->width = s->x[1] <= s->x[0] ? frame->width - s->x[0] : s->x[1] - s->x[0]; + frame->height = s->y[1] <= s->y[0] ? frame->height - s->y[0] : s->y[1] - s->y[0]; + + frame->data[0] += s->y[0] * frame->linesize[0]; + frame->data[0] += s->x[0] * s->max_step[0]; + + for (int i = 1; i < 3; i ++) { + if (frame->data[i]) { + frame->data[i] += (s->y[0] >> s->vsub) * frame->linesize[i]; + frame->data[i] += (s->x[0] * s->max_step[i]) >> s->hsub; + } + } + + if (frame->data[3]) { + frame->data[3] += s->y[0] * frame->linesize[3]; + frame->data[3] += s->x[0] * s->max_step[3]; + } + + return ff_filter_frame(ctx->outputs[1], frame); + } + } else { + ret = ff_inlink_consume_frame(ctx->inputs[1], &s->in[1]); + if (ret < 0) + return ret; + + if (ret > 0) { + AVFrame *dst = s->in[0]; + AVFrame *src = s->in[1]; + + for (int y = 0; y < src->height; y++) { + memmove(dst->data[0] + (s->y[0] + y) * dst->linesize[0] + s->x[0] * s->max_step[0], + src->data[0] + y * src->linesize[0], src->width * s->max_step[0]); + } + + for (int i = 1; i < 3; i ++) { + if (dst->data[i]) { + for (int y = 0; y < src->height; y++) { + memmove(dst->data[i] + ((s->y[0] + y) >> s->vsub) * dst->linesize[i] + ((s->x[0] * s->max_step[i]) >> s->hsub), + src->data[i] + (y >> s->vsub) * src->linesize[i], (src->width * s->max_step[i]) >> s->hsub); + } + } + } + + if (dst->data[3]) { + for (int y = 0; y < src->height; y++) { + memmove(dst->data[3] + (s->y[0] + y) * dst->linesize[3] + s->x[0] * s->max_step[3], + src->data[3] + y * src->linesize[3], src->width * s->max_step[3]); + } + } + + ret = ff_filter_frame(ctx->outputs[0], s->in[0]); + av_frame_free(&s->in[1]); + s->in[0] = NULL; + return ret; + } + } + + if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) { + ff_outlink_set_status(ctx->outputs[0], status, pts); + ff_outlink_set_status(ctx->outputs[1], status, pts); + return 0; + } + + if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) { + ff_outlink_set_status(ctx->outputs[0], status, pts); + ff_outlink_set_status(ctx->outputs[1], status, pts); + return 0; + } + + if (!s->in[0]) + FF_FILTER_FORWARD_WANTED(ctx->outputs[0], ctx->inputs[0]); + if (!s->in[1]) + FF_FILTER_FORWARD_WANTED(ctx->outputs[0], ctx->inputs[1]); + + return FFERROR_NOT_READY; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + FeedbackContext *s = ctx->priv; + + av_frame_free(&s->in[0]); + av_frame_free(&s->in[1]); +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, + { + .name = "feedinput", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { + .name = "feedoutput", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, +}; + +#define OFFSET(x) offsetof(FeedbackContext, x) +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM) + +static const AVOption feedback_options[] = { + { "x0", "set x0 crop position", OFFSET(x[0]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { "y0", "set y0 crop position", OFFSET(y[0]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { "x1", "set x1 crop position", OFFSET(x[1]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { "y1", "set y1 crop position", OFFSET(y[1]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(feedback); + +const AVFilter ff_vf_feedback = { + .name = "feedback", + .description = NULL_IF_CONFIG_SMALL("Apply feedback video filter."), + .priv_class = &feedback_class, + .priv_size = sizeof(FeedbackContext), + .activate = activate, + .uninit = uninit, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), + FILTER_QUERY_FUNC(query_formats), + .flags = AVFILTER_FLAG_METADATA_ONLY, +};