1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-28 20:53:54 +02:00
FFmpeg/libavfilter/vf_edgedetect.c

332 lines
11 KiB
C
Raw Normal View History

2012-07-26 20:45:53 +03:00
/*
* 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
*
* @see https://en.wikipedia.org/wiki/Canny_edge_detector
2012-07-26 20:45:53 +03:00
*/
#include "libavutil/opt.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
typedef struct {
const AVClass *class;
uint8_t *tmpbuf;
uint16_t *gradients;
char *directions;
double low, high;
uint8_t low_u8, high_u8;
} EdgeDetectContext;
#define OFFSET(x) offsetof(EdgeDetectContext, x)
#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[] = {
{ "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 },
{ NULL }
2012-07-26 20:45:53 +03:00
};
AVFILTER_DEFINE_CLASS(edgedetect);
static av_cold int init(AVFilterContext *ctx)
2012-07-26 20:45:53 +03:00
{
EdgeDetectContext *edgedetect = ctx->priv;
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)
{
Merge commit '716d413c13981da15323c7a3821860536eefdbbb' * commit '716d413c13981da15323c7a3821860536eefdbbb': Replace PIX_FMT_* -> AV_PIX_FMT_*, PixelFormat -> AVPixelFormat Conflicts: doc/examples/muxing.c ffmpeg.h ffmpeg_filter.c ffmpeg_opt.c ffplay.c ffprobe.c libavcodec/8bps.c libavcodec/aasc.c libavcodec/aura.c libavcodec/avcodec.h libavcodec/avs.c libavcodec/bfi.c libavcodec/bmp.c libavcodec/bmpenc.c libavcodec/c93.c libavcodec/cscd.c libavcodec/cyuv.c libavcodec/dpx.c libavcodec/dpxenc.c libavcodec/eatgv.c libavcodec/escape124.c libavcodec/ffv1.c libavcodec/flashsv.c libavcodec/fraps.c libavcodec/h264.c libavcodec/huffyuv.c libavcodec/iff.c libavcodec/imgconvert.c libavcodec/indeo3.c libavcodec/kmvc.c libavcodec/libopenjpegdec.c libavcodec/libopenjpegenc.c libavcodec/libx264.c libavcodec/ljpegenc.c libavcodec/mjpegdec.c libavcodec/mjpegenc.c libavcodec/motionpixels.c libavcodec/mpeg12.c libavcodec/mpeg12enc.c libavcodec/mpeg4videodec.c libavcodec/mpegvideo_enc.c libavcodec/pamenc.c libavcodec/pcxenc.c libavcodec/pgssubdec.c libavcodec/pngdec.c libavcodec/pngenc.c libavcodec/pnm.c libavcodec/pnmdec.c libavcodec/pnmenc.c libavcodec/ptx.c libavcodec/qdrw.c libavcodec/qpeg.c libavcodec/qtrleenc.c libavcodec/raw.c libavcodec/rawdec.c libavcodec/rl2.c libavcodec/sgidec.c libavcodec/sgienc.c libavcodec/snowdec.c libavcodec/snowenc.c libavcodec/sunrast.c libavcodec/targa.c libavcodec/targaenc.c libavcodec/tiff.c libavcodec/tiffenc.c libavcodec/tmv.c libavcodec/truemotion2.c libavcodec/utils.c libavcodec/vb.c libavcodec/vp3.c libavcodec/wnv1.c libavcodec/xl.c libavcodec/xwddec.c libavcodec/xwdenc.c libavcodec/yop.c libavdevice/v4l2.c libavdevice/x11grab.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersrc.c libavfilter/drawutils.c libavfilter/formats.c libavfilter/src_movie.c libavfilter/vf_ass.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_format.c libavfilter/vf_hflip.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_transpose.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavformat/movenc.c libavformat/mxf.h libavformat/utils.c libavformat/yuv4mpeg.c libavutil/imgutils.c libavutil/pixdesc.c libswscale/input.c libswscale/output.c libswscale/swscale_internal.h libswscale/swscale_unscaled.c libswscale/utils.c libswscale/x86/swscale_template.c libswscale/x86/yuv2rgb.c libswscale/x86/yuv2rgb_template.c libswscale/yuv2rgb.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
2012-10-08 21:54:00 +03:00
static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
2012-07-26 20:45:53 +03:00
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->tmpbuf = av_malloc(inlink->w * inlink->h);
edgedetect->gradients = av_calloc(inlink->w * inlink->h, sizeof(*edgedetect->gradients));
edgedetect->directions = av_malloc(inlink->w * inlink->h);
if (!edgedetect->tmpbuf || !edgedetect->gradients || !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;
memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
for (j = 2; j < h - 2; j++) {
dst[0] = src[0];
dst[1] = src[1];
for (i = 2; i < w - 2; i++) {
/* Gaussian mask of size 5x5 with sigma = 1.4 */
dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2
+ (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4
+ (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5
+ (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4
+ (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2
+ (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4
+ (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9
+ (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12
+ (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9
+ (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4
+ src[i-2] * 5
+ src[i-1] * 12
+ src[i ] * 15
+ src[i+1] * 12
+ src[i+2] * 5) / 159;
}
dst[i ] = src[i ];
dst[i + 1] = src[i + 1];
dst += dst_linesize;
src += src_linesize;
}
memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
memcpy(dst, src, w);
}
enum {
DIRECTION_45UP,
DIRECTION_45DOWN,
DIRECTION_HORIZONTAL,
DIRECTION_VERTICAL,
};
static int get_rounded_direction(int gx, int gy)
{
/* reference angles:
* tan( pi/8) = sqrt(2)-1
* tan(3pi/8) = sqrt(2)+1
* Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
* <ref-angle>, or more simply Gy against <ref-angle>*Gx
*
* Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
2012-07-26 20:45:53 +03:00
* round((sqrt(2)-1) * (1<<16)) = 27146
* round((sqrt(2)+1) * (1<<16)) = 158218
*/
if (gx) {
int tanpi8gx, tan3pi8gx;
if (gx < 0)
gx = -gx, gy = -gy;
gy <<= 16;
tanpi8gx = 27146 * gx;
tan3pi8gx = 158218 * gx;
if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
}
return DIRECTION_VERTICAL;
}
static void sobel(AVFilterContext *ctx, int w, int h,
uint16_t *dst, int dst_linesize,
const uint8_t *src, int src_linesize)
{
int i, j;
EdgeDetectContext *edgedetect = ctx->priv;
for (j = 1; j < h - 1; j++) {
dst += dst_linesize;
src += src_linesize;
for (i = 1; i < w - 1; i++) {
const int gx =
-1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1]
-2*src[ i-1] + 2*src[ i+1]
-1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1];
const int gy =
-1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1]
-2*src[-src_linesize + i ] + 2*src[ src_linesize + i ]
-1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1];
dst[i] = FFABS(gx) + FFABS(gy);
edgedetect->directions[j*w + i] = get_rounded_direction(gx, gy);
}
}
}
static void non_maximum_suppression(AVFilterContext *ctx, int w, int h,
uint8_t *dst, int dst_linesize,
const uint16_t *src, int src_linesize)
{
int i, j;
EdgeDetectContext *edgedetect = ctx->priv;
#define COPY_MAXIMA(ay, ax, by, bx) do { \
if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
src[i] > src[(by)*src_linesize + i+(bx)]) \
dst[i] = av_clip_uint8(src[i]); \
} while (0)
for (j = 1; j < h - 1; j++) {
dst += dst_linesize;
src += src_linesize;
for (i = 1; i < w - 1; i++) {
switch (edgedetect->directions[j*w + i]) {
case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
}
}
}
}
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;
EdgeDetectContext *edgedetect = ctx->priv;
const int low = edgedetect->low_u8;
const int high = edgedetect->high_u8;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (src[i] > high) {
dst[i] = src[i];
continue;
}
if ((!i || i == w - 1 || !j || j == h - 1) &&
src[i] > low &&
(src[-src_linesize + i-1] > high ||
src[-src_linesize + i ] > high ||
src[-src_linesize + i+1] > high ||
src[ i-1] > high ||
src[ i+1] > high ||
src[ src_linesize + i-1] > high ||
src[ src_linesize + i ] > high ||
src[ src_linesize + i+1] > high))
dst[i] = src[i];
else
dst[i] = 0;
}
dst += dst_linesize;
src += src_linesize;
}
}
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
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;
AVFilterLink *outlink = inlink->dst->outputs[0];
uint8_t *tmpbuf = edgedetect->tmpbuf;
uint16_t *gradients = edgedetect->gradients;
2013-04-03 01:21:11 +03:00
int direct = 0;
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
AVFrame *out;
2013-04-03 01:21:11 +03:00
if (av_frame_is_writable(in)) {
direct = 1;
out = in;
} else {
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
/* gaussian filter to reduce noise */
gaussian_blur(ctx, inlink->w, inlink->h,
tmpbuf, inlink->w,
in->data[0], in->linesize[0]);
2012-07-26 20:45:53 +03:00
/* compute the 16-bits gradients and directions for the next step */
sobel(ctx, inlink->w, inlink->h,
gradients, inlink->w,
tmpbuf, inlink->w);
/* non_maximum_suppression() will actually keep & clip what's necessary and
* ignore the rest, so we need a clean output buffer */
memset(tmpbuf, 0, inlink->w * inlink->h);
non_maximum_suppression(ctx, inlink->w, inlink->h,
tmpbuf, inlink->w,
gradients, inlink->w);
/* keep high values, or low values surrounded by high values */
double_threshold(ctx, inlink->w, inlink->h,
out->data[0], out->linesize[0],
tmpbuf, inlink->w);
2012-07-26 20:45:53 +03:00
2013-04-03 01:21:11 +03:00
if (!direct)
av_frame_free(&in);
return ff_filter_frame(outlink, out);
2012-07-26 20:45:53 +03:00
}
static av_cold void uninit(AVFilterContext *ctx)
{
EdgeDetectContext *edgedetect = ctx->priv;
av_freep(&edgedetect->tmpbuf);
av_freep(&edgedetect->gradients);
av_freep(&edgedetect->directions);
}
static const AVFilterPad edgedetect_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_props,
.filter_frame = filter_frame,
},
{ NULL }
};
static const AVFilterPad edgedetect_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
{ NULL }
};
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,
.query_formats = query_formats,
.inputs = edgedetect_inputs,
.outputs = edgedetect_outputs,
.priv_class = &edgedetect_class,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
2012-07-26 20:45:53 +03:00
};