FFmpeg coverage


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