FFmpeg
Loading...
Searching...
No Matches
vf_edgedetect.c
Go to the documentation of this file.
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/mem.h"
31#include "libavutil/opt.h"
32#include "avfilter.h"
33#include "filters.h"
34#include "formats.h"
35#include "video.h"
36#include "edge_common.h"
37
38#define PLANE_R 0x4
39#define PLANE_G 0x1
40#define PLANE_B 0x2
41#define PLANE_Y 0x1
42#define PLANE_U 0x2
43#define PLANE_V 0x4
44#define PLANE_A 0x8
45
52
53struct plane_info {
54 uint8_t *tmpbuf;
55 uint16_t *gradients;
58};
59
60typedef struct EdgeDetectContext {
61 const AVClass *class;
62 struct plane_info planes[3];
65 double low, high;
66 uint8_t low_u8, high_u8;
67 int mode;
69
70#define OFFSET(x) offsetof(EdgeDetectContext, x)
71#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
72static const AVOption edgedetect_options[] = {
73 { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1, FLAGS },
74 { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1, FLAGS },
75 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_WIRES}, 0, NB_MODE-1, FLAGS, .unit = "mode" },
76 { "wires", "white/gray wires on black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WIRES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
77 { "colormix", "mix colors", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLORMIX}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
78 { "canny", "detect edges on planes", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CANNY}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
79 { "planes", "set planes to filter", OFFSET(filter_planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 1, 0x7, FLAGS, .unit = "flags" },
80 { "y", "filter luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, .unit = "flags" },
81 { "u", "filter u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, .unit = "flags" },
82 { "v", "filter v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, .unit = "flags" },
83 { "r", "filter red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, .unit = "flags" },
84 { "g", "filter green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, .unit = "flags" },
85 { "b", "filter blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, .unit = "flags" },
86 { NULL }
87};
88
90
92{
93 EdgeDetectContext *edgedetect = ctx->priv;
94
95 edgedetect->low_u8 = edgedetect->low * 255. + .5;
96 edgedetect->high_u8 = edgedetect->high * 255. + .5;
97 return 0;
98}
99
101 AVFilterFormatsConfig **cfg_in,
102 AVFilterFormatsConfig **cfg_out)
103{
104 const EdgeDetectContext *edgedetect = ctx->priv;
105 static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
107 static const enum AVPixelFormat colormix_pix_fmts[] = {AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
108 const enum AVPixelFormat *pix_fmts = NULL;
109
110 if (edgedetect->mode == MODE_WIRES) {
111 pix_fmts = wires_pix_fmts;
112 } else if (edgedetect->mode == MODE_COLORMIX) {
113 pix_fmts = colormix_pix_fmts;
114 } else if (edgedetect->mode == MODE_CANNY) {
115 pix_fmts = canny_pix_fmts;
116 } else {
117 av_assert0(0);
118 }
119 return ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
120}
121
122static int config_props(AVFilterLink *inlink)
123{
124 int p;
125 AVFilterContext *ctx = inlink->dst;
126 EdgeDetectContext *edgedetect = ctx->priv;
128
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;
134
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);
142 }
143 return 0;
144}
145
146static void color_mix(int w, int h,
147 uint8_t *dst, int dst_linesize,
148 const uint8_t *src, int src_linesize)
149{
150 int i, j;
151
152 for (j = 0; j < h; j++) {
153 for (i = 0; i < w; i++)
154 dst[i] = (dst[i] + src[i]) >> 1;
155 dst += dst_linesize;
156 src += src_linesize;
157 }
158}
159
160static int filter_frame(AVFilterLink *inlink, AVFrame *in)
161{
162 AVFilterContext *ctx = inlink->dst;
163 EdgeDetectContext *edgedetect = ctx->priv;
164 AVFilterLink *outlink = ctx->outputs[0];
165 int p, direct = 0;
166 AVFrame *out;
167
168 if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
169 direct = 1;
170 out = in;
171 } else {
172 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
173 if (!out) {
174 av_frame_free(&in);
175 return AVERROR(ENOMEM);
176 }
178 }
179
180 for (p = 0; p < edgedetect->nb_planes; p++) {
181 struct plane_info *plane = &edgedetect->planes[p];
182 uint8_t *tmpbuf = plane->tmpbuf;
183 uint16_t *gradients = plane->gradients;
184 int8_t *directions = plane->directions;
185 const int width = plane->width;
186 const int height = plane->height;
187
188 if (!((1 << p) & edgedetect->filter_planes)) {
189 if (!direct)
190 av_image_copy_plane(out->data[p], out->linesize[p],
191 in->data[p], in->linesize[p],
192 width, height);
193 continue;
194 }
195
196 /* gaussian filter to reduce noise */
197 ff_gaussian_blur_8(width, height,
198 tmpbuf, width,
199 in->data[p], in->linesize[p], 1);
200
201 /* compute the 16-bits gradients and directions for the next step */
202 ff_sobel_8(width, height,
205 tmpbuf, width, 1);
206
207 /* non_maximum_suppression() will actually keep & clip what's necessary and
208 * ignore the rest, so we need a clean output buffer */
209 memset(tmpbuf, 0, width * height);
211 tmpbuf, width,
214
215 /* keep high values, or low values surrounded by high values */
216 ff_double_threshold(edgedetect->low_u8, edgedetect->high_u8,
217 width, height,
218 out->data[p], out->linesize[p],
219 tmpbuf, width);
220
221 if (edgedetect->mode == MODE_COLORMIX) {
223 out->data[p], out->linesize[p],
224 in->data[p], in->linesize[p]);
225 }
226 }
227
228 if (!direct)
229 av_frame_free(&in);
230 return ff_filter_frame(outlink, out);
231}
232
234{
235 int p;
236 EdgeDetectContext *edgedetect = ctx->priv;
237
238 for (p = 0; p < edgedetect->nb_planes; p++) {
239 struct plane_info *plane = &edgedetect->planes[p];
240 av_freep(&plane->tmpbuf);
241 av_freep(&plane->gradients);
242 av_freep(&plane->directions);
243 }
244}
245
247 {
248 .name = "default",
249 .type = AVMEDIA_TYPE_VIDEO,
250 .config_props = config_props,
251 .filter_frame = filter_frame,
252 },
253};
254
256 .p.name = "edgedetect",
257 .p.description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
258 .p.priv_class = &edgedetect_class,
260 .priv_size = sizeof(EdgeDetectContext),
261 .init = init,
262 .uninit = uninit,
266};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_vf_edgedetect
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int high
Definition dovi_rpuenc.c:39
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
void ff_non_maximum_suppression(int w, int h, uint8_t *dst, int dst_linesize, const int8_t *dir, int dir_linesize, const uint16_t *src, int src_linesize)
Filters rounded gradients to drop all non-maxima pixels in the magnitude image Expects gradients gene...
Definition edge_common.c:60
void ff_double_threshold(int low, int high, int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
Filters all pixels in src to keep all pixels > high, and keep all pixels > low where all surrounding ...
Definition edge_common.c:89
common functions for edge detection
int ff_set_pixel_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVPixelFormat *fmts)
Definition formats.c:1162
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
struct plane_info planes[3]
Definition swscale.c:71
uint16_t * gradients
uint8_t * tmpbuf
char * directions
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
#define PLANE_B
#define PLANE_R
#define PLANE_Y
static void color_mix(int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
@ MODE_COLORMIX
@ MODE_WIRES
@ MODE_CANNY
@ NB_MODE
static const AVOption edgedetect_options[]
#define PLANE_U
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define PLANE_G
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int config_props(AVFilterLink *inlink)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
#define PLANE_V
static const AVFilterPad edgedetect_inputs[]
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
FilterMode
Definition vp9.h:64