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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/opt.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "libavutil/imgutils.h"
00034 #include "libavutil/mathematics.h"
00035 #include "internal.h"
00036 #include "drawutils.h"
00037 
00038 static const char * const var_names[] = {
00039     "main_w",    "W", 
00040     "main_h",    "H", 
00041     "overlay_w", "w", 
00042     "overlay_h", "h", 
00043     NULL
00044 };
00045 
00046 enum var_name {
00047     VAR_MAIN_W,    VAR_MW,
00048     VAR_MAIN_H,    VAR_MH,
00049     VAR_OVERLAY_W, VAR_OW,
00050     VAR_OVERLAY_H, VAR_OH,
00051     VAR_VARS_NB
00052 };
00053 
00054 #define MAIN    0
00055 #define OVERLAY 1
00056 
00057 #define R 0
00058 #define G 1
00059 #define B 2
00060 #define A 3
00061 
00062 #define Y 0
00063 #define U 1
00064 #define V 2
00065 
00066 typedef struct {
00067     const AVClass *class;
00068     int x, y;                   
00069 
00070     int allow_packed_rgb;
00071     uint8_t main_is_packed_rgb;
00072     uint8_t main_rgba_map[4];
00073     uint8_t main_has_alpha;
00074     uint8_t overlay_is_packed_rgb;
00075     uint8_t overlay_rgba_map[4];
00076     uint8_t overlay_has_alpha;
00077 
00078     AVFilterBufferRef *overpicref;
00079 
00080     int main_pix_step[4];       
00081     int overlay_pix_step[4];    
00082     int hsub, vsub;             
00083 
00084     char *x_expr, *y_expr;
00085 } OverlayContext;
00086 
00087 #define OFFSET(x) offsetof(OverlayContext, x)
00088 
00089 static const AVOption overlay_options[] = {
00090     { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX },
00091     { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX },
00092     {"rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00093     {NULL},
00094 };
00095 
00096 static const char *overlay_get_name(void *ctx)
00097 {
00098     return "overlay";
00099 }
00100 
00101 static const AVClass overlay_class = {
00102     "OverlayContext",
00103     overlay_get_name,
00104     overlay_options
00105 };
00106 
00107 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00108 {
00109     OverlayContext *over = ctx->priv;
00110     char *args1 = av_strdup(args);
00111     char *expr, *bufptr = NULL;
00112     int ret = 0;
00113 
00114     over->class = &overlay_class;
00115     av_opt_set_defaults(over);
00116 
00117     if (expr = av_strtok(args1, ":", &bufptr)) {
00118         av_free(over->x_expr);
00119         if (!(over->x_expr = av_strdup(expr))) {
00120             ret = AVERROR(ENOMEM);
00121             goto end;
00122         }
00123     }
00124     if (expr = av_strtok(NULL, ":", &bufptr)) {
00125         av_free(over->y_expr);
00126         if (!(over->y_expr = av_strdup(expr))) {
00127             ret = AVERROR(ENOMEM);
00128             goto end;
00129         }
00130     }
00131 
00132     if (bufptr && (ret = av_set_options_string(over, bufptr, "=", ":")) < 0)
00133         goto end;
00134 
00135 end:
00136     av_free(args1);
00137     return ret;
00138 }
00139 
00140 static av_cold void uninit(AVFilterContext *ctx)
00141 {
00142     OverlayContext *over = ctx->priv;
00143 
00144     av_freep(&over->x_expr);
00145     av_freep(&over->y_expr);
00146 
00147     if (over->overpicref)
00148         avfilter_unref_buffer(over->overpicref);
00149 }
00150 
00151 static int query_formats(AVFilterContext *ctx)
00152 {
00153     OverlayContext *over = ctx->priv;
00154 
00155     /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
00156     const enum PixelFormat main_pix_fmts_yuv[] = { PIX_FMT_YUV420P,  PIX_FMT_NONE };
00157     const enum PixelFormat overlay_pix_fmts_yuv[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
00158     const enum PixelFormat main_pix_fmts_rgb[] = {
00159         PIX_FMT_ARGB,  PIX_FMT_RGBA,
00160         PIX_FMT_ABGR,  PIX_FMT_BGRA,
00161         PIX_FMT_RGB24, PIX_FMT_BGR24,
00162         PIX_FMT_NONE
00163     };
00164     const enum PixelFormat overlay_pix_fmts_rgb[] = {
00165         PIX_FMT_ARGB,  PIX_FMT_RGBA,
00166         PIX_FMT_ABGR,  PIX_FMT_BGRA,
00167         PIX_FMT_NONE
00168     };
00169 
00170     AVFilterFormats *main_formats;
00171     AVFilterFormats *overlay_formats;
00172 
00173     if (over->allow_packed_rgb) {
00174         main_formats    = avfilter_make_format_list(main_pix_fmts_rgb);
00175         overlay_formats = avfilter_make_format_list(overlay_pix_fmts_rgb);
00176     } else {
00177         main_formats    = avfilter_make_format_list(main_pix_fmts_yuv);
00178         overlay_formats = avfilter_make_format_list(overlay_pix_fmts_yuv);
00179     }
00180 
00181     avfilter_formats_ref(main_formats,    &ctx->inputs [MAIN   ]->out_formats);
00182     avfilter_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
00183     avfilter_formats_ref(main_formats,    &ctx->outputs[MAIN   ]->in_formats );
00184 
00185     return 0;
00186 }
00187 
00188 static const enum PixelFormat alpha_pix_fmts[] = {
00189     PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
00190     PIX_FMT_BGRA, PIX_FMT_NONE
00191 };
00192 
00193 static int config_input_main(AVFilterLink *inlink)
00194 {
00195     OverlayContext *over = inlink->dst->priv;
00196     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00197 
00198     av_image_fill_max_pixsteps(over->main_pix_step,    NULL, pix_desc);
00199 
00200     over->hsub = pix_desc->log2_chroma_w;
00201     over->vsub = pix_desc->log2_chroma_h;
00202 
00203     over->main_is_packed_rgb =
00204         ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
00205     over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
00206     return 0;
00207 }
00208 
00209 static int config_input_overlay(AVFilterLink *inlink)
00210 {
00211     AVFilterContext *ctx  = inlink->dst;
00212     OverlayContext  *over = inlink->dst->priv;
00213     char *expr;
00214     double var_values[VAR_VARS_NB], res;
00215     int ret;
00216     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00217 
00218     av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
00219 
00220     /* Finish the configuration by evaluating the expressions
00221        now when both inputs are configured. */
00222     var_values[VAR_MAIN_W   ] = var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
00223     var_values[VAR_MAIN_H   ] = var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
00224     var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
00225     var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
00226 
00227     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00228                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00229         goto fail;
00230     over->x = res;
00231     if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
00232                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)))
00233         goto fail;
00234     over->y = res;
00235     /* x may depend on y */
00236     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00237                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00238         goto fail;
00239     over->x = res;
00240 
00241     over->overlay_is_packed_rgb =
00242         ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
00243     over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
00244 
00245     av_log(ctx, AV_LOG_INFO,
00246            "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
00247            ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
00248            av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
00249            over->x, over->y,
00250            ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
00251            av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
00252 
00253     if (over->x < 0 || over->y < 0 ||
00254         over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
00255         over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
00256         av_log(ctx, AV_LOG_ERROR,
00257                "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
00258                over->x, over->y,
00259                (int)(over->x + var_values[VAR_OVERLAY_W]),
00260                (int)(over->y + var_values[VAR_OVERLAY_H]),
00261                (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
00262         return AVERROR(EINVAL);
00263     }
00264     return 0;
00265 
00266 fail:
00267     av_log(NULL, AV_LOG_ERROR,
00268            "Error when evaluating the expression '%s'\n", expr);
00269     return ret;
00270 }
00271 
00272 static int config_output(AVFilterLink *outlink)
00273 {
00274     AVFilterContext *ctx = outlink->src;
00275     int exact;
00276     // common timebase computation:
00277     AVRational tb1 = ctx->inputs[MAIN   ]->time_base;
00278     AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
00279     AVRational *tb = &ctx->outputs[0]->time_base;
00280     exact = av_reduce(&tb->num, &tb->den,
00281                       av_gcd((int64_t)tb1.num * tb2.den,
00282                              (int64_t)tb2.num * tb1.den),
00283                       (int64_t)tb1.den * tb2.den, INT_MAX);
00284     av_log(ctx, AV_LOG_INFO,
00285            "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
00286            tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
00287     if (!exact)
00288         av_log(ctx, AV_LOG_WARNING,
00289                "Timestamp conversion inexact, timestamp information loss may occurr\n");
00290 
00291     outlink->w = ctx->inputs[MAIN]->w;
00292     outlink->h = ctx->inputs[MAIN]->h;
00293 
00294     return 0;
00295 }
00296 
00297 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00298 {
00299     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
00300 }
00301 
00302 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00303 {
00304     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00305     AVFilterContext *ctx = inlink->dst;
00306     OverlayContext *over = ctx->priv;
00307 
00308     inlink->dst->outputs[0]->out_buf = outpicref;
00309     outpicref->pts = av_rescale_q(outpicref->pts, ctx->inputs[MAIN]->time_base,
00310                                   ctx->outputs[0]->time_base);
00311 
00312     if (!over->overpicref || over->overpicref->pts < outpicref->pts) {
00313         AVFilterBufferRef *old = over->overpicref;
00314         over->overpicref = NULL;
00315         avfilter_request_frame(ctx->inputs[OVERLAY]);
00316         if (over->overpicref) {
00317             if (old)
00318                 avfilter_unref_buffer(old);
00319         } else
00320             over->overpicref = old;
00321     }
00322 
00323     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
00324 }
00325 
00326 static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00327 {
00328     AVFilterContext *ctx = inlink->dst;
00329     OverlayContext *over = ctx->priv;
00330 
00331     over->overpicref = inpicref;
00332     over->overpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
00333                                          ctx->outputs[0]->time_base);
00334 }
00335 
00336 // divide by 255 and round to nearest
00337 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
00338 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
00339 
00340 static void blend_slice(AVFilterContext *ctx,
00341                         AVFilterBufferRef *dst, AVFilterBufferRef *src,
00342                         int x, int y, int w, int h,
00343                         int slice_y, int slice_w, int slice_h)
00344 {
00345     OverlayContext *over = ctx->priv;
00346     int i, j, k;
00347     int width, height;
00348     int overlay_end_y = y+h;
00349     int slice_end_y = slice_y+slice_h;
00350     int end_y, start_y;
00351 
00352     width = FFMIN(slice_w - x, w);
00353     end_y = FFMIN(slice_end_y, overlay_end_y);
00354     start_y = FFMAX(y, slice_y);
00355     height = end_y - start_y;
00356 
00357     if (over->main_is_packed_rgb) {
00358         uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
00359                       start_y * dst->linesize[0];
00360         uint8_t *sp = src->data[0];
00361         uint8_t alpha;          
00362         const int dr = over->main_rgba_map[R];
00363         const int dg = over->main_rgba_map[G];
00364         const int db = over->main_rgba_map[B];
00365         const int da = over->main_rgba_map[A];
00366         const int dstep = over->main_pix_step[0];
00367         const int sr = over->overlay_rgba_map[R];
00368         const int sg = over->overlay_rgba_map[G];
00369         const int sb = over->overlay_rgba_map[B];
00370         const int sa = over->overlay_rgba_map[A];
00371         const int sstep = over->overlay_pix_step[0];
00372         const int main_has_alpha = over->main_has_alpha;
00373         if (slice_y > y)
00374             sp += (slice_y - y) * src->linesize[0];
00375         for (i = 0; i < height; i++) {
00376             uint8_t *d = dp, *s = sp;
00377             for (j = 0; j < width; j++) {
00378                 alpha = s[sa];
00379 
00380                 // if the main channel has an alpha channel, alpha has to be calculated
00381                 // to create an un-premultiplied (straight) alpha value
00382                 if (main_has_alpha && alpha != 0 && alpha != 255) {
00383                     // apply the general equation:
00384                     // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
00385                     alpha =
00386                         // the next line is a faster version of: 255 * 255 * alpha
00387                         ( (alpha << 16) - (alpha << 9) + alpha )
00388                         /
00389                         // the next line is a faster version of: 255 * (alpha + d[da])
00390                         ( ((alpha + d[da]) << 8 ) - (alpha + d[da])
00391                           - d[da] * alpha );
00392                 }
00393 
00394                 switch (alpha) {
00395                 case 0:
00396                     break;
00397                 case 255:
00398                     d[dr] = s[sr];
00399                     d[dg] = s[sg];
00400                     d[db] = s[sb];
00401                     break;
00402                 default:
00403                     // main_value = main_value * (1 - alpha) + overlay_value * alpha
00404                     // since alpha is in the range 0-255, the result must divided by 255
00405                     d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
00406                     d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
00407                     d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
00408                 }
00409                 if (main_has_alpha) {
00410                     switch (alpha) {
00411                     case 0:
00412                         break;
00413                     case 255:
00414                         d[da] = s[sa];
00415                         break;
00416                     default:
00417                         // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
00418                         d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
00419                     }
00420                 }
00421                 d += dstep;
00422                 s += sstep;
00423             }
00424             dp += dst->linesize[0];
00425             sp += src->linesize[0];
00426         }
00427     } else {
00428         for (i = 0; i < 3; i++) {
00429             int hsub = i ? over->hsub : 0;
00430             int vsub = i ? over->vsub : 0;
00431             uint8_t *dp = dst->data[i] + (x >> hsub) +
00432                 (start_y >> vsub) * dst->linesize[i];
00433             uint8_t *sp = src->data[i];
00434             uint8_t *ap = src->data[3];
00435             int wp = FFALIGN(width, 1<<hsub) >> hsub;
00436             int hp = FFALIGN(height, 1<<vsub) >> vsub;
00437             if (slice_y > y) {
00438                 sp += ((slice_y - y) >> vsub) * src->linesize[i];
00439                 ap += (slice_y - y) * src->linesize[3];
00440             }
00441             for (j = 0; j < hp; j++) {
00442                 uint8_t *d = dp, *s = sp, *a = ap;
00443                 for (k = 0; k < wp; k++) {
00444                     // average alpha for color components, improve quality
00445                     int alpha_v, alpha_h, alpha;
00446                     if (hsub && vsub && j+1 < hp && k+1 < wp) {
00447                         alpha = (a[0] + a[src->linesize[3]] +
00448                                  a[1] + a[src->linesize[3]+1]) >> 2;
00449                     } else if (hsub || vsub) {
00450                         alpha_h = hsub && k+1 < wp ?
00451                             (a[0] + a[1]) >> 1 : a[0];
00452                         alpha_v = vsub && j+1 < hp ?
00453                             (a[0] + a[src->linesize[3]]) >> 1 : a[0];
00454                         alpha = (alpha_v + alpha_h) >> 1;
00455                     } else
00456                         alpha = a[0];
00457                     *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
00458                     s++;
00459                     d++;
00460                     a += 1 << hsub;
00461                 }
00462                 dp += dst->linesize[i];
00463                 sp += src->linesize[i];
00464                 ap += (1 << vsub) * src->linesize[3];
00465             }
00466         }
00467     }
00468 }
00469 
00470 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00471 {
00472     AVFilterContext *ctx = inlink->dst;
00473     AVFilterLink *outlink = ctx->outputs[0];
00474     AVFilterBufferRef *outpicref = outlink->out_buf;
00475     OverlayContext *over = ctx->priv;
00476 
00477     if (over->overpicref &&
00478         !(over->x >= outpicref->video->w || over->y >= outpicref->video->h ||
00479           y+h < over->y || y >= over->y + over->overpicref->video->h)) {
00480         blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
00481                     over->overpicref->video->w, over->overpicref->video->h,
00482                     y, outpicref->video->w, h);
00483     }
00484     avfilter_draw_slice(outlink, y, h, slice_dir);
00485 }
00486 
00487 static void end_frame(AVFilterLink *inlink)
00488 {
00489     avfilter_end_frame(inlink->dst->outputs[0]);
00490     avfilter_unref_buffer(inlink->cur_buf);
00491 }
00492 
00493 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
00494 
00495 static void null_end_frame(AVFilterLink *inlink) { }
00496 
00497 AVFilter avfilter_vf_overlay = {
00498     .name      = "overlay",
00499     .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
00500 
00501     .init      = init,
00502     .uninit    = uninit,
00503 
00504     .priv_size = sizeof(OverlayContext),
00505 
00506     .query_formats = query_formats,
00507 
00508     .inputs    = (const AVFilterPad[]) {{ .name      = "main",
00509                                     .type            = AVMEDIA_TYPE_VIDEO,
00510                                     .start_frame     = start_frame,
00511                                     .get_video_buffer= get_video_buffer,
00512                                     .config_props    = config_input_main,
00513                                     .draw_slice      = draw_slice,
00514                                     .end_frame       = end_frame,
00515                                     .min_perms       = AV_PERM_READ,
00516                                     .rej_perms       = AV_PERM_REUSE2|AV_PERM_PRESERVE, },
00517                                   { .name            = "overlay",
00518                                     .type            = AVMEDIA_TYPE_VIDEO,
00519                                     .start_frame     = start_frame_overlay,
00520                                     .config_props    = config_input_overlay,
00521                                     .draw_slice      = null_draw_slice,
00522                                     .end_frame       = null_end_frame,
00523                                     .min_perms       = AV_PERM_READ,
00524                                     .rej_perms       = AV_PERM_REUSE2, },
00525                                   { .name = NULL}},
00526     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00527                                     .type            = AVMEDIA_TYPE_VIDEO,
00528                                     .config_props    = config_output, },
00529                                   { .name = NULL}},
00530 };