[FFmpeg-devel] [PATCH] Implement histogram equalization filter.

Stefano Sabatini stefano.sabatini-lala
Sat Nov 20 16:44:01 CET 2010


---
 configure                |    1 +
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_histeq.c  |  217 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 220 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_histeq.c

diff --git a/configure b/configure
index 7dcb50f..4cca86c 100755
--- a/configure
+++ b/configure
@@ -1406,6 +1406,7 @@ udp_protocol_deps="network"
 blackframe_filter_deps="gpl"
 cropdetect_filter_deps="gpl"
 frei0r_filter_deps="frei0r dlopen strtok_r"
+histeq_filter_deps="gpl"
 ocv_smooth_filter_deps="libopencv"
 yadif_filter_deps="gpl"
 
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 71dcf2a..3499b0f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -28,6 +28,7 @@ OBJS-$(CONFIG_FIFO_FILTER)                   += vf_fifo.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
+OBJS-$(CONFIG_HISTEQ_FILTER)                 += vf_histeq.o
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index d30e95a..c56319b 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -49,6 +49,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (FORMAT,      format,      vf);
     REGISTER_FILTER (FREI0R,      frei0r,      vf);
     REGISTER_FILTER (HFLIP,       hflip,       vf);
+    REGISTER_FILTER (HISTEQ,      histeq,      vf);
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
     REGISTER_FILTER (NULL,        null,        vf);
     REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
diff --git a/libavfilter/vf_histeq.c b/libavfilter/vf_histeq.c
new file mode 100644
index 0000000..8578262
--- /dev/null
+++ b/libavfilter/vf_histeq.c
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2010 Stefano Sabatini
+ * Copyright (c) 2001 Donald A. Graft
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+/**
+ * @file
+ * Histogram equalization filter, based on the VirtualDub filter by
+ * Donald A. Graft  <neuron2 AT home DOT com>.
+ * Implements global automatic contrast adjustment by means of
+ * histogram equalization.
+ */
+
+/* #define DEBUG */
+
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+
+#define NONE   0
+#define WEAK   1
+#define STRONG 2
+
+typedef struct {
+    int strength;
+    int intensity;
+    int antibanding;
+    int in_histogram [256];	///< input histogram
+    int out_histogram[256];	///< output histogram
+    int LUT[256];		///< lookup table derived from histogram[]
+    unsigned int jran, ia, ic, im;
+    int vsub, hsub;
+} HisteqContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    HisteqContext *histeq = ctx->priv;
+    char antibanding_str[128] = "none";
+
+    histeq->strength    = 200;
+    histeq->intensity   = 210;
+    histeq->ia          = 4096;
+    histeq->ic          = 150889;
+    histeq->im          = 714025;
+
+    if (args)
+        sscanf(args, "%d:%d:%s", &histeq->strength, &histeq->intensity, antibanding_str);
+
+    if      (!strcmp(antibanding_str, "none"  )) histeq->antibanding = NONE;
+    else if (!strcmp(antibanding_str, "weak"  )) histeq->antibanding = WEAK;
+    else if (!strcmp(antibanding_str, "strong")) histeq->antibanding = STRONG;
+    else {
+        av_log(ctx, AV_LOG_ERROR, "Unknown value for antibanding '%s'.\n", antibanding_str);
+        return AVERROR(EINVAL);
+    }
+
+    av_log(ctx, AV_LOG_INFO, "strength:%d intensity:%d antibanding:%s\n",
+           histeq->strength, histeq->intensity, antibanding_str);
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum PixelFormat pix_fmts[] = {
+        PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
+        PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
+        PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
+        PIX_FMT_YUVA420P,
+        PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    HisteqContext *histeq = ctx->priv;
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
+
+    histeq->hsub = pix_desc->log2_chroma_w;
+    histeq->vsub = pix_desc->log2_chroma_h;
+
+    return 0;
+}
+
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    HisteqContext *histeq = ctx->priv;
+    AVFilterBufferRef *picref = inlink->cur_buf;
+    AVFilterLink *outlink = ctx->outputs[0];
+    int x, y, luthi, lutlo, lut, luma;
+    uint8_t *src;
+
+    /* Seed random generator for antibanding. */
+    histeq->jran = 739187UL;
+
+    /* Calculate and store the luminance and calculate the global histogram
+       based on the luminance. */
+    memset(histeq->in_histogram, 0, sizeof(histeq->in_histogram));
+    src = picref->data[0];
+    for (y = 0; y < inlink->h; y++) {
+        for (x = 0; x < inlink->w; x++)
+            histeq->in_histogram[src[x]]++;
+        src += picref->linesize[0];
+    }
+#ifdef DEBUG
+    for (x = 0; x < 256; x++)
+        av_log(ctx, AV_LOG_DEBUG, "in[%d]: %u\n", x, histeq->in_histogram[x]);
+#endif
+
+    /* Calculate the lookup table. */
+    histeq->LUT[0] = histeq->in_histogram[0];
+    /* Accumulate */
+    for (x = 1; x < 256; x++)
+        histeq->LUT[x] = histeq->LUT[x-1] + histeq->in_histogram[x];
+
+    /* Normalize. */
+    for (x = 0; x < 256; x++)
+        histeq->LUT[x] = (histeq->LUT[x] * histeq->intensity) / (inlink->h * inlink->w);
+
+    /* Adjust the LUT based on the selected strength. This is an alpha
+       mix of the calculated LUT and a linear LUT with gain 1. */
+    for (x = 0; x < 256; x++)
+        histeq->LUT[x] = (histeq->strength * histeq->LUT[x]) / 255 +
+                         ((255 - histeq->strength) * x)      / 255;
+
+    /* Output the equalized frame. */
+    memset(histeq->out_histogram, 0, sizeof(histeq->out_histogram));
+
+    src = picref->data[0];
+    for (y = 0; y < inlink->h; y++) {
+        for (x = 0; x < inlink->w; x++) {
+            luma = src[x];
+            if (luma == 0) {
+                src[x] = 0;
+                histeq->out_histogram[0]++;
+            } else {
+                lut = histeq->LUT[luma];
+                if (histeq->antibanding != NONE) {
+                    if (luma > 0) {
+                        lutlo = histeq->antibanding == WEAK ?
+                                (histeq->LUT[luma] + histeq->LUT[luma-1]) / 2 :
+                                 histeq->LUT[luma-1];
+                    } else
+                        lutlo = lut;
+
+                    if (luma < 255) {
+                        luthi = (histeq->antibanding == WEAK) ?
+                            (histeq->LUT[luma] + histeq->LUT[luma+1]) / 2 :
+                             histeq->LUT[luma+1];
+                    } else
+                        luthi = lut;
+
+                    if (lutlo != luthi) {
+                        histeq->jran = (histeq->jran * histeq->ia + histeq->ic) % histeq->im;
+                        lut = lutlo + ((luthi - lutlo + 1) * histeq->jran) / histeq->im;
+                    }
+                }
+                src[x] = src[x] * lut / luma;
+                histeq->out_histogram[src[x]]++;
+            }
+        }
+        src += picref->linesize[0];
+    }
+#ifdef DEBUG
+    for (x = 0; x < 256; x++)
+        av_log(ctx, AV_LOG_DEBUG, "out[%d]: %u\n", x, histeq->out_histogram[x]);
+#endif
+
+    avfilter_draw_slice(outlink, 0, picref->video->h, 1);
+    avfilter_end_frame(outlink);
+}
+
+AVFilter avfilter_vf_histeq = {
+    .name        = "histeq",
+    .description = NULL_IF_CONFIG_SMALL("Perform global color histogram equalization."),
+
+    .priv_size = sizeof(HisteqContext),
+    .init      = init,
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .config_props     = config_input,
+                                    .get_video_buffer = avfilter_null_get_video_buffer,
+                                    .start_frame      = avfilter_null_start_frame,
+                                    .draw_slice       = null_draw_slice,
+                                    .end_frame        = end_frame,
+                                    .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
+                                    .rej_perms        = AV_PERM_PRESERVE },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
-- 
1.7.1




More information about the ffmpeg-devel mailing list