[FFmpeg-devel] [PATCH] lavfi: add setfield filter

Stefano Sabatini stefasab at gmail.com
Mon Feb 6 12:27:26 CET 2012


---
 doc/filters.texi          |   22 +++++++++++
 libavfilter/Makefile      |    1 +
 libavfilter/allfilters.c  |    1 +
 libavfilter/vf_setfield.c |   90 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 114 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_setfield.c

diff --git a/doc/filters.texi b/doc/filters.texi
index d83f561..40cfaa5 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2385,6 +2385,28 @@ To change the sample aspect ratio to 10:11, specify:
 setsar=10:11
 @end example
 
+ at section setfield
+
+Force field for the output video frame.
+
+The @code{setfield} marks the interlace type field for the output
+frames. It does not change the input frame, but only sets the
+corresponding property, which affects how the frame is treated by
+followig filters (e.g. the @code{fieldorder} or @code{yadif}).
+
+It accepts a parameter representing an integer, which can assume the
+following values:
+ at table @samp
+ at item -1, auto
+Keep the same field property.
+
+ at item 0, bff
+Mark the frame as bottom-field-first.
+
+ at item 1, tff
+Mark the frame as top-field-first.
+ at end table
+
 @section setpts
 
 Change the PTS (presentation timestamp) of the input video frames.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 9fbb59b..9f5fdcb 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -79,6 +79,7 @@ OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SELECT_FILTER)                 += vf_select.o
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
+OBJS-$(CONFIG_SETFIELD_FILTER)               += vf_setfield.o
 OBJS-$(CONFIG_SETPTS_FILTER)                 += vf_setpts.o
 OBJS-$(CONFIG_SETSAR_FILTER)                 += vf_aspect.o
 OBJS-$(CONFIG_SETTB_FILTER)                  += vf_settb.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 8286d4d..487738a 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -87,6 +87,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SELECT,      select,      vf);
     REGISTER_FILTER (SETDAR,      setdar,      vf);
+    REGISTER_FILTER (SETFIELD,    setfield,    vf);
     REGISTER_FILTER (SETPTS,      setpts,      vf);
     REGISTER_FILTER (SETSAR,      setsar,      vf);
     REGISTER_FILTER (SETTB,       settb,       vf);
diff --git a/libavfilter/vf_setfield.c b/libavfilter/vf_setfield.c
new file mode 100644
index 0000000..8d75f70
--- /dev/null
+++ b/libavfilter/vf_setfield.c
@@ -0,0 +1,90 @@
+/*
+ * 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
+ * set field order
+ */
+
+#include "avfilter.h"
+
+typedef struct {
+    int top_field_first;
+} SetFieldContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    SetFieldContext *setfield = ctx->priv;
+
+    setfield->top_field_first = -1;
+
+    if (args) {
+        char c;
+        if (sscanf(args, "%d%c", &setfield->top_field_first, &c) != 1) {
+            if      (!strcmp("tff",  args)) setfield->top_field_first = 1;
+            else if (!strcmp("bff",  args)) setfield->top_field_first = 0;
+            else if (!strcmp("auto", args)) setfield->top_field_first = -1;
+            else {
+                av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'\n", args);
+                return AVERROR(EINVAL);
+            }
+        }
+    }
+
+    if (setfield->top_field_first < -1 || setfield->top_field_first > 1) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Provided integer value %d must be included between -1 and +1\n",
+               setfield->top_field_first);
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+{
+    SetFieldContext *setfield = inlink->dst->priv;
+    AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
+
+    if (setfield->top_field_first != -1) {
+        outpicref->video->interlaced = 1;
+        outpicref->video->top_field_first = setfield->top_field_first;
+    }
+    avfilter_start_frame(inlink->dst->outputs[0], outpicref);
+}
+
+AVFilter avfilter_vf_setfield = {
+    .name      = "setfield",
+    .description = NULL_IF_CONFIG_SMALL("Force field for the output video frame."),
+    .init      = init,
+
+    .priv_size = sizeof(SetFieldContext),
+
+    .inputs = (const AVFilterPad[]) {
+        { .name             = "default",
+          .type             = AVMEDIA_TYPE_VIDEO,
+          .get_video_buffer = avfilter_null_get_video_buffer,
+          .start_frame      = start_frame, },
+        { .name = NULL }
+    },
+    .outputs = (const AVFilterPad[]) {
+        { .name             = "default",
+          .type             = AVMEDIA_TYPE_VIDEO, },
+        { .name = NULL }
+    },
+};
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list