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