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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
29 #include <sys/time.h>
30 #include <time.h>
31 
32 #include "libavutil/colorspace.h"
33 #include "libavutil/common.h"
34 #include "libavutil/file.h"
35 #include "libavutil/eval.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/random_seed.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/tree.h"
42 #include "libavutil/lfg.h"
43 #include "avfilter.h"
44 #include "drawutils.h"
45 #include "formats.h"
46 #include "internal.h"
47 #include "video.h"
48 
49 #include <ft2build.h>
50 #include FT_FREETYPE_H
51 #include FT_GLYPH_H
52 
53 static const char *const var_names[] = {
54  "E",
55  "PHI",
56  "PI",
57  "main_w", "W",
58  "main_h", "H",
59  "text_w", "w",
60  "text_h", "h",
61  "x",
62  "y",
63  "n",
64  "t",
65  NULL
66 };
67 
68 static const char *const fun2_names[] = {
69  "rand"
70 };
71 
72 static double drand(void *opaque, double min, double max)
73 {
74  return min + (max-min) / UINT_MAX * av_lfg_get(opaque);
75 }
76 
77 typedef double (*eval_func2)(void *, double a, double b);
78 
79 static const eval_func2 fun2[] = {
80  drand,
81  NULL
82 };
83 
84 enum var_name {
97 };
98 
99 typedef struct {
100  const AVClass *class;
106  FT_Vector *positions;
107  size_t nb_positions;
108  char *textfile;
109  int x, y;
110  int w, h;
111  int shadowx, shadowy;
112  unsigned int fontsize;
116  uint8_t fontcolor[4];
117  uint8_t boxcolor[4];
118  uint8_t shadowcolor[4];
119  uint8_t fontcolor_rgba[4];
120  uint8_t boxcolor_rgba[4];
121  uint8_t shadowcolor_rgba[4];
122 
123  short int draw_box;
125  int tabsize;
127 
128  FT_Library library;
129  FT_Face face;
130  struct AVTreeNode *glyphs;
131  int hsub, vsub;
133  int pixel_step[4];
134  uint8_t rgba_map[4];
135  uint8_t *box_line[4];
136  char *x_expr, *y_expr;
137  AVExpr *x_pexpr, *y_pexpr;
138  double var_values[VAR_VARS_NB];
139  char *d_expr;
141  int draw;
144 
145 #define OFFSET(x) offsetof(DrawTextContext, x)
146 
147 static const AVOption drawtext_options[]= {
148 {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
149 {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
150 {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
151 {"fontcolor","set foreground color", OFFSET(fontcolor_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
152 {"boxcolor", "set box color", OFFSET(boxcolor_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
153 {"shadowcolor", "set shadow color", OFFSET(shadowcolor_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX },
154 {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
155 {"fontsize", "set font size", OFFSET(fontsize), AV_OPT_TYPE_INT, {.i64=16}, 1, 72 },
156 {"x", "set x", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX },
157 {"y", "set y", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX },
158 {"shadowx", "set x", OFFSET(shadowx), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX },
159 {"shadowy", "set y", OFFSET(shadowy), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX },
160 {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX },
161 {"draw", "if false do not draw", OFFSET(d_expr), AV_OPT_TYPE_STRING, {.str="1"}, CHAR_MIN, CHAR_MAX },
162 {"fix_bounds", "if true, check and fix text coords to avoid clipping",
163  OFFSET(fix_bounds), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
164 
165 /* FT_LOAD_* flags */
166 {"ft_load_flags", "set font loading flags for libfreetype", OFFSET(ft_load_flags), AV_OPT_TYPE_FLAGS, {.i64=FT_LOAD_DEFAULT|FT_LOAD_RENDER}, 0, INT_MAX, 0, "ft_load_flags" },
167 {"default", "set default", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_DEFAULT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
168 {"no_scale", "set no_scale", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_SCALE}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
169 {"no_hinting", "set no_hinting", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_HINTING}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
170 {"render", "set render", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_RENDER}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
171 {"no_bitmap", "set no_bitmap", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_BITMAP}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
172 {"vertical_layout", "set vertical_layout", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_VERTICAL_LAYOUT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
173 {"force_autohint", "set force_autohint", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_FORCE_AUTOHINT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
174 {"crop_bitmap", "set crop_bitmap", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_CROP_BITMAP}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
175 {"pedantic", "set pedantic", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_PEDANTIC}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
176 {"ignore_global_advance_width", "set ignore_global_advance_width", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
177 {"no_recurse", "set no_recurse", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_RECURSE}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
178 {"ignore_transform", "set ignore_transform", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_IGNORE_TRANSFORM}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
179 {"monochrome", "set monochrome", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_MONOCHROME}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
180 {"linear_design", "set linear_design", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_LINEAR_DESIGN}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
181 {"no_autohint", "set no_autohint", 0, AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_AUTOHINT}, INT_MIN, INT_MAX, 0, "ft_load_flags" },
182 {NULL},
183 };
184 
185 static const char *drawtext_get_name(void *ctx)
186 {
187  return "drawtext";
188 }
189 
190 static const AVClass drawtext_class = {
191  "DrawTextContext",
193  drawtext_options
194 };
195 
196 #undef __FTERRORS_H__
197 #define FT_ERROR_START_LIST {
198 #define FT_ERRORDEF(e, v, s) { (e), (s) },
199 #define FT_ERROR_END_LIST { 0, NULL } };
200 
201 struct ft_error
202 {
203  int err;
204  const char *err_msg;
205 } static ft_errors[] =
206 #include FT_ERRORS_H
207 
208 #define FT_ERRMSG(e) ft_errors[e].err_msg
209 
210 typedef struct {
211  FT_Glyph *glyph;
212  uint32_t code;
213  FT_Bitmap bitmap;
214  FT_BBox bbox;
215  int advance;
216  int bitmap_left;
217  int bitmap_top;
218 } Glyph;
219 
220 static int glyph_cmp(void *key, const void *b)
221 {
222  const Glyph *a = key, *bb = b;
223  int64_t diff = (int64_t)a->code - (int64_t)bb->code;
224  return diff > 0 ? 1 : diff < 0 ? -1 : 0;
225 }
226 
230 static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
231 {
232  DrawTextContext *dtext = ctx->priv;
233  Glyph *glyph;
234  struct AVTreeNode *node = NULL;
235  int ret;
236 
237  /* load glyph into dtext->face->glyph */
238  if (FT_Load_Char(dtext->face, code, dtext->ft_load_flags))
239  return AVERROR(EINVAL);
240 
241  /* save glyph */
242  if (!(glyph = av_mallocz(sizeof(*glyph))) ||
243  !(glyph->glyph = av_mallocz(sizeof(*glyph->glyph)))) {
244  ret = AVERROR(ENOMEM);
245  goto error;
246  }
247  glyph->code = code;
248 
249  if (FT_Get_Glyph(dtext->face->glyph, glyph->glyph)) {
250  ret = AVERROR(EINVAL);
251  goto error;
252  }
253 
254  glyph->bitmap = dtext->face->glyph->bitmap;
255  glyph->bitmap_left = dtext->face->glyph->bitmap_left;
256  glyph->bitmap_top = dtext->face->glyph->bitmap_top;
257  glyph->advance = dtext->face->glyph->advance.x >> 6;
258 
259  /* measure text height to calculate text_height (or the maximum text height) */
260  FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
261 
262  /* cache the newly created glyph */
263  if (!(node = av_tree_node_alloc())) {
264  ret = AVERROR(ENOMEM);
265  goto error;
266  }
267  av_tree_insert(&dtext->glyphs, glyph, glyph_cmp, &node);
268 
269  if (glyph_ptr)
270  *glyph_ptr = glyph;
271  return 0;
272 
273 error:
274  if (glyph)
275  av_freep(&glyph->glyph);
276  av_freep(&glyph);
277  av_freep(&node);
278  return ret;
279 }
280 
281 static av_cold int init(AVFilterContext *ctx, const char *args)
282 {
283  int err;
284  DrawTextContext *dtext = ctx->priv;
285  Glyph *glyph;
286 
287  dtext->class = &drawtext_class;
288  av_opt_set_defaults(dtext);
289  dtext->fontcolor_string = av_strdup("black");
290  dtext->boxcolor_string = av_strdup("white");
291  dtext->shadowcolor_string = av_strdup("black");
292 
293  if ((err = (av_set_options_string(dtext, args, "=", ":"))) < 0) {
294  av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
295  return err;
296  }
297 
298  if (!dtext->fontfile) {
299  av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
300  return AVERROR(EINVAL);
301  }
302 
303  if (dtext->textfile) {
304  uint8_t *textbuf;
305  size_t textbuf_size;
306 
307  if (dtext->text) {
308  av_log(ctx, AV_LOG_ERROR,
309  "Both text and text file provided. Please provide only one\n");
310  return AVERROR(EINVAL);
311  }
312  if ((err = av_file_map(dtext->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
313  av_log(ctx, AV_LOG_ERROR,
314  "The text file '%s' could not be read or is empty\n",
315  dtext->textfile);
316  return err;
317  }
318 
319  if (!(dtext->text = av_malloc(textbuf_size+1)))
320  return AVERROR(ENOMEM);
321  memcpy(dtext->text, textbuf, textbuf_size);
322  dtext->text[textbuf_size] = 0;
323  av_file_unmap(textbuf, textbuf_size);
324  }
325 
326  if (!dtext->text) {
327  av_log(ctx, AV_LOG_ERROR,
328  "Either text or a valid file must be provided\n");
329  return AVERROR(EINVAL);
330  }
331 
332  if ((err = av_parse_color(dtext->fontcolor_rgba, dtext->fontcolor_string, -1, ctx))) {
333  av_log(ctx, AV_LOG_ERROR,
334  "Invalid font color '%s'\n", dtext->fontcolor_string);
335  return err;
336  }
337 
338  if ((err = av_parse_color(dtext->boxcolor_rgba, dtext->boxcolor_string, -1, ctx))) {
339  av_log(ctx, AV_LOG_ERROR,
340  "Invalid box color '%s'\n", dtext->boxcolor_string);
341  return err;
342  }
343 
344  if ((err = av_parse_color(dtext->shadowcolor_rgba, dtext->shadowcolor_string, -1, ctx))) {
345  av_log(ctx, AV_LOG_ERROR,
346  "Invalid shadow color '%s'\n", dtext->shadowcolor_string);
347  return err;
348  }
349 
350  if ((err = FT_Init_FreeType(&(dtext->library)))) {
351  av_log(ctx, AV_LOG_ERROR,
352  "Could not load FreeType: %s\n", FT_ERRMSG(err));
353  return AVERROR(EINVAL);
354  }
355 
356  /* load the face, and set up the encoding, which is by default UTF-8 */
357  if ((err = FT_New_Face(dtext->library, dtext->fontfile, 0, &dtext->face))) {
358  av_log(ctx, AV_LOG_ERROR, "Could not load fontface from file '%s': %s\n",
359  dtext->fontfile, FT_ERRMSG(err));
360  return AVERROR(EINVAL);
361  }
362  if ((err = FT_Set_Pixel_Sizes(dtext->face, 0, dtext->fontsize))) {
363  av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
364  dtext->fontsize, FT_ERRMSG(err));
365  return AVERROR(EINVAL);
366  }
367 
368  dtext->use_kerning = FT_HAS_KERNING(dtext->face);
369 
370  /* load the fallback glyph with code 0 */
371  load_glyph(ctx, NULL, 0);
372 
373  /* set the tabsize in pixels */
374  if ((err = load_glyph(ctx, &glyph, ' ') < 0)) {
375  av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
376  return err;
377  }
378  dtext->tabsize *= glyph->advance;
379 
380 #if !HAVE_LOCALTIME_R
381  av_log(ctx, AV_LOG_WARNING, "strftime() expansion unavailable!\n");
382 #endif
383 
384  return 0;
385 }
386 
388 {
389  static const enum AVPixelFormat pix_fmts[] = {
397  };
398 
400  return 0;
401 }
402 
403 static int glyph_enu_free(void *opaque, void *elem)
404 {
405  av_free(elem);
406  return 0;
407 }
408 
409 static av_cold void uninit(AVFilterContext *ctx)
410 {
411  DrawTextContext *dtext = ctx->priv;
412  int i;
413 
414  av_freep(&dtext->fontfile);
415  av_freep(&dtext->text);
416  av_freep(&dtext->expanded_text);
417  av_freep(&dtext->fontcolor_string);
418  av_freep(&dtext->boxcolor_string);
419  av_freep(&dtext->positions);
420  av_freep(&dtext->shadowcolor_string);
422  av_tree_destroy(dtext->glyphs);
423  dtext->glyphs = 0;
424  FT_Done_Face(dtext->face);
425  FT_Done_FreeType(dtext->library);
426 
427  for (i = 0; i < 4; i++) {
428  av_freep(&dtext->box_line[i]);
429  dtext->pixel_step[i] = 0;
430  }
431 
432 }
433 
434 static inline int is_newline(uint32_t c)
435 {
436  return c == '\n' || c == '\r' || c == '\f' || c == '\v';
437 }
438 
440 {
441  DrawTextContext *dtext = ctx->priv;
442  uint32_t code = 0, prev_code = 0;
443  int x = 0, y = 0, i = 0, ret;
444  int text_height, baseline;
445  char *text = dtext->text;
446  uint8_t *p;
447  int str_w = 0, len;
448  int y_min = 32000, y_max = -32000;
449  FT_Vector delta;
450  Glyph *glyph = NULL, *prev_glyph = NULL;
451  Glyph dummy = { 0 };
452  int width = ctx->inputs[0]->w;
453  int height = ctx->inputs[0]->h;
454 
455 #if HAVE_LOCALTIME_R
456  time_t now = time(0);
457  struct tm ltime;
458  uint8_t *buf = dtext->expanded_text;
459  int buf_size = dtext->expanded_text_size;
460 
461  if (!buf)
462  buf_size = 2*strlen(dtext->text)+1;
463 
464  localtime_r(&now, &ltime);
465 
466  while ((buf = av_realloc(buf, buf_size))) {
467  *buf = 1;
468  if (strftime(buf, buf_size, dtext->text, &ltime) != 0 || *buf == 0)
469  break;
470  buf_size *= 2;
471  }
472 
473  if (!buf)
474  return AVERROR(ENOMEM);
475  text = dtext->expanded_text = buf;
476  dtext->expanded_text_size = buf_size;
477 #endif
478 
479  if ((len = strlen(text)) > dtext->nb_positions) {
480  FT_Vector *p = av_realloc(dtext->positions,
481  len * sizeof(*dtext->positions));
482  if (!p) {
483  av_freep(dtext->positions);
484  dtext->nb_positions = 0;
485  return AVERROR(ENOMEM);
486  } else {
487  dtext->positions = p;
488  dtext->nb_positions = len;
489  }
490  }
491 
492  /* load and cache glyphs */
493  for (i = 0, p = text; *p; i++) {
494  GET_UTF8(code, *p++, continue;);
495 
496  /* get glyph */
497  dummy.code = code;
498  glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
499  if (!glyph) {
500  ret = load_glyph(ctx, &glyph, code);
501  if (ret)
502  return ret;
503  }
504 
505  y_min = FFMIN(glyph->bbox.yMin, y_min);
506  y_max = FFMAX(glyph->bbox.yMax, y_max);
507  }
508  text_height = y_max - y_min;
509  baseline = y_max;
510 
511  /* compute and save position for each glyph */
512  glyph = NULL;
513  for (i = 0, p = text; *p; i++) {
514  GET_UTF8(code, *p++, continue;);
515 
516  /* skip the \n in the sequence \r\n */
517  if (prev_code == '\r' && code == '\n')
518  continue;
519 
520  prev_code = code;
521  if (is_newline(code)) {
522  str_w = FFMAX(str_w, x - dtext->x);
523  y += text_height;
524  x = 0;
525  continue;
526  }
527 
528  /* get glyph */
529  prev_glyph = glyph;
530  dummy.code = code;
531  glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
532 
533  /* kerning */
534  if (dtext->use_kerning && prev_glyph && glyph->code) {
535  FT_Get_Kerning(dtext->face, prev_glyph->code, glyph->code,
536  ft_kerning_default, &delta);
537  x += delta.x >> 6;
538  }
539 
540  if (x + glyph->bbox.xMax >= width) {
541  str_w = FFMAX(str_w, x);
542  y += text_height;
543  x = 0;
544  }
545 
546  /* save position */
547  dtext->positions[i].x = x + glyph->bitmap_left;
548  dtext->positions[i].y = y - glyph->bitmap_top + baseline;
549  if (code == '\t') x = (x / dtext->tabsize + 1)*dtext->tabsize;
550  else x += glyph->advance;
551  }
552 
553  str_w = FFMIN(width - 1, FFMAX(str_w, x));
554  y = FFMIN(y + text_height, height - 1);
555 
556  dtext->w = str_w;
557  dtext->var_values[VAR_TEXT_W] = dtext->var_values[VAR_TW] = dtext->w;
558  dtext->h = y;
559  dtext->var_values[VAR_TEXT_H] = dtext->var_values[VAR_TH] = dtext->h;
560 
561  return 0;
562 }
563 
564 
565 static int config_input(AVFilterLink *inlink)
566 {
567  AVFilterContext *ctx = inlink->dst;
568  DrawTextContext *dtext = ctx->priv;
569  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
570  int ret;
571 
572  dtext->hsub = pix_desc->log2_chroma_w;
573  dtext->vsub = pix_desc->log2_chroma_h;
574 
575  dtext->var_values[VAR_E ] = M_E;
576  dtext->var_values[VAR_PHI] = M_PHI;
577  dtext->var_values[VAR_PI ] = M_PI;
578 
579  dtext->var_values[VAR_MAIN_W] =
580  dtext->var_values[VAR_MW] = ctx->inputs[0]->w;
581  dtext->var_values[VAR_MAIN_H] =
582  dtext->var_values[VAR_MH] = ctx->inputs[0]->h;
583 
584  dtext->var_values[VAR_X] = 0;
585  dtext->var_values[VAR_Y] = 0;
586  dtext->var_values[VAR_N] = 0;
587  dtext->var_values[VAR_T] = NAN;
588 
589  av_lfg_init(&dtext->prng, av_get_random_seed());
590 
591  if ((ret = av_expr_parse(&dtext->x_pexpr, dtext->x_expr, var_names,
592  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
593  (ret = av_expr_parse(&dtext->y_pexpr, dtext->y_expr, var_names,
594  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
595  (ret = av_expr_parse(&dtext->d_pexpr, dtext->d_expr, var_names,
596  NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
597  return AVERROR(EINVAL);
598 
599  if ((ret =
601  inlink->w, dtext->boxcolor,
602  inlink->format, dtext->boxcolor_rgba,
603  &dtext->is_packed_rgb, dtext->rgba_map)) < 0)
604  return ret;
605 
606  if (!dtext->is_packed_rgb) {
607  uint8_t *rgba = dtext->fontcolor_rgba;
608  dtext->fontcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
609  dtext->fontcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
610  dtext->fontcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
611  dtext->fontcolor[3] = rgba[3];
612  rgba = dtext->shadowcolor_rgba;
613  dtext->shadowcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
614  dtext->shadowcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
615  dtext->shadowcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
616  dtext->shadowcolor[3] = rgba[3];
617  }
618 
619  dtext->draw = 1;
620 
621  return dtext_prepare_text(ctx);
622 }
623 
624 #define GET_BITMAP_VAL(r, c) \
625  bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
626  (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
627  bitmap->buffer[(r) * bitmap->pitch + (c)]
628 
629 #define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub) { \
630  luma_pos = ((x) ) + ((y) ) * picref->linesize[0]; \
631  alpha = yuva_color[3] * (val) * 129; \
632  picref->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * picref->data[0][luma_pos] ) >> 23; \
633  if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
634  chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[1]; \
635  chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[2]; \
636  picref->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * picref->data[1][chroma_pos1]) >> 23; \
637  picref->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * picref->data[2][chroma_pos2]) >> 23; \
638  }\
639 }
640 
641 static inline int draw_glyph_yuv(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x,
642  unsigned int y, unsigned int width, unsigned int height,
643  const uint8_t yuva_color[4], int hsub, int vsub)
644 {
645  int r, c, alpha;
646  unsigned int luma_pos, chroma_pos1, chroma_pos2;
647  uint8_t src_val;
648 
649  for (r = 0; r < bitmap->rows && r+y < height; r++) {
650  for (c = 0; c < bitmap->width && c+x < width; c++) {
651  /* get intensity value in the glyph bitmap (source) */
652  src_val = GET_BITMAP_VAL(r, c);
653  if (!src_val)
654  continue;
655 
656  SET_PIXEL_YUV(picref, yuva_color, src_val, c+x, y+r, hsub, vsub);
657  }
658  }
659 
660  return 0;
661 }
662 
663 #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
664  p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
665  alpha = rgba_color[3] * (val) * 129; \
666  *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
667  *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
668  *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
669 }
670 
671 static inline int draw_glyph_rgb(AVFilterBufferRef *picref, FT_Bitmap *bitmap,
672  unsigned int x, unsigned int y,
673  unsigned int width, unsigned int height, int pixel_step,
674  const uint8_t rgba_color[4], const uint8_t rgba_map[4])
675 {
676  int r, c, alpha;
677  uint8_t *p;
678  uint8_t src_val;
679 
680  for (r = 0; r < bitmap->rows && r+y < height; r++) {
681  for (c = 0; c < bitmap->width && c+x < width; c++) {
682  /* get intensity value in the glyph bitmap (source) */
683  src_val = GET_BITMAP_VAL(r, c);
684  if (!src_val)
685  continue;
686 
687  SET_PIXEL_RGB(picref, rgba_color, src_val, c+x, y+r, pixel_step,
688  rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
689  }
690  }
691 
692  return 0;
693 }
694 
695 static inline void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned int y,
696  unsigned int width, unsigned int height,
697  uint8_t *line[4], int pixel_step[4], uint8_t color[4],
698  int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
699 {
700  int i, j, alpha;
701 
702  if (color[3] != 0xFF) {
703  if (is_rgba_packed) {
704  uint8_t *p;
705  for (j = 0; j < height; j++)
706  for (i = 0; i < width; i++)
707  SET_PIXEL_RGB(picref, color, 255, i+x, y+j, pixel_step[0],
708  rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
709  } else {
710  unsigned int luma_pos, chroma_pos1, chroma_pos2;
711  for (j = 0; j < height; j++)
712  for (i = 0; i < width; i++)
713  SET_PIXEL_YUV(picref, color, 255, i+x, y+j, hsub, vsub);
714  }
715  } else {
716  ff_draw_rectangle(picref->data, picref->linesize,
717  line, pixel_step, hsub, vsub,
718  x, y, width, height);
719  }
720 }
721 
722 static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref,
723  int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
724 {
725  char *text = HAVE_LOCALTIME_R ? dtext->expanded_text : dtext->text;
726  uint32_t code = 0;
727  int i;
728  uint8_t *p;
729  Glyph *glyph = NULL;
730 
731  for (i = 0, p = text; *p; i++) {
732  Glyph dummy = { 0 };
733  GET_UTF8(code, *p++, continue;);
734 
735  /* skip new line chars, just go to new line */
736  if (code == '\n' || code == '\r' || code == '\t')
737  continue;
738 
739  dummy.code = code;
740  glyph = av_tree_find(dtext->glyphs, &dummy, (void *)glyph_cmp, NULL);
741 
742  if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
743  glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
744  return AVERROR(EINVAL);
745 
746  if (dtext->is_packed_rgb) {
747  draw_glyph_rgb(picref, &glyph->bitmap,
748  dtext->positions[i].x+x, dtext->positions[i].y+y, width, height,
749  dtext->pixel_step[0], rgbcolor, dtext->rgba_map);
750  } else {
751  draw_glyph_yuv(picref, &glyph->bitmap,
752  dtext->positions[i].x+x, dtext->positions[i].y+y, width, height,
753  yuvcolor, dtext->hsub, dtext->vsub);
754  }
755  }
756 
757  return 0;
758 }
759 
760 static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
761  int width, int height)
762 {
763  DrawTextContext *dtext = ctx->priv;
764  int ret;
765 
766  /* draw box */
767  if (dtext->draw_box)
768  drawbox(picref, dtext->x, dtext->y, dtext->w, dtext->h,
769  dtext->box_line, dtext->pixel_step, dtext->boxcolor,
770  dtext->hsub, dtext->vsub, dtext->is_packed_rgb,
771  dtext->rgba_map);
772 
773  if (dtext->shadowx || dtext->shadowy) {
774  if ((ret = draw_glyphs(dtext, picref, width, height,
775  dtext->shadowcolor_rgba,
776  dtext->shadowcolor,
777  dtext->x + dtext->shadowx,
778  dtext->y + dtext->shadowy)) < 0)
779  return ret;
780  }
781 
782  if ((ret = draw_glyphs(dtext, picref, width, height,
783  dtext->fontcolor_rgba,
784  dtext->fontcolor,
785  dtext->x,
786  dtext->y)) < 0)
787  return ret;
788 
789  return 0;
790 }
791 
792 static inline int normalize_double(int *n, double d)
793 {
794  int ret = 0;
795 
796  if (isnan(d)) {
797  ret = AVERROR(EINVAL);
798  } else if (d > INT_MAX || d < INT_MIN) {
799  *n = d > INT_MAX ? INT_MAX : INT_MIN;
800  ret = AVERROR(EINVAL);
801  } else
802  *n = round(d);
803 
804  return ret;
805 }
806 
807 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
808 {
809  AVFilterContext *ctx = inlink->dst;
810  DrawTextContext *dtext = ctx->priv;
811  int ret = 0;
812 
813  if ((ret = dtext_prepare_text(ctx)) < 0) {
814  av_log(ctx, AV_LOG_ERROR, "Can't draw text\n");
815  avfilter_unref_bufferp(&frame);
816  return ret;
817  }
818 
819  dtext->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
820  NAN : frame->pts * av_q2d(inlink->time_base);
821  dtext->var_values[VAR_X] =
822  av_expr_eval(dtext->x_pexpr, dtext->var_values, &dtext->prng);
823  dtext->var_values[VAR_Y] =
824  av_expr_eval(dtext->y_pexpr, dtext->var_values, &dtext->prng);
825  dtext->var_values[VAR_X] =
826  av_expr_eval(dtext->x_pexpr, dtext->var_values, &dtext->prng);
827 
828  dtext->draw = av_expr_eval(dtext->d_pexpr, dtext->var_values, &dtext->prng);
829 
830  normalize_double(&dtext->x, dtext->var_values[VAR_X]);
831  normalize_double(&dtext->y, dtext->var_values[VAR_Y]);
832 
833  if (dtext->fix_bounds) {
834  if (dtext->x < 0) dtext->x = 0;
835  if (dtext->y < 0) dtext->y = 0;
836  if ((unsigned)dtext->x + (unsigned)dtext->w > inlink->w)
837  dtext->x = inlink->w - dtext->w;
838  if ((unsigned)dtext->y + (unsigned)dtext->h > inlink->h)
839  dtext->y = inlink->h - dtext->h;
840  }
841 
842  dtext->x &= ~((1 << dtext->hsub) - 1);
843  dtext->y &= ~((1 << dtext->vsub) - 1);
844 
845  av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
846  (int)dtext->var_values[VAR_N], dtext->var_values[VAR_T],
847  dtext->x, dtext->y, dtext->x+dtext->w, dtext->y+dtext->h);
848 
849  if (dtext->draw)
850  draw_text(inlink->dst, frame, frame->video->w, frame->video->h);
851 
852  dtext->var_values[VAR_N] += 1.0;
853 
854  return ff_filter_frame(inlink->dst->outputs[0], frame);
855 }
856 
858  {
859  .name = "default",
860  .type = AVMEDIA_TYPE_VIDEO,
861  .get_video_buffer = ff_null_get_video_buffer,
862  .filter_frame = filter_frame,
863  .config_props = config_input,
864  .min_perms = AV_PERM_WRITE |
865  AV_PERM_READ,
866  .rej_perms = AV_PERM_PRESERVE
867  },
868  { NULL }
869 };
870 
872  {
873  .name = "default",
874  .type = AVMEDIA_TYPE_VIDEO,
875  },
876  { NULL }
877 };
878 
880  .name = "drawtext",
881  .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
882  .priv_size = sizeof(DrawTextContext),
883  .init = init,
884  .uninit = uninit,
886 
887  .inputs = avfilter_vf_drawtext_inputs,
888  .outputs = avfilter_vf_drawtext_outputs,
889 };
Definition: lfg.h:25
int draw
set to zero to prevent drawing
Definition: vf_drawtext.c:141
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
AVOption.
Definition: opt.h:233
static double drand(void *opaque, double min, double max)
Definition: vf_drawtext.c:72
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
static const AVOption drawtext_options[]
Definition: vf_drawtext.c:147
#define HAVE_LOCALTIME_R
Definition: config.h:185
unsigned int fontsize
font size to use
Definition: vf_drawtext.c:112
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
static const char * drawtext_get_name(void *ctx)
Definition: vf_drawtext.c:185
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
#define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub)
Definition: vf_drawtext.c:629
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
uint8_t * fontfile
font to be used
Definition: vf_drawtext.c:101
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
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:587
Various defines for YUV<->RGB conversion.
uint8_t * text
text to be drawn
Definition: vf_drawtext.c:102
uint8_t * box_line[4]
line used for filling the box background
Definition: vf_drawtext.c:135
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
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:489
static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, int width, int height)
Definition: vf_drawtext.c:760
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
uint8_t shadowcolor[4]
shadow color
Definition: vf_drawtext.c:118
uint8_t boxcolor_rgba[4]
background color in RGBA
Definition: vf_drawtext.c:120
const AVClass * class
Definition: vf_drawtext.c:100
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
uint8_t fontcolor_rgba[4]
foreground color in RGBA
Definition: vf_drawtext.c:119
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition: tree.c:35
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
AVFilter avfilter_vf_drawtext
Definition: vf_drawtext.c:879
static int glyph_enu_free(void *opaque, void *elem)
Definition: vf_drawtext.c:403
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(void *key, const void *b), void *next[2])
Definition: tree.c:40
uint8_t
float delta
AVOptions.
A tree container.
AVLFG prng
random
Definition: vf_drawtext.c:142
static av_always_inline av_const int isnan(float x)
Definition: libm.h:85
#define b
Definition: input.c:52
FT_Face face
freetype font face handle
Definition: vf_drawtext.c:129
#define NAN
Definition: math.h:7
Definition: eval.c:125
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
Misc file utilities.
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
static const AVFilterPad avfilter_vf_drawtext_inputs[]
Definition: vf_drawtext.c:857
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_drawtext.c:409
var_name
Definition: vf_boxblur.c:47
void 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:375
FT_Vector * positions
positions for each element in the text
Definition: vf_drawtext.c:106
AVExpr * x_pexpr
Definition: vf_drawtext.c:137
void av_tree_destroy(AVTreeNode *t)
Definition: tree.c:142
#define r
Definition: input.c:51
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:300
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
char * fontcolor_string
font color as string
Definition: vf_drawtext.c:113
A filter pad used for either input or output.
Definition: internal.h:33
uint8_t fontcolor[4]
foreground color
Definition: vf_drawtext.c:116
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
Definition: tree.c:59
double var_values[VAR_VARS_NB]
Definition: vf_drawtext.c:138
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:128
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
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:48
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
AVExpr * y_pexpr
parsed expressions for x and y
Definition: vf_drawtext.c:137
const char * err_msg
Definition: vf_drawtext.c:204
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
void * priv
private data for use by the filter
Definition: avfilter.h:439
int y
position to start drawing text
Definition: vf_drawtext.c:109
Definition: graph2dot.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
static av_always_inline av_const double round(double x)
Definition: libm.h:151
static int dtext_prepare_text(AVFilterContext *ctx)
Definition: vf_drawtext.c:439
#define GET_BITMAP_VAL(r, c)
Definition: vf_drawtext.c:624
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
static const char *const fun2_names[]
Definition: vf_drawtext.c:68
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
static int draw_glyph_rgb(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, int pixel_step, const uint8_t rgba_color[4], const uint8_t rgba_map[4])
Definition: vf_drawtext.c:671
#define M_E
Definition: ratecontrol.c:39
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
double(* eval_func2)(void *, double a, double b)
Definition: vf_drawtext.c:77
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
static int draw_glyph_yuv(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const uint8_t yuva_color[4], int hsub, int vsub)
Definition: vf_drawtext.c:641
char * boxcolor_string
box color as string
Definition: vf_drawtext.c:114
misc drawing utilities
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:166
size_t expanded_text_size
size in bytes of the expanded_text buffer
Definition: vf_drawtext.c:104
static void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned int y, unsigned int width, unsigned int height, uint8_t *line[4], int pixel_step[4], uint8_t color[4], int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
Definition: vf_drawtext.c:695
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
int pixel_step[4]
distance in bytes between the component of each pixel
Definition: vf_drawtext.c:133
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:371
int tabsize
tab size
Definition: vf_drawtext.c:125
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
struct AVTreeNode * glyphs
rendered glyphs, stored using the UTF-32 char code
Definition: vf_drawtext.c:130
#define M_PHI
Definition: mathematics.h:34
int h
dimension of the text block
Definition: vf_drawtext.c:110
static int is_newline(uint32_t c)
Definition: vf_drawtext.c:434
const char * name
filter name
Definition: avfilter.h:372
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
struct ft_error ft_errors[]
short int draw_box
draw box around text - true or false
Definition: vf_drawtext.c:123
static int config_input(AVFilterLink *inlink)
Definition: vf_drawtext.c:565
#define RGB_TO_U_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:103
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_drawtext.c:281
#define RGB_TO_V_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:107
int height
Definition: gxfenc.c:72
uint8_t shadowcolor_rgba[4]
shadow color in RGBA
Definition: vf_drawtext.c:121
uint8_t rgba_map[4]
map RGBA offsets to the positions in the packed RGBA format
Definition: vf_drawtext.c:134
static const AVClass drawtext_class
Definition: vf_drawtext.c:190
static const char *const var_names[]
Definition: vf_drawtext.c:53
int use_kerning
font kerning is used - true/false
Definition: vf_drawtext.c:124
int vsub
chroma subsampling values
Definition: vf_drawtext.c:131
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
char * shadowcolor_string
shadow color as string
Definition: vf_drawtext.c:115
common internal and external API header
static int glyph_cmp(void *key, const void *b)
Definition: vf_drawtext.c:220
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:116
static int query_formats(AVFilterContext *ctx)
Definition: vf_drawtext.c:387
FT_Library library
freetype font library handle
Definition: vf_drawtext.c:128
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
static const uint8_t color[]
Definition: log.c:52
static const AVFilterPad avfilter_vf_drawtext_outputs[]
Definition: vf_drawtext.c:871
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
#define RGB_TO_Y_CCIR(r, g, b)
Definition: colorspace.h:99
int len
uint8_t * expanded_text
used to contain the strftime()-expanded text
Definition: vf_drawtext.c:103
static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref, int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
Definition: vf_drawtext.c:722
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:539
#define FT_ERRMSG(e)
int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4], enum AVPixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba, uint8_t rgba_map_ptr[4])
Definition: drawutils.c:29
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
#define OFFSET(x)
Definition: vf_drawtext.c:145
uint8_t boxcolor[4]
background color
Definition: vf_drawtext.c:117
An instance of a filter.
Definition: avfilter.h:418
static const eval_func2 fun2[]
Definition: vf_drawtext.c:79
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
#define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off)
Definition: vf_drawtext.c:663
int fix_bounds
do we let it go out of frame bounds - t/f
Definition: vf_drawtext.c:126
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:85
char * textfile
file with text to be drawn
Definition: vf_drawtext.c:108
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:230
void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4], uint8_t *src[4], int pixelstep[4], int hsub, int vsub, int x, int y, int w, int h)
Definition: drawutils.c:82
int ft_load_flags
flags used for loading fonts, see FT_LOAD_*
Definition: vf_drawtext.c:105
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:151
float min
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72
size_t nb_positions
number of elements of positions array
Definition: vf_drawtext.c:107
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
static int normalize_double(int *n, double d)
Definition: vf_drawtext.c:792
void * elem
Definition: tree.c:27
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
Definition: vf_drawtext.c:807
simple arithmetic expression evaluator
AVExpr * d_pexpr
Definition: vf_drawtext.c:140