[FFmpeg-devel] [PATCH] lavfi: add histeq filter (WIP)

Clément Bœsch ubitux at gmail.com
Mon Oct 15 08:10:35 CEST 2012


On Mon, Oct 15, 2012 at 05:41:11AM +0200, Jérémy Tran wrote:
> This is a port of virtual dub's histogram equalization filter by
> Donald A. Graft.
> 
> I reworked stefano's previous port (that used YUV colorspace) to use RGB
> colorspace like the original filter but the output is not correct yet.
> I am posting this patch to get a fist review, maybe there's something that

fist review? mmh...

> I obviously got wrong.
> Thanks.
> ---
>  configure                |   1 +
>  doc/filters.texi         |  43 ++++++++
>  libavfilter/Makefile     |   1 +
>  libavfilter/allfilters.c |   1 +
>  libavfilter/vf_histeq.c  | 282 +++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 328 insertions(+)
>  create mode 100644 libavfilter/vf_histeq.c
> 
> diff --git a/configure b/configure
> index 5aa891b..a72987c 100755
> --- a/configure
> +++ b/configure
> @@ -1916,6 +1916,7 @@ frei0r_filter_deps="frei0r dlopen"
>  frei0r_filter_extralibs='$ldl'
>  frei0r_src_filter_deps="frei0r dlopen"
>  frei0r_src_filter_extralibs='$ldl'
> +histeq_filter_deps="gpl"
>  hqdn3d_filter_deps="gpl"
>  movie_filter_deps="avcodec avformat"
>  mp_filter_deps="gpl avcodec swscale postproc inline_asm"
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 725c7b5..6bb293e 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2285,6 +2285,49 @@ For example to horizontally flip the input video with @command{ffmpeg}:
>  ffmpeg -i in.avi -vf "hflip" out.avi
>  @end example
>  
> + at section histeq
> +From the original author's description:

I wonder if that sentence is useful

> +This filter applies a global color histogram equalization on a per-frame basis.
> +It can be used to correct video that has a compressed range of pixel intensities.
> +The filter redistributes the pixel intensities to equalize their distribution
> +across the intensity range. It may be viewed as an "automatically adjusting
> +contrast filter". This filter is useful only for correcting degraded or poorly
> +captured source video. It should not be applied willy-nilly, as it leads to a
> +modification of image content.
> +
> +The filter takes these optional parameters separated by ':':
> + at var{strength}:@var{intensity}:@var{antibanding}
> +
> + at var{strength} must be an integer and determines the amount of equalization to
> +be applied. As the strength is reduced, the distribution of pixel intensities
> +more-and-more approaches that of the input frame.
> +It defaults to 200.
> +
> + at var{intensity} must be an integer and sets the maximum intensity that can
> +generated and scales the output values appropriately. The strength should be
> +set as desired and then the intensity can be limited if needed to avoid
> +washing-out.
> +It defaults to 210.
> +
> + at var{antibanding} must be 'none', 'weak' or 'strong' and randomly varies the
> +luminance of output pixels by a small amount to avoid banding of the histogram.
> +It defaults to 'none'.
> +
> +Some examples follow:
> + at itemize
> + at item
> +Use default values
> + at example
> +histeq
> + at end example
> +
> + at item
> +Use defaults strength and intensity values and enable strong antibanding
> + at example
> +histeq=200:210:strong
> + at end example
> + at end itemize
> +
>  @section hqdn3d
>  
>  High precision/quality 3d denoise filter. This filter aims to reduce
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 3618f10..bf82266 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -106,6 +106,7 @@ OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
>  OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
>  OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
>  OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
> +OBJS-$(CONFIG_HISTEQ_FILTER)                 += vf_histeq.o
>  OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
>  OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
>  OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 348f369..2d7c95f 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -98,6 +98,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER (FREI0R,      frei0r,      vf);
>      REGISTER_FILTER (GRADFUN,     gradfun,     vf);
>      REGISTER_FILTER (HFLIP,       hflip,       vf);
> +    REGISTER_FILTER (HISTEQ,      histeq,      vf);
>      REGISTER_FILTER (HQDN3D,      hqdn3d,      vf);
>      REGISTER_FILTER (HUE,         hue,         vf);
>      REGISTER_FILTER (IDET,        idet,        vf);
> diff --git a/libavfilter/vf_histeq.c b/libavfilter/vf_histeq.c
> new file mode 100644
> index 0000000..5eeeb3b
> --- /dev/null
> +++ b/libavfilter/vf_histeq.c
> @@ -0,0 +1,282 @@
> +/*
> + * Copyright (c) 2010 Stefano Sabatini
> + * Copyright (c) 2001 Donald A. Graft
> + *

You didn't do anything?

> + * 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/common.h"
> +#include "libavutil/pixdesc.h"
> +
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "video.h"
> +
> +#define NONE   0
> +#define WEAK   1
> +#define STRONG 2
> +
> +#define R 0
> +#define G 1
> +#define B 2
> +#define A 3
> +
> +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;

These are the kind of fields I'd like to be documented :)

> +    int vsub, hsub;
> +    int rgba_map[4];               ///< components position
> +    int bpp;                       ///< bytes per pixel
> +} HisteqContext;
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    HisteqContext *histeq = ctx->priv;
> +    char antibanding_str[128] = "none";
> +
> +    histeq->strength    = 200;
> +    histeq->intensity   = 210;
> +    histeq->ia          = 4096;
> +    histeq->ic          = 150889;
> +    histeq->im          = 714025;

Any idea how these were computed?

> +
> +    if (args)
> +        sscanf(args, "%d:%d:%s", &histeq->strength, &histeq->intensity, antibanding_str);

This is not safe; add some length protection for %s. Also, why no
alternative av_set_options_string() like in vf hue and some other filters?

> +
> +    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_ARGB,         PIX_FMT_RGBA,
> +        PIX_FMT_ABGR,         PIX_FMT_BGRA,
> +        PIX_FMT_NONE
> +    };
> +
> +    ff_set_common_formats(ctx, ff_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];

av_pix_fmt_desc_get()

> +
> +    histeq->hsub = pix_desc->log2_chroma_w;
> +    histeq->vsub = pix_desc->log2_chroma_h;
> +    histeq->bpp = av_get_bits_per_pixel(pix_desc) / 8;
> +
> +    switch (inlink->format) {
> +        case PIX_FMT_ARGB:

nit: we usually align the "case" at the same level of the "switch"

> +            histeq->rgba_map[R] = 2;
> +            histeq->rgba_map[G] = 1;
> +            histeq->rgba_map[B] = 0;
> +            histeq->rgba_map[A] = 3;
> +            break;
> +        case PIX_FMT_RGBA:
> +            histeq->rgba_map[R] = 3;
> +            histeq->rgba_map[G] = 2;
> +            histeq->rgba_map[B] = 1;
> +            histeq->rgba_map[A] = 0;
> +            break;
> +        case PIX_FMT_ABGR:
> +            histeq->rgba_map[R] = 0;
> +            histeq->rgba_map[G] = 1;
> +            histeq->rgba_map[B] = 2;
> +            histeq->rgba_map[A] = 3;
> +            break;
> +        case PIX_FMT_BGRA:
> +            histeq->rgba_map[R] = 1;
> +            histeq->rgba_map[G] = 2;
> +            histeq->rgba_map[B] = 3;
> +            histeq->rgba_map[A] = 0;
> +            break;

sounds like this could be macrotized, but do as you please

[...]

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121015/3e2385e0/attachment.asc>


More information about the ffmpeg-devel mailing list