From patchwork Wed Mar 13 00:17:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 12299 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 224CF447D94 for ; Wed, 13 Mar 2019 02:18:08 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0B95568A859; Wed, 13 Mar 2019 02:18:08 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3898768A832 for ; Wed, 13 Mar 2019 02:18:01 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id t18so61955wrx.2 for ; Tue, 12 Mar 2019 17:18:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=vWKd/DwDvOJ4tkKPNCSWwZH2YIQFi4U64m8bfDdOcMM=; b=Vifkv4VEqs3iOFkgYB77J5oNcc7N3JR8ID04Ju7twPSjMIgqizYaJuJuks9t5icLmv eBXygaphxryv0+qvfcSWUkFzt3dITtL/tPggPkR3U1H80LwYGyNEhQ824f6kLqsx9o3t EzRYd3IzNgytEFvHD6twX6F9PZ9+QGpHzHJ/AMRiYmN3yfu5EXZu2mIeo1jhZ/4J2LSt VhYTEBZ+NuJFPDzvwltLCSgF2ZrWdruHRAyTBQBlC0Pjv8GbwUZlTmsRkXbpw4pPFjLP Y4h+QOy9fa4Yo6TJJyqHFWbOBruvbka/pd1nJuAZQq+p/dslxc5qH9eVQw+UBbtFIFu+ ssPQ== 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:mime-version:content-transfer-encoding; bh=vWKd/DwDvOJ4tkKPNCSWwZH2YIQFi4U64m8bfDdOcMM=; b=fBo5hIlx9kBC1vuh9rLg6J0+AUHKOyQek+KYfuqFOFrOzUGAYjenXkJfPFg2s42yMh noXC8zvffalqSXNxSBz9O/Sfz7wn/My+QCKfti7BQV6ZVEZHZzlpqMX6C0D2pFKWnkA5 vaYwpED3fjUBjnn4MVQBxTxl29dgSXG2uiv6ApQimD4Nvss5KZqbhJWRog/sF/Ey+zVO QPmc0Gf7aBRqbyjxkmGZMz7CW79YIJ6ICP3KWSr9rGsfq7BlsWQtWyrQzacbMggUxdwn hKg+PS2Q8I/K/tmwfPiAZEwekOQZl6C1RYhQZIXQ8v810w6OtmjvDb5NSeoOdX2fRJvG dMaw== X-Gm-Message-State: APjAAAXmvknlEVZNoPXxAQ3LqD3I6TaZ9q8ib8aO1kNnMcAx8UATRx/F XuXbCgX7zehiAeQgGmXZ5ohZ59wBGuo= X-Google-Smtp-Source: APXvYqzNphqja6Jk+XX2bioJ/cnEk0UJN6EdFLE6qJ9uL/yoVj7SXDRE5xf7u+vtjQchiLuB1p8v9g== X-Received: by 2002:a05:6000:12cc:: with SMTP id l12mr13857878wrx.303.1552436280373; Tue, 12 Mar 2019 17:18:00 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id u17sm32505710wrg.71.2019.03.12.17.17.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Mar 2019 17:17:59 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Wed, 13 Mar 2019 00:17:48 +0000 Message-Id: <20190313001748.11781-6-sw@jkqxz.net> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190313001748.11781-1-sw@jkqxz.net> References: <20190313001748.11781-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 6/6] lavfi: addroi 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This can be used to add region of interest side data to video frames. --- Now using the x,y,w,h style to match other filters. libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_addroi.c | 265 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 libavfilter/vf_addroi.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index fef6ec5c55..31ae738a50 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -149,6 +149,7 @@ OBJS-$(CONFIG_SINE_FILTER) += asrc_sine.o OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o # video filters +OBJS-$(CONFIG_ADDROI_FILTER) += vf_addroi.o OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o OBJS-$(CONFIG_AMPLIFY_FILTER) += vf_amplify.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index c51ae0f3c7..52413c8f0d 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -140,6 +140,7 @@ extern AVFilter ff_asrc_sine; extern AVFilter ff_asink_anullsink; +extern AVFilter ff_vf_addroi; extern AVFilter ff_vf_alphaextract; extern AVFilter ff_vf_alphamerge; extern AVFilter ff_vf_amplify; diff --git a/libavfilter/vf_addroi.c b/libavfilter/vf_addroi.c new file mode 100644 index 0000000000..8bca5e7371 --- /dev/null +++ b/libavfilter/vf_addroi.c @@ -0,0 +1,265 @@ +/* + * 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/eval.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "internal.h" + +enum { + X, Y, W, H, +}; +static const char *addroi_param_names[] = { + "x", "y", "w", "h", +}; + +enum { + VAR_IW, + VAR_IH, + NB_VARS, +}; +static const char *const addroi_var_names[] = { + "iw", + "ih", +}; + +typedef struct AddROIContext { + const AVClass *class; + + char *region_str[4]; + + AVExpr *region_expr[4]; + double vars[NB_VARS]; + + int region[4]; + AVRational qoffset; + + int clear; +} AddROIContext; + +static int addroi_config_input(AVFilterLink *inlink) +{ + AVFilterContext *avctx = inlink->dst; + AddROIContext *ctx = avctx->priv; + int i; + double val; + + ctx->vars[VAR_IW] = inlink->w; + ctx->vars[VAR_IH] = inlink->h; + + for (i = 0; i < 4; i++) { + int max_value; + switch (i) { + case X: max_value = inlink->w; break; + case Y: max_value = inlink->h; break; + case W: max_value = inlink->w - ctx->region[X]; break; + case H: max_value = inlink->h - ctx->region[Y]; break; + } + + val = av_expr_eval(ctx->region_expr[i], ctx->vars, NULL); + if (val < 0.0) { + av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %s is " + "less than zero - using zero instead.\n", val, + addroi_param_names[i]); + val = 0.0; + } else if (val > max_value) { + av_log(avctx, AV_LOG_WARNING, "Calculated value %g for %s is " + "greater than maximum allowed value %d - " + "using %d instead.\n", val, addroi_param_names[i], + max_value, max_value); + val = max_value; + } + ctx->region[i] = val; + } + + return 0; +} + +static int addroi_filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFilterContext *avctx = inlink->dst; + AVFilterLink *outlink = avctx->outputs[0]; + AddROIContext *ctx = avctx->priv; + AVRegionOfInterest *roi; + AVFrameSideData *sd; + int err; + + if (ctx->clear) { + av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + sd = NULL; + } else { + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + } + if (sd) { + const AVRegionOfInterest *old_roi; + AVBufferRef *roi_ref; + int nb_roi, i; + + old_roi = (const AVRegionOfInterest*)sd->data; + nb_roi = sd->size / old_roi->self_size + 1; + + roi_ref = av_buffer_alloc(sizeof(*roi) * nb_roi); + if (!roi_ref) { + err = AVERROR(ENOMEM); + goto fail; + } + roi = (AVRegionOfInterest*)roi_ref->data; + + for (i = 0; i < nb_roi - 1; i++) { + old_roi = (const AVRegionOfInterest*) + (sd->data + old_roi->self_size * i); + + roi[i] = (AVRegionOfInterest) { + .self_size = sizeof(*roi), + .x = old_roi->x, + .y = old_roi->y, + .width = old_roi->width, + .height = old_roi->height, + .qoffset = old_roi->qoffset, + }; + } + + roi[nb_roi - 1] = (AVRegionOfInterest) { + .self_size = sizeof(*roi), + .x = ctx->region[X], + .y = ctx->region[Y], + .width = ctx->region[W], + .height = ctx->region[H], + .qoffset = ctx->qoffset, + }; + + av_frame_remove_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + + sd = av_frame_new_side_data_from_buf(frame, + AV_FRAME_DATA_REGIONS_OF_INTEREST, + roi_ref); + if (!sd) { + av_buffer_unref(&roi_ref); + err = AVERROR(ENOMEM); + goto fail; + } + + } else { + sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST, + sizeof(AVRegionOfInterest)); + if (!sd) { + err = AVERROR(ENOMEM); + goto fail; + } + roi = (AVRegionOfInterest*)sd->data; + *roi = (AVRegionOfInterest) { + .self_size = sizeof(*roi), + .x = ctx->region[X], + .y = ctx->region[Y], + .width = ctx->region[W], + .height = ctx->region[H], + .qoffset = ctx->qoffset, + }; + } + + return ff_filter_frame(outlink, frame); + +fail: + av_frame_free(&frame); + return err; +} + +static av_cold int addroi_init(AVFilterContext *avctx) +{ + AddROIContext *ctx = avctx->priv; + int i, err; + + for (i = 0; i < 4; i++) { + err = av_expr_parse(&ctx->region_expr[i], ctx->region_str[i], + addroi_var_names, NULL, NULL, NULL, NULL, + 0, avctx); + if (err < 0) { + av_log(ctx, AV_LOG_ERROR, + "Error parsing %s expression '%s'.\n", + addroi_param_names[i], ctx->region_str[i]); + return err; + } + } + + return 0; +} + +static av_cold void addroi_uninit(AVFilterContext *avctx) +{ + AddROIContext *ctx = avctx->priv; + int i; + + for (i = 0; i < 4; i++) { + av_expr_free(ctx->region_expr[i]); + ctx->region_expr[i] = NULL; + } +} + +#define OFFSET(x) offsetof(AddROIContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM +static const AVOption addroi_options[] = { + { "x", "Region distance from top edge of frame.", + OFFSET(region_str[X]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, + { "y", "Region distance from left edge of frame.", + OFFSET(region_str[Y]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, + { "w", "Region width.", + OFFSET(region_str[W]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, + { "h", "Region height.", + OFFSET(region_str[H]), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS }, + + { "qoffset", "Quantisation offset to apply in the region.", + OFFSET(qoffset), AV_OPT_TYPE_RATIONAL, { .dbl = 0.0 }, -1, +1, FLAGS }, + + { "clear", "Remove any existing regions of interest before adding the new one.", + OFFSET(clear), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, + + { NULL } +}; + +AVFILTER_DEFINE_CLASS(addroi); + +static const AVFilterPad addroi_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = addroi_config_input, + .filter_frame = addroi_filter_frame, + }, + { NULL } +}; + +static const AVFilterPad addroi_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_addroi = { + .name = "addroi", + .description = NULL_IF_CONFIG_SMALL("Add region of interest to frame."), + .init = addroi_init, + .uninit = addroi_uninit, + + .priv_size = sizeof(AddROIContext), + .priv_class = &addroi_class, + + .inputs = addroi_inputs, + .outputs = addroi_outputs, +};