[FFmpeg-devel] [RFC] [PATCH] histogram filter

Clément Bœsch ubitux at gmail.com
Wed Dec 26 16:47:09 CET 2012


On Wed, Dec 26, 2012 at 01:29:29PM +0000, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  libavfilter/Makefile       |   1 +
>  libavfilter/allfilters.c   |   1 +
>  libavfilter/vf_histogram.c | 276 +++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 278 insertions(+)
>  create mode 100644 libavfilter/vf_histogram.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 7595e4c..2b4cde8 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -109,6 +109,7 @@ OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
>  OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
>  OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
>  OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
> +OBJS-$(CONFIG_HISTOGRAM_FILTER)              += vf_histogram.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 88b0979..94b1aa1 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -100,6 +100,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER (GEQ,         geq,         vf);
>      REGISTER_FILTER (GRADFUN,     gradfun,     vf);
>      REGISTER_FILTER (HFLIP,       hflip,       vf);
> +    REGISTER_FILTER (HISTOGRAM,   histogram,   vf);
>      REGISTER_FILTER (HQDN3D,      hqdn3d,      vf);
>      REGISTER_FILTER (HUE,         hue,         vf);
>      REGISTER_FILTER (IDET,        idet,        vf);
> diff --git a/libavfilter/vf_histogram.c b/libavfilter/vf_histogram.c
> new file mode 100644
> index 0000000..a7e4781
> --- /dev/null
> +++ b/libavfilter/vf_histogram.c
> @@ -0,0 +1,276 @@
> +/*
> + * Copyright (c) 2012 Paul B Mahol
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser 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
> + */
> +
> +#include "libavutil/opt.h"
> +#include "libavutil/parseutils.h"
> +#include "libavutil/pixdesc.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "drawutils.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +typedef struct {
> +    const AVClass *class;               ///< AVClass context for log and options purpose
> +    unsigned       histogram[1 << 16];
> +    unsigned       histogram_size;
> +    unsigned       max_hval;
> +    int            comp_yuv[4];
> +    int            comp_rgb[4];
> +    int            ncomp;
> +    int            do_graph;
> +    int            w, h;
> +    AVFilterBufferRef *outpicref;   ///< output picture reference, updated regularly
> +    FFDrawContext   draw;
> +    char           *fg_color_str, *bg_color_str;
> +    uint8_t         fg_color_rgba[4], bg_color_rgba[4];
> +} HistogramContext;
> +
> +static int config_input(AVFilterLink *link)
> +{
> +    HistogramContext *h = link->dst->priv;
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
> +
> +    h->histogram_size = 1 << desc->comp[0].depth_minus1 + 1;
> +    h->ncomp          = desc->nb_components;
> +
> +    return 0;
> +}
> +
> +static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
> +{
> +    HistogramContext *h = link->dst->priv;
> +    AVFilterContext *ctx = link->dst;
> +    const uint8_t *src;
> +    int i, j, k;
> +
> +    switch (in->format) {
> +    case AV_PIX_FMT_RGB24:
> +    case AV_PIX_FMT_RGBA:
> +        src = in->data[0];
> +        for (i = 0; i < in->video->h; i++) {
> +            for (j = 0; j < in->linesize[0]; j += h->ncomp) {

shouldn't the check be j < in->video->w * h->ncomp instead?

> +                unsigned v = 0, used = 0;
> +
> +                for (k = 0; k < h->ncomp; k++) {
> +                    if (h->comp_rgb[k]) {
> +                        v += src[j + k];
> +                        used++;
> +                    }
> +                }
> +
> +                if (used)
> +                    h->histogram[v / used]++;
> +            }
> +            src += in->linesize[0];
> +        }
> +        break;
> +    case AV_PIX_FMT_YUVA444P:
> +    case AV_PIX_FMT_YUV444P:
> +    case AV_PIX_FMT_YUVJ444P:
> +    case AV_PIX_FMT_GRAY8:
> +    case AV_PIX_FMT_GRAY8A:
> +        for (i = 0; i < in->video->h; i++) {
> +            for (j = 0; j < in->linesize[0]; j++) {

j < in->video->w?

> +                unsigned v = 0, used = 0;
> +
> +                for (k = 0; k < h->ncomp; k++) {
> +                    if (h->comp_yuv[k]) {
> +                        src = in->data[k] + i * in->linesize[k];
> +                        v += src[j];
> +                        used++;
> +                    }
> +                }
> +
> +                if (used)
> +                    h->histogram[v / used]++;
> +            }
> +        }
> +        break;
> +    }
> +
> +    for (i = 0; i < h->histogram_size; i++) {
> +        h->max_hval = FFMAX(h->max_hval, h->histogram[i]);
> +        if (h->histogram[i])
> +            av_log(ctx, h->do_graph ? AV_LOG_VERBOSE : AV_LOG_INFO, "i:%d c:%d\n", i, h->histogram[i]);
> +    }
> +
> +    if (h->do_graph) {
> +        AVFilterBufferRef *out = h->outpicref;
> +        FFDrawColor bg, fg;
> +        int ret, col_width = h->w / h->histogram_size;
> +
> +        out->pts = in->pts;
> +        out->pos = in->pos;
> +
> +        ff_draw_color(&h->draw, &bg, h->bg_color_rgba);
> +        ff_draw_color(&h->draw, &fg, h->fg_color_rgba);
> +        ff_fill_rectangle(&h->draw, &bg, out->data, out->linesize, 0, 0, h->w, h->h);
> +        for (i = 0; i < h->histogram_size; i++) {
> +            int col_height = (float) h->histogram[i] / h->max_hval * h->h;
> +
> +            ff_fill_rectangle(&h->draw, &fg, out->data, out->linesize,
> +                              i * col_width,
> +                              h->h - col_height,
> +                              col_width,
> +                              col_height);
> +        }
> +
> +        ret = ff_filter_frame(ctx->outputs[0], avfilter_ref_buffer(out, ~AV_PERM_WRITE));
> +        if (ret < 0)
> +            return ret;
> +    } else {
> +        ff_filter_frame(link->dst->outputs[0], in);
> +    }
> +    memset(h->histogram, 0, h->histogram_size * sizeof(*h->histogram));
> +    h->max_hval = 0;

> +    avfilter_unref_buffer(in);

Is this really supposed to happen if do_graph=0?

> +
> +    return 0;
> +}
> +
> +#define Y 0
> +#define U 1
> +#define V 2
> +#define R 0
> +#define G 1
> +#define B 2
> +#define A 3
> +
> +#define OFFSET(x) offsetof(HistogramContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption histogram_options[] = {
> +    { "graph", "show histogram graph", OFFSET(do_graph), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
> +    { "size", "set histogram graph video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "256x200"}, 0, 0, FLAGS },
> +    { "fg", "set histogram foreground color", OFFSET(fg_color_str), AV_OPT_TYPE_STRING, {.str = "white"}, CHAR_MIN, CHAR_MAX, FLAGS },
> +    { "bg", "set histogram background color", OFFSET(bg_color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
> +    { "y", "use Y component", OFFSET(comp_yuv[Y]), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
> +    { "u", "use U component", OFFSET(comp_yuv[U]), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
> +    { "v", "use V component", OFFSET(comp_yuv[V]), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
> +    { "a", "use A component", OFFSET(comp_yuv[A]), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
> +    { "r", "use R component", OFFSET(comp_rgb[R]), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
> +    { "g", "use G component", OFFSET(comp_rgb[G]), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
> +    { "b", "use B component", OFFSET(comp_rgb[B]), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
> +    { NULL },
> +};
> +
> +AVFILTER_DEFINE_CLASS(histogram);
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    HistogramContext *h = ctx->priv;
> +
> +    if (!h->do_graph)
> +        return 0;
> +
> +    outlink->w = h->w;
> +    outlink->h = h->h;
> +
> +    avfilter_unref_bufferp(&h->outpicref);
> +    h->outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE2,
> +                            outlink->w, outlink->h);

style: align

> +    if (!h->outpicref)
> +        return AVERROR(ENOMEM);
> +    outlink->sample_aspect_ratio = (AVRational){1,1};
> +
> +    ff_draw_init(&h->draw, outlink->format, 0);
> +
> +    return 0;
> +}
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    HistogramContext *h = ctx->priv;
> +    int ret;
> +
> +    h->class = &histogram_class;
> +    av_opt_set_defaults(h);
> +
> +    if ((ret = (av_set_options_string(h, args, "=", ":"))) < 0)
> +        return ret;
> +
> +    if ((ret = av_parse_color(h->fg_color_rgba, h->fg_color_str, -1, ctx)) < 0 ||
> +        (ret = av_parse_color(h->bg_color_rgba, h->bg_color_str, -1, ctx)) < 0 )
> +        return ret;
> +
> +    h->comp_rgb[A] = h->comp_yuv[A];
> +    if (!h->comp_rgb[R] && !h->comp_rgb[G] && !h->comp_rgb[B] && !h->comp_rgb[A] &&
> +        !h->comp_yuv[Y] && !h->comp_yuv[U] && !h->comp_yuv[V] && !h->comp_yuv[A])
> +        return AVERROR(EINVAL);
> +
> +    return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> +    HistogramContext *h = ctx->priv;
> +
> +    avfilter_unref_bufferp(&h->outpicref);
> +    av_opt_free(h);
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    static const enum AVPixelFormat pix_fmts[] = {
> +        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
> +        AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
> +        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
> +        AV_PIX_FMT_NONE
> +    };
> +
> +    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +
> +    return 0;
> +}
> +
> +static const AVFilterPad histogram_inputs[] = {
> +    {
> +        .name             = "default",
> +        .type             = AVMEDIA_TYPE_VIDEO,

> +        .get_video_buffer = ff_null_get_video_buffer,

Is this required?

> +        .filter_frame     = filter_frame,
> +        .config_props     = config_input,
> +        .min_perms        = AV_PERM_READ,
> +    },
> +    { NULL }
> +};
> +
> +static const AVFilterPad histogram_outputs[] = {
> +    {
> +        .name         = "default",
> +        .type         = AVMEDIA_TYPE_VIDEO,
> +        .config_props = config_output,
> +    },
> +    { NULL }
> +};
> +
> +AVFilter avfilter_vf_histogram = {
> +    .name          = "histogram",
> +    .description   = NULL_IF_CONFIG_SMALL("Histogram."),

"Compute and draw an histogram."?

> +    .priv_size     = sizeof(HistogramContext),
> +    .init          = init,
> +    .uninit        = uninit,
> +    .query_formats = query_formats,
> +    .inputs        = histogram_inputs,
> +    .outputs       = histogram_outputs,
> +    .priv_class    = &histogram_class,
> +};

-- 
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/20121226/f433225b/attachment.asc>


More information about the ffmpeg-devel mailing list