• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/vf_overlay.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Stefano Sabatini
00003  * Copyright (c) 2010 Baptiste Coudurier
00004  * Copyright (c) 2007 Bobby Bingham
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "avfilter.h"
00029 #include "libavutil/eval.h"
00030 #include "libavutil/avstring.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/mathematics.h"
00034 #include "internal.h"
00035 
00036 static const char *var_names[] = {
00037     "E",
00038     "PHI",
00039     "PI",
00040     "main_w",    "W", 
00041     "main_h",    "H", 
00042     "overlay_w", "w", 
00043     "overlay_h", "h", 
00044     NULL
00045 };
00046 
00047 enum var_name {
00048     VAR_E,
00049     VAR_PHI,
00050     VAR_PI,
00051     VAR_MAIN_W,    VAR_MW,
00052     VAR_MAIN_H,    VAR_MH,
00053     VAR_OVERLAY_W, VAR_OW,
00054     VAR_OVERLAY_H, VAR_OH,
00055     VAR_VARS_NB
00056 };
00057 
00058 #define MAIN    0
00059 #define OVERLAY 1
00060 
00061 typedef struct {
00062     int x, y;                   
00063 
00064     AVFilterBufferRef *overpicref;
00065 
00066     int max_plane_step[4];      
00067     int hsub, vsub;             
00068 
00069     char x_expr[256], y_expr[256];
00070 } OverlayContext;
00071 
00072 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00073 {
00074     OverlayContext *over = ctx->priv;
00075 
00076     av_strlcpy(over->x_expr, "0", sizeof(over->x_expr));
00077     av_strlcpy(over->y_expr, "0", sizeof(over->y_expr));
00078 
00079     if (args)
00080         sscanf(args, "%255[^:]:%255[^:]", over->x_expr, over->y_expr);
00081 
00082     return 0;
00083 }
00084 
00085 static av_cold void uninit(AVFilterContext *ctx)
00086 {
00087     OverlayContext *over = ctx->priv;
00088 
00089     if (over->overpicref)
00090         avfilter_unref_buffer(over->overpicref);
00091 }
00092 
00093 static int query_formats(AVFilterContext *ctx)
00094 {
00095     const enum PixelFormat inout_pix_fmts[] = { PIX_FMT_YUV420P,  PIX_FMT_NONE };
00096     const enum PixelFormat blend_pix_fmts[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
00097     AVFilterFormats *inout_formats = avfilter_make_format_list(inout_pix_fmts);
00098     AVFilterFormats *blend_formats = avfilter_make_format_list(blend_pix_fmts);
00099 
00100     avfilter_formats_ref(inout_formats, &ctx->inputs [MAIN   ]->out_formats);
00101     avfilter_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
00102     avfilter_formats_ref(inout_formats, &ctx->outputs[MAIN   ]->in_formats );
00103 
00104     return 0;
00105 }
00106 
00107 static int config_input_main(AVFilterLink *inlink)
00108 {
00109     OverlayContext *over = inlink->dst->priv;
00110     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00111 
00112     av_image_fill_max_pixsteps(over->max_plane_step, NULL, pix_desc);
00113     over->hsub = pix_desc->log2_chroma_w;
00114     over->vsub = pix_desc->log2_chroma_h;
00115 
00116     return 0;
00117 }
00118 
00119 static int config_input_overlay(AVFilterLink *inlink)
00120 {
00121     AVFilterContext *ctx  = inlink->dst;
00122     OverlayContext  *over = inlink->dst->priv;
00123     char *expr;
00124     double var_values[VAR_VARS_NB], res;
00125     int ret;
00126 
00127     /* Finish the configuration by evaluating the expressions
00128        now when both inputs are configured. */
00129     var_values[VAR_E  ] = M_E;
00130     var_values[VAR_PHI] = M_PHI;
00131     var_values[VAR_PI ] = M_PI;
00132 
00133     var_values[VAR_MAIN_W   ] = var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
00134     var_values[VAR_MAIN_H   ] = var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
00135     var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
00136     var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
00137 
00138     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00139                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00140         goto fail;
00141     over->x = res;
00142     if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
00143                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)))
00144         goto fail;
00145     over->y = res;
00146     /* x may depend on y */
00147     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00148                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00149         goto fail;
00150     over->x = res;
00151 
00152     av_log(ctx, AV_LOG_INFO,
00153            "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
00154            ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
00155            av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
00156            over->x, over->y,
00157            ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
00158            av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
00159 
00160     if (over->x < 0 || over->y < 0 ||
00161         over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
00162         over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
00163         av_log(ctx, AV_LOG_ERROR,
00164                "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
00165                over->x, over->y,
00166                (int)(over->x + var_values[VAR_OVERLAY_W]),
00167                (int)(over->y + var_values[VAR_OVERLAY_H]),
00168                (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
00169         return AVERROR(EINVAL);
00170     }
00171     return 0;
00172 
00173 fail:
00174     av_log(NULL, AV_LOG_ERROR,
00175            "Error when evaluating the expression '%s'\n", expr);
00176     return ret;
00177 }
00178 
00179 static int config_output(AVFilterLink *outlink)
00180 {
00181     AVFilterContext *ctx = outlink->src;
00182     int exact;
00183     // common timebase computation:
00184     AVRational tb1 = ctx->inputs[MAIN   ]->time_base;
00185     AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
00186     AVRational *tb = &ctx->outputs[0]->time_base;
00187     exact = av_reduce(&tb->num, &tb->den,
00188                       av_gcd((int64_t)tb1.num * tb2.den,
00189                              (int64_t)tb2.num * tb1.den),
00190                       (int64_t)tb1.den * tb2.den, INT_MAX);
00191     av_log(ctx, AV_LOG_INFO,
00192            "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
00193            tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
00194     if (!exact)
00195         av_log(ctx, AV_LOG_WARNING,
00196                "Timestamp conversion inexact, timestamp information loss may occurr\n");
00197 
00198     outlink->w = ctx->inputs[MAIN]->w;
00199     outlink->h = ctx->inputs[MAIN]->h;
00200 
00201     return 0;
00202 }
00203 
00204 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00205 {
00206     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
00207 }
00208 
00209 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00210 {
00211     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00212     AVFilterContext *ctx = inlink->dst;
00213     OverlayContext *over = ctx->priv;
00214 
00215     inlink->dst->outputs[0]->out_buf = outpicref;
00216     outpicref->pts = av_rescale_q(outpicref->pts, ctx->inputs[MAIN]->time_base,
00217                                   ctx->outputs[0]->time_base);
00218 
00219     if (!over->overpicref || over->overpicref->pts < outpicref->pts) {
00220         AVFilterBufferRef *old = over->overpicref;
00221         over->overpicref = NULL;
00222         avfilter_request_frame(ctx->inputs[OVERLAY]);
00223         if (over->overpicref) {
00224             if (old)
00225                 avfilter_unref_buffer(old);
00226         } else
00227             over->overpicref = old;
00228     }
00229 
00230     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
00231 }
00232 
00233 static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00234 {
00235     AVFilterContext *ctx = inlink->dst;
00236     OverlayContext *over = ctx->priv;
00237 
00238     over->overpicref = inpicref;
00239     over->overpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
00240                                          ctx->outputs[0]->time_base);
00241 }
00242 
00243 static void blend_slice(AVFilterContext *ctx,
00244                         AVFilterBufferRef *dst, AVFilterBufferRef *src,
00245                         int x, int y, int w, int h,
00246                         int slice_y, int slice_w, int slice_h)
00247 {
00248     OverlayContext *over = ctx->priv;
00249     int i, j, k;
00250     int width, height;
00251     int overlay_end_y = y+h;
00252     int slice_end_y = slice_y+slice_h;
00253     int end_y, start_y;
00254 
00255     width = FFMIN(slice_w - x, w);
00256     end_y = FFMIN(slice_end_y, overlay_end_y);
00257     start_y = FFMAX(y, slice_y);
00258     height = end_y - start_y;
00259 
00260     if (dst->format == PIX_FMT_BGR24 || dst->format == PIX_FMT_RGB24) {
00261         uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
00262         uint8_t *sp = src->data[0];
00263         int b = dst->format == PIX_FMT_BGR24 ? 2 : 0;
00264         int r = dst->format == PIX_FMT_BGR24 ? 0 : 2;
00265         if (slice_y > y)
00266             sp += (slice_y - y) * src->linesize[0];
00267         for (i = 0; i < height; i++) {
00268             uint8_t *d = dp, *s = sp;
00269             for (j = 0; j < width; j++) {
00270                 d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
00271                 d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
00272                 d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
00273                 d += 3;
00274                 s += 4;
00275             }
00276             dp += dst->linesize[0];
00277             sp += src->linesize[0];
00278         }
00279     } else {
00280         for (i = 0; i < 3; i++) {
00281             int hsub = i ? over->hsub : 0;
00282             int vsub = i ? over->vsub : 0;
00283             uint8_t *dp = dst->data[i] + (x >> hsub) +
00284                 (start_y >> vsub) * dst->linesize[i];
00285             uint8_t *sp = src->data[i];
00286             uint8_t *ap = src->data[3];
00287             int wp = FFALIGN(width, 1<<hsub) >> hsub;
00288             int hp = FFALIGN(height, 1<<vsub) >> vsub;
00289             if (slice_y > y) {
00290                 sp += ((slice_y - y) >> vsub) * src->linesize[i];
00291                 ap += (slice_y - y) * src->linesize[3];
00292             }
00293             for (j = 0; j < hp; j++) {
00294                 uint8_t *d = dp, *s = sp, *a = ap;
00295                 for (k = 0; k < wp; k++) {
00296                     // average alpha for color components, improve quality
00297                     int alpha_v, alpha_h, alpha;
00298                     if (hsub && vsub && j+1 < hp && k+1 < wp) {
00299                         alpha = (a[0] + a[src->linesize[3]] +
00300                                  a[1] + a[src->linesize[3]+1]) >> 2;
00301                     } else if (hsub || vsub) {
00302                         alpha_h = hsub && k+1 < wp ?
00303                             (a[0] + a[1]) >> 1 : a[0];
00304                         alpha_v = vsub && j+1 < hp ?
00305                             (a[0] + a[src->linesize[3]]) >> 1 : a[0];
00306                         alpha = (alpha_v + alpha_h) >> 1;
00307                     } else
00308                         alpha = a[0];
00309                     *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
00310                     d++;
00311                     a += 1 << hsub;
00312                 }
00313                 dp += dst->linesize[i];
00314                 sp += src->linesize[i];
00315                 ap += (1 << vsub) * src->linesize[3];
00316             }
00317         }
00318     }
00319 }
00320 
00321 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00322 {
00323     AVFilterContext *ctx = inlink->dst;
00324     AVFilterLink *outlink = ctx->outputs[0];
00325     AVFilterBufferRef *outpicref = outlink->out_buf;
00326     OverlayContext *over = ctx->priv;
00327 
00328     if (over->overpicref &&
00329         !(over->x >= outpicref->video->w || over->y >= outpicref->video->h ||
00330           y+h < over->y || y >= over->y + over->overpicref->video->h)) {
00331         blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
00332                     over->overpicref->video->w, over->overpicref->video->h,
00333                     y, outpicref->video->w, h);
00334     }
00335     avfilter_draw_slice(outlink, y, h, slice_dir);
00336 }
00337 
00338 static void end_frame(AVFilterLink *inlink)
00339 {
00340     avfilter_end_frame(inlink->dst->outputs[0]);
00341     avfilter_unref_buffer(inlink->cur_buf);
00342 }
00343 
00344 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
00345 
00346 static void null_end_frame(AVFilterLink *inlink) { }
00347 
00348 AVFilter avfilter_vf_overlay = {
00349     .name      = "overlay",
00350     .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
00351 
00352     .init      = init,
00353     .uninit    = uninit,
00354 
00355     .priv_size = sizeof(OverlayContext),
00356 
00357     .query_formats = query_formats,
00358 
00359     .inputs    = (AVFilterPad[]) {{ .name            = "main",
00360                                     .type            = AVMEDIA_TYPE_VIDEO,
00361                                     .start_frame     = start_frame,
00362                                     .get_video_buffer= get_video_buffer,
00363                                     .config_props    = config_input_main,
00364                                     .draw_slice      = draw_slice,
00365                                     .end_frame       = end_frame,
00366                                     .min_perms       = AV_PERM_READ,
00367                                     .rej_perms       = AV_PERM_REUSE2|AV_PERM_PRESERVE, },
00368                                   { .name            = "overlay",
00369                                     .type            = AVMEDIA_TYPE_VIDEO,
00370                                     .start_frame     = start_frame_overlay,
00371                                     .config_props    = config_input_overlay,
00372                                     .draw_slice      = null_draw_slice,
00373                                     .end_frame       = null_end_frame,
00374                                     .min_perms       = AV_PERM_READ,
00375                                     .rej_perms       = AV_PERM_REUSE2, },
00376                                   { .name = NULL}},
00377     .outputs   = (AVFilterPad[]) {{ .name            = "default",
00378                                     .type            = AVMEDIA_TYPE_VIDEO,
00379                                     .config_props    = config_output, },
00380                                   { .name = NULL}},
00381 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1