[FFmpeg-devel] [PATCH 1/2] colorbalance filter
Stefano Sabatini
stefasab at gmail.com
Tue Apr 16 23:48:25 CEST 2013
On date Tuesday 2013-04-16 12:23:30 +0000, Paul B Mahol encoded:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> doc/filters.texi | 27 +++++
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/vf_colorbalance.c | 222 ++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 251 insertions(+)
> create mode 100644 libavfilter/vf_colorbalance.c
>
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 5c2a54a..87811df 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2051,6 +2051,33 @@ boxblur=luma_radius=min(h\,w)/10:luma_power=1:chroma_radius=min(cw\,ch)/10:chrom
> @end example
> @end itemize
>
> + at section colorbalance
> +Modify intensity of primary colors (red, green and blue) of input image.
> +
> +The filter allows an image to be adjusted in the shadows, midtones or highlights
> +regions for the red-cyan, green-magenta or blue-yellow balance.
> +
> +The filter accepts the following options:
> +
> + at table @option
> + at item rs
> + at item gs
> + at item bs
> +Adjust red, green and blue shadows (darkest pixels).
> +
> + at item rm
> + at item gm
> + at item bm
> +Adjust red, green and blue midtones (medium pixels).
> +
> + at item rh
> + at item gh
> + at item bh
> +Adjust red, green and blue highlights (brightest pixels).
> +
> +Allowed range is from -100 to 100, default is 0.
A range [-1, +1] seems more mathematically appealing (also specify
this is a float number).
Examples are welcome.
> + at end table
> +
> @section colormatrix
>
> Convert color matrix.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 949972d..0927986 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -104,6 +104,7 @@ OBJS-$(CONFIG_BLACKDETECT_FILTER) += vf_blackdetect.o
> OBJS-$(CONFIG_BLACKFRAME_FILTER) += vf_blackframe.o
> OBJS-$(CONFIG_BLEND_FILTER) += vf_blend.o
> OBJS-$(CONFIG_BOXBLUR_FILTER) += vf_boxblur.o
> +OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o
> OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o
> OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o
> OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 95bd270..bebbf2b 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -102,6 +102,7 @@ void avfilter_register_all(void)
> REGISTER_FILTER(BLACKFRAME, blackframe, vf);
> REGISTER_FILTER(BLEND, blend, vf);
> REGISTER_FILTER(BOXBLUR, boxblur, vf);
> + REGISTER_FILTER(COLORBALANCE, colorbalance, vf);
> REGISTER_FILTER(COLORMATRIX, colormatrix, vf);
> REGISTER_FILTER(COPY, copy, vf);
> REGISTER_FILTER(CROP, crop, vf);
> diff --git a/libavfilter/vf_colorbalance.c b/libavfilter/vf_colorbalance.c
> new file mode 100644
> index 0000000..e9ecab6
> --- /dev/null
> +++ b/libavfilter/vf_colorbalance.c
> @@ -0,0 +1,222 @@
> +/*
> + * Copyright (c) 2013 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/pixdesc.h"
> +#include "avfilter.h"
> +#include "drawutils.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +typedef struct {
> + double shadows;
> + double midtones;
> + double highlights;
> +} Range;
> +
> +typedef struct {
> + const AVClass *class;
> + Range cyan_red;
> + Range magenta_green;
> + Range yellow_blue;
> +
> + uint8_t lookup[3][256];
> +
> + uint8_t rgba_map[4];
> + int nb_components;
> +} ColorBalanceContext;
> +
> +#define OFFSET(x) offsetof(ColorBalanceContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +static const AVOption colorbalance_options[] = {
> + { "rs", "set red shadows", OFFSET(cyan_red.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "gs", "set green shadows", OFFSET(magenta_green.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "bs", "set blue shadows", OFFSET(yellow_blue.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "rm", "set red midtones", OFFSET(cyan_red.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "gm", "set green midtones", OFFSET(magenta_green.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "bm", "set blue midtones", OFFSET(yellow_blue.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "rh", "set red hightlights", OFFSET(cyan_red.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "gh", "set green hightlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { "bh", "set blue hightlights", OFFSET(yellow_blue.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
> + { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(colorbalance);
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + static const enum AVPixelFormat pix_fmts[] = {
> + AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
> + AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
> + AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB,
> + AV_PIX_FMT_0BGR, AV_PIX_FMT_0RGB,
> + AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
> + AV_PIX_FMT_NONE
> + };
> +
> + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> + return 0;
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> + AVFilterContext *ctx = outlink->src;
> + ColorBalanceContext *cb = ctx->priv;
> + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
> + double *add[3], *sub[3];
> + double *coeff[3][3];
> + double *buffer;
> + int i, r, g, b;
> +
> + buffer = av_malloc(256 * 3 * 2 * sizeof(double));
> + if (!buffer)
> + return AVERROR(ENOMEM);
> +
> + for (i = 0; i < 3; i++) {
> + add[i] = buffer + 256 * i;
> + sub[i] = buffer + 256 * (3 + i);
> + }
> +
> + for (i = 0; i < 256; i++) {
> + double low = (1.075 - 1 / (i / 16.0 + 1));
> + double mid = 2.0 / 3.0 * (1 - ((i - 127.0) / 127.0) * ((i - 127.0) / 127.0));
> +
> + add[0][i] = low;
> + add[1][i] = mid;
> + add[2][255 - i] = low;
> + sub[0][255 - i] = low;
> + sub[1][i] = mid;
> + sub[2][i] = low;
> + }
> +
short explanation or link to paper is welcome
> + coeff[0][0] = cb->cyan_red.shadows > 0 ? add[0] : sub[0];
> + coeff[1][0] = cb->magenta_green.shadows > 0 ? add[0] : sub[0];
> + coeff[2][0] = cb->yellow_blue.shadows > 0 ? add[0] : sub[0];
> + coeff[0][1] = cb->cyan_red.midtones > 0 ? add[1] : sub[1];
> + coeff[1][1] = cb->magenta_green.midtones > 0 ? add[1] : sub[1];
> + coeff[2][1] = cb->yellow_blue.midtones > 0 ? add[1] : sub[1];
> + coeff[0][2] = cb->cyan_red.highlights > 0 ? add[2] : sub[2];
> + coeff[1][2] = cb->magenta_green.highlights > 0 ? add[2] : sub[2];
> + coeff[2][2] = cb->yellow_blue.highlights > 0 ? add[2] : sub[2];
nit: vertical align
> +
> + for (i = 0; i < 256; i++) {
> + r = g = b = i;
> +
> + r = av_clip_uint8(r + cb->cyan_red.shadows * coeff[0][0][r]);
> + r = av_clip_uint8(r + cb->cyan_red.midtones * coeff[0][1][r]);
> + r = av_clip_uint8(r + cb->cyan_red.highlights * coeff[0][2][r]);
> +
> + g = av_clip_uint8(g + cb->magenta_green.shadows * coeff[1][0][g]);
> + g = av_clip_uint8(g + cb->magenta_green.midtones * coeff[1][1][g]);
> + g = av_clip_uint8(g + cb->magenta_green.highlights * coeff[1][2][g]);
> +
> + b = av_clip_uint8(b + cb->yellow_blue.shadows * coeff[2][0][b]);
> + b = av_clip_uint8(b + cb->yellow_blue.midtones * coeff[2][1][b]);
> + b = av_clip_uint8(b + cb->yellow_blue.highlights * coeff[2][2][b]);
nit: vertical align
> +
> + cb->lookup[0][i] = r;
> + cb->lookup[1][i] = g;
> + cb->lookup[2][i] = b;
> + }
> +
> + av_free(buffer);
> +
> + ff_fill_rgba_map(cb->rgba_map, outlink->format);
> + cb->nb_components = av_get_padded_bits_per_pixel(desc) >> 3;
pixdesc->nb_components, the formula is wrong (even if it produces the
pixdesc->expected result)
[...]
--
FFmpeg = Forgiving and Formidable Multimedia Perennial Elastic Gladiator
More information about the ffmpeg-devel
mailing list