00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
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         if (!(over->x_expr = av_strdup(expr))) {
00119             ret = AVERROR(ENOMEM);
00120             goto end;
00121         }
00122     }
00123     if (expr = av_strtok(NULL, ":", &bufptr)) {
00124         if (!(over->y_expr = av_strdup(expr))) {
00125             ret = AVERROR(ENOMEM);
00126             goto end;
00127         }
00128     }
00129 
00130     if (bufptr && (ret = av_set_options_string(over, bufptr, "=", ":")) < 0)
00131         goto end;
00132 
00133 end:
00134     av_free(args1);
00135     return ret;
00136 }
00137 
00138 static av_cold void uninit(AVFilterContext *ctx)
00139 {
00140     OverlayContext *over = ctx->priv;
00141 
00142     av_freep(&over->x_expr);
00143     av_freep(&over->y_expr);
00144 
00145     if (over->overpicref)
00146         avfilter_unref_buffer(over->overpicref);
00147 }
00148 
00149 static int query_formats(AVFilterContext *ctx)
00150 {
00151     OverlayContext *over = ctx->priv;
00152 
00153     
00154     const enum PixelFormat main_pix_fmts_yuv[] = { PIX_FMT_YUV420P,  PIX_FMT_NONE };
00155     const enum PixelFormat overlay_pix_fmts_yuv[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
00156     const enum PixelFormat main_pix_fmts_rgb[] = {
00157         PIX_FMT_ARGB,  PIX_FMT_RGBA,
00158         PIX_FMT_ABGR,  PIX_FMT_BGRA,
00159         PIX_FMT_RGB24, PIX_FMT_BGR24,
00160         PIX_FMT_NONE
00161     };
00162     const enum PixelFormat overlay_pix_fmts_rgb[] = {
00163         PIX_FMT_ARGB,  PIX_FMT_RGBA,
00164         PIX_FMT_ABGR,  PIX_FMT_BGRA,
00165         PIX_FMT_NONE
00166     };
00167 
00168     AVFilterFormats *main_formats;
00169     AVFilterFormats *overlay_formats;
00170 
00171     if (over->allow_packed_rgb) {
00172         main_formats    = avfilter_make_format_list(main_pix_fmts_rgb);
00173         overlay_formats = avfilter_make_format_list(overlay_pix_fmts_rgb);
00174     } else {
00175         main_formats    = avfilter_make_format_list(main_pix_fmts_yuv);
00176         overlay_formats = avfilter_make_format_list(overlay_pix_fmts_yuv);
00177     }
00178 
00179     avfilter_formats_ref(main_formats,    &ctx->inputs [MAIN   ]->out_formats);
00180     avfilter_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
00181     avfilter_formats_ref(main_formats,    &ctx->outputs[MAIN   ]->in_formats );
00182 
00183     return 0;
00184 }
00185 
00186 static const enum PixelFormat alpha_pix_fmts[] = {
00187     PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
00188     PIX_FMT_BGRA, PIX_FMT_NONE
00189 };
00190 
00191 static int config_input_main(AVFilterLink *inlink)
00192 {
00193     OverlayContext *over = inlink->dst->priv;
00194     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00195 
00196     av_image_fill_max_pixsteps(over->main_pix_step,    NULL, pix_desc);
00197 
00198     over->hsub = pix_desc->log2_chroma_w;
00199     over->vsub = pix_desc->log2_chroma_h;
00200 
00201     over->main_is_packed_rgb =
00202         ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
00203     over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
00204     return 0;
00205 }
00206 
00207 static int config_input_overlay(AVFilterLink *inlink)
00208 {
00209     AVFilterContext *ctx  = inlink->dst;
00210     OverlayContext  *over = inlink->dst->priv;
00211     char *expr;
00212     double var_values[VAR_VARS_NB], res;
00213     int ret;
00214     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00215 
00216     av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
00217 
00218     
00219 
00220     var_values[VAR_MAIN_W   ] = var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
00221     var_values[VAR_MAIN_H   ] = var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
00222     var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
00223     var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
00224 
00225     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00226                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00227         goto fail;
00228     over->x = res;
00229     if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
00230                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)))
00231         goto fail;
00232     over->y = res;
00233     
00234     if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
00235                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00236         goto fail;
00237     over->x = res;
00238 
00239     over->overlay_is_packed_rgb =
00240         ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
00241     over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
00242 
00243     av_log(ctx, AV_LOG_INFO,
00244            "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
00245            ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
00246            av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
00247            over->x, over->y,
00248            ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
00249            av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
00250 
00251     if (over->x < 0 || over->y < 0 ||
00252         over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
00253         over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
00254         av_log(ctx, AV_LOG_ERROR,
00255                "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
00256                over->x, over->y,
00257                (int)(over->x + var_values[VAR_OVERLAY_W]),
00258                (int)(over->y + var_values[VAR_OVERLAY_H]),
00259                (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
00260         return AVERROR(EINVAL);
00261     }
00262     return 0;
00263 
00264 fail:
00265     av_log(NULL, AV_LOG_ERROR,
00266            "Error when evaluating the expression '%s'\n", expr);
00267     return ret;
00268 }
00269 
00270 static int config_output(AVFilterLink *outlink)
00271 {
00272     AVFilterContext *ctx = outlink->src;
00273     int exact;
00274     
00275     AVRational tb1 = ctx->inputs[MAIN   ]->time_base;
00276     AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
00277     AVRational *tb = &ctx->outputs[0]->time_base;
00278     exact = av_reduce(&tb->num, &tb->den,
00279                       av_gcd((int64_t)tb1.num * tb2.den,
00280                              (int64_t)tb2.num * tb1.den),
00281                       (int64_t)tb1.den * tb2.den, INT_MAX);
00282     av_log(ctx, AV_LOG_INFO,
00283            "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
00284            tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
00285     if (!exact)
00286         av_log(ctx, AV_LOG_WARNING,
00287                "Timestamp conversion inexact, timestamp information loss may occurr\n");
00288 
00289     outlink->w = ctx->inputs[MAIN]->w;
00290     outlink->h = ctx->inputs[MAIN]->h;
00291 
00292     return 0;
00293 }
00294 
00295 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00296 {
00297     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
00298 }
00299 
00300 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00301 {
00302     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00303     AVFilterContext *ctx = inlink->dst;
00304     OverlayContext *over = ctx->priv;
00305 
00306     inlink->dst->outputs[0]->out_buf = outpicref;
00307     outpicref->pts = av_rescale_q(outpicref->pts, ctx->inputs[MAIN]->time_base,
00308                                   ctx->outputs[0]->time_base);
00309 
00310     if (!over->overpicref || over->overpicref->pts < outpicref->pts) {
00311         AVFilterBufferRef *old = over->overpicref;
00312         over->overpicref = NULL;
00313         avfilter_request_frame(ctx->inputs[OVERLAY]);
00314         if (over->overpicref) {
00315             if (old)
00316                 avfilter_unref_buffer(old);
00317         } else
00318             over->overpicref = old;
00319     }
00320 
00321     avfilter_start_frame(inlink->dst->outputs[0], outpicref);
00322 }
00323 
00324 static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00325 {
00326     AVFilterContext *ctx = inlink->dst;
00327     OverlayContext *over = ctx->priv;
00328 
00329     over->overpicref = inpicref;
00330     over->overpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
00331                                          ctx->outputs[0]->time_base);
00332 }
00333 
00334 
00335 
00336 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
00337 
00338 static void blend_slice(AVFilterContext *ctx,
00339                         AVFilterBufferRef *dst, AVFilterBufferRef *src,
00340                         int x, int y, int w, int h,
00341                         int slice_y, int slice_w, int slice_h)
00342 {
00343     OverlayContext *over = ctx->priv;
00344     int i, j, k;
00345     int width, height;
00346     int overlay_end_y = y+h;
00347     int slice_end_y = slice_y+slice_h;
00348     int end_y, start_y;
00349 
00350     width = FFMIN(slice_w - x, w);
00351     end_y = FFMIN(slice_end_y, overlay_end_y);
00352     start_y = FFMAX(y, slice_y);
00353     height = end_y - start_y;
00354 
00355     if (over->main_is_packed_rgb) {
00356         uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
00357                       start_y * dst->linesize[0];
00358         uint8_t *sp = src->data[0];
00359         uint8_t alpha;          
00360         const int dr = over->main_rgba_map[R];
00361         const int dg = over->main_rgba_map[G];
00362         const int db = over->main_rgba_map[B];
00363         const int da = over->main_rgba_map[A];
00364         const int dstep = over->main_pix_step[0];
00365         const int sr = over->overlay_rgba_map[R];
00366         const int sg = over->overlay_rgba_map[G];
00367         const int sb = over->overlay_rgba_map[B];
00368         const int sa = over->overlay_rgba_map[A];
00369         const int sstep = over->overlay_pix_step[0];
00370         const int main_has_alpha = over->main_has_alpha;
00371         if (slice_y > y)
00372             sp += (slice_y - y) * src->linesize[0];
00373         for (i = 0; i < height; i++) {
00374             uint8_t *d = dp, *s = sp;
00375             for (j = 0; j < width; j++) {
00376                 alpha = s[sa];
00377 
00378                 
00379                 
00380                 if (main_has_alpha && alpha != 0 && alpha != 255) {
00381                     
00382                     
00383                     alpha =
00384                         
00385                         ( (alpha << 16) - (alpha << 9) + alpha )
00386                         /
00387                         
00388                         ( ((alpha + d[da]) << 8 ) - (alpha + d[da])
00389                           - d[da] * alpha );
00390                 }
00391 
00392                 switch (alpha) {
00393                 case 0:
00394                     break;
00395                 case 255:
00396                     d[dr] = s[sr];
00397                     d[dg] = s[sg];
00398                     d[db] = s[sb];
00399                     break;
00400                 default:
00401                     
00402                     
00403                     d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
00404                     d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
00405                     d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
00406                 }
00407                 if (main_has_alpha) {
00408                     switch (alpha) {
00409                     case 0:
00410                         break;
00411                     case 255:
00412                         d[da] = s[sa];
00413                         break;
00414                     default:
00415                         
00416                         d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
00417                     }
00418                 }
00419                 d += dstep;
00420                 s += sstep;
00421             }
00422             dp += dst->linesize[0];
00423             sp += src->linesize[0];
00424         }
00425     } else {
00426         for (i = 0; i < 3; i++) {
00427             int hsub = i ? over->hsub : 0;
00428             int vsub = i ? over->vsub : 0;
00429             uint8_t *dp = dst->data[i] + (x >> hsub) +
00430                 (start_y >> vsub) * dst->linesize[i];
00431             uint8_t *sp = src->data[i];
00432             uint8_t *ap = src->data[3];
00433             int wp = FFALIGN(width, 1<<hsub) >> hsub;
00434             int hp = FFALIGN(height, 1<<vsub) >> vsub;
00435             if (slice_y > y) {
00436                 sp += ((slice_y - y) >> vsub) * src->linesize[i];
00437                 ap += (slice_y - y) * src->linesize[3];
00438             }
00439             for (j = 0; j < hp; j++) {
00440                 uint8_t *d = dp, *s = sp, *a = ap;
00441                 for (k = 0; k < wp; k++) {
00442                     
00443                     int alpha_v, alpha_h, alpha;
00444                     if (hsub && vsub && j+1 < hp && k+1 < wp) {
00445                         alpha = (a[0] + a[src->linesize[3]] +
00446                                  a[1] + a[src->linesize[3]+1]) >> 2;
00447                     } else if (hsub || vsub) {
00448                         alpha_h = hsub && k+1 < wp ?
00449                             (a[0] + a[1]) >> 1 : a[0];
00450                         alpha_v = vsub && j+1 < hp ?
00451                             (a[0] + a[src->linesize[3]]) >> 1 : a[0];
00452                         alpha = (alpha_v + alpha_h) >> 1;
00453                     } else
00454                         alpha = a[0];
00455                     *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
00456                     s++;
00457                     d++;
00458                     a += 1 << hsub;
00459                 }
00460                 dp += dst->linesize[i];
00461                 sp += src->linesize[i];
00462                 ap += (1 << vsub) * src->linesize[3];
00463             }
00464         }
00465     }
00466 }
00467 
00468 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00469 {
00470     AVFilterContext *ctx = inlink->dst;
00471     AVFilterLink *outlink = ctx->outputs[0];
00472     AVFilterBufferRef *outpicref = outlink->out_buf;
00473     OverlayContext *over = ctx->priv;
00474 
00475     if (over->overpicref &&
00476         !(over->x >= outpicref->video->w || over->y >= outpicref->video->h ||
00477           y+h < over->y || y >= over->y + over->overpicref->video->h)) {
00478         blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
00479                     over->overpicref->video->w, over->overpicref->video->h,
00480                     y, outpicref->video->w, h);
00481     }
00482     avfilter_draw_slice(outlink, y, h, slice_dir);
00483 }
00484 
00485 static void end_frame(AVFilterLink *inlink)
00486 {
00487     avfilter_end_frame(inlink->dst->outputs[0]);
00488     avfilter_unref_buffer(inlink->cur_buf);
00489 }
00490 
00491 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
00492 
00493 static void null_end_frame(AVFilterLink *inlink) { }
00494 
00495 AVFilter avfilter_vf_overlay = {
00496     .name      = "overlay",
00497     .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
00498 
00499     .init      = init,
00500     .uninit    = uninit,
00501 
00502     .priv_size = sizeof(OverlayContext),
00503 
00504     .query_formats = query_formats,
00505 
00506     .inputs    = (const AVFilterPad[]) {{ .name      = "main",
00507                                     .type            = AVMEDIA_TYPE_VIDEO,
00508                                     .start_frame     = start_frame,
00509                                     .get_video_buffer= get_video_buffer,
00510                                     .config_props    = config_input_main,
00511                                     .draw_slice      = draw_slice,
00512                                     .end_frame       = end_frame,
00513                                     .min_perms       = AV_PERM_READ,
00514                                     .rej_perms       = AV_PERM_REUSE2|AV_PERM_PRESERVE, },
00515                                   { .name            = "overlay",
00516                                     .type            = AVMEDIA_TYPE_VIDEO,
00517                                     .start_frame     = start_frame_overlay,
00518                                     .config_props    = config_input_overlay,
00519                                     .draw_slice      = null_draw_slice,
00520                                     .end_frame       = null_end_frame,
00521                                     .min_perms       = AV_PERM_READ,
00522                                     .rej_perms       = AV_PERM_REUSE2, },
00523                                   { .name = NULL}},
00524     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00525                                     .type            = AVMEDIA_TYPE_VIDEO,
00526                                     .config_props    = config_output, },
00527                                   { .name = NULL}},
00528 };