[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  |  184 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 208 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..3a99e01
--- /dev/null
+++ b/libavfilter/vf_negate.c
@@ -0,0 +1,184 @@
+/*
+ * 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
+    int offset[4]   ;               ///< value offset for component
+    int min_range[4], max_range[4]; ///< minimum and maximum range value for 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;
+
+    neg->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+    neg->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+    /* offsets are used for applying the equation:
+     * X' = 2 * mid_range - X = 255 + offset - X */
+    switch (inlink->format) {
+    case PIX_FMT_YUVJ444P:
+    case PIX_FMT_YUVJ422P:
+    case PIX_FMT_YUVJ420P:
+    case PIX_FMT_YUVJ440P:
+        neg->min_range[Y] = neg->min_range[A] =
+        neg->min_range[U] = neg->min_range[V] = 0;
+        neg->max_range[Y] = neg->max_range[A] =
+        neg->max_range[U] = neg->max_range[V] = 255;
+        neg->offset[Y] = neg->offset[A] =
+        neg->offset[U] = neg->offset[V] = 0;
+        break;
+    default:
+        neg->min_range[Y] = neg->min_range[A] =
+        neg->min_range[U] = neg->min_range[V] = 16;
+        neg->max_range[Y] = neg->max_range[A] = 240;
+        neg->max_range[U] = neg->max_range[V] = 235;
+        neg->offset[Y] = neg->offset[A] = -4;
+        neg->offset[U] = neg->offset[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->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 - av_clip(inrow[j], neg->min_range[plane], neg->max_range[plane]) + neg->offset[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


--zYM0uCDKw75PZbzx--



More information about the ffmpeg-devel mailing list