... | ... |
@@ -1929,6 +1929,33 @@ For more information about libfreetype, check: |
1929 | 1929 |
For more information about fontconfig, check: |
1930 | 1930 |
@url{http://freedesktop.org/software/fontconfig/fontconfig-user.html}. |
1931 | 1931 |
|
1932 |
+@section edgedetect |
|
1933 |
+ |
|
1934 |
+Detect and draw edges. The filter uses the Canny Edge Detection algorithm. |
|
1935 |
+ |
|
1936 |
+This filter accepts the following optional named parameters: |
|
1937 |
+ |
|
1938 |
+@table @option |
|
1939 |
+@item low, high |
|
1940 |
+Set low and high threshold values used by the Canny thresholding |
|
1941 |
+algorithm. |
|
1942 |
+ |
|
1943 |
+The high threshold selects the "strong" edge pixels, which are then |
|
1944 |
+connected through 8-connectivity with the "weak" edge pixels selected |
|
1945 |
+by the low threshold. |
|
1946 |
+ |
|
1947 |
+@var{low} and @var{high} threshold values must be choosen in the range |
|
1948 |
+[0,1], and @var{low} should be lesser or equal to @var{high}. |
|
1949 |
+ |
|
1950 |
+Default value for @var{low} is @code{20/255}, and default value for @var{high} |
|
1951 |
+is @code{50/255}. |
|
1952 |
+@end table |
|
1953 |
+ |
|
1954 |
+Example: |
|
1955 |
+@example |
|
1956 |
+edgedetect=low=0.1:high=0.4 |
|
1957 |
+@end example |
|
1958 |
+ |
|
1932 | 1959 |
@section fade |
1933 | 1960 |
|
1934 | 1961 |
Apply fade-in/out effect to input video. |
... | ... |
@@ -90,6 +90,7 @@ OBJS-$(CONFIG_DELOGO_FILTER) += vf_delogo.o |
90 | 90 |
OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o |
91 | 91 |
OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o |
92 | 92 |
OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o |
93 |
+OBJS-$(CONFIG_EDGEDETECT_FILTER) += vf_edgedetect.o |
|
93 | 94 |
OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o |
94 | 95 |
OBJS-$(CONFIG_FIELDORDER_FILTER) += vf_fieldorder.o |
95 | 96 |
OBJS-$(CONFIG_FIFO_FILTER) += fifo.o |
... | ... |
@@ -81,6 +81,7 @@ void avfilter_register_all(void) |
81 | 81 |
REGISTER_FILTER (DESHAKE, deshake, vf); |
82 | 82 |
REGISTER_FILTER (DRAWBOX, drawbox, vf); |
83 | 83 |
REGISTER_FILTER (DRAWTEXT, drawtext, vf); |
84 |
+ REGISTER_FILTER (EDGEDETECT, edgedetect, vf); |
|
84 | 85 |
REGISTER_FILTER (FADE, fade, vf); |
85 | 86 |
REGISTER_FILTER (FIELDORDER, fieldorder, vf); |
86 | 87 |
REGISTER_FILTER (FIFO, fifo, vf); |
... | ... |
@@ -29,8 +29,8 @@ |
29 | 29 |
#include "libavutil/avutil.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
32 |
-#define LIBAVFILTER_VERSION_MINOR 9 |
|
33 |
-#define LIBAVFILTER_VERSION_MICRO 102 |
|
32 |
+#define LIBAVFILTER_VERSION_MINOR 10 |
|
33 |
+#define LIBAVFILTER_VERSION_MICRO 100 |
|
34 | 34 |
|
35 | 35 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
36 | 36 |
LIBAVFILTER_VERSION_MINOR, \ |
37 | 37 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,323 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Clément Bœsch |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @file |
|
22 |
+ * Edge detection filter |
|
23 |
+ * |
|
24 |
+ * @url https://en.wikipedia.org/wiki/Canny_edge_detector |
|
25 |
+ */ |
|
26 |
+ |
|
27 |
+#include "libavutil/opt.h" |
|
28 |
+#include "avfilter.h" |
|
29 |
+#include "formats.h" |
|
30 |
+#include "internal.h" |
|
31 |
+#include "video.h" |
|
32 |
+ |
|
33 |
+typedef struct { |
|
34 |
+ const AVClass *class; |
|
35 |
+ uint8_t *tmpbuf; |
|
36 |
+ uint16_t *gradients; |
|
37 |
+ char *directions; |
|
38 |
+ double low, high; |
|
39 |
+ uint8_t low_u8, high_u8; |
|
40 |
+} EdgeDetectContext; |
|
41 |
+ |
|
42 |
+#define OFFSET(x) offsetof(EdgeDetectContext, x) |
|
43 |
+static const AVOption edgedetect_options[] = { |
|
44 |
+ { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1 }, |
|
45 |
+ { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1 }, |
|
46 |
+ { NULL }, |
|
47 |
+}; |
|
48 |
+ |
|
49 |
+AVFILTER_DEFINE_CLASS(edgedetect); |
|
50 |
+ |
|
51 |
+static av_cold int init(AVFilterContext *ctx, const char *args) |
|
52 |
+{ |
|
53 |
+ int ret; |
|
54 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
55 |
+ |
|
56 |
+ edgedetect->class = &edgedetect_class; |
|
57 |
+ av_opt_set_defaults(edgedetect); |
|
58 |
+ |
|
59 |
+ if ((ret = av_set_options_string(edgedetect, args, "=", ":")) < 0) |
|
60 |
+ return ret; |
|
61 |
+ |
|
62 |
+ edgedetect->low_u8 = edgedetect->low * 255.; |
|
63 |
+ edgedetect->high_u8 = edgedetect->high * 255.; |
|
64 |
+ return 0; |
|
65 |
+} |
|
66 |
+ |
|
67 |
+static int query_formats(AVFilterContext *ctx) |
|
68 |
+{ |
|
69 |
+ static const enum PixelFormat pix_fmts[] = {PIX_FMT_GRAY8, PIX_FMT_NONE}; |
|
70 |
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
|
71 |
+ return 0; |
|
72 |
+} |
|
73 |
+ |
|
74 |
+static int config_props(AVFilterLink *inlink) |
|
75 |
+{ |
|
76 |
+ AVFilterContext *ctx = inlink->dst; |
|
77 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
78 |
+ |
|
79 |
+ edgedetect->tmpbuf = av_malloc(inlink->w * inlink->h); |
|
80 |
+ edgedetect->gradients = av_calloc(inlink->w * inlink->h, sizeof(*edgedetect->gradients)); |
|
81 |
+ edgedetect->directions = av_malloc(inlink->w * inlink->h); |
|
82 |
+ if (!edgedetect->tmpbuf || !edgedetect->gradients || !edgedetect->directions) |
|
83 |
+ return AVERROR(ENOMEM); |
|
84 |
+ return 0; |
|
85 |
+} |
|
86 |
+ |
|
87 |
+static void gaussian_blur(AVFilterContext *ctx, int w, int h, |
|
88 |
+ uint8_t *dst, int dst_linesize, |
|
89 |
+ const uint8_t *src, int src_linesize) |
|
90 |
+{ |
|
91 |
+ int i, j; |
|
92 |
+ |
|
93 |
+ memcpy(dst, src, w); dst += dst_linesize; src += src_linesize; |
|
94 |
+ memcpy(dst, src, w); dst += dst_linesize; src += src_linesize; |
|
95 |
+ for (j = 2; j < h - 2; j++) { |
|
96 |
+ dst[0] = src[0]; |
|
97 |
+ dst[1] = src[1]; |
|
98 |
+ for (i = 2; i < w - 2; i++) { |
|
99 |
+ /* Gaussian mask of size 5x5 with sigma = 1.4 */ |
|
100 |
+ dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2 |
|
101 |
+ + (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4 |
|
102 |
+ + (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5 |
|
103 |
+ + (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4 |
|
104 |
+ + (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2 |
|
105 |
+ |
|
106 |
+ + (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4 |
|
107 |
+ + (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9 |
|
108 |
+ + (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12 |
|
109 |
+ + (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9 |
|
110 |
+ + (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4 |
|
111 |
+ |
|
112 |
+ + src[i-2] * 5 |
|
113 |
+ + src[i-1] * 12 |
|
114 |
+ + src[i ] * 15 |
|
115 |
+ + src[i+1] * 12 |
|
116 |
+ + src[i+2] * 5) / 159; |
|
117 |
+ } |
|
118 |
+ dst[i ] = src[i ]; |
|
119 |
+ dst[i + 1] = src[i + 1]; |
|
120 |
+ |
|
121 |
+ dst += dst_linesize; |
|
122 |
+ src += src_linesize; |
|
123 |
+ } |
|
124 |
+ memcpy(dst, src, w); dst += dst_linesize; src += src_linesize; |
|
125 |
+ memcpy(dst, src, w); |
|
126 |
+} |
|
127 |
+ |
|
128 |
+enum { |
|
129 |
+ DIRECTION_45UP, |
|
130 |
+ DIRECTION_45DOWN, |
|
131 |
+ DIRECTION_HORIZONTAL, |
|
132 |
+ DIRECTION_VERTICAL, |
|
133 |
+}; |
|
134 |
+ |
|
135 |
+static int get_rounded_direction(int gx, int gy) |
|
136 |
+{ |
|
137 |
+ /* reference angles: |
|
138 |
+ * tan( pi/8) = sqrt(2)-1 |
|
139 |
+ * tan(3pi/8) = sqrt(2)+1 |
|
140 |
+ * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against |
|
141 |
+ * <ref-angle>, or more simply Gy against <ref-angle>*Gx |
|
142 |
+ * |
|
143 |
+ * Gx and Gy bounds = [1020;1020], using 16-bit arithmetic: |
|
144 |
+ * round((sqrt(2)-1) * (1<<16)) = 27146 |
|
145 |
+ * round((sqrt(2)+1) * (1<<16)) = 158218 |
|
146 |
+ */ |
|
147 |
+ if (gx) { |
|
148 |
+ int tanpi8gx, tan3pi8gx; |
|
149 |
+ |
|
150 |
+ if (gx < 0) |
|
151 |
+ gx = -gx, gy = -gy; |
|
152 |
+ gy <<= 16; |
|
153 |
+ tanpi8gx = 27146 * gx; |
|
154 |
+ tan3pi8gx = 158218 * gx; |
|
155 |
+ if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP; |
|
156 |
+ if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL; |
|
157 |
+ if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN; |
|
158 |
+ } |
|
159 |
+ return DIRECTION_VERTICAL; |
|
160 |
+} |
|
161 |
+ |
|
162 |
+static void sobel(AVFilterContext *ctx, int w, int h, |
|
163 |
+ uint16_t *dst, int dst_linesize, |
|
164 |
+ const uint8_t *src, int src_linesize) |
|
165 |
+{ |
|
166 |
+ int i, j; |
|
167 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
168 |
+ |
|
169 |
+ for (j = 1; j < h - 1; j++) { |
|
170 |
+ dst += dst_linesize; |
|
171 |
+ src += src_linesize; |
|
172 |
+ for (i = 1; i < w - 1; i++) { |
|
173 |
+ const int gx = |
|
174 |
+ -1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1] |
|
175 |
+ -2*src[ i-1] + 2*src[ i+1] |
|
176 |
+ -1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1]; |
|
177 |
+ const int gy = |
|
178 |
+ -1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1] |
|
179 |
+ -2*src[-src_linesize + i ] + 2*src[ src_linesize + i ] |
|
180 |
+ -1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1]; |
|
181 |
+ |
|
182 |
+ dst[i] = FFABS(gx) + FFABS(gy); |
|
183 |
+ edgedetect->directions[j*w + i] = get_rounded_direction(gx, gy); |
|
184 |
+ } |
|
185 |
+ } |
|
186 |
+} |
|
187 |
+ |
|
188 |
+static void non_maximum_suppression(AVFilterContext *ctx, int w, int h, |
|
189 |
+ uint8_t *dst, int dst_linesize, |
|
190 |
+ const uint16_t *src, int src_linesize) |
|
191 |
+{ |
|
192 |
+ int i, j; |
|
193 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
194 |
+ |
|
195 |
+#define COPY_MAXIMA(ay, ax, by, bx) do { \ |
|
196 |
+ if (src[i] > src[(ay)*src_linesize + i+(ax)] && \ |
|
197 |
+ src[i] > src[(by)*src_linesize + i+(bx)]) \ |
|
198 |
+ dst[i] = av_clip_uint8(src[i]); \ |
|
199 |
+} while (0) |
|
200 |
+ |
|
201 |
+ for (j = 1; j < h - 1; j++) { |
|
202 |
+ dst += dst_linesize; |
|
203 |
+ src += src_linesize; |
|
204 |
+ for (i = 1; i < w - 1; i++) { |
|
205 |
+ switch (edgedetect->directions[j*w + i]) { |
|
206 |
+ case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break; |
|
207 |
+ case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break; |
|
208 |
+ case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break; |
|
209 |
+ case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break; |
|
210 |
+ } |
|
211 |
+ } |
|
212 |
+ } |
|
213 |
+} |
|
214 |
+ |
|
215 |
+static void double_threshold(AVFilterContext *ctx, int w, int h, |
|
216 |
+ uint8_t *dst, int dst_linesize, |
|
217 |
+ const uint8_t *src, int src_linesize) |
|
218 |
+{ |
|
219 |
+ int i, j; |
|
220 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
221 |
+ const int low = edgedetect->low_u8; |
|
222 |
+ const int high = edgedetect->high_u8; |
|
223 |
+ |
|
224 |
+ for (j = 0; j < h; j++) { |
|
225 |
+ for (i = 0; i < w; i++) { |
|
226 |
+ if (src[i] > high) { |
|
227 |
+ dst[i] = src[i]; |
|
228 |
+ continue; |
|
229 |
+ } |
|
230 |
+ |
|
231 |
+ if ((!i || i == w - 1 || !j || j == h - 1) && |
|
232 |
+ src[i] > low && |
|
233 |
+ (src[-src_linesize + i-1] > high || |
|
234 |
+ src[-src_linesize + i ] > high || |
|
235 |
+ src[-src_linesize + i+1] > high || |
|
236 |
+ src[ i-1] > high || |
|
237 |
+ src[ i+1] > high || |
|
238 |
+ src[ src_linesize + i-1] > high || |
|
239 |
+ src[ src_linesize + i ] > high || |
|
240 |
+ src[ src_linesize + i+1] > high)) |
|
241 |
+ dst[i] = src[i]; |
|
242 |
+ else |
|
243 |
+ dst[i] = 0; |
|
244 |
+ } |
|
245 |
+ dst += dst_linesize; |
|
246 |
+ src += src_linesize; |
|
247 |
+ } |
|
248 |
+} |
|
249 |
+ |
|
250 |
+static int end_frame(AVFilterLink *inlink) |
|
251 |
+{ |
|
252 |
+ AVFilterContext *ctx = inlink->dst; |
|
253 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
254 |
+ AVFilterLink *outlink = inlink->dst->outputs[0]; |
|
255 |
+ AVFilterBufferRef *inpicref = inlink->cur_buf; |
|
256 |
+ AVFilterBufferRef *outpicref = outlink->out_buf; |
|
257 |
+ uint8_t *tmpbuf = edgedetect->tmpbuf; |
|
258 |
+ uint16_t *gradients = edgedetect->gradients; |
|
259 |
+ |
|
260 |
+ /* gaussian filter to reduce noise */ |
|
261 |
+ gaussian_blur(ctx, inlink->w, inlink->h, |
|
262 |
+ tmpbuf, inlink->w, |
|
263 |
+ inpicref->data[0], inpicref->linesize[0]); |
|
264 |
+ |
|
265 |
+ /* compute the 16-bits gradients and directions for the next step */ |
|
266 |
+ sobel(ctx, inlink->w, inlink->h, |
|
267 |
+ gradients, inlink->w, |
|
268 |
+ tmpbuf, inlink->w); |
|
269 |
+ |
|
270 |
+ /* non_maximum_suppression() will actually keep & clip what's necessary and |
|
271 |
+ * ignore the rest, so we need a clean output buffer */ |
|
272 |
+ memset(tmpbuf, 0, inlink->w * inlink->h); |
|
273 |
+ non_maximum_suppression(ctx, inlink->w, inlink->h, |
|
274 |
+ tmpbuf, inlink->w, |
|
275 |
+ gradients, inlink->w); |
|
276 |
+ |
|
277 |
+ /* keep high values, or low values surrounded by high values */ |
|
278 |
+ double_threshold(ctx, inlink->w, inlink->h, |
|
279 |
+ outpicref->data[0], outpicref->linesize[0], |
|
280 |
+ tmpbuf, inlink->w); |
|
281 |
+ |
|
282 |
+ ff_draw_slice(outlink, 0, outlink->h, 1); |
|
283 |
+ return ff_end_frame(outlink); |
|
284 |
+} |
|
285 |
+ |
|
286 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
287 |
+{ |
|
288 |
+ EdgeDetectContext *edgedetect = ctx->priv; |
|
289 |
+ av_freep(&edgedetect->tmpbuf); |
|
290 |
+ av_freep(&edgedetect->gradients); |
|
291 |
+ av_freep(&edgedetect->directions); |
|
292 |
+} |
|
293 |
+ |
|
294 |
+static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; } |
|
295 |
+ |
|
296 |
+AVFilter avfilter_vf_edgedetect = { |
|
297 |
+ .name = "edgedetect", |
|
298 |
+ .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."), |
|
299 |
+ .priv_size = sizeof(EdgeDetectContext), |
|
300 |
+ .init = init, |
|
301 |
+ .uninit = uninit, |
|
302 |
+ .query_formats = query_formats, |
|
303 |
+ |
|
304 |
+ .inputs = (const AVFilterPad[]) { |
|
305 |
+ { |
|
306 |
+ .name = "default", |
|
307 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
308 |
+ .draw_slice = null_draw_slice, |
|
309 |
+ .config_props = config_props, |
|
310 |
+ .end_frame = end_frame, |
|
311 |
+ .min_perms = AV_PERM_READ |
|
312 |
+ }, |
|
313 |
+ { .name = NULL } |
|
314 |
+ }, |
|
315 |
+ .outputs = (const AVFilterPad[]) { |
|
316 |
+ { |
|
317 |
+ .name = "default", |
|
318 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
319 |
+ }, |
|
320 |
+ { .name = NULL } |
|
321 |
+ }, |
|
322 |
+}; |
... | ... |
@@ -42,6 +42,7 @@ do_lavfi "crop_scale" "crop=iw-100:ih-100:100:100,scale=400:-1" |
42 | 42 |
do_lavfi "crop_scale_vflip" "null,null,crop=iw-200:ih-200:200:200,crop=iw-20:ih-20:20:20,scale=200:200,scale=250:250,vflip,vflip,null,scale=200:200,crop=iw-100:ih-100:100:100,vflip,scale=200:200,null,vflip,crop=iw-100:ih-100:100:100,null" |
43 | 43 |
do_lavfi "crop_vflip" "crop=iw-100:ih-100:100:100,vflip" |
44 | 44 |
do_lavfi "drawbox" "drawbox=224:24:88:72:#FF8010@0.5" |
45 |
+do_lavfi "edgedetect" "edgedetect" |
|
45 | 46 |
do_lavfi "fade" "fade=in:5:15,fade=out:30:15" |
46 | 47 |
do_lavfi "null" "null" |
47 | 48 |
do_lavfi "overlay" "split[m],scale=88:72,pad=96:80:4:4[o2];[m]fifo[o1],[o1][o2]overlay=240:16" |