2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/opt.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/intreadwrite.h"
29 typedef struct Points {
33 typedef struct FloodfillContext {
44 int (*is_same)(AVFrame *frame, int x, int y,
45 unsigned s0, unsigned s1, unsigned s2, unsigned s3);
46 void (*set_pixel)(AVFrame *frame, int x, int y,
47 unsigned d0, unsigned d1, unsigned d2, unsigned d3);
48 void (*pick_pixel)(AVFrame *frame, int x, int y,
49 int *s0, int *s1, int *s2, int *s3);
52 static int is_inside(int x, int y, int w, int h)
54 if (x >= 0 && x < w && y >= 0 && y < h)
59 static int is_same4(AVFrame *frame, int x, int y,
60 unsigned s0, unsigned s1, unsigned s2, unsigned s3)
62 unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
63 unsigned c1 = frame->data[1][y * frame->linesize[1] + x];
64 unsigned c2 = frame->data[2][y * frame->linesize[2] + x];
65 unsigned c3 = frame->data[3][y * frame->linesize[3] + x];
67 if (s0 == c0 && s1 == c1 && s2 == c2 && s3 == c3)
72 static int is_same4_16(AVFrame *frame, int x, int y,
73 unsigned s0, unsigned s1, unsigned s2, unsigned s3)
75 unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
76 unsigned c1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
77 unsigned c2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
78 unsigned c3 = AV_RN16(frame->data[3] + y * frame->linesize[3] + 2 * x);
80 if (s0 == c0 && s1 == c1 && s2 == c2 && s3 == c3)
85 static int is_same3(AVFrame *frame, int x, int y,
86 unsigned s0, unsigned s1, unsigned s2, unsigned s3)
88 unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
89 unsigned c1 = frame->data[1][y * frame->linesize[1] + x];
90 unsigned c2 = frame->data[2][y * frame->linesize[2] + x];
92 if (s0 == c0 && s1 == c1 && s2 == c2)
97 static int is_same3_16(AVFrame *frame, int x, int y,
98 unsigned s0, unsigned s1, unsigned s2, unsigned s3)
100 unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
101 unsigned c1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
102 unsigned c2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
104 if (s0 == c0 && s1 == c1 && s2 == c2)
109 static int is_same1(AVFrame *frame, int x, int y,
110 unsigned s0, unsigned s1, unsigned s2, unsigned s3)
112 unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
119 static int is_same1_16(AVFrame *frame, int x, int y,
120 unsigned s0, unsigned s1, unsigned s2, unsigned s3)
122 unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
129 static void set_pixel1(AVFrame *frame, int x, int y,
130 unsigned d0, unsigned d1, unsigned d2, unsigned d3)
132 frame->data[0][y * frame->linesize[0] + x] = d0;
135 static void set_pixel1_16(AVFrame *frame, int x, int y,
136 unsigned d0, unsigned d1, unsigned d2, unsigned d3)
138 AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
141 static void set_pixel3(AVFrame *frame, int x, int y,
142 unsigned d0, unsigned d1, unsigned d2, unsigned d3)
144 frame->data[0][y * frame->linesize[0] + x] = d0;
145 frame->data[1][y * frame->linesize[1] + x] = d1;
146 frame->data[2][y * frame->linesize[2] + x] = d2;
149 static void set_pixel3_16(AVFrame *frame, int x, int y,
150 unsigned d0, unsigned d1, unsigned d2, unsigned d3)
152 AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
153 AV_WN16(frame->data[1] + y * frame->linesize[1] + 2 * x, d1);
154 AV_WN16(frame->data[2] + y * frame->linesize[2] + 2 * x, d2);
157 static void set_pixel4(AVFrame *frame, int x, int y,
158 unsigned d0, unsigned d1, unsigned d2, unsigned d3)
160 frame->data[0][y * frame->linesize[0] + x] = d0;
161 frame->data[1][y * frame->linesize[1] + x] = d1;
162 frame->data[2][y * frame->linesize[2] + x] = d2;
163 frame->data[3][y * frame->linesize[3] + x] = d3;
166 static void set_pixel4_16(AVFrame *frame, int x, int y,
167 unsigned d0, unsigned d1, unsigned d2, unsigned d3)
169 AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
170 AV_WN16(frame->data[1] + y * frame->linesize[1] + 2 * x, d1);
171 AV_WN16(frame->data[2] + y * frame->linesize[2] + 2 * x, d2);
172 AV_WN16(frame->data[3] + y * frame->linesize[3] + 2 * x, d3);
175 static void pick_pixel1(AVFrame *frame, int x, int y,
176 int *s0, int *s1, int *s2, int *s3)
179 *s0 = frame->data[0][y * frame->linesize[0] + x];
182 static void pick_pixel1_16(AVFrame *frame, int x, int y,
183 int *s0, int *s1, int *s2, int *s3)
186 *s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
189 static void pick_pixel3(AVFrame *frame, int x, int y,
190 int *s0, int *s1, int *s2, int *s3)
193 *s0 = frame->data[0][y * frame->linesize[0] + x];
195 *s1 = frame->data[1][y * frame->linesize[1] + x];
197 *s2 = frame->data[2][y * frame->linesize[2] + x];
200 static void pick_pixel3_16(AVFrame *frame, int x, int y,
201 int *s0, int *s1, int *s2, int *s3)
204 *s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
206 *s1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
208 *s2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
211 static void pick_pixel4(AVFrame *frame, int x, int y,
212 int *s0, int *s1, int *s2, int *s3)
215 *s0 = frame->data[0][y * frame->linesize[0] + x];
217 *s1 = frame->data[1][y * frame->linesize[1] + x];
219 *s2 = frame->data[2][y * frame->linesize[2] + x];
221 *s3 = frame->data[3][y * frame->linesize[3] + x];
224 static void pick_pixel4_16(AVFrame *frame, int x, int y,
225 int *s0, int *s1, int *s2, int *s3)
228 *s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
230 *s1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
232 *s2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
234 *s3 = AV_RN16(frame->data[3] + y * frame->linesize[3] + 2 * x);
237 static int config_input(AVFilterLink *inlink)
239 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
240 AVFilterContext *ctx = inlink->dst;
241 FloodfillContext *s = ctx->priv;
244 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
245 depth = desc->comp[0].depth;
247 switch (s->nb_planes) {
248 case 1: s->set_pixel = set_pixel1;
249 s->is_same = is_same1;
250 s->pick_pixel = pick_pixel1; break;
251 case 3: s->set_pixel = set_pixel3;
252 s->is_same = is_same3;
253 s->pick_pixel = pick_pixel3; break;
254 case 4: s->set_pixel = set_pixel4;
255 s->is_same = is_same4;
256 s->pick_pixel = pick_pixel4; break;
259 switch (s->nb_planes) {
260 case 1: s->set_pixel = set_pixel1_16;
261 s->is_same = is_same1_16;
262 s->pick_pixel = pick_pixel1_16; break;
263 case 3: s->set_pixel = set_pixel3_16;
264 s->is_same = is_same3_16;
265 s->pick_pixel = pick_pixel3_16; break;
266 case 4: s->set_pixel = set_pixel4_16;
267 s->is_same = is_same4_16;
268 s->pick_pixel = pick_pixel4_16; break;
272 s->front = s->back = 0;
273 s->points = av_calloc(inlink->w * inlink->h, 4 * sizeof(Points));
275 return AVERROR(ENOMEM);
280 static int filter_frame(AVFilterLink *link, AVFrame *frame)
282 AVFilterContext *ctx = link->dst;
283 FloodfillContext *s = ctx->priv;
284 const unsigned d0 = s->d[0];
285 const unsigned d1 = s->d[1];
286 const unsigned d2 = s->d[2];
287 const unsigned d3 = s->d[3];
292 const int w = frame->width;
293 const int h = frame->height;
296 for (i = 0; i < s->nb_planes; i++) {
297 if (s->s[i] != s->d[i])
301 if (i == s->nb_planes)
304 if (ret = av_frame_make_writable(frame))
307 if (is_inside(s->x, s->y, w, h)) {
308 s->pick_pixel(frame, s->x, s->y, &s0, &s1, &s2, &s3);
310 if (s->is_same(frame, s->x, s->y, s0, s1, s2, s3)) {
311 s->points[s->front].x = s->x;
312 s->points[s->front].y = s->y;
316 while (s->front > s->back) {
320 x = s->points[s->front].x;
321 y = s->points[s->front].y;
323 if (s->is_same(frame, x, y, s0, s1, s2, s3)) {
324 s->set_pixel(frame, x, y, d0, d1, d2, d3);
326 if (is_inside(x + 1, y, w, h)) {
327 s->points[s->front] .x = x + 1;
328 s->points[s->front++].y = y;
331 if (is_inside(x - 1, y, w, h)) {
332 s->points[s->front] .x = x - 1;
333 s->points[s->front++].y = y;
336 if (is_inside(x, y + 1, w, h)) {
337 s->points[s->front] .x = x;
338 s->points[s->front++].y = y + 1;
341 if (is_inside(x, y - 1, w, h)) {
342 s->points[s->front] .x = x;
343 s->points[s->front++].y = y - 1;
350 return ff_filter_frame(ctx->outputs[0], frame);
353 static av_cold int query_formats(AVFilterContext *ctx)
355 static const enum AVPixelFormat pixel_fmts[] = {
356 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
357 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
358 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
359 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
360 AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GBRAP,
361 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
362 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
365 AVFilterFormats *formats;
367 formats = ff_make_format_list(pixel_fmts);
369 return AVERROR(ENOMEM);
371 return ff_set_common_formats(ctx, formats);
374 static av_cold void uninit(AVFilterContext *ctx)
376 FloodfillContext *s = ctx->priv;
378 av_freep(&s->points);
381 static const AVFilterPad floodfill_inputs[] = {
384 .type = AVMEDIA_TYPE_VIDEO,
385 .filter_frame = filter_frame,
386 .config_props = config_input,
391 static const AVFilterPad floodfill_outputs[] = {
394 .type = AVMEDIA_TYPE_VIDEO,
399 #define OFFSET(x) offsetof(FloodfillContext, x)
400 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
402 static const AVOption floodfill_options[] = {
403 { "x", "set pixel x coordinate", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
404 { "y", "set pixel y coordinate", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
405 { "s0", "set source #0 component value", OFFSET(s[0]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
406 { "s1", "set source #1 component value", OFFSET(s[1]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
407 { "s2", "set source #2 component value", OFFSET(s[2]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
408 { "s3", "set source #3 component value", OFFSET(s[3]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
409 { "d0", "set destination #0 component value", OFFSET(d[0]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
410 { "d1", "set destination #1 component value", OFFSET(d[1]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
411 { "d2", "set destination #2 component value", OFFSET(d[2]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
412 { "d3", "set destination #3 component value", OFFSET(d[3]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
416 AVFILTER_DEFINE_CLASS(floodfill);
418 AVFilter ff_vf_floodfill = {
420 .description = NULL_IF_CONFIG_SMALL("Fill area with same color with another color."),
421 .priv_size = sizeof(FloodfillContext),
422 .priv_class = &floodfill_class,
423 .query_formats = query_formats,
425 .inputs = floodfill_inputs,
426 .outputs = floodfill_outputs,
427 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,