[FFmpeg-devel] [PATCH 2/2] colorchannelmixer filter

Stefano Sabatini stefasab at gmail.com
Wed Apr 17 22:52:22 CEST 2013


On date Wednesday 2013-04-17 00:33:52 +0000, Paul B Mahol encoded:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  doc/filters.texi                   |  45 ++++++++
>  libavfilter/Makefile               |   1 +
>  libavfilter/allfilters.c           |   1 +
>  libavfilter/vf_colorchannelmixer.c | 229 +++++++++++++++++++++++++++++++++++++
>  4 files changed, 276 insertions(+)
>  create mode 100644 libavfilter/vf_colorchannelmixer.c
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 685791b..6681e52 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2078,6 +2078,51 @@ Adjust red, green and blue highlights (brightest pixels).
>  Allowed range is from -100 to 100, default is 0.
>  @end table
>  
> + at section colorchannelmixer
> +
> +Adjust video input frames by re-mixing color channels.
> +

> +This filter modify a color channel by adding the values associated to

modifies

> +the other channels of the same pixels. For example if the value to
> +modify is red, the output value will be:
> + at example
> + at var{red}=@var{red}*@var{rr} + @var{blue}*@var{rb} + @var{green}*@var{rg} + @var{alpha}*@var{ra}
> + at end example
> +
> +The filter accepts the following options:
> +
> + at table @option
> + at item rr
> + at item rg
> + at item rb
> + at item ra

> +Adjust contribution of input red, green, blue and alpha channels for output red channel.
> +Default is @code{100} for @var{rr}, and @code{0} for @var{rg}, @var{rb} and @var{ra}.

Inconsistent with above example, also I'm for the use of values in the
range [-2, 2] which is mathematically cleaner (what's so special about
100?), and I'm not convinced that you have a real gain in terms of
syntax.

> +
> + at item gr
> + at item gg
> + at item gb
> + at item ga
> +Adjust contribution of input red, green, blue and alpha channels for output green channel.
> +Default is @code{100} for @var{gg}, and @code{0} for @var{gr}, @var{gb} and @var{ga}.
> +
> + at item br
> + at item bg
> + at item bb
> + at item ba
> +Adjust contribution of input red, green, blue and alpha channels for output blue channel.
> +Default is @code{100} for @var{bb}, and @code{0} for @var{br}, @var{bg} and @var{ba}.
> +
> + at item ar
> + at item ag
> + at item ab
> + at item aa
> +Adjust contribution of input red, green, blue and alpha channels for output alpha channel.
> +Default is @code{100} for @var{aa}, and @code{0} for @var{ar}, @var{ag} and @var{ab}.
> +
> +Allowed ranges are from @code{-200} to @code{200}.
> + at end table

One or two examples would be nice.

> +
>  @section colormatrix
>  
>  Convert color matrix.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 0927986..ef40836 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -105,6 +105,7 @@ OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
>  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_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 bebbf2b..a6c8597 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -103,6 +103,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(BLEND,          blend,          vf);
>      REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
>      REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
> +    REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
>      REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
>      REGISTER_FILTER(COPY,           copy,           vf);
>      REGISTER_FILTER(CROP,           crop,           vf);
> diff --git a/libavfilter/vf_colorchannelmixer.c b/libavfilter/vf_colorchannelmixer.c
> new file mode 100644
> index 0000000..164c65b
> --- /dev/null
> +++ b/libavfilter/vf_colorchannelmixer.c
> @@ -0,0 +1,229 @@
> +/*
> + * 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/pixdesc.h"
> +#include "avfilter.h"
> +#include "drawutils.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +typedef struct {
> +    const AVClass *class;
> +    double rr, rg, rb, ra;
> +    double gr, gg, gb, ga;
> +    double br, bg, bb, ba;
> +    double ar, ag, ab, aa;
> +
> +    int RR[256], RG[256], RB[256], RA[256];
> +    int GR[256], GG[256], GB[256], GA[256];
> +    int BR[256], BG[256], BB[256], BA[256];
> +    int AR[256], AG[256], AB[256], AA[256];
> +
> +    uint8_t rgba_map[4];
> +    int step;
> +} ColorChannelMixerContext;
> +
> +#define OFFSET(x) offsetof(ColorChannelMixerContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +static const AVOption colorchannelmixer_options[] = {
> +    { "rr", "set the red gain for the red channel",     OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=100}, -200, 200, FLAGS },
> +    { "rg", "set the green gain for the red channel",   OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "rb", "set the blue gain for the red channel",    OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "ra", "set the alpha gain for the red channel",   OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "gr", "set the red gain for the green channel",   OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=100}, -200, 200, FLAGS },
> +    { "gb", "set the blue gain for the green channel",  OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "br", "set the red gain for the blue channel",    OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "bg", "set the green gain for the blue channel",  OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "bb", "set the blue gain for the blue channel",   OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=100}, -200, 200, FLAGS },
> +    { "ba", "set the alpha gain for the blue channel",  OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "ar", "set the red gain for the alpha channel",   OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "ab", "set the blue gain for the alpha channel",  OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
> +    { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=100}, -200, 200, FLAGS },
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(colorchannelmixer);
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    static const enum AVPixelFormat pix_fmts[] = {
> +        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
> +        AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
> +        AV_PIX_FMT_ABGR,  AV_PIX_FMT_ARGB,
> +        AV_PIX_FMT_0BGR,  AV_PIX_FMT_0RGB,
> +        AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
> +        AV_PIX_FMT_NONE
> +    };
> +
> +    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +    return 0;
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    ColorChannelMixerContext *cm = ctx->priv;
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
> +    int i;
> +
> +    for (i = 0; i < 256; i++) {
> +        cm->RR[i] = i * cm->rr * 0.01;
> +        cm->RG[i] = i * cm->rg * 0.01;
> +        cm->RB[i] = i * cm->rb * 0.01;
> +        cm->RA[i] = i * cm->ra * 0.01;
> +
> +        cm->GR[i] = i * cm->gr * 0.01;
> +        cm->GG[i] = i * cm->gg * 0.01;
> +        cm->GB[i] = i * cm->gb * 0.01;
> +        cm->GA[i] = i * cm->ga * 0.01;
> +
> +        cm->BR[i] = i * cm->br * 0.01;
> +        cm->BG[i] = i * cm->bg * 0.01;
> +        cm->BB[i] = i * cm->bb * 0.01;
> +        cm->BA[i] = i * cm->ba * 0.01;
> +
> +        cm->AR[i] = i * cm->ar * 0.01;
> +        cm->AG[i] = i * cm->ag * 0.01;
> +        cm->AB[i] = i * cm->ab * 0.01;
> +        cm->AA[i] = i * cm->aa * 0.01;
> +    }
> +
> +    ff_fill_rgba_map(cm->rgba_map, outlink->format);
> +    cm->step = av_get_padded_bits_per_pixel(desc) >> 3;
> +
> +    return 0;
> +}
> +
> +static int filter_frame(AVFilterLink *inlink, AVFrame *in)
> +{
> +    AVFilterContext *ctx = inlink->dst;
> +    ColorChannelMixerContext *cm = ctx->priv;
> +    AVFilterLink *outlink = ctx->outputs[0];
> +    const uint8_t roffset = cm->rgba_map[0];
> +    const uint8_t goffset = cm->rgba_map[1];
> +    const uint8_t boffset = cm->rgba_map[2];
> +    const uint8_t aoffset = cm->rgba_map[3];
> +    const uint8_t *srcrow = in->data[0];
> +    const int step = cm->step;
> +    uint8_t *dstrow;
> +    AVFrame *out;
> +    int i, j;
> +
> +    if (av_frame_is_writable(in)) {
> +        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);
> +    }
> +

> +    dstrow = out->data[0];
> +    switch (outlink->format) {
> +    case AV_PIX_FMT_0BGR:
> +    case AV_PIX_FMT_0RGB:
> +    case AV_PIX_FMT_BGR0:
> +    case AV_PIX_FMT_BGR24:
> +    case AV_PIX_FMT_RGB0:
> +    case AV_PIX_FMT_RGB24:
> +        for (i = 0; i < outlink->h; i++) {
> +            const uint8_t *src = srcrow;
> +            uint8_t *dst = dstrow;
> +
> +            for (j = 0; j < outlink->w * step; j += step) {
> +                const uint8_t rin = src[j + roffset];
> +                const uint8_t gin = src[j + goffset];
> +                const uint8_t bin = src[j + boffset];
> +
> +                dst[j + roffset] = av_clip_uint8(cm->RR[rin] + cm->RG[gin] + cm->RB[bin]);
> +                dst[j + goffset] = av_clip_uint8(cm->GR[rin] + cm->GG[gin] + cm->GB[bin]);
> +                dst[j + boffset] = av_clip_uint8(cm->BR[rin] + cm->BG[gin] + cm->BB[bin]);
> +                if (in != out && step == 4)
> +                    dst[j + aoffset] = 0;
> +            }
> +
> +            srcrow += in->linesize[0];
> +            dstrow += out->linesize[0];
> +        }
> +        break;
> +    case AV_PIX_FMT_ABGR:
> +    case AV_PIX_FMT_ARGB:
> +    case AV_PIX_FMT_BGRA:
> +    case AV_PIX_FMT_RGBA:
> +        for (i = 0; i < outlink->h; i++) {
> +            const uint8_t *src = srcrow;
> +            uint8_t *dst = dstrow;
> +
> +            for (j = 0; j < outlink->w * 4; j += 4) {
> +                const uint8_t rin = src[j + roffset];
> +                const uint8_t gin = src[j + goffset];
> +                const uint8_t bin = src[j + boffset];
> +                const uint8_t ain = src[j + aoffset];
> +
> +                dst[j + roffset] = av_clip_uint8(cm->RR[rin] + cm->RG[gin] + cm->RB[bin] + cm->RA[ain]);
> +                dst[j + goffset] = av_clip_uint8(cm->GR[rin] + cm->GG[gin] + cm->GB[bin] + cm->GA[ain]);
> +                dst[j + boffset] = av_clip_uint8(cm->BR[rin] + cm->BG[gin] + cm->BB[bin] + cm->BA[ain]);
> +                dst[j + aoffset] = av_clip_uint8(cm->AR[rin] + cm->AG[gin] + cm->AB[bin] + cm->AA[ain]);
> +            }
> +
> +            srcrow += in->linesize[0];
> +            dstrow += out->linesize[0];
> +        }

I suppose this can't be easily factorized with no speedloss...

[...]

LGTM otherwise apart the 100 vs 1 thing.
-- 
FFmpeg = Faithless and Faithless Magical Pitiful Elfic Gargoyle


More information about the ffmpeg-devel mailing list