[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  |  171 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 195 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_negate.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 1cba2d6..95b235e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -360,6 +360,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 210510f..4379e9a 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 c3067b8..3291bd1 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..bfd4120
--- /dev/null
+++ b/libavfilter/vf_negate.c
@@ -0,0 +1,171 @@
+/*
+ * 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 plane_neg[4]; ///< tells which components to negate
+    int plane_off[4]; ///< plane value offset
+} 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 plane_neg_str[16] = "yuv";
+    int i;
+
+    if (args)
+        sscanf(args, "%15s", plane_neg_str);
+
+    for (i = 0; plane_neg_str[i]; i++) {
+        char c = plane_neg_str[i];
+        switch (c) {
+        case 'y': neg->plane_neg[Y] = 1; break;
+        case 'u': neg->plane_neg[U] = 1; break;
+        case 'v': neg->plane_neg[V] = 1; break;
+        case 'a': neg->plane_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->plane_neg[Y], neg->plane_neg[U], neg->plane_neg[V], neg->plane_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;
+
+    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:
+        neg->plane_off[Y] =
+        neg->plane_off[U] = neg->plane_off[V] = 0;
+        break;
+    default:
+        neg->plane_off[Y] = neg->plane_off[A] = -4;
+        neg->plane_off[U] = neg->plane_off[V] = 1;
+    }
+
+    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->plane_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] = 255 - inrow[j] + neg->plane_off[plane];
+                    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


--/9DWx/yDrRhgMJTb--



More information about the ffmpeg-devel mailing list