[FFmpeg-devel] [PATCH] colormixer filter

Paul B Mahol onemda at gmail.com
Mon Apr 15 22:33:12 CEST 2013


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

diff --git a/doc/filters.texi b/doc/filters.texi
index 178dbe5..eec836b 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2135,6 +2135,29 @@ For example to convert from BT.601 to SMPTE-240M, use the command:
 colormatrix=bt601:smpte240m
 @end example
 
+ at section colormixer
+
+This filter adjust video input frame by re-mixing its 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 channel to output red channel.
+
+ at item gr
+ at item gg
+ at item gb
+Adjust contribution of input red, green and blue channel to output green channel.
+
+ at item br
+ at item bg
+ at item bb
+Adjust contribution of input red, green and blue channel to output blue channel.
+ at end table
+
 @section copy
 
 Copy the input source unchanged to the output. Mainly useful for
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 0927986..35d46d6 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -106,6 +106,7 @@ OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
 OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
+OBJS-$(CONFIG_COLORMIXER_FILTER)             += vf_colormixer.o
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index bebbf2b..1fa900a 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -104,6 +104,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
     REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
+    REGISTER_FILTER(COLORMIXER,     colormixer,     vf);
     REGISTER_FILTER(COPY,           copy,           vf);
     REGISTER_FILTER(CROP,           crop,           vf);
     REGISTER_FILTER(CROPDETECT,     cropdetect,     vf);
diff --git a/libavfilter/vf_colormixer.c b/libavfilter/vf_colormixer.c
new file mode 100644
index 0000000..216f84c
--- /dev/null
+++ b/libavfilter/vf_colormixer.c
@@ -0,0 +1,165 @@
+/*
+ * 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/avassert.h"
+#include "avfilter.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];
+} ColorMixerContext;
+
+#define OFFSET(x) offsetof(ColorMixerContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption colormixer_options[] = {
+    { "rr", "", OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { "rg", "", OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "rb", "", OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "gr", "", OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "gg", "", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { "gb", "", OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "br", "", OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "bg", "", OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "bb", "", OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(colormixer);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_RGB24, 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;
+    ColorMixerContext *cm = ctx->priv;
+    int i;
+
+    for (i = 0; i < 256; i++) {
+        cm->RR[i] = cm->rr * i;
+        cm->RG[i] = cm->rg * i;
+        cm->RB[i] = cm->rb * i;
+
+        cm->GR[i] = cm->gr * i;
+        cm->GG[i] = cm->gg * i;
+        cm->GB[i] = cm->gb * i;
+
+        cm->BR[i] = cm->br * i;
+        cm->BG[i] = cm->bg * i;
+        cm->BB[i] = cm->bb * i;
+    }
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    ColorMixerContext *cm = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    const uint8_t *srcrow = in->data[0];
+    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 * 3; j += 3) {
+            uint8_t rin = src[j + 0];
+            uint8_t gin = src[j + 1];
+            uint8_t bin = src[j + 2];
+            int r, g, b;
+
+            r = av_clip_uint8(cm->RR[rin] + cm->RG[gin] + cm->RB[bin]);
+            g = av_clip_uint8(cm->GR[rin] + cm->GG[gin] + cm->GB[bin]);
+            b = av_clip_uint8(cm->BR[rin] + cm->BG[gin] + cm->BB[bin]);
+
+            dst[j + 0] = r;
+            dst[j + 1] = g;
+            dst[j + 2] = b;
+        }
+
+        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 colormixer_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad colormixer_outputs[] = {
+     {
+         .name         = "default",
+         .type         = AVMEDIA_TYPE_VIDEO,
+         .config_props = config_output,
+     },
+     { NULL }
+};
+
+AVFilter avfilter_vf_colormixer = {
+    .name          = "colormixer",
+    .description   = NULL_IF_CONFIG_SMALL("Mix color channels."),
+    .priv_size     = sizeof(ColorMixerContext),
+    .query_formats = query_formats,
+    .inputs        = colormixer_inputs,
+    .outputs       = colormixer_outputs,
+    .priv_class    = &colormixer_class,
+};
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list