[FFmpeg-devel] [PATCH] lavfi: port libmpcodecs delogo filter

Stefano Sabatini stefano.sabatini-lala at poste.it
Wed Aug 10 19:29:28 CEST 2011


---
 configure                |    1 +
 doc/filters.texi         |   26 +++++
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_delogo.c  |  252 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 281 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_delogo.c

diff --git a/configure b/configure
index 62babd7..b73b0bd 100755
--- a/configure
+++ b/configure
@@ -1502,6 +1502,7 @@ udp_protocol_deps="network"
 blackframe_filter_deps="gpl"
 boxblur_filter_deps="gpl"
 cropdetect_filter_deps="gpl"
+delogo_filter_deps="gpl"
 drawtext_filter_deps="libfreetype"
 frei0r_filter_deps="frei0r dlopen strtok_r"
 frei0r_src_filter_deps="frei0r dlopen strtok_r"
diff --git a/doc/filters.texi b/doc/filters.texi
index 5ca0e92..15f29ab 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -405,6 +405,32 @@ indicates never reset and return the largest area encountered during
 playback.
 @end table
 
+ at section delogo
+
+Suppresses a TV station logo by a simple interpolation of the
+surrounding pixels. Just set a rectangle covering the logo and watch it
+disappear (and sometimes something even uglier appear - your mileage may
+vary).
+
+It accepts a list of mandatory parameters, separated by ":":
+ at example
+delogo=@var{x}:@var{y}:@var{w}:@var{h}:@var{t}
+ at end example
+
+ at table @option
+
+ at item x, y
+Specify the top left corner coordinates of the logo.
+
+ at item w, h
+Specify the width and height of the logo to clear.
+
+ at item band
+Specify the thickness of the fuzzy edge of the rectangle (added to
+ at var{w} and @var{h}). When set to -1, a green rectangle is drawn on the
+screen to simplify finding the right @var{x}, @var{y}, @var{w}, @var{h}
+parameters.
+
 @section drawbox
 
 Draw a colored box on the input image.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index e5c253a..0b45cf4 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -30,6 +30,7 @@ OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
+OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 8fd0609..ea2e3f2 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -46,6 +46,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (COPY,        copy,        vf);
     REGISTER_FILTER (CROP,        crop,        vf);
     REGISTER_FILTER (CROPDETECT,  cropdetect,  vf);
+    REGISTER_FILTER (DELOGO,      delogo,      vf);
     REGISTER_FILTER (DRAWBOX,     drawbox,     vf);
     REGISTER_FILTER (DRAWTEXT,    drawtext,    vf);
     REGISTER_FILTER (FADE,        fade,        vf);
diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c
new file mode 100644
index 0000000..2dd6582
--- /dev/null
+++ b/libavfilter/vf_delogo.c
@@ -0,0 +1,252 @@
+/*
+ * Copyright (c) 2002 Jindrich Makovicka <makovick at gmail.com>
+ * Copyright (c) 2011 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 General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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
+ * A very simple tv station logo remover
+ * Ported from MPlayer libmpcodecs/vf_delogo.c.
+ */
+
+#include "libavutil/imgutils.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+
+typedef struct {
+    unsigned int outfmt;
+    int x, y, w, h, band, show;
+}  DelogoContext;
+
+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_YUV440P,
+        PIX_FMT_YUVA420P,
+        PIX_FMT_NONE
+    };
+
+    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
+    return 0;
+}
+
+/**
+ * Apply a simple delogo algorithm to the image in dst and put the
+ * result in src.
+ *
+ * The algorithm is only applied to the region specified by the logo
+ * parameters.
+ *
+ * @param w      width of the input image
+ * @param h      height of the input image
+ * @param logo_x x coordinate of the top left corner of the logo region
+ * @param logo_y y coordinate of the top left corner of the logo region
+ * @param logo_w width of the logo
+ * @param logo_h height of the logo
+ * @param band
+ * @param show
+ * @param direct
+ */
+static void apply_delogo(uint8_t *dst, int dst_linesize,
+                         uint8_t *src, int src_linesize,
+                         int w, int h,
+                         int logo_x, int logo_y, int logo_w, int logo_h,
+                         int band, int show, int direct)
+{
+    int x, y;
+    int interp, dist;
+    uint8_t *xdst, *xsrc;
+
+    uint8_t *topleft, *botleft, *topright;
+    int xclipl, xclipr, yclipt, yclipb;
+    int logo_x1, logo_x2, logo_y1, logo_y2;
+
+    xclipl = FFMAX(-logo_x, 0);
+    xclipr = FFMAX(logo_x+logo_w-w, 0);
+    yclipt = FFMAX(-logo_y, 0);
+    yclipb = FFMAX(logo_y+logo_h-h, 0);
+
+    logo_x1 = logo_x + xclipl;
+    logo_x2 = logo_x + logo_w - xclipr;
+    logo_y1 = logo_y + yclipt;
+    logo_y2 = logo_y + logo_h - yclipb;
+
+    topleft  = src+logo_y1     * src_linesize+logo_x1;
+    topright = src+logo_y1     * src_linesize+logo_x2-1;
+    botleft  = src+(logo_y2-1) * src_linesize+logo_x1;
+
+    dst += (logo_y1+1)*dst_linesize;
+    src += (logo_y1+1)*src_linesize;
+
+    if (!direct)
+        av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
+
+    for (y = logo_y1+1; y < logo_y2-1; y++) {
+        for (x = logo_x1+1,
+             xdst = dst+logo_x1+1,
+             xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
+            interp =
+                (topleft[src_linesize*(y-logo_y  -yclipt)]   +
+                 topleft[src_linesize*(y-logo_y-1-yclipt)]   +
+                 topleft[src_linesize*(y-logo_y+1-yclipt)])  * (logo_w-(x-logo_x))/logo_w
+                +
+                (topright[src_linesize*(y-logo_y-yclipt)]    +
+                 topright[src_linesize*(y-logo_y-1-yclipt)]  +
+                 topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
+                +
+                (topleft[x-logo_x-xclipl]    +
+                 topleft[x-logo_x-1-xclipl]  +
+                 topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
+                +
+                (botleft[x-logo_x-xclipl]    +
+                 botleft[x-logo_x-1-xclipl]  +
+                 botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
+            interp /= 6;
+
+            if (y >= logo_y+band && y < logo_y+logo_h-band
+                && x >= logo_x+band && x < logo_x+logo_w-band) {
+                *xdst = interp;
+            } else {
+                dist = 0;
+                if      (x < logo_x+band)
+                    dist = FFMAX(dist, logo_x-x+band);
+                else if (x >= logo_x+logo_w-band)
+                    dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
+
+                if      (y < logo_y+band)
+                    dist = FFMAX(dist, logo_y-y+band);
+                else if (y >= logo_y+logo_h-band)
+                    dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
+
+                *xdst = (*xsrc*dist + interp*(band-dist))/band;
+                if (show && (dist == band-1))
+                    *xdst = 0;
+            }
+        }
+
+        dst += dst_linesize;
+        src += src_linesize;
+    }
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    DelogoContext *delogo = ctx->priv;
+    int res = 0;
+
+    if (args)
+        res = sscanf(args, "%d:%d:%d:%d:%d",
+                     &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
+
+    if (res != 5) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Expected five arguments, only %d provided\n", res);
+        return AVERROR(EINVAL);
+    }
+
+    av_log(ctx, AV_LOG_INFO, "x:%d y:%d, w:%d h:%d band:%d\n",
+           delogo->x, delogo->y, delogo->w, delogo->h, delogo->band);
+
+    delogo->show = 0;
+
+    if (delogo->band < 0) {
+        delogo->band = 4;
+        delogo->show = 1;
+    }
+
+    delogo->w   += delogo->band*2;
+    delogo->h   += delogo->band*2;
+    delogo->x   -= delogo->band;
+    delogo->y   -= delogo->band;
+
+    return 0;
+}
+
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+{
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFilterBufferRef *outpicref;
+
+    if (inpicref->perms & AV_PERM_PRESERVE) {
+        outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
+                                              outlink->w, outlink->h);
+        avfilter_copy_buffer_ref_props(outpicref, inpicref);
+        outpicref->video->w = outlink->w;
+        outpicref->video->h = outlink->h;
+    } else
+        outpicref = inpicref;
+
+    outlink->out_buf = outpicref;
+    avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
+}
+
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink)
+{
+    DelogoContext *delogo = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
+    AVFilterBufferRef *outpicref = outlink->out_buf;
+    int direct = inpicref == outpicref;
+    int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+    int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+    int plane;
+
+    for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
+        int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
+        int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
+
+        apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
+                     inpicref->data [plane], inpicref ->linesize[plane],
+                     inlink->w>>hsub, inlink->h>>vsub,
+                     delogo->x>>hsub, delogo->y>>vsub,
+                     delogo->w>>hsub, delogo->h>>vsub,
+                     delogo->band>>FFMAX(hsub, vsub),
+                     delogo->show, direct);
+    }
+
+    avfilter_draw_slice(outlink, 0, inlink->h, 1);
+    avfilter_end_frame(outlink);
+    avfilter_unref_buffer(inpicref);
+    if (!direct)
+        avfilter_unref_buffer(outpicref);
+}
+
+AVFilter avfilter_vf_delogo = {
+    .name          = "delogo",
+    .description   = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
+    .priv_size     = sizeof(DelogoContext),
+    .init          = init,
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .get_video_buffer = avfilter_null_get_video_buffer,
+                                    .start_frame      = start_frame,
+                                    .draw_slice       = null_draw_slice,
+                                    .end_frame        = end_frame,
+                                    .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
+                                    .rej_perms        = AV_PERM_PRESERVE },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list