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