00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include <sys/time.h>
00030 #include <time.h>
00031
00032 #include "libavutil/colorspace.h"
00033 #include "libavutil/file.h"
00034 #include "libavutil/eval.h"
00035 #include "libavutil/opt.h"
00036 #include "libavutil/random_seed.h"
00037 #include "libavutil/parseutils.h"
00038 #include "libavutil/pixdesc.h"
00039 #include "libavutil/tree.h"
00040 #include "libavutil/lfg.h"
00041 #include "avfilter.h"
00042 #include "drawutils.h"
00043
00044 #undef time
00045
00046 #include <ft2build.h>
00047 #include <freetype/config/ftheader.h>
00048 #include FT_FREETYPE_H
00049 #include FT_GLYPH_H
00050
00051 static const char * const var_names[] = {
00052 "main_w", "w", "W",
00053 "main_h", "h", "H",
00054 "tw", "text_w",
00055 "th", "text_h",
00056 "max_glyph_w",
00057 "max_glyph_h",
00058 "max_glyph_a", "ascent",
00059 "max_glyph_d", "descent",
00060 "line_h", "lh",
00061 "sar",
00062 "dar",
00063 "hsub",
00064 "vsub",
00065 "x",
00066 "y",
00067 "n",
00068 "t",
00069 NULL
00070 };
00071
00072 static const char *fun2_names[] = {
00073 "rand",
00074 };
00075
00076 static double drand(void *opaque, double min, double max)
00077 {
00078 return min + (max-min) / UINT_MAX * av_lfg_get(opaque);
00079 }
00080
00081 typedef double (*eval_func2)(void *, double a, double b);
00082
00083 static const eval_func2 fun2[] = {
00084 drand,
00085 NULL
00086 };
00087
00088 enum var_name {
00089 VAR_MAIN_W, VAR_w, VAR_W,
00090 VAR_MAIN_H, VAR_h, VAR_H,
00091 VAR_TW, VAR_TEXT_W,
00092 VAR_TH, VAR_TEXT_H,
00093 VAR_MAX_GLYPH_W,
00094 VAR_MAX_GLYPH_H,
00095 VAR_MAX_GLYPH_A, VAR_ASCENT,
00096 VAR_MAX_GLYPH_D, VAR_DESCENT,
00097 VAR_LINE_H, VAR_LH,
00098 VAR_SAR,
00099 VAR_DAR,
00100 VAR_HSUB,
00101 VAR_VSUB,
00102 VAR_X,
00103 VAR_Y,
00104 VAR_N,
00105 VAR_T,
00106 VAR_VARS_NB
00107 };
00108
00109 typedef struct {
00110 const AVClass *class;
00111 int reinit;
00112 uint8_t *fontfile;
00113 uint8_t *text;
00114 uint8_t *expanded_text;
00115 size_t expanded_text_size;
00116 int ft_load_flags;
00117 FT_Vector *positions;
00118 size_t nb_positions;
00119 char *textfile;
00120 int x;
00121 int y;
00122 int max_glyph_w;
00123 int max_glyph_h;
00124 int shadowx, shadowy;
00125 unsigned int fontsize;
00126 char *fontcolor_string;
00127 char *boxcolor_string;
00128 char *shadowcolor_string;
00129 uint8_t fontcolor[4];
00130 uint8_t boxcolor[4];
00131 uint8_t shadowcolor[4];
00132 uint8_t fontcolor_rgba[4];
00133 uint8_t boxcolor_rgba[4];
00134 uint8_t shadowcolor_rgba[4];
00135
00136 short int draw_box;
00137 int use_kerning;
00138 int tabsize;
00139
00140 FT_Library library;
00141 FT_Face face;
00142 struct AVTreeNode *glyphs;
00143 int hsub, vsub;
00144 int is_packed_rgb;
00145 int pixel_step[4];
00146 uint8_t rgba_map[4];
00147 uint8_t *box_line[4];
00148 char *x_expr;
00149 char *y_expr;
00150 AVExpr *x_pexpr, *y_pexpr;
00151 int64_t basetime;
00152 double var_values[VAR_VARS_NB];
00153 char *d_expr;
00154 AVExpr *d_pexpr;
00155 int draw;
00156 AVLFG prng;
00157 } DrawTextContext;
00158
00159 #define OFFSET(x) offsetof(DrawTextContext, x)
00160
00161 static const AVOption drawtext_options[]= {
00162 {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
00163 {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
00164 {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
00165 {"fontcolor", "set foreground color", OFFSET(fontcolor_string), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX },
00166 {"boxcolor", "set box color", OFFSET(boxcolor_string), AV_OPT_TYPE_STRING, {.str="white"}, CHAR_MIN, CHAR_MAX },
00167 {"shadowcolor", "set shadow color", OFFSET(shadowcolor_string), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX },
00168 {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00169 {"fontsize", "set font size", OFFSET(fontsize), AV_OPT_TYPE_INT, {.dbl=16}, 1, INT_MAX },
00170 {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX },
00171 {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX },
00172 {"shadowx", "set x", OFFSET(shadowx), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
00173 {"shadowy", "set y", OFFSET(shadowy), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
00174 {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.dbl=4}, 0, INT_MAX },
00175 {"basetime", "set base time", OFFSET(basetime), AV_OPT_TYPE_INT64, {.dbl=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX },
00176 {"draw", "if false do not draw", OFFSET(d_expr), AV_OPT_TYPE_STRING, {.str="1"}, CHAR_MIN, CHAR_MAX },
00177
00178
00179 {"ft_load_flags", "set font loading flags for libfreetype", OFFSET(ft_load_flags), AV_OPT_TYPE_FLAGS, {.dbl=FT_LOAD_DEFAULT|FT_LOAD_RENDER}, 0, INT_MAX, 0, "ft_load_flags" },
00180 {"default", "set default", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_DEFAULT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00181 {"no_scale", "set no_scale", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_NO_SCALE}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00182 {"no_hinting", "set no_hinting", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_NO_HINTING}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00183 {"render", "set render", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_RENDER}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00184 {"no_bitmap", "set no_bitmap", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_NO_BITMAP}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00185 {"vertical_layout", "set vertical_layout", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_VERTICAL_LAYOUT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00186 {"force_autohint", "set force_autohint", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_FORCE_AUTOHINT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00187 {"crop_bitmap", "set crop_bitmap", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_CROP_BITMAP}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00188 {"pedantic", "set pedantic", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_PEDANTIC}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00189 {"ignore_global_advance_width", "set ignore_global_advance_width", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00190 {"no_recurse", "set no_recurse", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_NO_RECURSE}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00191 {"ignore_transform", "set ignore_transform", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_IGNORE_TRANSFORM}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00192 {"monochrome", "set monochrome", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_MONOCHROME}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00193 {"linear_design", "set linear_design", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_LINEAR_DESIGN}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00194 {"no_autohint", "set no_autohint", 0, AV_OPT_TYPE_CONST, {.dbl=FT_LOAD_NO_AUTOHINT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
00195 {NULL},
00196 };
00197
00198 static const char *drawtext_get_name(void *ctx)
00199 {
00200 return "drawtext";
00201 }
00202
00203 static const AVClass drawtext_class = {
00204 "DrawTextContext",
00205 drawtext_get_name,
00206 drawtext_options
00207 };
00208
00209 #undef __FTERRORS_H__
00210 #define FT_ERROR_START_LIST {
00211 #define FT_ERRORDEF(e, v, s) { (e), (s) },
00212 #define FT_ERROR_END_LIST { 0, NULL } };
00213
00214 struct ft_error
00215 {
00216 int err;
00217 const char *err_msg;
00218 } static ft_errors[] =
00219 #include FT_ERRORS_H
00220
00221 #define FT_ERRMSG(e) ft_errors[e].err_msg
00222
00223 typedef struct {
00224 FT_Glyph *glyph;
00225 uint32_t code;
00226 FT_Bitmap bitmap;
00227 FT_BBox bbox;
00228 int advance;
00229 int bitmap_left;
00230 int bitmap_top;
00231 } Glyph;
00232
00233 static int glyph_cmp(void *key, const void *b)
00234 {
00235 const Glyph *a = key, *bb = b;
00236 int64_t diff = (int64_t)a->code - (int64_t)bb->code;
00237 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
00238 }
00239
00243 static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
00244 {
00245 DrawTextContext *dtext = ctx->priv;
00246 Glyph *glyph;
00247 struct AVTreeNode *node = NULL;
00248 int ret;
00249
00250
00251 if (FT_Load_Char(dtext->face, code, dtext->ft_load_flags))
00252 return AVERROR(EINVAL);
00253
00254
00255 if (!(glyph = av_mallocz(sizeof(*glyph))) ||
00256 !(glyph->glyph = av_mallocz(sizeof(*glyph->glyph)))) {
00257 ret = AVERROR(ENOMEM);
00258 goto error;
00259 }
00260 glyph->code = code;
00261
00262 if (FT_Get_Glyph(dtext->face->glyph, glyph->glyph)) {
00263 ret = AVERROR(EINVAL);
00264 goto error;
00265 }
00266
00267 glyph->bitmap = dtext->face->glyph->bitmap;
00268 glyph->bitmap_left = dtext->face->glyph->bitmap_left;
00269 glyph->bitmap_top = dtext->face->glyph->bitmap_top;
00270 glyph->advance = dtext->face->glyph->advance.x >> 6;
00271
00272
00273 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
00274
00275
00276 if (!(node = av_mallocz(av_tree_node_size))) {
00277 ret = AVERROR(ENOMEM);
00278 goto error;
00279 }
00280 av_tree_insert(&dtext->glyphs, glyph, glyph_cmp, &node);
00281
00282 if (glyph_ptr)
00283 *glyph_ptr = glyph;
00284 return 0;
00285
00286 error:
00287 if (glyph)
00288 av_freep(&glyph->glyph);
00289 av_freep(&glyph);
00290 av_freep(&node);
00291 return ret;
00292 }
00293
00294 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00295 {
00296 int err;
00297 DrawTextContext *dtext = ctx->priv;
00298 Glyph *glyph;
00299
00300 dtext->class = &drawtext_class;
00301 av_opt_set_defaults(dtext);
00302
00303 if ((err = (av_set_options_string(dtext, args, "=", ":"))) < 0) {
00304 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00305 return err;
00306 }
00307
00308 if (!dtext->fontfile) {
00309 av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
00310 return AVERROR(EINVAL);
00311 }
00312
00313 if (dtext->textfile) {
00314 uint8_t *textbuf;
00315 size_t textbuf_size;
00316
00317 if (dtext->text) {
00318 av_log(ctx, AV_LOG_ERROR,
00319 "Both text and text file provided. Please provide only one\n");
00320 return AVERROR(EINVAL);
00321 }
00322 if ((err = av_file_map(dtext->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
00323 av_log(ctx, AV_LOG_ERROR,
00324 "The text file '%s' could not be read or is empty\n",
00325 dtext->textfile);
00326 return err;
00327 }
00328
00329 if (!(dtext->text = av_malloc(textbuf_size+1)))
00330 return AVERROR(ENOMEM);
00331 memcpy(dtext->text, textbuf, textbuf_size);
00332 dtext->text[textbuf_size] = 0;
00333 av_file_unmap(textbuf, textbuf_size);
00334 }
00335
00336 if (!dtext->text) {
00337 av_log(ctx, AV_LOG_ERROR,
00338 "Either text or a valid file must be provided\n");
00339 return AVERROR(EINVAL);
00340 }
00341
00342 if ((err = av_parse_color(dtext->fontcolor_rgba, dtext->fontcolor_string, -1, ctx))) {
00343 av_log(ctx, AV_LOG_ERROR,
00344 "Invalid font color '%s'\n", dtext->fontcolor_string);
00345 return err;
00346 }
00347
00348 if ((err = av_parse_color(dtext->boxcolor_rgba, dtext->boxcolor_string, -1, ctx))) {
00349 av_log(ctx, AV_LOG_ERROR,
00350 "Invalid box color '%s'\n", dtext->boxcolor_string);
00351 return err;
00352 }
00353
00354 if ((err = av_parse_color(dtext->shadowcolor_rgba, dtext->shadowcolor_string, -1, ctx))) {
00355 av_log(ctx, AV_LOG_ERROR,
00356 "Invalid shadow color '%s'\n", dtext->shadowcolor_string);
00357 return err;
00358 }
00359
00360 if ((err = FT_Init_FreeType(&(dtext->library)))) {
00361 av_log(ctx, AV_LOG_ERROR,
00362 "Could not load FreeType: %s\n", FT_ERRMSG(err));
00363 return AVERROR(EINVAL);
00364 }
00365
00366
00367 if ((err = FT_New_Face(dtext->library, dtext->fontfile, 0, &dtext->face))) {
00368 av_log(ctx, AV_LOG_ERROR, "Could not load fontface from file '%s': %s\n",
00369 dtext->fontfile, FT_ERRMSG(err));
00370 return AVERROR(EINVAL);
00371 }
00372 if ((err = FT_Set_Pixel_Sizes(dtext->face, 0, dtext->fontsize))) {
00373 av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
00374 dtext->fontsize, FT_ERRMSG(err));
00375 return AVERROR(EINVAL);
00376 }
00377
00378 dtext->use_kerning = FT_HAS_KERNING(dtext->face);
00379
00380
00381 load_glyph(ctx, NULL, 0);
00382
00383
00384 if ((err = load_glyph(ctx, &glyph, ' ')) < 0) {
00385 av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
00386 return err;
00387 }
00388 dtext->tabsize *= glyph->advance;
00389
00390 return 0;
00391 }
00392
00393 static int query_formats(AVFilterContext *ctx)
00394 {
00395 static const enum PixelFormat pix_fmts[] = {
00396 PIX_FMT_ARGB, PIX_FMT_RGBA,
00397 PIX_FMT_ABGR, PIX_FMT_BGRA,
00398 PIX_FMT_RGB24, PIX_FMT_BGR24,
00399 PIX_FMT_YUV420P, PIX_FMT_YUV444P,
00400 PIX_FMT_YUV422P, PIX_FMT_YUV411P,
00401 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
00402 PIX_FMT_NONE
00403 };
00404
00405 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00406 return 0;
00407 }
00408
00409 static int glyph_enu_free(void *opaque, void *elem)
00410 {
00411 av_free(elem);
00412 return 0;
00413 }
00414
00415 static av_cold void uninit(AVFilterContext *ctx)
00416 {
00417 DrawTextContext *dtext = ctx->priv;
00418 int i;
00419
00420 av_expr_free(dtext->x_pexpr); dtext->x_pexpr = NULL;
00421 av_expr_free(dtext->y_pexpr); dtext->y_pexpr = NULL;
00422
00423 av_freep(&dtext->boxcolor_string);
00424 av_freep(&dtext->expanded_text);
00425 av_freep(&dtext->fontcolor_string);
00426 av_freep(&dtext->fontfile);
00427 av_freep(&dtext->shadowcolor_string);
00428 av_freep(&dtext->text);
00429 av_freep(&dtext->x_expr);
00430 av_freep(&dtext->y_expr);
00431
00432 av_freep(&dtext->positions);
00433 dtext->nb_positions = 0;
00434
00435 av_tree_enumerate(dtext->glyphs, NULL, NULL, glyph_enu_free);
00436 av_tree_destroy(dtext->glyphs);
00437 dtext->glyphs = NULL;
00438
00439 FT_Done_Face(dtext->face);
00440 FT_Done_FreeType(dtext->library);
00441
00442 for (i = 0; i < 4; i++) {
00443 av_freep(&dtext->box_line[i]);
00444 dtext->pixel_step[i] = 0;
00445 }
00446 }
00447
00448 static inline int is_newline(uint32_t c)
00449 {
00450 return (c == '\n' || c == '\r' || c == '\f' || c == '\v');
00451 }
00452
00453 static int config_input(AVFilterLink *inlink)
00454 {
00455 AVFilterContext *ctx = inlink->dst;
00456 DrawTextContext *dtext = ctx->priv;
00457 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00458 int ret;
00459
00460 dtext->hsub = pix_desc->log2_chroma_w;
00461 dtext->vsub = pix_desc->log2_chroma_h;
00462
00463 if ((ret =
00464 ff_fill_line_with_color(dtext->box_line, dtext->pixel_step,
00465 inlink->w, dtext->boxcolor,
00466 inlink->format, dtext->boxcolor_rgba,
00467 &dtext->is_packed_rgb, dtext->rgba_map)) < 0)
00468 return ret;
00469
00470 if (!dtext->is_packed_rgb) {
00471 uint8_t *rgba = dtext->fontcolor_rgba;
00472 dtext->fontcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
00473 dtext->fontcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
00474 dtext->fontcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
00475 dtext->fontcolor[3] = rgba[3];
00476 rgba = dtext->shadowcolor_rgba;
00477 dtext->shadowcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
00478 dtext->shadowcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
00479 dtext->shadowcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
00480 dtext->shadowcolor[3] = rgba[3];
00481 }
00482
00483 dtext->var_values[VAR_w] = dtext->var_values[VAR_W] = dtext->var_values[VAR_MAIN_W] = inlink->w;
00484 dtext->var_values[VAR_h] = dtext->var_values[VAR_H] = dtext->var_values[VAR_MAIN_H] = inlink->h;
00485 dtext->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
00486 dtext->var_values[VAR_DAR] = (double)inlink->w / inlink->h * dtext->var_values[VAR_SAR];
00487 dtext->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
00488 dtext->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
00489 dtext->var_values[VAR_X] = NAN;
00490 dtext->var_values[VAR_Y] = NAN;
00491 if (!dtext->reinit)
00492 dtext->var_values[VAR_N] = 0;
00493 dtext->var_values[VAR_T] = NAN;
00494
00495 av_lfg_init(&dtext->prng, av_get_random_seed());
00496
00497 if ((ret = av_expr_parse(&dtext->x_pexpr, dtext->x_expr, var_names,
00498 NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
00499 (ret = av_expr_parse(&dtext->y_pexpr, dtext->y_expr, var_names,
00500 NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
00501 (ret = av_expr_parse(&dtext->d_pexpr, dtext->d_expr, var_names,
00502 NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
00503
00504 return AVERROR(EINVAL);
00505
00506 return 0;
00507 }
00508
00509 static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
00510 {
00511 DrawTextContext *dtext = ctx->priv;
00512
00513 if (!strcmp(cmd, "reinit")) {
00514 int ret;
00515 uninit(ctx);
00516 dtext->reinit = 1;
00517 if ((ret = init(ctx, arg, NULL)) < 0)
00518 return ret;
00519 return config_input(ctx->inputs[0]);
00520 }
00521
00522 return AVERROR(ENOSYS);
00523 }
00524
00525 #define GET_BITMAP_VAL(r, c) \
00526 bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
00527 (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
00528 bitmap->buffer[(r) * bitmap->pitch + (c)]
00529
00530 #define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub) { \
00531 luma_pos = ((x) ) + ((y) ) * picref->linesize[0]; \
00532 alpha = yuva_color[3] * (val) * 129; \
00533 picref->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * picref->data[0][luma_pos] ) >> 23; \
00534 if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
00535 chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[1]; \
00536 chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[2]; \
00537 picref->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * picref->data[1][chroma_pos1]) >> 23; \
00538 picref->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * picref->data[2][chroma_pos2]) >> 23; \
00539 }\
00540 }
00541
00542 static inline int draw_glyph_yuv(AVFilterBufferRef *picref, FT_Bitmap *bitmap,
00543 int x, int y, int width, int height,
00544 const uint8_t yuva_color[4], int hsub, int vsub)
00545 {
00546 int r, c, alpha;
00547 unsigned int luma_pos, chroma_pos1, chroma_pos2;
00548 uint8_t src_val;
00549
00550 for (r = 0; r < bitmap->rows && r+y < height; r++) {
00551 for (c = 0; c < bitmap->width && c+x < width; c++) {
00552 if (c+x < 0 || r+y < 0)
00553 continue;
00554
00555
00556 src_val = GET_BITMAP_VAL(r, c);
00557 if (!src_val)
00558 continue;
00559
00560 SET_PIXEL_YUV(picref, yuva_color, src_val, c+x, y+r, hsub, vsub);
00561 }
00562 }
00563
00564 return 0;
00565 }
00566
00567 #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
00568 p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
00569 alpha = rgba_color[3] * (val) * 129; \
00570 *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
00571 *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
00572 *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
00573 }
00574
00575 static inline int draw_glyph_rgb(AVFilterBufferRef *picref, FT_Bitmap *bitmap,
00576 int x, int y, int width, int height, int pixel_step,
00577 const uint8_t rgba_color[4], const uint8_t rgba_map[4])
00578 {
00579 int r, c, alpha;
00580 uint8_t *p;
00581 uint8_t src_val;
00582
00583 for (r = 0; r < bitmap->rows && r+y < height; r++) {
00584 for (c = 0; c < bitmap->width && c+x < width; c++) {
00585 if (c+x < 0 || r+y < 0)
00586 continue;
00587
00588 src_val = GET_BITMAP_VAL(r, c);
00589 if (!src_val)
00590 continue;
00591
00592 SET_PIXEL_RGB(picref, rgba_color, src_val, c+x, y+r, pixel_step,
00593 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
00594 }
00595 }
00596
00597 return 0;
00598 }
00599
00600 static inline void drawbox(AVFilterBufferRef *picref, int x, int y,
00601 int width, int height,
00602 uint8_t *line[4], int pixel_step[4], uint8_t color[4],
00603 int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
00604 {
00605 int i, j, alpha;
00606
00607 if (color[3] != 0xFF) {
00608 if (is_rgba_packed) {
00609 uint8_t *p;
00610 for (j = 0; j < height; j++)
00611 for (i = 0; i < width; i++)
00612 SET_PIXEL_RGB(picref, color, 255, i+x, y+j, pixel_step[0],
00613 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
00614 } else {
00615 unsigned int luma_pos, chroma_pos1, chroma_pos2;
00616 for (j = 0; j < height; j++)
00617 for (i = 0; i < width; i++)
00618 SET_PIXEL_YUV(picref, color, 255, i+x, y+j, hsub, vsub);
00619 }
00620 } else {
00621 ff_draw_rectangle(picref->data, picref->linesize,
00622 line, pixel_step, hsub, vsub,
00623 x, y, width, height);
00624 }
00625 }
00626
00627 static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref,
00628 int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
00629 {
00630 char *text = dtext->expanded_text;
00631 uint32_t code = 0;
00632 int i, x1, y1;
00633 uint8_t *p;
00634 Glyph *glyph = NULL;
00635
00636 for (i = 0, p = text; *p; i++) {
00637 Glyph dummy = { 0 };
00638 GET_UTF8(code, *p++, continue;);
00639
00640
00641 if (code == '\n' || code == '\r' || code == '\t')
00642 continue;
00643
00644 dummy.code = code;
00645 glyph = av_tree_find(dtext->glyphs, &dummy, (void *)glyph_cmp, NULL);
00646
00647 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
00648 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
00649 return AVERROR(EINVAL);
00650
00651 x1 = dtext->positions[i].x+dtext->x+x;
00652 y1 = dtext->positions[i].y+dtext->y+y;
00653
00654 if (dtext->is_packed_rgb) {
00655 draw_glyph_rgb(picref, &glyph->bitmap,
00656 x1, y1, width, height,
00657 dtext->pixel_step[0], rgbcolor, dtext->rgba_map);
00658 } else {
00659 draw_glyph_yuv(picref, &glyph->bitmap,
00660 x1, y1, width, height,
00661 yuvcolor, dtext->hsub, dtext->vsub);
00662 }
00663 }
00664
00665 return 0;
00666 }
00667
00668 static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
00669 int width, int height)
00670 {
00671 DrawTextContext *dtext = ctx->priv;
00672 uint32_t code = 0, prev_code = 0;
00673 int x = 0, y = 0, i = 0, ret;
00674 int max_text_line_w = 0, len;
00675 int box_w, box_h;
00676 char *text = dtext->text;
00677 uint8_t *p;
00678 int y_min = 32000, y_max = -32000;
00679 int x_min = 32000, x_max = -32000;
00680 FT_Vector delta;
00681 Glyph *glyph = NULL, *prev_glyph = NULL;
00682 Glyph dummy = { 0 };
00683
00684 time_t now = time(0);
00685 struct tm ltime;
00686 uint8_t *buf = dtext->expanded_text;
00687 int buf_size = dtext->expanded_text_size;
00688
00689 if(dtext->basetime != AV_NOPTS_VALUE)
00690 now= picref->pts*av_q2d(ctx->inputs[0]->time_base) + dtext->basetime/1000000;
00691
00692 if (!buf) {
00693 buf_size = 2*strlen(dtext->text)+1;
00694 buf = av_malloc(buf_size);
00695 }
00696
00697 #if HAVE_LOCALTIME_R
00698 localtime_r(&now, <ime);
00699 #else
00700 if(strchr(dtext->text, '%'))
00701 ltime= *localtime(&now);
00702 #endif
00703
00704 do {
00705 *buf = 1;
00706 if (strftime(buf, buf_size, dtext->text, <ime) != 0 || *buf == 0)
00707 break;
00708 buf_size *= 2;
00709 } while ((buf = av_realloc(buf, buf_size)));
00710
00711 if (!buf)
00712 return AVERROR(ENOMEM);
00713 text = dtext->expanded_text = buf;
00714 dtext->expanded_text_size = buf_size;
00715 if ((len = strlen(text)) > dtext->nb_positions) {
00716 if (!(dtext->positions =
00717 av_realloc(dtext->positions, len*sizeof(*dtext->positions))))
00718 return AVERROR(ENOMEM);
00719 dtext->nb_positions = len;
00720 }
00721
00722 x = 0;
00723 y = 0;
00724
00725
00726 for (i = 0, p = text; *p; i++) {
00727 GET_UTF8(code, *p++, continue;);
00728
00729
00730 dummy.code = code;
00731 glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
00732 if (!glyph)
00733 load_glyph(ctx, &glyph, code);
00734
00735 y_min = FFMIN(glyph->bbox.yMin, y_min);
00736 y_max = FFMAX(glyph->bbox.yMax, y_max);
00737 x_min = FFMIN(glyph->bbox.xMin, x_min);
00738 x_max = FFMAX(glyph->bbox.xMax, x_max);
00739 }
00740 dtext->max_glyph_h = y_max - y_min;
00741 dtext->max_glyph_w = x_max - x_min;
00742
00743
00744 glyph = NULL;
00745 for (i = 0, p = text; *p; i++) {
00746 GET_UTF8(code, *p++, continue;);
00747
00748
00749 if (prev_code == '\r' && code == '\n')
00750 continue;
00751
00752 prev_code = code;
00753 if (is_newline(code)) {
00754 max_text_line_w = FFMAX(max_text_line_w, x);
00755 y += dtext->max_glyph_h;
00756 x = 0;
00757 continue;
00758 }
00759
00760
00761 prev_glyph = glyph;
00762 dummy.code = code;
00763 glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
00764
00765
00766 if (dtext->use_kerning && prev_glyph && glyph->code) {
00767 FT_Get_Kerning(dtext->face, prev_glyph->code, glyph->code,
00768 ft_kerning_default, &delta);
00769 x += delta.x >> 6;
00770 }
00771
00772
00773 dtext->positions[i].x = x + glyph->bitmap_left;
00774 dtext->positions[i].y = y - glyph->bitmap_top + y_max;
00775 if (code == '\t') x = (x / dtext->tabsize + 1)*dtext->tabsize;
00776 else x += glyph->advance;
00777 }
00778
00779 max_text_line_w = FFMAX(x, max_text_line_w);
00780
00781 dtext->var_values[VAR_TW] = dtext->var_values[VAR_TEXT_W] = max_text_line_w;
00782 dtext->var_values[VAR_TH] = dtext->var_values[VAR_TEXT_H] = y + dtext->max_glyph_h;
00783
00784 dtext->var_values[VAR_MAX_GLYPH_W] = dtext->max_glyph_w;
00785 dtext->var_values[VAR_MAX_GLYPH_H] = dtext->max_glyph_h;
00786 dtext->var_values[VAR_MAX_GLYPH_A] = dtext->var_values[VAR_ASCENT ] = y_max;
00787 dtext->var_values[VAR_MAX_GLYPH_D] = dtext->var_values[VAR_DESCENT] = y_min;
00788
00789 dtext->var_values[VAR_LINE_H] = dtext->var_values[VAR_LH] = dtext->max_glyph_h;
00790
00791 dtext->x = dtext->var_values[VAR_X] = av_expr_eval(dtext->x_pexpr, dtext->var_values, &dtext->prng);
00792 dtext->y = dtext->var_values[VAR_Y] = av_expr_eval(dtext->y_pexpr, dtext->var_values, &dtext->prng);
00793 dtext->x = dtext->var_values[VAR_X] = av_expr_eval(dtext->x_pexpr, dtext->var_values, &dtext->prng);
00794 dtext->draw = av_expr_eval(dtext->d_pexpr, dtext->var_values, &dtext->prng);
00795
00796 if(!dtext->draw)
00797 return 0;
00798
00799 dtext->x &= ~((1 << dtext->hsub) - 1);
00800 dtext->y &= ~((1 << dtext->vsub) - 1);
00801
00802 box_w = FFMIN(width - 1 , max_text_line_w);
00803 box_h = FFMIN(height - 1, y + dtext->max_glyph_h);
00804
00805
00806 if (dtext->draw_box)
00807 drawbox(picref, dtext->x, dtext->y, box_w, box_h,
00808 dtext->box_line, dtext->pixel_step, dtext->is_packed_rgb ? dtext->boxcolor_rgba : dtext->boxcolor,
00809 dtext->hsub, dtext->vsub, dtext->is_packed_rgb, dtext->rgba_map);
00810
00811 if (dtext->shadowx || dtext->shadowy) {
00812 if ((ret = draw_glyphs(dtext, picref, width, height, dtext->shadowcolor_rgba,
00813 dtext->shadowcolor, dtext->shadowx, dtext->shadowy)) < 0)
00814 return ret;
00815 }
00816
00817 if ((ret = draw_glyphs(dtext, picref, width, height, dtext->fontcolor_rgba,
00818 dtext->fontcolor, 0, 0)) < 0)
00819 return ret;
00820
00821 return 0;
00822 }
00823
00824 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
00825
00826 static void end_frame(AVFilterLink *inlink)
00827 {
00828 AVFilterContext *ctx = inlink->dst;
00829 AVFilterLink *outlink = ctx->outputs[0];
00830 DrawTextContext *dtext = ctx->priv;
00831 AVFilterBufferRef *picref = inlink->cur_buf;
00832
00833 dtext->var_values[VAR_T] = picref->pts == AV_NOPTS_VALUE ?
00834 NAN : picref->pts * av_q2d(inlink->time_base);
00835
00836 draw_text(ctx, picref, picref->video->w, picref->video->h);
00837
00838 av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
00839 (int)dtext->var_values[VAR_N], dtext->var_values[VAR_T],
00840 (int)dtext->var_values[VAR_TEXT_W], (int)dtext->var_values[VAR_TEXT_H],
00841 dtext->x, dtext->y);
00842
00843 dtext->var_values[VAR_N] += 1.0;
00844
00845 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
00846 avfilter_end_frame(outlink);
00847 }
00848
00849 AVFilter avfilter_vf_drawtext = {
00850 .name = "drawtext",
00851 .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
00852 .priv_size = sizeof(DrawTextContext),
00853 .init = init,
00854 .uninit = uninit,
00855 .query_formats = query_formats,
00856
00857 .inputs = (const AVFilterPad[]) {{ .name = "default",
00858 .type = AVMEDIA_TYPE_VIDEO,
00859 .get_video_buffer = avfilter_null_get_video_buffer,
00860 .start_frame = avfilter_null_start_frame,
00861 .draw_slice = null_draw_slice,
00862 .end_frame = end_frame,
00863 .config_props = config_input,
00864 .min_perms = AV_PERM_WRITE |
00865 AV_PERM_READ,
00866 .rej_perms = AV_PERM_PRESERVE },
00867 { .name = NULL}},
00868 .outputs = (const AVFilterPad[]) {{ .name = "default",
00869 .type = AVMEDIA_TYPE_VIDEO, },
00870 { .name = NULL}},
00871 .process_command = command,
00872 };