You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1118 lines
38KB

  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. * @file
  24. * drawtext filter, based on the original vhook/drawtext.c
  25. * filter by Gustavo Sverzut Barbieri
  26. */
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include "config.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/bprint.h"
  32. #include "libavutil/common.h"
  33. #include "libavutil/file.h"
  34. #include "libavutil/eval.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/random_seed.h"
  37. #include "libavutil/parseutils.h"
  38. #include "libavutil/timecode.h"
  39. #include "libavutil/tree.h"
  40. #include "libavutil/lfg.h"
  41. #include "avfilter.h"
  42. #include "drawutils.h"
  43. #include "formats.h"
  44. #include "internal.h"
  45. #include "video.h"
  46. #include <ft2build.h>
  47. #include FT_FREETYPE_H
  48. #include FT_GLYPH_H
  49. #include FT_STROKER_H
  50. #if CONFIG_FONTCONFIG
  51. #include <fontconfig/fontconfig.h>
  52. #endif
  53. static const char *const var_names[] = {
  54. "dar",
  55. "hsub", "vsub",
  56. "line_h", "lh", ///< line height, same as max_glyph_h
  57. "main_h", "h", "H", ///< height of the input video
  58. "main_w", "w", "W", ///< width of the input video
  59. "max_glyph_a", "ascent", ///< max glyph ascent
  60. "max_glyph_d", "descent", ///< min glyph descent
  61. "max_glyph_h", ///< max glyph height
  62. "max_glyph_w", ///< max glyph width
  63. "n", ///< number of frame
  64. "sar",
  65. "t", ///< timestamp expressed in seconds
  66. "text_h", "th", ///< height of the rendered text
  67. "text_w", "tw", ///< width of the rendered text
  68. "x",
  69. "y",
  70. "pict_type",
  71. NULL
  72. };
  73. static const char *const fun2_names[] = {
  74. "rand"
  75. };
  76. static double drand(void *opaque, double min, double max)
  77. {
  78. return min + (max-min) / UINT_MAX * av_lfg_get(opaque);
  79. }
  80. typedef double (*eval_func2)(void *, double a, double b);
  81. static const eval_func2 fun2[] = {
  82. drand,
  83. NULL
  84. };
  85. enum var_name {
  86. VAR_DAR,
  87. VAR_HSUB, VAR_VSUB,
  88. VAR_LINE_H, VAR_LH,
  89. VAR_MAIN_H, VAR_h, VAR_H,
  90. VAR_MAIN_W, VAR_w, VAR_W,
  91. VAR_MAX_GLYPH_A, VAR_ASCENT,
  92. VAR_MAX_GLYPH_D, VAR_DESCENT,
  93. VAR_MAX_GLYPH_H,
  94. VAR_MAX_GLYPH_W,
  95. VAR_N,
  96. VAR_SAR,
  97. VAR_T,
  98. VAR_TEXT_H, VAR_TH,
  99. VAR_TEXT_W, VAR_TW,
  100. VAR_X,
  101. VAR_Y,
  102. VAR_PICT_TYPE,
  103. VAR_VARS_NB
  104. };
  105. enum expansion_mode {
  106. EXP_NONE,
  107. EXP_NORMAL,
  108. EXP_STRFTIME,
  109. };
  110. typedef struct {
  111. const AVClass *class;
  112. enum expansion_mode exp_mode; ///< expansion mode to use for the text
  113. int reinit; ///< tells if the filter is being reinited
  114. uint8_t *fontfile; ///< font to be used
  115. uint8_t *text; ///< text to be drawn
  116. AVBPrint expanded_text; ///< used to contain the expanded text
  117. int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_*
  118. FT_Vector *positions; ///< positions for each element in the text
  119. size_t nb_positions; ///< number of elements of positions array
  120. char *textfile; ///< file with text to be drawn
  121. int x; ///< x position to start drawing text
  122. int y; ///< y position to start drawing text
  123. int max_glyph_w; ///< max glyph width
  124. int max_glyph_h; ///< max glyph height
  125. int shadowx, shadowy;
  126. int borderw; ///< border width
  127. unsigned int fontsize; ///< font size to use
  128. short int draw_box; ///< draw box around text - true or false
  129. int use_kerning; ///< font kerning is used - true/false
  130. int tabsize; ///< tab size
  131. int fix_bounds; ///< do we let it go out of frame bounds - t/f
  132. FFDrawContext dc;
  133. FFDrawColor fontcolor; ///< foreground color
  134. FFDrawColor shadowcolor; ///< shadow color
  135. FFDrawColor bordercolor; ///< border color
  136. FFDrawColor boxcolor; ///< background color
  137. FT_Library library; ///< freetype font library handle
  138. FT_Face face; ///< freetype font face handle
  139. FT_Stroker stroker; ///< freetype stroker handle
  140. struct AVTreeNode *glyphs; ///< rendered glyphs, stored using the UTF-32 char code
  141. char *x_expr; ///< expression for x position
  142. char *y_expr; ///< expression for y position
  143. AVExpr *x_pexpr, *y_pexpr; ///< parsed expressions for x and y
  144. int64_t basetime; ///< base pts time in the real world for display
  145. double var_values[VAR_VARS_NB];
  146. #if FF_API_DRAWTEXT_OLD_TIMELINE
  147. char *draw_expr; ///< expression for draw
  148. AVExpr *draw_pexpr; ///< parsed expression for draw
  149. int draw; ///< set to zero to prevent drawing
  150. #endif
  151. AVLFG prng; ///< random
  152. char *tc_opt_string; ///< specified timecode option string
  153. AVRational tc_rate; ///< frame rate for timecode
  154. AVTimecode tc; ///< timecode context
  155. int tc24hmax; ///< 1 if timecode is wrapped to 24 hours, 0 otherwise
  156. int reload; ///< reload text file for each frame
  157. int start_number; ///< starting frame number for n/frame_num var
  158. AVDictionary *metadata;
  159. } DrawTextContext;
  160. #define OFFSET(x) offsetof(DrawTextContext, x)
  161. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  162. static const AVOption drawtext_options[]= {
  163. {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
  164. {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
  165. {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
  166. {"fontcolor", "set foreground color", OFFSET(fontcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
  167. {"boxcolor", "set box color", OFFSET(boxcolor.rgba), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
  168. {"bordercolor", "set border color", OFFSET(bordercolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
  169. {"shadowcolor", "set shadow color", OFFSET(shadowcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
  170. {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 , FLAGS},
  171. {"fontsize", "set font size", OFFSET(fontsize), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX , FLAGS},
  172. {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
  173. {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
  174. {"shadowx", "set x", OFFSET(shadowx), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
  175. {"shadowy", "set y", OFFSET(shadowy), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
  176. {"borderw", "set border width", OFFSET(borderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
  177. {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX , FLAGS},
  178. {"basetime", "set base time", OFFSET(basetime), AV_OPT_TYPE_INT64, {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX , FLAGS},
  179. #if FF_API_DRAWTEXT_OLD_TIMELINE
  180. {"draw", "if false do not draw (deprecated)", OFFSET(draw_expr), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
  181. #endif
  182. {"expansion", "set the expansion mode", OFFSET(exp_mode), AV_OPT_TYPE_INT, {.i64=EXP_NORMAL}, 0, 2, FLAGS, "expansion"},
  183. {"none", "set no expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NONE}, 0, 0, FLAGS, "expansion"},
  184. {"normal", "set normal expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NORMAL}, 0, 0, FLAGS, "expansion"},
  185. {"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0, 0, FLAGS, "expansion"},
  186. {"timecode", "set initial timecode", OFFSET(tc_opt_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
  187. {"tc24hmax", "set 24 hours max (timecode only)", OFFSET(tc24hmax), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  188. {"timecode_rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
  189. {"r", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
  190. {"rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
  191. {"reload", "reload text file for each frame", OFFSET(reload), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  192. {"fix_bounds", "if true, check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
  193. {"start_number", "start frame number for n/frame_num variable", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS},
  194. /* FT_LOAD_* flags */
  195. { "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" },
  196. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags = FLAGS, .unit = "ft_load_flags" },
  197. { "no_scale", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags = FLAGS, .unit = "ft_load_flags" },
  198. { "no_hinting", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags = FLAGS, .unit = "ft_load_flags" },
  199. { "render", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags = FLAGS, .unit = "ft_load_flags" },
  200. { "no_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
  201. { "vertical_layout", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags = FLAGS, .unit = "ft_load_flags" },
  202. { "force_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
  203. { "crop_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
  204. { "pedantic", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags = FLAGS, .unit = "ft_load_flags" },
  205. { "ignore_global_advance_width", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags = FLAGS, .unit = "ft_load_flags" },
  206. { "no_recurse", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags = FLAGS, .unit = "ft_load_flags" },
  207. { "ignore_transform", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags = FLAGS, .unit = "ft_load_flags" },
  208. { "monochrome", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags = FLAGS, .unit = "ft_load_flags" },
  209. { "linear_design", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags = FLAGS, .unit = "ft_load_flags" },
  210. { "no_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
  211. { NULL }
  212. };
  213. AVFILTER_DEFINE_CLASS(drawtext);
  214. #undef __FTERRORS_H__
  215. #define FT_ERROR_START_LIST {
  216. #define FT_ERRORDEF(e, v, s) { (e), (s) },
  217. #define FT_ERROR_END_LIST { 0, NULL } };
  218. struct ft_error
  219. {
  220. int err;
  221. const char *err_msg;
  222. } static ft_errors[] =
  223. #include FT_ERRORS_H
  224. #define FT_ERRMSG(e) ft_errors[e].err_msg
  225. typedef struct {
  226. FT_Glyph *glyph;
  227. uint32_t code;
  228. FT_Bitmap bitmap; ///< array holding bitmaps of font
  229. FT_Bitmap border_bitmap; ///< array holding bitmaps of font border
  230. FT_BBox bbox;
  231. int advance;
  232. int bitmap_left;
  233. int bitmap_top;
  234. } Glyph;
  235. static int glyph_cmp(void *key, const void *b)
  236. {
  237. const Glyph *a = key, *bb = b;
  238. int64_t diff = (int64_t)a->code - (int64_t)bb->code;
  239. return diff > 0 ? 1 : diff < 0 ? -1 : 0;
  240. }
  241. /**
  242. * Load glyphs corresponding to the UTF-32 codepoint code.
  243. */
  244. static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
  245. {
  246. DrawTextContext *s = ctx->priv;
  247. FT_BitmapGlyph bitmapglyph;
  248. Glyph *glyph;
  249. struct AVTreeNode *node = NULL;
  250. int ret;
  251. /* load glyph into s->face->glyph */
  252. if (FT_Load_Char(s->face, code, s->ft_load_flags))
  253. return AVERROR(EINVAL);
  254. /* save glyph */
  255. if (!(glyph = av_mallocz(sizeof(*glyph))) ||
  256. !(glyph->glyph = av_mallocz(sizeof(*glyph->glyph)))) {
  257. ret = AVERROR(ENOMEM);
  258. goto error;
  259. }
  260. glyph->code = code;
  261. if (FT_Get_Glyph(s->face->glyph, glyph->glyph)) {
  262. ret = AVERROR(EINVAL);
  263. goto error;
  264. }
  265. if (s->borderw) {
  266. FT_Glyph border_glyph = *glyph->glyph;
  267. if (FT_Glyph_StrokeBorder(&border_glyph, s->stroker, 0, 0) ||
  268. FT_Glyph_To_Bitmap(&border_glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
  269. ret = AVERROR_EXTERNAL;
  270. goto error;
  271. }
  272. bitmapglyph = (FT_BitmapGlyph) border_glyph;
  273. glyph->border_bitmap = bitmapglyph->bitmap;
  274. }
  275. if (FT_Glyph_To_Bitmap(glyph->glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
  276. ret = AVERROR_EXTERNAL;
  277. goto error;
  278. }
  279. bitmapglyph = (FT_BitmapGlyph) *glyph->glyph;
  280. glyph->bitmap = bitmapglyph->bitmap;
  281. glyph->bitmap_left = bitmapglyph->left;
  282. glyph->bitmap_top = bitmapglyph->top;
  283. glyph->advance = s->face->glyph->advance.x >> 6;
  284. /* measure text height to calculate text_height (or the maximum text height) */
  285. FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
  286. /* cache the newly created glyph */
  287. if (!(node = av_tree_node_alloc())) {
  288. ret = AVERROR(ENOMEM);
  289. goto error;
  290. }
  291. av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
  292. if (glyph_ptr)
  293. *glyph_ptr = glyph;
  294. return 0;
  295. error:
  296. if (glyph)
  297. av_freep(&glyph->glyph);
  298. av_freep(&glyph);
  299. av_freep(&node);
  300. return ret;
  301. }
  302. static int load_font_file(AVFilterContext *ctx, const char *path, int index,
  303. const char **error)
  304. {
  305. DrawTextContext *s = ctx->priv;
  306. int err;
  307. err = FT_New_Face(s->library, path, index, &s->face);
  308. if (err) {
  309. *error = FT_ERRMSG(err);
  310. return AVERROR(EINVAL);
  311. }
  312. return 0;
  313. }
  314. #if CONFIG_FONTCONFIG
  315. static int load_font_fontconfig(AVFilterContext *ctx, const char **error)
  316. {
  317. DrawTextContext *s = ctx->priv;
  318. FcConfig *fontconfig;
  319. FcPattern *pattern, *fpat;
  320. FcResult result = FcResultMatch;
  321. FcChar8 *filename;
  322. int err, index;
  323. double size;
  324. fontconfig = FcInitLoadConfigAndFonts();
  325. if (!fontconfig) {
  326. *error = "impossible to init fontconfig\n";
  327. return AVERROR(EINVAL);
  328. }
  329. pattern = FcNameParse(s->fontfile ? s->fontfile :
  330. (uint8_t *)(intptr_t)"default");
  331. if (!pattern) {
  332. *error = "could not parse fontconfig pattern";
  333. return AVERROR(EINVAL);
  334. }
  335. if (!FcConfigSubstitute(fontconfig, pattern, FcMatchPattern)) {
  336. *error = "could not substitue fontconfig options"; /* very unlikely */
  337. return AVERROR(EINVAL);
  338. }
  339. FcDefaultSubstitute(pattern);
  340. fpat = FcFontMatch(fontconfig, pattern, &result);
  341. if (!fpat || result != FcResultMatch) {
  342. *error = "impossible to find a matching font";
  343. return AVERROR(EINVAL);
  344. }
  345. if (FcPatternGetString (fpat, FC_FILE, 0, &filename) != FcResultMatch ||
  346. FcPatternGetInteger(fpat, FC_INDEX, 0, &index ) != FcResultMatch ||
  347. FcPatternGetDouble (fpat, FC_SIZE, 0, &size ) != FcResultMatch) {
  348. *error = "impossible to find font information";
  349. return AVERROR(EINVAL);
  350. }
  351. av_log(ctx, AV_LOG_INFO, "Using \"%s\"\n", filename);
  352. if (!s->fontsize)
  353. s->fontsize = size + 0.5;
  354. err = load_font_file(ctx, filename, index, error);
  355. if (err)
  356. return err;
  357. FcPatternDestroy(fpat);
  358. FcPatternDestroy(pattern);
  359. FcConfigDestroy(fontconfig);
  360. return 0;
  361. }
  362. #endif
  363. static int load_font(AVFilterContext *ctx)
  364. {
  365. DrawTextContext *s = ctx->priv;
  366. int err;
  367. const char *error = "unknown error\n";
  368. /* load the face, and set up the encoding, which is by default UTF-8 */
  369. err = load_font_file(ctx, s->fontfile, 0, &error);
  370. if (!err)
  371. return 0;
  372. #if CONFIG_FONTCONFIG
  373. err = load_font_fontconfig(ctx, &error);
  374. if (!err)
  375. return 0;
  376. #endif
  377. av_log(ctx, AV_LOG_ERROR, "Could not load font \"%s\": %s\n",
  378. s->fontfile, error);
  379. return err;
  380. }
  381. static int load_textfile(AVFilterContext *ctx)
  382. {
  383. DrawTextContext *s = ctx->priv;
  384. int err;
  385. uint8_t *textbuf;
  386. size_t textbuf_size;
  387. if ((err = av_file_map(s->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
  388. av_log(ctx, AV_LOG_ERROR,
  389. "The text file '%s' could not be read or is empty\n",
  390. s->textfile);
  391. return err;
  392. }
  393. if (!(s->text = av_realloc(s->text, textbuf_size + 1)))
  394. return AVERROR(ENOMEM);
  395. memcpy(s->text, textbuf, textbuf_size);
  396. s->text[textbuf_size] = 0;
  397. av_file_unmap(textbuf, textbuf_size);
  398. return 0;
  399. }
  400. static av_cold int init(AVFilterContext *ctx)
  401. {
  402. int err;
  403. DrawTextContext *s = ctx->priv;
  404. Glyph *glyph;
  405. #if FF_API_DRAWTEXT_OLD_TIMELINE
  406. if (s->draw_expr)
  407. av_log(ctx, AV_LOG_WARNING, "'draw' option is deprecated and will be removed soon, "
  408. "you are encouraged to use the generic timeline support through the 'enable' option\n");
  409. #endif
  410. if (!s->fontfile && !CONFIG_FONTCONFIG) {
  411. av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
  412. return AVERROR(EINVAL);
  413. }
  414. if (s->textfile) {
  415. if (s->text) {
  416. av_log(ctx, AV_LOG_ERROR,
  417. "Both text and text file provided. Please provide only one\n");
  418. return AVERROR(EINVAL);
  419. }
  420. if ((err = load_textfile(ctx)) < 0)
  421. return err;
  422. }
  423. if (s->reload && !s->textfile)
  424. av_log(ctx, AV_LOG_WARNING, "No file to reload\n");
  425. if (s->tc_opt_string) {
  426. int ret = av_timecode_init_from_string(&s->tc, s->tc_rate,
  427. s->tc_opt_string, ctx);
  428. if (ret < 0)
  429. return ret;
  430. if (s->tc24hmax)
  431. s->tc.flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  432. if (!s->text)
  433. s->text = av_strdup("");
  434. }
  435. if (!s->text) {
  436. av_log(ctx, AV_LOG_ERROR,
  437. "Either text, a valid file or a timecode must be provided\n");
  438. return AVERROR(EINVAL);
  439. }
  440. if ((err = FT_Init_FreeType(&(s->library)))) {
  441. av_log(ctx, AV_LOG_ERROR,
  442. "Could not load FreeType: %s\n", FT_ERRMSG(err));
  443. return AVERROR(EINVAL);
  444. }
  445. err = load_font(ctx);
  446. if (err)
  447. return err;
  448. if (!s->fontsize)
  449. s->fontsize = 16;
  450. if ((err = FT_Set_Pixel_Sizes(s->face, 0, s->fontsize))) {
  451. av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
  452. s->fontsize, FT_ERRMSG(err));
  453. return AVERROR(EINVAL);
  454. }
  455. if (s->borderw) {
  456. if (FT_Stroker_New(s->library, &s->stroker)) {
  457. av_log(ctx, AV_LOG_ERROR, "Coult not init FT stroker\n");
  458. return AVERROR_EXTERNAL;
  459. }
  460. FT_Stroker_Set(s->stroker, s->borderw << 6, FT_STROKER_LINECAP_ROUND,
  461. FT_STROKER_LINEJOIN_ROUND, 0);
  462. }
  463. s->use_kerning = FT_HAS_KERNING(s->face);
  464. /* load the fallback glyph with code 0 */
  465. load_glyph(ctx, NULL, 0);
  466. /* set the tabsize in pixels */
  467. if ((err = load_glyph(ctx, &glyph, ' ')) < 0) {
  468. av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
  469. return err;
  470. }
  471. s->tabsize *= glyph->advance;
  472. if (s->exp_mode == EXP_STRFTIME &&
  473. (strchr(s->text, '%') || strchr(s->text, '\\')))
  474. av_log(ctx, AV_LOG_WARNING, "expansion=strftime is deprecated.\n");
  475. av_bprint_init(&s->expanded_text, 0, AV_BPRINT_SIZE_UNLIMITED);
  476. return 0;
  477. }
  478. static int query_formats(AVFilterContext *ctx)
  479. {
  480. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  481. return 0;
  482. }
  483. static int glyph_enu_free(void *opaque, void *elem)
  484. {
  485. Glyph *glyph = elem;
  486. FT_Done_Glyph(*glyph->glyph);
  487. av_freep(&glyph->glyph);
  488. av_free(elem);
  489. return 0;
  490. }
  491. static av_cold void uninit(AVFilterContext *ctx)
  492. {
  493. DrawTextContext *s = ctx->priv;
  494. av_expr_free(s->x_pexpr);
  495. av_expr_free(s->y_pexpr);
  496. #if FF_API_DRAWTEXT_OLD_TIMELINE
  497. av_expr_free(s->draw_pexpr);
  498. s->x_pexpr = s->y_pexpr = s->draw_pexpr = NULL;
  499. #endif
  500. av_freep(&s->positions);
  501. s->nb_positions = 0;
  502. av_tree_enumerate(s->glyphs, NULL, NULL, glyph_enu_free);
  503. av_tree_destroy(s->glyphs);
  504. s->glyphs = NULL;
  505. FT_Done_Face(s->face);
  506. FT_Stroker_Done(s->stroker);
  507. FT_Done_FreeType(s->library);
  508. av_bprint_finalize(&s->expanded_text, NULL);
  509. }
  510. static inline int is_newline(uint32_t c)
  511. {
  512. return c == '\n' || c == '\r' || c == '\f' || c == '\v';
  513. }
  514. static int config_input(AVFilterLink *inlink)
  515. {
  516. AVFilterContext *ctx = inlink->dst;
  517. DrawTextContext *s = ctx->priv;
  518. int ret;
  519. ff_draw_init(&s->dc, inlink->format, 0);
  520. ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
  521. ff_draw_color(&s->dc, &s->shadowcolor, s->shadowcolor.rgba);
  522. ff_draw_color(&s->dc, &s->bordercolor, s->bordercolor.rgba);
  523. ff_draw_color(&s->dc, &s->boxcolor, s->boxcolor.rgba);
  524. s->var_values[VAR_w] = s->var_values[VAR_W] = s->var_values[VAR_MAIN_W] = inlink->w;
  525. s->var_values[VAR_h] = s->var_values[VAR_H] = s->var_values[VAR_MAIN_H] = inlink->h;
  526. s->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
  527. s->var_values[VAR_DAR] = (double)inlink->w / inlink->h * s->var_values[VAR_SAR];
  528. s->var_values[VAR_HSUB] = 1 << s->dc.hsub_max;
  529. s->var_values[VAR_VSUB] = 1 << s->dc.vsub_max;
  530. s->var_values[VAR_X] = NAN;
  531. s->var_values[VAR_Y] = NAN;
  532. s->var_values[VAR_T] = NAN;
  533. av_lfg_init(&s->prng, av_get_random_seed());
  534. av_expr_free(s->x_pexpr);
  535. av_expr_free(s->y_pexpr);
  536. #if FF_API_DRAWTEXT_OLD_TIMELINE
  537. av_expr_free(s->draw_pexpr);
  538. s->x_pexpr = s->y_pexpr = s->draw_pexpr = NULL;
  539. #else
  540. s->x_pexpr = s->y_pexpr = NULL;
  541. #endif
  542. if ((ret = av_expr_parse(&s->x_pexpr, s->x_expr, var_names,
  543. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
  544. (ret = av_expr_parse(&s->y_pexpr, s->y_expr, var_names,
  545. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
  546. return AVERROR(EINVAL);
  547. #if FF_API_DRAWTEXT_OLD_TIMELINE
  548. if (s->draw_expr &&
  549. (ret = av_expr_parse(&s->draw_pexpr, s->draw_expr, var_names,
  550. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
  551. return ret;
  552. #endif
  553. return 0;
  554. }
  555. static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
  556. {
  557. DrawTextContext *s = ctx->priv;
  558. if (!strcmp(cmd, "reinit")) {
  559. int ret;
  560. uninit(ctx);
  561. s->reinit = 1;
  562. if ((ret = init(ctx)) < 0)
  563. return ret;
  564. return config_input(ctx->inputs[0]);
  565. }
  566. return AVERROR(ENOSYS);
  567. }
  568. static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp,
  569. char *fct, unsigned argc, char **argv, int tag)
  570. {
  571. DrawTextContext *s = ctx->priv;
  572. av_bprintf(bp, "%c", av_get_picture_type_char(s->var_values[VAR_PICT_TYPE]));
  573. return 0;
  574. }
  575. static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
  576. char *fct, unsigned argc, char **argv, int tag)
  577. {
  578. DrawTextContext *s = ctx->priv;
  579. av_bprintf(bp, "%.6f", s->var_values[VAR_T]);
  580. return 0;
  581. }
  582. static int func_frame_num(AVFilterContext *ctx, AVBPrint *bp,
  583. char *fct, unsigned argc, char **argv, int tag)
  584. {
  585. DrawTextContext *s = ctx->priv;
  586. av_bprintf(bp, "%d", (int)s->var_values[VAR_N]);
  587. return 0;
  588. }
  589. static int func_metadata(AVFilterContext *ctx, AVBPrint *bp,
  590. char *fct, unsigned argc, char **argv, int tag)
  591. {
  592. DrawTextContext *s = ctx->priv;
  593. AVDictionaryEntry *e = av_dict_get(s->metadata, argv[0], NULL, 0);
  594. if (e && e->value)
  595. av_bprintf(bp, "%s", e->value);
  596. return 0;
  597. }
  598. #if !HAVE_LOCALTIME_R
  599. static void localtime_r(const time_t *t, struct tm *tm)
  600. {
  601. *tm = *localtime(t);
  602. }
  603. #endif
  604. static int func_strftime(AVFilterContext *ctx, AVBPrint *bp,
  605. char *fct, unsigned argc, char **argv, int tag)
  606. {
  607. const char *fmt = argc ? argv[0] : "%Y-%m-%d %H:%M:%S";
  608. time_t now;
  609. struct tm tm;
  610. time(&now);
  611. if (tag == 'L')
  612. localtime_r(&now, &tm);
  613. else
  614. tm = *gmtime(&now);
  615. av_bprint_strftime(bp, fmt, &tm);
  616. return 0;
  617. }
  618. static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp,
  619. char *fct, unsigned argc, char **argv, int tag)
  620. {
  621. DrawTextContext *s = ctx->priv;
  622. double res;
  623. int ret;
  624. ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
  625. NULL, NULL, fun2_names, fun2,
  626. &s->prng, 0, ctx);
  627. if (ret < 0)
  628. av_log(ctx, AV_LOG_ERROR,
  629. "Expression '%s' for the expr text expansion function is not valid\n",
  630. argv[0]);
  631. else
  632. av_bprintf(bp, "%f", res);
  633. return ret;
  634. }
  635. static const struct drawtext_function {
  636. const char *name;
  637. unsigned argc_min, argc_max;
  638. int tag; /**< opaque argument to func */
  639. int (*func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int);
  640. } functions[] = {
  641. { "expr", 1, 1, 0, func_eval_expr },
  642. { "e", 1, 1, 0, func_eval_expr },
  643. { "pict_type", 0, 0, 0, func_pict_type },
  644. { "pts", 0, 0, 0, func_pts },
  645. { "gmtime", 0, 1, 'G', func_strftime },
  646. { "localtime", 0, 1, 'L', func_strftime },
  647. { "frame_num", 0, 0, 0, func_frame_num },
  648. { "n", 0, 0, 0, func_frame_num },
  649. { "metadata", 1, 1, 0, func_metadata },
  650. };
  651. static int eval_function(AVFilterContext *ctx, AVBPrint *bp, char *fct,
  652. unsigned argc, char **argv)
  653. {
  654. unsigned i;
  655. for (i = 0; i < FF_ARRAY_ELEMS(functions); i++) {
  656. if (strcmp(fct, functions[i].name))
  657. continue;
  658. if (argc < functions[i].argc_min) {
  659. av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at least %d arguments\n",
  660. fct, functions[i].argc_min);
  661. return AVERROR(EINVAL);
  662. }
  663. if (argc > functions[i].argc_max) {
  664. av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at most %d arguments\n",
  665. fct, functions[i].argc_max);
  666. return AVERROR(EINVAL);
  667. }
  668. break;
  669. }
  670. if (i >= FF_ARRAY_ELEMS(functions)) {
  671. av_log(ctx, AV_LOG_ERROR, "%%{%s} is not known\n", fct);
  672. return AVERROR(EINVAL);
  673. }
  674. return functions[i].func(ctx, bp, fct, argc, argv, functions[i].tag);
  675. }
  676. static int expand_function(AVFilterContext *ctx, AVBPrint *bp, char **rtext)
  677. {
  678. const char *text = *rtext;
  679. char *argv[16] = { NULL };
  680. unsigned argc = 0, i;
  681. int ret;
  682. if (*text != '{') {
  683. av_log(ctx, AV_LOG_ERROR, "Stray %% near '%s'\n", text);
  684. return AVERROR(EINVAL);
  685. }
  686. text++;
  687. while (1) {
  688. if (!(argv[argc++] = av_get_token(&text, ":}"))) {
  689. ret = AVERROR(ENOMEM);
  690. goto end;
  691. }
  692. if (!*text) {
  693. av_log(ctx, AV_LOG_ERROR, "Unterminated %%{} near '%s'\n", *rtext);
  694. ret = AVERROR(EINVAL);
  695. goto end;
  696. }
  697. if (argc == FF_ARRAY_ELEMS(argv))
  698. av_freep(&argv[--argc]); /* error will be caught later */
  699. if (*text == '}')
  700. break;
  701. text++;
  702. }
  703. if ((ret = eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
  704. goto end;
  705. ret = 0;
  706. *rtext = (char *)text + 1;
  707. end:
  708. for (i = 0; i < argc; i++)
  709. av_freep(&argv[i]);
  710. return ret;
  711. }
  712. static int expand_text(AVFilterContext *ctx)
  713. {
  714. DrawTextContext *s = ctx->priv;
  715. char *text = s->text;
  716. AVBPrint *bp = &s->expanded_text;
  717. int ret;
  718. av_bprint_clear(bp);
  719. while (*text) {
  720. if (*text == '\\' && text[1]) {
  721. av_bprint_chars(bp, text[1], 1);
  722. text += 2;
  723. } else if (*text == '%') {
  724. text++;
  725. if ((ret = expand_function(ctx, bp, &text)) < 0)
  726. return ret;
  727. } else {
  728. av_bprint_chars(bp, *text, 1);
  729. text++;
  730. }
  731. }
  732. if (!av_bprint_is_complete(bp))
  733. return AVERROR(ENOMEM);
  734. return 0;
  735. }
  736. static int draw_glyphs(DrawTextContext *s, AVFrame *frame,
  737. int width, int height, const uint8_t rgbcolor[4],
  738. FFDrawColor *color, int x, int y, int borderw)
  739. {
  740. char *text = s->expanded_text.str;
  741. uint32_t code = 0;
  742. int i, x1, y1;
  743. uint8_t *p;
  744. Glyph *glyph = NULL;
  745. for (i = 0, p = text; *p; i++) {
  746. FT_Bitmap bitmap;
  747. Glyph dummy = { 0 };
  748. GET_UTF8(code, *p++, continue;);
  749. /* skip new line chars, just go to new line */
  750. if (code == '\n' || code == '\r' || code == '\t')
  751. continue;
  752. dummy.code = code;
  753. glyph = av_tree_find(s->glyphs, &dummy, (void *)glyph_cmp, NULL);
  754. bitmap = borderw ? glyph->border_bitmap : glyph->bitmap;
  755. if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
  756. glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
  757. return AVERROR(EINVAL);
  758. x1 = s->positions[i].x+s->x+x - borderw;
  759. y1 = s->positions[i].y+s->y+y - borderw;
  760. ff_blend_mask(&s->dc, color,
  761. frame->data, frame->linesize, width, height,
  762. bitmap.buffer, bitmap.pitch,
  763. bitmap.width, bitmap.rows,
  764. bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
  765. 0, x1, y1);
  766. }
  767. return 0;
  768. }
  769. static int draw_text(AVFilterContext *ctx, AVFrame *frame,
  770. int width, int height)
  771. {
  772. DrawTextContext *s = ctx->priv;
  773. AVFilterLink *inlink = ctx->inputs[0];
  774. uint32_t code = 0, prev_code = 0;
  775. int x = 0, y = 0, i = 0, ret;
  776. int max_text_line_w = 0, len;
  777. int box_w, box_h;
  778. char *text;
  779. uint8_t *p;
  780. int y_min = 32000, y_max = -32000;
  781. int x_min = 32000, x_max = -32000;
  782. FT_Vector delta;
  783. Glyph *glyph = NULL, *prev_glyph = NULL;
  784. Glyph dummy = { 0 };
  785. time_t now = time(0);
  786. struct tm ltime;
  787. AVBPrint *bp = &s->expanded_text;
  788. av_bprint_clear(bp);
  789. if(s->basetime != AV_NOPTS_VALUE)
  790. now= frame->pts*av_q2d(ctx->inputs[0]->time_base) + s->basetime/1000000;
  791. switch (s->exp_mode) {
  792. case EXP_NONE:
  793. av_bprintf(bp, "%s", s->text);
  794. break;
  795. case EXP_NORMAL:
  796. if ((ret = expand_text(ctx)) < 0)
  797. return ret;
  798. break;
  799. case EXP_STRFTIME:
  800. localtime_r(&now, &ltime);
  801. av_bprint_strftime(bp, s->text, &ltime);
  802. break;
  803. }
  804. if (s->tc_opt_string) {
  805. char tcbuf[AV_TIMECODE_STR_SIZE];
  806. av_timecode_make_string(&s->tc, tcbuf, inlink->frame_count);
  807. av_bprint_clear(bp);
  808. av_bprintf(bp, "%s%s", s->text, tcbuf);
  809. }
  810. if (!av_bprint_is_complete(bp))
  811. return AVERROR(ENOMEM);
  812. text = s->expanded_text.str;
  813. if ((len = s->expanded_text.len) > s->nb_positions) {
  814. if (!(s->positions =
  815. av_realloc(s->positions, len*sizeof(*s->positions))))
  816. return AVERROR(ENOMEM);
  817. s->nb_positions = len;
  818. }
  819. x = 0;
  820. y = 0;
  821. /* load and cache glyphs */
  822. for (i = 0, p = text; *p; i++) {
  823. GET_UTF8(code, *p++, continue;);
  824. /* get glyph */
  825. dummy.code = code;
  826. glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
  827. if (!glyph) {
  828. load_glyph(ctx, &glyph, code);
  829. }
  830. y_min = FFMIN(glyph->bbox.yMin, y_min);
  831. y_max = FFMAX(glyph->bbox.yMax, y_max);
  832. x_min = FFMIN(glyph->bbox.xMin, x_min);
  833. x_max = FFMAX(glyph->bbox.xMax, x_max);
  834. }
  835. s->max_glyph_h = y_max - y_min;
  836. s->max_glyph_w = x_max - x_min;
  837. /* compute and save position for each glyph */
  838. glyph = NULL;
  839. for (i = 0, p = text; *p; i++) {
  840. GET_UTF8(code, *p++, continue;);
  841. /* skip the \n in the sequence \r\n */
  842. if (prev_code == '\r' && code == '\n')
  843. continue;
  844. prev_code = code;
  845. if (is_newline(code)) {
  846. max_text_line_w = FFMAX(max_text_line_w, x);
  847. y += s->max_glyph_h;
  848. x = 0;
  849. continue;
  850. }
  851. /* get glyph */
  852. prev_glyph = glyph;
  853. dummy.code = code;
  854. glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
  855. /* kerning */
  856. if (s->use_kerning && prev_glyph && glyph->code) {
  857. FT_Get_Kerning(s->face, prev_glyph->code, glyph->code,
  858. ft_kerning_default, &delta);
  859. x += delta.x >> 6;
  860. }
  861. /* save position */
  862. s->positions[i].x = x + glyph->bitmap_left;
  863. s->positions[i].y = y - glyph->bitmap_top + y_max;
  864. if (code == '\t') x = (x / s->tabsize + 1)*s->tabsize;
  865. else x += glyph->advance;
  866. }
  867. max_text_line_w = FFMAX(x, max_text_line_w);
  868. s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = max_text_line_w;
  869. s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = y + s->max_glyph_h;
  870. s->var_values[VAR_MAX_GLYPH_W] = s->max_glyph_w;
  871. s->var_values[VAR_MAX_GLYPH_H] = s->max_glyph_h;
  872. s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT ] = y_max;
  873. s->var_values[VAR_MAX_GLYPH_D] = s->var_values[VAR_DESCENT] = y_min;
  874. s->var_values[VAR_LINE_H] = s->var_values[VAR_LH] = s->max_glyph_h;
  875. s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
  876. s->y = s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, &s->prng);
  877. s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
  878. #if FF_API_DRAWTEXT_OLD_TIMELINE
  879. if (s->draw_pexpr){
  880. s->draw = av_expr_eval(s->draw_pexpr, s->var_values, &s->prng);
  881. if(!s->draw)
  882. return 0;
  883. }
  884. if (ctx->is_disabled)
  885. return 0;
  886. #endif
  887. box_w = FFMIN(width - 1 , max_text_line_w);
  888. box_h = FFMIN(height - 1, y + s->max_glyph_h);
  889. /* draw box */
  890. if (s->draw_box)
  891. ff_blend_rectangle(&s->dc, &s->boxcolor,
  892. frame->data, frame->linesize, width, height,
  893. s->x, s->y, box_w, box_h);
  894. if (s->shadowx || s->shadowy) {
  895. if ((ret = draw_glyphs(s, frame, width, height, s->shadowcolor.rgba,
  896. &s->shadowcolor, s->shadowx, s->shadowy, 0)) < 0)
  897. return ret;
  898. }
  899. if (s->borderw) {
  900. if ((ret = draw_glyphs(s, frame, width, height, s->bordercolor.rgba,
  901. &s->bordercolor, 0, 0, s->borderw)) < 0)
  902. return ret;
  903. }
  904. if ((ret = draw_glyphs(s, frame, width, height, s->fontcolor.rgba,
  905. &s->fontcolor, 0, 0, 0)) < 0)
  906. return ret;
  907. return 0;
  908. }
  909. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  910. {
  911. AVFilterContext *ctx = inlink->dst;
  912. AVFilterLink *outlink = ctx->outputs[0];
  913. DrawTextContext *s = ctx->priv;
  914. int ret;
  915. if (s->reload)
  916. if ((ret = load_textfile(ctx)) < 0)
  917. return ret;
  918. s->var_values[VAR_N] = inlink->frame_count+s->start_number;
  919. s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
  920. NAN : frame->pts * av_q2d(inlink->time_base);
  921. s->var_values[VAR_PICT_TYPE] = frame->pict_type;
  922. s->metadata = av_frame_get_metadata(frame);
  923. draw_text(ctx, frame, frame->width, frame->height);
  924. av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
  925. (int)s->var_values[VAR_N], s->var_values[VAR_T],
  926. (int)s->var_values[VAR_TEXT_W], (int)s->var_values[VAR_TEXT_H],
  927. s->x, s->y);
  928. return ff_filter_frame(outlink, frame);
  929. }
  930. static const AVFilterPad avfilter_vf_drawtext_inputs[] = {
  931. {
  932. .name = "default",
  933. .type = AVMEDIA_TYPE_VIDEO,
  934. .filter_frame = filter_frame,
  935. .config_props = config_input,
  936. .needs_writable = 1,
  937. },
  938. { NULL }
  939. };
  940. static const AVFilterPad avfilter_vf_drawtext_outputs[] = {
  941. {
  942. .name = "default",
  943. .type = AVMEDIA_TYPE_VIDEO,
  944. },
  945. { NULL }
  946. };
  947. AVFilter ff_vf_drawtext = {
  948. .name = "drawtext",
  949. .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
  950. .priv_size = sizeof(DrawTextContext),
  951. .priv_class = &drawtext_class,
  952. .init = init,
  953. .uninit = uninit,
  954. .query_formats = query_formats,
  955. .inputs = avfilter_vf_drawtext_inputs,
  956. .outputs = avfilter_vf_drawtext_outputs,
  957. .process_command = command,
  958. #if FF_API_DRAWTEXT_OLD_TIMELINE
  959. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  960. #else
  961. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  962. #endif
  963. };