[PATCH] Implement lut filter.
Stefano Sabatini
stefano.sabatini-lala
Fri Dec 3 12:45:29 CET 2010
---
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_lut.c | 252 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 254 insertions(+), 0 deletions(-)
create mode 100644 libavfilter/vf_lut.c
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 792e845..7693e6f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -27,6 +27,7 @@ OBJS-$(CONFIG_FIFO_FILTER) += vf_fifo.o
OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o
OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o
OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
+OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o
OBJS-$(CONFIG_NOFORMAT_FILTER) += vf_format.o
OBJS-$(CONFIG_NULL_FILTER) += vf_null.o
OBJS-$(CONFIG_OCV_SMOOTH_FILTER) += vf_libopencv.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 4ecb00e..5ec4a23 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -48,6 +48,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (FORMAT, format, vf);
REGISTER_FILTER (FREI0R, frei0r, vf);
REGISTER_FILTER (HFLIP, hflip, vf);
+ REGISTER_FILTER (LUT, lut, vf);
REGISTER_FILTER (NOFORMAT, noformat, vf);
REGISTER_FILTER (NULL, null, vf);
REGISTER_FILTER (OCV_SMOOTH, ocv_smooth, vf);
diff --git a/libavfilter/vf_lut.c b/libavfilter/vf_lut.c
new file mode 100644
index 0000000..d8071cc
--- /dev/null
+++ b/libavfilter/vf_lut.c
@@ -0,0 +1,252 @@
+/*
+ * Copyright (c) 2010 Stefano Sabatini
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * Compute a look-up table for binding the input value to the output
+ * value, and apply it to input video.
+ */
+
+#define DEBUG
+
+#include "libavutil/eval.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+
+static const char *var_names[] = {
+ "E",
+ "PHI",
+ "PI",
+ "W", ///< width of the input video
+ "H", ///< height of the input video
+ "v", ///< input value for the pixel
+ "maxv", ///< input value for the pixel
+ "minv", ///< input value for the pixel
+ NULL
+};
+
+enum var_name {
+ VAR_E,
+ VAR_PHI,
+ VAR_PI,
+ VAR_W,
+ VAR_H,
+ VAR_v,
+ VAR_maxv,
+ VAR_minv,
+ VAR_VARS_NB
+};
+
+typedef struct {
+ const AVClass *class;
+ uint8_t lut[4][256]; ///< lookup table for each component
+ char *comp_expr_str[4];
+ AVExpr *comp_expr[4];
+ int hsub, vsub;
+ double var_values[VAR_VARS_NB];
+} LutContext;
+
+#define Y 0
+#define U 1
+#define V 2
+#define A 3
+
+#define OFFSET(x) offsetof(LutContext, x)
+
+static const AVOption lut_options[] = {
+{"y", "set Y expression", OFFSET(comp_expr_str[Y]), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX},
+{"u", "set U expression", OFFSET(comp_expr_str[U]), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX},
+{"v", "set V expression", OFFSET(comp_expr_str[V]), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX},
+{"a", "set A expression", OFFSET(comp_expr_str[A]), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX},
+{NULL},
+};
+
+static const char *lut_get_name(void *ctx)
+{
+ return "lut";
+}
+
+static const AVClass lut_class = {
+ "LutContext",
+ lut_get_name,
+ lut_options
+};
+
+static int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ LutContext *lut = ctx->priv;
+ int i, ret;
+
+ lut->class = &lut_class;
+ av_opt_set_defaults2(lut, 0, 0);
+ for (i = 0; i < 4; i++)
+ lut->comp_expr_str[i] = av_strdup("v");
+
+ lut->var_values[VAR_PHI] = M_PHI;
+ lut->var_values[VAR_PI] = M_PI;
+ lut->var_values[VAR_E ] = M_E;
+
+ if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
+ return ret;
+
+ return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ LutContext *lut = ctx->priv;
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ av_expr_free(lut->comp_expr[i]);
+ lut->comp_expr[i] = NULL;
+ av_freep(&lut->comp_expr_str[i]);
+ }
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ enum PixelFormat pix_fmts[] = {
+ PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
+ PIX_FMT_YUV411P, PIX_FMT_YUV410P,
+ PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
+ PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
+ PIX_FMT_YUVA420P,
+ PIX_FMT_NONE
+ };
+
+ avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+ return 0;
+}
+
+static int config_props(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ LutContext *lut = ctx->priv;
+ int min_range[4], max_range[4];
+ int v, comp, ret;
+
+ lut->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+ lut->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+ lut->var_values[VAR_W] = inlink->w;
+ lut->var_values[VAR_H] = inlink->h;
+
+ min_range[0] = min_range[1] =
+ min_range[2] = min_range[3] = 0;
+ max_range[0] = max_range[1] =
+ max_range[2] = max_range[3] = 255;
+
+ switch (inlink->format) {
+ case PIX_FMT_YUV444P:
+ case PIX_FMT_YUV422P:
+ case PIX_FMT_YUV420P:
+ case PIX_FMT_YUV440P:
+ min_range[Y] =
+ min_range[U] = min_range[V] = 16;
+ max_range[Y] = 240;
+ max_range[U] = max_range[V] = 235;
+ break;
+ }
+
+ /* create the parsed expressions */
+ for (comp = 0; comp < 4; comp++) {
+ ret = av_expr_parse(&lut->comp_expr[comp], lut->comp_expr_str[comp],
+ var_names, NULL, NULL, NULL, NULL, 0, ctx);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Error when parsing the expression '%s' for then component %d.\n",
+ lut->comp_expr_str[comp], comp);
+ return AVERROR(EINVAL);
+ }
+ }
+
+ /* compute the lut */
+ for (comp = 0; comp < 4; comp++) {
+ double res;
+ for (v = 0; v < 255; v++) {
+ lut->var_values[VAR_v] = v;
+ lut->var_values[VAR_maxv] = max_range[comp];
+ lut->var_values[VAR_minv] = min_range[comp];
+ res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
+ if (isnan(res)) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
+ lut->comp_expr_str[comp], v, comp);
+ return AVERROR(EINVAL);
+ }
+ lut->lut[comp][v] = av_clip((int)res, min_range[comp], max_range[comp]);
+#ifdef DEBUG
+ av_log(ctx, AV_LOG_DEBUG, "v[%d][%d] = %d\n", comp, v, lut->lut[comp][v]);
+#endif
+ }
+ }
+
+ return 0;
+}
+
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+{
+ AVFilterContext *ctx = inlink->dst;
+ LutContext *lut = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ AVFilterBufferRef *inpic = inlink ->cur_buf;
+ AVFilterBufferRef *outpic = outlink->out_buf;
+ uint8_t *inrow, *outrow;
+ int i, j, plane;
+
+ for (plane = 0; inpic->data[plane]; plane++) {
+ int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
+ int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
+
+ inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane];
+ outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
+
+ for (i = 0; i < h>>vsub; i ++) {
+ for (j = 0; j < inlink->w>>hsub; j++)
+ outrow[j] = lut->lut[plane][inrow[j]];
+ inrow += inpic ->linesize[plane];
+ outrow += outpic->linesize[plane];
+ }
+ }
+
+ avfilter_draw_slice(outlink, y, h, slice_dir);
+}
+
+AVFilter avfilter_vf_lut = {
+ .name = "lut",
+ .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table to the input video."),
+ .priv_size = sizeof(LutContext),
+
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+
+ .inputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .draw_slice = draw_slice,
+ .config_props = config_props,
+ .min_perms = AV_PERM_READ, },
+ { .name = NULL}},
+ .outputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO, },
+ { .name = NULL}},
+};
--
1.7.1
--bg08WKrSYDhXBjb5--
More information about the ffmpeg-devel
mailing list