[misc-filters PATCH] Add overlay filter.

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


---
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_overlay.c |  442 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 444 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_overlay.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index c2c1a21..385225d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -19,6 +19,7 @@ OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.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 e8bc771..41ca939 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (FORMAT,      format,      vf);
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
     REGISTER_FILTER (NULL,        null,        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/vf_overlay.c b/libavfilter/vf_overlay.c
new file mode 100644
index 0000000..99fa2bb
--- /dev/null
+++ b/libavfilter/vf_overlay.c
@@ -0,0 +1,442 @@
+/*
+ * 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"
+
+static const char *var_names[] = {
+    "E",
+    "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 {
+    E,
+    PI,
+    MAIN_W,
+    MAIN_H,
+    OVERLAY_W,
+    OVERLAY_H,
+    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. */
+    AVFilterPicRef *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_pic(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)
+{
+    int i;
+    OverlayContext *over = inlink->dst->priv;
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
+
+    memset(over->max_plane_step, 0, sizeof(over->max_plane_step));
+    for (i = 0; i < 4; i++) {
+        const AVComponentDescriptor *comp = &(pix_desc->comp[i]);
+        if ((comp->step_minus1+1) > over->max_plane_step[comp->plane])
+            over->max_plane_step[comp->plane] = comp->step_minus1+1;
+    }
+
+    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[VARS_NB], res;
+    int ret;
+
+    /* Finish the configuration by evaluating the expressions
+       now when both inputs are configured. */
+    var_values[E ] = M_E;
+    var_values[PI] = M_PI;
+
+    var_values[MAIN_W   ] = ctx->inputs[MAIN   ]->w;
+    var_values[MAIN_H   ] = ctx->inputs[MAIN   ]->h;
+    var_values[OVERLAY_W] = ctx->inputs[OVERLAY]->w;
+    var_values[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;
+
+    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[OVERLAY_W] > var_values[MAIN_W] ||
+        over->y + var_values[OVERLAY_H] > var_values[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[OVERLAY_W]),
+               (int)(over->y + var_values[OVERLAY_H]),
+               (int)var_values[MAIN_W], (int)var_values[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 void shift_input(OverlayContext *over, int idx)
+{
+    assert(over->pics[idx][PREV  ]);
+    assert(over->pics[idx][QUEUED]);
+
+    avfilter_unref_pic(over->pics[idx][PREV]);
+
+    over->pics[idx][PREV  ] = over->pics[idx][QUEUED];
+    over->pics[idx][QUEUED] = NULL;
+}
+
+static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
+{
+    OverlayContext *over = link->dst->priv;
+    /* There shouldn't be any previous queued frame in this queue */
+    assert(!over->pics[link->dstpad][1]);
+    if (over->pics[link->dstpad][0]) {
+        /* Queue the new frame */
+        over->pics[link->dstpad][1] = picref;
+    } else {
+        /* No previous unused frame, take this one into use directly */
+        over->pics[link->dstpad][0] = picref;
+    }
+}
+
+static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
+
+static void 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 */
+
+    if     (over->pics[MAIN][QUEUED]->pts == over->pics[OVERLAY][QUEUED]->pts) return 2;
+    return (over->pics[MAIN][QUEUED]->pts < over->pics[OVERLAY][QUEUED]->pts) ? MAIN : OVERLAY;
+}
+
+static void overlay_image_rgb(AVFilterPicRef *dst, int x, int y,
+                              AVFilterPicRef *src, int w, int h, int step)
+{
+    AVPicture pic;
+
+    memcpy(&pic, &dst->data, sizeof(AVPicture));
+    pic.data[0] += x * step;
+    pic.data[0] += y * pic.linesize[0];
+
+    if (src->pic->format == PIX_FMT_BGRA) {
+        for (y = 0; y < h; y++) {
+                  uint8_t *optr = pic .data[0] + y * pic .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_picture_copy(&pic, (AVPicture *)src->data, dst->pic->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(AVFilterPicRef *dst, int x, int y,
+                              AVFilterPicRef *src, int w, int h,
+                              const int plane_step[4], int hsub, int vsub)
+{
+    AVPicture pic;
+    int i;
+
+    memcpy(&pic, &dst->data, sizeof(AVPicture));
+    for (i = 0; i < 4; i ++) {
+        if (pic.data[i]) {
+            int x_off = x;
+            int y_off = y;
+            if (i == 1 || i == 2) {
+                x_off >>= hsub;
+                y_off >>= vsub;
+            }
+            pic.data[i] += x_off * plane_step[i];
+            pic.data[i] += y_off * pic.linesize[i];
+        }
+    }
+
+    if (src->pic->format == PIX_FMT_YUVA420P) {
+        int chroma_w = w>>hsub;
+        int chroma_h = h>>vsub;
+        assert(dst->pic->format == PIX_FMT_YUV420P);
+        copy_blended(pic.data[0], pic.linesize[0], src->data[0], src->linesize[0], src->data[3], src->linesize[3], w       , h       , 0   , 0   );
+        copy_blended(pic.data[1], pic.linesize[1], src->data[1], src->linesize[1], src->data[3], src->linesize[3], chroma_w, chroma_h, hsub, vsub);
+        copy_blended(pic.data[2], pic.linesize[2], src->data[2], src->linesize[2], src->data[3], src->linesize[3], chroma_w, chroma_h, hsub, vsub);
+    } else {
+        av_picture_copy(&pic, (AVPicture *)src->data, dst->pic->format, w, h);
+    }
+}
+
+static void overlay_image(AVFilterPicRef *dst, int x, int y,
+                          AVFilterPicRef *src, int w, int h,
+                          const int max_plane_step[4], int hsub, int vsub)
+{
+    if (dst->pic->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 void dprintf_picref(void *ctx, AVFilterPicRef *picref, int end)
+{
+    dprintf(ctx,
+            "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64" a:%d/%d s:%dx%d]%s",
+            picref,
+            picref->data    [0], picref->data    [1], picref->data    [2], picref->data    [3],
+            picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
+            picref->pts, picref->pos,
+            picref->pixel_aspect.num, picref->pixel_aspect.den, picref->w, picref->h,
+            end ? "\n" : "");
+}
+
+static int request_frame(AVFilterLink *link)
+{
+    AVFilterPicRef *pic;
+    OverlayContext *over = link->src->priv;
+    int idx;
+    int x, y, w, h;
+
+#define DPRINT_PIC(i, j) \
+    dprintf(link->src, "pics[%-7s][%-7s]:", #i, #j);                    \
+    if (over->pics[i][j]) dprintf_picref(link->src, over->pics[i][j], 1); \
+    else                  dprintf(link->src, "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(link->src->inputs[MAIN]))
+            return AVERROR_EOF;
+        if (!over->pics[OVERLAY][PREV] && avfilter_request_frame(link->src->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(link->src->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 */
+    pic = avfilter_get_video_buffer(link, AV_PERM_WRITE, link->w, link->h);
+    if (over->pics[MAIN][PREV]) {
+        avfilter_copy_picref_props(pic, over->pics[MAIN][PREV]);
+        overlay_image(pic, 0, 0, over->pics[MAIN][PREV], link->w, link->h,
+                      over->max_plane_step, over->hsub, over->vsub);
+    }
+    x = FFMIN(over->x, link->w-1);
+    y = FFMIN(over->y, link->h-1);
+    w = FFMIN(link->w-x, over->pics[OVERLAY][PREV]->w);
+    h = FFMIN(link->h-y, over->pics[OVERLAY][PREV]->h);
+    if (over->pics[OVERLAY][PREV])
+        overlay_image(pic, 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 */
+    pic->pts = FFMAX(over->pics[MAIN][PREV]->pts, over->pics[OVERLAY][PREV]->pts);
+
+    /* and send it to the next filter */
+    avfilter_start_frame(link, avfilter_ref_pic(pic, ~0));
+    avfilter_draw_slice (link, 0, pic->h, 1);
+    avfilter_end_frame  (link);
+    avfilter_unref_pic(pic);
+
+    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      = draw_slice,
+                                    .end_frame       = 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      = draw_slice,
+                                    .end_frame       = end_frame,
+                                    .min_perms       = AV_PERM_READ,
+                                    .rej_perms       = AV_PERM_REUSE2, },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .request_frame   = request_frame, },
+                                  { .name = NULL}},
+};
+
-- 
1.6.0.4


--0eh6TmSyL6TZE2Uz--



More information about the ffmpeg-devel mailing list