FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_drawtext.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
4  * Copyright (c) 2003 Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * drawtext filter, based on the original vhook/drawtext.c
26  * filter by Gustavo Sverzut Barbieri
27  */
28 
29 #include "config.h"
30 
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <time.h>
37 #if HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #include <fenv.h>
41 
42 #if CONFIG_LIBFONTCONFIG
43 #include <fontconfig/fontconfig.h>
44 #endif
45 
46 #include "libavutil/avstring.h"
47 #include "libavutil/bprint.h"
48 #include "libavutil/common.h"
49 #include "libavutil/file.h"
50 #include "libavutil/eval.h"
51 #include "libavutil/opt.h"
52 #include "libavutil/random_seed.h"
53 #include "libavutil/parseutils.h"
54 #include "libavutil/timecode.h"
56 #include "libavutil/tree.h"
57 #include "libavutil/lfg.h"
58 #include "avfilter.h"
59 #include "drawutils.h"
60 #include "formats.h"
61 #include "internal.h"
62 #include "video.h"
63 
64 #if CONFIG_LIBFRIBIDI
65 #include <fribidi.h>
66 #endif
67 
68 #include <ft2build.h>
69 #include FT_FREETYPE_H
70 #include FT_GLYPH_H
71 #include FT_STROKER_H
72 
73 static const char *const var_names[] = {
74  "dar",
75  "hsub", "vsub",
76  "line_h", "lh", ///< line height, same as max_glyph_h
77  "main_h", "h", "H", ///< height of the input video
78  "main_w", "w", "W", ///< width of the input video
79  "max_glyph_a", "ascent", ///< max glyph ascent
80  "max_glyph_d", "descent", ///< min glyph descent
81  "max_glyph_h", ///< max glyph height
82  "max_glyph_w", ///< max glyph width
83  "n", ///< number of frame
84  "sar",
85  "t", ///< timestamp expressed in seconds
86  "text_h", "th", ///< height of the rendered text
87  "text_w", "tw", ///< width of the rendered text
88  "x",
89  "y",
90  "pict_type",
91  NULL
92 };
93 
94 static const char *const fun2_names[] = {
95  "rand"
96 };
97 
98 static double drand(void *opaque, double min, double max)
99 {
100  return min + (max-min) / UINT_MAX * av_lfg_get(opaque);
101 }
102 
103 typedef double (*eval_func2)(void *, double a, double b);
104 
105 static const eval_func2 fun2[] = {
106  drand,
107  NULL
108 };
109 
110 enum var_name {
129 };
130 
135 };
136 
137 typedef struct DrawTextContext {
138  const AVClass *class;
139  int exp_mode; ///< expansion mode to use for the text
140  int reinit; ///< tells if the filter is being reinited
141 #if CONFIG_LIBFONTCONFIG
142  uint8_t *font; ///< font to be used
143 #endif
144  uint8_t *fontfile; ///< font to be used
145  uint8_t *text; ///< text to be drawn
146  AVBPrint expanded_text; ///< used to contain the expanded text
147  uint8_t *fontcolor_expr; ///< fontcolor expression to evaluate
148  AVBPrint expanded_fontcolor; ///< used to contain the expanded fontcolor spec
149  int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_*
150  FT_Vector *positions; ///< positions for each element in the text
151  size_t nb_positions; ///< number of elements of positions array
152  char *textfile; ///< file with text to be drawn
153  int x; ///< x position to start drawing text
154  int y; ///< y position to start drawing text
155  int max_glyph_w; ///< max glyph width
156  int max_glyph_h; ///< max glyph height
158  int borderw; ///< border width
159  unsigned int fontsize; ///< font size to use
160 
161  short int draw_box; ///< draw box around text - true or false
162  int boxborderw; ///< box border width
163  int use_kerning; ///< font kerning is used - true/false
164  int tabsize; ///< tab size
165  int fix_bounds; ///< do we let it go out of frame bounds - t/f
166 
168  FFDrawColor fontcolor; ///< foreground color
169  FFDrawColor shadowcolor; ///< shadow color
170  FFDrawColor bordercolor; ///< border color
171  FFDrawColor boxcolor; ///< background color
172 
173  FT_Library library; ///< freetype font library handle
174  FT_Face face; ///< freetype font face handle
175  FT_Stroker stroker; ///< freetype stroker handle
176  struct AVTreeNode *glyphs; ///< rendered glyphs, stored using the UTF-32 char code
177  char *x_expr; ///< expression for x position
178  char *y_expr; ///< expression for y position
179  AVExpr *x_pexpr, *y_pexpr; ///< parsed expressions for x and y
180  int64_t basetime; ///< base pts time in the real world for display
182  char *a_expr;
184  int alpha;
185  AVLFG prng; ///< random
186  char *tc_opt_string; ///< specified timecode option string
187  AVRational tc_rate; ///< frame rate for timecode
188  AVTimecode tc; ///< timecode context
189  int tc24hmax; ///< 1 if timecode is wrapped to 24 hours, 0 otherwise
190  int reload; ///< reload text file for each frame
191  int start_number; ///< starting frame number for n/frame_num var
192 #if CONFIG_LIBFRIBIDI
193  int text_shaping; ///< 1 to shape the text before drawing it
194 #endif
197 
198 #define OFFSET(x) offsetof(DrawTextContext, x)
199 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
200 
201 static const AVOption drawtext_options[]= {
202  {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
203  {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
204  {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
205  {"fontcolor", "set foreground color", OFFSET(fontcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
206  {"fontcolor_expr", "set foreground color expression", OFFSET(fontcolor_expr), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS},
207  {"boxcolor", "set box color", OFFSET(boxcolor.rgba), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
208  {"bordercolor", "set border color", OFFSET(bordercolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
209  {"shadowcolor", "set shadow color", OFFSET(shadowcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
210  {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 , FLAGS},
211  {"boxborderw", "set box border width", OFFSET(boxborderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
212  {"fontsize", "set font size", OFFSET(fontsize), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX , FLAGS},
213  {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
214  {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
215  {"shadowx", "set shadow x offset", OFFSET(shadowx), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
216  {"shadowy", "set shadow y offset", OFFSET(shadowy), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
217  {"borderw", "set border width", OFFSET(borderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
218  {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX , FLAGS},
219  {"basetime", "set base time", OFFSET(basetime), AV_OPT_TYPE_INT64, {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX , FLAGS},
220 #if CONFIG_LIBFONTCONFIG
221  { "font", "Font name", OFFSET(font), AV_OPT_TYPE_STRING, { .str = "Sans" }, .flags = FLAGS },
222 #endif
223 
224  {"expansion", "set the expansion mode", OFFSET(exp_mode), AV_OPT_TYPE_INT, {.i64=EXP_NORMAL}, 0, 2, FLAGS, "expansion"},
225  {"none", "set no expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NONE}, 0, 0, FLAGS, "expansion"},
226  {"normal", "set normal expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NORMAL}, 0, 0, FLAGS, "expansion"},
227  {"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0, 0, FLAGS, "expansion"},
228 
229  {"timecode", "set initial timecode", OFFSET(tc_opt_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
230  {"tc24hmax", "set 24 hours max (timecode only)", OFFSET(tc24hmax), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
231  {"timecode_rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
232  {"r", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
233  {"rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
234  {"reload", "reload text file for each frame", OFFSET(reload), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
235  { "alpha", "apply alpha while rendering", OFFSET(a_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
236  {"fix_bounds", "check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
237  {"start_number", "start frame number for n/frame_num variable", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS},
238 
239 #if CONFIG_LIBFRIBIDI
240  {"text_shaping", "attempt to shape text before drawing", OFFSET(text_shaping), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
241 #endif
242 
243  /* FT_LOAD_* flags */
244  { "ft_load_flags", "set font loading flags for libfreetype", OFFSET(ft_load_flags), AV_OPT_TYPE_FLAGS, { .i64 = FT_LOAD_DEFAULT }, 0, INT_MAX, FLAGS, "ft_load_flags" },
245  { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags = FLAGS, .unit = "ft_load_flags" },
246  { "no_scale", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags = FLAGS, .unit = "ft_load_flags" },
247  { "no_hinting", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags = FLAGS, .unit = "ft_load_flags" },
248  { "render", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags = FLAGS, .unit = "ft_load_flags" },
249  { "no_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
250  { "vertical_layout", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags = FLAGS, .unit = "ft_load_flags" },
251  { "force_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
252  { "crop_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
253  { "pedantic", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags = FLAGS, .unit = "ft_load_flags" },
254  { "ignore_global_advance_width", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags = FLAGS, .unit = "ft_load_flags" },
255  { "no_recurse", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags = FLAGS, .unit = "ft_load_flags" },
256  { "ignore_transform", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags = FLAGS, .unit = "ft_load_flags" },
257  { "monochrome", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags = FLAGS, .unit = "ft_load_flags" },
258  { "linear_design", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags = FLAGS, .unit = "ft_load_flags" },
259  { "no_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
260  { NULL }
261 };
262 
264 
265 #undef __FTERRORS_H__
266 #define FT_ERROR_START_LIST {
267 #define FT_ERRORDEF(e, v, s) { (e), (s) },
268 #define FT_ERROR_END_LIST { 0, NULL } };
269 
270 static const struct ft_error
271 {
272  int err;
273  const char *err_msg;
274 } ft_errors[] =
275 #include FT_ERRORS_H
276 
277 #define FT_ERRMSG(e) ft_errors[e].err_msg
278 
279 typedef struct Glyph {
280  FT_Glyph glyph;
281  FT_Glyph border_glyph;
282  uint32_t code;
283  FT_Bitmap bitmap; ///< array holding bitmaps of font
284  FT_Bitmap border_bitmap; ///< array holding bitmaps of font border
285  FT_BBox bbox;
286  int advance;
287  int bitmap_left;
288  int bitmap_top;
289 } Glyph;
290 
291 static int glyph_cmp(const void *key, const void *b)
292 {
293  const Glyph *a = key, *bb = b;
294  int64_t diff = (int64_t)a->code - (int64_t)bb->code;
295  return diff > 0 ? 1 : diff < 0 ? -1 : 0;
296 }
297 
298 /**
299  * Load glyphs corresponding to the UTF-32 codepoint code.
300  */
301 static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
302 {
303  DrawTextContext *s = ctx->priv;
304  FT_BitmapGlyph bitmapglyph;
305  Glyph *glyph;
306  struct AVTreeNode *node = NULL;
307  int ret;
308 
309  /* load glyph into s->face->glyph */
310  if (FT_Load_Char(s->face, code, s->ft_load_flags))
311  return AVERROR(EINVAL);
312 
313  glyph = av_mallocz(sizeof(*glyph));
314  if (!glyph) {
315  ret = AVERROR(ENOMEM);
316  goto error;
317  }
318  glyph->code = code;
319 
320  if (FT_Get_Glyph(s->face->glyph, &glyph->glyph)) {
321  ret = AVERROR(EINVAL);
322  goto error;
323  }
324  if (s->borderw) {
325  glyph->border_glyph = glyph->glyph;
326  if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0) ||
327  FT_Glyph_To_Bitmap(&glyph->border_glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
328  ret = AVERROR_EXTERNAL;
329  goto error;
330  }
331  bitmapglyph = (FT_BitmapGlyph) glyph->border_glyph;
332  glyph->border_bitmap = bitmapglyph->bitmap;
333  }
334  if (FT_Glyph_To_Bitmap(&glyph->glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
335  ret = AVERROR_EXTERNAL;
336  goto error;
337  }
338  bitmapglyph = (FT_BitmapGlyph) glyph->glyph;
339 
340  glyph->bitmap = bitmapglyph->bitmap;
341  glyph->bitmap_left = bitmapglyph->left;
342  glyph->bitmap_top = bitmapglyph->top;
343  glyph->advance = s->face->glyph->advance.x >> 6;
344 
345  /* measure text height to calculate text_height (or the maximum text height) */
346  FT_Glyph_Get_CBox(glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
347 
348  /* cache the newly created glyph */
349  if (!(node = av_tree_node_alloc())) {
350  ret = AVERROR(ENOMEM);
351  goto error;
352  }
353  av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
354 
355  if (glyph_ptr)
356  *glyph_ptr = glyph;
357  return 0;
358 
359 error:
360  if (glyph)
361  av_freep(&glyph->glyph);
362 
363  av_freep(&glyph);
364  av_freep(&node);
365  return ret;
366 }
367 
368 static int load_font_file(AVFilterContext *ctx, const char *path, int index)
369 {
370  DrawTextContext *s = ctx->priv;
371  int err;
372 
373  err = FT_New_Face(s->library, path, index, &s->face);
374  if (err) {
375 #if !CONFIG_LIBFONTCONFIG
376  av_log(ctx, AV_LOG_ERROR, "Could not load font \"%s\": %s\n",
377  s->fontfile, FT_ERRMSG(err));
378 #endif
379  return AVERROR(EINVAL);
380  }
381  return 0;
382 }
383 
384 #if CONFIG_LIBFONTCONFIG
385 static int load_font_fontconfig(AVFilterContext *ctx)
386 {
387  DrawTextContext *s = ctx->priv;
388  FcConfig *fontconfig;
389  FcPattern *pat, *best;
390  FcResult result = FcResultMatch;
391  FcChar8 *filename;
392  int index;
393  double size;
394  int err = AVERROR(ENOENT);
395 
396  fontconfig = FcInitLoadConfigAndFonts();
397  if (!fontconfig) {
398  av_log(ctx, AV_LOG_ERROR, "impossible to init fontconfig\n");
399  return AVERROR_UNKNOWN;
400  }
401  pat = FcNameParse(s->fontfile ? s->fontfile :
402  (uint8_t *)(intptr_t)"default");
403  if (!pat) {
404  av_log(ctx, AV_LOG_ERROR, "could not parse fontconfig pat");
405  return AVERROR(EINVAL);
406  }
407 
408  FcPatternAddString(pat, FC_FAMILY, s->font);
409  if (s->fontsize)
410  FcPatternAddDouble(pat, FC_SIZE, (double)s->fontsize);
411 
412  FcDefaultSubstitute(pat);
413 
414  if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
415  av_log(ctx, AV_LOG_ERROR, "could not substitue fontconfig options"); /* very unlikely */
416  FcPatternDestroy(pat);
417  return AVERROR(ENOMEM);
418  }
419 
420  best = FcFontMatch(fontconfig, pat, &result);
421  FcPatternDestroy(pat);
422 
423  if (!best || result != FcResultMatch) {
424  av_log(ctx, AV_LOG_ERROR,
425  "Cannot find a valid font for the family %s\n",
426  s->font);
427  goto fail;
428  }
429 
430  if (
431  FcPatternGetInteger(best, FC_INDEX, 0, &index ) != FcResultMatch ||
432  FcPatternGetDouble (best, FC_SIZE, 0, &size ) != FcResultMatch) {
433  av_log(ctx, AV_LOG_ERROR, "impossible to find font information");
434  return AVERROR(EINVAL);
435  }
436 
437  if (FcPatternGetString(best, FC_FILE, 0, &filename) != FcResultMatch) {
438  av_log(ctx, AV_LOG_ERROR, "No file path for %s\n",
439  s->font);
440  goto fail;
441  }
442 
443  av_log(ctx, AV_LOG_INFO, "Using \"%s\"\n", filename);
444  if (!s->fontsize)
445  s->fontsize = size + 0.5;
446 
447  err = load_font_file(ctx, filename, index);
448  if (err)
449  return err;
450  FcConfigDestroy(fontconfig);
451 fail:
452  FcPatternDestroy(best);
453  return err;
454 }
455 #endif
456 
457 static int load_font(AVFilterContext *ctx)
458 {
459  DrawTextContext *s = ctx->priv;
460  int err;
461 
462  /* load the face, and set up the encoding, which is by default UTF-8 */
463  err = load_font_file(ctx, s->fontfile, 0);
464  if (!err)
465  return 0;
466 #if CONFIG_LIBFONTCONFIG
467  err = load_font_fontconfig(ctx);
468  if (!err)
469  return 0;
470 #endif
471  return err;
472 }
473 
475 {
476  DrawTextContext *s = ctx->priv;
477  int err;
478  uint8_t *textbuf;
479  uint8_t *tmp;
480  size_t textbuf_size;
481 
482  if ((err = av_file_map(s->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
483  av_log(ctx, AV_LOG_ERROR,
484  "The text file '%s' could not be read or is empty\n",
485  s->textfile);
486  return err;
487  }
488 
489  if (textbuf_size > SIZE_MAX - 1 || !(tmp = av_realloc(s->text, textbuf_size + 1))) {
490  av_file_unmap(textbuf, textbuf_size);
491  return AVERROR(ENOMEM);
492  }
493  s->text = tmp;
494  memcpy(s->text, textbuf, textbuf_size);
495  s->text[textbuf_size] = 0;
496  av_file_unmap(textbuf, textbuf_size);
497 
498  return 0;
499 }
500 
501 static inline int is_newline(uint32_t c)
502 {
503  return c == '\n' || c == '\r' || c == '\f' || c == '\v';
504 }
505 
506 #if CONFIG_LIBFRIBIDI
507 static int shape_text(AVFilterContext *ctx)
508 {
509  DrawTextContext *s = ctx->priv;
510  uint8_t *tmp;
511  int ret = AVERROR(ENOMEM);
512  static const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT |
513  FRIBIDI_FLAGS_ARABIC;
514  FriBidiChar *unicodestr = NULL;
515  FriBidiStrIndex len;
516  FriBidiParType direction = FRIBIDI_PAR_LTR;
517  FriBidiStrIndex line_start = 0;
518  FriBidiStrIndex line_end = 0;
519  FriBidiLevel *embedding_levels = NULL;
520  FriBidiArabicProp *ar_props = NULL;
521  FriBidiCharType *bidi_types = NULL;
522  FriBidiStrIndex i,j;
523 
524  len = strlen(s->text);
525  if (!(unicodestr = av_malloc_array(len, sizeof(*unicodestr)))) {
526  goto out;
527  }
528  len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8,
529  s->text, len, unicodestr);
530 
531  bidi_types = av_malloc_array(len, sizeof(*bidi_types));
532  if (!bidi_types) {
533  goto out;
534  }
535 
536  fribidi_get_bidi_types(unicodestr, len, bidi_types);
537 
538  embedding_levels = av_malloc_array(len, sizeof(*embedding_levels));
539  if (!embedding_levels) {
540  goto out;
541  }
542 
543  if (!fribidi_get_par_embedding_levels(bidi_types, len, &direction,
544  embedding_levels)) {
545  goto out;
546  }
547 
548  ar_props = av_malloc_array(len, sizeof(*ar_props));
549  if (!ar_props) {
550  goto out;
551  }
552 
553  fribidi_get_joining_types(unicodestr, len, ar_props);
554  fribidi_join_arabic(bidi_types, len, embedding_levels, ar_props);
555  fribidi_shape(flags, embedding_levels, len, ar_props, unicodestr);
556 
557  for (line_end = 0, line_start = 0; line_end < len; line_end++) {
558  if (is_newline(unicodestr[line_end]) || line_end == len - 1) {
559  if (!fribidi_reorder_line(flags, bidi_types,
560  line_end - line_start + 1, line_start,
561  direction, embedding_levels, unicodestr,
562  NULL)) {
563  goto out;
564  }
565  line_start = line_end + 1;
566  }
567  }
568 
569  /* Remove zero-width fill chars put in by libfribidi */
570  for (i = 0, j = 0; i < len; i++)
571  if (unicodestr[i] != FRIBIDI_CHAR_FILL)
572  unicodestr[j++] = unicodestr[i];
573  len = j;
574 
575  if (!(tmp = av_realloc(s->text, (len * 4 + 1) * sizeof(*s->text)))) {
576  /* Use len * 4, as a unicode character can be up to 4 bytes in UTF-8 */
577  goto out;
578  }
579 
580  s->text = tmp;
581  len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8,
582  unicodestr, len, s->text);
583  ret = 0;
584 
585 out:
586  av_free(unicodestr);
587  av_free(embedding_levels);
588  av_free(ar_props);
589  av_free(bidi_types);
590  return ret;
591 }
592 #endif
593 
594 static av_cold int init(AVFilterContext *ctx)
595 {
596  int err;
597  DrawTextContext *s = ctx->priv;
598  Glyph *glyph;
599 
600  if (!s->fontfile && !CONFIG_LIBFONTCONFIG) {
601  av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
602  return AVERROR(EINVAL);
603  }
604 
605  if (s->textfile) {
606  if (s->text) {
607  av_log(ctx, AV_LOG_ERROR,
608  "Both text and text file provided. Please provide only one\n");
609  return AVERROR(EINVAL);
610  }
611  if ((err = load_textfile(ctx)) < 0)
612  return err;
613  }
614 
615  if (s->reload && !s->textfile)
616  av_log(ctx, AV_LOG_WARNING, "No file to reload\n");
617 
618  if (s->tc_opt_string) {
619  int ret = av_timecode_init_from_string(&s->tc, s->tc_rate,
620  s->tc_opt_string, ctx);
621  if (ret < 0)
622  return ret;
623  if (s->tc24hmax)
625  if (!s->text)
626  s->text = av_strdup("");
627  }
628 
629  if (!s->text) {
630  av_log(ctx, AV_LOG_ERROR,
631  "Either text, a valid file or a timecode must be provided\n");
632  return AVERROR(EINVAL);
633  }
634 
635 #if CONFIG_LIBFRIBIDI
636  if (s->text_shaping)
637  if ((err = shape_text(ctx)) < 0)
638  return err;
639 #endif
640 
641  if ((err = FT_Init_FreeType(&(s->library)))) {
642  av_log(ctx, AV_LOG_ERROR,
643  "Could not load FreeType: %s\n", FT_ERRMSG(err));
644  return AVERROR(EINVAL);
645  }
646 
647  err = load_font(ctx);
648  if (err)
649  return err;
650  if (!s->fontsize)
651  s->fontsize = 16;
652  if ((err = FT_Set_Pixel_Sizes(s->face, 0, s->fontsize))) {
653  av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
654  s->fontsize, FT_ERRMSG(err));
655  return AVERROR(EINVAL);
656  }
657 
658  if (s->borderw) {
659  if (FT_Stroker_New(s->library, &s->stroker)) {
660  av_log(ctx, AV_LOG_ERROR, "Coult not init FT stroker\n");
661  return AVERROR_EXTERNAL;
662  }
663  FT_Stroker_Set(s->stroker, s->borderw << 6, FT_STROKER_LINECAP_ROUND,
664  FT_STROKER_LINEJOIN_ROUND, 0);
665  }
666 
667  s->use_kerning = FT_HAS_KERNING(s->face);
668 
669  /* load the fallback glyph with code 0 */
670  load_glyph(ctx, NULL, 0);
671 
672  /* set the tabsize in pixels */
673  if ((err = load_glyph(ctx, &glyph, ' ')) < 0) {
674  av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
675  return err;
676  }
677  s->tabsize *= glyph->advance;
678 
679  if (s->exp_mode == EXP_STRFTIME &&
680  (strchr(s->text, '%') || strchr(s->text, '\\')))
681  av_log(ctx, AV_LOG_WARNING, "expansion=strftime is deprecated.\n");
682 
685 
686  return 0;
687 }
688 
690 {
692 }
693 
694 static int glyph_enu_free(void *opaque, void *elem)
695 {
696  Glyph *glyph = elem;
697 
698  FT_Done_Glyph(glyph->glyph);
699  FT_Done_Glyph(glyph->border_glyph);
700  av_free(elem);
701  return 0;
702 }
703 
704 static av_cold void uninit(AVFilterContext *ctx)
705 {
706  DrawTextContext *s = ctx->priv;
707 
708  av_expr_free(s->x_pexpr);
709  av_expr_free(s->y_pexpr);
710  s->x_pexpr = s->y_pexpr = NULL;
711  av_freep(&s->positions);
712  s->nb_positions = 0;
713 
714 
717  s->glyphs = NULL;
718 
719  FT_Done_Face(s->face);
720  FT_Stroker_Done(s->stroker);
721  FT_Done_FreeType(s->library);
722 
725 }
726 
727 static int config_input(AVFilterLink *inlink)
728 {
729  AVFilterContext *ctx = inlink->dst;
730  DrawTextContext *s = ctx->priv;
731  int ret;
732 
733  ff_draw_init(&s->dc, inlink->format, 0);
734  ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
737  ff_draw_color(&s->dc, &s->boxcolor, s->boxcolor.rgba);
738 
739  s->var_values[VAR_w] = s->var_values[VAR_W] = s->var_values[VAR_MAIN_W] = inlink->w;
740  s->var_values[VAR_h] = s->var_values[VAR_H] = s->var_values[VAR_MAIN_H] = inlink->h;
742  s->var_values[VAR_DAR] = (double)inlink->w / inlink->h * s->var_values[VAR_SAR];
743  s->var_values[VAR_HSUB] = 1 << s->dc.hsub_max;
744  s->var_values[VAR_VSUB] = 1 << s->dc.vsub_max;
745  s->var_values[VAR_X] = NAN;
746  s->var_values[VAR_Y] = NAN;
747  s->var_values[VAR_T] = NAN;
748 
750 
751  av_expr_free(s->x_pexpr);
752  av_expr_free(s->y_pexpr);
753  s->x_pexpr = s->y_pexpr = NULL;
754 
755  if ((ret = av_expr_parse(&s->x_pexpr, s->x_expr, var_names,
756  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
757  (ret = av_expr_parse(&s->y_pexpr, s->y_expr, var_names,
758  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
759  (ret = av_expr_parse(&s->a_pexpr, s->a_expr, var_names,
760  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
761 
762  return AVERROR(EINVAL);
763 
764  return 0;
765 }
766 
767 static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
768 {
769  DrawTextContext *s = ctx->priv;
770 
771  if (!strcmp(cmd, "reinit")) {
772  int ret;
773  uninit(ctx);
774  s->reinit = 1;
775  if ((ret = av_set_options_string(ctx, arg, "=", ":")) < 0)
776  return ret;
777  if ((ret = init(ctx)) < 0)
778  return ret;
779  return config_input(ctx->inputs[0]);
780  }
781 
782  return AVERROR(ENOSYS);
783 }
784 
785 static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp,
786  char *fct, unsigned argc, char **argv, int tag)
787 {
788  DrawTextContext *s = ctx->priv;
789 
791  return 0;
792 }
793 
794 static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
795  char *fct, unsigned argc, char **argv, int tag)
796 {
797  DrawTextContext *s = ctx->priv;
798  const char *fmt;
799  double pts = s->var_values[VAR_T];
800  int ret;
801 
802  fmt = argc >= 1 ? argv[0] : "flt";
803  if (argc >= 2) {
804  int64_t delta;
805  if ((ret = av_parse_time(&delta, argv[1], 1)) < 0) {
806  av_log(ctx, AV_LOG_ERROR, "Invalid delta '%s'\n", argv[1]);
807  return ret;
808  }
809  pts += (double)delta / AV_TIME_BASE;
810  }
811  if (!strcmp(fmt, "flt")) {
812  av_bprintf(bp, "%.6f", s->var_values[VAR_T]);
813  } else if (!strcmp(fmt, "hms")) {
814  if (isnan(pts)) {
815  av_bprintf(bp, " ??:??:??.???");
816  } else {
817  int64_t ms = llrint(pts * 1000);
818  char sign = ' ';
819  if (ms < 0) {
820  sign = '-';
821  ms = -ms;
822  }
823  av_bprintf(bp, "%c%02d:%02d:%02d.%03d", sign,
824  (int)(ms / (60 * 60 * 1000)),
825  (int)(ms / (60 * 1000)) % 60,
826  (int)(ms / 1000) % 60,
827  (int)(ms % 1000));
828  }
829  } else if (!strcmp(fmt, "localtime") ||
830  !strcmp(fmt, "gmtime")) {
831  struct tm tm;
832  time_t ms = (time_t)pts;
833  const char *timefmt = argc >= 3 ? argv[2] : "%Y-%m-%d %H:%M:%S";
834  if (!strcmp(fmt, "localtime"))
835  localtime_r(&ms, &tm);
836  else
837  gmtime_r(&ms, &tm);
838  av_bprint_strftime(bp, timefmt, &tm);
839  } else {
840  av_log(ctx, AV_LOG_ERROR, "Invalid format '%s'\n", fmt);
841  return AVERROR(EINVAL);
842  }
843  return 0;
844 }
845 
846 static int func_frame_num(AVFilterContext *ctx, AVBPrint *bp,
847  char *fct, unsigned argc, char **argv, int tag)
848 {
849  DrawTextContext *s = ctx->priv;
850 
851  av_bprintf(bp, "%d", (int)s->var_values[VAR_N]);
852  return 0;
853 }
854 
855 static int func_metadata(AVFilterContext *ctx, AVBPrint *bp,
856  char *fct, unsigned argc, char **argv, int tag)
857 {
858  DrawTextContext *s = ctx->priv;
859  AVDictionaryEntry *e = av_dict_get(s->metadata, argv[0], NULL, 0);
860 
861  if (e && e->value)
862  av_bprintf(bp, "%s", e->value);
863  return 0;
864 }
865 
866 static int func_strftime(AVFilterContext *ctx, AVBPrint *bp,
867  char *fct, unsigned argc, char **argv, int tag)
868 {
869  const char *fmt = argc ? argv[0] : "%Y-%m-%d %H:%M:%S";
870  time_t now;
871  struct tm tm;
872 
873  time(&now);
874  if (tag == 'L')
875  localtime_r(&now, &tm);
876  else
877  tm = *gmtime_r(&now, &tm);
878  av_bprint_strftime(bp, fmt, &tm);
879  return 0;
880 }
881 
882 static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp,
883  char *fct, unsigned argc, char **argv, int tag)
884 {
885  DrawTextContext *s = ctx->priv;
886  double res;
887  int ret;
888 
889  ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
891  &s->prng, 0, ctx);
892  if (ret < 0)
893  av_log(ctx, AV_LOG_ERROR,
894  "Expression '%s' for the expr text expansion function is not valid\n",
895  argv[0]);
896  else
897  av_bprintf(bp, "%f", res);
898 
899  return ret;
900 }
901 
902 static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp,
903  char *fct, unsigned argc, char **argv, int tag)
904 {
905  DrawTextContext *s = ctx->priv;
906  double res;
907  int intval;
908  int ret;
909  unsigned int positions = 0;
910  char fmt_str[30] = "%";
911 
912  /*
913  * argv[0] expression to be converted to `int`
914  * argv[1] format: 'x', 'X', 'd' or 'u'
915  * argv[2] positions printed (optional)
916  */
917 
918  ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
920  &s->prng, 0, ctx);
921  if (ret < 0) {
922  av_log(ctx, AV_LOG_ERROR,
923  "Expression '%s' for the expr text expansion function is not valid\n",
924  argv[0]);
925  return ret;
926  }
927 
928  if (!strchr("xXdu", argv[1][0])) {
929  av_log(ctx, AV_LOG_ERROR, "Invalid format '%c' specified,"
930  " allowed values: 'x', 'X', 'd', 'u'\n", argv[1][0]);
931  return AVERROR(EINVAL);
932  }
933 
934  if (argc == 3) {
935  ret = sscanf(argv[2], "%u", &positions);
936  if (ret != 1) {
937  av_log(ctx, AV_LOG_ERROR, "expr_int_format(): Invalid number of positions"
938  " to print: '%s'\n", argv[2]);
939  return AVERROR(EINVAL);
940  }
941  }
942 
943  feclearexcept(FE_ALL_EXCEPT);
944  intval = res;
945  if ((ret = fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW))) {
946  av_log(ctx, AV_LOG_ERROR, "Conversion of floating-point result to int failed. Control register: 0x%08x. Conversion result: %d\n", ret, intval);
947  return AVERROR(EINVAL);
948  }
949 
950  if (argc == 3)
951  av_strlcatf(fmt_str, sizeof(fmt_str), "0%u", positions);
952  av_strlcatf(fmt_str, sizeof(fmt_str), "%c", argv[1][0]);
953 
954  av_log(ctx, AV_LOG_DEBUG, "Formatting value %f (expr '%s') with spec '%s'\n",
955  res, argv[0], fmt_str);
956 
957  av_bprintf(bp, fmt_str, intval);
958 
959  return 0;
960 }
961 
962 static const struct drawtext_function {
963  const char *name;
964  unsigned argc_min, argc_max;
965  int tag; /**< opaque argument to func */
966  int (*func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int);
967 } functions[] = {
968  { "expr", 1, 1, 0, func_eval_expr },
969  { "e", 1, 1, 0, func_eval_expr },
970  { "expr_int_format", 2, 3, 0, func_eval_expr_int_format },
971  { "eif", 2, 3, 0, func_eval_expr_int_format },
972  { "pict_type", 0, 0, 0, func_pict_type },
973  { "pts", 0, 3, 0, func_pts },
974  { "gmtime", 0, 1, 'G', func_strftime },
975  { "localtime", 0, 1, 'L', func_strftime },
976  { "frame_num", 0, 0, 0, func_frame_num },
977  { "n", 0, 0, 0, func_frame_num },
978  { "metadata", 1, 1, 0, func_metadata },
979 };
980 
981 static int eval_function(AVFilterContext *ctx, AVBPrint *bp, char *fct,
982  unsigned argc, char **argv)
983 {
984  unsigned i;
985 
986  for (i = 0; i < FF_ARRAY_ELEMS(functions); i++) {
987  if (strcmp(fct, functions[i].name))
988  continue;
989  if (argc < functions[i].argc_min) {
990  av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at least %d arguments\n",
991  fct, functions[i].argc_min);
992  return AVERROR(EINVAL);
993  }
994  if (argc > functions[i].argc_max) {
995  av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at most %d arguments\n",
996  fct, functions[i].argc_max);
997  return AVERROR(EINVAL);
998  }
999  break;
1000  }
1001  if (i >= FF_ARRAY_ELEMS(functions)) {
1002  av_log(ctx, AV_LOG_ERROR, "%%{%s} is not known\n", fct);
1003  return AVERROR(EINVAL);
1004  }
1005  return functions[i].func(ctx, bp, fct, argc, argv, functions[i].tag);
1006 }
1007 
1008 static int expand_function(AVFilterContext *ctx, AVBPrint *bp, char **rtext)
1009 {
1010  const char *text = *rtext;
1011  char *argv[16] = { NULL };
1012  unsigned argc = 0, i;
1013  int ret;
1014 
1015  if (*text != '{') {
1016  av_log(ctx, AV_LOG_ERROR, "Stray %% near '%s'\n", text);
1017  return AVERROR(EINVAL);
1018  }
1019  text++;
1020  while (1) {
1021  if (!(argv[argc++] = av_get_token(&text, ":}"))) {
1022  ret = AVERROR(ENOMEM);
1023  goto end;
1024  }
1025  if (!*text) {
1026  av_log(ctx, AV_LOG_ERROR, "Unterminated %%{} near '%s'\n", *rtext);
1027  ret = AVERROR(EINVAL);
1028  goto end;
1029  }
1030  if (argc == FF_ARRAY_ELEMS(argv))
1031  av_freep(&argv[--argc]); /* error will be caught later */
1032  if (*text == '}')
1033  break;
1034  text++;
1035  }
1036 
1037  if ((ret = eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
1038  goto end;
1039  ret = 0;
1040  *rtext = (char *)text + 1;
1041 
1042 end:
1043  for (i = 0; i < argc; i++)
1044  av_freep(&argv[i]);
1045  return ret;
1046 }
1047 
1048 static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
1049 {
1050  int ret;
1051 
1052  av_bprint_clear(bp);
1053  while (*text) {
1054  if (*text == '\\' && text[1]) {
1055  av_bprint_chars(bp, text[1], 1);
1056  text += 2;
1057  } else if (*text == '%') {
1058  text++;
1059  if ((ret = expand_function(ctx, bp, &text)) < 0)
1060  return ret;
1061  } else {
1062  av_bprint_chars(bp, *text, 1);
1063  text++;
1064  }
1065  }
1066  if (!av_bprint_is_complete(bp))
1067  return AVERROR(ENOMEM);
1068  return 0;
1069 }
1070 
1072  int width, int height,
1073  FFDrawColor *color,
1074  int x, int y, int borderw)
1075 {
1076  char *text = s->expanded_text.str;
1077  uint32_t code = 0;
1078  int i, x1, y1;
1079  uint8_t *p;
1080  Glyph *glyph = NULL;
1081 
1082  for (i = 0, p = text; *p; i++) {
1083  FT_Bitmap bitmap;
1084  Glyph dummy = { 0 };
1085  GET_UTF8(code, *p++, continue;);
1086 
1087  /* skip new line chars, just go to new line */
1088  if (code == '\n' || code == '\r' || code == '\t')
1089  continue;
1090 
1091  dummy.code = code;
1092  glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1093 
1094  bitmap = borderw ? glyph->border_bitmap : glyph->bitmap;
1095 
1096  if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
1097  glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
1098  return AVERROR(EINVAL);
1099 
1100  x1 = s->positions[i].x+s->x+x - borderw;
1101  y1 = s->positions[i].y+s->y+y - borderw;
1102 
1103  ff_blend_mask(&s->dc, color,
1104  frame->data, frame->linesize, width, height,
1105  bitmap.buffer, bitmap.pitch,
1106  bitmap.width, bitmap.rows,
1107  bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
1108  0, x1, y1);
1109  }
1110 
1111  return 0;
1112 }
1113 
1114 
1116 {
1117  *color = incolor;
1118  color->rgba[3] = (color->rgba[3] * s->alpha) / 255;
1119  ff_draw_color(&s->dc, color, color->rgba);
1120 }
1121 
1123 {
1124  double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
1125 
1126  if (isnan(alpha))
1127  return;
1128 
1129  if (alpha >= 1.0)
1130  s->alpha = 255;
1131  else if (alpha <= 0)
1132  s->alpha = 0;
1133  else
1134  s->alpha = 256 * alpha;
1135 }
1136 
1138  int width, int height)
1139 {
1140  DrawTextContext *s = ctx->priv;
1141  AVFilterLink *inlink = ctx->inputs[0];
1142 
1143  uint32_t code = 0, prev_code = 0;
1144  int x = 0, y = 0, i = 0, ret;
1145  int max_text_line_w = 0, len;
1146  int box_w, box_h;
1147  char *text;
1148  uint8_t *p;
1149  int y_min = 32000, y_max = -32000;
1150  int x_min = 32000, x_max = -32000;
1151  FT_Vector delta;
1152  Glyph *glyph = NULL, *prev_glyph = NULL;
1153  Glyph dummy = { 0 };
1154 
1155  time_t now = time(0);
1156  struct tm ltime;
1157  AVBPrint *bp = &s->expanded_text;
1158 
1159  FFDrawColor fontcolor;
1160  FFDrawColor shadowcolor;
1161  FFDrawColor bordercolor;
1162  FFDrawColor boxcolor;
1163 
1164  av_bprint_clear(bp);
1165 
1166  if(s->basetime != AV_NOPTS_VALUE)
1167  now= frame->pts*av_q2d(ctx->inputs[0]->time_base) + s->basetime/1000000;
1168 
1169  switch (s->exp_mode) {
1170  case EXP_NONE:
1171  av_bprintf(bp, "%s", s->text);
1172  break;
1173  case EXP_NORMAL:
1174  if ((ret = expand_text(ctx, s->text, &s->expanded_text)) < 0)
1175  return ret;
1176  break;
1177  case EXP_STRFTIME:
1178  localtime_r(&now, &ltime);
1179  av_bprint_strftime(bp, s->text, &ltime);
1180  break;
1181  }
1182 
1183  if (s->tc_opt_string) {
1184  char tcbuf[AV_TIMECODE_STR_SIZE];
1185  av_timecode_make_string(&s->tc, tcbuf, inlink->frame_count);
1186  av_bprint_clear(bp);
1187  av_bprintf(bp, "%s%s", s->text, tcbuf);
1188  }
1189 
1190  if (!av_bprint_is_complete(bp))
1191  return AVERROR(ENOMEM);
1192  text = s->expanded_text.str;
1193  if ((len = s->expanded_text.len) > s->nb_positions) {
1194  if (!(s->positions =
1195  av_realloc(s->positions, len*sizeof(*s->positions))))
1196  return AVERROR(ENOMEM);
1197  s->nb_positions = len;
1198  }
1199 
1200  if (s->fontcolor_expr[0]) {
1201  /* If expression is set, evaluate and replace the static value */
1203  if ((ret = expand_text(ctx, s->fontcolor_expr, &s->expanded_fontcolor)) < 0)
1204  return ret;
1206  return AVERROR(ENOMEM);
1207  av_log(s, AV_LOG_DEBUG, "Evaluated fontcolor is '%s'\n", s->expanded_fontcolor.str);
1208  ret = av_parse_color(s->fontcolor.rgba, s->expanded_fontcolor.str, -1, s);
1209  if (ret)
1210  return ret;
1211  ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
1212  }
1213 
1214  x = 0;
1215  y = 0;
1216 
1217  /* load and cache glyphs */
1218  for (i = 0, p = text; *p; i++) {
1219  GET_UTF8(code, *p++, continue;);
1220 
1221  /* get glyph */
1222  dummy.code = code;
1223  glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1224  if (!glyph) {
1225  load_glyph(ctx, &glyph, code);
1226  }
1227 
1228  y_min = FFMIN(glyph->bbox.yMin, y_min);
1229  y_max = FFMAX(glyph->bbox.yMax, y_max);
1230  x_min = FFMIN(glyph->bbox.xMin, x_min);
1231  x_max = FFMAX(glyph->bbox.xMax, x_max);
1232  }
1233  s->max_glyph_h = y_max - y_min;
1234  s->max_glyph_w = x_max - x_min;
1235 
1236  /* compute and save position for each glyph */
1237  glyph = NULL;
1238  for (i = 0, p = text; *p; i++) {
1239  GET_UTF8(code, *p++, continue;);
1240 
1241  /* skip the \n in the sequence \r\n */
1242  if (prev_code == '\r' && code == '\n')
1243  continue;
1244 
1245  prev_code = code;
1246  if (is_newline(code)) {
1247 
1248  max_text_line_w = FFMAX(max_text_line_w, x);
1249  y += s->max_glyph_h;
1250  x = 0;
1251  continue;
1252  }
1253 
1254  /* get glyph */
1255  prev_glyph = glyph;
1256  dummy.code = code;
1257  glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1258 
1259  /* kerning */
1260  if (s->use_kerning && prev_glyph && glyph->code) {
1261  FT_Get_Kerning(s->face, prev_glyph->code, glyph->code,
1262  ft_kerning_default, &delta);
1263  x += delta.x >> 6;
1264  }
1265 
1266  /* save position */
1267  s->positions[i].x = x + glyph->bitmap_left;
1268  s->positions[i].y = y - glyph->bitmap_top + y_max;
1269  if (code == '\t') x = (x / s->tabsize + 1)*s->tabsize;
1270  else x += glyph->advance;
1271  }
1272 
1273  max_text_line_w = FFMAX(x, max_text_line_w);
1274 
1275  s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = max_text_line_w;
1276  s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = y + s->max_glyph_h;
1277 
1280  s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT ] = y_max;
1282 
1284 
1285  s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
1286  s->y = s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, &s->prng);
1287  s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
1288 
1289  update_alpha(s);
1290  update_color_with_alpha(s, &fontcolor , s->fontcolor );
1291  update_color_with_alpha(s, &shadowcolor, s->shadowcolor);
1292  update_color_with_alpha(s, &bordercolor, s->bordercolor);
1293  update_color_with_alpha(s, &boxcolor , s->boxcolor );
1294 
1295  box_w = FFMIN(width - 1 , max_text_line_w);
1296  box_h = FFMIN(height - 1, y + s->max_glyph_h);
1297 
1298  /* draw box */
1299  if (s->draw_box)
1300  ff_blend_rectangle(&s->dc, &boxcolor,
1301  frame->data, frame->linesize, width, height,
1302  s->x - s->boxborderw, s->y - s->boxborderw,
1303  box_w + s->boxborderw * 2, box_h + s->boxborderw * 2);
1304 
1305  if (s->shadowx || s->shadowy) {
1306  if ((ret = draw_glyphs(s, frame, width, height,
1307  &shadowcolor, s->shadowx, s->shadowy, 0)) < 0)
1308  return ret;
1309  }
1310 
1311  if (s->borderw) {
1312  if ((ret = draw_glyphs(s, frame, width, height,
1313  &bordercolor, 0, 0, s->borderw)) < 0)
1314  return ret;
1315  }
1316  if ((ret = draw_glyphs(s, frame, width, height,
1317  &fontcolor, 0, 0, 0)) < 0)
1318  return ret;
1319 
1320  return 0;
1321 }
1322 
1324 {
1325  AVFilterContext *ctx = inlink->dst;
1326  AVFilterLink *outlink = ctx->outputs[0];
1327  DrawTextContext *s = ctx->priv;
1328  int ret;
1329 
1330  if (s->reload) {
1331  if ((ret = load_textfile(ctx)) < 0) {
1332  av_frame_free(&frame);
1333  return ret;
1334  }
1335 #if CONFIG_LIBFRIBIDI
1336  if (s->text_shaping)
1337  if ((ret = shape_text(ctx)) < 0) {
1338  av_frame_free(&frame);
1339  return ret;
1340  }
1341 #endif
1342  }
1343 
1344  s->var_values[VAR_N] = inlink->frame_count+s->start_number;
1345  s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
1346  NAN : frame->pts * av_q2d(inlink->time_base);
1347 
1348  s->var_values[VAR_PICT_TYPE] = frame->pict_type;
1349  s->metadata = av_frame_get_metadata(frame);
1350 
1351  draw_text(ctx, frame, frame->width, frame->height);
1352 
1353  av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
1354  (int)s->var_values[VAR_N], s->var_values[VAR_T],
1355  (int)s->var_values[VAR_TEXT_W], (int)s->var_values[VAR_TEXT_H],
1356  s->x, s->y);
1357 
1358  return ff_filter_frame(outlink, frame);
1359 }
1360 
1362  {
1363  .name = "default",
1364  .type = AVMEDIA_TYPE_VIDEO,
1365  .filter_frame = filter_frame,
1366  .config_props = config_input,
1367  .needs_writable = 1,
1368  },
1369  { NULL }
1370 };
1371 
1373  {
1374  .name = "default",
1375  .type = AVMEDIA_TYPE_VIDEO,
1376  },
1377  { NULL }
1378 };
1379 
1381  .name = "drawtext",
1382  .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
1383  .priv_size = sizeof(DrawTextContext),
1384  .priv_class = &drawtext_class,
1385  .init = init,
1386  .uninit = uninit,
1388  .inputs = avfilter_vf_drawtext_inputs,
1389  .outputs = avfilter_vf_drawtext_outputs,
1392 };
Definition: lfg.h:25
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:535
#define NULL
Definition: coverity.c:32
static int func_frame_num(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:846
char * y_expr
expression for y position
Definition: vf_drawtext.c:178
const char * s
Definition: avisynth_c.h:631
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:361
int tc24hmax
1 if timecode is wrapped to 24 hours, 0 otherwise
Definition: vf_drawtext.c:189
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
uint8_t * fontcolor_expr
fontcolor expression to evaluate
Definition: vf_drawtext.c:147
AVOption.
Definition: opt.h:245
int x
x position to start drawing text
Definition: vf_drawtext.c:153
static double drand(void *opaque, double min, double max)
Definition: vf_drawtext.c:98
static int func_metadata(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:855
AVFormatContext * ctx
Definition: movenc-test.c:48
static const AVOption drawtext_options[]
Definition: vf_drawtext.c:201
const char * fmt
Definition: avisynth_c.h:632
unsigned int fontsize
font size to use
Definition: vf_drawtext.c:159
FFDrawColor boxcolor
background color
Definition: vf_drawtext.c:171
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
char * x_expr
expression for x position
Definition: vf_drawtext.c:177
Main libavfilter public API header.
uint8_t * fontfile
font to be used
Definition: vf_drawtext.c:144
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:559
#define FLAGS
Definition: vf_drawtext.c:199
int num
numerator
Definition: rational.h:44
static const struct drawtext_function functions[]
const char * b
Definition: vf_curves.c:109
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:1315
static int draw_text(AVFilterContext *ctx, AVFrame *frame, int width, int height)
Definition: vf_drawtext.c:1137
uint8_t * text
text to be drawn
Definition: vf_drawtext.c:145
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
Definition: tree.c:39
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:657
char * tc_opt_string
specified timecode option string
Definition: vf_drawtext.c:186
static int draw_glyphs(DrawTextContext *s, AVFrame *frame, int width, int height, FFDrawColor *color, int x, int y, int borderw)
Definition: vf_drawtext.c:1071
int(* func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int)
Definition: vf_drawtext.c:966
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
int boxborderw
box border width
Definition: vf_drawtext.c:162
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:122
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition: tree.c:34
expansion_mode
Definition: vf_drawtext.c:131
const char * name
Pad name.
Definition: internal.h:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:312
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:193
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1163
FT_Stroker stroker
freetype stroker handle
Definition: vf_drawtext.c:175
static int glyph_enu_free(void *opaque, void *elem)
Definition: vf_drawtext.c:694
uint8_t
#define av_cold
Definition: attributes.h:82
static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:785
float delta
AVOptions.
A tree container.
AVLFG prng
random
Definition: vf_drawtext.c:185
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
FT_Face face
freetype font face handle
Definition: vf_drawtext.c:174
static int glyph_cmp(const void *key, const void *b)
Definition: vf_drawtext.c:291
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:262
static av_cold int init(AVFilterContext *ctx)
Definition: vf_drawtext.c:594
Definition: eval.c:148
int start_number
starting frame number for n/frame_num var
Definition: vf_drawtext.c:191
static AVFrame * frame
static int load_font_file(AVFilterContext *ctx, const char *path, int index)
Definition: vf_drawtext.c:368
Misc file utilities.
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
static const AVFilterPad avfilter_vf_drawtext_inputs[]
Definition: vf_drawtext.c:1361
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
uint32_t tag
Definition: movenc.c:1348
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:91
const char * name
Definition: vf_drawtext.c:963
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_drawtext.c:704
ptrdiff_t size
Definition: opengl_enc.c:101
FT_Vector * positions
positions for each element in the text
Definition: vf_drawtext.c:150
AVExpr * x_pexpr
Definition: vf_drawtext.c:179
#define av_log(a,...)
void av_tree_destroy(AVTreeNode *t)
Definition: tree.c:146
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:349
A filter pad used for either input or output.
Definition: internal.h:53
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:722
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:99
double var_values[VAR_VARS_NB]
Definition: vf_drawtext.c:181
int width
width and height of the video frame
Definition: frame.h:230
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:129
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
AVBPrint expanded_text
used to contain the expanded text
Definition: vf_drawtext.c:146
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition: file.c:49
#define AV_BPRINT_SIZE_UNLIMITED
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
AVExpr * y_pexpr
parsed expressions for x and y
Definition: vf_drawtext.c:179
const char * err_msg
Definition: vf_drawtext.c:273
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
void * priv
private data for use by the filter
Definition: avfilter.h:319
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int y
y position to start drawing text
Definition: vf_drawtext.c:154
const char * arg
Definition: jacosubdec.c:66
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:207
static int expand_function(AVFilterContext *ctx, AVBPrint *bp, char **rtext)
Definition: vf_drawtext.c:1008
uint8_t vsub_max
Definition: drawutils.h:57
FFDrawColor fontcolor
foreground color
Definition: vf_drawtext.c:168
AVExpr * a_pexpr
Definition: vf_drawtext.c:183
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:80
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:149
static const char *const fun2_names[]
Definition: vf_drawtext.c:94
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
int reload
reload text file for each frame
Definition: vf_drawtext.c:190
AVBPrint expanded_fontcolor
used to contain the expanded fontcolor spec
Definition: vf_drawtext.c:148
var_name
Definition: aeval.c:46
FFDrawContext dc
Definition: vf_drawtext.c:167
static const struct ft_error ft_errors[]
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
#define FFMIN(a, b)
Definition: common.h:96
static struct tm * localtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:37
int64_t basetime
base pts time in the real world for display
Definition: vf_drawtext.c:180
int max_glyph_h
max glyph height
Definition: vf_drawtext.c:156
AVRational tc_rate
frame rate for timecode
Definition: vf_drawtext.c:187
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:882
double(* eval_func2)(void *, double a, double b)
Definition: vf_drawtext.c:103
#define FF_ARRAY_ELEMS(a)
AVTimecode tc
timecode context
Definition: vf_drawtext.c:188
FILE * out
Definition: movenc-test.c:54
static void update_color_with_alpha(DrawTextContext *s, FFDrawColor *color, const FFDrawColor incolor)
Definition: vf_drawtext.c:1115
void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h, const uint8_t *mask, int mask_linesize, int mask_w, int mask_h, int l2depth, unsigned endianness, int x0, int y0)
Blend an alpha mask with an uniform color.
Definition: drawutils.c:462
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
misc drawing utilities
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:317
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
Timecode helpers header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
static int load_font(AVFilterContext *ctx)
Definition: vf_drawtext.c:457
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Definition: vf_drawtext.c:767
timecode wraps after 24 hours
Definition: timecode.h:37
int borderw
border width
Definition: vf_drawtext.c:158
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
#define llrint(x)
Definition: libm.h:394
uint8_t hsub_max
Definition: drawutils.h:56
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
AVDictionary * av_frame_get_metadata(const AVFrame *frame)
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:141
int tabsize
tab size
Definition: vf_drawtext.c:164
int index
Definition: gxfenc.c:89
static int func_strftime(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:866
rational number numerator/denominator
Definition: rational.h:43
void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h, int x0, int y0, int w, int h)
Blend a rectangle with an uniform color.
Definition: drawutils.c:364
#define isnan(x)
Definition: libm.h:340
struct AVTreeNode * glyphs
rendered glyphs, stored using the UTF-32 char code
Definition: vf_drawtext.c:176
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
static int is_newline(uint32_t c)
Definition: vf_drawtext.c:501
const char * name
Filter name.
Definition: avfilter.h:145
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:165
static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
Definition: vf_drawtext.c:1048
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
int tag
opaque argument to func
Definition: vf_drawtext.c:965
short int draw_box
draw box around text - true or false
Definition: vf_drawtext.c:161
static int config_input(AVFilterLink *inlink)
Definition: vf_drawtext.c:727
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
AVDictionary * metadata
Definition: vf_drawtext.c:195
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
Append a formatted date and time to a print buffer.
Definition: bprint.c:176
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
Load timecode string in buf.
Definition: timecode.c:84
AVFILTER_DEFINE_CLASS(drawtext)
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
static void update_alpha(DrawTextContext *s)
Definition: vf_drawtext.c:1122
static const char *const var_names[]
Definition: vf_drawtext.c:73
int use_kerning
font kerning is used - true/false
Definition: vf_drawtext.c:163
common internal and external API header
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
static double c[64]
static int query_formats(AVFilterContext *ctx)
Definition: vf_drawtext.c:689
FT_Library library
freetype font library handle
Definition: vf_drawtext.c:173
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_drawtext.c:1323
static const AVFilterPad avfilter_vf_drawtext_outputs[]
Definition: vf_drawtext.c:1372
AVFilter ff_vf_drawtext
Definition: vf_drawtext.c:1380
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
char * value
Definition: dict.h:88
#define NAN
Definition: math.h:28
int len
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:712
#define FT_ERRMSG(e)
int reinit
tells if the filter is being reinited
Definition: vf_drawtext.c:140
FFDrawColor shadowcolor
shadow color
Definition: vf_drawtext.c:169
#define OFFSET(x)
Definition: vf_drawtext.c:198
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(const void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
Definition: tree.c:59
An instance of a filter.
Definition: avfilter.h:304
int max_glyph_w
max glyph width
Definition: vf_drawtext.c:155
static const eval_func2 fun2[]
Definition: vf_drawtext.c:105
int height
Definition: frame.h:230
static int func_pts(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:794
#define av_freep(p)
int fix_bounds
do we let it go out of frame bounds - t/f
Definition: vf_drawtext.c:165
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:109
#define av_malloc_array(a, b)
char * textfile
file with text to be drawn
Definition: vf_drawtext.c:152
internal API functions
static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
Load glyphs corresponding to the UTF-32 codepoint code.
Definition: vf_drawtext.c:301
static int load_textfile(AVFilterContext *ctx)
Definition: vf_drawtext.c:474
int dummy
Definition: motion-test.c:64
static int eval_function(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv)
Definition: vf_drawtext.c:981
static void drawtext(AVFrame *pic, int x, int y, const char *txt)
int ft_load_flags
flags used for loading fonts, see FT_LOAD_*
Definition: vf_drawtext.c:149
int exp_mode
expansion mode to use for the text
Definition: vf_drawtext.c:139
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
void av_tree_enumerate(AVTreeNode *t, void *opaque, int(*cmp)(void *opaque, void *elem), int(*enu)(void *opaque, void *elem))
Apply enu(opaque, &elem) to all the elements in the tree in a given range.
Definition: tree.c:155
float min
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
size_t nb_positions
number of elements of positions array
Definition: vf_drawtext.c:151
FFDrawColor bordercolor
border color
Definition: vf_drawtext.c:170
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
void * elem
Definition: tree.c:28
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
simple arithmetic expression evaluator
uint8_t rgba[4]
Definition: drawutils.h:61
const char * name
Definition: opengl_enc.c:103
static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:902
static int width
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140