From patchwork Sat Jul 29 20:34:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 4512 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.85 with SMTP id 82csp652670vsb; Sat, 29 Jul 2017 13:40:45 -0700 (PDT) X-Received: by 10.28.19.9 with SMTP id 9mr8585791wmt.171.1501360845541; Sat, 29 Jul 2017 13:40:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501360845; cv=none; d=google.com; s=arc-20160816; b=GbTEfox/cLP9qxUNdN5fAfrMEbfvgAtROs19x9WpQm5FirOMpyrXZctP5PKpGhDEPb MomDbI4VcWUv84BzCd9wolyxlO/IMAljIF8syjApzzU5142DjZuCODxLQEvNFHfj4hGQ tzShlHP5sgbRb21bAqjZEfokvngKrpPIFd5h7h7e3YS24NsChKGry9yZl6+hW08/G2sh uuu6KA9MfXPfybsNdUFL/bpYuA13w2gGXrvqajQIdJRXRzjpigJIQfXtLa/75f6ULa3e 18zLq2l5geSESprU15pjH6ws62smM5cNa39519hIPdQ671G0WphWYWVq/iRrgfxxX9ls Lq4g== 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=V2OU1NJ/ExuaCrEVbS/miweOOOp2KIAgJyJv9FE6cew=; b=cxGcu09n6cD3zImj+dqUjq8SKTCsUHNmCi5mtsfF2uaQVykOh0r1wo4VTjprw7s6vM pwYnyuj/c6eVyHJUmfld00cxOf+nBpeVXzXL0ZUQ6HqfkD6gAq2SUmWAkvxCDjj6FTGH hGNlOx97dlHoTOGz3TIr0t5Ea6io2+aN7p77y7xkQue44o9R5ljXk8pnC+nu/u3annJX sTpJvpChfoUhorO5C+KVLX3mXjXlX6yuGaoo9mi+MjWeG1hINPe56skpT4rLmkJD8N+m ZDb/gDboO3pvex8rmISe+X4tgqZBpU91c+q1vq2+VFkmCTPrtAATkHBurX5fqo56mt3N 0c8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=WVBxJyab; 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=NONE 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 j19si5546501wrd.108.2017.07.29.13.40.45; Sat, 29 Jul 2017 13:40:45 -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.b=WVBxJyab; 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=NONE 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 1F8E9689E38; Sat, 29 Jul 2017 23:40:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f195.google.com (mail-wr0-f195.google.com [209.85.128.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 52542689BBA for ; Sat, 29 Jul 2017 23:40:34 +0300 (EEST) Received: by mail-wr0-f195.google.com with SMTP id g32so11605506wrd.5 for ; Sat, 29 Jul 2017 13:40:36 -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=tj4Ozthr5nc3V6v+BPAINm9UEqTXgX2NMtdMch33Yis=; b=WVBxJyabZK6rxXNg9Sqf1FBiIIORzU5aVQeN8ta3BdTpGecJGnypR/uMXYYW8iYa4G s84RaTNR/rBXgGW5lfhZeq/Ug9I/Yws5LwOia0PcycrJsdTexuT1tRi8Gb9/HmBDLQmC HnpZAs3BI/SGO+nrZl3MPNNoe/wX+d94JgNyjYdM3Zvl3ZWdy0dlc/9bUhEGHqdHsYDT RBctJiNLxuj9s7AaLYfsIapTtuE9nqGRtC80yLwccMkO7xTev74i/veTMTpMfocB2yKy 7kRULFQwghJWrIxcQ7JqzE1glN23Re9JHOBibjGOecBa+3aO/mtyvZkilKqrK+UwBQ96 7dSg== 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=tj4Ozthr5nc3V6v+BPAINm9UEqTXgX2NMtdMch33Yis=; b=S+6lw9Q0kMnC9EcuVaJjk9hio/LvRj9QEPUoVDCnQ4fkXDbCKxlDGGtbC755lCx93B qFzL/BYLMZA5HEyadbqyP4vJUz9RTa96XqX6JZ3f/Msmkp67TScxc5klDRo+6krhB4u8 0184gF4BSJY9QusrNyuKiyyAn2uIp6MzF+wl1t5JqwfTFyeJDOC2GmJhqDYsG1u0DJ2w V/7isrljF0XMeElXK8tLaYrifMTnl+TTzRupnCPFiEGomXm5YbmyhMVXZhGuc0Dsyt8W GqFwdUYnFQzpz45TTwNYvA+v68Xwu2mAxxf3ttVkigNWYjxf2GqXLOgFWkoaDiRj1zsT acLQ== X-Gm-Message-State: AIVw1128Mmkp63FjPpX2Aa4XsGQkf6jmYd4/8kENOf97iPoSmLwLQSD/ z9kFurQWCKacukQL X-Received: by 10.223.155.205 with SMTP id e13mr8507774wrc.71.1501360457181; Sat, 29 Jul 2017 13:34:17 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id b4sm25624193wrf.70.2017.07.29.13.34.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Jul 2017 13:34:16 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 29 Jul 2017 22:34:05 +0200 Message-Id: <20170729203405.24597-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add floodfill 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/allfilters.c | 1 + libavfilter/vf_floodfill.c | 212 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 libavfilter/vf_floodfill.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index ee16361..5ec0f99 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -186,6 +186,7 @@ OBJS-$(CONFIG_FIELDHINT_FILTER) += vf_fieldhint.o OBJS-$(CONFIG_FIELDMATCH_FILTER) += vf_fieldmatch.o OBJS-$(CONFIG_FIELDORDER_FILTER) += vf_fieldorder.o OBJS-$(CONFIG_FIND_RECT_FILTER) += vf_find_rect.o lavfutils.o +OBJS-$(CONFIG_FLOODFILL_FILTER) += vf_floodfill.o OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o OBJS-$(CONFIG_FPS_FILTER) += vf_fps.o OBJS-$(CONFIG_FRAMEPACK_FILTER) += vf_framepack.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index b1c2d11..635c668 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -198,6 +198,7 @@ static void register_all(void) REGISTER_FILTER(FIELDMATCH, fieldmatch, vf); REGISTER_FILTER(FIELDORDER, fieldorder, vf); REGISTER_FILTER(FIND_RECT, find_rect, vf); + REGISTER_FILTER(FLOODFILL, floodfill, vf); REGISTER_FILTER(FORMAT, format, vf); REGISTER_FILTER(FPS, fps, vf); REGISTER_FILTER(FRAMEPACK, framepack, vf); diff --git a/libavfilter/vf_floodfill.c b/libavfilter/vf_floodfill.c new file mode 100644 index 0000000..a34766b --- /dev/null +++ b/libavfilter/vf_floodfill.c @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2017 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/opt.h" +#include "libavutil/imgutils.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct Points { + uint16_t x, y; +} Points; + +typedef struct FloodfillContext { + const AVClass *class; + + int x, y; + uint64_t src, dst; + + unsigned s0, s1, s2; + unsigned d0, d1, d2; + + int back, front; + Points *points; +} FloodfillContext; + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + AVFilterContext *ctx = inlink->dst; + FloodfillContext *s = ctx->priv; + int depth; + + depth = desc->comp[0].depth; + if (depth == 8) { + s->s0 = (s->src & 0xFF0000) >> 16; + s->s1 = (s->src & 0x00FF00) >> 8; + s->s2 = (s->src & 0x0000FF); + s->d0 = (s->dst & 0xFF0000) >> 16; + s->d1 = (s->dst & 0x00FF00) >> 8; + s->d2 = (s->dst & 0x0000FF); + } else { + } + + s->front = s->back = 0; + s->points = av_calloc(inlink->w * inlink->h, 4 * sizeof(Points)); + if (!s->points) + return AVERROR(ENOMEM); + + return 0; +} + +static int is_inside(int x, int y, AVFrame *frame) +{ + if (x >= 0 && x < frame->width && y >= 0 && y < frame->height) + return 1; + return 0; +} + +static int is_same(AVFrame *frame, int x, int y, uint32_t src) +{ + unsigned c0 = frame->data[0][y * frame->linesize[0] + x]; + unsigned c1 = frame->data[1][y * frame->linesize[1] + x]; + unsigned c2 = frame->data[2][y * frame->linesize[2] + x]; + + if (src == (c0 << 16 | c1 << 8 | c2)) + return 1; + return 0; +} + +static int filter_frame(AVFilterLink *link, AVFrame *frame) +{ + AVFilterContext *ctx = link->dst; + FloodfillContext *s = ctx->priv; + int ret; + + if (ret = av_frame_make_writable(frame)) + return ret; + + if (is_inside(s->x, s->y, frame)) { + if (is_same(frame, s->x, s->y, s->src)) { + s->points[s->front].x = s->x; + s->points[s->front].y = s->y; + s->front++; + } + + while (s->front > s->back) { + int x, y; + + s->front--; + x = s->points[s->front].x; + y = s->points[s->front].y; + + if (is_same(frame, x, y, s->src)) { + frame->data[0][y * frame->linesize[0] + x] = s->d0; + frame->data[1][y * frame->linesize[1] + x] = s->d1; + frame->data[2][y * frame->linesize[2] + x] = s->d2; + + if (is_inside(x + 1, y, frame)) { + s->points[s->front] .x = x + 1; + s->points[s->front++].y = y; + } + + if (is_inside(x - 1, y, frame)) { + s->points[s->front] .x = x - 1; + s->points[s->front++].y = y; + } + + if (is_inside(x, y + 1, frame)) { + s->points[s->front] .x = x; + s->points[s->front++].y = y + 1; + } + + if (is_inside(x, y - 1, frame)) { + s->points[s->front] .x = x; + s->points[s->front++].y = y - 1; + } + } + } + } + + return ff_filter_frame(ctx->outputs[0], frame); +} + +static av_cold int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pixel_fmts[] = { + AV_PIX_FMT_YUV444P, + AV_PIX_FMT_GBRP, + AV_PIX_FMT_YUV444P9, + AV_PIX_FMT_YUV444P10, + AV_PIX_FMT_YUV444P12, + AV_PIX_FMT_YUV444P14, + AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_NONE + }; + AVFilterFormats *formats; + + formats = ff_make_format_list(pixel_fmts); + if (!formats) + return AVERROR(ENOMEM); + + return ff_set_common_formats(ctx, formats); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + FloodfillContext *s = ctx->priv; + + av_freep(&s->points); +} + +static const AVFilterPad floodfill_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad floodfill_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(FloodfillContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption floodfill_options[] = { + { "x", "set pixel x coordinate", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS }, + { "y", "set pixel y coordinate", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS }, + { "src", "set source pixel value", OFFSET(src), AV_OPT_TYPE_UINT64, {.i64=0}, 0, UINT64_MAX, FLAGS }, + { "dst", "set destination pixel value", OFFSET(dst), AV_OPT_TYPE_UINT64, {.i64=0}, 0, UINT64_MAX, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(floodfill); + +AVFilter ff_vf_floodfill = { + .name = "floodfill", + .description = NULL_IF_CONFIG_SMALL("Fill area with same color with another color."), + .priv_size = sizeof(FloodfillContext), + .priv_class = &floodfill_class, + .query_formats = query_formats, + .uninit = uninit, + .inputs = floodfill_inputs, + .outputs = floodfill_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +};