Browse code

lavfi: add histeq filter

This is a port of virtual dub's histogram equalization filter by Donald
A. Graft. Based on the work by Jérémy Tran <tran.jeremy.av@gmail.com>,
done for SOCIS 2012.

Stefano Sabatini authored on 2012/10/20 14:45:51
Showing 11 changed files
... ...
@@ -56,6 +56,7 @@ version <next>:
56 56
 - data: URI scheme
57 57
 - support building on the Plan 9 operating system
58 58
 - kerndeint filter ported from MPlayer
59
+- histeq filter ported from VirtualDub
59 60
 
60 61
 
61 62
 version 1.0:
... ...
@@ -31,6 +31,7 @@ Specifically, the GPL parts of FFmpeg are
31 31
     - vf_decimate.c
32 32
     - vf_delogo.c
33 33
     - vf_geq.c
34
+    - vf_histeq.c
34 35
     - vf_hqdn3d.c
35 36
     - vf_hue.c
36 37
     - vf_kerndeint.c
... ...
@@ -1985,6 +1985,7 @@ frei0r_filter_extralibs='$ldl'
1985 1985
 frei0r_src_filter_deps="frei0r dlopen"
1986 1986
 frei0r_src_filter_extralibs='$ldl'
1987 1987
 geq_filter_deps="gpl"
1988
+histeq_filter_deps="gpl"
1988 1989
 hqdn3d_filter_deps="gpl"
1989 1990
 hue_filter_deps="gpl"
1990 1991
 kerndeint_filter_deps="gpl"
... ...
@@ -2664,6 +2664,44 @@ For example to horizontally flip the input video with @command{ffmpeg}:
2664 2664
 ffmpeg -i in.avi -vf "hflip" out.avi
2665 2665
 @end example
2666 2666
 
2667
+@section histeq
2668
+This filter applies a global color histogram equalization on a
2669
+per-frame basis.
2670
+
2671
+It can be used to correct video that has a compressed range of pixel
2672
+intensities.  The filter redistributes the pixel intensities to
2673
+equalize their distribution across the intensity range. It may be
2674
+viewed as an "automatically adjusting contrast filter". This filter is
2675
+useful only for correcting degraded or poorly captured source
2676
+video.
2677
+
2678
+The filter accepts parameters as a list of @var{key}=@var{value}
2679
+pairs, separated by ":". If the key of the first options is omitted,
2680
+the arguments are interpreted according to syntax
2681
+@var{strength}:@var{intensity}:@var{antibanding}.
2682
+
2683
+This filter accepts the following named options:
2684
+
2685
+@table @option
2686
+@item strength
2687
+Determine the amount of equalization to be applied.  As the strength
2688
+is reduced, the distribution of pixel intensities more-and-more
2689
+approaches that of the input frame. The value must be a float number
2690
+in the range [0,1] and defaults to 0.200.
2691
+
2692
+@item intensity
2693
+Set the maximum intensity that can generated and scale the output
2694
+values appropriately.  The strength should be set as desired and then
2695
+the intensity can be limited if needed to avoid washing-out. The value
2696
+must be a float number in the range [0,1] and defaults to 0.210.
2697
+
2698
+@item antibanding
2699
+Set the antibanding level. If enabled the filter will randomly vary
2700
+the luminance of output pixels by a small amount to avoid banding of
2701
+the histogram. Possible values are @code{none}, @code{weak} or
2702
+@code{strong}. It defaults to @code{none}.
2703
+@end table
2704
+
2667 2705
 @section hqdn3d
2668 2706
 
2669 2707
 High precision/quality 3d denoise filter. This filter aims to reduce
... ...
@@ -111,6 +111,7 @@ OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
111 111
 OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
112 112
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
113 113
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
114
+OBJS-$(CONFIG_HISTEQ_FILTER)                 += vf_histeq.o
114 115
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
115 116
 OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
116 117
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
... ...
@@ -105,6 +105,7 @@ void avfilter_register_all(void)
105 105
     REGISTER_FILTER(GEQ,            geq,            vf);
106 106
     REGISTER_FILTER(GRADFUN,        gradfun,        vf);
107 107
     REGISTER_FILTER(HFLIP,          hflip,          vf);
108
+    REGISTER_FILTER(HISTEQ,         histeq,         vf);
108 109
     REGISTER_FILTER(HQDN3D,         hqdn3d,         vf);
109 110
     REGISTER_FILTER(HUE,            hue,            vf);
110 111
     REGISTER_FILTER(IDET,           idet,           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  31
32
+#define LIBAVFILTER_VERSION_MINOR  32
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,298 @@
0
+/*
1
+ * Copyright (c) 2012 Jeremy Tran
2
+ * Copyright (c) 2001 Donald A. Graft
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
+ * Histogram equalization filter, based on the VirtualDub filter by
24
+ * Donald A. Graft  <neuron2 AT home DOT com>.
25
+ * Implements global automatic contrast adjustment by means of
26
+ * histogram equalization.
27
+ */
28
+
29
+#include "libavutil/common.h"
30
+#include "libavutil/opt.h"
31
+#include "libavutil/pixdesc.h"
32
+
33
+#include "avfilter.h"
34
+#include "drawutils.h"
35
+#include "formats.h"
36
+#include "internal.h"
37
+#include "video.h"
38
+
39
+// #define DEBUG
40
+
41
+// Linear Congruential Generator, see "Numerical Recipes"
42
+#define LCG_A 4096
43
+#define LCG_C 150889
44
+#define LCG_M 714025
45
+#define LCG(x) (((x) * LCG_A + LCG_C) % LCG_M)
46
+#define LCG_SEED 739187
47
+
48
+enum HisteqAntibanding {
49
+    HISTEQ_ANTIBANDING_NONE   = 0,
50
+    HISTEQ_ANTIBANDING_WEAK   = 1,
51
+    HISTEQ_ANTIBANDING_STRONG = 2,
52
+    HISTEQ_ANTIBANDING_NB,
53
+};
54
+
55
+typedef struct {
56
+    const AVClass *class;
57
+    float strength;
58
+    float intensity;
59
+    enum HisteqAntibanding antibanding;
60
+    char* antibanding_str;
61
+    int in_histogram [256];        ///< input histogram
62
+    int out_histogram[256];        ///< output histogram
63
+    int LUT[256];                  ///< lookup table derived from histogram[]
64
+    uint8_t rgba_map[4];           ///< components position
65
+    int bpp;                       ///< bytes per pixel
66
+} HisteqContext;
67
+
68
+#define OFFSET(x) offsetof(HisteqContext, x)
69
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
70
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
71
+
72
+static const AVOption histeq_options[] = {
73
+    { "strength",    "set the strength", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 1, FLAGS },
74
+    { "intensity",   "set the intensity", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0.21}, 0, 1, FLAGS },
75
+    { "antibanding", "set the antibanding level", OFFSET(antibanding), AV_OPT_TYPE_INT, {.i64=HISTEQ_ANTIBANDING_NONE}, 0, HISTEQ_ANTIBANDING_NB-1, FLAGS, "antibanding" },
76
+    CONST("none",    "apply no antibanding",     HISTEQ_ANTIBANDING_NONE,   "antibanding"),
77
+    CONST("weak",    "apply weak antibanding",   HISTEQ_ANTIBANDING_WEAK,   "antibanding"),
78
+    CONST("strong",  "apply strong antibanding", HISTEQ_ANTIBANDING_STRONG, "antibanding"),
79
+    { NULL }
80
+};
81
+
82
+AVFILTER_DEFINE_CLASS(histeq);
83
+
84
+static av_cold int init(AVFilterContext *ctx, const char *args)
85
+{
86
+    HisteqContext *histeq = ctx->priv;
87
+    const char *shorthand[] = { "strength", "intensity", "antibanding", NULL };
88
+    int ret;
89
+
90
+    histeq->class = &histeq_class;
91
+    av_opt_set_defaults(histeq);
92
+
93
+    if ((ret = av_opt_set_from_string(histeq, args, shorthand, "=", ":")) < 0)
94
+        return ret;
95
+
96
+    av_log(ctx, AV_LOG_VERBOSE,
97
+           "strength:%0.3f intensity:%0.3f antibanding:%d\n",
98
+           histeq->strength, histeq->intensity, histeq->antibanding);
99
+
100
+    return 0;
101
+}
102
+
103
+static av_cold void uninit(AVFilterContext *ctx)
104
+{
105
+    HisteqContext *histeq = ctx->priv;
106
+    av_opt_free(histeq);
107
+}
108
+
109
+static int query_formats(AVFilterContext *ctx)
110
+{
111
+    static const enum PixelFormat pix_fmts[] = {
112
+        AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
113
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
114
+        AV_PIX_FMT_NONE
115
+    };
116
+
117
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
118
+    return 0;
119
+}
120
+
121
+static int config_input(AVFilterLink *inlink)
122
+{
123
+    AVFilterContext *ctx = inlink->dst;
124
+    HisteqContext *histeq = ctx->priv;
125
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
126
+
127
+    histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
128
+    ff_fill_rgba_map(histeq->rgba_map, inlink->format);
129
+
130
+    return 0;
131
+}
132
+
133
+#define R 0
134
+#define G 1
135
+#define B 2
136
+#define A 3
137
+
138
+#define GET_RGB_VALUES(r, g, b, src, map) do { \
139
+    r = src[x + map[R]];                       \
140
+    g = src[x + map[G]];                       \
141
+    b = src[x + map[B]];                       \
142
+} while (0)
143
+
144
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
145
+{
146
+    AVFilterContext   *ctx     = inlink->dst;
147
+    HisteqContext     *histeq  = ctx->priv;
148
+    AVFilterLink      *outlink = ctx->outputs[0];
149
+    int strength  = histeq->strength  * 1000;
150
+    int intensity = histeq->intensity * 1000;
151
+    int x, y, i, luthi, lutlo, lut, luma, oluma, m;
152
+    AVFilterBufferRef *outpic;
153
+    unsigned int r, g, b, jran;
154
+    uint8_t *src, *dst;
155
+
156
+    outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
157
+    if (!outpic) {
158
+        avfilter_unref_bufferp(&inpic);
159
+        return AVERROR(ENOMEM);
160
+    }
161
+    avfilter_copy_buffer_ref_props(outpic, inpic);
162
+
163
+    /* Seed random generator for antibanding. */
164
+    jran = LCG_SEED;
165
+
166
+    /* Calculate and store the luminance and calculate the global histogram
167
+       based on the luminance. */
168
+    memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
169
+    src = inpic->data[0];
170
+    dst = outpic->data[0];
171
+    for (y = 0; y < inlink->h; y++) {
172
+        for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
173
+            GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
174
+            luma = (55 * r + 182 * g + 19 * b) >> 8;
175
+            dst[x + histeq->rgba_map[A]] = luma;
176
+            histeq->in_histogram[luma]++;
177
+        }
178
+        src += inpic->linesize[0];
179
+        dst += outpic->linesize[0];
180
+    }
181
+
182
+#ifdef DEBUG
183
+    for (x = 0; x < 256; x++)
184
+        av_dlog(ctx, "in[%d]: %u\n", x, histeq->in_histogram[x]);
185
+#endif
186
+
187
+    /* Calculate the lookup table. */
188
+    histeq->LUT[0] = histeq->in_histogram[0];
189
+    /* Accumulate */
190
+    for (x = 1; x < 256; x++)
191
+        histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
192
+
193
+    /* Normalize */
194
+    for (x = 0; x < 256; x++)
195
+        histeq->LUT[x] = (histeq->LUT[x] * intensity) / (inlink->h * inlink->w);
196
+
197
+    /* Adjust the LUT based on the selected strength. This is an alpha
198
+       mix of the calculated LUT and a linear LUT with gain 1. */
199
+    for (x = 0; x < 256; x++)
200
+        histeq->LUT[x] = (strength * histeq->LUT[x]) / 255 +
201
+                         ((255 - strength) * x)      / 255;
202
+
203
+    /* Output the equalized frame. */
204
+    memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
205
+
206
+    src = inpic->data[0];
207
+    dst = outpic->data[0];
208
+    for (y = 0; y < inlink->h; y++) {
209
+        for (x = 0; x < inlink->w * histeq->bpp; x += histeq->bpp) {
210
+            luma = dst[x + histeq->rgba_map[A]];
211
+            if (luma == 0) {
212
+                for (i = 0; i < histeq->bpp; ++i)
213
+                    dst[x + i] = 0;
214
+                histeq->out_histogram[0]++;
215
+            } else {
216
+                lut = histeq->LUT[luma];
217
+                if (histeq->antibanding != HISTEQ_ANTIBANDING_NONE) {
218
+                    if (luma > 0) {
219
+                        lutlo = histeq->antibanding == HISTEQ_ANTIBANDING_WEAK ?
220
+                                (histeq->LUT[luma] + histeq->LUT[luma - 1]) / 2 :
221
+                                 histeq->LUT[luma - 1];
222
+                    } else
223
+                        lutlo = lut;
224
+
225
+                    if (luma < 255) {
226
+                        luthi = (histeq->antibanding == HISTEQ_ANTIBANDING_WEAK) ?
227
+                            (histeq->LUT[luma] + histeq->LUT[luma + 1]) / 2 :
228
+                             histeq->LUT[luma + 1];
229
+                    } else
230
+                        luthi = lut;
231
+
232
+                    if (lutlo != luthi) {
233
+                        jran = LCG(jran);
234
+                        lut = lutlo + ((luthi - lutlo + 1) * jran) / LCG_M;
235
+                    }
236
+                }
237
+
238
+                GET_RGB_VALUES(r, g, b, src, histeq->rgba_map);
239
+                if (((m = FFMAX3(r, g, b)) * lut) / luma > 255) {
240
+                    r = (r * 255) / m;
241
+                    g = (g * 255) / m;
242
+                    b = (b * 255) / m;
243
+                } else {
244
+                    r = (r * lut) / luma;
245
+                    g = (g * lut) / luma;
246
+                    b = (b * lut) / luma;
247
+                }
248
+                dst[x + histeq->rgba_map[R]] = r;
249
+                dst[x + histeq->rgba_map[G]] = g;
250
+                dst[x + histeq->rgba_map[B]] = b;
251
+                oluma = (55 * r + 182 * g + 19 * b) >> 8;
252
+                histeq->out_histogram[oluma]++;
253
+            }
254
+        }
255
+        src += inpic->linesize[0];
256
+        dst += outpic->linesize[0];
257
+    }
258
+#ifdef DEBUG
259
+    for (x = 0; x < 256; x++)
260
+        av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
261
+#endif
262
+
263
+    avfilter_unref_bufferp(&inpic);
264
+    return ff_filter_frame(outlink, outpic);
265
+}
266
+
267
+static const AVFilterPad histeq_inputs[] = {
268
+    {
269
+        .name             = "default",
270
+        .type             = AVMEDIA_TYPE_VIDEO,
271
+        .config_props     = config_input,
272
+        .filter_frame     = filter_frame,
273
+        .min_perms        = AV_PERM_READ,
274
+    },
275
+    { NULL }
276
+};
277
+
278
+static const AVFilterPad histeq_outputs[] = {
279
+    {
280
+        .name             = "default",
281
+        .type             = AVMEDIA_TYPE_VIDEO,
282
+    },
283
+    { NULL }
284
+};
285
+
286
+AVFilter avfilter_vf_histeq = {
287
+    .name          = "histeq",
288
+    .description   = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
289
+    .priv_size     = sizeof(HisteqContext),
290
+    .init          = init,
291
+    .uninit        = uninit,
292
+    .query_formats = query_formats,
293
+
294
+    .inputs        = histeq_inputs,
295
+    .outputs       = histeq_outputs,
296
+    .priv_class    = &histeq_class,
297
+};
... ...
@@ -41,6 +41,7 @@ FATE_LAVFI = fate-lavfi-alphaextract_rgb                                \
41 41
 
42 42
 FATE_LAVFI-$(CONFIG_GPL) += fate-lavfi-colormatrix1                     \
43 43
                             fate-lavfi-colormatrix2                     \
44
+                            fate-lavfi-histeq                           \
44 45
                             fate-lavfi-hue                              \
45 46
                             fate-lavfi-pixfmts_super2xsai               \
46 47
                             fate-lavfi-tinterlace_merge                 \
... ...
@@ -105,6 +105,7 @@ do_lavfi_pixfmts(){
105 105
 
106 106
 # all these filters have exactly one input and exactly one output
107 107
 do_lavfi_pixfmts "field"               "field"   "bottom"
108
+do_lavfi_pixfmts "histeq"              "histeq"  "antibanding=strong"
108 109
 do_lavfi_pixfmts "kerndeint"           "kerndeint" "" "tinterlace=interleave_top,"
109 110
 do_lavfi_pixfmts "pixfmts_copy"        "copy"    ""
110 111
 do_lavfi_pixfmts "pixfmts_crop"        "crop"    "100:100:100:100"
111 112
new file mode 100644
... ...
@@ -0,0 +1,6 @@
0
+abgr                a538e1221c94a12fb4e60b47b5358f67
1
+argb                d0ef008d603d67a6a7d698d2a8f53d6a
2
+bgr24               9ef3c69a658490c4fbc807272372e73a
3
+bgra                716e70fdf7413d9a3b83e0365c2b0a99
4
+rgb24               8423322bbc66bc5050f6b93fdab23433
5
+rgba                a960c9423bbb3925c3511362348b38e2