lavfi/delogo: don't recompute the same difference again and again
[ffmpeg.git] / libavfilter / vf_delogo.c
index f0852f8..3a9bdd4 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  * Copyright (c) 2011 Stefano Sabatini
+ * Copyright (c) 2013 Jean Delvare <khali@linux-fr.org>
  *
  * This file is part of FFmpeg.
  *
 /**
  * @file
  * A very simple tv station logo remover
- * Ported from MPlayer libmpcodecs/vf_delogo.c.
+ * Originally imported from MPlayer libmpcodecs/vf_delogo.c,
+ * the algorithm was later improved.
  */
 
+#include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
 
 /**
- * Apply a simple delogo algorithm to the image in dst and put the
- * result in src.
+ * Apply a simple delogo algorithm to the image in src and put the
+ * result in dst.
  *
  * The algorithm is only applied to the region specified by the logo
  * parameters.
  */
 static void apply_delogo(uint8_t *dst, int dst_linesize,
                          uint8_t *src, int src_linesize,
-                         int w, int h,
+                         int w, int h, AVRational sar,
                          int logo_x, int logo_y, int logo_w, int logo_h,
                          int band, int show, int direct)
 {
-    int x, y;
-    int interp, dist;
+    int x, y, dist;
+    uint64_t interp, weightl, weightr, weightt, weightb;
     uint8_t *xdst, *xsrc;
 
     uint8_t *topleft, *botleft, *topright;
@@ -76,33 +82,40 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
     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);
 
+    dst += (logo_y1 + 1) * dst_linesize;
+    src += (logo_y1 + 1) * src_linesize;
+
     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++) {
+
+            /* Weighted interpolation based on relative distances, taking SAR into account */
+            weightl = (uint64_t)              (logo_x2-1-x) * (y-logo_y1) * (logo_y2-1-y) * sar.den;
+            weightr = (uint64_t)(x-logo_x1)                 * (y-logo_y1) * (logo_y2-1-y) * sar.den;
+            weightt = (uint64_t)(x-logo_x1) * (logo_x2-1-x)               * (logo_y2-1-y) * sar.num;
+            weightb = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (y-logo_y1)                 * sar.num;
+
             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
+                (topleft[src_linesize*(y-logo_y1)]    +
+                 topleft[src_linesize*(y-logo_y1-1)]  +
+                 topleft[src_linesize*(y-logo_y1+1)]) * weightl
                 +
-                (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
+                (topright[src_linesize*(y-logo_y1)]    +
+                 topright[src_linesize*(y-logo_y1-1)]  +
+                 topright[src_linesize*(y-logo_y1+1)]) * weightr
                 +
-                (topleft[x-logo_x-xclipl]    +
-                 topleft[x-logo_x-1-xclipl]  +
-                 topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
+                (topleft[x-logo_x1]    +
+                 topleft[x-logo_x1-1]  +
+                 topleft[x-logo_x1+1]) * weightt
                 +
-                (botleft[x-logo_x-xclipl]    +
-                 botleft[x-logo_x-1-xclipl]  +
-                 botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
-            interp /= 6;
+                (botleft[x-logo_x1]    +
+                 botleft[x-logo_x1-1]  +
+                 botleft[x-logo_x1+1]) * weightb;
+            interp /= (weightl + weightr + weightt + weightb) * 3U;
 
             if (y >= logo_y+band && y < logo_y+logo_h-band &&
                 x >= logo_x+band && x < logo_x+logo_w-band) {
@@ -136,64 +149,41 @@ typedef struct {
 }  DelogoContext;
 
 #define OFFSET(x) offsetof(DelogoContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption delogo_options[]= {
-    {"x",    "set logo x position",       OFFSET(x),    FF_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
-    {"y",    "set logo y position",       OFFSET(y),    FF_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
-    {"w",    "set logo width",            OFFSET(w),    FF_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
-    {"h",    "set logo height",           OFFSET(h),    FF_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
-    {"band", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
-    {"t",    "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
-    {"show", "show delogo area",          OFFSET(show), FF_OPT_TYPE_INT, {.dbl= 0},  0, 1       },
-    {NULL},
+    { "x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  4 }, -1, INT_MAX, FLAGS },
+    { "t",    "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  4 }, -1, INT_MAX, FLAGS },
+    { "show", "show delogo area",          OFFSET(show), AV_OPT_TYPE_INT, { .i64 =  0 },  0, 1,       FLAGS },
+    { NULL },
 };
 
-static const char *delogo_get_name(void *ctx)
-{
-    return "delogo";
-}
-
-static const AVClass delogo_class = {
-    "DelogoContext",
-    delogo_get_name,
-    delogo_options
-};
+AVFILTER_DEFINE_CLASS(delogo);
 
 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_GRAY8,
-        PIX_FMT_NONE
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
+        AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_NONE
     };
 
-    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+static av_cold int init(AVFilterContext *ctx)
 {
-    DelogoContext *delogo = ctx->priv;
-    int ret = 0;
-
-    delogo->class = &delogo_class;
-    av_opt_set_defaults2(delogo, 0, 0);
-
-    if (args)
-        ret = sscanf(args, "%d:%d:%d:%d:%d",
-                     &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
-    if (ret == 5) {
-        if (delogo->band < 0)
-            delogo->show = 1;
-    } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
-        return ret;
-    }
+    DelogoContext *s = ctx->priv;
 
 #define CHECK_UNSET_OPT(opt)                                            \
-    if (delogo->opt == -1) {                                            \
-        av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
+    if (s->opt == -1) {                                            \
+        av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
         return AVERROR(EINVAL);                                         \
     }
     CHECK_UNSET_OPT(x);
@@ -201,88 +191,102 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
     CHECK_UNSET_OPT(w);
     CHECK_UNSET_OPT(h);
 
-    if (delogo->show)
-        delogo->band = 4;
+    if (s->band < 0 || s->show) {
+        s->show = 1;
+        s->band = 4;
+    }
 
-    av_log(ctx, AV_LOG_INFO, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
-           delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
+    av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
+           s->x, s->y, s->w, s->h, s->band, s->show);
 
-    delogo->w += delogo->band*2;
-    delogo->h += delogo->band*2;
-    delogo->x -= delogo->band;
-    delogo->y -= delogo->band;
+    s->w += s->band*2;
+    s->h += s->band*2;
+    s->x -= s->band;
+    s->y -= s->band;
 
     return 0;
 }
 
-static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
+    DelogoContext *s = inlink->dst->priv;
     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));
-}
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    AVFrame *out;
+    int hsub0 = desc->log2_chroma_w;
+    int vsub0 = desc->log2_chroma_h;
+    int direct = 0;
+    int plane;
+    AVRational sar;
+
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
 
-static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+        av_frame_copy_props(out, in);
+    }
 
-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;
+    sar = in->sample_aspect_ratio;
+    /* Assume square pixels if SAR is unknown */
+    if (!sar.num)
+        sar.num = sar.den = 1;
 
-    for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
+    for (plane = 0; plane < 4 && in->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>>FFMIN(hsub, vsub),
-                     delogo->show, direct);
+        apply_delogo(out->data[plane], out->linesize[plane],
+                     in ->data[plane], in ->linesize[plane],
+                     FF_CEIL_RSHIFT(inlink->w, hsub),
+                     FF_CEIL_RSHIFT(inlink->h, vsub),
+                     sar, s->x>>hsub, s->y>>vsub,
+                     /* Up and left borders were rounded down, inject lost bits
+                      * into width and height to avoid error accumulation */
+                     FF_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
+                     FF_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
+                     s->band>>FFMIN(hsub, vsub),
+                     s->show, direct);
     }
 
-    avfilter_draw_slice(outlink, 0, inlink->h, 1);
-    avfilter_end_frame(outlink);
-    avfilter_unref_buffer(inpicref);
     if (!direct)
-        avfilter_unref_buffer(outpicref);
+        av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
 }
 
+static const AVFilterPad avfilter_vf_delogo_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .get_video_buffer = ff_null_get_video_buffer,
+        .filter_frame     = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad avfilter_vf_delogo_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
 AVFilter avfilter_vf_delogo = {
     .name          = "delogo",
     .description   = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
     .priv_size     = sizeof(DelogoContext),
+    .priv_class    = &delogo_class,
     .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}},
+    .inputs    = avfilter_vf_delogo_inputs,
+    .outputs   = avfilter_vf_delogo_outputs,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
 };