[FFmpeg-devel] [PATCH] Add negate filter.

Michael Niedermayer michaelni
Mon Nov 29 03:21:22 CET 2010


On Sun, Nov 28, 2010 at 12:30:09PM +0100, Stefano Sabatini wrote:
> On date Sunday 2010-11-28 03:09:35 +0100, Michael Niedermayer encoded:
> > On Fri, Nov 26, 2010 at 05:32:13PM +0100, Stefano Sabatini wrote:
> [...]
> > > +static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
> > > +{
> > > +    NegContext *neg = inlink->dst->priv;
> > > +    AVFilterLink *outlink = inlink->dst->outputs[0];
> > > +    AVFilterBufferRef *inpic  = inlink ->cur_buf;
> > > +    AVFilterBufferRef *outpic = outlink->out_buf;
> > > +    uint8_t *inrow, *outrow;
> > > +    int i, j, plane;
> > > +
> > > +    if (inlink->format == PIX_FMT_MONOWHITE || inlink->format == PIX_FMT_MONOBLACK) {
> > > +        inrow  = inpic ->data[0] + y * inpic ->linesize[0];
> > > +        outrow = outpic->data[0] + y * outpic->linesize[0];
> > > +        for (i = 0; i < h; i++) {
> > > +            for (j = 0; j < inlink->w >> 3; j++)
> > > +                outrow[j] = ~inrow[j];
> > > +            inrow  += inpic ->linesize[0];
> > > +            outrow += outpic->linesize[0];
> > > +        }
> > > +    } else {
> > > +        /* luma plane */
> > > +        inrow  = inpic ->data[0] + y * inpic-> linesize[0];
> > > +        outrow = outpic->data[0] + y * outpic->linesize[0];
> > > +        for (i = 0; i < h; i ++) {
> > > +            for(j = 0; j < inlink->w; j++)
> > > +                outrow[j] = 255 - inrow[j] + neg->off_y;
> > > +            inrow  += inpic ->linesize[0];
> > > +            outrow += outpic->linesize[0];
> > > +        }
> > > +
> > > +        /* chroma planes */
> > > +        for (plane = 1; plane < 3; plane++) {
> > > +            inrow  = inpic-> data[plane] + (y >> neg->vsub) * inpic ->linesize[plane];
> > > +            outrow = outpic->data[plane] + (y >> neg->vsub) * outpic->linesize[plane];
> > > +
> > > +            for (i = 0; i < h >> neg->vsub; i++) {
> > > +                for (j = 0; j < inlink->w >> neg->hsub; j++)
> > > +                    outrow[j] = 255 - inrow[j] + neg->off_uv;
> > > +                inrow  += inpic ->linesize[plane];
> > > +                outrow += outpic->linesize[plane];
> > > +            }
> > > +        }
> > 
> > the user should be able to selct which planes are negated
> 
> Good idea, updated.
> -- 
> FFmpeg = Fast & Faithless Meaningful Powerful Extreme Guru

>  doc/filters.texi         |   22 ++++++
>  libavfilter/Makefile     |    1 
>  libavfilter/allfilters.c |    1 
>  libavfilter/vf_negate.c  |  171 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 195 insertions(+)
> eb64b3d38766ee01540a93169fe9f72323850247  0001-Add-negate-filter.patch
> From 6e830a904f785b5ef8663a40ff67a3bbb7e5d524 Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefano.sabatini-lala at poste.it>
> Date: Fri, 26 Nov 2010 17:31:11 +0100
> Subject: [PATCH] Add negate filter.
> 
> Ported from the libavfilter-soc repo with per-component negation
> addition.
> ---
>  doc/filters.texi         |   22 ++++++
>  libavfilter/Makefile     |    1 +
>  libavfilter/allfilters.c |    1 +
>  libavfilter/vf_negate.c  |  171 ++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 195 insertions(+), 0 deletions(-)
>  create mode 100644 libavfilter/vf_negate.c
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 1cba2d6..95b235e 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -360,6 +360,28 @@ The following command:
>  will make libavfilter use a format different from "yuv420p" for the
>  input to the vflip filter.
>  
> + at section negate
> +
> +Convert one or more components of a video to its negative.
> +
> +This filter accepts as paramter a string containing a sequence of the
> +characters 'y' (luma component), 'u' (U chroma component), 'v' (V
> +chroma component), 'a' (alpha component). The components corresponding
> +to the specified characters will be negated. If the string is not
> +specified "yuv" is assumed.
> +
> +Follow some examples:
> + at example
> +# only negate luma
> +negate=y
> +
> +# negate chroma components
> +negate=uv
> +
> +# negate all the components
> +negate=yuva
> + at end example
> +
>  @section null
>  
>  Pass the video source unchanged to the output.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 210510f..4379e9a 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -27,6 +27,7 @@ OBJS-$(CONFIG_FIFO_FILTER)                   += vf_fifo.o
>  OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
>  OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
>  OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
> +OBJS-$(CONFIG_NEGATE_FILTER)                 += vf_negate.o
>  OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
>  OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
>  OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index c3067b8..3291bd1 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -48,6 +48,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER (FORMAT,      format,      vf);
>      REGISTER_FILTER (FREI0R,      frei0r,      vf);
>      REGISTER_FILTER (HFLIP,       hflip,       vf);
> +    REGISTER_FILTER (NEGATE,      negate,      vf);
>      REGISTER_FILTER (NOFORMAT,    noformat,    vf);
>      REGISTER_FILTER (NULL,        null,        vf);
>      REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
> diff --git a/libavfilter/vf_negate.c b/libavfilter/vf_negate.c
> new file mode 100644
> index 0000000..bfd4120
> --- /dev/null
> +++ b/libavfilter/vf_negate.c
> @@ -0,0 +1,171 @@
> +/*
> + * Copyright (c) 2007 Bobby Bingham
> + * Copyright (c) 2010 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
> + * video negative filter
> + */
> +
> +#include "libavutil/pixdesc.h"
> +#include "avfilter.h"
> +
> +typedef struct {
> +    int hsub, vsub;
> +    int plane_neg[4]; ///< tells which components to negate
> +    int plane_off[4]; ///< plane value offset
> +} NegContext;
> +
> +#define Y 0
> +#define U 1
> +#define V 2
> +#define A 3
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
> +{
> +    NegContext *neg = ctx->priv;
> +    char plane_neg_str[16] = "yuv";
> +    int i;
> +
> +    if (args)
> +        sscanf(args, "%15s", plane_neg_str);
> +
> +    for (i = 0; plane_neg_str[i]; i++) {
> +        char c = plane_neg_str[i];
> +        switch (c) {
> +        case 'y': neg->plane_neg[Y] = 1; break;
> +        case 'u': neg->plane_neg[U] = 1; break;
> +        case 'v': neg->plane_neg[V] = 1; break;
> +        case 'a': neg->plane_neg[A] = 1; break;
> +        default:
> +            av_log(ctx, AV_LOG_ERROR, "Invalid character '%c' in arguments '%s'.\n",
> +                   c, args);
> +            return AVERROR(EINVAL);
> +        }
> +    }
> +
> +    av_log(ctx, AV_LOG_INFO, "y:%d u:%d v:%d a:%d\n",
> +           neg->plane_neg[Y], neg->plane_neg[U], neg->plane_neg[V], neg->plane_neg[A]);
> +    return 0;
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    enum PixelFormat pix_fmts[] = {
> +        PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
> +        PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
> +        PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
> +        PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
> +        PIX_FMT_YUVA420P,
> +        PIX_FMT_MONOWHITE, PIX_FMT_MONOBLACK,
> +        PIX_FMT_NONE
> +    };
> +
> +    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
> +    return 0;
> +}
> +
> +static int config_props(AVFilterLink *inlink)
> +{
> +    NegContext *neg = inlink->dst->priv;
> +
> +    neg->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
> +    neg->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
> +
> +    switch (inlink->format) {
> +    case PIX_FMT_YUVJ444P:
> +    case PIX_FMT_YUVJ422P:
> +    case PIX_FMT_YUVJ420P:
> +    case PIX_FMT_YUVJ440P:
> +        neg->plane_off[Y] =
> +        neg->plane_off[U] = neg->plane_off[V] = 0;
> +        break;
> +    default:
> +        neg->plane_off[Y] = neg->plane_off[A] = -4;
> +        neg->plane_off[U] = neg->plane_off[V] = 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
> +{
> +    NegContext *neg = inlink->dst->priv;
> +    AVFilterLink *outlink = inlink->dst->outputs[0];
> +    AVFilterBufferRef *inpic  = inlink ->cur_buf;
> +    AVFilterBufferRef *outpic = outlink->out_buf;
> +    uint8_t *inrow, *outrow;
> +    int i, j, plane;
> +
> +    for (plane = 0; inpic->data[plane]; plane++) {
> +        int vsub = plane == 1 || plane == 2 ? neg->vsub : 0;
> +        int hsub = plane == 1 || plane == 2 ? neg->hsub : 0;
> +
> +        inrow  = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane];
> +        outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
> +
> +        if (neg->plane_neg[plane]) {
> +            if (inlink->format == PIX_FMT_MONOWHITE || inlink->format == PIX_FMT_MONOBLACK) {
> +                for (i = 0; i < inlink->h; i++) {
> +                    for (j = 0; j < inlink->w >> 3; j++)
> +                        outrow[j] = ~inrow[j];
> +                    inrow  += inpic ->linesize[0];
> +                    outrow += outpic->linesize[0];
> +                }
> +            } else {
> +                for (i = 0; i < (h>>vsub); i ++) {
> +                    for (j = 0; j < (inlink->w>>hsub); j++)
> +                        outrow[j] = 255 - inrow[j] + neg->plane_off[plane];

can overflow and others likely too

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Concerning the gods, I have no means of knowing whether they exist or not
or of what sort they may be, because of the obscurity of the subject, and
the brevity of human life -- Protagoras
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20101129/e82fb091/attachment.pgp>



More information about the ffmpeg-devel mailing list