Signed-off-by: Paul B Mahol <one...@gmail.com> --- 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, +}; -- 2.9.3 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel