[FFmpeg-devel] [PATCH] colorbalance filter
Paul B Mahol
onemda at gmail.com
Mon Apr 15 13:02:34 CEST 2013
Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
doc/filters.texi | 25 ++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_colorbalance.c | 282 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 309 insertions(+)
create mode 100644 libavfilter/vf_colorbalance.c
diff --git a/doc/filters.texi b/doc/filters.texi
index b766985..566745e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2043,6 +2043,31 @@ 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 color balance of input video.
+
+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: complementary colors: cyan for red, magenta for green and yellow for blue to 100: red, green and blue.
+ 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..cd7839c
--- /dev/null
+++ b/libavfilter/vf_colorbalance.c
@@ -0,0 +1,282 @@
+/*
+ * 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/avassert.h"
+#include "avfilter.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 lookup8[3][256];
+ uint16_t lookup16[3][256 * 256];
+} 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", "red shadows", OFFSET(cyan_red.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "gs", "green shadows", OFFSET(magenta_green.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "bs", "blue shadows", OFFSET(yellow_blue.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "rm", "red midtones", OFFSET(cyan_red.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "gm", "green midtones", OFFSET(magenta_green.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "bm", "blue midtones", OFFSET(yellow_blue.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "rh", "red hightlights", OFFSET(cyan_red.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "gh", "green hightlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -100, 100, FLAGS },
+ { "bh", "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_RGB48, 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;
+ AVFilterLink *inlink = ctx->inputs[0];
+ double *add[3], *sub[3];
+ double *coeff[3][3];
+ double *buffer;
+ int i, r, g, b;
+
+ switch (inlink->format) {
+ case AV_PIX_FMT_RGB24:
+ 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;
+ }
+
+ coeff[0][0] = cb->cyan_red.shadows > 0 ? add[0] : sub[0];
+ coeff[0][1] = cb->cyan_red.midtones > 0 ? add[1] : sub[1];
+ coeff[0][2] = cb->cyan_red.highlights > 0 ? add[2] : sub[2];
+ coeff[1][0] = cb->magenta_green.shadows > 0 ? add[0] : sub[0];
+ coeff[1][1] = cb->magenta_green.midtones > 0 ? add[1] : sub[1];
+ coeff[1][2] = cb->magenta_green.highlights > 0 ? add[2] : sub[2];
+ coeff[2][0] = cb->yellow_blue.shadows > 0 ? add[0] : sub[0];
+ coeff[2][1] = cb->yellow_blue.midtones > 0 ? add[1] : sub[1];
+ coeff[2][2] = cb->yellow_blue.highlights > 0 ? add[2] : sub[2];
+
+ for (i = 0; i < 256; i++) {
+ r = g = b = i;
+
+ r += cb->cyan_red.shadows * coeff[0][0][r];
+ r = av_clip_uint8(r);
+ r += cb->cyan_red.midtones * coeff[0][1][r];
+ r = av_clip_uint8(r);
+ r += cb->cyan_red.highlights * coeff[0][2][r];
+ r = av_clip_uint8(r);
+
+ g += cb->magenta_green.shadows * coeff[1][0][g];
+ g = av_clip_uint8(g);
+ g += cb->magenta_green.midtones * coeff[1][1][g];
+ g = av_clip_uint8(g);
+ g += cb->magenta_green.highlights * coeff[1][2][g];
+ g = av_clip_uint8(g);
+
+ b += cb->yellow_blue.shadows * coeff[2][0][b];
+ b = av_clip_uint8(b);
+ b += cb->yellow_blue.midtones * coeff[2][1][b];
+ b = av_clip_uint8(b);
+ b += cb->yellow_blue.highlights * coeff[2][2][b];
+ b = av_clip_uint8(b);
+
+ cb->lookup8[0][i] = r;
+ cb->lookup8[1][i] = g;
+ cb->lookup8[2][i] = b;
+ }
+ break;
+ case AV_PIX_FMT_RGB48:
+ buffer = av_malloc(256 * 256 * 3 * 2 * sizeof(double));
+ if (!buffer)
+ return AVERROR(ENOMEM);
+
+ for (i = 0; i < 3; i++) {
+ add[i] = buffer + 256 * 256 * i;
+ sub[i] = buffer + 256 * 256 * (3 + i);
+ }
+
+ for (i = 0; i < 256 * 256; i++) {
+ double low = (100.075 - 100 / (i / 256.0 + 100));
+ double mid = 2.0 / 3.0 * (100 - ((i - 32767.0) / 32767.0) * ((i - 32767.0) / 32767.0));
+
+ add[0][i] = low;
+ add[1][i] = mid;
+ add[2][65535 - i] = low;
+ sub[0][65535 - i] = low;
+ sub[1][i] = mid;
+ sub[2][i] = low;
+ }
+
+ coeff[0][0] = cb->cyan_red.shadows > 0 ? add[0] : sub[0];
+ coeff[0][1] = cb->cyan_red.midtones > 0 ? add[1] : sub[1];
+ coeff[0][2] = cb->cyan_red.highlights > 0 ? add[2] : sub[2];
+ coeff[1][0] = cb->magenta_green.shadows > 0 ? add[0] : sub[0];
+ coeff[1][1] = cb->magenta_green.midtones > 0 ? add[1] : sub[1];
+ coeff[1][2] = cb->magenta_green.highlights > 0 ? add[2] : sub[2];
+ coeff[2][0] = cb->yellow_blue.shadows > 0 ? add[0] : sub[0];
+ coeff[2][1] = cb->yellow_blue.midtones > 0 ? add[1] : sub[1];
+ coeff[2][2] = cb->yellow_blue.highlights > 0 ? add[2] : sub[2];
+
+ for (i = 0; i < 256 * 256; i++) {
+ r = g = b = i;
+
+ r += cb->cyan_red.shadows * coeff[0][0][r];
+ r = av_clip_uint16(r);
+ r += cb->cyan_red.midtones * coeff[0][1][r];
+ r = av_clip_uint16(r);
+ r += cb->cyan_red.highlights * coeff[0][2][r];
+ r = av_clip_uint16(r);
+
+ g += cb->magenta_green.shadows * coeff[1][0][g];
+ g = av_clip_uint16(g);
+ g += cb->magenta_green.midtones * coeff[1][1][g];
+ g = av_clip_uint16(g);
+ g += cb->magenta_green.highlights * coeff[1][2][g];
+ g = av_clip_uint16(g);
+
+ b += cb->yellow_blue.shadows * coeff[2][0][b];
+ b = av_clip_uint16(b);
+ b += cb->yellow_blue.midtones * coeff[2][1][b];
+ b = av_clip_uint16(b);
+ b += cb->yellow_blue.highlights * coeff[2][2][b];
+ b = av_clip_uint16(b);
+
+ cb->lookup16[0][i] = r;
+ cb->lookup16[1][i] = g;
+ cb->lookup16[2][i] = b;
+ }
+ break;
+ default:
+ av_assert0(0);
+ }
+
+ av_free(buffer);
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
+{
+ AVFilterContext *ctx = inlink->dst;
+ ColorBalanceContext *cb = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ uint8_t *row = buf->data[0];
+ int i, j;
+
+ switch (outlink->format) {
+ case AV_PIX_FMT_RGB24:
+ for (i = 0; i < outlink->h; i++) {
+ uint8_t *ptr = row;
+
+ for (j = 0; j < outlink->w * 3; j += 3) {
+ ptr[j + 0] = cb->lookup8[0][ptr[j + 0]];
+ ptr[j + 1] = cb->lookup8[1][ptr[j + 1]];
+ ptr[j + 2] = cb->lookup8[2][ptr[j + 2]];
+ }
+
+ row += buf->linesize[0];
+ }
+ break;
+ case AV_PIX_FMT_RGB48:
+ for (i = 0; i < outlink->h; i++) {
+ uint16_t *ptr = (uint16_t *)row;
+
+ for (j = 0; j < outlink->w * 3; j += 3) {
+ ptr[j + 0] = cb->lookup16[0][ptr[j + 0]];
+ ptr[j + 1] = cb->lookup16[1][ptr[j + 1]];
+ ptr[j + 2] = cb->lookup16[2][ptr[j + 2]];
+ }
+
+ row += buf->linesize[0];
+ }
+ break;
+ default:
+ av_assert0(0);
+ }
+
+ return ff_filter_frame(ctx->outputs[0], buf);
+}
+
+static const AVFilterPad colorbalance_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ .needs_writable = 1,
+ },
+ { NULL }
+};
+
+static const AVFilterPad colorbalance_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = config_output,
+ },
+ { NULL }
+};
+
+AVFilter avfilter_vf_colorbalance = {
+ .name = "colorbalance",
+ .description = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
+ .priv_size = sizeof(ColorBalanceContext),
+ .query_formats = query_formats,
+ .inputs = colorbalance_inputs,
+ .outputs = colorbalance_outputs,
+ .priv_class = &colorbalance_class,
+};
--
1.7.11.2
More information about the ffmpeg-devel
mailing list