1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-12 19:18:44 +02:00
FFmpeg/libavfilter/vf_edgedetect.c

265 lines
9.3 KiB
C
Raw Normal View History

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
*
* @see https://en.wikipedia.org/wiki/Canny_edge_detector
2012-07-26 20:45:53 +03:00
*/
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h"
#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"
#include "edge_common.h"
2012-07-26 20:45:53 +03: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
enum FilterMode {
MODE_WIRES,
MODE_COLORMIX,
MODE_CANNY,
NB_MODE
};
struct plane_info {
2012-07-26 20:45:53 +03:00
uint8_t *tmpbuf;
uint16_t *gradients;
char *directions;
int width, height;
};
typedef struct EdgeDetectContext {
const AVClass *class;
struct plane_info planes[3];
int filter_planes;
int nb_planes;
2012-07-26 20:45:53 +03:00
double low, high;
uint8_t low_u8, high_u8;
int mode;
2012-07-26 20:45:53 +03:00
} 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 },
{ "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" },
{ 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)
{
const EdgeDetectContext *edgedetect = ctx->priv;
static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
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};
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;
if (edgedetect->mode == MODE_WIRES) {
pix_fmts = wires_pix_fmts;
} else if (edgedetect->mode == MODE_COLORMIX) {
pix_fmts = colormix_pix_fmts;
} else if (edgedetect->mode == MODE_CANNY) {
pix_fmts = canny_pix_fmts;
} else {
av_assert0(0);
}
return ff_set_common_formats_from_list(ctx, pix_fmts);
2012-07-26 20:45:53 +03:00
}
static int config_props(AVFilterLink *inlink)
{
int p;
2012-07-26 20:45:53 +03:00
AVFilterContext *ctx = inlink->dst;
EdgeDetectContext *edgedetect = ctx->priv;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2012-07-26 20:45:53 +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];
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);
if (!plane->tmpbuf || !plane->gradients || !plane->directions)
return AVERROR(ENOMEM);
}
2012-07-26 20:45:53 +03:00
return 0;
}
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;
}
}
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 = ctx->outputs[0];
int p, 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;
if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
2013-04-03 01:21:11 +03:00
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
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;
const int width = plane->width;
const int height = plane->height;
if (!((1 << p) & edgedetect->filter_planes)) {
if (!direct)
av_image_copy_plane(out->data[p], out->linesize[p],
in->data[p], in->linesize[p],
width, height);
continue;
}
/* gaussian filter to reduce noise */
ff_gaussian_blur_8(width, height,
tmpbuf, width,
in->data[p], in->linesize[p], 1);
/* compute the 16-bits gradients and directions for the next step */
ff_sobel_8(width, height,
gradients, width,
directions,width,
tmpbuf, width, 1);
/* non_maximum_suppression() will actually keep & clip what's necessary and
* ignore the rest, so we need a clean output buffer */
memset(tmpbuf, 0, width * height);
ff_non_maximum_suppression(width, height,
tmpbuf, width,
directions,width,
gradients, width);
/* keep high values, or low values surrounded by high values */
ff_double_threshold(edgedetect->low_u8, edgedetect->high_u8,
width, height,
out->data[p], out->linesize[p],
tmpbuf, width);
2012-07-26 20:45:53 +03:00
if (edgedetect->mode == MODE_COLORMIX) {
color_mix(width, height,
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);
return ff_filter_frame(outlink, out);
2012-07-26 20:45:53 +03:00
}
static av_cold void uninit(AVFilterContext *ctx)
{
int p;
2012-07-26 20:45:53 +03:00
EdgeDetectContext *edgedetect = ctx->priv;
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
}
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
},
};
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),
FILTER_OUTPUTS(ff_video_default_filterpad),
avfilter: Replace query_formats callback with union of list and callback If one looks at the many query_formats callbacks in existence, one will immediately recognize that there is one type of default callback for video and a slightly different default callback for audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);" for video with a filter-specific pix_fmts list. For audio, it is the same with a filter-specific sample_fmts list together with ff_set_common_all_samplerates() and ff_set_common_all_channel_counts(). This commit allows to remove the boilerplate query_formats callbacks by replacing said callback with a union consisting the old callback and pointers for pixel and sample format arrays. For the not uncommon case in which these lists only contain a single entry (besides the sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also added to the union to store them directly in the AVFilter, thereby avoiding a relocation. The state of said union will be contained in a new, dedicated AVFilter field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t in order to create a hole for this new field; this is no problem, as the maximum of all the nb_inputs is four; for nb_outputs it is only two). The state's default value coincides with the earlier default of query_formats being unset, namely that the filter accepts all formats (and also sample rates and channel counts/layouts for audio) provided that these properties agree coincide for all inputs and outputs. By using different union members for audio and video filters the type-unsafety of using the same functions for audio and video lists will furthermore be more confined to formats.c than before. When the new fields are used, they will also avoid allocations: Currently something nearly equivalent to ff_default_query_formats() is called after every successful call to a query_formats callback; yet in the common case that the newly allocated AVFilterFormats are not used at all (namely if there are no free links) these newly allocated AVFilterFormats are freed again without ever being used. Filters no longer using the callback will not exhibit this any more. Reviewed-by: Paul B Mahol <onemda@gmail.com> Reviewed-by: Nicolas George <george@nsup.org> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-27 12:07:35 +02:00
FILTER_QUERY_FUNC(query_formats),
.priv_class = &edgedetect_class,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
2012-07-26 20:45:53 +03:00
};