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

Clément Bœsch ubitux at gmail.com
Tue Oct 30 21:37:52 CET 2012


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 ')'

> + 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...

>  @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)

> + */
> +
> +#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?

> +
> +    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,

nit: weird align

> +};
> 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"?

[...]

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).

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121030/2cf0b5ca/attachment.asc>


More information about the ffmpeg-devel mailing list