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

Stefano Sabatini stefasab at gmail.com
Tue Oct 30 22:36:21 CET 2012


On date Tuesday 2012-10-30 21:37:52 +0100, Clément Bœsch encoded:
> On Tue, Oct 30, 2012 at 01:31:14PM +0100, Stefano Sabatini wrote:
> > 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}.
> 
> missing closing ')'

Should be fixed in later version.
 
> > + 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
> > +
> 

> I'm still wondering about the kind of use-case for this filter though...

I don't know (never used it for practical purposes), but I suppose it
was implemented for a reason. Possible uses: testing (you want to test
the checksum of every single field), or you want to get a very
computationally cheap deinterlacer (yes it loses half of the
information, and squeeze the image).

> 
> >  @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
> 
> missing \n (I think Paul pointed that out already)

Should be already fixed.

> 
> > + */
> > +
> > +#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;
> 
> If we have an odd height, won't this drop one of the line?

Fixed, although the case is not contemplated in mp=field.

[...] 
> nit: weird align

Fixed.

> > +};
> > 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"
> 
> why the double "field"?

If there are three arguments, the first one is assumed to be the name
of the test (rather than "pixfmts_field"), seemed a good way to keep
the changes minimal.
 
> 
> [...]
> 
> Not much to say, that port looks pretty OK, feel free to dismiss any
> comment I have, and push with the mp drop when you think it's fine (unless
> someone doesn't agree).

Updated. I'll push tomorrow if I read no more comments.
-- 
FFmpeg = Fiendish Fierce Minimalistic Peaceful EniGma
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0005-lavfi-add-field-filter.patch
Type: text/x-diff
Size: 13840 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121030/effbf264/attachment.bin>


More information about the ffmpeg-devel mailing list