[FFmpeg-devel] [PATCH] libavfilter: add vf_hue.c

Stefano Sabatini stefasab at gmail.com
Sat Aug 11 12:38:25 CEST 2012


On date Saturday 2012-08-11 11:35:03 +0200, Jérémy Tran encoded:
> This is a port of the MPlayer hue filter (libmpcodecs/vf_hue.c)
> 
> Signed-off-by: Jérémy Tran <tran.jeremy.av at gmail.com>
> ---
>  doc/filters.texi         |  20 +++++
>  libavfilter/Makefile     |   1 +
>  libavfilter/allfilters.c |   1 +
>  libavfilter/vf_hue.c     | 216 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 238 insertions(+)
>  create mode 100644 libavfilter/vf_hue.c
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 18be723..71f6670 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2187,6 +2187,26 @@ a float number which specifies chroma temporal strength, defaults to
>  @var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}
>  @end table
>  
> + at section hue
> +
> +Modify the hue and/or the saturation of the input.
> +

> +The filter supports the following syntaxes:
> + at example
> +# hue = 100 ; saturation = 1
> +ffmpeg -i in.avi -vf hue=100:1
> +
> +# same command but using named options
> +ffmpeg -i in.avi -vf hue=h=100:s=1
> + at end example
> +
> + at table @option
> + at item hue
> +A float number that specifies the hue in the [-360;360] range, defaults to 0.0.
> + at item saturation
> +A float number that specifies the saturation in the [0;2] range, defaults to 1.0.
> + at end table
> +
>  @section idet
>  
>  Interlaceing detect filter. This filter tries to detect if the input is
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 727ab4e..66b5d52 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -98,6 +98,7 @@ OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
>  OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
>  OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
>  OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
> +OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
>  OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
>  OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
>  OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 403383d..f097bcf 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -88,6 +88,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER (GRADFUN,     gradfun,     vf);
>      REGISTER_FILTER (HFLIP,       hflip,       vf);
>      REGISTER_FILTER (HQDN3D,      hqdn3d,      vf);
> +    REGISTER_FILTER (HUE,         hue,         vf);
>      REGISTER_FILTER (IDET,        idet,        vf);
>      REGISTER_FILTER (LUT,         lut,         vf);
>      REGISTER_FILTER (LUTRGB,      lutrgb,      vf);
> diff --git a/libavfilter/vf_hue.c b/libavfilter/vf_hue.c
> new file mode 100644
> index 0000000..4993ae6
> --- /dev/null
> +++ b/libavfilter/vf_hue.c
> @@ -0,0 +1,216 @@
> +/*
> + * Copyright (c) 2003 Michael Niedermayer
> + * Copyright (c) 2012 Jeremy Tran
> + *
> + * 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
> + * Apply a hue/saturation filter to the input video
> + * Ported from MPlayer libmpcodecs/vf_hue.c
> + */
> +

> +#include "libavutil/common.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/mem.h"

useless?

> +#include "libavutil/opt.h"
> +#include "libavutil/pixdesc.h"
> +
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +typedef struct {
> +    const AVClass *class;
> +    float         h;
> +    float         s;
> +    int32_t       hsub;
> +    int32_t       vsub;

> +    int32_t       sin;
> +    int32_t       cos;

won't this conflict with sin/cos symbols? You could name them hue_sin
and hue_cos (which should also be more explicative).

> +} HueContext;
> +
> +#define OFFSET(x) offsetof(HueContext, x)
> +static const AVOption hue_options[] = {
> +    { "h", NULL, OFFSET(h), AV_OPT_TYPE_FLOAT, { 0 }, -360, 360, AV_OPT_FLAG_VIDEO_PARAM },
> +    { "s", NULL, OFFSET(s), AV_OPT_TYPE_FLOAT, { 1 }, 0, 2, AV_OPT_FLAG_VIDEO_PARAM },
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(hue);

Since we decided to go with multiple syntax, I ask you to move
opt-support in a separate patch, so it's easier to discriminate ported
code and new additions.

> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    HueContext *hue = ctx->priv;
> +    char *colon, *equal;
> +    float h = 0, s = 1;
> +    int32_t ret;
> +
> +    hue->class = &hue_class;
> +
> +    // named options syntax
> +    if (equal = strchr(args, '=')) {
> +        av_opt_set_defaults(hue);
> +        ret = av_set_options_string(hue, args, "=", ":");
> +        if (ret < 0) {
> +            av_log(ctx, AV_LOG_ERROR,
> +                   "Error parsing the options string: %s\n", args);
> +            av_opt_free(hue);
> +            return ret;
> +        }
> +    // compatibility syntax
> +    } else {
> +        sscanf(args, "%f:%f", &h, &s);
> +        hue->h = h;
> +        hue->s = s;

this check could be made more robust, for example in case the user
set: "foo:bar"

char c1, c2;
n = sscanf(args, "%f%c%f%c", &h, &c1, &s, &c2);
if (n != 0 && n != 1 && (n != 3 || c1 != ':') {
   invalid syntax
}

> +    }
> +

> +    if (hue->h < -360 || hue->h > 360)
> +        av_log(ctx, AV_LOG_ERROR, "Invalid value for h:%0.1f\n", h);

We could drop entirely the limitation on the range, since if I'm not
wrong sin and cos will work with any double value (periodic
functions).

> +    else if (hue->s < 0 || hue->s > 2)
> +        av_log(ctx, AV_LOG_ERROR, "Invalid value for s:%0.1f\n", s);

"Invalid value for s '%f'...: must be included in the range 0-2\n"

should be more user-friendly.

> +    else {
> +        /* Convert angle from degree to radian */
> +        hue->h *= M_PI / 180;
> +        return 0;
> +    }
> +
> +    return AVERROR(EINVAL);
> +}
> +
[...]
> +AVFilter avfilter_vf_hue = {
> +    .name        = "hue",
> +    .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video"),
> +
> +    .priv_size = sizeof(HueContext),
> +
> +    .init          = init,
> +    .query_formats = query_formats,
> +
> +    .inputs = (const AVFilterPad[]) {{ .name             = "default",
> +                                       .type             = AVMEDIA_TYPE_VIDEO,
> +                                       .draw_slice       = draw_slice,
> +                                       .config_props     = config_props,
> +                                       .min_perms        = AV_PERM_READ },
> +                                     { .name = NULL}},
> +    .outputs = (const AVFilterPad[]) {{ .name             = "default",
> +                                        .type             = AVMEDIA_TYPE_VIDEO },
> +                                      { .name = NULL}}
> +};

Nit: could be indented more nicely (check for example asetpts.

Should be good otherwise, assuming the output is the same as mp=hue.
-- 
FFmpeg = Fanciful and Fiendish Merciless Purposeless Evil Gadget


More information about the ffmpeg-devel mailing list