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