1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/*
* 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 "libavutil/intreadwrite.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;
int s[4];
int S[4];
int d[4];
int nb_planes;
int back, front;
Points *points;
int (*is_same)(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3);
void (*set_pixel)(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3);
void (*pick_pixel)(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3);
} FloodfillContext;
static int is_inside(int x, int y, int w, int h)
{
if (x >= 0 && x < w && y >= 0 && y < h)
return 1;
return 0;
}
static int is_same4(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3)
{
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];
unsigned c3 = frame->data[3][y * frame->linesize[3] + x];
if (s0 == c0 && s1 == c1 && s2 == c2 && s3 == c3)
return 1;
return 0;
}
static int is_same4_16(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3)
{
unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
unsigned c1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
unsigned c2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
unsigned c3 = AV_RN16(frame->data[3] + y * frame->linesize[3] + 2 * x);
if (s0 == c0 && s1 == c1 && s2 == c2 && s3 == c3)
return 1;
return 0;
}
static int is_same3(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3)
{
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 (s0 == c0 && s1 == c1 && s2 == c2)
return 1;
return 0;
}
static int is_same3_16(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3)
{
unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
unsigned c1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
unsigned c2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
if (s0 == c0 && s1 == c1 && s2 == c2)
return 1;
return 0;
}
static int is_same1(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3)
{
unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
if (s0 == c0)
return 1;
return 0;
}
static int is_same1_16(const AVFrame *frame, int x, int y,
unsigned s0, unsigned s1, unsigned s2, unsigned s3)
{
unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
if (s0 == c0)
return 1;
return 0;
}
static void set_pixel1(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3)
{
frame->data[0][y * frame->linesize[0] + x] = d0;
}
static void set_pixel1_16(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3)
{
AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
}
static void set_pixel3(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3)
{
frame->data[0][y * frame->linesize[0] + x] = d0;
frame->data[1][y * frame->linesize[1] + x] = d1;
frame->data[2][y * frame->linesize[2] + x] = d2;
}
static void set_pixel3_16(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3)
{
AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
AV_WN16(frame->data[1] + y * frame->linesize[1] + 2 * x, d1);
AV_WN16(frame->data[2] + y * frame->linesize[2] + 2 * x, d2);
}
static void set_pixel4(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3)
{
frame->data[0][y * frame->linesize[0] + x] = d0;
frame->data[1][y * frame->linesize[1] + x] = d1;
frame->data[2][y * frame->linesize[2] + x] = d2;
frame->data[3][y * frame->linesize[3] + x] = d3;
}
static void set_pixel4_16(AVFrame *frame, int x, int y,
unsigned d0, unsigned d1, unsigned d2, unsigned d3)
{
AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
AV_WN16(frame->data[1] + y * frame->linesize[1] + 2 * x, d1);
AV_WN16(frame->data[2] + y * frame->linesize[2] + 2 * x, d2);
AV_WN16(frame->data[3] + y * frame->linesize[3] + 2 * x, d3);
}
static void pick_pixel1(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3)
{
if (*s0 < 0)
*s0 = frame->data[0][y * frame->linesize[0] + x];
}
static void pick_pixel1_16(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3)
{
if (*s0 < 0)
*s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
}
static void pick_pixel3(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3)
{
if (*s0 < 0)
*s0 = frame->data[0][y * frame->linesize[0] + x];
if (*s1 < 0)
*s1 = frame->data[1][y * frame->linesize[1] + x];
if (*s2 < 0)
*s2 = frame->data[2][y * frame->linesize[2] + x];
}
static void pick_pixel3_16(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3)
{
if (*s0 < 0)
*s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
if (*s1 < 0)
*s1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
if (*s2 < 0)
*s2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
}
static void pick_pixel4(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3)
{
if (*s0 < 0)
*s0 = frame->data[0][y * frame->linesize[0] + x];
if (*s1 < 0)
*s1 = frame->data[1][y * frame->linesize[1] + x];
if (*s2 < 0)
*s2 = frame->data[2][y * frame->linesize[2] + x];
if (*s3 < 0)
*s3 = frame->data[3][y * frame->linesize[3] + x];
}
static void pick_pixel4_16(const AVFrame *frame, int x, int y,
int *s0, int *s1, int *s2, int *s3)
{
if (*s0 < 0)
*s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
if (*s1 < 0)
*s1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
if (*s2 < 0)
*s2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
if (*s3 < 0)
*s3 = AV_RN16(frame->data[3] + y * frame->linesize[3] + 2 * x);
}
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;
s->nb_planes = av_pix_fmt_count_planes(inlink->format);
depth = desc->comp[0].depth;
if (depth == 8) {
switch (s->nb_planes) {
case 1: s->set_pixel = set_pixel1;
s->is_same = is_same1;
s->pick_pixel = pick_pixel1; break;
case 3: s->set_pixel = set_pixel3;
s->is_same = is_same3;
s->pick_pixel = pick_pixel3; break;
case 4: s->set_pixel = set_pixel4;
s->is_same = is_same4;
s->pick_pixel = pick_pixel4; break;
}
} else {
switch (s->nb_planes) {
case 1: s->set_pixel = set_pixel1_16;
s->is_same = is_same1_16;
s->pick_pixel = pick_pixel1_16; break;
case 3: s->set_pixel = set_pixel3_16;
s->is_same = is_same3_16;
s->pick_pixel = pick_pixel3_16; break;
case 4: s->set_pixel = set_pixel4_16;
s->is_same = is_same4_16;
s->pick_pixel = pick_pixel4_16; break;
}
}
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 filter_frame(AVFilterLink *link, AVFrame *frame)
{
AVFilterContext *ctx = link->dst;
FloodfillContext *s = ctx->priv;
const unsigned d0 = s->d[0];
const unsigned d1 = s->d[1];
const unsigned d2 = s->d[2];
const unsigned d3 = s->d[3];
int s0 = s->s[0];
int s1 = s->s[1];
int s2 = s->s[2];
int s3 = s->s[3];
const int w = frame->width;
const int h = frame->height;
int i, ret;
if (is_inside(s->x, s->y, w, h)) {
s->pick_pixel(frame, s->x, s->y, &s0, &s1, &s2, &s3);
s->S[0] = s0;
s->S[1] = s1;
s->S[2] = s2;
s->S[3] = s3;
for (i = 0; i < s->nb_planes; i++) {
if (s->S[i] != s->d[i])
break;
}
if (i == s->nb_planes)
goto end;
if (s->is_same(frame, s->x, s->y, s0, s1, s2, s3)) {
s->points[s->front].x = s->x;
s->points[s->front].y = s->y;
s->front++;
}
if (ret = av_frame_make_writable(frame))
return ret;
while (s->front > s->back) {
int x, y;
s->front--;
x = s->points[s->front].x;
y = s->points[s->front].y;
if (s->is_same(frame, x, y, s0, s1, s2, s3)) {
s->set_pixel(frame, x, y, d0, d1, d2, d3);
if (is_inside(x + 1, y, w, h)) {
s->points[s->front] .x = x + 1;
s->points[s->front++].y = y;
}
if (is_inside(x - 1, y, w, h)) {
s->points[s->front] .x = x - 1;
s->points[s->front++].y = y;
}
if (is_inside(x, y + 1, w, h)) {
s->points[s->front] .x = x;
s->points[s->front++].y = y + 1;
}
if (is_inside(x, y - 1, w, h)) {
s->points[s->front] .x = x;
s->points[s->front++].y = y - 1;
}
}
}
}
end:
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_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GBRAP,
AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
AV_PIX_FMT_NONE
};
return ff_set_common_formats_from_list(ctx, pixel_fmts);
}
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,
},
};
static const AVFilterPad floodfill_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
};
#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 },
{ "s0", "set source #0 component value", OFFSET(s[0]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
{ "s1", "set source #1 component value", OFFSET(s[1]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
{ "s2", "set source #2 component value", OFFSET(s[2]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
{ "s3", "set source #3 component value", OFFSET(s[3]), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
{ "d0", "set destination #0 component value", OFFSET(d[0]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
{ "d1", "set destination #1 component value", OFFSET(d[1]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
{ "d2", "set destination #2 component value", OFFSET(d[2]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
{ "d3", "set destination #3 component value", OFFSET(d[3]), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
{ NULL }
};
AVFILTER_DEFINE_CLASS(floodfill);
const 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,
.uninit = uninit,
FILTER_INPUTS(floodfill_inputs),
FILTER_OUTPUTS(floodfill_outputs),
FILTER_QUERY_FUNC(query_formats),
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
};
|