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

Stefano Sabatini stefasab at gmail.com
Tue Oct 30 13:31:14 CET 2012


The filter is a port of libmpcodecs/vf_field.c, since there is no common
code I relicensed it as LGPL, while keeping the original author
copyright.

TODO: bump lavfi minor, update Changelog
---
 doc/filters.texi          |   25 ++++++++
 libavfilter/Makefile      |    1 +
 libavfilter/allfilters.c  |    1 +
 libavfilter/vf_field.c    |  145 +++++++++++++++++++++++++++++++++++++++++++++
 tests/fate/avfilter.mak   |    1 +
 tests/lavfi-regression.sh |    1 +
 tests/ref/lavfi/field     |   83 ++++++++++++++++++++++++++
 7 files changed, 257 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_field.c
 create mode 100644 tests/ref/lavfi/field

diff --git a/doc/filters.texi b/doc/filters.texi
index 0e77914..215c12a 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2115,6 +2115,31 @@ fade=in:5:20
 fade=in:0:25:alpha=1
 @end example
 
+ at section field
+
+Extract a single field from an interlaced image using stride
+arithmetic to avoid wasting CPU time. The output frames are marked as
+non-interlaced.
+
+This filter accepts the following named options:
+ at table @option
+ at item type
+Specify whether to extract the top (if the value is @code{0} or
+ at code{top}) or the bottom field (if the value is @code{1} or
+ at code{bottom}.
+ at end table
+
+If the option key is not specified, the first value specifies the
+type. For example:
+ at example
+field=type=bottom
+ at end example
+
+is equivalent to:
+ at example
+field=1
+ at end example
+
 @section fieldorder
 
 Transform the field order of the input video.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 3618f10..0da0645 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -98,6 +98,7 @@ OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
 OBJS-$(CONFIG_EDGEDETECT_FILTER)             += vf_edgedetect.o
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
+OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
 OBJS-$(CONFIG_FIFO_FILTER)                   += fifo.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 348f369..fef40e9 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -90,6 +90,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (DRAWTEXT,    drawtext,    vf);
     REGISTER_FILTER (EDGEDETECT,  edgedetect,  vf);
     REGISTER_FILTER (FADE,        fade,        vf);
+    REGISTER_FILTER (FIELD,       field,       vf);
     REGISTER_FILTER (FIELDORDER,  fieldorder,  vf);
     REGISTER_FILTER (FIFO,        fifo,        vf);
     REGISTER_FILTER (FORMAT,      format,      vf);
diff --git a/libavfilter/vf_field.c b/libavfilter/vf_field.c
new file mode 100644
index 0000000..db55b99
--- /dev/null
+++ b/libavfilter/vf_field.c
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2003 Rich Felker
+ * Copyright (c) 2012 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 field filter, based on libmpcodecs/vf_field.c by Rich
+ * Felker
+ */
+
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "internal.h"
+
+enum FieldType { FIELD_TYPE_TOP = 0, FIELD_TYPE_BOTTOM };
+
+typedef struct {
+    const AVClass *class;
+    enum FieldType type;
+    int nb_planes;              ///< number of planes of the current format
+} FieldContext;
+
+#define OFFSET(x) offsetof(FieldContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption field_options[] = {
+    {"type", "set field type (top or bottom)", OFFSET(type), AV_OPT_TYPE_INT, {.i64=FIELD_TYPE_TOP}, 0, 1, FLAGS, "field_type" },
+    {"top",    "select top field",    0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_TOP},    INT_MIN, INT_MAX, FLAGS, "field_type"},
+    {"bottom", "select bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field_type"},
+
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(field);
+
+static av_cold int init(AVFilterContext *ctx, const char *args)
+{
+    FieldContext *field = ctx->priv;
+    static const char *shorthand[] = { "type", NULL };
+
+    field->class = &field_class;
+    av_opt_set_defaults(field);
+
+    return av_opt_set_from_string(field, args, shorthand, "=", ":");
+}
+
+static int config_props_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    FieldContext *field = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int i;
+
+    for (i = 0; i < desc->nb_components; i++)
+        field->nb_planes = FFMAX(field->nb_planes, desc->comp[i].plane);
+    field->nb_planes++;
+
+    outlink->w = inlink->w;
+    outlink->h = inlink->h/2;
+
+    av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d type:%s -> w:%d h:%d\n",
+           inlink->w, inlink->h, field->type == FIELD_TYPE_BOTTOM ? "bottom" : "top",
+           outlink->w, outlink->h);
+    return 0;
+}
+
+static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+{
+    FieldContext *field = inlink->dst->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
+    int i;
+
+    if (!outpicref)
+        return AVERROR(ENOMEM);
+    outpicref->video->h = inpicref->video->h/2;
+    outpicref->video->interlaced = 0;
+
+    for (i = 0; i < field->nb_planes; i++) {
+        outpicref->data[i] = inpicref->data[i] +
+            (field->type == FIELD_TYPE_BOTTOM ? inpicref->linesize[i] : 0);
+        outpicref->linesize[i] = 2 * inpicref->linesize[i];
+    }
+
+    if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)
+        outpicref->data[1] = inpicref->data[1];
+
+    return ff_start_frame(inlink->dst->outputs[0], outpicref);
+}
+
+static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+{
+    return ff_draw_slice(inlink->dst->outputs[0], y/2, h/2, slice_dir);
+}
+
+static const AVFilterPad field_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .get_video_buffer = ff_null_get_video_buffer,
+        .start_frame      = start_frame,
+        .draw_slice       = draw_slice,
+        .end_frame        = ff_null_end_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad field_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_props_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_field = {
+    .name          = "field",
+    .description   = NULL_IF_CONFIG_SMALL("Extract a field from the input image."),
+
+    .priv_size     = sizeof(FieldContext),
+    .init          = init,
+
+    .inputs    = field_inputs,
+    .outputs   = field_outputs,
+    .priv_class = &field_class,
+};
diff --git a/tests/fate/avfilter.mak b/tests/fate/avfilter.mak
index b931244..239fe64 100644
--- a/tests/fate/avfilter.mak
+++ b/tests/fate/avfilter.mak
@@ -11,6 +11,7 @@ FATE_LAVFI = fate-lavfi-alphaextract_rgb                                \
              fate-lavfi-drawbox                                         \
              fate-lavfi-edgedetect                                      \
              fate-lavfi-fade                                            \
+             fate-lavfi-field                                           \
              fate-lavfi-idet                                            \
              fate-lavfi-life                                            \
              fate-lavfi-null                                            \
diff --git a/tests/lavfi-regression.sh b/tests/lavfi-regression.sh
index 703d56c..76b2e5a 100755
--- a/tests/lavfi-regression.sh
+++ b/tests/lavfi-regression.sh
@@ -111,6 +111,7 @@ do_lavfi_pixfmts(){
 do_lavfi_pixfmts "copy"    ""
 do_lavfi_pixfmts "crop"    "100:100:100:100"
 do_lavfi_pixfmts "hflip"   ""
+do_lavfi_pixfmts "field"   "field" "bottom"
 do_lavfi_pixfmts "null"    ""
 do_lavfi_pixfmts "pad"     "500:400:20:20"
 do_lavfi_pixfmts "pixdesctest" ""
diff --git a/tests/ref/lavfi/field b/tests/ref/lavfi/field
new file mode 100644
index 0000000..33a5761
--- /dev/null
+++ b/tests/ref/lavfi/field
@@ -0,0 +1,83 @@
+0bgr                57434af4bddb691877f2400c704604eb
+0rgb                fc2ba950163aeee98590181e31fcd202
+abgr                3c78d0a72484a1ecd3cae245b9fa988c
+argb                d5057a2be412864719ffb8ba129c1f2c
+bgr0                b33c6b58b0d7bf6ce07d5a2d7c267040
+bgr24               bd6620738df19410d5df5f31e7451709
+bgr444be            219e318b7e0e05050181e71df6b9539d
+bgr444le            8354f2cf5b30de0233d302a74816649d
+bgr48be             22be50bd0aa39f07ad1b1aa57cb741ce
+bgr48le             c4b0f8057b3eac237e9228e83bdc4c66
+bgr4_byte           d4c3304b4b823a130c335379e4d3444d
+bgr555be            c3072da465233dbfc8f61dc7a9766d2c
+bgr555le            be83adcf0b802b061442f0c564fd5987
+bgr565be            bf955b9a035af0e613cf1de249f55f9d
+bgr565le            6dd85cd5e19266c53a54cbcf06d396a7
+bgr8                9669f6974f0fc1c0afa1c7d4df093c0b
+bgra                f7cabae31dd7465dab2203f45db646f8
+gray                66a09b53f7d3f79dcb6096f3ec3740c5
+gray16be            a447af6482b922c9997ac02e5d3535f1
+gray16le            c1dd0db327295898ff282d07f48c105d
+monob               1b7fb7e69a913a0a1c0dffc54e5899ea
+monow               b5d3778a054fc73d515d36f8a6bc693b
+nv12                b3829e9ae2a15349432b7efac4236068
+nv21                963cf5780e07301ff2906bf345b6d0ff
+pal8                bfedafc3bf19c2c12eeb87125833142a
+rgb0                d7481143742ff68abfbac4195edbede0
+rgb24               908d5494062c617bc87149c9daaf2167
+rgb444be            281a8f186a2726c2b31aa7e09e21c865
+rgb444le            93f9ee6265d8ad5e744ab652563f9b78
+rgb48be             0d1d60e1639edb2758ad776cb5583970
+rgb48le             c958b5e98324263e97de2bb528f5bda4
+rgb4_byte           2ec97bf65649e3d47eb6812701544593
+rgb555be            21b9138b229d4065b02d38b5b62f18d1
+rgb555le            0307ee34e562b2fb2b1c6988ae18b2b2
+rgb565be            e8f3ebcbb9a5fff000eca8a312f89782
+rgb565le            53bbd558fb0dcd82f1fad83ea855c3ad
+rgb8                67bfdd4fa88b1ab9be876f42dfc75683
+rgba                d0ebdf1495bc6b7e9d3bfbe2813a9d16
+uyvy422             a6a52504a16f09b8f2ec2405bc8190b5
+yuv410p             3feb55b1e2a385b488c82e808a12587b
+yuv411p             ab3dd8e6cf1452afe2d2e976e4726370
+yuv420p             52e26ad198368e2326bef97cdfa6a2bb
+yuv420p10be         04353bfc21e9b88cd7776e83be756742
+yuv420p10le         3f8e7167dbd12976c6ee516b8c952363
+yuv420p12be         b058ac076c8a5fe522b9fd9b8422054e
+yuv420p12le         ad0bf28e69eeb14eac5d8f9ea8b801f1
+yuv420p14be         c7a435d42f07928332ecb21a7d96ad7a
+yuv420p14le         5507e8db4e58c9517012686a7408996b
+yuv420p16be         5241d64e9fa2fd6590fd23ea0e8a6f90
+yuv420p16le         78da606f761a4fb62fdac05aa5092742
+yuv420p9be          e4bcaf5d6a7030f950b08501327f6175
+yuv420p9le          bbf80e57389578be66d4a7a12335a613
+yuv422p             e461a21995da361b88202339a2ebb879
+yuv422p10be         a3e13070215f5a016ac9bae7e7115417
+yuv422p10le         8e9e3d9adc8fdb8a0a03d79bdc31eefe
+yuv422p12be         4c339f71d79d2dac1dabc6121e1cf021
+yuv422p12le         69d336fccbe1ffa88106ea5bde0c8743
+yuv422p14be         f20c21dbfda632d26816fce27c1cb6e9
+yuv422p14le         70dddb8bdad188079a05113059d139f8
+yuv422p16be         55cfed8fa610f82b6625e16871dab235
+yuv422p16le         e2488df0f22987fe7ed12a5ef2adf835
+yuv422p9be          80fcdd7fd9cdd79632104dcc32f78b4b
+yuv422p9le          e0ec9f94c875297ee5d0546274df40e9
+yuv440p             f8e80596babcdb94378ec8bebf2dd46d
+yuv444p             572bad9e12ed53e242658fa613412279
+yuv444p10be         c5304f086afc4624d4fffb66a3cf3cb8
+yuv444p10le         d1754974b936f74028752d49413d30aa
+yuv444p12be         206d6b0fbd84d4e013b9b074cbd65135
+yuv444p12le         dc2b1bfbecba71eba50e7e4da470a8d0
+yuv444p14be         19cd2ef75ed5698898c55040e51def88
+yuv444p14le         8d47c9575d1355572ee9bfc873d46753
+yuv444p16be         3a67c28325978db734ba03b1828c15da
+yuv444p16le         c6dc275a4277fd3c65535253bb298263
+yuv444p9be          f47357cdd775fc399aeab3ae58712fb9
+yuv444p9le          e29799ecb6fac9f5b6d85bc34d248d4b
+yuva420p            82ab09bb7a3a24bf95aeb3fa9d939847
+yuva422p            c162b37ce05360c47b2a2224ea0748ad
+yuva444p            4a85f1f17e95829cd53c9a28928fd8eb
+yuvj420p            762dc6a157d0bee72da3e3d852668aef
+yuvj422p            8cec955c1c62b00b6798361ef82962b7
+yuvj440p            7b469444994d8b52766ee461bcb795ea
+yuvj444p            b395162325af489c465a3e6a31fbb0e7
+yuyv422             1efb17cd0a48d2e956fd574ea6f412e7
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list