[FFmpeg-devel] [PATCH][WIP] lavfi: edgedetect filter
Clément Bœsch
ubitux at gmail.com
Sun Aug 5 10:41:55 CEST 2012
---
Still a lot of things to do, fix and optimize, but results are good enough for
a first demonstration. I'm just sharing this to prevent SOCIS students (or
other people) to duplicate work.
---
doc/filters.texi | 4 +
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_edgedetect.c | 288 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 294 insertions(+)
create mode 100644 libavfilter/vf_edgedetect.c
diff --git a/doc/filters.texi b/doc/filters.texi
index 2609065..cb54409 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1929,6 +1929,10 @@ For more information about libfreetype, check:
For more information about fontconfig, check:
@url{http://freedesktop.org/software/fontconfig/fontconfig-user.html}.
+ at section edgedetect
+
+Detect and draw edges. No parameter available.
+
@section fade
Apply fade-in/out effect to input video.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 727ab4e..da657a2 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -89,6 +89,7 @@ OBJS-$(CONFIG_DELOGO_FILTER) += vf_delogo.o
OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o
OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o
OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o
+OBJS-$(CONFIG_EDGEDETECT_FILTER) += vf_edgedetect.o
OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o
OBJS-$(CONFIG_FIELDORDER_FILTER) += vf_fieldorder.o
OBJS-$(CONFIG_FIFO_FILTER) += fifo.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 403383d..858b9e6 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -79,6 +79,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (DESHAKE, deshake, vf);
REGISTER_FILTER (DRAWBOX, drawbox, vf);
REGISTER_FILTER (DRAWTEXT, drawtext, vf);
+ REGISTER_FILTER (EDGEDETECT, edgedetect, vf);
REGISTER_FILTER (FADE, fade, vf);
REGISTER_FILTER (FIELDORDER, fieldorder, vf);
REGISTER_FILTER (FIFO, fifo, vf);
diff --git a/libavfilter/vf_edgedetect.c b/libavfilter/vf_edgedetect.c
new file mode 100644
index 0000000..baf0dc2
--- /dev/null
+++ b/libavfilter/vf_edgedetect.c
@@ -0,0 +1,288 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Edge detection filter
+ */
+
+#include <math.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/eval.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct {
+ uint8_t *tmpbuf1;
+ uint8_t *tmpbuf2;
+ char *directions;
+} EdgeDetectContext;
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum PixelFormat pix_fmts[] = {PIX_FMT_GRAY8, PIX_FMT_NONE};
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+ return 0;
+}
+
+static int config_props(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ EdgeDetectContext *edgedetect = ctx->priv;
+
+ edgedetect->tmpbuf1 = av_malloc(inlink->w * inlink->h);
+ edgedetect->tmpbuf2 = av_malloc(inlink->w * inlink->h);
+ edgedetect->directions = av_malloc(inlink->w * inlink->h);
+ if (!edgedetect->tmpbuf1 || !edgedetect->tmpbuf2 || !edgedetect->directions)
+ return AVERROR(ENOMEM);
+ return 0;
+}
+
+static void gaussian_blur(AVFilterContext *ctx, int w, int h,
+ uint8_t *dst, int dst_linesize,
+ const uint8_t *src, int src_linesize)
+{
+ int i, j;
+
+ for (j = 0; j < h; j++) {
+ for (i = 0; i < w; i++) {
+ if (i < 2 || i >= w - 2 || j < 2 || j >= h - 2) {
+ // FIXME: necessary?
+ dst[j*dst_linesize + i] = src[j*src_linesize + i];
+ } else // TODO: optimize? (can be approximated)
+ dst[j*dst_linesize + i] =
+ ((src[(j-2)*src_linesize + (i-2)] + src[(j+2)*src_linesize + (i-2)]) * 2
+ + (src[(j-2)*src_linesize + (i-1)] + src[(j+2)*src_linesize + (i-1)]) * 4
+ + (src[(j-2)*src_linesize + i ] + src[(j+2)*src_linesize + i ]) * 5
+ + (src[(j-2)*src_linesize + (i+1)] + src[(j+2)*src_linesize + (i+1)]) * 4
+ + (src[(j-2)*src_linesize + (i+2)] + src[(j+2)*src_linesize + (i+2)]) * 2
+
+ + (src[(j-1)*src_linesize + (i-2)] + src[(j-1)*src_linesize + (i-2)])* 4
+ + (src[(j-1)*src_linesize + (i-1)] + src[(j-1)*src_linesize + (i-1)])* 9
+ + (src[(j-1)*src_linesize + i ] + src[(j-1)*src_linesize + i ])* 12
+ + (src[(j-1)*src_linesize + (i+1)] + src[(j-1)*src_linesize + (i+1)])* 9
+ + (src[(j-1)*src_linesize + (i+2)] + src[(j-1)*src_linesize + (i+2)])* 4
+
+ + src[j*src_linesize + (i-2)] * 5
+ + src[j*src_linesize + (i-1)] * 12
+ + src[j*src_linesize + i ] * 15
+ + src[j*src_linesize + (i+1)] * 12
+ + src[j*src_linesize + (i+2)] * 5) / 159;
+ }
+ }
+}
+
+enum {
+ DIRECTION_45UP,
+ DIRECTION_45DOWN,
+ DIRECTION_HORIZONTAL,
+ DIRECTION_VERTICAL,
+};
+
+static int get_closer_direction(int gx, int gy)
+{
+ // TODO: avoid atan2() call by using the y/x tan
+ const double theta = atan2(FFABS(gy), FFABS(gx));
+
+ if (theta < 3*M_PI/8 && theta > M_PI/8) {
+ if ((gx > 0 && gy > 0) || (gx < 0 && gy < 0))
+ return DIRECTION_45DOWN;
+ else
+ return DIRECTION_45UP;
+ }
+ if (theta < M_PI/8)
+ return DIRECTION_HORIZONTAL;
+ return DIRECTION_VERTICAL;
+}
+
+static void sobel(AVFilterContext *ctx, int w, int h,
+ uint8_t *dst, int dst_linesize,
+ const uint8_t *src, int src_linesize)
+{
+ int i, j;
+ EdgeDetectContext *edgedetect = ctx->priv;
+
+ for (j = 0; j < h; j++) {
+ for (i = 0; i < w; i++) {
+ if (!i || i == w - 1 || !j || j == h - 1) {
+ // FIXME: necessary?
+ dst[j*dst_linesize + i] = src[j*src_linesize + i];
+ } else {
+ const int gx =
+ -1*src[(j-1)*src_linesize + i-1] + 1*src[(j-1)*src_linesize + i+1]
+ -2*src[ j *src_linesize + i-1] + 2*src[ j *src_linesize + i+1]
+ -1*src[(j+1)*src_linesize + i-1] + 1*src[(j+1)*src_linesize + i+1];
+ const int gy =
+ -1*src[(j-1)*src_linesize + i-1] + 1*src[(j+1)*src_linesize + i-1]
+ -2*src[(j-1)*src_linesize + i ] + 2*src[(j+1)*src_linesize + i ]
+ -1*src[(j-1)*src_linesize + i+1] + 1*src[(j+1)*src_linesize + i+1];
+
+ dst[j*dst_linesize + i] = av_clip_uint8(FFABS(gx) + FFABS(gy));
+ edgedetect->directions[j*w + i] = get_closer_direction(gx, gy);
+ }
+ }
+ }
+}
+
+static void non_maximum_suppression(AVFilterContext *ctx, int w, int h,
+ uint8_t *dst, int dst_linesize,
+ const uint8_t *src, int src_linesize)
+{
+ int i, j;
+ EdgeDetectContext *edgedetect = ctx->priv;
+
+#define RESET_NON_MAXIMA(ay, ax, by, bx) do { \
+ if (src[j*src_linesize + i] > src[(j+(ay))*src_linesize + i+(ax)] && \
+ src[j*src_linesize + i] > src[(j+(by))*src_linesize + i+(bx)]) \
+ dst[j*dst_linesize + i] = src[j*src_linesize + i]; \
+ else \
+ dst[j*dst_linesize + i] = 0; \
+} while (0)
+
+ for (j = 0; j < h; j++) {
+ for (i = 0; i < w; i++) {
+
+ if (!i || i == w - 1 || !j || j == h - 1) {
+ // FIXME: sth better?
+ dst[j*dst_linesize + i] = 0;
+ continue;
+ }
+
+#if 0
+ /* skip double threshold step when using this */
+ switch (edgedetect->directions[j*w + i]) {
+ case DIRECTION_45UP: dst[j*dst_linesize + i] = 0x00; break;
+ case DIRECTION_45DOWN: dst[j*dst_linesize + i] = 0x55; break;
+ case DIRECTION_HORIZONTAL: dst[j*dst_linesize + i] = 0xaa; break;
+ case DIRECTION_VERTICAL: dst[j*dst_linesize + i] = 0xff; break;
+ }
+#else
+ switch (edgedetect->directions[j*w + i]) {
+ case DIRECTION_45UP: RESET_NON_MAXIMA( 1, -1, -1, 1); break;
+ case DIRECTION_45DOWN: RESET_NON_MAXIMA(-1, -1, 1, 1); break;
+ case DIRECTION_HORIZONTAL: RESET_NON_MAXIMA( 0, -1, 0, 1); break;
+ case DIRECTION_VERTICAL: RESET_NON_MAXIMA(-1, 0, 1, 0); break;
+ }
+#endif
+ }
+ }
+}
+
+static void double_threshold(AVFilterContext *ctx, int w, int h,
+ uint8_t *dst, int dst_linesize,
+ const uint8_t *src, int src_linesize)
+{
+ int i, j;
+
+#define THRES_HIGH 80
+#define THRES_LOW 20
+
+ for (j = 0; j < h; j++) {
+ for (i = 0; i < w; i++) {
+ if (src[j*src_linesize + i] > THRES_HIGH) {
+ dst[j*dst_linesize + i] = src[j*src_linesize + i];
+ continue;
+ }
+
+ if ((!i || i == w - 1 || !j || j == h - 1) &&
+ src[j*src_linesize + i] > THRES_LOW &&
+ (src[(j-1)*src_linesize + i-1] > THRES_HIGH ||
+ src[(j-1)*src_linesize + i ] > THRES_HIGH ||
+ src[(j-1)*src_linesize + i+1] > THRES_HIGH ||
+ src[ j *src_linesize + i-1] > THRES_HIGH ||
+ src[ j *src_linesize + i+1] > THRES_HIGH ||
+ src[(j+1)*src_linesize + i-1] > THRES_HIGH ||
+ src[(j+1)*src_linesize + i ] > THRES_HIGH ||
+ src[(j+1)*src_linesize + i+1] > THRES_HIGH))
+ dst[j*dst_linesize + i] = src[j*src_linesize + i];
+ else
+ dst[j*dst_linesize + i] = 0;
+ }
+ }
+}
+
+static int end_frame(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ EdgeDetectContext *edgedetect = ctx->priv;
+ AVFilterLink *outlink = inlink->dst->outputs[0];
+ AVFilterBufferRef *inpicref = inlink->cur_buf;
+ AVFilterBufferRef *outpicref = outlink->out_buf;
+ uint8_t *tmpbuf1 = edgedetect->tmpbuf1;
+ uint8_t *tmpbuf2 = edgedetect->tmpbuf2;
+
+ /* canny edge detection */
+
+ gaussian_blur(ctx, inlink->w, inlink->h,
+ tmpbuf1, inlink->w,
+ inpicref->data[0], inpicref->linesize[0]);
+
+ sobel(ctx, inlink->w, inlink->h,
+ tmpbuf2, inlink->w,
+ tmpbuf1, inlink->w);
+
+ non_maximum_suppression(ctx, inlink->w, inlink->h,
+ tmpbuf1, inlink->w,
+ tmpbuf2, inlink->w);
+
+ double_threshold(ctx, inlink->w, inlink->h,
+ outpicref->data[0], outpicref->linesize[0],
+ tmpbuf1, inlink->w);
+
+ ff_draw_slice(outlink, 0, outlink->h, 1);
+ return ff_end_frame(outlink);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ EdgeDetectContext *edgedetect = ctx->priv;
+ av_freep(&edgedetect->tmpbuf1);
+ av_freep(&edgedetect->tmpbuf2);
+ av_freep(&edgedetect->directions);
+}
+
+static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
+
+AVFilter avfilter_vf_edgedetect = {
+ .name = "edgedetect",
+ .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
+ .priv_size = sizeof(EdgeDetectContext),
+ .uninit = uninit,
+ .query_formats = query_formats,
+
+ .inputs = (const AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .draw_slice = null_draw_slice,
+ .config_props = config_props,
+ .end_frame = end_frame,
+ .min_perms = AV_PERM_READ
+ },
+ { .name = NULL }
+ },
+ .outputs = (const AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ },
+ { .name = NULL }
+ },
+};
--
1.7.11.4
More information about the ffmpeg-devel
mailing list