[FFmpeg-devel] [PATCH] joinfields filter

Paul B Mahol onemda at gmail.com
Mon Apr 22 11:59:22 CEST 2013


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi            |  18 ++++++
 libavfilter/Makefile        |   1 +
 libavfilter/allfilters.c    |   1 +
 libavfilter/vf_joinfields.c | 145 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 165 insertions(+)
 create mode 100644 libavfilter/vf_joinfields.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 1a93309..864f3ea 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -5457,6 +5457,24 @@ This filter use field-dominance information in frame to decide which
 of each pair of fields to place first in the output.
 If it gets it wrong use @ref{setfield} filter before @code{separatefields} filter.
 
+ at section joinfields
+
+The @code{joinfields} takes a field-based video input and joins
+top and bottom field into its components frame, producing a double height clip
+with half the frame rate and half the frame count.
+
+The filter accept the following option:
+ at table @option
+ at item first_field
+ at table @samp
+ at item top, t
+top field first
+ at item bottom, b
+bottom field first
+The default value is @code{top}.
+ at end table
+ at end table
+
 @section setdar, setsar
 
 The @code{setdar} filter sets the Display Aspect Ratio for the filter
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index b005f3a..9ebfdbc 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -138,6 +138,7 @@ OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
 OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
 OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
 OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
+OBJS-$(CONFIG_JOINFIELDS_FILTER)             += vf_joinfields.o
 OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 3e2d62c..eb5f657 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -134,6 +134,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(HUE,            hue,            vf);
     REGISTER_FILTER(IDET,           idet,           vf);
     REGISTER_FILTER(IL,             il,             vf);
+    REGISTER_FILTER(JOINFIELDS,     joinfields,     vf);
     REGISTER_FILTER(INTERLACE,      interlace,      vf);
     REGISTER_FILTER(INTERLEAVE,     interleave,     vf);
     REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
diff --git a/libavfilter/vf_joinfields.c b/libavfilter/vf_joinfields.c
new file mode 100644
index 0000000..0de5fbb
--- /dev/null
+++ b/libavfilter/vf_joinfields.c
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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
+ */
+
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct {
+    const AVClass *class;
+    int first_field;
+    int64_t frame_count;
+    double ts_unit;
+    int nb_planes;
+    int planeheight[4];
+    int linesize[4];
+
+    AVFrame *prev;
+} JoinFieldsContext;
+
+#define OFFSET(x) offsetof(JoinFieldsContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption joinfields_options[] = {
+    {"first_field", "set first field", OFFSET(first_field), AV_OPT_TYPE_INT,   {.i64=0}, 0, 1, FLAGS, "field"},
+        {"top",    "set top field first",                0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
+        {"t",      "set top field first",                0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
+        {"bottom", "set bottom field first",             0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
+        {"b",      "set bottom field first",             0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(joinfields);
+
+static int config_props_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    JoinFieldsContext *jf = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int ret;
+
+    outlink->time_base.num = inlink->time_base.num * 2;
+    outlink->time_base.den = inlink->time_base.den;
+    outlink->frame_rate.num = inlink->frame_rate.num;
+    outlink->frame_rate.den = inlink->frame_rate.den * 2;
+    outlink->w = inlink->w;
+    outlink->h = inlink->h * 2;
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+    jf->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
+
+    if ((ret = av_image_fill_linesizes(jf->linesize, inlink->format, inlink->w)) < 0)
+        return ret;
+
+    jf->planeheight[1] = jf->planeheight[2] = inlink->h >> desc->log2_chroma_h;
+    jf->planeheight[0] = jf->planeheight[3] = inlink->h;
+
+    jf->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
+{
+    AVFilterContext *ctx = inlink->dst;
+    JoinFieldsContext *jf = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *out;
+    int i;
+
+    if (!jf->prev) {
+        jf->prev = cur;
+        return 0;
+    }
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        av_frame_free(&cur);
+        av_frame_free(&jf->prev);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, cur);
+
+    for (i = 0; i < jf->nb_planes; i++) {
+        av_image_copy_plane(out->data[i] + out->linesize[i] * !jf->first_field,
+                            out->linesize[i] * 2,
+                            cur->data[i], cur->linesize[i],
+                            jf->linesize[i], jf->planeheight[i]);
+        av_image_copy_plane(out->data[i] + out->linesize[i] * jf->first_field,
+                            out->linesize[i] * 2,
+                            jf->prev->data[i], jf->prev->linesize[i],
+                            jf->linesize[i], jf->planeheight[i]);
+    }
+
+    out->pts = jf->frame_count++ * jf->ts_unit;
+
+    av_frame_free(&cur);
+    av_frame_free(&jf->prev);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad joinfields_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad joinfields_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_props_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_joinfields = {
+    .name          = "joinfields",
+    .description   = NULL_IF_CONFIG_SMALL("Join input video fields into frames."),
+    .priv_size     = sizeof(JoinFieldsContext),
+    .priv_class    = &joinfields_class,
+    .inputs        = joinfields_inputs,
+    .outputs       = joinfields_outputs,
+};
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list