[PATCH 12/12] Add overlay filter.

Stefano Sabatini stefano.sabatini-lala
Mon Jun 28 19:30:15 CEST 2010


---
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/avfilter.c   |    2 +-
 libavfilter/vf_overlay.c |  459 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 462 insertions(+), 1 deletions(-)
 create mode 100644 libavfilter/vf_overlay.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 20f9c1e..7018a90 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -31,6 +31,7 @@ OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
+OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index c00160f..d716330 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -51,6 +51,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
     REGISTER_FILTER (NULL,        null,        vf);
     REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
+    REGISTER_FILTER (OVERLAY,     overlay,     vf);
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index bbe8d78..949207e 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -19,7 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/* #define DEBUG */
+#define DEBUG
 
 #include "libavcodec/audioconvert.c"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_overlay.c b/libavfilter/vf_overlay.c
new file mode 100644
index 0000000..a5f5c96
--- /dev/null
+++ b/libavfilter/vf_overlay.c
@@ -0,0 +1,459 @@
+/*
+ * Copyright (C) 2010 Stefano Sabatini
+ * Copyright (C) 2010 Baptiste Coudurier
+ * Copyright (C) 2007 Bobby Bingham
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ * overlay one video on top of another
+ */
+
+#define DEBUG
+
+#include "avfilter.h"
+#include "libavutil/eval.h"
+#include "libavutil/avstring.h"
+#include "libavutil/pixdesc.h"
+#include "libavcore/imgutils.h"
+#include "internal.h"
+
+static const char *var_names[] = {
+    "E",
+    "PHI",
+    "PI",
+    "main_w",    ///< width  of the main    video
+    "main_h",    ///< height of the main    video
+    "overlay_w", ///< width  of the overlay video
+    "overlay_h", ///< height of the overlay video
+    NULL
+};
+
+enum var_name {
+    VAR_E,
+    VAR_PHI,
+    VAR_PI,
+    VAR_MAIN_W,
+    VAR_MAIN_H,
+    VAR_OVERLAY_W,
+    VAR_OVERLAY_H,
+    VAR_VARS_NB
+};
+
+#define MAIN    0
+#define OVERLAY 1
+#define PREV    0
+#define QUEUED  1
+
+typedef struct {
+    int x, y;                   ///< position of overlayed picture
+
+    /** pics[MAIN   ][0..1] are pictures for the main image.
+     *  pics[OVERLAY][0..1] are pictures for the overlay image.
+     *  pics[x][PREV  ]     are previously output images.
+     *  pics[x][QUEUED]     are queued, yet unused frames for each input. */
+    AVFilterBufferRef *pics[2][2];
+
+    int max_plane_step[4];      ///< steps per pixel for each plane
+    int hsub, vsub;             ///< chroma subsampling values
+
+    char x_expr[256], y_expr[256];
+} OverlayContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    OverlayContext *over = ctx->priv;
+
+    av_strlcpy(over->x_expr, "0", sizeof(over->x_expr));
+    av_strlcpy(over->y_expr, "0", sizeof(over->y_expr));
+
+    if (args)
+        sscanf(args, "%255[^:]:%255[^:]", over->x_expr, over->y_expr);
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    OverlayContext *over = ctx->priv;
+    int i, j;
+
+    for (i = 0; i < 2; i ++)
+        for (j = 0; j < 2; j ++)
+            if (over->pics[i][j])
+                avfilter_unref_buffer(over->pics[i][j]);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    const enum PixelFormat inout_pix_fmts[] = { PIX_FMT_YUV420P,  PIX_FMT_NONE };
+    const enum PixelFormat blend_pix_fmts[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
+    AVFilterFormats *inout_formats = avfilter_make_format_list(inout_pix_fmts);
+    AVFilterFormats *blend_formats = avfilter_make_format_list(blend_pix_fmts);
+
+    avfilter_formats_ref(inout_formats, &ctx->inputs [MAIN   ]->out_formats);
+    avfilter_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
+    avfilter_formats_ref(inout_formats, &ctx->outputs[MAIN   ]->in_formats );
+
+    return 0;
+}
+
+static int config_input_main(AVFilterLink *inlink)
+{
+    OverlayContext *over = inlink->dst->priv;
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
+
+    av_image_fill_max_pixsteps(over->max_plane_step, NULL, pix_desc);
+    over->hsub = pix_desc->log2_chroma_w;
+    over->vsub = pix_desc->log2_chroma_h;
+
+    return 0;
+}
+
+static int config_input_overlay(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx  = inlink->dst;
+    OverlayContext  *over = inlink->dst->priv;
+    char *expr;
+    double var_values[VAR_VARS_NB], res;
+    int ret;
+
+    /* Finish the configuration by evaluating the expressions
+       now when both inputs are configured. */
+    var_values[VAR_E  ] = M_E;
+    var_values[VAR_PHI] = M_PHI;
+    var_values[VAR_PI ] = M_PI;
+
+    var_values[VAR_MAIN_W   ] = ctx->inputs[MAIN   ]->w;
+    var_values[VAR_MAIN_H   ] = ctx->inputs[MAIN   ]->h;
+    var_values[VAR_OVERLAY_W] = ctx->inputs[OVERLAY]->w;
+    var_values[VAR_OVERLAY_H] = ctx->inputs[OVERLAY]->h;
+
+    if ((ret = av_parse_and_eval_expr(&res, (expr = over->x_expr), var_names, var_values,
+                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
+        goto fail;
+    over->x = res;
+    if ((ret = av_parse_and_eval_expr(&res, (expr = over->y_expr), var_names, var_values,
+                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)))
+        goto fail;
+    over->y = res;
+    /* x may depend on y */
+    if ((ret = av_parse_and_eval_expr(&res, (expr = over->x_expr), var_names, var_values,
+                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
+        goto fail;
+    over->x = res;
+
+    av_log(ctx, AV_LOG_INFO,
+           "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
+           ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
+           av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
+           over->x, over->y,
+           ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
+           av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
+
+    if (over->x < 0 || over->y < 0                           ||
+        over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
+        over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
+               over->x, over->y,
+               (int)(over->x + var_values[VAR_OVERLAY_W]),
+               (int)(over->y + var_values[VAR_OVERLAY_H]),
+               (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
+        return AVERROR(EINVAL);
+    }
+    return 0;
+
+fail:
+    av_log(NULL, AV_LOG_ERROR,
+           "Error when evaluating the expression '%s'\n", expr);
+    return ret;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+
+    /* choose the output timebase */
+    AVRational tb1 = ctx->input_pads[MAIN   ].time_base;
+    AVRational tb2 = ctx->input_pads[OVERLAY].time_base;
+    AVRational tb;
+    int exact = av_reduce(&tb.num, &tb.den,
+                          tb1.num * tb2.num, tb1.den * tb2.den, INT_MAX);
+    ctx->input_pads[MAIN].time_base = ctx->input_pads[OVERLAY].time_base = tb;
+
+    av_log(ctx, AV_LOG_INFO,
+           "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
+           tb1.num, tb1.den, tb2.num, tb2.den, tb.num, tb.den, exact);
+    if (!exact)
+        av_log(ctx, AV_LOG_WARNING,
+               "Timestamp conversion inexact, timestamp information loss may occurr\n");
+
+    outlink->w = ctx->inputs[MAIN]->w;
+    outlink->h = ctx->inputs[MAIN]->h;
+
+    return 0;
+}
+
+static void shift_input(OverlayContext *over, int idx)
+{
+    assert(over->pics[idx][PREV  ]);
+    assert(over->pics[idx][QUEUED]);
+
+    avfilter_unref_buffer(over->pics[idx][PREV]);
+
+    over->pics[idx][PREV  ] = over->pics[idx][QUEUED];
+    over->pics[idx][QUEUED] = NULL;
+}
+
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+{
+    OverlayContext *over = inlink->dst->priv;
+    /* There shouldn't be any previous queued frame in this queue */
+    assert(!over->pics[inlink->dstpad - inlink->dst->input_pads][1]);
+    if (over->pics[inlink->dstpad - inlink->dst->input_pads][0]) {
+        /* Queue the new frame */
+        over->pics[inlink->dstpad - inlink->dst->input_pads][1] = picref;
+    } else {
+        /* No previous unused frame, take this one into use directly */
+        over->pics[inlink->dstpad - inlink->dst->input_pads][0] = picref;
+    }
+}
+
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void null_end_frame(AVFilterLink *link) { }
+
+static int lower_timestamp(OverlayContext *over)
+{
+    if (!over->pics[MAIN   ][PREV  ] &&
+        !over->pics[OVERLAY][PREV  ]) return 2;       /* no previous frames */
+    if (!over->pics[MAIN   ][QUEUED]) return MAIN;    /* no queued frame from main */
+    if (!over->pics[OVERLAY][QUEUED]) return OVERLAY; /* no queued frame from overlay */
+
+    return over->pics[MAIN][QUEUED]->pts == over->pics[OVERLAY][QUEUED]->pts ? 2    :
+           over->pics[MAIN][QUEUED]->pts <  over->pics[OVERLAY][QUEUED]->pts ? MAIN : OVERLAY;
+}
+
+static void overlay_image_rgb(AVFilterBufferRef *dst, int x, int y,
+                              AVFilterBufferRef *src, int w, int h, int step)
+{
+    dst->data[0] += x * step;
+    dst->data[0] += y * dst->linesize[0];
+
+    if (src->format == PIX_FMT_BGRA) {
+        for (y = 0; y < h; y++) {
+                  uint8_t *optr = dst->data[0] + y * dst->linesize[0];
+            const uint8_t *iptr = src->data[0] + y * src->linesize[0];
+            for (x = 0; x < w; x++) {
+                uint8_t a = iptr[3];
+                optr[0] = (optr[0] * (0xff - a) + iptr[0] * a + 128) >> 8;
+                optr[1] = (optr[1] * (0xff - a) + iptr[1] * a + 128) >> 8;
+                optr[2] = (optr[2] * (0xff - a) + iptr[2] * a + 128) >> 8;
+                iptr += step+1;
+                optr += step;
+            }
+        }
+    } else {
+        av_image_copy(dst->data, dst->linesize,
+                      src->data, src->linesize,
+                      dst->format, w, h);
+    }
+}
+
+static void copy_blended(uint8_t *out, int out_linesize,
+                         const uint8_t *in,    int in_linesize,
+                         const uint8_t *alpha, int alpha_linesize,
+                         int w, int h, int hsub, int vsub)
+{
+    int y, x;
+
+    for (y = 0; y < h; y++) {
+              uint8_t *optr = out   + y         * out_linesize;
+        const uint8_t *iptr = in    + y         * in_linesize;
+        const uint8_t *aptr = alpha + (y<<vsub) * alpha_linesize;
+        for (x = 0; x < w; x++) {
+            uint8_t a = *aptr;
+            *optr = (*optr * (0xff - a) + *iptr * a + 128) >> 8;
+            optr++;
+            iptr++;
+            aptr += 1 << hsub;
+        }
+    }
+}
+
+static void overlay_image_yuv(AVFilterBufferRef *dst, int x, int y,
+                              AVFilterBufferRef *src, int w, int h,
+                              const int plane_step[4], int hsub, int vsub)
+{
+    int i;
+
+    for (i = 0; i < 4; i ++) {
+        if (dst->data[i]) {
+            int x_off = x;
+            int y_off = y;
+            if (i == 1 || i == 2) {
+                x_off >>= hsub;
+                y_off >>= vsub;
+            }
+            dst->data[i] += x_off * plane_step[i];
+            dst->data[i] += y_off * dst->linesize[i];
+        }
+    }
+
+    if (src->format == PIX_FMT_YUVA420P) {
+        int chroma_w = w>>hsub;
+        int chroma_h = h>>vsub;
+        assert(dst->format == PIX_FMT_YUV420P);
+        copy_blended(dst->data[0], dst->linesize[0], src->data[0], src->linesize[0], src->data[3], src->linesize[3], w       , h       , 0   , 0   );
+        copy_blended(dst->data[1], dst->linesize[1], src->data[1], src->linesize[1], src->data[3], src->linesize[3], chroma_w, chroma_h, hsub, vsub);
+        copy_blended(dst->data[2], dst->linesize[2], src->data[2], src->linesize[2], src->data[3], src->linesize[3], chroma_w, chroma_h, hsub, vsub);
+    } else {
+        av_image_copy(dst->data, dst->linesize, src->data, src->linesize, dst->format, w, h);
+    }
+}
+
+static void overlay_image(AVFilterBufferRef *dst, int x, int y,
+                          AVFilterBufferRef *src, int w, int h,
+                          const int max_plane_step[4], int hsub, int vsub)
+{
+    if (dst->format == PIX_FMT_YUV420P)
+        overlay_image_yuv(dst, x, y, src, w, h, max_plane_step, hsub, vsub);
+    else
+        overlay_image_rgb(dst, x, y, src, w, h, max_plane_step[0]);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterBufferRef *picref;
+    OverlayContext *over = ctx->priv;
+    int idx;
+    int x, y, w, h;
+
+#define DPRINT_PIC(i, j) \
+    dprintf(ctx, "pics[%-7s][%-7s]: ", #i, #j);                         \
+    if (over->pics[i][j]) {                                             \
+        ff_dprintf_ref(ctx, over->pics[i][j], 1);                       \
+    } else                                                              \
+        dprintf(ctx, "null\n")
+
+    DPRINT_PIC(MAIN   , PREV  );
+    DPRINT_PIC(MAIN   , QUEUED);
+    DPRINT_PIC(OVERLAY, PREV  );
+    DPRINT_PIC(OVERLAY, QUEUED);
+
+    if (!over->pics[MAIN][PREV] || !over->pics[OVERLAY][PREV]) {
+        /* No frame output yet, we need one frame from each input */
+        if (!over->pics[MAIN   ][PREV] && avfilter_request_frame(ctx->inputs[MAIN]))
+            return AVERROR_EOF;
+        if (!over->pics[OVERLAY][PREV] && avfilter_request_frame(ctx->inputs[OVERLAY]))
+            return AVERROR_EOF;
+    } else {
+        int eof = 0;
+
+        /* Try pulling a new candidate from each input unless we already have one */
+        for (idx = 0; idx < 2; idx++) {
+            if (!over->pics[idx][QUEUED] &&
+                 avfilter_request_frame(ctx->inputs[idx]))
+                eof++;
+        }
+        if (eof == 2)
+            return AVERROR_EOF; /* No new candidates in any input; EOF */
+
+        /* At least one new frame */
+        assert(over->pics[MAIN][QUEUED] || over->pics[OVERLAY][QUEUED]);
+
+        if (over->pics[MAIN][QUEUED] && over->pics[OVERLAY][QUEUED]) {
+            /* Neither one of the inputs has finished */
+            if ((idx = lower_timestamp(over)) == 2) {
+                shift_input(over, MAIN);
+                shift_input(over, OVERLAY);
+            } else
+                shift_input(over, idx);
+        } else if (over->pics[MAIN][QUEUED]) {
+            /* Use the single new input frame */
+            shift_input(over, MAIN);
+        } else {
+            assert(over->pics[OVERLAY][QUEUED]);
+            shift_input(over, OVERLAY);
+        }
+    }
+
+    /* we draw the output frame */
+    picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    if (over->pics[MAIN][PREV]) {
+        avfilter_copy_buffer_ref_props(picref, over->pics[MAIN][PREV]);
+        overlay_image(picref, 0, 0, over->pics[MAIN][PREV], outlink->w, outlink->h,
+                      over->max_plane_step, over->hsub, over->vsub);
+    }
+    x = FFMIN(over->x, outlink->w-1);
+    y = FFMIN(over->y, outlink->h-1);
+    w = FFMIN(outlink->w-x, over->pics[OVERLAY][PREV]->video->w);
+    h = FFMIN(outlink->h-y, over->pics[OVERLAY][PREV]->video->h);
+    if (over->pics[OVERLAY][PREV])
+        overlay_image(picref, x, y, over->pics[OVERLAY][MAIN], w, h,
+                      over->max_plane_step, over->hsub, over->vsub);
+
+    /* we give the output frame the higher of the two current pts values */
+    picref->pts = FFMAX(over->pics[MAIN][PREV]->pts, over->pics[OVERLAY][PREV]->pts);
+
+    /* and send it to the next filter */
+    avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
+    avfilter_draw_slice (outlink, 0, picref->video->h, 1);
+    avfilter_end_frame  (outlink);
+    avfilter_unref_buffer(picref);
+
+    return 0;
+}
+
+AVFilter avfilter_vf_overlay = {
+    .name      = "overlay",
+    .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
+
+    .init      = init,
+    .uninit    = uninit,
+
+    .priv_size = sizeof(OverlayContext),
+
+    .query_formats = query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "main",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame     = start_frame,
+                                    .config_props    = config_input_main,
+                                    .draw_slice      = null_draw_slice,
+                                    .end_frame       = null_end_frame,
+                                    .min_perms       = AV_PERM_READ,
+                                    .rej_perms       = AV_PERM_REUSE2, },
+                                  { .name            = "overlay",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .start_frame     = start_frame,
+                                    .config_props    = config_input_overlay,
+                                    .draw_slice      = null_draw_slice,
+                                    .end_frame       = null_end_frame,
+                                    .min_perms       = AV_PERM_READ,
+                                    .rej_perms       = AV_PERM_REUSE2, },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .config_props    = config_output,
+                                    .request_frame   = request_frame, },
+                                  { .name = NULL}},
+};
-- 
1.7.1


--ibTvN161/egqYuK8--



More information about the ffmpeg-devel mailing list