• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
init(AVFilterContext * ctx)90 static av_cold int init(AVFilterContext *ctx)
91 {
92     EdgeDetectContext *edgedetect = ctx->priv;
93 
94     edgedetect->low_u8  = edgedetect->low  * 255. + .5;
95     edgedetect->high_u8 = edgedetect->high * 255. + .5;
96     return 0;
97 }
98 
query_formats(AVFilterContext * ctx)99 static int query_formats(AVFilterContext *ctx)
100 {
101     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     const enum AVPixelFormat *pix_fmts = NULL;
106 
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;
113     } else {
114         av_assert0(0);
115     }
116     return ff_set_common_formats_from_list(ctx, pix_fmts);
117 }
118 
config_props(AVFilterLink * inlink)119 static int config_props(AVFilterLink *inlink)
120 {
121     int p;
122     AVFilterContext *ctx = inlink->dst;
123     EdgeDetectContext *edgedetect = ctx->priv;
124     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
125 
126     edgedetect->nb_planes = inlink->format == AV_PIX_FMT_GRAY8 ? 1 : 3;
127     for (p = 0; p < edgedetect->nb_planes; p++) {
128         struct plane_info *plane = &edgedetect->planes[p];
129         int vsub = p ? desc->log2_chroma_h : 0;
130         int hsub = p ? desc->log2_chroma_w : 0;
131 
132         plane->width      = AV_CEIL_RSHIFT(inlink->w, hsub);
133         plane->height     = AV_CEIL_RSHIFT(inlink->h, vsub);
134         plane->tmpbuf     = av_malloc(plane->width * plane->height);
135         plane->gradients  = av_calloc(plane->width * plane->height, sizeof(*plane->gradients));
136         plane->directions = av_malloc(plane->width * plane->height);
137         if (!plane->tmpbuf || !plane->gradients || !plane->directions)
138             return AVERROR(ENOMEM);
139     }
140     return 0;
141 }
142 
color_mix(int w,int h,uint8_t * dst,int dst_linesize,const uint8_t * src,int src_linesize)143 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     for (j = 0; j < h; j++) {
150         for (i = 0; i < w; i++)
151             dst[i] = (dst[i] + src[i]) >> 1;
152         dst += dst_linesize;
153         src += src_linesize;
154     }
155 }
156 
filter_frame(AVFilterLink * inlink,AVFrame * in)157 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
158 {
159     AVFilterContext *ctx = inlink->dst;
160     EdgeDetectContext *edgedetect = ctx->priv;
161     AVFilterLink *outlink = ctx->outputs[0];
162     int p, direct = 0;
163     AVFrame *out;
164 
165     if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
166         direct = 1;
167         out = in;
168     } else {
169         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
170         if (!out) {
171             av_frame_free(&in);
172             return AVERROR(ENOMEM);
173         }
174         av_frame_copy_props(out, in);
175     }
176 
177     for (p = 0; p < edgedetect->nb_planes; p++) {
178         struct plane_info *plane = &edgedetect->planes[p];
179         uint8_t  *tmpbuf     = plane->tmpbuf;
180         uint16_t *gradients  = plane->gradients;
181         int8_t   *directions = plane->directions;
182         const int width      = plane->width;
183         const int height     = plane->height;
184 
185         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         ff_gaussian_blur(width, height,
195                          tmpbuf,      width,
196                          in->data[p], in->linesize[p]);
197 
198         /* compute the 16-bits gradients and directions for the next step */
199         ff_sobel(width, height,
200               gradients, width,
201               directions,width,
202               tmpbuf,    width);
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         memset(tmpbuf, 0, width * height);
207         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         ff_double_threshold(edgedetect->low_u8, edgedetect->high_u8,
214                          width, height,
215                          out->data[p], out->linesize[p],
216                          tmpbuf,       width);
217 
218         if (edgedetect->mode == MODE_COLORMIX) {
219             color_mix(width, height,
220                       out->data[p], out->linesize[p],
221                       in->data[p], in->linesize[p]);
222         }
223     }
224 
225     if (!direct)
226         av_frame_free(&in);
227     return ff_filter_frame(outlink, out);
228 }
229 
uninit(AVFilterContext * ctx)230 static av_cold void uninit(AVFilterContext *ctx)
231 {
232     int p;
233     EdgeDetectContext *edgedetect = ctx->priv;
234 
235     for (p = 0; p < edgedetect->nb_planes; p++) {
236         struct plane_info *plane = &edgedetect->planes[p];
237         av_freep(&plane->tmpbuf);
238         av_freep(&plane->gradients);
239         av_freep(&plane->directions);
240     }
241 }
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