[PATCH] Add negate filter.
Stefano Sabatini
stefano.sabatini-lala
Fri Nov 26 17:31:11 CET 2010
Ported from the libavfilter-soc repo with per-component negation
addition.
---
doc/filters.texi | 22 ++++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_negate.c | 182 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 206 insertions(+), 0 deletions(-)
create mode 100644 libavfilter/vf_negate.c
diff --git a/doc/filters.texi b/doc/filters.texi
index 9583e4e..4a00a0d 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -361,6 +361,28 @@ The following command:
will make libavfilter use a format different from "yuv420p" for the
input to the vflip filter.
+ at section negate
+
+Convert one or more components of a video to its negative.
+
+This filter accepts as paramter a string containing a sequence of the
+characters 'y' (luma component), 'u' (U chroma component), 'v' (V
+chroma component), 'a' (alpha component). The components corresponding
+to the specified characters will be negated. If the string is not
+specified "yuv" is assumed.
+
+Follow some examples:
+ at example
+# only negate luma
+negate=y
+
+# negate chroma components
+negate=uv
+
+# negate all the components
+negate=yuva
+ at end example
+
@section null
Pass the video source unchanged to the output.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 792e845..481fd9a 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_NEGATE_FILTER) += vf_negate.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..0611b98 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 (NEGATE, negate, vf);
REGISTER_FILTER (NOFORMAT, noformat, vf);
REGISTER_FILTER (NULL, null, vf);
REGISTER_FILTER (OCV_SMOOTH, ocv_smooth, vf);
diff --git a/libavfilter/vf_negate.c b/libavfilter/vf_negate.c
new file mode 100644
index 0000000..0bd9ed9
--- /dev/null
+++ b/libavfilter/vf_negate.c
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2007 Bobby Bingham
+ * 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
+ * video negative filter
+ */
+
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+
+typedef struct {
+ int hsub, vsub;
+ int neg[4]; ///< tells which components to negate
+ uint8_t lut[256][4]; ///< lookup table for each component
+} NegContext;
+
+#define Y 0
+#define U 1
+#define V 2
+#define A 3
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ NegContext *neg = ctx->priv;
+ char neg_str[16] = "yuv";
+ int i;
+
+ if (args)
+ sscanf(args, "%15s", neg_str);
+
+ for (i = 0; neg_str[i]; i++) {
+ char c = neg_str[i];
+ switch (c) {
+ case 'y': neg->neg[Y] = 1; break;
+ case 'u': neg->neg[U] = 1; break;
+ case 'v': neg->neg[V] = 1; break;
+ case 'a': neg->neg[A] = 1; break;
+ default:
+ av_log(ctx, AV_LOG_ERROR, "Invalid character '%c' in arguments '%s'.\n",
+ c, args);
+ return AVERROR(EINVAL);
+ }
+ }
+
+ av_log(ctx, AV_LOG_INFO, "y:%d u:%d v:%d a:%d\n",
+ neg->neg[Y], neg->neg[U], neg->neg[V], neg->neg[A]);
+ return 0;
+}
+
+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_MONOWHITE, PIX_FMT_MONOBLACK,
+ PIX_FMT_NONE
+ };
+
+ avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+ return 0;
+}
+
+static int config_props(AVFilterLink *inlink)
+{
+ NegContext *neg = inlink->dst->priv;
+ int plane, i, min_range[4], max_range[4];
+
+ neg->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+ neg->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+ switch (inlink->format) {
+ case PIX_FMT_YUVJ444P:
+ case PIX_FMT_YUVJ422P:
+ case PIX_FMT_YUVJ420P:
+ case PIX_FMT_YUVJ440P:
+ min_range[Y] = min_range[A] =
+ min_range[U] = min_range[V] = 0;
+ max_range[Y] = max_range[A] =
+ max_range[U] = max_range[V] = 255;
+ break;
+ default:
+ min_range[Y] = min_range[A] =
+ min_range[U] = min_range[V] = 16;
+ max_range[Y] = max_range[A] = 240;
+ max_range[U] = max_range[V] = 235;
+ }
+
+ /* X' = 2 * mid_range - X = MAX + MIN - X */
+ for (plane = 0; plane < 4; plane++) {
+ int mid2 = max_range[plane] + min_range[plane];
+ for (i = 0; i < 256; i++)
+ neg->lut[plane][i] = mid2 - av_clip(i, min_range[plane], max_range[plane]);
+ }
+ return 0;
+}
+
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+{
+ NegContext *neg = inlink->dst->priv;
+ AVFilterLink *outlink = inlink->dst->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 ? neg->vsub : 0;
+ int hsub = plane == 1 || plane == 2 ? neg->hsub : 0;
+
+ inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane];
+ outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
+
+ if (neg->neg[plane]) {
+ if (inlink->format == PIX_FMT_MONOWHITE || inlink->format == PIX_FMT_MONOBLACK) {
+ for (i = 0; i < inlink->h; i++) {
+ for (j = 0; j < inlink->w >> 3; j++)
+ outrow[j] = ~inrow[j];
+ inrow += inpic ->linesize[0];
+ outrow += outpic->linesize[0];
+ }
+ } else {
+ for (i = 0; i < h>>vsub; i ++) {
+ for (j = 0; j < inlink->w>>hsub; j++)
+ outrow[j] = neg->lut[plane][inrow[j]];
+ inrow += inpic ->linesize[plane];
+ outrow += outpic->linesize[plane];
+ }
+ }
+ } else {
+ /* copy unchanged the input plane */
+ for (i = 0; i < (h>>vsub); i ++) {
+ memcpy(outrow, inrow, inpic ->linesize[plane]);
+ inrow += inpic ->linesize[plane];
+ outrow += outpic->linesize[plane];
+ }
+ }
+ }
+
+ avfilter_draw_slice(outlink, y, h, slice_dir);
+}
+
+AVFilter avfilter_vf_negate = {
+ .name = "negate",
+ .description = NULL_IF_CONFIG_SMALL("Negate the input video."),
+
+ .priv_size = sizeof(NegContext),
+
+ .init = init,
+ .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
--fUYQa+Pmc3FrFX/N--
More information about the ffmpeg-devel
mailing list