Browse code

avfilter: add vibrance filter

Paul B Mahol authored on 2018/10/20 03:10:48
Showing 6 changed files
... ...
@@ -37,6 +37,7 @@ version <next>:
37 37
 - sinc audio filter source
38 38
 - chromahold filter
39 39
 - setparams filter
40
+- vibrance filter
40 41
 
41 42
 
42 43
 version 4.0:
... ...
@@ -17393,6 +17393,35 @@ and ones with constant delta pts.
17393 17393
 If there was frames with variable delta, than it will also show min and max delta
17394 17394
 encountered.
17395 17395
 
17396
+@section vibrance
17397
+
17398
+Boost or alter saturation.
17399
+
17400
+The filter accepts the following options:
17401
+@table @option
17402
+@item intensity
17403
+Set strength of boost if positive value or strength of alter if negative value.
17404
+Default is 0. Allowed range is from -2 to 2.
17405
+
17406
+@item rbal
17407
+Set the red balance. Default is 1. Allowed range is from -10 to 10.
17408
+
17409
+@item gbal
17410
+Set the green balance. Default is 1. Allowed range is from -10 to 10.
17411
+
17412
+@item bbal
17413
+Set the blue balance. Default is 1. Allowed range is from -10 to 10.
17414
+
17415
+@item rlum
17416
+Set the red luma coefficient.
17417
+
17418
+@item glum
17419
+Set the green luma coefficient.
17420
+
17421
+@item blum
17422
+Set the blue luma coefficient.
17423
+@end table
17424
+
17396 17425
 @anchor{vignette}
17397 17426
 @section vignette
17398 17427
 
... ...
@@ -394,6 +394,7 @@ OBJS-$(CONFIG_VAGUEDENOISER_FILTER)          += vf_vaguedenoiser.o
394 394
 OBJS-$(CONFIG_VECTORSCOPE_FILTER)            += vf_vectorscope.o
395 395
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
396 396
 OBJS-$(CONFIG_VFRDET_FILTER)                 += vf_vfrdet.o
397
+OBJS-$(CONFIG_VIBRANCE_FILTER)               += vf_vibrance.o
397 398
 OBJS-$(CONFIG_VIDSTABDETECT_FILTER)          += vidstabutils.o vf_vidstabdetect.o
398 399
 OBJS-$(CONFIG_VIDSTABTRANSFORM_FILTER)       += vidstabutils.o vf_vidstabtransform.o
399 400
 OBJS-$(CONFIG_VIGNETTE_FILTER)               += vf_vignette.o
... ...
@@ -375,6 +375,7 @@ extern AVFilter ff_vf_vaguedenoiser;
375 375
 extern AVFilter ff_vf_vectorscope;
376 376
 extern AVFilter ff_vf_vflip;
377 377
 extern AVFilter ff_vf_vfrdet;
378
+extern AVFilter ff_vf_vibrance;
378 379
 extern AVFilter ff_vf_vidstabdetect;
379 380
 extern AVFilter ff_vf_vidstabtransform;
380 381
 extern AVFilter ff_vf_vignette;
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   7
33
-#define LIBAVFILTER_VERSION_MINOR  36
33
+#define LIBAVFILTER_VERSION_MINOR  37
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,240 @@
0
+/*
1
+ * Copyright (c) 2018 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/imgutils.h"
22
+#include "avfilter.h"
23
+#include "formats.h"
24
+#include "internal.h"
25
+#include "video.h"
26
+
27
+typedef struct VibranceContext {
28
+    const AVClass *class;
29
+
30
+    float intensity;
31
+    float balance[3];
32
+    float lcoeffs[3];
33
+
34
+    int depth;
35
+
36
+    int (*do_slice)(AVFilterContext *s, void *arg,
37
+                    int jobnr, int nb_jobs);
38
+} VibranceContext;
39
+
40
+static inline float lerpf(float v0, float v1, float f)
41
+{
42
+    return v0 + (v1 - v0) * f;
43
+}
44
+
45
+static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
46
+{
47
+    VibranceContext *s = avctx->priv;
48
+    AVFrame *frame = arg;
49
+    const int width = frame->width;
50
+    const int height = frame->height;
51
+    const float gc = s->lcoeffs[0];
52
+    const float bc = s->lcoeffs[1];
53
+    const float rc = s->lcoeffs[2];
54
+    const float intensity = s->intensity;
55
+    const float gintensity = intensity * s->balance[0];
56
+    const float bintensity = intensity * s->balance[1];
57
+    const float rintensity = intensity * s->balance[2];
58
+    const int slice_start = (height * jobnr) / nb_jobs;
59
+    const int slice_end = (height * (jobnr + 1)) / nb_jobs;
60
+    const int glinesize = frame->linesize[0];
61
+    const int blinesize = frame->linesize[1];
62
+    const int rlinesize = frame->linesize[2];
63
+    uint8_t *gptr = frame->data[0] + slice_start * glinesize;
64
+    uint8_t *bptr = frame->data[1] + slice_start * blinesize;
65
+    uint8_t *rptr = frame->data[2] + slice_start * rlinesize;
66
+
67
+    for (int y = slice_start; y < slice_end; y++) {
68
+        for (int x = 0; x < width; x++) {
69
+            float g = gptr[x] / 255.f;
70
+            float b = bptr[x] / 255.f;
71
+            float r = rptr[x] / 255.f;
72
+            float max_color = FFMAX3(r, g, b);
73
+            float min_color = FFMIN3(r, g, b);
74
+            float color_saturation = max_color - min_color;
75
+            float luma = g * gc + r * rc + b * bc;
76
+            const float cg = 1.f + gintensity * (1.f - FFSIGN(gintensity) * color_saturation);
77
+            const float cb = 1.f + bintensity * (1.f - FFSIGN(bintensity) * color_saturation);
78
+            const float cr = 1.f + rintensity * (1.f - FFSIGN(rintensity) * color_saturation);
79
+
80
+            g = lerpf(luma, g, cg);
81
+            b = lerpf(luma, b, cb);
82
+            r = lerpf(luma, r, cr);
83
+
84
+            gptr[x] = av_clip_uint8(g * 255.f);
85
+            bptr[x] = av_clip_uint8(b * 255.f);
86
+            rptr[x] = av_clip_uint8(r * 255.f);
87
+        }
88
+
89
+        gptr += glinesize;
90
+        bptr += blinesize;
91
+        rptr += rlinesize;
92
+    }
93
+
94
+    return 0;
95
+}
96
+
97
+static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
98
+{
99
+    VibranceContext *s = avctx->priv;
100
+    AVFrame *frame = arg;
101
+    const int depth = s->depth;
102
+    const float max = (1 << depth) - 1;
103
+    const float gc = s->lcoeffs[0];
104
+    const float bc = s->lcoeffs[1];
105
+    const float rc = s->lcoeffs[2];
106
+    const int width = frame->width;
107
+    const int height = frame->height;
108
+    const float intensity = s->intensity;
109
+    const float gintensity = intensity * s->balance[0];
110
+    const float bintensity = intensity * s->balance[1];
111
+    const float rintensity = intensity * s->balance[2];
112
+    const int slice_start = (height * jobnr) / nb_jobs;
113
+    const int slice_end = (height * (jobnr + 1)) / nb_jobs;
114
+    const int glinesize = frame->linesize[0] / 2;
115
+    const int blinesize = frame->linesize[1] / 2;
116
+    const int rlinesize = frame->linesize[2] / 2;
117
+    uint16_t *gptr = (uint16_t *)frame->data[0] + slice_start * glinesize;
118
+    uint16_t *bptr = (uint16_t *)frame->data[1] + slice_start * blinesize;
119
+    uint16_t *rptr = (uint16_t *)frame->data[2] + slice_start * rlinesize;
120
+
121
+    for (int y = slice_start; y < slice_end; y++) {
122
+        for (int x = 0; x < width; x++) {
123
+            float g = gptr[x] / max;
124
+            float b = bptr[x] / max;
125
+            float r = rptr[x] / max;
126
+            float max_color = FFMAX3(r, g, b);
127
+            float min_color = FFMIN3(r, g, b);
128
+            float color_saturation = max_color - min_color;
129
+            float luma = g * gc + r * rc + b * bc;
130
+            const float cg = 1.f + gintensity * (1.f - FFSIGN(gintensity) * color_saturation);
131
+            const float cb = 1.f + bintensity * (1.f - FFSIGN(bintensity) * color_saturation);
132
+            const float cr = 1.f + rintensity * (1.f - FFSIGN(rintensity) * color_saturation);
133
+
134
+            g = lerpf(luma, g, cg);
135
+            b = lerpf(luma, b, cb);
136
+            r = lerpf(luma, r, cr);
137
+
138
+            gptr[x] = av_clip_uintp2_c(g * max, depth);
139
+            bptr[x] = av_clip_uintp2_c(b * max, depth);
140
+            rptr[x] = av_clip_uintp2_c(r * max, depth);
141
+        }
142
+
143
+        gptr += glinesize;
144
+        bptr += blinesize;
145
+        rptr += rlinesize;
146
+    }
147
+
148
+    return 0;
149
+}
150
+
151
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
152
+{
153
+    AVFilterContext *avctx = link->dst;
154
+    VibranceContext *s = avctx->priv;
155
+    int res;
156
+
157
+    if (res = avctx->internal->execute(avctx, s->do_slice, frame, NULL,
158
+                                       FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
159
+        return res;
160
+
161
+    return ff_filter_frame(avctx->outputs[0], frame);
162
+}
163
+
164
+static av_cold int query_formats(AVFilterContext *avctx)
165
+{
166
+    static const enum AVPixelFormat pixel_fmts[] = {
167
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
168
+        AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
169
+        AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
170
+        AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
171
+        AV_PIX_FMT_NONE
172
+    };
173
+
174
+    AVFilterFormats *formats = NULL;
175
+
176
+    formats = ff_make_format_list(pixel_fmts);
177
+    if (!formats)
178
+        return AVERROR(ENOMEM);
179
+
180
+    return ff_set_common_formats(avctx, formats);
181
+}
182
+
183
+static av_cold int config_input(AVFilterLink *inlink)
184
+{
185
+    AVFilterContext *avctx = inlink->dst;
186
+    VibranceContext *s = avctx->priv;
187
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
188
+
189
+    s->depth = desc->comp[0].depth;
190
+    s->do_slice = s->depth <= 8 ? vibrance_slice8 : vibrance_slice16;
191
+
192
+    return 0;
193
+}
194
+
195
+static const AVFilterPad vibrance_inputs[] = {
196
+    {
197
+        .name           = "default",
198
+        .type           = AVMEDIA_TYPE_VIDEO,
199
+        .needs_writable = 1,
200
+        .filter_frame   = filter_frame,
201
+        .config_props   = config_input,
202
+    },
203
+    { NULL }
204
+};
205
+
206
+static const AVFilterPad vibrance_outputs[] = {
207
+    {
208
+        .name = "default",
209
+        .type = AVMEDIA_TYPE_VIDEO,
210
+    },
211
+    { NULL }
212
+};
213
+
214
+#define OFFSET(x) offsetof(VibranceContext, x)
215
+#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
216
+
217
+static const AVOption vibrance_options[] = {
218
+    { "intensity", "set the intensity value",   OFFSET(intensity),  AV_OPT_TYPE_FLOAT, {.dbl=0},       -2,  2, VF },
219
+    { "rbal", "set the red balance value",      OFFSET(balance[2]), AV_OPT_TYPE_FLOAT, {.dbl=1},      -10, 10, VF },
220
+    { "gbal", "set the green balance value",    OFFSET(balance[0]), AV_OPT_TYPE_FLOAT, {.dbl=1},      -10, 10, VF },
221
+    { "bbal", "set the blue balance value",     OFFSET(balance[1]), AV_OPT_TYPE_FLOAT, {.dbl=1},      -10, 10, VF },
222
+    { "rlum", "set the red luma coefficient",   OFFSET(lcoeffs[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.072186}, 0,  1, VF },
223
+    { "glum", "set the green luma coefficient", OFFSET(lcoeffs[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.715158}, 0,  1, VF },
224
+    { "blum", "set the blue luma coefficient",  OFFSET(lcoeffs[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.212656}, 0,  1, VF },
225
+    { NULL }
226
+};
227
+
228
+AVFILTER_DEFINE_CLASS(vibrance);
229
+
230
+AVFilter ff_vf_vibrance = {
231
+    .name          = "vibrance",
232
+    .description   = NULL_IF_CONFIG_SMALL("Boost or alter saturation."),
233
+    .priv_size     = sizeof(VibranceContext),
234
+    .priv_class    = &vibrance_class,
235
+    .query_formats = query_formats,
236
+    .inputs        = vibrance_inputs,
237
+    .outputs       = vibrance_outputs,
238
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
239
+};