[FFmpeg-devel] [PATCH] Port tinterlace filter from MPlayer.

Stefano Sabatini stefano.sabatini-lala
Sun Sep 26 19:17:02 CEST 2010


---
 configure                   |    1 +
 doc/filters.texi            |   26 +++++
 libavfilter/Makefile        |    1 +
 libavfilter/allfilters.c    |    1 +
 libavfilter/avfilter.c      |    2 +-
 libavfilter/vf_tinterlace.c |  260 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 290 insertions(+), 1 deletions(-)
 create mode 100644 libavfilter/vf_tinterlace.c

diff --git a/configure b/configure
index 0538201..d3e2c14 100755
--- a/configure
+++ b/configure
@@ -1402,6 +1402,7 @@ udp_protocol_deps="network"
 
 # filters
 ocv_smooth_filter_deps="libopencv"
+tinterlace_filter_deps="gpl"
 yadif_filter_deps="gpl"
 
 # libraries
diff --git a/doc/filters.texi b/doc/filters.texi
index d000276..3397989 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -390,6 +390,32 @@ not specified it will use the default value of 16.
 Adding this in the beginning of filter chains should make filtering
 faster due to better use of the memory cache.
 
+ at section tinterlace
+
+Perform temporal field interlacing - merge pairs of frames into an
+interlaced frame, halving the framerate.  Even frames are moved into
+the upper field, odd frames to the lower field.
+
+The frames are counted starting from 1, so the first input frame is
+considered odd.
+
+Accepts a parameters specifying the mode. Available modes are:
+
+0  Move odd frames into the upper field of the new frame, even into the
+   lower field, generating a double height frame at half framerate.
+
+1  Only output odd  frames, even frames are dropped; height
+   unchanged, half framerate
+
+2  Only output even frames, odd  frames are dropped; height
+   unchanged, half framerate
+
+3  Expand each frame to full height, but pad alternate lines with
+   black; framerate unchanged and double height.
+
+4  Interleave even lines from even frames with odd lines from odd
+   frames. Height unchanged at half framerate.
+
 @section unsharp
 
 Sharpen or blur the input video.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 174b83e..431fe33 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -34,6 +34,7 @@ OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SLICIFY_FILTER)                += vf_slicify.o
+OBJS-$(CONFIG_TINTERLACE_FILTER)             += vf_tinterlace.o
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
 OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index ec7d933..5224fde 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -54,6 +54,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SLICIFY,     slicify,     vf);
+    REGISTER_FILTER (TINTERLACE,  tinterlace,  vf);
     REGISTER_FILTER (UNSHARP,     unsharp,     vf);
     REGISTER_FILTER (VFLIP,       vflip,       vf);
     REGISTER_FILTER (YADIF,       yadif,       vf);
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 221dfaa..72566e5 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -19,7 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/* #define DEBUG */
+#define DEBUG
 
 #include "libavcodec/audioconvert.c"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_tinterlace.c b/libavfilter/vf_tinterlace.c
new file mode 100644
index 0000000..771c6be
--- /dev/null
+++ b/libavfilter/vf_tinterlace.c
@@ -0,0 +1,260 @@
+/*
+ * Copyright (C) 2010 Stefano Sabatini
+ * Copyright (C) 2003 Michael Zucchi <notzed at ximian.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+#include "libavcore/imgutils.h"
+#include "avfilter.h"
+
+typedef struct {
+    /**
+     * 0: Move odd frames into the upper field of the new image, even into the lower field, generating a double-height video at half framerate.
+     * 1: Only output odd  frames, even frames are dropped; height unchanged, half framerate
+     * 2: Only output even frames, odd  frames are dropped; height unchanged, half framerate
+     * 3: Expand each frame to double height, but pad alternate lines with black; framerate unchanged.
+     * 4: Interleave even lines from even frames with odd lines from odd frames. Height unchanged at half framerate.
+     */
+    int mode;
+
+    int vsub;                   ///< chroma vertical subsampling
+    int frame;                  ///< frame number
+    int frame_pending;          ///< tell if there is a pending frame ready to be sent
+    AVFilterBufferRef *picref;  ///< previously stored picref
+} TInterlaceContext;
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum PixelFormat pix_fmts[] = {
+        PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV410P,
+        PIX_FMT_YUV444P, PIX_FMT_GRAY8, PIX_FMT_NONE
+    };
+
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
+
+    return 0;
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    TInterlaceContext *tinterlace = ctx->priv;
+    tinterlace->mode = 0;
+    tinterlace->picref = NULL;
+    tinterlace->frame = 1;
+    tinterlace->frame_pending = 0;
+
+    if (args) sscanf(args, "%d", &tinterlace->mode);
+
+    if (tinterlace->mode < 0 || tinterlace->mode > 4) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Unknown mode %d, use a value between 0 and 4\n", tinterlace->mode);
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    TInterlaceContext *tinterlace = ctx->priv;
+
+    if (tinterlace->picref) avfilter_unref_buffer(tinterlace->picref);
+}
+
+static int config_out_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = outlink->src->inputs[0];
+    TInterlaceContext *tinterlace = ctx->priv;
+
+    tinterlace->vsub = av_pix_fmt_descriptors[outlink->format].log2_chroma_h;
+    outlink->w = inlink->w;
+    outlink->h = (tinterlace->mode == 0 || tinterlace->mode == 3) ? inlink->h*2 : inlink->h;
+
+    av_log(ctx, AV_LOG_INFO, "mode:%d in_h:%d out_h:%d\n",
+           tinterlace->mode, inlink->h, outlink->h);
+
+    return 0;
+}
+
+static void copy_picture_fields(uint8_t *dst[4], int dst_linesize[4],
+                                const uint8_t *src[4], int src_linesize[4],
+                                enum PixelFormat format, int w, int
+                                src_h, int lower_fields,
+                                int only_half_src_fields, int src_lower_fields)
+{
+    int i, k = 1, vsub = av_pix_fmt_descriptors[format].log2_chroma_h;
+    if (only_half_src_fields) {
+        src_h /= 2;
+        k = 2;
+    }
+
+    for (i = 0; i < 3; i++) {
+        int h = i == 1 || i == 2 ? src_h >> vsub : src_h;
+        int linesize = av_image_get_linesize(format, w, i);
+
+        if (lower_fields)
+            dst[i] += dst_linesize[i];
+        if (only_half_src_fields && src_lower_fields)
+            src[i] += src_linesize[i];
+        av_image_copy_plane(dst[i], dst_linesize[i]*2,
+                            src[i], src_linesize[i]*k, linesize, h);
+    }
+}
+
+static void end_frame(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    TInterlaceContext *tinterlace = ctx->priv;
+    AVFilterBufferRef *outpicref;
+    AVFilterBufferRef *picref = inlink->cur_buf;
+
+    if (tinterlace->mode == 0) {
+        if (!tinterlace->picref) {
+            outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+            avfilter_copy_buffer_ref_props(outpicref, picref);
+            outpicref->video->w = outlink->w;
+            outpicref->video->h = outlink->h;
+            outpicref->video->interlaced = 1;
+            outpicref->video->top_field_first = 1;
+
+            /* write odd frame fields into the upper fields of the new frame */
+            copy_picture_fields(outpicref->data, outpicref->linesize,
+                                picref   ->data, picref   ->linesize,
+                                outlink->format, inlink->w, inlink->h, 0, 0, 0);
+            tinterlace->picref = outpicref;
+        } else {
+            outpicref = tinterlace->picref;
+
+            /* write even frame fields into the lower fields of the new frame */
+            copy_picture_fields(outpicref->data, outpicref->linesize,
+                                picref   ->data, picref   ->linesize,
+                                outlink->format, inlink->w, inlink->h, 1, 0, 0);
+            tinterlace->frame_pending = 1;
+        }
+    } else if ((tinterlace->mode == 1 &&   tinterlace->frame & 1) ||  /* send only odd  frames */
+               (tinterlace->mode == 2 && !(tinterlace->frame & 1))) { /* send only even frames */
+        tinterlace->picref = avfilter_ref_buffer(picref, AV_PERM_READ);
+        tinterlace->frame_pending = 1;
+    } else if (tinterlace->mode == 3) {
+        outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        avfilter_copy_buffer_ref_props(outpicref, picref);
+        outpicref->video->w = outlink->w;
+        outpicref->video->h = outlink->h;
+        outpicref->video->interlaced = 1;
+        outpicref->video->top_field_first = tinterlace->frame & 1;
+
+        /* cleanup the destination picture */
+        memset(outpicref->data[0], 0, outpicref->linesize[0] * outlink->h);
+        if (outpicref->data[1])
+            memset(outpicref->data[1], 128, outpicref->linesize[1] * (outlink->h >> tinterlace->vsub));
+        if (outpicref->data[2])
+            memset(outpicref->data[2], 128, outpicref->linesize[2] * (outlink->h >> tinterlace->vsub));
+
+        /* copy fields */
+        copy_picture_fields(outpicref->data, outpicref->linesize,
+                            picref   ->data, picref   ->linesize,
+                            outlink->format, inlink->w, inlink->h, tinterlace->frame & 1, 0, 0);
+        tinterlace->picref = outpicref;
+        tinterlace->frame_pending = 1;
+    } else if (tinterlace->mode == 4) {
+        /* Interleave even lines from even frames with odd lines from odd frames. Height unchanged at half framerate. */
+        if (!tinterlace->picref) {
+            outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+            avfilter_copy_buffer_ref_props(outpicref, picref);
+            outpicref->video->w = outlink->w;
+            outpicref->video->h = outlink->h;
+            outpicref->video->interlaced = 1;
+            outpicref->video->top_field_first = 1;
+
+            /* write odd frame odd fields into the upper fields of the new frame */
+            copy_picture_fields(outpicref->data, outpicref->linesize,
+                                picref   ->data, picref   ->linesize,
+                                outlink->format, inlink->w, inlink->h, 0, 1, 0);
+            tinterlace->picref = outpicref;
+        } else {
+            outpicref = tinterlace->picref;
+
+            /* write even frame even fields into the lower fields of the new frame */
+            copy_picture_fields(outpicref->data, outpicref->linesize,
+                                picref   ->data, picref   ->linesize,
+                                outlink->format, inlink->w, inlink->h, 1, 1, 1);
+            tinterlace->frame_pending = 1;
+        }
+    }
+
+    tinterlace->frame++;
+    avfilter_unref_buffer(inlink->cur_buf);
+}
+
+static void null_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) { }
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    TInterlaceContext *tinterlace = ctx->priv;
+
+    while (!tinterlace->frame_pending) {
+        int ret;
+
+        if ((ret = avfilter_request_frame(outlink->src->inputs[0])))
+            return ret;
+    }
+
+    avfilter_start_frame(outlink, tinterlace->picref);
+    avfilter_draw_slice(outlink, 0, outlink->h, 1);
+    avfilter_end_frame(outlink);
+    tinterlace->picref = NULL;
+    tinterlace->frame_pending = 0;
+    return 0;
+}
+
+static int poll_frame(AVFilterLink *outlink)
+{
+    TInterlaceContext *tinterlace = outlink->src->priv;
+
+    return tinterlace->frame_pending;
+}
+
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+AVFilter avfilter_vf_tinterlace = {
+    .name          = "tinterlace",
+    .description   = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
+
+    .priv_size     = sizeof(TInterlaceContext),
+    .init          = init,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame      = null_start_frame,
+                                    .draw_slice       = null_draw_slice,
+                                    .end_frame        = end_frame, },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .config_props     = config_out_props,
+                                    .poll_frame       = poll_frame,
+                                    .request_frame    = request_frame, },
+
+                                  { .name = NULL}},
+};
-- 
1.7.1




More information about the ffmpeg-devel mailing list