Browse code

lavfi: add hue filter

This is a port of the MPlayer hue filter (libmpcodecs/vf_hue.c) by
Michael Niedermayer.

Signed-off-by: Jérémy Tran <tran.jeremy.av@gmail.com>
Signed-off-by: Stefano Sabatini <stefasab@gmail.com>

Jérémy Tran authored on 2012/08/12 20:26:17
Showing 6 changed files
... ...
@@ -44,6 +44,7 @@ version next:
44 44
 - bmp parser
45 45
 - smptebars source
46 46
 - asetpts filter
47
+- hue filter
47 48
 
48 49
 
49 50
 version 0.11:
... ...
@@ -2187,6 +2187,17 @@ a float number which specifies chroma temporal strength, defaults to
2187 2187
 @var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}
2188 2188
 @end table
2189 2189
 
2190
+@section hue
2191
+
2192
+Modify the hue and/or the saturation of the input.
2193
+
2194
+This filter accepts the optional parameters: @var{hue}:@var{saturation}.
2195
+
2196
+@var{hue} must be a float number that specifies the hue angle as a
2197
+number of degrees, and defaults to 0.0.
2198
+@var{saturation} must be a float number that specifies the saturation
2199
+in the [-10,10] range, and defaults to 1.0.
2200
+
2190 2201
 @section idet
2191 2202
 
2192 2203
 Interlaceing detect filter. This filter tries to detect if the input is
... ...
@@ -99,6 +99,7 @@ OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
99 99
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
100 100
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
101 101
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
102
+OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
102 103
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
103 104
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
104 105
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
... ...
@@ -89,6 +89,7 @@ void avfilter_register_all(void)
89 89
     REGISTER_FILTER (GRADFUN,     gradfun,     vf);
90 90
     REGISTER_FILTER (HFLIP,       hflip,       vf);
91 91
     REGISTER_FILTER (HQDN3D,      hqdn3d,      vf);
92
+    REGISTER_FILTER (HUE,         hue,         vf);
92 93
     REGISTER_FILTER (IDET,        idet,        vf);
93 94
     REGISTER_FILTER (LUT,         lut,         vf);
94 95
     REGISTER_FILTER (LUTRGB,      lutrgb,      vf);
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  8
32
+#define LIBAVFILTER_VERSION_MINOR  9
33 33
 #define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
new file mode 100644
... ...
@@ -0,0 +1,202 @@
0
+/*
1
+ * Copyright (c) 2003 Michael Niedermayer
2
+ * Copyright (c) 2012 Jeremy Tran
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Apply a hue/saturation filter to the input video
24
+ * Ported from MPlayer libmpcodecs/vf_hue.c.
25
+ */
26
+
27
+#include "libavutil/imgutils.h"
28
+#include "libavutil/pixdesc.h"
29
+
30
+#include "avfilter.h"
31
+#include "formats.h"
32
+#include "internal.h"
33
+#include "video.h"
34
+
35
+typedef struct {
36
+    float    hue;
37
+    float    saturation;
38
+    int      hsub;
39
+    int      vsub;
40
+    int32_t hue_sin;
41
+    int32_t hue_cos;
42
+} HueContext;
43
+
44
+static av_cold int init(AVFilterContext *ctx, const char *args)
45
+{
46
+    HueContext *hue = ctx->priv;
47
+    float h = 0, s = 1;
48
+    int n;
49
+    char c1 = 0, c2 = 0;
50
+
51
+    if (args) {
52
+        n = sscanf(args, "%f%c%f%c", &h, &c1, &s, &c2);
53
+        if (n != 0 && n != 1 && (n != 3 || c1 != ':')) {
54
+            av_log(ctx, AV_LOG_ERROR,
55
+                   "Invalid syntax for argument '%s': "
56
+                   "must be in the form 'hue[:saturation]'\n", args);
57
+            return AVERROR(EINVAL);
58
+        }
59
+    }
60
+
61
+    if (s < -10 || s > 10) {
62
+        av_log(ctx, AV_LOG_ERROR,
63
+               "Invalid value for saturation %0.1f: "
64
+               "must be included between range -10 and +10\n", s);
65
+        return AVERROR(EINVAL);
66
+    }
67
+
68
+    /* Convert angle from degree to radian */
69
+    hue->hue = h * M_PI / 180;
70
+    hue->saturation = s;
71
+
72
+    return 0;
73
+}
74
+
75
+static int query_formats(AVFilterContext *ctx)
76
+{
77
+    static const enum PixelFormat pix_fmts[] = {
78
+        PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
79
+        PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
80
+        PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
81
+        PIX_FMT_YUVA420P,
82
+        PIX_FMT_NONE
83
+    };
84
+
85
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
86
+
87
+    return 0;
88
+}
89
+
90
+static int config_props(AVFilterLink *inlink)
91
+{
92
+    HueContext *hue = inlink->dst->priv;
93
+    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
94
+
95
+    hue->hsub = desc->log2_chroma_w;
96
+    hue->vsub = desc->log2_chroma_h;
97
+    /*
98
+     * Scale the value to the norm of the resulting (U,V) vector, that is
99
+     * the saturation.
100
+     * This will be useful in the process_chrominance function.
101
+     */
102
+    hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
103
+    hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
104
+
105
+    return 0;
106
+}
107
+
108
+static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
109
+                                uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
110
+                                int w, int h,
111
+                                const int32_t c, const int32_t s)
112
+{
113
+    int32_t u, v, new_u, new_v;
114
+    int i;
115
+
116
+    /*
117
+     * If we consider U and V as the components of a 2D vector then its angle
118
+     * is the hue and the norm is the saturation
119
+     */
120
+    while (h--) {
121
+        for (i = 0; i < w; i++) {
122
+            /* Normalize the components from range [16;140] to [-112;112] */
123
+            u = usrc[i] - 128;
124
+            v = vsrc[i] - 128;
125
+            /*
126
+             * Apply the rotation of the vector : (c * u) - (s * v)
127
+             *                                    (s * u) + (c * v)
128
+             * De-normalize the components (without forgetting to scale 128
129
+             * by << 16)
130
+             * Finally scale back the result by >> 16
131
+             */
132
+            new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
133
+            new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
134
+
135
+            /* Prevent a potential overflow */
136
+            udst[i] = av_clip_uint8_c(new_u);
137
+            vdst[i] = av_clip_uint8_c(new_v);
138
+        }
139
+
140
+        usrc += src_linesize;
141
+        vsrc += src_linesize;
142
+        udst += dst_linesize;
143
+        vdst += dst_linesize;
144
+    }
145
+}
146
+
147
+static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
148
+{
149
+    HueContext        *hue    = inlink->dst->priv;
150
+    AVFilterBufferRef *inpic  = inlink->cur_buf;
151
+    AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
152
+    uint8_t *inrow[3], *outrow[3]; // 0 : Y, 1 : U, 2 : V
153
+    int plane;
154
+
155
+    inrow[0]  = inpic->data[0]  + y * inpic->linesize[0];
156
+    outrow[0] = outpic->data[0] + y * outpic->linesize[0];
157
+
158
+    for (plane = 1; plane < 3; plane++) {
159
+        inrow[plane]  = inpic->data[plane]  + (y >> hue->vsub) * inpic->linesize[plane];
160
+        outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
161
+    }
162
+
163
+    av_image_copy_plane(outrow[0], outpic->linesize[0],
164
+                        inrow[0],  inpic->linesize[0],
165
+                        inlink->w, inlink->h);
166
+
167
+    process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
168
+                        inrow[1], inrow[2], inpic->linesize[1],
169
+                        inlink->w >> hue->hsub, inlink->h >> hue->vsub,
170
+                        hue->hue_cos, hue->hue_sin);
171
+
172
+    return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
173
+}
174
+
175
+AVFilter avfilter_vf_hue = {
176
+    .name        = "hue",
177
+    .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
178
+
179
+    .priv_size = sizeof(HueContext),
180
+
181
+    .init          = init,
182
+    .query_formats = query_formats,
183
+
184
+    .inputs = (const AVFilterPad[]) {
185
+        {
186
+            .name         = "default",
187
+            .type         = AVMEDIA_TYPE_VIDEO,
188
+            .draw_slice   = draw_slice,
189
+            .config_props = config_props,
190
+            .min_perms    = AV_PERM_READ,
191
+        },
192
+        { .name = NULL }
193
+    },
194
+    .outputs = (const AVFilterPad[]) {
195
+        {
196
+            .name         = "default",
197
+            .type         = AVMEDIA_TYPE_VIDEO,
198
+        },
199
+        { .name = NULL }
200
+    }
201
+};