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>
6 * This file is part of FFmpeg.
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.
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.
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
25 * drawtext filter, based on the original vhook/drawtext.c
26 * filter by Gustavo Sverzut Barbieri
34 #include <sys/types.h>
42 #if CONFIG_LIBFONTCONFIG
43 #include <fontconfig/fontconfig.h>
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"
55 #include "libavutil/time_internal.h"
56 #include "libavutil/tree.h"
57 #include "libavutil/lfg.h"
59 #include "drawutils.h"
69 #include FT_FREETYPE_H
73 static const char *const var_names[] = {
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
85 "t", ///< timestamp expressed in seconds
86 "text_h", "th", ///< height of the rendered text
87 "text_w", "tw", ///< width of the rendered text
94 static const char *const fun2_names[] = {
98 static double drand(void *opaque, double min, double max)
100 return min + (max-min) / UINT_MAX * av_lfg_get(opaque);
103 typedef double (*eval_func2)(void *, double a, double b);
105 static const eval_func2 fun2[] = {
114 VAR_MAIN_H, VAR_h, VAR_H,
115 VAR_MAIN_W, VAR_w, VAR_W,
116 VAR_MAX_GLYPH_A, VAR_ASCENT,
117 VAR_MAX_GLYPH_D, VAR_DESCENT,
131 enum expansion_mode {
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
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
157 int shadowx, shadowy;
158 int borderw; ///< border width
159 unsigned int fontsize; ///< font size to use
161 short int draw_box; ///< draw box around text - true or false
162 int boxborderw; ///< box border width
163 int use_kerning; ///< font kerning is used - true/false
164 int tabsize; ///< tab size
165 int fix_bounds; ///< do we let it go out of frame bounds - t/f
168 FFDrawColor fontcolor; ///< foreground color
169 FFDrawColor shadowcolor; ///< shadow color
170 FFDrawColor bordercolor; ///< border color
171 FFDrawColor boxcolor; ///< background color
173 FT_Library library; ///< freetype font library handle
174 FT_Face face; ///< freetype font face handle
175 FT_Stroker stroker; ///< freetype stroker handle
176 struct AVTreeNode *glyphs; ///< rendered glyphs, stored using the UTF-32 char code
177 char *x_expr; ///< expression for x position
178 char *y_expr; ///< expression for y position
179 AVExpr *x_pexpr, *y_pexpr; ///< parsed expressions for x and y
180 int64_t basetime; ///< base pts time in the real world for display
181 double var_values[VAR_VARS_NB];
185 AVLFG prng; ///< random
186 char *tc_opt_string; ///< specified timecode option string
187 AVRational tc_rate; ///< frame rate for timecode
188 AVTimecode tc; ///< timecode context
189 int tc24hmax; ///< 1 if timecode is wrapped to 24 hours, 0 otherwise
190 int reload; ///< reload text file for each frame
191 int start_number; ///< starting frame number for n/frame_num var
192 #if CONFIG_LIBFRIBIDI
193 int text_shaping; ///< 1 to shape the text before drawing it
195 AVDictionary *metadata;
198 #define OFFSET(x) offsetof(DrawTextContext, x)
199 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
201 static const AVOption drawtext_options[]= {
202 {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
203 {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
204 {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
205 {"fontcolor", "set foreground color", OFFSET(fontcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
206 {"fontcolor_expr", "set foreground color expression", OFFSET(fontcolor_expr), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS},
207 {"boxcolor", "set box color", OFFSET(boxcolor.rgba), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
208 {"bordercolor", "set border color", OFFSET(bordercolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
209 {"shadowcolor", "set shadow color", OFFSET(shadowcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
210 {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 , FLAGS},
211 {"boxborderw", "set box border width", OFFSET(boxborderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
212 {"fontsize", "set font size", OFFSET(fontsize), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX , FLAGS},
213 {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
214 {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, CHAR_MIN, CHAR_MAX, FLAGS},
215 {"shadowx", "set shadow x offset", OFFSET(shadowx), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
216 {"shadowy", "set shadow y offset", OFFSET(shadowy), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
217 {"borderw", "set border width", OFFSET(borderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX , FLAGS},
218 {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX , FLAGS},
219 {"basetime", "set base time", OFFSET(basetime), AV_OPT_TYPE_INT64, {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX , FLAGS},
220 #if CONFIG_LIBFONTCONFIG
221 { "font", "Font name", OFFSET(font), AV_OPT_TYPE_STRING, { .str = "Sans" }, .flags = FLAGS },
224 {"expansion", "set the expansion mode", OFFSET(exp_mode), AV_OPT_TYPE_INT, {.i64=EXP_NORMAL}, 0, 2, FLAGS, "expansion"},
225 {"none", "set no expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NONE}, 0, 0, FLAGS, "expansion"},
226 {"normal", "set normal expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NORMAL}, 0, 0, FLAGS, "expansion"},
227 {"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0, 0, FLAGS, "expansion"},
229 {"timecode", "set initial timecode", OFFSET(tc_opt_string), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
230 {"tc24hmax", "set 24 hours max (timecode only)", OFFSET(tc24hmax), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
231 {"timecode_rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
232 {"r", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
233 {"rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
234 {"reload", "reload text file for each frame", OFFSET(reload), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
235 { "alpha", "apply alpha while rendering", OFFSET(a_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
236 {"fix_bounds", "check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
237 {"start_number", "start frame number for n/frame_num variable", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS},
239 #if CONFIG_LIBFRIBIDI
240 {"text_shaping", "attempt to shape text before drawing", OFFSET(text_shaping), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
243 /* FT_LOAD_* flags */
244 { "ft_load_flags", "set font loading flags for libfreetype", OFFSET(ft_load_flags), AV_OPT_TYPE_FLAGS, { .i64 = FT_LOAD_DEFAULT }, 0, INT_MAX, FLAGS, "ft_load_flags" },
245 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags = FLAGS, .unit = "ft_load_flags" },
246 { "no_scale", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags = FLAGS, .unit = "ft_load_flags" },
247 { "no_hinting", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags = FLAGS, .unit = "ft_load_flags" },
248 { "render", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags = FLAGS, .unit = "ft_load_flags" },
249 { "no_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
250 { "vertical_layout", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags = FLAGS, .unit = "ft_load_flags" },
251 { "force_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
252 { "crop_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
253 { "pedantic", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags = FLAGS, .unit = "ft_load_flags" },
254 { "ignore_global_advance_width", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags = FLAGS, .unit = "ft_load_flags" },
255 { "no_recurse", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags = FLAGS, .unit = "ft_load_flags" },
256 { "ignore_transform", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags = FLAGS, .unit = "ft_load_flags" },
257 { "monochrome", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags = FLAGS, .unit = "ft_load_flags" },
258 { "linear_design", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags = FLAGS, .unit = "ft_load_flags" },
259 { "no_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
263 AVFILTER_DEFINE_CLASS(drawtext);
265 #undef __FTERRORS_H__
266 #define FT_ERROR_START_LIST {
267 #define FT_ERRORDEF(e, v, s) { (e), (s) },
268 #define FT_ERROR_END_LIST { 0, NULL } };
270 static const struct ft_error
277 #define FT_ERRMSG(e) ft_errors[e].err_msg
279 typedef struct Glyph {
281 FT_Glyph border_glyph;
283 FT_Bitmap bitmap; ///< array holding bitmaps of font
284 FT_Bitmap border_bitmap; ///< array holding bitmaps of font border
291 static int glyph_cmp(const void *key, const void *b)
293 const Glyph *a = key, *bb = b;
294 int64_t diff = (int64_t)a->code - (int64_t)bb->code;
295 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
299 * Load glyphs corresponding to the UTF-32 codepoint code.
301 static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
303 DrawTextContext *s = ctx->priv;
304 FT_BitmapGlyph bitmapglyph;
306 struct AVTreeNode *node = NULL;
309 /* load glyph into s->face->glyph */
310 if (FT_Load_Char(s->face, code, s->ft_load_flags))
311 return AVERROR(EINVAL);
313 glyph = av_mallocz(sizeof(*glyph));
315 ret = AVERROR(ENOMEM);
320 if (FT_Get_Glyph(s->face->glyph, &glyph->glyph)) {
321 ret = AVERROR(EINVAL);
325 glyph->border_glyph = glyph->glyph;
326 if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0) ||
327 FT_Glyph_To_Bitmap(&glyph->border_glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
328 ret = AVERROR_EXTERNAL;
331 bitmapglyph = (FT_BitmapGlyph) glyph->border_glyph;
332 glyph->border_bitmap = bitmapglyph->bitmap;
334 if (FT_Glyph_To_Bitmap(&glyph->glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
335 ret = AVERROR_EXTERNAL;
338 bitmapglyph = (FT_BitmapGlyph) glyph->glyph;
340 glyph->bitmap = bitmapglyph->bitmap;
341 glyph->bitmap_left = bitmapglyph->left;
342 glyph->bitmap_top = bitmapglyph->top;
343 glyph->advance = s->face->glyph->advance.x >> 6;
345 /* measure text height to calculate text_height (or the maximum text height) */
346 FT_Glyph_Get_CBox(glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
348 /* cache the newly created glyph */
349 if (!(node = av_tree_node_alloc())) {
350 ret = AVERROR(ENOMEM);
353 av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
361 av_freep(&glyph->glyph);
368 static int load_font_file(AVFilterContext *ctx, const char *path, int index)
370 DrawTextContext *s = ctx->priv;
373 err = FT_New_Face(s->library, path, index, &s->face);
375 #if !CONFIG_LIBFONTCONFIG
376 av_log(ctx, AV_LOG_ERROR, "Could not load font \"%s\": %s\n",
377 s->fontfile, FT_ERRMSG(err));
379 return AVERROR(EINVAL);
384 #if CONFIG_LIBFONTCONFIG
385 static int load_font_fontconfig(AVFilterContext *ctx)
387 DrawTextContext *s = ctx->priv;
388 FcConfig *fontconfig;
389 FcPattern *pat, *best;
390 FcResult result = FcResultMatch;
394 int err = AVERROR(ENOENT);
396 fontconfig = FcInitLoadConfigAndFonts();
398 av_log(ctx, AV_LOG_ERROR, "impossible to init fontconfig\n");
399 return AVERROR_UNKNOWN;
401 pat = FcNameParse(s->fontfile ? s->fontfile :
402 (uint8_t *)(intptr_t)"default");
404 av_log(ctx, AV_LOG_ERROR, "could not parse fontconfig pat");
405 return AVERROR(EINVAL);
408 FcPatternAddString(pat, FC_FAMILY, s->font);
410 FcPatternAddDouble(pat, FC_SIZE, (double)s->fontsize);
412 FcDefaultSubstitute(pat);
414 if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
415 av_log(ctx, AV_LOG_ERROR, "could not substitue fontconfig options"); /* very unlikely */
416 FcPatternDestroy(pat);
417 return AVERROR(ENOMEM);
420 best = FcFontMatch(fontconfig, pat, &result);
421 FcPatternDestroy(pat);
423 if (!best || result != FcResultMatch) {
424 av_log(ctx, AV_LOG_ERROR,
425 "Cannot find a valid font for the family %s\n",
431 FcPatternGetInteger(best, FC_INDEX, 0, &index ) != FcResultMatch ||
432 FcPatternGetDouble (best, FC_SIZE, 0, &size ) != FcResultMatch) {
433 av_log(ctx, AV_LOG_ERROR, "impossible to find font information");
434 return AVERROR(EINVAL);
437 if (FcPatternGetString(best, FC_FILE, 0, &filename) != FcResultMatch) {
438 av_log(ctx, AV_LOG_ERROR, "No file path for %s\n",
443 av_log(ctx, AV_LOG_INFO, "Using \"%s\"\n", filename);
445 s->fontsize = size + 0.5;
447 err = load_font_file(ctx, filename, index);
450 FcConfigDestroy(fontconfig);
452 FcPatternDestroy(best);
457 static int load_font(AVFilterContext *ctx)
459 DrawTextContext *s = ctx->priv;
462 /* load the face, and set up the encoding, which is by default UTF-8 */
463 err = load_font_file(ctx, s->fontfile, 0);
466 #if CONFIG_LIBFONTCONFIG
467 err = load_font_fontconfig(ctx);
474 static int load_textfile(AVFilterContext *ctx)
476 DrawTextContext *s = ctx->priv;
482 if ((err = av_file_map(s->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
483 av_log(ctx, AV_LOG_ERROR,
484 "The text file '%s' could not be read or is empty\n",
489 if (textbuf_size > SIZE_MAX - 1 || !(tmp = av_realloc(s->text, textbuf_size + 1))) {
490 av_file_unmap(textbuf, textbuf_size);
491 return AVERROR(ENOMEM);
494 memcpy(s->text, textbuf, textbuf_size);
495 s->text[textbuf_size] = 0;
496 av_file_unmap(textbuf, textbuf_size);
501 static inline int is_newline(uint32_t c)
503 return c == '\n' || c == '\r' || c == '\f' || c == '\v';
506 #if CONFIG_LIBFRIBIDI
507 static int shape_text(AVFilterContext *ctx)
509 DrawTextContext *s = ctx->priv;
511 int ret = AVERROR(ENOMEM);
512 static const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT |
513 FRIBIDI_FLAGS_ARABIC;
514 FriBidiChar *unicodestr = NULL;
516 FriBidiParType direction = FRIBIDI_PAR_LTR;
517 FriBidiStrIndex line_start = 0;
518 FriBidiStrIndex line_end = 0;
519 FriBidiLevel *embedding_levels = NULL;
520 FriBidiArabicProp *ar_props = NULL;
521 FriBidiCharType *bidi_types = NULL;
524 len = strlen(s->text);
525 if (!(unicodestr = av_malloc_array(len, sizeof(*unicodestr)))) {
528 len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8,
529 s->text, len, unicodestr);
531 bidi_types = av_malloc_array(len, sizeof(*bidi_types));
536 fribidi_get_bidi_types(unicodestr, len, bidi_types);
538 embedding_levels = av_malloc_array(len, sizeof(*embedding_levels));
539 if (!embedding_levels) {
543 if (!fribidi_get_par_embedding_levels(bidi_types, len, &direction,
548 ar_props = av_malloc_array(len, sizeof(*ar_props));
553 fribidi_get_joining_types(unicodestr, len, ar_props);
554 fribidi_join_arabic(bidi_types, len, embedding_levels, ar_props);
555 fribidi_shape(flags, embedding_levels, len, ar_props, unicodestr);
557 for (line_end = 0, line_start = 0; line_end < len; line_end++) {
558 if (is_newline(unicodestr[line_end]) || line_end == len - 1) {
559 if (!fribidi_reorder_line(flags, bidi_types,
560 line_end - line_start + 1, line_start,
561 direction, embedding_levels, unicodestr,
565 line_start = line_end + 1;
569 /* Remove zero-width fill chars put in by libfribidi */
570 for (i = 0, j = 0; i < len; i++)
571 if (unicodestr[i] != FRIBIDI_CHAR_FILL)
572 unicodestr[j++] = unicodestr[i];
575 if (!(tmp = av_realloc(s->text, (len * 4 + 1) * sizeof(*s->text)))) {
576 /* Use len * 4, as a unicode character can be up to 4 bytes in UTF-8 */
581 len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8,
582 unicodestr, len, s->text);
587 av_free(embedding_levels);
594 static av_cold int init(AVFilterContext *ctx)
597 DrawTextContext *s = ctx->priv;
600 if (!s->fontfile && !CONFIG_LIBFONTCONFIG) {
601 av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
602 return AVERROR(EINVAL);
607 av_log(ctx, AV_LOG_ERROR,
608 "Both text and text file provided. Please provide only one\n");
609 return AVERROR(EINVAL);
611 if ((err = load_textfile(ctx)) < 0)
615 if (s->reload && !s->textfile)
616 av_log(ctx, AV_LOG_WARNING, "No file to reload\n");
618 if (s->tc_opt_string) {
619 int ret = av_timecode_init_from_string(&s->tc, s->tc_rate,
620 s->tc_opt_string, ctx);
624 s->tc.flags |= AV_TIMECODE_FLAG_24HOURSMAX;
626 s->text = av_strdup("");
630 av_log(ctx, AV_LOG_ERROR,
631 "Either text, a valid file or a timecode must be provided\n");
632 return AVERROR(EINVAL);
635 #if CONFIG_LIBFRIBIDI
637 if ((err = shape_text(ctx)) < 0)
641 if ((err = FT_Init_FreeType(&(s->library)))) {
642 av_log(ctx, AV_LOG_ERROR,
643 "Could not load FreeType: %s\n", FT_ERRMSG(err));
644 return AVERROR(EINVAL);
647 err = load_font(ctx);
652 if ((err = FT_Set_Pixel_Sizes(s->face, 0, s->fontsize))) {
653 av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
654 s->fontsize, FT_ERRMSG(err));
655 return AVERROR(EINVAL);
659 if (FT_Stroker_New(s->library, &s->stroker)) {
660 av_log(ctx, AV_LOG_ERROR, "Coult not init FT stroker\n");
661 return AVERROR_EXTERNAL;
663 FT_Stroker_Set(s->stroker, s->borderw << 6, FT_STROKER_LINECAP_ROUND,
664 FT_STROKER_LINEJOIN_ROUND, 0);
667 s->use_kerning = FT_HAS_KERNING(s->face);
669 /* load the fallback glyph with code 0 */
670 load_glyph(ctx, NULL, 0);
672 /* set the tabsize in pixels */
673 if ((err = load_glyph(ctx, &glyph, ' ')) < 0) {
674 av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
677 s->tabsize *= glyph->advance;
679 if (s->exp_mode == EXP_STRFTIME &&
680 (strchr(s->text, '%') || strchr(s->text, '\\')))
681 av_log(ctx, AV_LOG_WARNING, "expansion=strftime is deprecated.\n");
683 av_bprint_init(&s->expanded_text, 0, AV_BPRINT_SIZE_UNLIMITED);
684 av_bprint_init(&s->expanded_fontcolor, 0, AV_BPRINT_SIZE_UNLIMITED);
689 static int query_formats(AVFilterContext *ctx)
691 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
694 static int glyph_enu_free(void *opaque, void *elem)
698 FT_Done_Glyph(glyph->glyph);
699 FT_Done_Glyph(glyph->border_glyph);
704 static av_cold void uninit(AVFilterContext *ctx)
706 DrawTextContext *s = ctx->priv;
708 av_expr_free(s->x_pexpr);
709 av_expr_free(s->y_pexpr);
710 s->x_pexpr = s->y_pexpr = NULL;
711 av_freep(&s->positions);
715 av_tree_enumerate(s->glyphs, NULL, NULL, glyph_enu_free);
716 av_tree_destroy(s->glyphs);
719 FT_Done_Face(s->face);
720 FT_Stroker_Done(s->stroker);
721 FT_Done_FreeType(s->library);
723 av_bprint_finalize(&s->expanded_text, NULL);
724 av_bprint_finalize(&s->expanded_fontcolor, NULL);
727 static int config_input(AVFilterLink *inlink)
729 AVFilterContext *ctx = inlink->dst;
730 DrawTextContext *s = ctx->priv;
733 ff_draw_init(&s->dc, inlink->format, 0);
734 ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
735 ff_draw_color(&s->dc, &s->shadowcolor, s->shadowcolor.rgba);
736 ff_draw_color(&s->dc, &s->bordercolor, s->bordercolor.rgba);
737 ff_draw_color(&s->dc, &s->boxcolor, s->boxcolor.rgba);
739 s->var_values[VAR_w] = s->var_values[VAR_W] = s->var_values[VAR_MAIN_W] = inlink->w;
740 s->var_values[VAR_h] = s->var_values[VAR_H] = s->var_values[VAR_MAIN_H] = inlink->h;
741 s->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
742 s->var_values[VAR_DAR] = (double)inlink->w / inlink->h * s->var_values[VAR_SAR];
743 s->var_values[VAR_HSUB] = 1 << s->dc.hsub_max;
744 s->var_values[VAR_VSUB] = 1 << s->dc.vsub_max;
745 s->var_values[VAR_X] = NAN;
746 s->var_values[VAR_Y] = NAN;
747 s->var_values[VAR_T] = NAN;
749 av_lfg_init(&s->prng, av_get_random_seed());
751 av_expr_free(s->x_pexpr);
752 av_expr_free(s->y_pexpr);
753 s->x_pexpr = s->y_pexpr = NULL;
755 if ((ret = av_expr_parse(&s->x_pexpr, s->x_expr, var_names,
756 NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
757 (ret = av_expr_parse(&s->y_pexpr, s->y_expr, var_names,
758 NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
759 (ret = av_expr_parse(&s->a_pexpr, s->a_expr, var_names,
760 NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
762 return AVERROR(EINVAL);
767 static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
769 DrawTextContext *s = ctx->priv;
771 if (!strcmp(cmd, "reinit")) {
775 if ((ret = av_set_options_string(ctx, arg, "=", ":")) < 0)
777 if ((ret = init(ctx)) < 0)
779 return config_input(ctx->inputs[0]);
782 return AVERROR(ENOSYS);
785 static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp,
786 char *fct, unsigned argc, char **argv, int tag)
788 DrawTextContext *s = ctx->priv;
790 av_bprintf(bp, "%c", av_get_picture_type_char(s->var_values[VAR_PICT_TYPE]));
794 static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
795 char *fct, unsigned argc, char **argv, int tag)
797 DrawTextContext *s = ctx->priv;
799 double pts = s->var_values[VAR_T];
802 fmt = argc >= 1 ? argv[0] : "flt";
805 if ((ret = av_parse_time(&delta, argv[1], 1)) < 0) {
806 av_log(ctx, AV_LOG_ERROR, "Invalid delta '%s'\n", argv[1]);
809 pts += (double)delta / AV_TIME_BASE;
811 if (!strcmp(fmt, "flt")) {
812 av_bprintf(bp, "%.6f", s->var_values[VAR_T]);
813 } else if (!strcmp(fmt, "hms")) {
815 av_bprintf(bp, " ??:??:??.???");
817 int64_t ms = llrint(pts * 1000);
823 av_bprintf(bp, "%c%02d:%02d:%02d.%03d", sign,
824 (int)(ms / (60 * 60 * 1000)),
825 (int)(ms / (60 * 1000)) % 60,
826 (int)(ms / 1000) % 60,
829 } else if (!strcmp(fmt, "localtime") ||
830 !strcmp(fmt, "gmtime")) {
832 time_t ms = (time_t)pts;
833 const char *timefmt = argc >= 3 ? argv[2] : "%Y-%m-%d %H:%M:%S";
834 if (!strcmp(fmt, "localtime"))
835 localtime_r(&ms, &tm);
838 av_bprint_strftime(bp, timefmt, &tm);
840 av_log(ctx, AV_LOG_ERROR, "Invalid format '%s'\n", fmt);
841 return AVERROR(EINVAL);
846 static int func_frame_num(AVFilterContext *ctx, AVBPrint *bp,
847 char *fct, unsigned argc, char **argv, int tag)
849 DrawTextContext *s = ctx->priv;
851 av_bprintf(bp, "%d", (int)s->var_values[VAR_N]);
855 static int func_metadata(AVFilterContext *ctx, AVBPrint *bp,
856 char *fct, unsigned argc, char **argv, int tag)
858 DrawTextContext *s = ctx->priv;
859 AVDictionaryEntry *e = av_dict_get(s->metadata, argv[0], NULL, 0);
862 av_bprintf(bp, "%s", e->value);
864 av_bprintf(bp, "%s", argv[1]);
868 static int func_strftime(AVFilterContext *ctx, AVBPrint *bp,
869 char *fct, unsigned argc, char **argv, int tag)
871 const char *fmt = argc ? argv[0] : "%Y-%m-%d %H:%M:%S";
877 localtime_r(&now, &tm);
879 tm = *gmtime_r(&now, &tm);
880 av_bprint_strftime(bp, fmt, &tm);
884 static int func_eval_expr(AVFilterContext *ctx, AVBPrint *bp,
885 char *fct, unsigned argc, char **argv, int tag)
887 DrawTextContext *s = ctx->priv;
891 ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
892 NULL, NULL, fun2_names, fun2,
895 av_log(ctx, AV_LOG_ERROR,
896 "Expression '%s' for the expr text expansion function is not valid\n",
899 av_bprintf(bp, "%f", res);
904 static int func_eval_expr_int_format(AVFilterContext *ctx, AVBPrint *bp,
905 char *fct, unsigned argc, char **argv, int tag)
907 DrawTextContext *s = ctx->priv;
911 unsigned int positions = 0;
912 char fmt_str[30] = "%";
915 * argv[0] expression to be converted to `int`
916 * argv[1] format: 'x', 'X', 'd' or 'u'
917 * argv[2] positions printed (optional)
920 ret = av_expr_parse_and_eval(&res, argv[0], var_names, s->var_values,
921 NULL, NULL, fun2_names, fun2,
924 av_log(ctx, AV_LOG_ERROR,
925 "Expression '%s' for the expr text expansion function is not valid\n",
930 if (!strchr("xXdu", argv[1][0])) {
931 av_log(ctx, AV_LOG_ERROR, "Invalid format '%c' specified,"
932 " allowed values: 'x', 'X', 'd', 'u'\n", argv[1][0]);
933 return AVERROR(EINVAL);
937 ret = sscanf(argv[2], "%u", &positions);
939 av_log(ctx, AV_LOG_ERROR, "expr_int_format(): Invalid number of positions"
940 " to print: '%s'\n", argv[2]);
941 return AVERROR(EINVAL);
945 feclearexcept(FE_ALL_EXCEPT);
947 if ((ret = fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW))) {
948 av_log(ctx, AV_LOG_ERROR, "Conversion of floating-point result to int failed. Control register: 0x%08x. Conversion result: %d\n", ret, intval);
949 return AVERROR(EINVAL);
953 av_strlcatf(fmt_str, sizeof(fmt_str), "0%u", positions);
954 av_strlcatf(fmt_str, sizeof(fmt_str), "%c", argv[1][0]);
956 av_log(ctx, AV_LOG_DEBUG, "Formatting value %f (expr '%s') with spec '%s'\n",
957 res, argv[0], fmt_str);
959 av_bprintf(bp, fmt_str, intval);
964 static const struct drawtext_function {
966 unsigned argc_min, argc_max;
967 int tag; /**< opaque argument to func */
968 int (*func)(AVFilterContext *, AVBPrint *, char *, unsigned, char **, int);
970 { "expr", 1, 1, 0, func_eval_expr },
971 { "e", 1, 1, 0, func_eval_expr },
972 { "expr_int_format", 2, 3, 0, func_eval_expr_int_format },
973 { "eif", 2, 3, 0, func_eval_expr_int_format },
974 { "pict_type", 0, 0, 0, func_pict_type },
975 { "pts", 0, 3, 0, func_pts },
976 { "gmtime", 0, 1, 'G', func_strftime },
977 { "localtime", 0, 1, 'L', func_strftime },
978 { "frame_num", 0, 0, 0, func_frame_num },
979 { "n", 0, 0, 0, func_frame_num },
980 { "metadata", 1, 2, 0, func_metadata },
983 static int eval_function(AVFilterContext *ctx, AVBPrint *bp, char *fct,
984 unsigned argc, char **argv)
988 for (i = 0; i < FF_ARRAY_ELEMS(functions); i++) {
989 if (strcmp(fct, functions[i].name))
991 if (argc < functions[i].argc_min) {
992 av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at least %d arguments\n",
993 fct, functions[i].argc_min);
994 return AVERROR(EINVAL);
996 if (argc > functions[i].argc_max) {
997 av_log(ctx, AV_LOG_ERROR, "%%{%s} requires at most %d arguments\n",
998 fct, functions[i].argc_max);
999 return AVERROR(EINVAL);
1003 if (i >= FF_ARRAY_ELEMS(functions)) {
1004 av_log(ctx, AV_LOG_ERROR, "%%{%s} is not known\n", fct);
1005 return AVERROR(EINVAL);
1007 return functions[i].func(ctx, bp, fct, argc, argv, functions[i].tag);
1010 static int expand_function(AVFilterContext *ctx, AVBPrint *bp, char **rtext)
1012 const char *text = *rtext;
1013 char *argv[16] = { NULL };
1014 unsigned argc = 0, i;
1018 av_log(ctx, AV_LOG_ERROR, "Stray %% near '%s'\n", text);
1019 return AVERROR(EINVAL);
1023 if (!(argv[argc++] = av_get_token(&text, ":}"))) {
1024 ret = AVERROR(ENOMEM);
1028 av_log(ctx, AV_LOG_ERROR, "Unterminated %%{} near '%s'\n", *rtext);
1029 ret = AVERROR(EINVAL);
1032 if (argc == FF_ARRAY_ELEMS(argv))
1033 av_freep(&argv[--argc]); /* error will be caught later */
1039 if ((ret = eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
1042 *rtext = (char *)text + 1;
1045 for (i = 0; i < argc; i++)
1050 static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
1054 av_bprint_clear(bp);
1056 if (*text == '\\' && text[1]) {
1057 av_bprint_chars(bp, text[1], 1);
1059 } else if (*text == '%') {
1061 if ((ret = expand_function(ctx, bp, &text)) < 0)
1064 av_bprint_chars(bp, *text, 1);
1068 if (!av_bprint_is_complete(bp))
1069 return AVERROR(ENOMEM);
1073 static int draw_glyphs(DrawTextContext *s, AVFrame *frame,
1074 int width, int height,
1076 int x, int y, int borderw)
1078 char *text = s->expanded_text.str;
1082 Glyph *glyph = NULL;
1084 for (i = 0, p = text; *p; i++) {
1086 Glyph dummy = { 0 };
1087 GET_UTF8(code, *p++, continue;);
1089 /* skip new line chars, just go to new line */
1090 if (code == '\n' || code == '\r' || code == '\t')
1094 glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1096 bitmap = borderw ? glyph->border_bitmap : glyph->bitmap;
1098 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
1099 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
1100 return AVERROR(EINVAL);
1102 x1 = s->positions[i].x+s->x+x - borderw;
1103 y1 = s->positions[i].y+s->y+y - borderw;
1105 ff_blend_mask(&s->dc, color,
1106 frame->data, frame->linesize, width, height,
1107 bitmap.buffer, bitmap.pitch,
1108 bitmap.width, bitmap.rows,
1109 bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
1117 static void update_color_with_alpha(DrawTextContext *s, FFDrawColor *color, const FFDrawColor incolor)
1120 color->rgba[3] = (color->rgba[3] * s->alpha) / 255;
1121 ff_draw_color(&s->dc, color, color->rgba);
1124 static void update_alpha(DrawTextContext *s)
1126 double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
1133 else if (alpha <= 0)
1136 s->alpha = 256 * alpha;
1139 static int draw_text(AVFilterContext *ctx, AVFrame *frame,
1140 int width, int height)
1142 DrawTextContext *s = ctx->priv;
1143 AVFilterLink *inlink = ctx->inputs[0];
1145 uint32_t code = 0, prev_code = 0;
1146 int x = 0, y = 0, i = 0, ret;
1147 int max_text_line_w = 0, len;
1151 int y_min = 32000, y_max = -32000;
1152 int x_min = 32000, x_max = -32000;
1154 Glyph *glyph = NULL, *prev_glyph = NULL;
1155 Glyph dummy = { 0 };
1157 time_t now = time(0);
1159 AVBPrint *bp = &s->expanded_text;
1161 FFDrawColor fontcolor;
1162 FFDrawColor shadowcolor;
1163 FFDrawColor bordercolor;
1164 FFDrawColor boxcolor;
1166 av_bprint_clear(bp);
1168 if(s->basetime != AV_NOPTS_VALUE)
1169 now= frame->pts*av_q2d(ctx->inputs[0]->time_base) + s->basetime/1000000;
1171 switch (s->exp_mode) {
1173 av_bprintf(bp, "%s", s->text);
1176 if ((ret = expand_text(ctx, s->text, &s->expanded_text)) < 0)
1180 localtime_r(&now, <ime);
1181 av_bprint_strftime(bp, s->text, <ime);
1185 if (s->tc_opt_string) {
1186 char tcbuf[AV_TIMECODE_STR_SIZE];
1187 av_timecode_make_string(&s->tc, tcbuf, inlink->frame_count);
1188 av_bprint_clear(bp);
1189 av_bprintf(bp, "%s%s", s->text, tcbuf);
1192 if (!av_bprint_is_complete(bp))
1193 return AVERROR(ENOMEM);
1194 text = s->expanded_text.str;
1195 if ((len = s->expanded_text.len) > s->nb_positions) {
1196 if (!(s->positions =
1197 av_realloc(s->positions, len*sizeof(*s->positions))))
1198 return AVERROR(ENOMEM);
1199 s->nb_positions = len;
1202 if (s->fontcolor_expr[0]) {
1203 /* If expression is set, evaluate and replace the static value */
1204 av_bprint_clear(&s->expanded_fontcolor);
1205 if ((ret = expand_text(ctx, s->fontcolor_expr, &s->expanded_fontcolor)) < 0)
1207 if (!av_bprint_is_complete(&s->expanded_fontcolor))
1208 return AVERROR(ENOMEM);
1209 av_log(s, AV_LOG_DEBUG, "Evaluated fontcolor is '%s'\n", s->expanded_fontcolor.str);
1210 ret = av_parse_color(s->fontcolor.rgba, s->expanded_fontcolor.str, -1, s);
1213 ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
1219 /* load and cache glyphs */
1220 for (i = 0, p = text; *p; i++) {
1221 GET_UTF8(code, *p++, continue;);
1225 glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1227 ret = load_glyph(ctx, &glyph, code);
1232 y_min = FFMIN(glyph->bbox.yMin, y_min);
1233 y_max = FFMAX(glyph->bbox.yMax, y_max);
1234 x_min = FFMIN(glyph->bbox.xMin, x_min);
1235 x_max = FFMAX(glyph->bbox.xMax, x_max);
1237 s->max_glyph_h = y_max - y_min;
1238 s->max_glyph_w = x_max - x_min;
1240 /* compute and save position for each glyph */
1242 for (i = 0, p = text; *p; i++) {
1243 GET_UTF8(code, *p++, continue;);
1245 /* skip the \n in the sequence \r\n */
1246 if (prev_code == '\r' && code == '\n')
1250 if (is_newline(code)) {
1252 max_text_line_w = FFMAX(max_text_line_w, x);
1253 y += s->max_glyph_h;
1261 glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
1264 if (s->use_kerning && prev_glyph && glyph->code) {
1265 FT_Get_Kerning(s->face, prev_glyph->code, glyph->code,
1266 ft_kerning_default, &delta);
1271 s->positions[i].x = x + glyph->bitmap_left;
1272 s->positions[i].y = y - glyph->bitmap_top + y_max;
1273 if (code == '\t') x = (x / s->tabsize + 1)*s->tabsize;
1274 else x += glyph->advance;
1277 max_text_line_w = FFMAX(x, max_text_line_w);
1279 s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = max_text_line_w;
1280 s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = y + s->max_glyph_h;
1282 s->var_values[VAR_MAX_GLYPH_W] = s->max_glyph_w;
1283 s->var_values[VAR_MAX_GLYPH_H] = s->max_glyph_h;
1284 s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT ] = y_max;
1285 s->var_values[VAR_MAX_GLYPH_D] = s->var_values[VAR_DESCENT] = y_min;
1287 s->var_values[VAR_LINE_H] = s->var_values[VAR_LH] = s->max_glyph_h;
1289 s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
1290 s->y = s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, &s->prng);
1291 s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
1294 update_color_with_alpha(s, &fontcolor , s->fontcolor );
1295 update_color_with_alpha(s, &shadowcolor, s->shadowcolor);
1296 update_color_with_alpha(s, &bordercolor, s->bordercolor);
1297 update_color_with_alpha(s, &boxcolor , s->boxcolor );
1299 box_w = FFMIN(width - 1 , max_text_line_w);
1300 box_h = FFMIN(height - 1, y + s->max_glyph_h);
1304 ff_blend_rectangle(&s->dc, &boxcolor,
1305 frame->data, frame->linesize, width, height,
1306 s->x - s->boxborderw, s->y - s->boxborderw,
1307 box_w + s->boxborderw * 2, box_h + s->boxborderw * 2);
1309 if (s->shadowx || s->shadowy) {
1310 if ((ret = draw_glyphs(s, frame, width, height,
1311 &shadowcolor, s->shadowx, s->shadowy, 0)) < 0)
1316 if ((ret = draw_glyphs(s, frame, width, height,
1317 &bordercolor, 0, 0, s->borderw)) < 0)
1320 if ((ret = draw_glyphs(s, frame, width, height,
1321 &fontcolor, 0, 0, 0)) < 0)
1327 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
1329 AVFilterContext *ctx = inlink->dst;
1330 AVFilterLink *outlink = ctx->outputs[0];
1331 DrawTextContext *s = ctx->priv;
1335 if ((ret = load_textfile(ctx)) < 0) {
1336 av_frame_free(&frame);
1339 #if CONFIG_LIBFRIBIDI
1340 if (s->text_shaping)
1341 if ((ret = shape_text(ctx)) < 0) {
1342 av_frame_free(&frame);
1348 s->var_values[VAR_N] = inlink->frame_count+s->start_number;
1349 s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
1350 NAN : frame->pts * av_q2d(inlink->time_base);
1352 s->var_values[VAR_PICT_TYPE] = frame->pict_type;
1353 s->metadata = av_frame_get_metadata(frame);
1355 draw_text(ctx, frame, frame->width, frame->height);
1357 av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
1358 (int)s->var_values[VAR_N], s->var_values[VAR_T],
1359 (int)s->var_values[VAR_TEXT_W], (int)s->var_values[VAR_TEXT_H],
1362 return ff_filter_frame(outlink, frame);
1365 static const AVFilterPad avfilter_vf_drawtext_inputs[] = {
1368 .type = AVMEDIA_TYPE_VIDEO,
1369 .filter_frame = filter_frame,
1370 .config_props = config_input,
1371 .needs_writable = 1,
1376 static const AVFilterPad avfilter_vf_drawtext_outputs[] = {
1379 .type = AVMEDIA_TYPE_VIDEO,
1384 AVFilter ff_vf_drawtext = {
1386 .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
1387 .priv_size = sizeof(DrawTextContext),
1388 .priv_class = &drawtext_class,
1391 .query_formats = query_formats,
1392 .inputs = avfilter_vf_drawtext_inputs,
1393 .outputs = avfilter_vf_drawtext_outputs,
1394 .process_command = command,
1395 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,