Browse code

colorbalance filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/04/15 07:16:51
Showing 6 changed files
... ...
@@ -23,6 +23,7 @@ version <next>:
23 23
 - new interlace filter
24 24
 - smptehdbars source
25 25
 - inverse telecine filters (fieldmatch and decimate)
26
+- colorbalance filter
26 27
 
27 28
 
28 29
 version 1.2:
... ...
@@ -2051,6 +2051,46 @@ boxblur=luma_radius=min(h\,w)/10:luma_power=1:chroma_radius=min(cw\,ch)/10:chrom
2051 2051
 @end example
2052 2052
 @end itemize
2053 2053
 
2054
+@section colorbalance
2055
+Modify intensity of primary colors (red, green and blue) of input frames.
2056
+
2057
+The filter allows an input frame to be adjusted in the shadows, midtones or highlights
2058
+regions for the red-cyan, green-magenta or blue-yellow balance.
2059
+
2060
+A positive adjustment value shifts the balance towards the primary color, a negative
2061
+value towards the complementary color.
2062
+
2063
+The filter accepts the following options:
2064
+
2065
+@table @option
2066
+@item rs
2067
+@item gs
2068
+@item bs
2069
+Adjust red, green and blue shadows (darkest pixels).
2070
+
2071
+@item rm
2072
+@item gm
2073
+@item bm
2074
+Adjust red, green and blue midtones (medium pixels).
2075
+
2076
+@item rh
2077
+@item gh
2078
+@item bh
2079
+Adjust red, green and blue highlights (brightest pixels).
2080
+
2081
+Allowed ranges for options are @code{[-1.0, 1.0]}. Defaults are @code{0}.
2082
+@end table
2083
+
2084
+@subsection Examples
2085
+
2086
+@itemize
2087
+@item
2088
+Add red color cast to shadows:
2089
+@example
2090
+colorbalance=rs=.3
2091
+@end example
2092
+@end itemize
2093
+
2054 2094
 @section colormatrix
2055 2095
 
2056 2096
 Convert color matrix.
... ...
@@ -104,6 +104,7 @@ OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
104 104
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
105 105
 OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o
106 106
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
107
+OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
107 108
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
108 109
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
109 110
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
... ...
@@ -102,6 +102,7 @@ void avfilter_register_all(void)
102 102
     REGISTER_FILTER(BLACKFRAME,     blackframe,     vf);
103 103
     REGISTER_FILTER(BLEND,          blend,          vf);
104 104
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
105
+    REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
105 106
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
106 107
     REGISTER_FILTER(COPY,           copy,           vf);
107 108
     REGISTER_FILTER(CROP,           crop,           vf);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  56
33
-#define LIBAVFILTER_VERSION_MICRO 103
32
+#define LIBAVFILTER_VERSION_MINOR  57
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,212 @@
0
+/*
1
+ * Copyright (c) 2013 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/opt.h"
21
+#include "libavutil/pixdesc.h"
22
+#include "avfilter.h"
23
+#include "drawutils.h"
24
+#include "formats.h"
25
+#include "internal.h"
26
+#include "video.h"
27
+
28
+#define R 0
29
+#define G 1
30
+#define B 2
31
+#define A 3
32
+
33
+typedef struct {
34
+    double shadows;
35
+    double midtones;
36
+    double highlights;
37
+} Range;
38
+
39
+typedef struct {
40
+    const AVClass *class;
41
+    Range cyan_red;
42
+    Range magenta_green;
43
+    Range yellow_blue;
44
+
45
+    uint8_t lut[3][256];
46
+
47
+    uint8_t rgba_map[4];
48
+    int step;
49
+} ColorBalanceContext;
50
+
51
+#define OFFSET(x) offsetof(ColorBalanceContext, x)
52
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53
+static const AVOption colorbalance_options[] = {
54
+    { "rs", "set red shadows",      OFFSET(cyan_red.shadows),         AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
55
+    { "gs", "set green shadows",    OFFSET(magenta_green.shadows),    AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
56
+    { "bs", "set blue shadows",     OFFSET(yellow_blue.shadows),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
57
+    { "rm", "set red midtones",     OFFSET(cyan_red.midtones),        AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
58
+    { "gm", "set green midtones",   OFFSET(magenta_green.midtones),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
59
+    { "bm", "set blue midtones",    OFFSET(yellow_blue.midtones),     AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
60
+    { "rh", "set red highlights",   OFFSET(cyan_red.highlights),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
61
+    { "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
62
+    { "bh", "set blue highlights",  OFFSET(yellow_blue.highlights),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
63
+    { NULL }
64
+};
65
+
66
+AVFILTER_DEFINE_CLASS(colorbalance);
67
+
68
+static int query_formats(AVFilterContext *ctx)
69
+{
70
+    static const enum AVPixelFormat pix_fmts[] = {
71
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
72
+        AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
73
+        AV_PIX_FMT_ABGR,  AV_PIX_FMT_ARGB,
74
+        AV_PIX_FMT_0BGR,  AV_PIX_FMT_0RGB,
75
+        AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
76
+        AV_PIX_FMT_NONE
77
+    };
78
+
79
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
80
+    return 0;
81
+}
82
+
83
+static int config_output(AVFilterLink *outlink)
84
+{
85
+    AVFilterContext *ctx = outlink->src;
86
+    ColorBalanceContext *cb = ctx->priv;
87
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
88
+    double *shadows, *midtones, *highlights, *buffer;
89
+    int i, r, g, b;
90
+
91
+    buffer = av_malloc(256 * 3 * sizeof(*buffer));
92
+    if (!buffer)
93
+        return AVERROR(ENOMEM);
94
+
95
+    shadows    = buffer + 256 * 0;
96
+    midtones   = buffer + 256 * 1;
97
+    highlights = buffer + 256 * 2;
98
+
99
+    for (i = 0; i < 256; i++) {
100
+        double low = av_clipd((i - 85.0) / -64.0 + 0.5, 0, 1) * 178.5;
101
+        double mid = av_clipd((i - 85.0) /  64.0 + 0.5, 0, 1) *
102
+                     av_clipd((i + 85.0 - 255.0) / -64.0 + 0.5, 0, 1) * 178.5;
103
+
104
+        shadows[i] = low;
105
+        midtones[i] = mid;
106
+        highlights[255 - i] = low;
107
+    }
108
+
109
+    for (i = 0; i < 256; i++) {
110
+        r = g = b = i;
111
+
112
+        r = av_clip_uint8(r + cb->cyan_red.shadows         * shadows[r]);
113
+        r = av_clip_uint8(r + cb->cyan_red.midtones        * midtones[r]);
114
+        r = av_clip_uint8(r + cb->cyan_red.highlights      * highlights[r]);
115
+
116
+        g = av_clip_uint8(g + cb->magenta_green.shadows    * shadows[g]);
117
+        g = av_clip_uint8(g + cb->magenta_green.midtones   * midtones[g]);
118
+        g = av_clip_uint8(g + cb->magenta_green.highlights * highlights[g]);
119
+
120
+        b = av_clip_uint8(b + cb->yellow_blue.shadows      * shadows[b]);
121
+        b = av_clip_uint8(b + cb->yellow_blue.midtones     * midtones[b]);
122
+        b = av_clip_uint8(b + cb->yellow_blue.highlights   * highlights[b]);
123
+
124
+        cb->lut[R][i] = r;
125
+        cb->lut[G][i] = g;
126
+        cb->lut[B][i] = b;
127
+    }
128
+
129
+    av_free(buffer);
130
+
131
+    ff_fill_rgba_map(cb->rgba_map, outlink->format);
132
+    cb->step = av_get_padded_bits_per_pixel(desc) >> 3;
133
+
134
+    return 0;
135
+}
136
+
137
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
138
+{
139
+    AVFilterContext *ctx = inlink->dst;
140
+    ColorBalanceContext *cb = ctx->priv;
141
+    AVFilterLink *outlink = ctx->outputs[0];
142
+    const uint8_t roffset = cb->rgba_map[R];
143
+    const uint8_t goffset = cb->rgba_map[G];
144
+    const uint8_t boffset = cb->rgba_map[B];
145
+    const uint8_t aoffset = cb->rgba_map[A];
146
+    const int step = cb->step;
147
+    const uint8_t *srcrow = in->data[0];
148
+    uint8_t *dstrow;
149
+    AVFrame *out;
150
+    int i, j;
151
+
152
+    if (av_frame_is_writable(in)) {
153
+        out = in;
154
+    } else {
155
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
156
+        if (!out) {
157
+            av_frame_free(&in);
158
+            return AVERROR(ENOMEM);
159
+        }
160
+        av_frame_copy_props(out, in);
161
+    }
162
+
163
+    dstrow = out->data[0];
164
+    for (i = 0; i < outlink->h; i++) {
165
+        const uint8_t *src = srcrow;
166
+        uint8_t *dst = dstrow;
167
+
168
+        for (j = 0; j < outlink->w * step; j += step) {
169
+            dst[j + roffset] = cb->lut[R][src[j + roffset]];
170
+            dst[j + goffset] = cb->lut[G][src[j + goffset]];
171
+            dst[j + boffset] = cb->lut[B][src[j + boffset]];
172
+            if (in != out && step == 4)
173
+                dst[j + aoffset] = src[j + aoffset];
174
+        }
175
+
176
+        srcrow += in->linesize[0];
177
+        dstrow += out->linesize[0];
178
+    }
179
+
180
+    if (in != out)
181
+        av_frame_free(&in);
182
+    return ff_filter_frame(ctx->outputs[0], out);
183
+}
184
+
185
+static const AVFilterPad colorbalance_inputs[] = {
186
+    {
187
+        .name           = "default",
188
+        .type           = AVMEDIA_TYPE_VIDEO,
189
+        .filter_frame   = filter_frame,
190
+    },
191
+    { NULL }
192
+};
193
+
194
+static const AVFilterPad colorbalance_outputs[] = {
195
+     {
196
+         .name         = "default",
197
+         .type         = AVMEDIA_TYPE_VIDEO,
198
+         .config_props = config_output,
199
+     },
200
+     { NULL }
201
+};
202
+
203
+AVFilter avfilter_vf_colorbalance = {
204
+    .name          = "colorbalance",
205
+    .description   = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
206
+    .priv_size     = sizeof(ColorBalanceContext),
207
+    .priv_class    = &colorbalance_class,
208
+    .query_formats = query_formats,
209
+    .inputs        = colorbalance_inputs,
210
+    .outputs       = colorbalance_outputs,
211
+};