[FFmpeg-devel] [PATCH] avfilter/WIP: add selectivecolor filter

Clément Bœsch u at pkh.me
Sun Jul 19 18:27:44 CEST 2015


---
This is an attempt to reproduce the exact same behaviour as the
selective color tool in photoshop (and maybe other adobe tool). Since
I didn't find time to write the documentation yet, the filter can be
summarized with something like: it allows you to adjust cyan, magenta,
yellow on only a certain ranges of colors (reds, yellows, darks, ...).
The adjustment range is defined by the "purity" of the color (that is,
how saturated it already is).

It's still incomplete (see @todo in the header and XXX in the code). If
anyone is feeling like helping in the reverse of the filter, it's very
welcome.
---
 doc/filters.texi                |   4 +
 libavfilter/Makefile            |   1 +
 libavfilter/allfilters.c        |   1 +
 libavfilter/vf_selectivecolor.c | 373 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 379 insertions(+)
 create mode 100644 libavfilter/vf_selectivecolor.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 1bef836..747f573 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -11917,6 +11917,10 @@ select=n=2:e='mod(n, 2)+1' [odd][even]; [odd] pad=h=2*ih [tmp]; [tmp][even] over
 @end example
 @end itemize
 
+ at section selectivecolor
+
+TODO
+
 @section sendcmd, asendcmd
 
 Send commands to filters in the filtergraph.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a259851..4aebf4e 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -191,6 +191,7 @@ OBJS-$(CONFIG_SEPARATEFIELDS_FILTER)         += vf_separatefields.o
 OBJS-$(CONFIG_SAB_FILTER)                    += vf_sab.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
+OBJS-$(CONFIG_SELECTIVECOLOR_FILTER)         += vf_selectivecolor.o
 OBJS-$(CONFIG_SENDCMD_FILTER)                += f_sendcmd.o
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
 OBJS-$(CONFIG_SETFIELD_FILTER)               += vf_setfield.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 01c9e38..e1e5162 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -205,6 +205,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(SAB,            sab,            vf);
     REGISTER_FILTER(SCALE,          scale,          vf);
     REGISTER_FILTER(SELECT,         select,         vf);
+    REGISTER_FILTER(SELECTIVECOLOR, selectivecolor, vf);
     REGISTER_FILTER(SENDCMD,        sendcmd,        vf);
     REGISTER_FILTER(SEPARATEFIELDS, separatefields, vf);
     REGISTER_FILTER(SETDAR,         setdar,         vf);
diff --git a/libavfilter/vf_selectivecolor.c b/libavfilter/vf_selectivecolor.c
new file mode 100644
index 0000000..a93de27
--- /dev/null
+++ b/libavfilter/vf_selectivecolor.c
@@ -0,0 +1,373 @@
+/*
+ * Copyright (c) 2015 Clément Bœsch <u pkh me>
+ *
+ * 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
+ */
+
+/**
+ * @todo
+ *
+ * - make sure code works properly (likely bugs everywhere)
+ * - use integers
+ * - fate test
+ * - PS param file input
+ * - doc
+ * - eval expressions?
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+enum color_range {
+    RANGE_REDS,
+    RANGE_YELLOWS,
+    RANGE_GREENS,
+    RANGE_CYANS,
+    RANGE_BLUES,
+    RANGE_MAGENTAS,
+    RANGE_WHITES,
+    RANGE_NEUTRALS,
+    RANGE_BLACKS,
+    NB_RANGES
+};
+
+typedef int (*get_adjust_range_func)(int r, int g, int b, int min_val, int max_val);
+
+struct process_range {
+    int range_id;
+    uint32_t mask;
+    get_adjust_range_func get_adjust_range;
+};
+
+typedef struct ThreadData {
+    AVFrame *in, *out;
+} ThreadData;
+
+typedef struct {
+    const AVClass *class;
+
+    int relative;
+    char *opt_cmyk_adjust[NB_RANGES];
+    float cmyk_adjust[NB_RANGES][4];
+
+    struct process_range process_ranges[NB_RANGES]; // color ranges to process
+    int nb_process_ranges;
+
+} SelectiveColorContext;
+
+#define OFFSET(x) offsetof(SelectiveColorContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+#define RANGE_OPTION(color_name, range) \
+    { color_name"s", "adjust "color_name" regions", OFFSET(opt_cmyk_adjust[range]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS }
+
+static const AVOption selectivecolor_options[] = {
+    { "relative", "set for relative adjustments", OFFSET(relative), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
+    RANGE_OPTION("red",     RANGE_REDS),
+    RANGE_OPTION("yellow",  RANGE_YELLOWS),
+    RANGE_OPTION("green",   RANGE_GREENS),
+    RANGE_OPTION("cyan",    RANGE_CYANS),
+    RANGE_OPTION("blue",    RANGE_BLUES),
+    RANGE_OPTION("magenta", RANGE_MAGENTAS),
+    RANGE_OPTION("white",   RANGE_WHITES),
+    RANGE_OPTION("neutral", RANGE_NEUTRALS),
+    RANGE_OPTION("black",   RANGE_BLACKS),
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(selectivecolor);
+
+static inline int get_mid_val(int r, int g, int b)
+{
+    if ((r < g && r > b) || (r < b && r > g)) return r;
+    if ((g < r && g > b) || (g < b && g > r)) return g;
+    if ((b < r && b > g) || (b < g && b > r)) return b;
+    return -1;
+}
+
+static int get_rgb_adjust_range(int r, int g, int b, int min_val, int max_val)
+{
+    // max - mid
+    const int mid_val = get_mid_val(r, g, b);
+    if (mid_val == -1) {
+        // XXX: can be simplified
+        if ((r != min_val && g == min_val && b == min_val) ||
+            (r == min_val && g != min_val && b == min_val) ||
+            (r == min_val && g == min_val && b != min_val))
+            return max_val - min_val;
+        return 0;
+    }
+    return max_val - mid_val;
+}
+
+static int get_cmy_adjust_range(int r, int g, int b, int min_val, int max_val)
+{
+    // mid - min
+    const int mid_val = get_mid_val(r, g, b);
+    if (mid_val == -1) {
+        // XXX: refactor with rgb
+        if ((r != max_val && g == max_val && b == max_val) ||
+            (r == max_val && g != max_val && b == max_val) ||
+            (r == max_val && g == max_val && b != max_val))
+            return max_val - min_val;
+        return 0;
+    }
+    return mid_val - min_val;
+}
+
+static int get_neutrals_adjust_range(int r, int g, int b, int min_val, int max_val)
+{
+    // 1 - (|max-0.5| + |min-0.5|)
+    return (255*2 - (abs((max_val<<1) - 255) + abs((min_val<<1) - 255)) + 1) >> 1;
+}
+
+static int get_whites_adjust_range(int r, int g, int b, int min_val, int max_val)
+{
+    // (min - 0.5) * 2
+    return (min_val<<1) - 255;
+}
+
+static int get_blacks_adjust_range(int r, int g, int b, int min_val, int max_val)
+{
+    // (0.5 - max) * 2
+    return 255 - (max_val<<1);
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    int i;
+    SelectiveColorContext *s = ctx->priv;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(s->opt_cmyk_adjust); i++) {
+        const char *opt_cmyk_adjust = s->opt_cmyk_adjust[i];
+
+        if (opt_cmyk_adjust) {
+            float *cmyk = s->cmyk_adjust[i];
+
+            sscanf(s->opt_cmyk_adjust[i], "%f|%f|%f|%f", cmyk, cmyk+1, cmyk+2, cmyk+3);
+
+            if (cmyk[0] < -1.0 || cmyk[0] > 1.0 ||
+                cmyk[1] < -1.0 || cmyk[1] > 1.0 ||
+                cmyk[2] < -1.0 || cmyk[2] > 1.0 ||
+                cmyk[3] < -1.0 || cmyk[3] > 1.0) {
+                av_log(s, AV_LOG_ERROR, "CMYK settings must be set in [-1;1] range\n");
+                return AVERROR(EINVAL);
+            }
+
+            /* If the color range has user settings, register the color range
+             * as "to be processed" */
+            if (cmyk[0] || cmyk[1] || cmyk[2] || cmyk[3]) {
+                struct process_range *pr = &s->process_ranges[s->nb_process_ranges++];
+
+                pr->range_id = i;
+                pr->mask = 1 << i;
+                if      (pr->mask & (1<<RANGE_REDS  | 1<<RANGE_GREENS   | 1<<RANGE_BLUES))   pr->get_adjust_range = get_rgb_adjust_range;
+                else if (pr->mask & (1<<RANGE_CYANS | 1<<RANGE_MAGENTAS | 1<<RANGE_YELLOWS)) pr->get_adjust_range = get_cmy_adjust_range;
+                else if (pr->mask & 1<<RANGE_WHITES)                                         pr->get_adjust_range = get_whites_adjust_range;
+                else if (pr->mask & 1<<RANGE_NEUTRALS)                                       pr->get_adjust_range = get_neutrals_adjust_range;
+                else if (pr->mask & 1<<RANGE_BLACKS)                                         pr->get_adjust_range = get_blacks_adjust_range;
+                else
+                    av_assert0(0);
+            }
+        }
+    }
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[]  = {AV_PIX_FMT_RGB32, AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE};
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
+}
+
+static inline int comp_adjust(int adjust_range, int value, float adjust, int relative)
+{
+    const float left_pcent  =      value / 255.;
+    const float right_pcent = 1. - value / 255.;
+    if (relative)
+        adjust *= right_pcent; // XXX: to be confirmed
+    return lrint(av_clipf(-adjust, -left_pcent, right_pcent) * adjust_range);
+}
+
+static inline int absolute_comp_adjust(int adjust_range, int value, float adjust)
+{
+    return comp_adjust(adjust_range, value, adjust, 0);
+}
+
+static inline int relative_comp_adjust(int adjust_range, int value, float adjust)
+{
+    return comp_adjust(adjust_range, value, adjust, 1);
+}
+
+static inline int selective_color(AVFilterContext *ctx, ThreadData *td,
+                                  int jobnr, int nb_jobs, int direct, int relative)
+{
+    int i, x, y;
+    const AVFrame *in = td->in;
+    AVFrame *out = td->out;
+    const SelectiveColorContext *s = ctx->priv;
+    const int height = in->height;
+    const int width  = in->width;
+    const int slice_start = (height *  jobnr   ) / nb_jobs;
+    const int slice_end   = (height * (jobnr+1)) / nb_jobs;
+    const int dst_linesize = out->linesize[0];
+    const int src_linesize =  in->linesize[0];
+    uint8_t       *dst = out->data[0] + slice_start * dst_linesize;
+    const uint8_t *src =  in->data[0] + slice_start * src_linesize;
+
+    for (y = slice_start; y < slice_end; y++) {
+        const uint32_t *src32 = (const uint32_t *)src;
+        uint32_t       *dst32 = (uint32_t *)dst;
+
+        for (x = 0; x < width; x++) {
+            const uint32_t color = *src32++;
+            const int r = color >> 16 & 0xff;
+            const int g = color >>  8 & 0xff;
+            const int b = color       & 0xff;
+            const int min_color = FFMIN3(r, g, b);
+            const int max_color = FFMAX3(r, g, b);
+            const uint32_t range_flag = (r == max_color) << RANGE_REDS
+                                      | (r == min_color) << RANGE_CYANS
+                                      | (g == max_color) << RANGE_GREENS
+                                      | (g == min_color) << RANGE_MAGENTAS
+                                      | (b == max_color) << RANGE_BLUES
+                                      | (b == min_color) << RANGE_YELLOWS
+                                      | (r > 128 && g > 128 && b > 128) << RANGE_WHITES
+                                      | (color && (color & 0xffffff) != 0xffffff) << RANGE_NEUTRALS
+                                      | (r < 128 && g < 128 && b < 128) << RANGE_BLACKS;
+
+            int adjust_r = 0, adjust_g = 0, adjust_b = 0;
+
+            for (i = 0; i < s->nb_process_ranges; i++) {
+                const struct process_range *pr = &s->process_ranges[i];
+
+                if (range_flag & pr->mask) {
+                    const int adjust_range = pr->get_adjust_range(r, g, b, min_color, max_color);
+
+                    if (adjust_range > 0) {
+                        const float *cmyk_adjust = s->cmyk_adjust[pr->range_id];
+
+                        if (relative) {
+                            adjust_r += relative_comp_adjust(adjust_range, r, cmyk_adjust[0] + cmyk_adjust[3]);
+                            adjust_g += relative_comp_adjust(adjust_range, g, cmyk_adjust[1] + cmyk_adjust[3]);
+                            adjust_b += relative_comp_adjust(adjust_range, b, cmyk_adjust[2] + cmyk_adjust[3]);
+                        } else {
+                            adjust_r += absolute_comp_adjust(adjust_range, r, cmyk_adjust[0] + cmyk_adjust[3]);
+                            adjust_g += absolute_comp_adjust(adjust_range, g, cmyk_adjust[1] + cmyk_adjust[3]);
+                            adjust_b += absolute_comp_adjust(adjust_range, b, cmyk_adjust[2] + cmyk_adjust[3]);
+                        }
+                    }
+                }
+            }
+
+            if (!direct || adjust_r || adjust_g || adjust_b)
+                *dst32 = (color & 0xff000000)
+                       | av_clip_uint8(r + adjust_r) << 16
+                       | av_clip_uint8(g + adjust_g) <<  8
+                       | av_clip_uint8(b + adjust_b);
+            dst32++;
+        }
+        src += src_linesize;
+        dst += dst_linesize;
+    }
+    return 0;
+}
+
+#define DEF_SELECTIVE_COLOR_FUNC(name, direct, relative)                                    \
+static int selective_color_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)  \
+{                                                                                           \
+    return selective_color(ctx, arg, jobnr, nb_jobs, direct, relative);                     \
+}
+
+DEF_SELECTIVE_COLOR_FUNC(indirect_absolute, 0, 0)
+DEF_SELECTIVE_COLOR_FUNC(indirect_relative, 0, 1)
+DEF_SELECTIVE_COLOR_FUNC(  direct_absolute, 1, 0)
+DEF_SELECTIVE_COLOR_FUNC(  direct_relative, 1, 1)
+
+typedef int (*selective_color_func_type)(AVFilterContext *ctx, void *td, int jobnr, int nb_jobs);
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    int direct;
+    AVFrame *out;
+    ThreadData td;
+    const SelectiveColorContext *s = ctx->priv;
+    static selective_color_func_type funcs[2][2] = {
+        {selective_color_indirect_absolute, selective_color_indirect_relative},
+        {selective_color_direct_absolute,   selective_color_direct_relative},
+    };
+
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        direct = 0;
+        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);
+    }
+
+    td.in = in;
+    td.out = out;
+    ctx->internal->execute(ctx, funcs[direct][s->relative], &td, NULL,
+                           FFMIN(inlink->h, ctx->graph->nb_threads));
+
+    if (!direct)
+        av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+
+static const AVFilterPad selectivecolor_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad selectivecolor_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_selectivecolor = {
+    .name          = "selectivecolor",
+    .description   = NULL_IF_CONFIG_SMALL("Apply CMYK adjustments to specific color ranges."),
+    .priv_size     = sizeof(SelectiveColorContext),
+    .init          = init,
+    .query_formats = query_formats,
+    .inputs        = selectivecolor_inputs,
+    .outputs       = selectivecolor_outputs,
+    .priv_class    = &selectivecolor_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
+};
-- 
2.4.5



More information about the ffmpeg-devel mailing list