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  char *fontsize_expr; ///< expression for fontsize
160  AVExpr *fontsize_pexpr; ///< parsed expressions for fontsize
161  unsigned int fontsize; ///< font size to use
162  unsigned int default_fontsize; ///< default font size to use
163 
164  int line_spacing; ///< lines spacing in pixels
165  short int draw_box; ///< draw box around text - true or false
166  int boxborderw; ///< box border width
167  int use_kerning; ///< font kerning is used - true/false
168  int tabsize; ///< tab size
169  int fix_bounds; ///< do we let it go out of frame bounds - t/f
170 
172  FFDrawColor fontcolor; ///< foreground color
173  FFDrawColor shadowcolor; ///< shadow color
174  FFDrawColor bordercolor; ///< border color
175  FFDrawColor boxcolor; ///< background color
176 
177  FT_Library library; ///< freetype font library handle
178  FT_Face face; ///< freetype font face handle
179  FT_Stroker stroker; ///< freetype stroker handle
180  struct AVTreeNode *glyphs; ///< rendered glyphs, stored using the UTF-32 char code
181  char *x_expr; ///< expression for x position
182  char *y_expr; ///< expression for y position
183  AVExpr *x_pexpr, *y_pexpr; ///< parsed expressions for x and y
184  int64_t basetime; ///< base pts time in the real world for display
186  char *a_expr;
188  int alpha;
189  AVLFG prng; ///< random
190  char *tc_opt_string; ///< specified timecode option string
191  AVRational tc_rate; ///< frame rate for timecode
192  AVTimecode tc; ///< timecode context
193  int tc24hmax; ///< 1 if timecode is wrapped to 24 hours, 0 otherwise
194  int reload; ///< reload text file for each frame
195  int start_number; ///< starting frame number for n/frame_num var
196 #if CONFIG_LIBFRIBIDI
197  int text_shaping; ///< 1 to shape the text before drawing it
198 #endif
201 
202 #define OFFSET(x) offsetof(DrawTextContext, x)
203 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
204 
205 static const AVOption drawtext_options[]= {
206  {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
207  {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
208  {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
209  {"fontcolor", "set foreground color", OFFSET(fontcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
210  {"fontcolor_expr", "set foreground color expression", OFFSET(fontcolor_expr), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS},
211  {"boxcolor", "set box color", OFFSET(boxcolor.rgba), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
212  {"bordercolor", "set border color", OFFSET(bordercolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
213  {"shadowcolor", "set shadow color", OFFSET(shadowcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
214  {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 , FLAGS},
215  {"boxborderw", "set box border width", OFFSET(boxborderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
216  {"line_spacing", "set line spacing in pixels", OFFSET(line_spacing), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX,FLAGS},
217  {"fontsize", "set font size", OFFSET(fontsize_expr), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX , FLAGS},
218  {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
219  {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
220  {"shadowx", "set shadow x offset", OFFSET(shadowx), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
221  {"shadowy", "set shadow y offset", OFFSET(shadowy), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
222  {"borderw", "set border width", OFFSET(borderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
223  {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX , FLAGS},
224  {"basetime", "set base time", OFFSET(basetime), AV_OPT_TYPE_INT64, {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX , FLAGS},
225 #if CONFIG_LIBFONTCONFIG
226  { "font", "Font name", OFFSET(font), AV_OPT_TYPE_STRING, { .str = "Sans" }, .flags = FLAGS },
227 #endif
228 
229  {"expansion", "set the expansion mode", OFFSET(exp_mode), AV_OPT_TYPE_INT, {.i64=EXP_NORMAL}, 0, 2, FLAGS, "expansion"},
230  {"none", "set no expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NONE}, 0, 0, FLAGS, "expansion"},
231  {"normal", "set normal expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NORMAL}, 0, 0, FLAGS, "expansion"},
232  {"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0, 0, FLAGS, "expansion"},
233 
234  {"timecode", "set initial timecode", OFFSET(tc_opt_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
235  {"tc24hmax", "set 24 hours max (timecode only)", OFFSET(tc24hmax), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
236  {"timecode_rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
237  {"r", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
238  {"rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
239  {"reload", "reload text file for each frame", OFFSET(reload), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
240  { "alpha", "apply alpha while rendering", OFFSET(a_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
241  {"fix_bounds", "check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
242  {"start_number", "start frame number for n/frame_num variable", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS},
243 
244 #if CONFIG_LIBFRIBIDI
245  {"text_shaping", "attempt to shape text before drawing", OFFSET(text_shaping), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
246 #endif
247 
248  /* FT_LOAD_* flags */
249  { "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" },
250  { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags = FLAGS, .unit = "ft_load_flags" },
251  { "no_scale", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags = FLAGS, .unit = "ft_load_flags" },
252  { "no_hinting", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags = FLAGS, .unit = "ft_load_flags" },
253  { "render", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags = FLAGS, .unit = "ft_load_flags" },
254  { "no_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
255  { "vertical_layout", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags = FLAGS, .unit = "ft_load_flags" },
256  { "force_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
257  { "crop_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
258  { "pedantic", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags = FLAGS, .unit = "ft_load_flags" },
259  { "ignore_global_advance_width", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags = FLAGS, .unit = "ft_load_flags" },
260  { "no_recurse", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags = FLAGS, .unit = "ft_load_flags" },
261  { "ignore_transform", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags = FLAGS, .unit = "ft_load_flags" },
262  { "monochrome", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags = FLAGS, .unit = "ft_load_flags" },
263  { "linear_design", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags = FLAGS, .unit = "ft_load_flags" },
264  { "no_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
265  { NULL }
266 };
267 
269 
270 #undef __FTERRORS_H__
271 #define FT_ERROR_START_LIST {
272 #define FT_ERRORDEF(e, v, s) { (e), (s) },
273 #define FT_ERROR_END_LIST { 0, NULL } };
274 
275 static const struct ft_error {
276  int err;
277  const char *err_msg;
278 } ft_errors[] =
279 #include FT_ERRORS_H
280 
281 #define FT_ERRMSG(e) ft_errors[e].err_msg
282 
283 typedef struct Glyph {
284  FT_Glyph glyph;
285  FT_Glyph border_glyph;
286  uint32_t code;
287  unsigned int fontsize;
288  FT_Bitmap bitmap; ///< array holding bitmaps of font
289  FT_Bitmap border_bitmap; ///< array holding bitmaps of font border
290  FT_BBox bbox;
291  int advance;
292  int bitmap_left;
293  int bitmap_top;
294 } Glyph;
295 
296 static int glyph_cmp(const void *key, const void *b)
297 {
298  const Glyph *a = key, *bb = b;
299  int64_t diff = (int64_t)a->code - (int64_t)bb->code;
300 
301  if (diff != 0)
302  return diff > 0 ? 1 : -1;
303  else
304  return FFDIFFSIGN((int64_t)a->fontsize, (int64_t)bb->fontsize);
305 }
306 
307 /**
308  * Load glyphs corresponding to the UTF-32 codepoint code.
309  */
310 static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
311 {
312  DrawTextContext *s = ctx->priv;
313  FT_BitmapGlyph bitmapglyph;
314  Glyph *glyph;
315  struct AVTreeNode *node = NULL;
316  int ret;
317 
318  /* load glyph into s->face->glyph */
319  if (FT_Load_Char(s->face, code, s->ft_load_flags))
320  return AVERROR(EINVAL);
321 
322  glyph = av_mallocz(sizeof(*glyph));
323  if (!glyph) {
324  ret = AVERROR(ENOMEM);
325  goto error;
326  }
327  glyph->code = code;
328  glyph->fontsize = s->fontsize;
329 
330  if (FT_Get_Glyph(s->face->glyph, &glyph->glyph)) {
331  ret = AVERROR(EINVAL);
332  goto error;
333  }
334  if (s->borderw) {
335  glyph->border_glyph = glyph->glyph;
336  if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0) ||
337  FT_Glyph_To_Bitmap(&glyph->border_glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
338  ret = AVERROR_EXTERNAL;
339  goto error;
340  }
341  bitmapglyph = (FT_BitmapGlyph) glyph->border_glyph;
342  glyph->border_bitmap = bitmapglyph->bitmap;
343  }
344  if (FT_Glyph_To_Bitmap(&glyph->glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
345  ret = AVERROR_EXTERNAL;
346  goto error;
347  }
348  bitmapglyph = (FT_BitmapGlyph) glyph->glyph;
349 
350  glyph->bitmap = bitmapglyph->bitmap;
351  glyph->bitmap_left = bitmapglyph->left;
352  glyph->bitmap_top = bitmapglyph->top;
353  glyph->advance = s->face->glyph->advance.x >> 6;
354 
355  /* measure text height to calculate text_height (or the maximum text height) */
356  FT_Glyph_Get_CBox(glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
357 
358  /* cache the newly created glyph */
359  if (!(node = av_tree_node_alloc())) {
360  ret = AVERROR(ENOMEM);
361  goto error;
362  }
363  av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
364 
365  if (glyph_ptr)
366  *glyph_ptr = glyph;
367  return 0;
368 
369 error:
370  if (glyph)
371  av_freep(&glyph->glyph);
372 
373  av_freep(&glyph);
374  av_freep(&node);
375  return ret;
376 }
377 
378 static av_cold int set_fontsize(AVFilterContext *ctx, unsigned int fontsize)
379 {
380  int err;
381  DrawTextContext *s = ctx->priv;
382 
383  if ((err = FT_Set_Pixel_Sizes(s->face, 0, fontsize))) {
384  av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
385  fontsize, FT_ERRMSG(err));
386  return AVERROR(EINVAL);
387  }
388 
389  s->fontsize = fontsize;
390 
391  return 0;
392 }
393 
395 {
396  DrawTextContext *s = ctx->priv;
397  int err;
398 
399  if (s->fontsize_pexpr)
400  return 0;
401 
402  if (s->fontsize_expr == NULL)
403  return AVERROR(EINVAL);
404 
406  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
407  return err;
408 
409  return 0;
410 }
411 
413 {
414  DrawTextContext *s = ctx->priv;
415  unsigned int fontsize = s->default_fontsize;
416  int err;
417  double size, roundedsize;
418 
419  // if no fontsize specified use the default
420  if (s->fontsize_expr != NULL) {
421  if ((err = parse_fontsize(ctx)) < 0)
422  return err;
423 
424  size = av_expr_eval(s->fontsize_pexpr, s->var_values, &s->prng);
425 
426  if (!isnan(size)) {
427  roundedsize = round(size);
428  // test for overflow before cast
429  if (!(roundedsize > INT_MIN && roundedsize < INT_MAX)) {
430  av_log(ctx, AV_LOG_ERROR, "fontsize overflow\n");
431  return AVERROR(EINVAL);
432  }
433 
434  fontsize = roundedsize;
435  }
436  }
437 
438  if (fontsize == 0)
439  fontsize = 1;
440 
441  // no change
442  if (fontsize == s->fontsize)
443  return 0;
444 
445  return set_fontsize(ctx, fontsize);
446 }
447 
448 static int load_font_file(AVFilterContext *ctx, const char *path, int index)
449 {
450  DrawTextContext *s = ctx->priv;
451  int err;
452 
453  err = FT_New_Face(s->library, path, index, &s->face);
454  if (err) {
455 #if !CONFIG_LIBFONTCONFIG
456  av_log(ctx, AV_LOG_ERROR, "Could not load font \"%s\": %s\n",
457  s->fontfile, FT_ERRMSG(err));
458 #endif
459  return AVERROR(EINVAL);
460  }
461  return 0;
462 }
463 
464 #if CONFIG_LIBFONTCONFIG
465 static int load_font_fontconfig(AVFilterContext *ctx)
466 {
467  DrawTextContext *s = ctx->priv;
468  FcConfig *fontconfig;
469  FcPattern *pat, *best;
470  FcResult result = FcResultMatch;
471  FcChar8 *filename;
472  int index;
473  double size;
474  int err = AVERROR(ENOENT);
475  int parse_err;
476 
477  fontconfig = FcInitLoadConfigAndFonts();
478  if (!fontconfig) {
479  av_log(ctx, AV_LOG_ERROR, "impossible to init fontconfig\n");
480  return AVERROR_UNKNOWN;
481  }
482  pat = FcNameParse(s->fontfile ? s->fontfile :
483  (uint8_t *)(intptr_t)"default");
484  if (!pat) {
485  av_log(ctx, AV_LOG_ERROR, "could not parse fontconfig pat");
486  return AVERROR(EINVAL);
487  }
488 
489  FcPatternAddString(pat, FC_FAMILY, s->font);
490 
491  parse_err = parse_fontsize(ctx);
492  if (!parse_err) {
493  double size = av_expr_eval(s->fontsize_pexpr, s->var_values, &s->prng);
494 
495  if (isnan(size)) {
496  av_log(ctx, AV_LOG_ERROR, "impossible to find font information");
497  return AVERROR(EINVAL);
498  }
499 
500  FcPatternAddDouble(pat, FC_SIZE, size);
501  }
502 
503  FcDefaultSubstitute(pat);
504 
505  if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
506  av_log(ctx, AV_LOG_ERROR, "could not substitue fontconfig options"); /* very unlikely */
507  FcPatternDestroy(pat);
508  return AVERROR(ENOMEM);
509  }
510 
511  best = FcFontMatch(fontconfig, pat, &result);
512  FcPatternDestroy(pat);
513 
514  if (!best || result != FcResultMatch) {
515  av_log(ctx, AV_LOG_ERROR,
516  "Cannot find a valid font for the family %s\n",
517  s->font);
518  goto fail;
519  }
520 
521  if (
522  FcPatternGetInteger(best, FC_INDEX, 0, &index ) != FcResultMatch ||
523  FcPatternGetDouble (best, FC_SIZE, 0, &size ) != FcResultMatch) {
524  av_log(ctx, AV_LOG_ERROR, "impossible to find font information");
525  return AVERROR(EINVAL);
526  }
527 
528  if (FcPatternGetString(best, FC_FILE, 0, &filename) != FcResultMatch) {
529  av_log(ctx, AV_LOG_ERROR, "No file path for %s\n",
530  s->font);
531  goto fail;
532  }
533 
534  av_log(ctx, AV_LOG_INFO, "Using \"%s\"\n", filename);
535  if (parse_err)
536  s->default_fontsize = size + 0.5;
537 
538  err = load_font_file(ctx, filename, index);
539  if (err)
540  return err;
541  FcConfigDestroy(fontconfig);
542 fail:
543  FcPatternDestroy(best);
544  return err;
545 }
546 #endif
547 
548 static int load_font(AVFilterContext *ctx)
549 {
550  DrawTextContext *s = ctx->priv;
551  int err;
552 
553  /* load the face, and set up the encoding, which is by default UTF-8 */
554  err = load_font_file(ctx, s->fontfile, 0);
555  if (!err)
556  return 0;
557 #if CONFIG_LIBFONTCONFIG
558  err = load_font_fontconfig(ctx);
559  if (!err)
560  return 0;
561 #endif
562  return err;
563 }
564 
566 {
567  DrawTextContext *s = ctx->priv;
568  int err;
569  uint8_t *textbuf;
570  uint8_t *tmp;
571  size_t textbuf_size;
572 
573  if ((err = av_file_map(s->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
574  av_log(ctx, AV_LOG_ERROR,
575  "The text file '%s' could not be read or is empty\n",
576  s->textfile);
577  return err;
578  }
579 
580  if (textbuf_size > SIZE_MAX - 1 || !(tmp = av_realloc(s->text, textbuf_size + 1))) {
581  av_file_unmap(textbuf, textbuf_size);
582  return AVERROR(ENOMEM);
583  }
584  s->text = tmp;
585  memcpy(s->text, textbuf, textbuf_size);
586  s->text[textbuf_size] = 0;
587  av_file_unmap(textbuf, textbuf_size);
588 
589  return 0;
590 }
591 
592 static inline int is_newline(uint32_t c)
593 {
594  return c == '\n' || c == '\r' || c == '\f' || c == '\v';
595 }
596 
597 #if CONFIG_LIBFRIBIDI
598 static int shape_text(AVFilterContext *ctx)
599 {
600  DrawTextContext *s = ctx->priv;
601  uint8_t *tmp;
602  int ret = AVERROR(ENOMEM);
603  static const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT |
604  FRIBIDI_FLAGS_ARABIC;
605  FriBidiChar *unicodestr = NULL;
606  FriBidiStrIndex len;
607  FriBidiParType direction = FRIBIDI_PAR_LTR;
608  FriBidiStrIndex line_start = 0;
609  FriBidiStrIndex line_end = 0;
610  FriBidiLevel *embedding_levels = NULL;
611  FriBidiArabicProp *ar_props = NULL;
612  FriBidiCharType *bidi_types = NULL;
613  FriBidiStrIndex i,j;
614 
615  len = strlen(s->text);
616  if (!(unicodestr = av_malloc_array(len, sizeof(*unicodestr)))) {
617  goto out;
618  }
619  len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8,
620  s->text, len, unicodestr);
621 
622  bidi_types = av_malloc_array(len, sizeof(*bidi_types));
623  if (!bidi_types) {
624  goto out;
625  }
626 
627  fribidi_get_bidi_types(unicodestr, len, bidi_types);
628 
629  embedding_levels = av_malloc_array(len, sizeof(*embedding_levels));
630  if (!embedding_levels) {
631  goto out;
632  }
633 
634  if (!fribidi_get_par_embedding_levels(bidi_types, len, &direction,
635  embedding_levels)) {
636  goto out;
637  }
638 
639  ar_props = av_malloc_array(len, sizeof(*ar_props));
640  if (!ar_props) {
641  goto out;
642  }
643 
644  fribidi_get_joining_types(unicodestr, len, ar_props);
645  fribidi_join_arabic(bidi_types, len, embedding_levels, ar_props);
646  fribidi_shape(flags, embedding_levels, len, ar_props, unicodestr);
647 
648  for (line_end = 0, line_start = 0; line_end < len; line_end++) {
649  if (is_newline(unicodestr[line_end]) || line_end == len - 1) {
650  if (!fribidi_reorder_line(flags, bidi_types,
651  line_end - line_start + 1, line_start,
652  direction, embedding_levels, unicodestr,
653  NULL)) {
654  goto out;
655  }
656  line_start = line_end + 1;
657  }
658  }
659 
660  /* Remove zero-width fill chars put in by libfribidi */
661  for (i = 0, j = 0; i < len; i++)
662  if (unicodestr[i] != FRIBIDI_CHAR_FILL)
663  unicodestr[j++] = unicodestr[i];
664  len = j;
665 
666  if (!(tmp = av_realloc(s->text, (len * 4 + 1) * sizeof(*s->text)))) {
667  /* Use len * 4, as a unicode character can be up to 4 bytes in UTF-8 */
668  goto out;
669  }
670 
671  s->text = tmp;
672  len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8,
673  unicodestr, len, s->text);
674  ret = 0;
675 
676 out:
677  av_free(unicodestr);
678  av_free(embedding_levels);
679  av_free(ar_props);
680  av_free(bidi_types);
681  return ret;
682 }
683 #endif
684 
685 static av_cold int init(AVFilterContext *ctx)
686 {
687  int err;
688  DrawTextContext *s = ctx->priv;
689  Glyph *glyph;
690 
692  s->fontsize_pexpr = NULL;
693 
694  s->fontsize = 0;
695  s->default_fontsize = 16;
696 
697  if (!s->fontfile && !CONFIG_LIBFONTCONFIG) {
698  av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
699  return AVERROR(EINVAL);
700  }
701 
702  if (s->textfile) {
703  if (s->text) {
704  av_log(ctx, AV_LOG_ERROR,
705  "Both text and text file provided. Please provide only one\n");
706  return AVERROR(EINVAL);
707  }
708  if ((err = load_textfile(ctx)) < 0)
709  return err;
710  }
711 
712  if (s->reload && !s->textfile)
713  av_log(ctx, AV_LOG_WARNING, "No file to reload\n");
714 
715  if (s->tc_opt_string) {
716  int ret = av_timecode_init_from_string(&s->tc, s->tc_rate,
717  s->tc_opt_string, ctx);
718  if (ret < 0)
719  return ret;
720  if (s->tc24hmax)
722  if (!s->text)
723  s->text = av_strdup("");
724  }
725 
726  if (!s->text) {
727  av_log(ctx, AV_LOG_ERROR,
728  "Either text, a valid file or a timecode must be provided\n");
729  return AVERROR(EINVAL);
730  }
731 
732 #if CONFIG_LIBFRIBIDI
733  if (s->text_shaping)
734  if ((err = shape_text(ctx)) < 0)
735  return err;
736 #endif
737 
738  if ((err = FT_Init_FreeType(&(s->library)))) {
739  av_log(ctx, AV_LOG_ERROR,
740  "Could not load FreeType: %s\n", FT_ERRMSG(err));
741  return AVERROR(EINVAL);
742  }
743 
744  if ((err = load_font(ctx)) < 0)
745  return err;
746 
747  if ((err = update_fontsize(ctx)) < 0)
748  return err;
749 
750  if (s->borderw) {
751  if (FT_Stroker_New(s->library, &s->stroker)) {
752  av_log(ctx, AV_LOG_ERROR, "Coult not init FT stroker\n");
753  return AVERROR_EXTERNAL;
754  }
755  FT_Stroker_Set(s->stroker, s->borderw << 6, FT_STROKER_LINECAP_ROUND,
756  FT_STROKER_LINEJOIN_ROUND, 0);
757  }
758 
759  s->use_kerning = FT_HAS_KERNING(s->face);
760 
761  /* load the fallback glyph with code 0 */
762  load_glyph(ctx, NULL, 0);
763 
764  /* set the tabsize in pixels */
765  if ((err = load_glyph(ctx, &glyph, ' ')) < 0) {
766  av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
767  return err;
768  }
769  s->tabsize *= glyph->advance;
770 
771  if (s->exp_mode == EXP_STRFTIME &&
772  (strchr(s->text, '%') || strchr(s->text, '\\')))
773  av_log(ctx, AV_LOG_WARNING, "expansion=strftime is deprecated.\n");
774 
777 
778  return 0;
779 }
780 
782 {
784 }
785 
786 static int glyph_enu_free(void *opaque, void *elem)
787 {
788  Glyph *glyph = elem;
789 
790  FT_Done_Glyph(glyph->glyph);
791  FT_Done_Glyph(glyph->border_glyph);
792  av_free(elem);
793  return 0;
794 }
795 
796 static av_cold void uninit(AVFilterContext *ctx)
797 {
798  DrawTextContext *s = ctx->priv;
799 
800  av_expr_free(s->x_pexpr);
801  av_expr_free(s->y_pexpr);
802  av_expr_free(s->a_pexpr);
804 
805  s->x_pexpr = s->y_pexpr = s->a_pexpr = s->fontsize_pexpr = NULL;
806 
807  av_freep(&s->positions);
808  s->nb_positions = 0;
809 
812  s->glyphs = NULL;
813 
814  FT_Done_Face(s->face);
815  FT_Stroker_Done(s->stroker);
816  FT_Done_FreeType(s->library);
817 
820 }
821 
822 static int config_input(AVFilterLink *inlink)
823 {
824  AVFilterContext *ctx = inlink->dst;
825  DrawTextContext *s = ctx->priv;
826  int ret;
827 
829  ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
832  ff_draw_color(&s->dc, &s->boxcolor, s->boxcolor.rgba);
833 
834  s->var_values[VAR_w] = s->var_values[VAR_W] = s->var_values[VAR_MAIN_W] = inlink->w;
835  s->var_values[VAR_h] = s->var_values[VAR_H] = s->var_values[VAR_MAIN_H] = inlink->h;
837  s->var_values[VAR_DAR] = (double)inlink->w / inlink->h * s->var_values[VAR_SAR];
838  s->var_values[VAR_HSUB] = 1 << s->dc.hsub_max;
839  s->var_values[VAR_VSUB] = 1 << s->dc.vsub_max;
840  s->var_values[VAR_X] = NAN;
841  s->var_values[VAR_Y] = NAN;
842  s->var_values[VAR_T] = NAN;
843 
845 
846  av_expr_free(s->x_pexpr);
847  av_expr_free(s->y_pexpr);
848  av_expr_free(s->a_pexpr);
849  s->x_pexpr = s->y_pexpr = s->a_pexpr = NULL;
850 
851  if ((ret = av_expr_parse(&s->x_pexpr, s->x_expr, var_names,
852  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
853  (ret = av_expr_parse(&s->y_pexpr, s->y_expr, var_names,
854  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
855  (ret = av_expr_parse(&s->a_pexpr, s->a_expr, var_names,
856  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
857 
858  return AVERROR(EINVAL);
859 
860  return 0;
861 }
862 
863 static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
864 {
865  DrawTextContext *s = ctx->priv;
866 
867  if (!strcmp(cmd, "reinit")) {
868  int ret;
869  uninit(ctx);
870  s->reinit = 1;
871  if ((ret = av_set_options_string(ctx, arg, "=", ":")) < 0)
872  return ret;
873  if ((ret = init(ctx)) < 0)
874  return ret;
875  return config_input(ctx->inputs[0]);
876  }
877 
878  return AVERROR(ENOSYS);
879 }
880 
881 static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp,
882  char *fct, unsigned argc, char **argv, int tag)
883 {
884  DrawTextContext *s = ctx->priv;
885 
887  return 0;
888 }
889 
890 static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
891  char *fct, unsigned argc, char **argv, int tag)
892 {
893  DrawTextContext *s = ctx->priv;
894  const char *fmt;
895  double pts = s->var_values[VAR_T];
896  int ret;
897 
898  fmt = argc >= 1 ? argv[0] : "flt";
899  if (argc >= 2) {
900  int64_t delta;
901  if ((ret = av_parse_time(&delta, argv[1], 1)) < 0) {
902  av_log(ctx, AV_LOG_ERROR, "Invalid delta '%s'\n", argv[1]);
903  return ret;
904  }
905  pts += (double)delta / AV_TIME_BASE;
906  }
907  if (!strcmp(fmt, "flt")) {
908  av_bprintf(bp, "%.6f", pts);
909  } else if (!strcmp(fmt, "hms")) {
910  if (isnan(pts)) {
911  av_bprintf(bp, " ??:??:??.???");
912  } else {
913  int64_t ms = llrint(pts * 1000);
914  char sign = ' ';
915  if (ms < 0) {
916  sign = '-';
917  ms = -ms;
918  }
919  av_bprintf(bp, "%c%02d:%02d:%02d.%03d", sign,
920  (int)(ms / (60 * 60 * 1000)),
921  (int)(ms / (60 * 1000)) % 60,
922  (int)(ms / 1000) % 60,
923  (int)(ms % 1000));
924  }
925  } else if (!strcmp(fmt, "localtime") ||
926  !strcmp(fmt, "gmtime")) {
927  struct tm tm;
928  time_t ms = (time_t)pts;
929  const char *timefmt = argc >= 3 ? argv[2] : "%Y-%m-%d %H:%M:%S";
930  if (!strcmp(fmt, "localtime"))
931  localtime_r(&ms, &tm);
932  else
933  gmtime_r(&ms, &tm);
934  av_bprint_strftime(bp, timefmt, &tm);
935  } else {
936  av_log(ctx, AV_LOG_ERROR, "Invalid format '%s'\n", fmt);
937  return AVERROR(EINVAL);
938  }
939  return 0;
940 }
941 
942 static int func_frame_num(AVFilterContext *ctx, AVBPrint *bp,
943  char *fct, unsigned argc, char **argv, int tag)
944 {
945  DrawTextContext *s = ctx->priv;
946 
947  av_bprintf(bp, "%d", (int)s->var_values[VAR_N]);
948  return 0;
949 }
950 
951 static int func_metadata(AVFilterContext *ctx, AVBPrint *bp,
952  char *fct, unsigned argc, char **argv, int tag)
953 {
954  DrawTextContext *s = ctx->priv;
955  AVDictionaryEntry *e = av_dict_get(s->metadata, argv[0], NULL, 0);
956 
957  if (e && e->value)
958  av_bprintf(bp, "%s", e->value);
959  else if (argc >= 2)
960  av_bprintf(bp, "%s", argv[1]);
961  return 0;
962 }
963 
964 static int func_strftime(AVFilterContext *ctx, AVBPrint *bp,
965  char *fct, unsigned argc, char **argv, int tag)
966 {
967  const char *fmt = argc ? argv[0] : "%Y-%m-%d %H:%M:%S";
968  time_t now;
969  struct tm tm;
970 
971  time(&now);
972  if (tag == 'L')
973  localtime_r(&now, &tm);
974  else
975  tm = *gmtime_r(&now, &tm);
976  av_bprint_strftime(bp, fmt, &tm);
977  return 0;
978 }
979 
980 static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp,
981  char *fct, unsigned argc, char **argv, int tag)
982 {
983  DrawTextContext *s = ctx->priv;
984  double res;
985  int ret;
986 
987  ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
989  &s->prng, 0, ctx);
990  if (ret < 0)
991  av_log(ctx, AV_LOG_ERROR,
992  "Expression '%s' for the expr text expansion function is not valid\n",
993  argv[0]);
994  else
995  av_bprintf(bp, "%f", res);
996 
997  return ret;
998 }
999 
1000 static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp,
1001  char *fct, unsigned argc, char **argv, int tag)
1002 {
1003  DrawTextContext *s = ctx->priv;
1004  double res;
1005  int intval;
1006  int ret;
1007  unsigned int positions = 0;
1008  char fmt_str[30] = "%";
1009 
1010  /*
1011  * argv[0] expression to be converted to `int`
1012  * argv[1] format: 'x', 'X', 'd' or 'u'
1013  * argv[2] positions printed (optional)
1014  */
1015 
1016  ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
1017  NULL, NULL, fun2_names, fun2,
1018  &s->prng, 0, ctx);
1019  if (ret < 0) {
1020  av_log(ctx, AV_LOG_ERROR,
1021  "Expression '%s' for the expr text expansion function is not valid\n",
1022  argv[0]);
1023  return ret;
1024  }
1025 
1026  if (!strchr("xXdu", argv[1][0])) {
1027  av_log(ctx, AV_LOG_ERROR, "Invalid format '%c' specified,"
1028  " allowed values: 'x', 'X', 'd', 'u'\n", argv[1][0]);
1029  return AVERROR(EINVAL);
1030  }
1031 
1032  if (argc == 3) {
1033  ret = sscanf(argv[2], "%u", &positions);
1034  if (ret != 1) {
1035  av_log(ctx, AV_LOG_ERROR, "expr_int_format(): Invalid number of positions"
1036  " to print: '%s'\n", argv[2]);
1037  return AVERROR(EINVAL);
1038  }
1039  }
1040 
1041  feclearexcept(FE_ALL_EXCEPT);
1042  intval = res;
1043  if ((ret = fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW))) {
1044  av_log(ctx, AV_LOG_ERROR, "Conversion of floating-point result to int failed. Control register: 0x%08x. Conversion result: %d\n", ret, intval);
1045  return AVERROR(EINVAL);
1046  }
1047 
1048  if (argc == 3)
1049  av_strlcatf(fmt_str, sizeof(fmt_str), "0%u", positions);
1050  av_strlcatf(fmt_str, sizeof(fmt_str), "%c", argv[1][0]);
1051 
1052  av_log(ctx, AV_LOG_DEBUG, "Formatting value %f (expr '%s') with spec '%s'\n",
1053  res, argv[0], fmt_str);
1054 
1055  av_bprintf(bp, fmt_str, intval);
1056 
1057  return 0;
1058 }
1059 
1060 static const struct drawtext_function {
1061  const char *name;
1062  unsigned argc_min, argc_max;
1063  int tag; /**< opaque argument to func */
1064  int (*func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int);
1065 } functions[] = {
1066  { "expr", 1, 1, 0, func_eval_expr },
1067  { "e", 1, 1, 0, func_eval_expr },
1068  { "expr_int_format", 2, 3, 0, func_eval_expr_int_format },
1069  { "eif", 2, 3, 0, func_eval_expr_int_format },
1070  { "pict_type", 0, 0, 0, func_pict_type },
1071  { "pts", 0, 3, 0, func_pts },
1072  { "gmtime", 0, 1, 'G', func_strftime },
1073  { "localtime", 0, 1, 'L', func_strftime },
1074  { "frame_num", 0, 0, 0, func_frame_num },
1075  { "n", 0, 0, 0, func_frame_num },
1076  { "metadata", 1, 2, 0, func_metadata },
1077 };
1078 
1079 static int eval_function(AVFilterContext *ctx, AVBPrint *bp, char *fct,
1080  unsigned argc, char **argv)
1081 {
1082  unsigned i;
1083 
1084  for (i = 0; i < FF_ARRAY_ELEMS(functions); i++) {
1085  if (strcmp(fct, functions[i].name))
1086  continue;
1087  if (argc < functions[i].argc_min) {
1088  av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at least %d arguments\n",
1089  fct, functions[i].argc_min);
1090  return AVERROR(EINVAL);
1091  }
1092  if (argc > functions[i].argc_max) {
1093  av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at most %d arguments\n",
1094  fct, functions[i].argc_max);
1095  return AVERROR(EINVAL);
1096  }
1097  break;
1098  }
1099  if (i >= FF_ARRAY_ELEMS(functions)) {
1100  av_log(ctx, AV_LOG_ERROR, "%%{%s} is not known\n", fct);
1101  return AVERROR(EINVAL);
1102  }
1103  return functions[i].func(ctx, bp, fct, argc, argv, functions[i].tag);
1104 }
1105 
1106 static int expand_function(AVFilterContext *ctx, AVBPrint *bp, char **rtext)
1107 {
1108  const char *text = *rtext;
1109  char *argv[16] = { NULL };
1110  unsigned argc = 0, i;
1111  int ret;
1112 
1113  if (*text != '{') {
1114  av_log(ctx, AV_LOG_ERROR, "Stray %% near '%s'\n", text);
1115  return AVERROR(EINVAL);
1116  }
1117  text++;
1118  while (1) {
1119  if (!(argv[argc++] = av_get_token(&text, ":}"))) {
1120  ret = AVERROR(ENOMEM);
1121  goto end;
1122  }
1123  if (!*text) {
1124  av_log(ctx, AV_LOG_ERROR, "Unterminated %%{} near '%s'\n", *rtext);
1125  ret = AVERROR(EINVAL);
1126  goto end;
1127  }
1128  if (argc == FF_ARRAY_ELEMS(argv))
1129  av_freep(&argv[--argc]); /* error will be caught later */
1130  if (*text == '}')
1131  break;
1132  text++;
1133  }
1134 
1135  if ((ret = eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
1136  goto end;
1137  ret = 0;
1138  *rtext = (char *)text + 1;
1139 
1140 end:
1141  for (i = 0; i < argc; i++)
1142  av_freep(&argv[i]);
1143  return ret;
1144 }
1145 
1146 static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
1147 {
1148  int ret;
1149 
1150  av_bprint_clear(bp);
1151  while (*text) {
1152  if (*text == '\\' && text[1]) {
1153  av_bprint_chars(bp, text[1], 1);
1154  text += 2;
1155  } else if (*text == '%') {
1156  text++;
1157  if ((ret = expand_function(ctx, bp, &text)) < 0)
1158  return ret;
1159  } else {
1160  av_bprint_chars(bp, *text, 1);
1161  text++;
1162  }
1163  }
1164  if (!av_bprint_is_complete(bp))
1165  return AVERROR(ENOMEM);
1166  return 0;
1167 }
1168 
1170  int width, int height,
1171  FFDrawColor *color,
1172  int x, int y, int borderw)
1173 {
1174  char *text = s->expanded_text.str;
1175  uint32_t code = 0;
1176  int i, x1, y1;
1177  uint8_t *p;
1178  Glyph *glyph = NULL;
1179 
1180  for (i = 0, p = text; *p; i++) {
1181  FT_Bitmap bitmap;
1182  Glyph dummy = { 0 };
1183  GET_UTF8(code, *p++, continue;);
1184 
1185  /* skip new line chars, just go to new line */
1186  if (code == '\n' || code == '\r' || code == '\t')
1187  continue;
1188 
1189  dummy.code = code;
1190  dummy.fontsize = s->fontsize;
1191  glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1192 
1193  bitmap = borderw ? glyph->border_bitmap : glyph->bitmap;
1194 
1195  if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
1196  glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
1197  return AVERROR(EINVAL);
1198 
1199  x1 = s->positions[i].x+s->x+x - borderw;
1200  y1 = s->positions[i].y+s->y+y - borderw;
1201 
1202  ff_blend_mask(&s->dc, color,
1203  frame->data, frame->linesize, width, height,
1204  bitmap.buffer, bitmap.pitch,
1205  bitmap.width, bitmap.rows,
1206  bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
1207  0, x1, y1);
1208  }
1209 
1210  return 0;
1211 }
1212 
1213 
1215 {
1216  *color = incolor;
1217  color->rgba[3] = (color->rgba[3] * s->alpha) / 255;
1218  ff_draw_color(&s->dc, color, color->rgba);
1219 }
1220 
1222 {
1223  double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
1224 
1225  if (isnan(alpha))
1226  return;
1227 
1228  if (alpha >= 1.0)
1229  s->alpha = 255;
1230  else if (alpha <= 0)
1231  s->alpha = 0;
1232  else
1233  s->alpha = 256 * alpha;
1234 }
1235 
1237  int width, int height)
1238 {
1239  DrawTextContext *s = ctx->priv;
1240  AVFilterLink *inlink = ctx->inputs[0];
1241 
1242  uint32_t code = 0, prev_code = 0;
1243  int x = 0, y = 0, i = 0, ret;
1244  int max_text_line_w = 0, len;
1245  int box_w, box_h;
1246  char *text;
1247  uint8_t *p;
1248  int y_min = 32000, y_max = -32000;
1249  int x_min = 32000, x_max = -32000;
1250  FT_Vector delta;
1251  Glyph *glyph = NULL, *prev_glyph = NULL;
1252  Glyph dummy = { 0 };
1253 
1254  time_t now = time(0);
1255  struct tm ltime;
1256  AVBPrint *bp = &s->expanded_text;
1257 
1258  FFDrawColor fontcolor;
1259  FFDrawColor shadowcolor;
1260  FFDrawColor bordercolor;
1261  FFDrawColor boxcolor;
1262 
1263  av_bprint_clear(bp);
1264 
1265  if(s->basetime != AV_NOPTS_VALUE)
1266  now= frame->pts*av_q2d(ctx->inputs[0]->time_base) + s->basetime/1000000;
1267 
1268  switch (s->exp_mode) {
1269  case EXP_NONE:
1270  av_bprintf(bp, "%s", s->text);
1271  break;
1272  case EXP_NORMAL:
1273  if ((ret = expand_text(ctx, s->text, &s->expanded_text)) < 0)
1274  return ret;
1275  break;
1276  case EXP_STRFTIME:
1277  localtime_r(&now, &ltime);
1278  av_bprint_strftime(bp, s->text, &ltime);
1279  break;
1280  }
1281 
1282  if (s->tc_opt_string) {
1283  char tcbuf[AV_TIMECODE_STR_SIZE];
1284  av_timecode_make_string(&s->tc, tcbuf, inlink->frame_count_out);
1285  av_bprint_clear(bp);
1286  av_bprintf(bp, "%s%s", s->text, tcbuf);
1287  }
1288 
1289  if (!av_bprint_is_complete(bp))
1290  return AVERROR(ENOMEM);
1291  text = s->expanded_text.str;
1292  if ((len = s->expanded_text.len) > s->nb_positions) {
1293  if (!(s->positions =
1294  av_realloc(s->positions, len*sizeof(*s->positions))))
1295  return AVERROR(ENOMEM);
1296  s->nb_positions = len;
1297  }
1298 
1299  if (s->fontcolor_expr[0]) {
1300  /* If expression is set, evaluate and replace the static value */
1302  if ((ret = expand_text(ctx, s->fontcolor_expr, &s->expanded_fontcolor)) < 0)
1303  return ret;
1305  return AVERROR(ENOMEM);
1306  av_log(s, AV_LOG_DEBUG, "Evaluated fontcolor is '%s'\n", s->expanded_fontcolor.str);
1307  ret = av_parse_color(s->fontcolor.rgba, s->expanded_fontcolor.str, -1, s);
1308  if (ret)
1309  return ret;
1310  ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
1311  }
1312 
1313  x = 0;
1314  y = 0;
1315 
1316  if ((ret = update_fontsize(ctx)) < 0)
1317  return ret;
1318 
1319  /* load and cache glyphs */
1320  for (i = 0, p = text; *p; i++) {
1321  GET_UTF8(code, *p++, continue;);
1322 
1323  /* get glyph */
1324  dummy.code = code;
1325  dummy.fontsize = s->fontsize;
1326  glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1327  if (!glyph) {
1328  ret = load_glyph(ctx, &glyph, code);
1329  if (ret < 0)
1330  return ret;
1331  }
1332 
1333  y_min = FFMIN(glyph->bbox.yMin, y_min);
1334  y_max = FFMAX(glyph->bbox.yMax, y_max);
1335  x_min = FFMIN(glyph->bbox.xMin, x_min);
1336  x_max = FFMAX(glyph->bbox.xMax, x_max);
1337  }
1338  s->max_glyph_h = y_max - y_min;
1339  s->max_glyph_w = x_max - x_min;
1340 
1341  /* compute and save position for each glyph */
1342  glyph = NULL;
1343  for (i = 0, p = text; *p; i++) {
1344  GET_UTF8(code, *p++, continue;);
1345 
1346  /* skip the \n in the sequence \r\n */
1347  if (prev_code == '\r' && code == '\n')
1348  continue;
1349 
1350  prev_code = code;
1351  if (is_newline(code)) {
1352 
1353  max_text_line_w = FFMAX(max_text_line_w, x);
1354  y += s->max_glyph_h + s->line_spacing;
1355  x = 0;
1356  continue;
1357  }
1358 
1359  /* get glyph */
1360  prev_glyph = glyph;
1361  dummy.code = code;
1362  dummy.fontsize = s->fontsize;
1363  glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1364 
1365  /* kerning */
1366  if (s->use_kerning && prev_glyph && glyph->code) {
1367  FT_Get_Kerning(s->face, prev_glyph->code, glyph->code,
1368  ft_kerning_default, &delta);
1369  x += delta.x >> 6;
1370  }
1371 
1372  /* save position */
1373  s->positions[i].x = x + glyph->bitmap_left;
1374  s->positions[i].y = y - glyph->bitmap_top + y_max;
1375  if (code == '\t') x = (x / s->tabsize + 1)*s->tabsize;
1376  else x += glyph->advance;
1377  }
1378 
1379  max_text_line_w = FFMAX(x, max_text_line_w);
1380 
1381  s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = max_text_line_w;
1382  s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = y + s->max_glyph_h;
1383 
1386  s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT ] = y_max;
1388 
1390 
1391  s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
1392  s->y = s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, &s->prng);
1393  s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
1394 
1395  update_alpha(s);
1396  update_color_with_alpha(s, &fontcolor , s->fontcolor );
1397  update_color_with_alpha(s, &shadowcolor, s->shadowcolor);
1398  update_color_with_alpha(s, &bordercolor, s->bordercolor);
1399  update_color_with_alpha(s, &boxcolor , s->boxcolor );
1400 
1401  box_w = FFMIN(width - 1 , max_text_line_w);
1402  box_h = FFMIN(height - 1, y + s->max_glyph_h);
1403 
1404  /* draw box */
1405  if (s->draw_box)
1406  ff_blend_rectangle(&s->dc, &boxcolor,
1407  frame->data, frame->linesize, width, height,
1408  s->x - s->boxborderw, s->y - s->boxborderw,
1409  box_w + s->boxborderw * 2, box_h + s->boxborderw * 2);
1410 
1411  if (s->shadowx || s->shadowy) {
1412  if ((ret = draw_glyphs(s, frame, width, height,
1413  &shadowcolor, s->shadowx, s->shadowy, 0)) < 0)
1414  return ret;
1415  }
1416 
1417  if (s->borderw) {
1418  if ((ret = draw_glyphs(s, frame, width, height,
1419  &bordercolor, 0, 0, s->borderw)) < 0)
1420  return ret;
1421  }
1422  if ((ret = draw_glyphs(s, frame, width, height,
1423  &fontcolor, 0, 0, 0)) < 0)
1424  return ret;
1425 
1426  return 0;
1427 }
1428 
1430 {
1431  AVFilterContext *ctx = inlink->dst;
1432  AVFilterLink *outlink = ctx->outputs[0];
1433  DrawTextContext *s = ctx->priv;
1434  int ret;
1435 
1436  if (s->reload) {
1437  if ((ret = load_textfile(ctx)) < 0) {
1438  av_frame_free(&frame);
1439  return ret;
1440  }
1441 #if CONFIG_LIBFRIBIDI
1442  if (s->text_shaping)
1443  if ((ret = shape_text(ctx)) < 0) {
1444  av_frame_free(&frame);
1445  return ret;
1446  }
1447 #endif
1448  }
1449 
1450  s->var_values[VAR_N] = inlink->frame_count_out + s->start_number;
1451  s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
1452  NAN : frame->pts * av_q2d(inlink->time_base);
1453 
1454  s->var_values[VAR_PICT_TYPE] = frame->pict_type;
1455  s->metadata = frame->metadata;
1456 
1457  draw_text(ctx, frame, frame->width, frame->height);
1458 
1459  av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
1460  (int)s->var_values[VAR_N], s->var_values[VAR_T],
1461  (int)s->var_values[VAR_TEXT_W], (int)s->var_values[VAR_TEXT_H],
1462  s->x, s->y);
1463 
1464  return ff_filter_frame(outlink, frame);
1465 }
1466 
1468  {
1469  .name = "default",
1470  .type = AVMEDIA_TYPE_VIDEO,
1471  .filter_frame = filter_frame,
1472  .config_props = config_input,
1473  .needs_writable = 1,
1474  },
1475  { NULL }
1476 };
1477 
1479  {
1480  .name = "default",
1481  .type = AVMEDIA_TYPE_VIDEO,
1482  },
1483  { NULL }
1484 };
1485 
1487  .name = "drawtext",
1488  .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
1489  .priv_size = sizeof(DrawTextContext),
1490  .priv_class = &drawtext_class,
1491  .init = init,
1492  .uninit = uninit,
1494  .inputs = avfilter_vf_drawtext_inputs,
1495  .outputs = avfilter_vf_drawtext_outputs,
1498 };
Definition: lfg.h:27
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:725
#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:942
char * y_expr
expression for y position
Definition: vf_drawtext.c:182
const char * s
Definition: avisynth_c.h:768
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
static float alpha(float a)
int tc24hmax
1 if timecode is wrapped to 24 hours, 0 otherwise
Definition: vf_drawtext.c:193
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
uint8_t * fontcolor_expr
fontcolor expression to evaluate
Definition: vf_drawtext.c:147
AVOption.
Definition: opt.h:246
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
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:951
static const AVOption drawtext_options[]
Definition: vf_drawtext.c:205
const char * fmt
Definition: avisynth_c.h:769
unsigned int fontsize
font size to use
Definition: vf_drawtext.c:161
FFDrawColor boxcolor
background color
Definition: vf_drawtext.c:175
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static av_cold int parse_fontsize(AVFilterContext *ctx)
Definition: vf_drawtext.c:394
char * x_expr
expression for x position
Definition: vf_drawtext.c:181
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:587
#define FLAGS
Definition: vf_drawtext.c:203
int num
Numerator.
Definition: rational.h:59
static const struct drawtext_function functions[]
const char * b
Definition: vf_curves.c:113
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:1412
static int draw_text(AVFilterContext *ctx, AVFrame *frame, int width, int height)
Definition: vf_drawtext.c:1236
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
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
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:672
char * tc_opt_string
specified timecode option string
Definition: vf_drawtext.c:190
static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
static int draw_glyphs(DrawTextContext *s, AVFrame *frame, int width, int height, FFDrawColor *color, int x, int y, int borderw)
Definition: vf_drawtext.c:1169
int(* func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int)
Definition: vf_drawtext.c:1064
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:166
#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:125
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:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
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:194
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
FT_Stroker stroker
freetype stroker handle
Definition: vf_drawtext.c:179
static int glyph_enu_free(void *opaque, void *elem)
Definition: vf_drawtext.c:786
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:881
AVExpr * fontsize_pexpr
parsed expressions for fontsize
Definition: vf_drawtext.c:160
float delta
AVOptions.
A tree container.
AVLFG prng
random
Definition: vf_drawtext.c:189
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
FT_Face face
freetype font face handle
Definition: vf_drawtext.c:178
static int glyph_cmp(const void *key, const void *b)
Definition: vf_drawtext.c:296
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:294
static av_cold int init(AVFilterContext *ctx)
Definition: vf_drawtext.c:685
Definition: eval.c:150
int start_number
starting frame number for n/frame_num var
Definition: vf_drawtext.c:195
static AVFrame * frame
static int load_font_file(AVFilterContext *ctx, const char *path, int index)
Definition: vf_drawtext.c:448
Misc file utilities.
#define height
static const AVFilterPad avfilter_vf_drawtext_inputs[]
Definition: vf_drawtext.c:1467
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:40
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1409
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:1061
AVDictionary * metadata
metadata.
Definition: frame.h:488
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_drawtext.c:796
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:183
#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:354
A filter pad used for either input or output.
Definition: internal.h:54
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:737
double var_values[VAR_VARS_NB]
Definition: vf_drawtext.c:185
int width
Definition: frame.h:259
#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
static const uint16_t positions[][14][3]
#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:163
AVExpr * y_pexpr
parsed expressions for x and y
Definition: vf_drawtext.c:183
const char * err_msg
Definition: vf_drawtext.c:277
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
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:353
#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:226
static int expand_function(AVFilterContext *ctx, AVBPrint *bp, char **rtext)
Definition: vf_drawtext.c:1106
uint16_t width
Definition: gdv.c:47
uint8_t vsub_max
Definition: drawutils.h:57
static av_always_inline av_const double round(double x)
Definition: libm.h:444
FFDrawColor fontcolor
foreground color
Definition: vf_drawtext.c:172
AVExpr * a_pexpr
Definition: vf_drawtext.c:187
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:109
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:194
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:171
#define FFDIFFSIGN(x, y)
Comparator.
Definition: common.h:92
static const struct ft_error ft_errors[]
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
#define FF_DRAW_PROCESS_ALPHA
Process alpha pixel component.
Definition: drawutils.h:73
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#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:184
char * fontsize_expr
expression for fontsize
Definition: vf_drawtext.c:159
AVFormatContext * ctx
Definition: movenc.c:48
int max_glyph_h
max glyph height
Definition: vf_drawtext.c:156
AVRational tc_rate
frame rate for timecode
Definition: vf_drawtext.c:191
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
int dummy
Definition: motion.c:64
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:980
double(* eval_func2)(void *, double a, double b)
Definition: vf_drawtext.c:103
static void error(const char *err)
#define FF_ARRAY_ELEMS(a)
AVTimecode tc
timecode context
Definition: vf_drawtext.c:192
static void update_color_with_alpha(DrawTextContext *s, FFDrawColor *color, const FFDrawColor incolor)
Definition: vf_drawtext.c:1214
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:616
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:379
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
misc drawing utilities
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:327
Timecode helpers header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
static int load_font(AVFilterContext *ctx)
Definition: vf_drawtext.c:548
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Definition: vf_drawtext.c:863
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:47
#define llrint(x)
Definition: libm.h:394
uint8_t hsub_max
Definition: drawutils.h:56
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int tabsize
tab size
Definition: vf_drawtext.c:168
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:964
Rational number (pair of numerator and denominator).
Definition: rational.h:58
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:439
#define isnan(x)
Definition: libm.h:340
struct AVTreeNode * glyphs
rendered glyphs, stored using the UTF-32 char code
Definition: vf_drawtext.c:180
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:592
const char * name
Filter name.
Definition: avfilter.h:148
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:178
static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
Definition: vf_drawtext.c:1146
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
int tag
opaque argument to func
Definition: vf_drawtext.c:1063
short int draw_box
draw box around text - true or false
Definition: vf_drawtext.c:165
static int config_input(AVFilterLink *inlink)
Definition: vf_drawtext.c:822
static int64_t pts
Global timestamp for the audio frames.
AVDictionary * metadata
Definition: vf_drawtext.c:199
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
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:1221
static const char *const var_names[]
Definition: vf_drawtext.c:73
int use_kerning
font kerning is used - true/false
Definition: vf_drawtext.c:167
int
common internal and external API header
static double c[64]
static int query_formats(AVFilterContext *ctx)
Definition: vf_drawtext.c:781
FT_Library library
freetype font library handle
Definition: vf_drawtext.c:177
#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:1429
static const AVFilterPad avfilter_vf_drawtext_outputs[]
Definition: vf_drawtext.c:1478
AVFilter ff_vf_drawtext
Definition: vf_drawtext.c:1486
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
char * value
Definition: dict.h:87
#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:727
#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:173
static av_cold int update_fontsize(AVFilterContext *ctx)
Definition: vf_drawtext.c:412
#define OFFSET(x)
Definition: vf_drawtext.c:202
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
int line_spacing
lines spacing in pixels
Definition: vf_drawtext.c:164
An instance of a filter.
Definition: avfilter.h:338
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:259
static int func_pts(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag)
Definition: vf_drawtext.c:890
FILE * out
Definition: movenc.c:54
unsigned int default_fontsize
default font size to use
Definition: vf_drawtext.c:162
#define av_freep(p)
int fix_bounds
do we let it go out of frame bounds - t/f
Definition: vf_drawtext.c:169
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
#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:310
static int load_textfile(AVFilterContext *ctx)
Definition: vf_drawtext.c:565
static int eval_function(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv)
Definition: vf_drawtext.c:1079
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:174
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static av_cold int set_fontsize(AVFilterContext *ctx, unsigned int fontsize)
Definition: vf_drawtext.c:378
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:62
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:1000
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
static uint8_t tmp[11]
Definition: aes_ctr.c:26