[FFmpeg-devel] [PATCH] colorkeymask filter

Stefano Sabatini stefasab at gmail.com
Fri May 10 16:25:07 CEST 2013


On date Wednesday 2013-05-08 14:21:54 +0000, Paul B Mahol encoded:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  libavfilter/Makefile          |   1 +
>  libavfilter/allfilters.c      |   1 +
>  libavfilter/vf_colorkeymask.c | 161 ++++++++++++++++++++++++++++++++++++++++++

Missing docs.

>  3 files changed, 163 insertions(+)
>  create mode 100644 libavfilter/vf_colorkeymask.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index f0e703e..04f9646 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -110,6 +110,7 @@ OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o
>  OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
>  OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
>  OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER)      += vf_colorchannelmixer.o
> +OBJS-$(CONFIG_COLORKEYMASK_FILTER)           += vf_colorkeymask.o
>  OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
>  OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
>  OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index b8f273d..8d6e2fa 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -108,6 +108,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
>      REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
>      REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
> +    REGISTER_FILTER(COLORKEYMASK,   colorkeymask,   vf);
>      REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
>      REGISTER_FILTER(COPY,           copy,           vf);
>      REGISTER_FILTER(CROP,           crop,           vf);
> diff --git a/libavfilter/vf_colorkeymask.c b/libavfilter/vf_colorkeymask.c
> new file mode 100644
> index 0000000..7efec0f
> --- /dev/null
> +++ b/libavfilter/vf_colorkeymask.c
> @@ -0,0 +1,161 @@
> +/*
> + * Copyright (c) 2013 Paul B Mahol
> + *
> + * 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
> + */
> +
> +#include "libavutil/opt.h"
> +#include "libavutil/parseutils.h"
> +#include "avfilter.h"
> +#include "drawutils.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +#define R 0
> +#define G 1
> +#define B 2
> +#define A 3
> +
> +typedef struct {
> +    const AVClass *class;
> +    char *color_str;
> +    int rt, gt, bt;
> +    uint8_t lut[3][256];
> +    uint8_t rgba_map[4];
> +} ColorKeyMaskContext;
> +
> +#define OFFSET(x) offsetof(ColorKeyMaskContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +static const AVOption colorkeymask_options[] = {

> +    { "color", "set color",   OFFSET(color_str), AV_OPT_TYPE_STRING, {.str="black"}, 0, 0, FLAGS },

I wonder if we should add a color option.

> +    { "r", "set red tolerance",   OFFSET(rt), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
> +    { "g", "set green tolerance", OFFSET(gt), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
> +    { "b", "set blue tolerance",  OFFSET(bt), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },

0-1 range?

> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(colorkeymask);
> +
> +static av_cold int init(AVFilterContext *ctx)
> +{
> +    ColorKeyMaskContext *s = ctx->priv;
> +    uint8_t rmin, gmin, bmin;
> +    uint8_t rmax, gmax, bmax;
> +    uint8_t rgba[4];
> +    int i, ret;
> +
> +    ret = av_parse_color(rgba, s->color_str, -1, ctx);
> +    if (ret < 0)
> +        return ret;
> +

> +    rmin = av_clip_uint8(rgba[R] - s->rt);
> +    gmin = av_clip_uint8(rgba[G] - s->gt);
> +    bmin = av_clip_uint8(rgba[B] - s->bt);
> +    rmax = av_clip_uint8(rgba[R] + s->rt);
> +    gmax = av_clip_uint8(rgba[G] + s->gt);
> +    bmax = av_clip_uint8(rgba[B] + s->bt);

There is a better way to do this, by adopting the HSV colorspace so
that you don't need to cut the interval:
http://web.archiveorange.com/archive/v/yR2T4tPhfGQVuuTxzOh1

and enjoy the flames.

Sorry for the spam but the patch was not available in the archive:
http://ffmpeg.org/pipermail/ffmpeg-devel/2009-June/070428.html

[...]
-- 
FFmpeg = Fantastic and Fast Miracolous Peaceful Egregious Goblin


More information about the ffmpeg-devel mailing list