FFmpeg
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 "formats.h"
34 #include "internal.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 
46 enum FilterMode {
51 };
52 
53 struct plane_info {
54  uint8_t *tmpbuf;
55  uint16_t *gradients;
56  char *directions;
57  int width, height;
58 };
59 
60 typedef struct EdgeDetectContext {
61  const AVClass *class;
62  struct plane_info planes[3];
64  int nb_planes;
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
72 static 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 {
94 
95  edgedetect->low_u8 = edgedetect->low * 255. + .5;
96  edgedetect->high_u8 = edgedetect->high * 255. + .5;
97  return 0;
98 }
99 
101 {
102  const EdgeDetectContext *edgedetect = ctx->priv;
103  static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
105  static const enum AVPixelFormat colormix_pix_fmts[] = {AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
106  const enum AVPixelFormat *pix_fmts = NULL;
107 
108  if (edgedetect->mode == MODE_WIRES) {
109  pix_fmts = wires_pix_fmts;
110  } else if (edgedetect->mode == MODE_COLORMIX) {
111  pix_fmts = colormix_pix_fmts;
112  } else if (edgedetect->mode == MODE_CANNY) {
113  pix_fmts = canny_pix_fmts;
114  } else {
115  av_assert0(0);
116  }
118 }
119 
121 {
122  int p;
123  AVFilterContext *ctx = inlink->dst;
126 
127  edgedetect->nb_planes = inlink->format == AV_PIX_FMT_GRAY8 ? 1 : 3;
128  for (p = 0; p < edgedetect->nb_planes; p++) {
129  struct plane_info *plane = &edgedetect->planes[p];
130  int vsub = p ? desc->log2_chroma_h : 0;
131  int hsub = p ? desc->log2_chroma_w : 0;
132 
133  plane->width = AV_CEIL_RSHIFT(inlink->w, hsub);
134  plane->height = AV_CEIL_RSHIFT(inlink->h, vsub);
135  plane->tmpbuf = av_malloc(plane->width * plane->height);
136  plane->gradients = av_calloc(plane->width * plane->height, sizeof(*plane->gradients));
137  plane->directions = av_malloc(plane->width * plane->height);
138  if (!plane->tmpbuf || !plane->gradients || !plane->directions)
139  return AVERROR(ENOMEM);
140  }
141  return 0;
142 }
143 
144 static void color_mix(int w, int h,
145  uint8_t *dst, int dst_linesize,
146  const uint8_t *src, int src_linesize)
147 {
148  int i, j;
149 
150  for (j = 0; j < h; j++) {
151  for (i = 0; i < w; i++)
152  dst[i] = (dst[i] + src[i]) >> 1;
153  dst += dst_linesize;
154  src += src_linesize;
155  }
156 }
157 
159 {
160  AVFilterContext *ctx = inlink->dst;
162  AVFilterLink *outlink = ctx->outputs[0];
163  int p, direct = 0;
164  AVFrame *out;
165 
166  if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
167  direct = 1;
168  out = in;
169  } else {
170  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
171  if (!out) {
172  av_frame_free(&in);
173  return AVERROR(ENOMEM);
174  }
176  }
177 
178  for (p = 0; p < edgedetect->nb_planes; p++) {
179  struct plane_info *plane = &edgedetect->planes[p];
180  uint8_t *tmpbuf = plane->tmpbuf;
181  uint16_t *gradients = plane->gradients;
182  int8_t *directions = plane->directions;
183  const int width = plane->width;
184  const int height = plane->height;
185 
186  if (!((1 << p) & edgedetect->filter_planes)) {
187  if (!direct)
188  av_image_copy_plane(out->data[p], out->linesize[p],
189  in->data[p], in->linesize[p],
190  width, height);
191  continue;
192  }
193 
194  /* gaussian filter to reduce noise */
195  ff_gaussian_blur_8(width, height,
196  tmpbuf, width,
197  in->data[p], in->linesize[p], 1);
198 
199  /* compute the 16-bits gradients and directions for the next step */
200  ff_sobel_8(width, height,
201  gradients, width,
203  tmpbuf, width, 1);
204 
205  /* non_maximum_suppression() will actually keep & clip what's necessary and
206  * ignore the rest, so we need a clean output buffer */
207  memset(tmpbuf, 0, width * height);
209  tmpbuf, width,
211  gradients, width);
212 
213  /* keep high values, or low values surrounded by high values */
214  ff_double_threshold(edgedetect->low_u8, edgedetect->high_u8,
215  width, height,
216  out->data[p], out->linesize[p],
217  tmpbuf, width);
218 
219  if (edgedetect->mode == MODE_COLORMIX) {
221  out->data[p], out->linesize[p],
222  in->data[p], in->linesize[p]);
223  }
224  }
225 
226  if (!direct)
227  av_frame_free(&in);
228  return ff_filter_frame(outlink, out);
229 }
230 
232 {
233  int p;
235 
236  for (p = 0; p < edgedetect->nb_planes; p++) {
237  struct plane_info *plane = &edgedetect->planes[p];
238  av_freep(&plane->tmpbuf);
239  av_freep(&plane->gradients);
240  av_freep(&plane->directions);
241  }
242 }
243 
244 static const AVFilterPad edgedetect_inputs[] = {
245  {
246  .name = "default",
247  .type = AVMEDIA_TYPE_VIDEO,
248  .config_props = config_props,
249  .filter_frame = filter_frame,
250  },
251 };
252 
254  .name = "edgedetect",
255  .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
256  .priv_size = sizeof(EdgeDetectContext),
257  .init = init,
258  .uninit = uninit,
262  .priv_class = &edgedetect_class,
264 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:112
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
PLANE_B
#define PLANE_B
Definition: vf_edgedetect.c:40
edgedetect
This document is a tutorial initiation for writing simple filters in libavfilter libavfilter is which means that it is highly recommended that you submit your filters to the FFmpeg development mailing list and make sure that they are applied your filters are likely to have a very short lifetime due to more or less regular internal API and a limited and testing changes the pixels in whatever fashion you and outputs the modified frame The most simple way of doing this is to take a similar filter We ll pick edgedetect
Definition: writing_filters.txt:16
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
w
uint8_t w
Definition: llviddspenc.c:38
edge_common.h
AVOption
AVOption.
Definition: opt.h:357
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
high
int high
Definition: dovi_rpuenc.c:38
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
av_image_copy_plane
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
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:74
formats.h
plane_info::height
int height
Definition: vf_edgedetect.c:57
FilterMode
FilterMode
Definition: vp9.h:64
MODE_COLORMIX
@ MODE_COLORMIX
Definition: vf_edgedetect.c:48
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
PLANE_V
#define PLANE_V
Definition: vf_edgedetect.c:43
avassert.h
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
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
width
#define width
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:247
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:874
MODE_CANNY
@ MODE_CANNY
Definition: vf_edgedetect.c:49
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
MODE_WIRES
@ MODE_WIRES
Definition: vf_edgedetect.c:47
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
edgedetect_inputs
static const AVFilterPad edgedetect_inputs[]
Definition: vf_edgedetect.c:244
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_edgedetect.c:91
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:709
EdgeDetectContext::high
double high
Definition: vf_edgedetect.c:65
PLANE_G
#define PLANE_G
Definition: vf_edgedetect.c:39
ff_vf_edgedetect
const AVFilter ff_vf_edgedetect
Definition: vf_edgedetect.c:253
ff_non_maximum_suppression
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
plane_info::directions
char * directions
Definition: vf_edgedetect.c:56
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_edgedetect.c:158
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_edgedetect.c:231
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_double_threshold
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
EdgeDetectContext::mode
int mode
Definition: vf_edgedetect.c:67
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:645
height
#define height
plane_info::gradients
uint16_t * gradients
Definition: vf_edgedetect.c:55
plane_info::width
int width
Definition: vf_edgedetect.c:57
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:147
EdgeDetectContext::low
double low
Definition: vf_edgedetect.c:65
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
PLANE_R
#define PLANE_R
Definition: vf_edgedetect.c:38
EdgeDetectContext::planes
struct plane_info planes[3]
Definition: vf_edgedetect.c:62
EdgeDetectContext::filter_planes
int filter_planes
Definition: vf_edgedetect.c:63
plane_info
Definition: vf_edgedetect.c:53
EdgeDetectContext::low_u8
uint8_t low_u8
Definition: vf_edgedetect.c:66
plane_info::tmpbuf
uint8_t * tmpbuf
Definition: vf_edgedetect.c:54
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_edgedetect.c:120
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AVFilter
Filter definition.
Definition: avfilter.h:166
EdgeDetectContext::high_u8
uint8_t high_u8
Definition: vf_edgedetect.c:66
edgedetect_options
static const AVOption edgedetect_options[]
Definition: vf_edgedetect.c:72
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_edgedetect.c:100
PLANE_Y
#define PLANE_Y
Definition: vf_edgedetect.c:41
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:245
avfilter.h
EdgeDetectContext::nb_planes
int nb_planes
Definition: vf_edgedetect.c:64
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(edgedetect)
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
OFFSET
#define OFFSET(x)
Definition: vf_edgedetect.c:70
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FLAGS
#define FLAGS
Definition: vf_edgedetect.c:71
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
mem.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
PLANE_U
#define PLANE_U
Definition: vf_edgedetect.c:42
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:244
imgutils.h
AVFrame::linesize
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:419
NB_MODE
@ NB_MODE
Definition: vf_edgedetect.c:50
h
h
Definition: vp9dsp_template.c:2038
color_mix
static void color_mix(int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
Definition: vf_edgedetect.c:144
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:254
EdgeDetectContext
Definition: vf_edgedetect.c:60