]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_overlay.c
mandelbrot: only check periodicity once for each period instead of twice.
[ffmpeg] / libavfilter / vf_overlay.c
index db5398551e65f15c1bac911a1646b7889b0c3b4e..062783ea560b41d33a45dd1a328f31cc58dab8c6 100644 (file)
@@ -35,7 +35,7 @@
 #include "internal.h"
 #include "drawutils.h"
 
-static const char *var_names[] = {
+static const char * const var_names[] = {
     "main_w",    "W", ///< width  of the main    video
     "main_h",    "H", ///< height of the main    video
     "overlay_w", "w", ///< width  of the overlay video
@@ -183,7 +183,7 @@ static int query_formats(AVFilterContext *ctx)
     return 0;
 }
 
-static enum PixelFormat alpha_pix_fmts[] = {
+static const enum PixelFormat alpha_pix_fmts[] = {
     PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
     PIX_FMT_BGRA, PIX_FMT_NONE
 };
@@ -360,18 +360,35 @@ static void blend_slice(AVFilterContext *ctx,
         const int dr = over->main_rgba_map[R];
         const int dg = over->main_rgba_map[G];
         const int db = over->main_rgba_map[B];
+        const int da = over->main_rgba_map[A];
         const int dstep = over->main_pix_step[0];
         const int sr = over->overlay_rgba_map[R];
         const int sg = over->overlay_rgba_map[G];
         const int sb = over->overlay_rgba_map[B];
         const int sa = over->overlay_rgba_map[A];
         const int sstep = over->overlay_pix_step[0];
+        const int main_has_alpha = over->main_has_alpha;
         if (slice_y > y)
             sp += (slice_y - y) * src->linesize[0];
         for (i = 0; i < height; i++) {
             uint8_t *d = dp, *s = sp;
             for (j = 0; j < width; j++) {
                 alpha = s[sa];
+
+                // if the main channel has an alpha channel, alpha has to be calculated
+                // to create an un-premultiplied (straight) alpha value
+                if (main_has_alpha && alpha != 0 && alpha != 255) {
+                    // apply the general equation:
+                    // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
+                    alpha =
+                        // the next line is a faster version of: 255 * 255 * alpha
+                        ( (alpha << 16) - (alpha << 9) + alpha )
+                        /
+                        // the next line is a faster version of: 255 * (alpha + d[da])
+                        ( ((alpha + d[da]) << 8 ) - (alpha + d[da])
+                          - d[da] * alpha );
+                }
+
                 switch (alpha) {
                 case 0:
                     break;
@@ -387,6 +404,18 @@ static void blend_slice(AVFilterContext *ctx,
                     d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
                     d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
                 }
+                if (main_has_alpha) {
+                    switch (alpha) {
+                    case 0:
+                        break;
+                    case 255:
+                        d[da] = s[sa];
+                        break;
+                    default:
+                        // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
+                        d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
+                    }
+                }
                 d += dstep;
                 s += sstep;
             }
@@ -423,7 +452,8 @@ static void blend_slice(AVFilterContext *ctx,
                         alpha = (alpha_v + alpha_h) >> 1;
                     } else
                         alpha = a[0];
-                    *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
+                    *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
+                    s++;
                     d++;
                     a += 1 << hsub;
                 }
@@ -473,7 +503,7 @@ AVFilter avfilter_vf_overlay = {
 
     .query_formats = query_formats,
 
-    .inputs    = (AVFilterPad[]) {{ .name            = "main",
+    .inputs    = (const AVFilterPad[]) {{ .name      = "main",
                                     .type            = AVMEDIA_TYPE_VIDEO,
                                     .start_frame     = start_frame,
                                     .get_video_buffer= get_video_buffer,
@@ -491,7 +521,7 @@ AVFilter avfilter_vf_overlay = {
                                     .min_perms       = AV_PERM_READ,
                                     .rej_perms       = AV_PERM_REUSE2, },
                                   { .name = NULL}},
-    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+    .outputs   = (const AVFilterPad[]) {{ .name      = "default",
                                     .type            = AVMEDIA_TYPE_VIDEO,
                                     .config_props    = config_output, },
                                   { .name = NULL}},