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

Paul B Mahol onemda at gmail.com
Tue Apr 16 14:23:31 CEST 2013


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi                   |  28 ++++++
 libavfilter/Makefile               |   1 +
 libavfilter/allfilters.c           |   1 +
 libavfilter/vf_colorchannelmixer.c | 180 +++++++++++++++++++++++++++++++++++++
 4 files changed, 210 insertions(+)
 create mode 100644 libavfilter/vf_colorchannelmixer.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 87811df..11880e4 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2078,6 +2078,34 @@ Adjust red, green and blue highlights (brightest pixels).
 Allowed range is from -100 to 100, default is 0.
 @end table
 
+ at section colorchannelmixer
+
+This filter adjust video input frames by re-mixing color channels.
+
+The filter accepts the following options:
+
+ at table @option
+ at item rr
+ at item rg
+ at item rb
+Adjust contribution of input red, green and blue channels for output red channel.
+Default is @code{100} for @var{rr}, and @code{0} for @var{rg} and @var{rb}.
+
+ at item gr
+ at item gg
+ at item gb
+Adjust contribution of input red, green and blue channels for output green channel.
+Default is @code{100} for @var{gg}, and @code{0} for @var{gr} and @var{gb}.
+
+ at item br
+ at item bg
+ at item bb
+Adjust contribution of input red, green and blue channels for output blue channel.
+Default is @code{100} for @var{bb}, and @code{0} for @var{br} and @var{bg}.
+
+Allowed ranges are from @code{-200} to @code{200}.
+ at end table
+
 @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..c146bb6
--- /dev/null
+++ b/libavfilter/vf_colorchannelmixer.c
@@ -0,0 +1,180 @@
+/*
+ * 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;
+    double gr, gg, gb;
+    double br, bg, bb;
+
+    int RR[256], RG[256], RB[256];
+    int GR[256], GG[256], GB[256];
+    int BR[256], BG[256], BB[256];
+
+    uint8_t rgba_map[4];
+    int nb_components;
+} 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", "", OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=100}, -200, 200, FLAGS },
+    { "rg", "", OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
+    { "rb", "", OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
+    { "gr", "", OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
+    { "gg", "", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=100}, -200, 200, FLAGS },
+    { "gb", "", OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
+    { "br", "", OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
+    { "bg", "", OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=  0}, -200, 200, FLAGS },
+    { "bb", "", OFFSET(bb), 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->GR[i] = i * cm->gr * 0.01;
+        cm->GG[i] = i * cm->gg * 0.01;
+        cm->GB[i] = i * cm->gb * 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;
+    }
+
+    ff_fill_rgba_map(cm->rgba_map, outlink->format);
+    cm->nb_components = 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 comp = cm->nb_components;
+    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];
+    for (i = 0; i < outlink->h; i++) {
+        const uint8_t *src = srcrow;
+        uint8_t *dst = dstrow;
+
+        for (j = 0; j < outlink->w * comp; j += comp) {
+            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 && comp == 4)
+                dst[j + aoffset] = src[j + aoffset];
+        }
+
+        srcrow += in->linesize[0];
+        dstrow += out->linesize[0];
+    }
+
+    if (in != out)
+        av_frame_free(&in);
+    return ff_filter_frame(ctx->outputs[0], out);
+}
+
+static const AVFilterPad colorchannelmixer_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad colorchannelmixer_outputs[] = {
+     {
+         .name         = "default",
+         .type         = AVMEDIA_TYPE_VIDEO,
+         .config_props = config_output,
+     },
+     { NULL }
+};
+
+AVFilter avfilter_vf_colorchannelmixer = {
+    .name          = "colorchannelmixer",
+    .description   = NULL_IF_CONFIG_SMALL("Mix color channels."),
+    .priv_size     = sizeof(ColorChannelMixerContext),
+    .query_formats = query_formats,
+    .inputs        = colorchannelmixer_inputs,
+    .outputs       = colorchannelmixer_outputs,
+    .priv_class    = &colorchannelmixer_class,
+};
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list