2 * Copyright (c) 2012-2014 Clément Bœsch <u pkh me>
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
23 * Edge detection filter
25 * @see https://en.wikipedia.org/wiki/Canny_edge_detector
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
58 typedef struct EdgeDetectContext {
60 struct plane_info planes[3];
64 uint8_t low_u8, high_u8;
68 #define OFFSET(x) offsetof(EdgeDetectContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
70 static const AVOption edgedetect_options[] = {
71 { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1, FLAGS },
72 { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1, FLAGS },
73 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_WIRES}, 0, NB_MODE-1, FLAGS, "mode" },
74 { "wires", "white/gray wires on black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WIRES}, INT_MIN, INT_MAX, FLAGS, "mode" },
75 { "colormix", "mix colors", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLORMIX}, INT_MIN, INT_MAX, FLAGS, "mode" },
76 { "canny", "detect edges on planes", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CANNY}, INT_MIN, INT_MAX, FLAGS, "mode" },
77 { "planes", "set planes to filter", OFFSET(filter_planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 1, 0x7, FLAGS, "flags" },
78 { "y", "filter luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags" },
79 { "u", "filter u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags" },
80 { "v", "filter v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags" },
81 { "r", "filter red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags" },
82 { "g", "filter green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags" },
83 { "b", "filter blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags" },
87 AVFILTER_DEFINE_CLASS(edgedetect);
89 static av_cold int init(AVFilterContext *ctx)
91 EdgeDetectContext *edgedetect = ctx->priv;
93 edgedetect->low_u8 = edgedetect->low * 255. + .5;
94 edgedetect->high_u8 = edgedetect->high * 255. + .5;
98 static int query_formats(AVFilterContext *ctx)
100 const EdgeDetectContext *edgedetect = ctx->priv;
101 static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
102 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};
103 static const enum AVPixelFormat colormix_pix_fmts[] = {AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
104 AVFilterFormats *fmts_list;
105 const enum AVPixelFormat *pix_fmts = NULL;
107 if (edgedetect->mode == MODE_WIRES) {
108 pix_fmts = wires_pix_fmts;
109 } else if (edgedetect->mode == MODE_COLORMIX) {
110 pix_fmts = colormix_pix_fmts;
111 } else if (edgedetect->mode == MODE_CANNY) {
112 pix_fmts = canny_pix_fmts;
116 fmts_list = ff_make_format_list(pix_fmts);
118 return AVERROR(ENOMEM);
119 return ff_set_common_formats(ctx, fmts_list);
122 static int config_props(AVFilterLink *inlink)
125 AVFilterContext *ctx = inlink->dst;
126 EdgeDetectContext *edgedetect = ctx->priv;
127 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
129 edgedetect->nb_planes = inlink->format == AV_PIX_FMT_GRAY8 ? 1 : 3;
130 for (p = 0; p < edgedetect->nb_planes; p++) {
131 struct plane_info *plane = &edgedetect->planes[p];
132 int vsub = p ? desc->log2_chroma_h : 0;
133 int hsub = p ? desc->log2_chroma_w : 0;
135 plane->width = AV_CEIL_RSHIFT(inlink->w, hsub);
136 plane->height = AV_CEIL_RSHIFT(inlink->h, vsub);
137 plane->tmpbuf = av_malloc(plane->width * plane->height);
138 plane->gradients = av_calloc(plane->width * plane->height, sizeof(*plane->gradients));
139 plane->directions = av_malloc(plane->width * plane->height);
140 if (!plane->tmpbuf || !plane->gradients || !plane->directions)
141 return AVERROR(ENOMEM);
146 static void gaussian_blur(AVFilterContext *ctx, int w, int h,
147 uint8_t *dst, int dst_linesize,
148 const uint8_t *src, int src_linesize)
152 memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
153 memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
154 for (j = 2; j < h - 2; j++) {
157 for (i = 2; i < w - 2; i++) {
158 /* Gaussian mask of size 5x5 with sigma = 1.4 */
159 dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2
160 + (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4
161 + (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5
162 + (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4
163 + (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2
165 + (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4
166 + (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9
167 + (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12
168 + (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9
169 + (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4
175 + src[i+2] * 5) / 159;
178 dst[i + 1] = src[i + 1];
183 memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
190 DIRECTION_HORIZONTAL,
194 static int get_rounded_direction(int gx, int gy)
197 * tan( pi/8) = sqrt(2)-1
198 * tan(3pi/8) = sqrt(2)+1
199 * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
200 * <ref-angle>, or more simply Gy against <ref-angle>*Gx
202 * Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
203 * round((sqrt(2)-1) * (1<<16)) = 27146
204 * round((sqrt(2)+1) * (1<<16)) = 158218
207 int tanpi8gx, tan3pi8gx;
212 tanpi8gx = 27146 * gx;
213 tan3pi8gx = 158218 * gx;
214 if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
215 if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
216 if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
218 return DIRECTION_VERTICAL;
221 static void sobel(int w, int h,
222 uint16_t *dst, int dst_linesize,
223 int8_t *dir, int dir_linesize,
224 const uint8_t *src, int src_linesize)
228 for (j = 1; j < h - 1; j++) {
232 for (i = 1; i < w - 1; i++) {
234 -1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1]
235 -2*src[ i-1] + 2*src[ i+1]
236 -1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1];
238 -1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1]
239 -2*src[-src_linesize + i ] + 2*src[ src_linesize + i ]
240 -1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1];
242 dst[i] = FFABS(gx) + FFABS(gy);
243 dir[i] = get_rounded_direction(gx, gy);
248 static void non_maximum_suppression(int w, int h,
249 uint8_t *dst, int dst_linesize,
250 const int8_t *dir, int dir_linesize,
251 const uint16_t *src, int src_linesize)
255 #define COPY_MAXIMA(ay, ax, by, bx) do { \
256 if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
257 src[i] > src[(by)*src_linesize + i+(bx)]) \
258 dst[i] = av_clip_uint8(src[i]); \
261 for (j = 1; j < h - 1; j++) {
265 for (i = 1; i < w - 1; i++) {
267 case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
268 case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
269 case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
270 case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
276 static void double_threshold(int low, int high, int w, int h,
277 uint8_t *dst, int dst_linesize,
278 const uint8_t *src, int src_linesize)
282 for (j = 0; j < h; j++) {
283 for (i = 0; i < w; i++) {
289 if ((!i || i == w - 1 || !j || j == h - 1) &&
291 (src[-src_linesize + i-1] > high ||
292 src[-src_linesize + i ] > high ||
293 src[-src_linesize + i+1] > high ||
296 src[ src_linesize + i-1] > high ||
297 src[ src_linesize + i ] > high ||
298 src[ src_linesize + i+1] > high))
308 static void color_mix(int w, int h,
309 uint8_t *dst, int dst_linesize,
310 const uint8_t *src, int src_linesize)
314 for (j = 0; j < h; j++) {
315 for (i = 0; i < w; i++)
316 dst[i] = (dst[i] + src[i]) >> 1;
322 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
324 AVFilterContext *ctx = inlink->dst;
325 EdgeDetectContext *edgedetect = ctx->priv;
326 AVFilterLink *outlink = ctx->outputs[0];
330 if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
334 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
337 return AVERROR(ENOMEM);
339 av_frame_copy_props(out, in);
342 for (p = 0; p < edgedetect->nb_planes; p++) {
343 struct plane_info *plane = &edgedetect->planes[p];
344 uint8_t *tmpbuf = plane->tmpbuf;
345 uint16_t *gradients = plane->gradients;
346 int8_t *directions = plane->directions;
347 const int width = plane->width;
348 const int height = plane->height;
350 if (!((1 << p) & edgedetect->filter_planes)) {
352 av_image_copy_plane(out->data[p], out->linesize[p],
353 in->data[p], in->linesize[p],
358 /* gaussian filter to reduce noise */
359 gaussian_blur(ctx, width, height,
361 in->data[p], in->linesize[p]);
363 /* compute the 16-bits gradients and directions for the next step */
369 /* non_maximum_suppression() will actually keep & clip what's necessary and
370 * ignore the rest, so we need a clean output buffer */
371 memset(tmpbuf, 0, width * height);
372 non_maximum_suppression(width, height,
377 /* keep high values, or low values surrounded by high values */
378 double_threshold(edgedetect->low_u8, edgedetect->high_u8,
380 out->data[p], out->linesize[p],
383 if (edgedetect->mode == MODE_COLORMIX) {
384 color_mix(width, height,
385 out->data[p], out->linesize[p],
386 in->data[p], in->linesize[p]);
392 return ff_filter_frame(outlink, out);
395 static av_cold void uninit(AVFilterContext *ctx)
398 EdgeDetectContext *edgedetect = ctx->priv;
400 for (p = 0; p < edgedetect->nb_planes; p++) {
401 struct plane_info *plane = &edgedetect->planes[p];
402 av_freep(&plane->tmpbuf);
403 av_freep(&plane->gradients);
404 av_freep(&plane->directions);
408 static const AVFilterPad edgedetect_inputs[] = {
411 .type = AVMEDIA_TYPE_VIDEO,
412 .config_props = config_props,
413 .filter_frame = filter_frame,
418 static const AVFilterPad edgedetect_outputs[] = {
421 .type = AVMEDIA_TYPE_VIDEO,
426 AVFilter ff_vf_edgedetect = {
427 .name = "edgedetect",
428 .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
429 .priv_size = sizeof(EdgeDetectContext),
432 .query_formats = query_formats,
433 .inputs = edgedetect_inputs,
434 .outputs = edgedetect_outputs,
435 .priv_class = &edgedetect_class,
436 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,