]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_overlay.c
Merge commit 'd2e56cf753a6c462041dee897d9d0c90f349988c'
[ffmpeg] / libavfilter / vf_overlay.c
index 177544e8f0d079febea1e02a8ff4b3b14bbdb7f5..108a6fce3c53a105d8196613ff3b41f27ce8d914 100644 (file)
@@ -125,6 +125,7 @@ typedef struct OverlayContext {
     int main_pix_step[4];       ///< steps per pixel for each plane of the main output
     int overlay_pix_step[4];    ///< steps per pixel for each plane of the overlay
     int hsub, vsub;             ///< chroma subsampling values
+    const AVPixFmtDescriptor *main_desc; ///< format descriptor for main input
 
     double var_values[VAR_VARS_NB];
     char *x_expr, *y_expr;
@@ -215,21 +216,23 @@ static int query_formats(AVFilterContext *ctx)
 
     /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
     static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
-        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
+        AV_PIX_FMT_NONE
     };
     static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
         AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
     };
 
     static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
-        AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
+        AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
     };
     static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
     };
 
     static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
-        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
     };
     static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
@@ -301,7 +304,7 @@ fail:
 }
 
 static const enum AVPixelFormat alpha_pix_fmts[] = {
-    AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
+    AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
     AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
     AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
 };
@@ -462,121 +465,167 @@ static void blend_image_packed_rgb(AVFilterContext *ctx,
     }
 }
 
-static void blend_image_yuv(AVFilterContext *ctx,
-                            AVFrame *dst, const AVFrame *src,
-                            int x, int y)
+static av_always_inline void blend_plane(AVFilterContext *ctx,
+                                         AVFrame *dst, const AVFrame *src,
+                                         int src_w, int src_h,
+                                         int dst_w, int dst_h,
+                                         int i, int hsub, int vsub,
+                                         int x, int y,
+                                         int main_has_alpha)
 {
-    OverlayContext *s = ctx->priv;
-    int i, imax, j, jmax, k, kmax;
-    const int src_w = src->width;
-    const int src_h = src->height;
-    const int dst_w = dst->width;
-    const int dst_h = dst->height;
-    const int main_has_alpha = s->main_has_alpha;
-
-    if (main_has_alpha) {
-        uint8_t alpha;          ///< the amount of overlay to blend on to main
-        uint8_t *s, *sa, *d, *da;
-
-        i = FFMAX(-y, 0);
-        sa = src->data[3] + i     * src->linesize[3];
-        da = dst->data[3] + (y+i) * dst->linesize[3];
-
-        for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
-            j = FFMAX(-x, 0);
-            s = sa + j;
-            d = da + x+j;
-
-            for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
-                alpha = *s;
-                if (alpha != 0 && alpha != 255) {
-                    uint8_t alpha_d = *d;
-                    alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
-                }
-                switch (alpha) {
-                case 0:
-                    break;
-                case 255:
-                    *d = *s;
-                    break;
-                default:
-                    // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
-                    *d += FAST_DIV255((255 - *d) * *s);
-                }
-                d += 1;
-                s += 1;
-            }
-            da += dst->linesize[3];
-            sa += src->linesize[3];
-        }
-    }
-    for (i = 0; i < 3; i++) {
-        int hsub = i ? s->hsub : 0;
-        int vsub = i ? s->vsub : 0;
-        int src_wp = AV_CEIL_RSHIFT(src_w, hsub);
-        int src_hp = AV_CEIL_RSHIFT(src_h, vsub);
-        int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub);
-        int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub);
-        int yp = y>>vsub;
-        int xp = x>>hsub;
-        uint8_t *s, *sp, *d, *dp, *a, *ap;
-
-        j = FFMAX(-yp, 0);
-        sp = src->data[i] + j         * src->linesize[i];
-        dp = dst->data[i] + (yp+j)    * dst->linesize[i];
-        ap = src->data[3] + (j<<vsub) * src->linesize[3];
-
-        for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
-            k = FFMAX(-xp, 0);
-            d = dp + xp+k;
-            s = sp + k;
-            a = ap + (k<<hsub);
-
-            for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
-                int alpha_v, alpha_h, alpha;
-
+    OverlayContext *ol = ctx->priv;
+    int src_wp = AV_CEIL_RSHIFT(src_w, hsub);
+    int src_hp = AV_CEIL_RSHIFT(src_h, vsub);
+    int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub);
+    int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub);
+    int yp = y>>vsub;
+    int xp = x>>hsub;
+    uint8_t *s, *sp, *d, *dp, *a, *ap;
+    int jmax, j, k, kmax;
+
+    int dst_plane  = ol->main_desc->comp[i].plane;
+    int dst_offset = ol->main_desc->comp[i].offset;
+    int dst_step   = ol->main_desc->comp[i].step;
+
+    j = FFMAX(-yp, 0);
+    sp = src->data[i] + j         * src->linesize[i];
+    dp = dst->data[dst_plane]
+                      + (yp+j)    * dst->linesize[dst_plane]
+                      + dst_offset;
+    ap = src->data[3] + (j<<vsub) * src->linesize[3];
+
+    for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
+        k = FFMAX(-xp, 0);
+        d = dp + (xp+k) * dst_step;
+        s = sp + k;
+        a = ap + (k<<hsub);
+
+        for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
+            int alpha_v, alpha_h, alpha;
+
+            // average alpha for color components, improve quality
+            if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
+                alpha = (a[0] + a[src->linesize[3]] +
+                         a[1] + a[src->linesize[3]+1]) >> 2;
+            } else if (hsub || vsub) {
+                alpha_h = hsub && k+1 < src_wp ?
+                    (a[0] + a[1]) >> 1 : a[0];
+                alpha_v = vsub && j+1 < src_hp ?
+                    (a[0] + a[src->linesize[3]]) >> 1 : a[0];
+                alpha = (alpha_v + alpha_h) >> 1;
+            } else
+                alpha = a[0];
+            // 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) {
                 // average alpha for color components, improve quality
+                uint8_t alpha_d;
                 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
-                    alpha = (a[0] + a[src->linesize[3]] +
-                             a[1] + a[src->linesize[3]+1]) >> 2;
+                    alpha_d = (d[0] + d[src->linesize[3]] +
+                               d[1] + d[src->linesize[3]+1]) >> 2;
                 } else if (hsub || vsub) {
                     alpha_h = hsub && k+1 < src_wp ?
-                        (a[0] + a[1]) >> 1 : a[0];
+                        (d[0] + d[1]) >> 1 : d[0];
                     alpha_v = vsub && j+1 < src_hp ?
-                        (a[0] + a[src->linesize[3]]) >> 1 : a[0];
-                    alpha = (alpha_v + alpha_h) >> 1;
+                        (d[0] + d[src->linesize[3]]) >> 1 : d[0];
+                    alpha_d = (alpha_v + alpha_h) >> 1;
                 } else
-                    alpha = a[0];
-                // 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) {
-                    // average alpha for color components, improve quality
-                    uint8_t alpha_d;
-                    if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
-                        alpha_d = (d[0] + d[src->linesize[3]] +
-                                   d[1] + d[src->linesize[3]+1]) >> 2;
-                    } else if (hsub || vsub) {
-                        alpha_h = hsub && k+1 < src_wp ?
-                            (d[0] + d[1]) >> 1 : d[0];
-                        alpha_v = vsub && j+1 < src_hp ?
-                            (d[0] + d[src->linesize[3]]) >> 1 : d[0];
-                        alpha_d = (alpha_v + alpha_h) >> 1;
-                    } else
-                        alpha_d = d[0];
-                    alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
-                }
-                *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
-                s++;
-                d++;
-                a += 1 << hsub;
+                    alpha_d = d[0];
+                alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
+            }
+            *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
+            s++;
+            d += dst_step;
+            a += 1 << hsub;
+        }
+        dp += dst->linesize[dst_plane];
+        sp += src->linesize[i];
+        ap += (1 << vsub) * src->linesize[3];
+    }
+}
+
+static inline void alpha_composite(const AVFrame *src, const AVFrame *dst,
+                                   int src_w, int src_h,
+                                   int dst_w, int dst_h,
+                                   int x, int y)
+{
+    uint8_t alpha;          ///< the amount of overlay to blend on to main
+    uint8_t *s, *sa, *d, *da;
+    int i, imax, j, jmax;
+
+    i = FFMAX(-y, 0);
+    sa = src->data[3] + i     * src->linesize[3];
+    da = dst->data[3] + (y+i) * dst->linesize[3];
+
+    for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
+        j = FFMAX(-x, 0);
+        s = sa + j;
+        d = da + x+j;
+
+        for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
+            alpha = *s;
+            if (alpha != 0 && alpha != 255) {
+                uint8_t alpha_d = *d;
+                alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
+            }
+            switch (alpha) {
+            case 0:
+                break;
+            case 255:
+                *d = *s;
+                break;
+            default:
+                // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
+                *d += FAST_DIV255((255 - *d) * *s);
             }
-            dp += dst->linesize[i];
-            sp += src->linesize[i];
-            ap += (1 << vsub) * src->linesize[3];
+            d += 1;
+            s += 1;
         }
+        da += dst->linesize[3];
+        sa += src->linesize[3];
     }
 }
 
+static av_always_inline void blend_image_yuv(AVFilterContext *ctx,
+                                             AVFrame *dst, const AVFrame *src,
+                                             int hsub, int vsub,
+                                             int main_has_alpha,
+                                             int x, int y)
+{
+    const int src_w = src->width;
+    const int src_h = src->height;
+    const int dst_w = dst->width;
+    const int dst_h = dst->height;
+
+    if (main_has_alpha)
+        alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y);
+
+    blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0,       0, x, y, main_has_alpha);
+    blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha);
+    blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha);
+}
+
+static void blend_image_yuv420(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
+{
+    OverlayContext *s = ctx->priv;
+
+    blend_image_yuv(ctx, dst, src, 1, 1, s->main_has_alpha, x, y);
+}
+
+static void blend_image_yuv422(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
+{
+    OverlayContext *s = ctx->priv;
+
+    blend_image_yuv(ctx, dst, src, 1, 0, s->main_has_alpha, x, y);
+}
+
+static void blend_image_yuv444(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
+{
+    OverlayContext *s = ctx->priv;
+
+    blend_image_yuv(ctx, dst, src, 0, 0, s->main_has_alpha, x, y);
+}
+
 static int config_input_main(AVFilterLink *inlink)
 {
     OverlayContext *s = inlink->dst->priv;
@@ -587,14 +636,20 @@ static int config_input_main(AVFilterLink *inlink)
     s->hsub = pix_desc->log2_chroma_w;
     s->vsub = pix_desc->log2_chroma_h;
 
+    s->main_desc = pix_desc;
+
     s->main_is_packed_rgb =
         ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
     s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
     switch (s->format) {
     case OVERLAY_FORMAT_YUV420:
+        s->blend_image = blend_image_yuv420;
+        break;
     case OVERLAY_FORMAT_YUV422:
+        s->blend_image = blend_image_yuv422;
+        break;
     case OVERLAY_FORMAT_YUV444:
-        s->blend_image = blend_image_yuv;
+        s->blend_image = blend_image_yuv444;
         break;
     case OVERLAY_FORMAT_RGB:
         s->blend_image = blend_image_packed_rgb;
@@ -612,7 +667,7 @@ static AVFrame *do_blend(AVFilterContext *ctx, AVFrame *mainpic,
     if (s->eval_mode == EVAL_MODE_FRAME) {
         int64_t pos = av_frame_get_pkt_pos(mainpic);
 
-        s->var_values[VAR_N] = inlink->frame_count;
+        s->var_values[VAR_N] = inlink->frame_count_out;
         s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
             NAN : mainpic->pts * av_q2d(inlink->time_base);
         s->var_values[VAR_POS] = pos == -1 ? NAN : pos;