[FFmpeg-devel] [PATCH] Add negate filter.

Stefano Sabatini stefano.sabatini-lala
Fri Nov 26 17:32:13 CET 2010


Ported from the libavfilter-soc repo with some cosmetical change.
---
 doc/filters.texi         |    4 ++
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_negate.c  |  135 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_negate.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 1cba2d6..037ad78 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -360,6 +360,10 @@ The following command:
 will make libavfilter use a format different from "yuv420p" for the
 input to the vflip filter.
 
+ at section negate
+
+Convert a video to its negative.
+
 @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 9e3ba14..1a776ca 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..d3c388e
--- /dev/null
+++ b/libavfilter/vf_negate.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2007 Bobby Bingham
+ *
+ * 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 off_y, off_uv;
+    int hsub, vsub;
+} NegContext;
+
+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_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->off_y  =
+        neg->off_uv = 0;
+        break;
+    default:
+        neg->off_y  = -4;
+        neg->off_uv = 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;
+
+    if (inlink->format == PIX_FMT_MONOWHITE || inlink->format == PIX_FMT_MONOBLACK) {
+        inrow  = inpic ->data[0] + y * inpic ->linesize[0];
+        outrow = outpic->data[0] + y * outpic->linesize[0];
+        for (i = 0; i < h; i++) {
+            for (j = 0; j < inlink->w >> 3; j++)
+                outrow[j] = ~inrow[j];
+            inrow  += inpic ->linesize[0];
+            outrow += outpic->linesize[0];
+        }
+    } else {
+        /* luma plane */
+        inrow  = inpic ->data[0] + y * inpic-> linesize[0];
+        outrow = outpic->data[0] + y * outpic->linesize[0];
+        for (i = 0; i < h; i ++) {
+            for(j = 0; j < inlink->w; j++)
+                outrow[j] = 255 - inrow[j] + neg->off_y;
+            inrow  += inpic ->linesize[0];
+            outrow += outpic->linesize[0];
+        }
+
+        /* chroma planes */
+        for (plane = 1; plane < 3; plane++) {
+            inrow  = inpic-> data[plane] + (y >> neg->vsub) * inpic ->linesize[plane];
+            outrow = outpic->data[plane] + (y >> neg->vsub) * outpic->linesize[plane];
+
+            for (i = 0; i < h >> neg->vsub; i++) {
+                for (j = 0; j < inlink->w >> neg->hsub; j++)
+                    outrow[j] = 255 - inrow[j] + neg->off_uv;
+                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),
+
+    .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




More information about the ffmpeg-devel mailing list