2012-07-26 20:45:53 +03:00
|
|
|
/*
|
2014-05-03 20:10:28 +03:00
|
|
|
* Copyright (c) 2012-2014 Clément Bœsch <u pkh me>
|
2012-07-26 20:45:53 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2012-08-21 00:12:34 +03:00
|
|
|
* @see https://en.wikipedia.org/wiki/Canny_edge_detector
|
2012-07-26 20:45:53 +03:00
|
|
|
*/
|
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2018-05-03 15:20:21 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2012-07-26 20:45:53 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
2021-11-30 01:16:52 +02:00
|
|
|
#include "edge_common.h"
|
2012-07-26 20:45:53 +03:00
|
|
|
|
2018-05-03 15:20:21 +02:00
|
|
|
#define PLANE_R 0x4
|
|
|
|
#define PLANE_G 0x1
|
|
|
|
#define PLANE_B 0x2
|
|
|
|
#define PLANE_Y 0x1
|
|
|
|
#define PLANE_U 0x2
|
|
|
|
#define PLANE_V 0x4
|
|
|
|
#define PLANE_A 0x8
|
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
enum FilterMode {
|
|
|
|
MODE_WIRES,
|
|
|
|
MODE_COLORMIX,
|
2018-05-03 15:05:51 +02:00
|
|
|
MODE_CANNY,
|
2014-05-03 18:41:32 +03:00
|
|
|
NB_MODE
|
|
|
|
};
|
|
|
|
|
|
|
|
struct plane_info {
|
2012-07-26 20:45:53 +03:00
|
|
|
uint8_t *tmpbuf;
|
|
|
|
uint16_t *gradients;
|
|
|
|
char *directions;
|
2018-05-03 15:39:06 +02:00
|
|
|
int width, height;
|
2014-05-03 18:41:32 +03:00
|
|
|
};
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct EdgeDetectContext {
|
2014-05-03 18:41:32 +03:00
|
|
|
const AVClass *class;
|
|
|
|
struct plane_info planes[3];
|
2018-05-03 15:20:21 +02:00
|
|
|
int filter_planes;
|
2014-05-03 18:41:32 +03:00
|
|
|
int nb_planes;
|
2012-07-26 20:45:53 +03:00
|
|
|
double low, high;
|
|
|
|
uint8_t low_u8, high_u8;
|
2015-03-08 21:59:51 +02:00
|
|
|
int mode;
|
2012-07-26 20:45:53 +03:00
|
|
|
} EdgeDetectContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(EdgeDetectContext, x)
|
2012-08-13 14:40:01 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
2012-07-26 20:45:53 +03:00
|
|
|
static const AVOption edgedetect_options[] = {
|
2012-08-13 14:40:01 +03:00
|
|
|
{ "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1, FLAGS },
|
|
|
|
{ "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1, FLAGS },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_WIRES}, 0, NB_MODE-1, FLAGS, .unit = "mode" },
|
|
|
|
{ "wires", "white/gray wires on black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WIRES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
|
|
|
|
{ "colormix", "mix colors", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLORMIX}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
|
|
|
|
{ "canny", "detect edges on planes", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CANNY}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
|
|
|
|
{ "planes", "set planes to filter", OFFSET(filter_planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 1, 0x7, FLAGS, .unit = "flags" },
|
|
|
|
{ "y", "filter luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, .unit = "flags" },
|
|
|
|
{ "u", "filter u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, .unit = "flags" },
|
|
|
|
{ "v", "filter v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, .unit = "flags" },
|
|
|
|
{ "r", "filter red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, .unit = "flags" },
|
|
|
|
{ "g", "filter green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, .unit = "flags" },
|
|
|
|
{ "b", "filter blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, .unit = "flags" },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-07-26 20:45:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(edgedetect);
|
|
|
|
|
2013-04-12 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2012-07-26 20:45:53 +03:00
|
|
|
{
|
|
|
|
EdgeDetectContext *edgedetect = ctx->priv;
|
|
|
|
|
2012-08-17 09:07:37 +03:00
|
|
|
edgedetect->low_u8 = edgedetect->low * 255. + .5;
|
|
|
|
edgedetect->high_u8 = edgedetect->high * 255. + .5;
|
2012-07-26 20:45:53 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2014-05-03 18:41:32 +03:00
|
|
|
const EdgeDetectContext *edgedetect = ctx->priv;
|
2015-03-15 15:20:34 +02:00
|
|
|
static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
|
2018-05-03 15:39:06 +02:00
|
|
|
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};
|
2015-03-15 15:20:34 +02:00
|
|
|
static const enum AVPixelFormat colormix_pix_fmts[] = {AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
|
|
|
|
const enum AVPixelFormat *pix_fmts = NULL;
|
2014-05-03 18:41:32 +03:00
|
|
|
|
|
|
|
if (edgedetect->mode == MODE_WIRES) {
|
2015-03-15 15:20:34 +02:00
|
|
|
pix_fmts = wires_pix_fmts;
|
2014-05-03 18:41:32 +03:00
|
|
|
} else if (edgedetect->mode == MODE_COLORMIX) {
|
2015-03-15 15:20:34 +02:00
|
|
|
pix_fmts = colormix_pix_fmts;
|
2018-05-03 15:05:51 +02:00
|
|
|
} else if (edgedetect->mode == MODE_CANNY) {
|
|
|
|
pix_fmts = canny_pix_fmts;
|
2014-05-03 18:41:32 +03:00
|
|
|
} else {
|
|
|
|
av_assert0(0);
|
|
|
|
}
|
2021-08-10 01:25:31 +02:00
|
|
|
return ff_set_common_formats_from_list(ctx, pix_fmts);
|
2012-07-26 20:45:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
2014-05-03 18:41:32 +03:00
|
|
|
int p;
|
2012-07-26 20:45:53 +03:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
EdgeDetectContext *edgedetect = ctx->priv;
|
2018-05-03 15:39:06 +02:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2012-07-26 20:45:53 +03:00
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
edgedetect->nb_planes = inlink->format == AV_PIX_FMT_GRAY8 ? 1 : 3;
|
|
|
|
for (p = 0; p < edgedetect->nb_planes; p++) {
|
|
|
|
struct plane_info *plane = &edgedetect->planes[p];
|
2018-05-03 15:39:06 +02:00
|
|
|
int vsub = p ? desc->log2_chroma_h : 0;
|
|
|
|
int hsub = p ? desc->log2_chroma_w : 0;
|
|
|
|
|
|
|
|
plane->width = AV_CEIL_RSHIFT(inlink->w, hsub);
|
|
|
|
plane->height = AV_CEIL_RSHIFT(inlink->h, vsub);
|
|
|
|
plane->tmpbuf = av_malloc(plane->width * plane->height);
|
|
|
|
plane->gradients = av_calloc(plane->width * plane->height, sizeof(*plane->gradients));
|
|
|
|
plane->directions = av_malloc(plane->width * plane->height);
|
2014-05-03 18:41:32 +03:00
|
|
|
if (!plane->tmpbuf || !plane->gradients || !plane->directions)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2012-07-26 20:45:53 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
static void color_mix(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++)
|
|
|
|
dst[i] = (dst[i] + src[i]) >> 1;
|
|
|
|
dst += dst_linesize;
|
|
|
|
src += src_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
2012-07-26 20:45:53 +03:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
EdgeDetectContext *edgedetect = ctx->priv;
|
2014-05-17 12:47:28 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2014-05-03 18:41:32 +03:00
|
|
|
int p, direct = 0;
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *out;
|
2012-11-28 21:56:18 +03:00
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
|
2013-04-03 01:21:11 +03:00
|
|
|
direct = 1;
|
|
|
|
out = in;
|
|
|
|
} else {
|
2013-04-03 01:31:19 +03:00
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(out, in);
|
2013-04-03 01:21:11 +03:00
|
|
|
}
|
2012-07-26 20:45:53 +03:00
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
for (p = 0; p < edgedetect->nb_planes; p++) {
|
|
|
|
struct plane_info *plane = &edgedetect->planes[p];
|
|
|
|
uint8_t *tmpbuf = plane->tmpbuf;
|
|
|
|
uint16_t *gradients = plane->gradients;
|
|
|
|
int8_t *directions = plane->directions;
|
2018-05-03 15:39:06 +02:00
|
|
|
const int width = plane->width;
|
|
|
|
const int height = plane->height;
|
2014-05-03 18:41:32 +03:00
|
|
|
|
2018-05-03 15:20:21 +02:00
|
|
|
if (!((1 << p) & edgedetect->filter_planes)) {
|
|
|
|
if (!direct)
|
|
|
|
av_image_copy_plane(out->data[p], out->linesize[p],
|
|
|
|
in->data[p], in->linesize[p],
|
2018-05-03 15:39:06 +02:00
|
|
|
width, height);
|
2018-05-03 15:20:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-03 19:46:16 +03:00
|
|
|
/* gaussian filter to reduce noise */
|
2022-07-18 16:09:46 +02:00
|
|
|
ff_gaussian_blur_8(width, height,
|
|
|
|
tmpbuf, width,
|
|
|
|
in->data[p], in->linesize[p], 1);
|
2014-05-03 19:46:16 +03:00
|
|
|
|
|
|
|
/* compute the 16-bits gradients and directions for the next step */
|
2022-07-18 16:09:46 +02:00
|
|
|
ff_sobel_8(width, height,
|
|
|
|
gradients, width,
|
|
|
|
directions,width,
|
|
|
|
tmpbuf, width, 1);
|
2014-05-03 19:46:16 +03:00
|
|
|
|
|
|
|
/* non_maximum_suppression() will actually keep & clip what's necessary and
|
|
|
|
* ignore the rest, so we need a clean output buffer */
|
2018-05-03 15:39:06 +02:00
|
|
|
memset(tmpbuf, 0, width * height);
|
2021-11-30 01:16:52 +02:00
|
|
|
ff_non_maximum_suppression(width, height,
|
2018-05-03 15:39:06 +02:00
|
|
|
tmpbuf, width,
|
|
|
|
directions,width,
|
|
|
|
gradients, width);
|
2014-05-03 19:46:16 +03:00
|
|
|
|
|
|
|
/* keep high values, or low values surrounded by high values */
|
2021-11-30 01:16:52 +02:00
|
|
|
ff_double_threshold(edgedetect->low_u8, edgedetect->high_u8,
|
2018-05-03 15:39:06 +02:00
|
|
|
width, height,
|
2014-05-03 19:46:16 +03:00
|
|
|
out->data[p], out->linesize[p],
|
2018-05-03 15:39:06 +02:00
|
|
|
tmpbuf, width);
|
2012-07-26 20:45:53 +03:00
|
|
|
|
2014-05-03 18:41:32 +03:00
|
|
|
if (edgedetect->mode == MODE_COLORMIX) {
|
2018-05-03 15:39:06 +02:00
|
|
|
color_mix(width, height,
|
2014-05-03 18:41:32 +03:00
|
|
|
out->data[p], out->linesize[p],
|
|
|
|
in->data[p], in->linesize[p]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 01:21:11 +03:00
|
|
|
if (!direct)
|
|
|
|
av_frame_free(&in);
|
2012-11-28 21:56:18 +03:00
|
|
|
return ff_filter_frame(outlink, out);
|
2012-07-26 20:45:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2014-05-03 18:41:32 +03:00
|
|
|
int p;
|
2012-07-26 20:45:53 +03:00
|
|
|
EdgeDetectContext *edgedetect = ctx->priv;
|
2014-05-03 18:41:32 +03:00
|
|
|
|
|
|
|
for (p = 0; p < edgedetect->nb_planes; p++) {
|
|
|
|
struct plane_info *plane = &edgedetect->planes[p];
|
|
|
|
av_freep(&plane->tmpbuf);
|
|
|
|
av_freep(&plane->gradients);
|
|
|
|
av_freep(&plane->directions);
|
|
|
|
}
|
2012-07-26 20:45:53 +03:00
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad edgedetect_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.filter_frame = filter_frame,
|
2015-01-15 12:39:41 +02:00
|
|
|
},
|
2012-11-28 22:01:59 +03:00
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_edgedetect = {
|
2012-07-26 20:45:53 +03:00
|
|
|
.name = "edgedetect",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
|
|
|
|
.priv_size = sizeof(EdgeDetectContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(edgedetect_inputs),
|
2023-08-03 14:37:51 +02:00
|
|
|
FILTER_OUTPUTS(ff_video_default_filterpad),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2012-11-29 01:36:50 +03:00
|
|
|
.priv_class = &edgedetect_class,
|
2013-05-09 02:04:41 +03:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2012-07-26 20:45:53 +03:00
|
|
|
};
|