avfilter/af_sofalizer: speed up fast convolution
[ffmpeg.git] / libavfilter / vf_delogo.c
index e882dd5..065d093 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
  * Copyright (c) 2011 Stefano Sabatini
- * Copyright (c) 2013 Jean Delvare <khali@linux-fr.org>
+ * Copyright (c) 2013, 2015 Jean Delvare <jdelvare@suse.com>
  *
  * This file is part of FFmpeg.
  *
@@ -58,13 +58,14 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
                          uint8_t *src, int src_linesize,
                          int w, int h, AVRational sar,
                          int logo_x, int logo_y, int logo_w, int logo_h,
-                         int band, int show, int direct)
+                         unsigned int band, int show, int direct)
 {
-    int x, y, dist;
-    uint64_t interp, weightl, weightr, weightt, weightb;
+    int x, y;
+    uint64_t interp, weightl, weightr, weightt, weightb, weight;
     uint8_t *xdst, *xsrc;
 
     uint8_t *topleft, *botleft, *topright;
+    unsigned int left_sample, right_sample;
     int xclipl, xclipr, yclipt, yclipb;
     int logo_x1, logo_x2, logo_y1, logo_y2;
 
@@ -74,13 +75,13 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
     yclipb = FFMAX(logo_y+logo_h-h, 0);
 
     logo_x1 = logo_x + xclipl;
-    logo_x2 = logo_x + logo_w - xclipr;
+    logo_x2 = logo_x + logo_w - xclipr - 1;
     logo_y1 = logo_y + yclipt;
-    logo_y2 = logo_y + logo_h - yclipb;
+    logo_y2 = logo_y + logo_h - yclipb - 1;
 
-    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;
+    topleft  = src+logo_y1 * src_linesize+logo_x1;
+    topright = src+logo_y1 * src_linesize+logo_x2;
+    botleft  = src+logo_y2 * src_linesize+logo_x1;
 
     if (!direct)
         av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
@@ -88,40 +89,51 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
     dst += (logo_y1 + 1) * dst_linesize;
     src += (logo_y1 + 1) * src_linesize;
 
-    for (y = logo_y1+1; y < logo_y2-1; y++) {
+    for (y = logo_y1+1; y < logo_y2; y++) {
+        left_sample = topleft[src_linesize*(y-logo_y1)]   +
+                      topleft[src_linesize*(y-logo_y1-1)] +
+                      topleft[src_linesize*(y-logo_y1+1)];
+        right_sample = topright[src_linesize*(y-logo_y1)]   +
+                       topright[src_linesize*(y-logo_y1-1)] +
+                       topright[src_linesize*(y-logo_y1+1)];
+
         for (x = logo_x1+1,
              xdst = dst+logo_x1+1,
-             xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
+             xsrc = src+logo_x1+1; x < logo_x2; x++, xdst++, xsrc++) {
+
+            if (show && (y == logo_y1+1 || y == logo_y2-1 ||
+                         x == logo_x1+1 || x == logo_x2-1)) {
+                *xdst = 0;
+                continue;
+            }
 
             /* 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;
+            weightl = (uint64_t)              (logo_x2-x) * (y-logo_y1) * (logo_y2-y) * sar.den;
+            weightr = (uint64_t)(x-logo_x1)               * (y-logo_y1) * (logo_y2-y) * sar.den;
+            weightt = (uint64_t)(x-logo_x1) * (logo_x2-x)               * (logo_y2-y) * sar.num;
+            weightb = (uint64_t)(x-logo_x1) * (logo_x2-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)])  * weightl
+                left_sample * weightl
                 +
-                (topright[src_linesize*(y-logo_y-yclipt)]    +
-                 topright[src_linesize*(y-logo_y-1-yclipt)]  +
-                 topright[src_linesize*(y-logo_y+1-yclipt)]) * weightr
+                right_sample * weightr
                 +
-                (topleft[x-logo_x-xclipl]    +
-                 topleft[x-logo_x-1-xclipl]  +
-                 topleft[x-logo_x+1-xclipl]) * weightt
+                (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]) * weightb;
-            interp /= (weightl + weightr + weightt + weightb) * 3U;
+                (botleft[x-logo_x1]    +
+                 botleft[x-logo_x1-1]  +
+                 botleft[x-logo_x1+1]) * weightb;
+            weight = (weightl + weightr + weightt + weightb) * 3U;
+            interp = ROUNDED_DIV(interp, weight);
 
             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;
+                unsigned dist = 0;
+
                 if      (x < logo_x+band)
                     dist = FFMAX(dist, logo_x-x+band);
                 else if (x >= logo_x+logo_w-band)
@@ -133,8 +145,6 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
                     dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
 
                 *xdst = (*xsrc*dist + interp*(band-dist))/band;
-                if (show && (dist == band-1))
-                    *xdst = 0;
             }
         }
 
@@ -143,7 +153,7 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
     }
 }
 
-typedef struct {
+typedef struct DelogoContext {
     const AVClass *class;
     int x, y, w, h, band, show;
 }  DelogoContext;
@@ -156,10 +166,13 @@ static const AVOption delogo_options[]= {
     { "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 },
+#if LIBAVFILTER_VERSION_MAJOR < 7
+    /* Actual default value for band/t is 1, set in init */
+    { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  0 },  0, INT_MAX, FLAGS },
+    { "t",    "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  0 },  0, INT_MAX, FLAGS },
+#endif
+    { "show", "show delogo area",          OFFSET(show), AV_OPT_TYPE_BOOL,{ .i64 =  0 },  0, 1,       FLAGS },
+    { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(delogo);
@@ -172,9 +185,10 @@ static int query_formats(AVFilterContext *ctx)
         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
         AV_PIX_FMT_NONE
     };
-
-    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
-    return 0;
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
 }
 
 static av_cold int init(AVFilterContext *ctx)
@@ -191,11 +205,16 @@ static av_cold int init(AVFilterContext *ctx)
     CHECK_UNSET_OPT(w);
     CHECK_UNSET_OPT(h);
 
-    if (s->band < 0 || s->show) {
-        s->show = 1;
-        s->band = 4;
+#if LIBAVFILTER_VERSION_MAJOR < 7
+    if (s->band == 0) { /* Unset, use default */
+        av_log(ctx, AV_LOG_WARNING, "Note: default band value was changed from 4 to 1.\n");
+        s->band = 1;
+    } else if (s->band != 1) {
+        av_log(ctx, AV_LOG_WARNING, "Option band is deprecated.\n");
     }
-
+#else
+    s->band = 1;
+#endif
     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);
 
@@ -207,6 +226,20 @@ static av_cold int init(AVFilterContext *ctx)
     return 0;
 }
 
+static int config_input(AVFilterLink *inlink)
+{
+    DelogoContext *s = inlink->dst->priv;
+
+    /* Check whether the logo area fits in the frame */
+    if (s->x + (s->band - 1) < 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
+        s->y + (s->band - 1) < 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
+        av_log(s, AV_LOG_ERROR, "Logo area is outside of the frame.\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     DelogoContext *s = inlink->dst->priv;
@@ -237,19 +270,19 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     if (!sar.num)
         sar.num = sar.den = 1;
 
-    for (plane = 0; plane < 4 && in->data[plane]; plane++) {
+    for (plane = 0; plane < desc->nb_components; plane++) {
         int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
         int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
 
         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),
+                     AV_CEIL_RSHIFT(inlink->w, hsub),
+                     AV_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),
+                     AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
+                     AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub),
                      s->band>>FFMIN(hsub, vsub),
                      s->show, direct);
     }
@@ -262,10 +295,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
 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,
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
     },
     { NULL }
 };
@@ -278,15 +311,14 @@ static const AVFilterPad avfilter_vf_delogo_outputs[] = {
     { NULL }
 };
 
-AVFilter avfilter_vf_delogo = {
+AVFilter ff_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    = avfilter_vf_delogo_inputs,
-    .outputs   = avfilter_vf_delogo_outputs,
-    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+    .inputs        = avfilter_vf_delogo_inputs,
+    .outputs       = avfilter_vf_delogo_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
 };