FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_edgedetect.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 75 85 88.2%
Functions: 6 6 100.0%
Branches: 31 42 73.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012-2014 Clément Bœsch <u pkh me>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * Edge detection filter
24 *
25 * @see https://en.wikipedia.org/wiki/Canny_edge_detector
26 */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 #include "edge_common.h"
37
38 #define PLANE_R 0x4
39 #define PLANE_G 0x1
40 #define PLANE_B 0x2
41 #define PLANE_Y 0x1
42 #define PLANE_U 0x2
43 #define PLANE_V 0x4
44 #define PLANE_A 0x8
45
46 enum FilterMode {
47 MODE_WIRES,
48 MODE_COLORMIX,
49 MODE_CANNY,
50 NB_MODE
51 };
52
53 struct plane_info {
54 uint8_t *tmpbuf;
55 uint16_t *gradients;
56 char *directions;
57 int width, height;
58 };
59
60 typedef struct EdgeDetectContext {
61 const AVClass *class;
62 struct plane_info planes[3];
63 int filter_planes;
64 int nb_planes;
65 double low, high;
66 uint8_t low_u8, high_u8;
67 int mode;
68 } EdgeDetectContext;
69
70 #define OFFSET(x) offsetof(EdgeDetectContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
72 static const AVOption edgedetect_options[] = {
73 { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1, FLAGS },
74 { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1, FLAGS },
75 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_WIRES}, 0, NB_MODE-1, FLAGS, .unit = "mode" },
76 { "wires", "white/gray wires on black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WIRES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
77 { "colormix", "mix colors", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLORMIX}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
78 { "canny", "detect edges on planes", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CANNY}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
79 { "planes", "set planes to filter", OFFSET(filter_planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 1, 0x7, FLAGS, .unit = "flags" },
80 { "y", "filter luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, .unit = "flags" },
81 { "u", "filter u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, .unit = "flags" },
82 { "v", "filter v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, .unit = "flags" },
83 { "r", "filter red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, .unit = "flags" },
84 { "g", "filter green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, .unit = "flags" },
85 { "b", "filter blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, .unit = "flags" },
86 { NULL }
87 };
88
89 AVFILTER_DEFINE_CLASS(edgedetect);
90
91 4 static av_cold int init(AVFilterContext *ctx)
92 {
93 4 EdgeDetectContext *edgedetect = ctx->priv;
94
95 4 edgedetect->low_u8 = edgedetect->low * 255. + .5;
96 4 edgedetect->high_u8 = edgedetect->high * 255. + .5;
97 4 return 0;
98 }
99
100 2 static int query_formats(AVFilterContext *ctx)
101 {
102 2 const EdgeDetectContext *edgedetect = ctx->priv;
103 static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
104 static const enum AVPixelFormat canny_pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
105 static const enum AVPixelFormat colormix_pix_fmts[] = {AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
106 2 const enum AVPixelFormat *pix_fmts = NULL;
107
108
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (edgedetect->mode == MODE_WIRES) {
109 1 pix_fmts = wires_pix_fmts;
110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (edgedetect->mode == MODE_COLORMIX) {
111 1 pix_fmts = colormix_pix_fmts;
112 } else if (edgedetect->mode == MODE_CANNY) {
113 pix_fmts = canny_pix_fmts;
114 } else {
115 av_assert0(0);
116 }
117 2 return ff_set_common_formats_from_list(ctx, pix_fmts);
118 }
119
120 2 static int config_props(AVFilterLink *inlink)
121 {
122 int p;
123 2 AVFilterContext *ctx = inlink->dst;
124 2 EdgeDetectContext *edgedetect = ctx->priv;
125 2 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
126
127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 edgedetect->nb_planes = inlink->format == AV_PIX_FMT_GRAY8 ? 1 : 3;
128
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (p = 0; p < edgedetect->nb_planes; p++) {
129 4 struct plane_info *plane = &edgedetect->planes[p];
130
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 int vsub = p ? desc->log2_chroma_h : 0;
131
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 int hsub = p ? desc->log2_chroma_w : 0;
132
133 4 plane->width = AV_CEIL_RSHIFT(inlink->w, hsub);
134 4 plane->height = AV_CEIL_RSHIFT(inlink->h, vsub);
135 4 plane->tmpbuf = av_malloc(plane->width * plane->height);
136 4 plane->gradients = av_calloc(plane->width * plane->height, sizeof(*plane->gradients));
137 4 plane->directions = av_malloc(plane->width * plane->height);
138
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (!plane->tmpbuf || !plane->gradients || !plane->directions)
139 return AVERROR(ENOMEM);
140 }
141 2 return 0;
142 }
143
144 60 static void color_mix(int w, int h,
145 uint8_t *dst, int dst_linesize,
146 const uint8_t *src, int src_linesize)
147 {
148 int i, j;
149
150
2/2
✓ Branch 0 taken 17280 times.
✓ Branch 1 taken 60 times.
17340 for (j = 0; j < h; j++) {
151
2/2
✓ Branch 0 taken 6082560 times.
✓ Branch 1 taken 17280 times.
6099840 for (i = 0; i < w; i++)
152 6082560 dst[i] = (dst[i] + src[i]) >> 1;
153 17280 dst += dst_linesize;
154 17280 src += src_linesize;
155 }
156 60 }
157
158 40 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
159 {
160 40 AVFilterContext *ctx = inlink->dst;
161 40 EdgeDetectContext *edgedetect = ctx->priv;
162 40 AVFilterLink *outlink = ctx->outputs[0];
163 40 int p, direct = 0;
164 AVFrame *out;
165
166
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
40 if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
167 20 direct = 1;
168 20 out = in;
169 } else {
170 20 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!out) {
172 av_frame_free(&in);
173 return AVERROR(ENOMEM);
174 }
175 20 av_frame_copy_props(out, in);
176 }
177
178
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 40 times.
120 for (p = 0; p < edgedetect->nb_planes; p++) {
179 80 struct plane_info *plane = &edgedetect->planes[p];
180 80 uint8_t *tmpbuf = plane->tmpbuf;
181 80 uint16_t *gradients = plane->gradients;
182 80 int8_t *directions = plane->directions;
183 80 const int width = plane->width;
184 80 const int height = plane->height;
185
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (!((1 << p) & edgedetect->filter_planes)) {
187 if (!direct)
188 av_image_copy_plane(out->data[p], out->linesize[p],
189 in->data[p], in->linesize[p],
190 width, height);
191 continue;
192 }
193
194 /* gaussian filter to reduce noise */
195 80 ff_gaussian_blur_8(width, height,
196 tmpbuf, width,
197 80 in->data[p], in->linesize[p], 1);
198
199 /* compute the 16-bits gradients and directions for the next step */
200 80 ff_sobel_8(width, height,
201 gradients, width,
202 directions,width,
203 tmpbuf, width, 1);
204
205 /* non_maximum_suppression() will actually keep & clip what's necessary and
206 * ignore the rest, so we need a clean output buffer */
207 80 memset(tmpbuf, 0, width * height);
208 80 ff_non_maximum_suppression(width, height,
209 tmpbuf, width,
210 directions,width,
211 gradients, width);
212
213 /* keep high values, or low values surrounded by high values */
214 80 ff_double_threshold(edgedetect->low_u8, edgedetect->high_u8,
215 width, height,
216 out->data[p], out->linesize[p],
217 tmpbuf, width);
218
219
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 if (edgedetect->mode == MODE_COLORMIX) {
220 60 color_mix(width, height,
221 out->data[p], out->linesize[p],
222 60 in->data[p], in->linesize[p]);
223 }
224 }
225
226
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 if (!direct)
227 20 av_frame_free(&in);
228 40 return ff_filter_frame(outlink, out);
229 }
230
231 4 static av_cold void uninit(AVFilterContext *ctx)
232 {
233 int p;
234 4 EdgeDetectContext *edgedetect = ctx->priv;
235
236
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (p = 0; p < edgedetect->nb_planes; p++) {
237 4 struct plane_info *plane = &edgedetect->planes[p];
238 4 av_freep(&plane->tmpbuf);
239 4 av_freep(&plane->gradients);
240 4 av_freep(&plane->directions);
241 }
242 4 }
243
244 static const AVFilterPad edgedetect_inputs[] = {
245 {
246 .name = "default",
247 .type = AVMEDIA_TYPE_VIDEO,
248 .config_props = config_props,
249 .filter_frame = filter_frame,
250 },
251 };
252
253 const AVFilter ff_vf_edgedetect = {
254 .name = "edgedetect",
255 .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
256 .priv_size = sizeof(EdgeDetectContext),
257 .init = init,
258 .uninit = uninit,
259 FILTER_INPUTS(edgedetect_inputs),
260 FILTER_OUTPUTS(ff_video_default_filterpad),
261 FILTER_QUERY_FUNC(query_formats),
262 .priv_class = &edgedetect_class,
263 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
264 };
265