Browse code

Port drawtext filter by Hemanth from the libavfilter soc repo, with the following additions: * support to anti-aliased glyph rendering * support to UTF-8 text and Unicode chars rendering * support for RGB packed formats

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Stefano Sabatini authored on 2011/02/21 09:02:29
Showing 5 changed files
... ...
@@ -169,6 +169,7 @@ External library support:
169 169
                            and libraw1394 [no]
170 170
   --enable-libdirac        enable Dirac support via libdirac [no]
171 171
   --enable-libfaac         enable FAAC support via libfaac [no]
172
+  --enable-libfreetype     enable libfreetype [no]
172 173
   --enable-libgsm          enable GSM support via libgsm [no]
173 174
   --enable-libmp3lame      enable MP3 encoding via libmp3lame [no]
174 175
   --enable-libnut          enable NUT (de)muxing via libnut,
... ...
@@ -897,6 +898,7 @@ CONFIG_LIST="
897 897
     libdc1394
898 898
     libdirac
899 899
     libfaac
900
+    libfreetype
900 901
     libgsm
901 902
     libmp3lame
902 903
     libnut
... ...
@@ -1043,6 +1045,7 @@ HAVE_LIST="
1043 1043
     llrintf
1044 1044
     local_aligned_16
1045 1045
     local_aligned_8
1046
+    localtime_r
1046 1047
     log2
1047 1048
     log2f
1048 1049
     loongson
... ...
@@ -1425,6 +1428,7 @@ udp_protocol_deps="network"
1425 1425
 # filters
1426 1426
 blackframe_filter_deps="gpl"
1427 1427
 cropdetect_filter_deps="gpl"
1428
+drawtext_filter_deps="libfreetype"
1428 1429
 frei0r_filter_deps="frei0r dlopen strtok_r"
1429 1430
 frei0r_src_filter_deps="frei0r dlopen strtok_r"
1430 1431
 hqdn3d_filter_deps="gpl"
... ...
@@ -2740,6 +2744,7 @@ check_func  getrusage
2740 2740
 check_struct "sys/time.h sys/resource.h" "struct rusage" ru_maxrss
2741 2741
 check_func  inet_aton $network_extralibs
2742 2742
 check_func  isatty
2743
+check_func  localtime_r
2743 2744
 check_func  ${malloc_prefix}memalign            && enable memalign
2744 2745
 check_func  mkstemp
2745 2746
 check_func  mmap
... ...
@@ -2826,6 +2831,7 @@ enabled libdirac   && add_cflags $(pkg-config --cflags dirac) &&
2826 2826
                       require  libdirac libdirac_decoder/dirac_parser.h dirac_decoder_init $(pkg-config --libs dirac) &&
2827 2827
                       require  libdirac libdirac_encoder/dirac_encoder.h dirac_encoder_init $(pkg-config --libs dirac)
2828 2828
 enabled libfaac    && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac
2829
+enabled libfreetype && add_cflags $(pkg-config --cflags freetype2) && require libfreetype ft2build.h FT_Init_FreeType -lfreetype
2829 2830
 enabled libgsm     && require  libgsm gsm/gsm.h gsm_create -lgsm
2830 2831
 enabled libmp3lame && require  "libmp3lame >= 3.98.3" lame/lame.h lame_set_VBR_quality -lmp3lame
2831 2832
 enabled libnut     && require  libnut libnut.h nut_demuxer_init -lnut
... ...
@@ -386,6 +386,118 @@ fade=in:0:25, fade=out:975:25
386 386
 fade=in:5:20
387 387
 @end example
388 388
 
389
+@section drawtext
390
+
391
+Draw text string or text from specified file on top of video using the
392
+libfreetype library.
393
+
394
+To enable compilation of this filter you need to configure FFmpeg with
395
+@code{--enable-libfreetype}.
396
+
397
+The filter also recognizes strftime() sequences in the provided text
398
+and expands them accordingly. Check the documentation of strftime().
399
+
400
+The filter accepts parameters as a list of @var{key}=@var{value} pairs,
401
+separated by ":".
402
+
403
+The description of the accepted parameters follows.
404
+
405
+@table @option
406
+
407
+@item fontfile
408
+The font file to be used for drawing text. Path must be included.
409
+This parameter is mandatory.
410
+
411
+@item text
412
+The text string to be drawn. The text must be a sequence of UTF-8
413
+encoded characters.
414
+This parameter is mandatory if no file is specified.
415
+
416
+@item textfile
417
+A text file containing text to be drawn. The text must be a sequence
418
+of UTF-8 encoded characters
419
+
420
+This parameter is mandatory if no text string is specified.
421
+
422
+If both text and textfile are specified, an error is thrown.
423
+
424
+@item x, y
425
+The offsets where text will be drawn within the video frame.
426
+Relative to the top/left border of the output image.
427
+
428
+The default value of @var{x} and @var{y} is 0.
429
+
430
+@item fontsize
431
+The font size to be used for drawing text.
432
+The default value of @var{size} is 16.
433
+
434
+@item fontcolor
435
+The color to be used for drawing fonts.
436
+Either a string (e.g. "red") or in 0xRRGGBB[AA] format
437
+(e.g. "0xff000033"), possibly followed by an alpha specifier.
438
+The default value of @var{fontcolor} is "black".
439
+
440
+@item boxcolor
441
+The color to be used for drawing box around text.
442
+Either a string (e.g. "yellow") or in 0xRRGGBB[AA] format
443
+(e.g. "0xff00ff"), possibly followed by an alpha specifier.
444
+
445
+The default value of @var{bgcolor} is "white".
446
+
447
+@item box
448
+Used to draw a box around text using background color.
449
+Value should be either 1 (enable) or 0 (disable).
450
+The default value of @var{box} is 0.
451
+
452
+@item ft_load_flags
453
+Flags to be used for loading the fonts.
454
+
455
+The flags map the corresponding flags supported by libfreetype, and are
456
+a combination of the following values:
457
+@table @var
458
+@item default
459
+@item no_scale
460
+@item no_hinting
461
+@item render
462
+@item no_bitmap
463
+@item vertical_layout
464
+@item force_autohint
465
+@item crop_bitmap
466
+@item pedantic
467
+@item ignore_global_advance_width
468
+@item no_recurse
469
+@item ignore_transform
470
+@item monochrome
471
+@item linear_design
472
+@item no_autohint
473
+@item end table
474
+@end table
475
+
476
+Default value is "render".
477
+
478
+For more information consult the documentation for the FT_LOAD_*
479
+libfreetype flags.
480
+
481
+@item tabsize
482
+The size in number of spaces to use for rendering the tab.
483
+Default value is 4.
484
+@end table
485
+
486
+For example the command:
487
+@example
488
+drawtext=fontfile=FreeSerif.ttf: text='Test Text': x=100: y=50: fontsize=24: fontcolor=yellow@@0.2: boxcolor=red@@0.2: box=1"
489
+@end example
490
+
491
+will draw 'Test Text' with font FreeSerif of size 24 at position
492
+(100,50), text color is yellow, and draw a red box around text. Both
493
+the text and the box have an opacity of 20%.
494
+
495
+Note that the double quotes are not necessary if spaces are not used
496
+within the parameter list.
497
+
498
+For more information about libfreetype, check:
499
+@url{http://www.freetype.org/}
500
+
389 501
 @section fifo
390 502
 
391 503
 Buffer input images and send them when they are requested.
... ...
@@ -27,6 +27,7 @@ OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
27 27
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
28 28
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
29 29
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
30
+OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
30 31
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
31 32
 OBJS-$(CONFIG_FIFO_FILTER)                   += vf_fifo.o
32 33
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
... ...
@@ -45,6 +45,7 @@ void avfilter_register_all(void)
45 45
     REGISTER_FILTER (CROP,        crop,        vf);
46 46
     REGISTER_FILTER (CROPDETECT,  cropdetect,  vf);
47 47
     REGISTER_FILTER (DRAWBOX,     drawbox,     vf);
48
+    REGISTER_FILTER (DRAWTEXT,    drawtext,    vf);
48 49
     REGISTER_FILTER (FADE,        fade,        vf);
49 50
     REGISTER_FILTER (FIFO,        fifo,        vf);
50 51
     REGISTER_FILTER (FORMAT,      format,      vf);
51 52
new file mode 100644
... ...
@@ -0,0 +1,650 @@
0
+/*
1
+ * Copyright (c) 2011 Stefano Sabatini
2
+ * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
3
+ * Copyright (c) 2003 Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file
24
+ * drawtext filter, based on the original FFmpeg vhook/drawtext.c
25
+ * filter by Gustavo Sverzut Barbieri
26
+ */
27
+
28
+#include <sys/time.h>
29
+#include <time.h>
30
+
31
+#include "libavutil/colorspace.h"
32
+#include "libavutil/file.h"
33
+#include "libavutil/opt.h"
34
+#include "libavutil/parseutils.h"
35
+#include "libavutil/pixdesc.h"
36
+#include "libavutil/tree.h"
37
+#include "avfilter.h"
38
+#include "drawutils.h"
39
+
40
+#undef time
41
+
42
+#include <ft2build.h>
43
+#include <freetype/config/ftheader.h>
44
+#include FT_FREETYPE_H
45
+#include FT_GLYPH_H
46
+
47
+#define MAX_EXPANDED_TEXT_SIZE 2048
48
+
49
+typedef struct {
50
+    const AVClass *class;
51
+    char *fontfile;                 ///< font to be used
52
+    char *text;                     ///< text to be drawn
53
+    int ft_load_flags;              ///< flags used for loading fonts, see FT_LOAD_*
54
+    /** buffer containing the text expanded by strftime */
55
+    char expanded_text[MAX_EXPANDED_TEXT_SIZE];
56
+    /** positions for each element in the text */
57
+    FT_Vector positions[MAX_EXPANDED_TEXT_SIZE];
58
+    char *textfile;                 ///< file with text to be drawn
59
+    unsigned int x;                 ///< x position to start drawing text
60
+    unsigned int y;                 ///< y position to start drawing text
61
+    unsigned int fontsize;          ///< font size to use
62
+    char *fontcolor_string;         ///< font color as string
63
+    char *boxcolor_string;          ///< box color as string
64
+    uint8_t fontcolor[4];           ///< foreground color
65
+    uint8_t boxcolor[4];            ///< background color
66
+    uint8_t fontcolor_rgba[4];      ///< foreground color in RGBA
67
+    uint8_t boxcolor_rgba[4];       ///< background color in RGBA
68
+
69
+    short int draw_box;             ///< draw box around text - true or false
70
+    int use_kerning;                ///< font kerning is used - true/false
71
+    int tabsize;                    ///< tab size
72
+
73
+    FT_Library library;             ///< freetype font library handle
74
+    FT_Face face;                   ///< freetype font face handle
75
+    struct AVTreeNode *glyphs;      ///< rendered glyphs, stored using the UTF-32 char code
76
+    int hsub, vsub;                 ///< chroma subsampling values
77
+    int is_packed_rgb;
78
+    int pixel_step[4];              ///< distance in bytes between the component of each pixel
79
+    uint8_t rgba_map[4];            ///< map RGBA offsets to the positions in the packed RGBA format
80
+    uint8_t *box_line[4];           ///< line used for filling the box background
81
+} DrawTextContext;
82
+
83
+#define OFFSET(x) offsetof(DrawTextContext, x)
84
+
85
+static const AVOption drawtext_options[]= {
86
+{"fontfile", "set font file",        OFFSET(fontfile),         FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
87
+{"text",     "set text",             OFFSET(text),             FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
88
+{"textfile", "set text file",        OFFSET(textfile),         FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
89
+{"fontcolor","set foreground color", OFFSET(fontcolor_string), FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
90
+{"boxcolor", "set box color",        OFFSET(boxcolor_string),  FF_OPT_TYPE_STRING, 0,  CHAR_MIN, CHAR_MAX },
91
+{"box",      "set box",              OFFSET(draw_box),         FF_OPT_TYPE_INT,    0,         0,        1 },
92
+{"fontsize", "set font size",        OFFSET(fontsize),         FF_OPT_TYPE_INT,   16,         1,       72 },
93
+{"x",        "set x",                OFFSET(x),                FF_OPT_TYPE_INT,    0,         0,  INT_MAX },
94
+{"y",        "set y",                OFFSET(y),                FF_OPT_TYPE_INT,    0,         0,  INT_MAX },
95
+{"tabsize",  "set tab size",         OFFSET(tabsize),          FF_OPT_TYPE_INT,    4,         0,  INT_MAX },
96
+
97
+/* FT_LOAD_* flags */
98
+{"ft_load_flags", "set font loading flags for libfreetype",   OFFSET(ft_load_flags),  FF_OPT_TYPE_FLAGS,  FT_LOAD_DEFAULT|FT_LOAD_RENDER, 0, INT_MAX, 0, "ft_load_flags" },
99
+{"default",                     "set default",                     0, FF_OPT_TYPE_CONST, FT_LOAD_DEFAULT,                     INT_MIN, INT_MAX, 0, "ft_load_flags" },
100
+{"no_scale",                    "set no_scale",                    0, FF_OPT_TYPE_CONST, FT_LOAD_NO_SCALE,                    INT_MIN, INT_MAX, 0, "ft_load_flags" },
101
+{"no_hinting",                  "set no_hinting",                  0, FF_OPT_TYPE_CONST, FT_LOAD_NO_HINTING,                  INT_MIN, INT_MAX, 0, "ft_load_flags" },
102
+{"render",                      "set render",                      0, FF_OPT_TYPE_CONST, FT_LOAD_RENDER,                      INT_MIN, INT_MAX, 0, "ft_load_flags" },
103
+{"no_bitmap",                   "set no_bitmap",                   0, FF_OPT_TYPE_CONST, FT_LOAD_NO_BITMAP,                   INT_MIN, INT_MAX, 0, "ft_load_flags" },
104
+{"vertical_layout",             "set vertical_layout",             0, FF_OPT_TYPE_CONST, FT_LOAD_VERTICAL_LAYOUT,             INT_MIN, INT_MAX, 0, "ft_load_flags" },
105
+{"force_autohint",              "set force_autohint",              0, FF_OPT_TYPE_CONST, FT_LOAD_FORCE_AUTOHINT,              INT_MIN, INT_MAX, 0, "ft_load_flags" },
106
+{"crop_bitmap",                 "set crop_bitmap",                 0, FF_OPT_TYPE_CONST, FT_LOAD_CROP_BITMAP,                 INT_MIN, INT_MAX, 0, "ft_load_flags" },
107
+{"pedantic",                    "set pedantic",                    0, FF_OPT_TYPE_CONST, FT_LOAD_PEDANTIC,                    INT_MIN, INT_MAX, 0, "ft_load_flags" },
108
+{"ignore_global_advance_width", "set ignore_global_advance_width", 0, FF_OPT_TYPE_CONST, FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH, INT_MIN, INT_MAX, 0, "ft_load_flags" },
109
+{"no_recurse",                  "set no_recurse",                  0, FF_OPT_TYPE_CONST, FT_LOAD_NO_RECURSE,                  INT_MIN, INT_MAX, 0, "ft_load_flags" },
110
+{"ignore_transform",            "set ignore_transform",            0, FF_OPT_TYPE_CONST, FT_LOAD_IGNORE_TRANSFORM,            INT_MIN, INT_MAX, 0, "ft_load_flags" },
111
+{"monochrome",                  "set monochrome",                  0, FF_OPT_TYPE_CONST, FT_LOAD_MONOCHROME,                  INT_MIN, INT_MAX, 0, "ft_load_flags" },
112
+{"linear_design",               "set linear_design",               0, FF_OPT_TYPE_CONST, FT_LOAD_LINEAR_DESIGN,               INT_MIN, INT_MAX, 0, "ft_load_flags" },
113
+{"no_autohint",                 "set no_autohint",                 0, FF_OPT_TYPE_CONST, FT_LOAD_NO_AUTOHINT,                 INT_MIN, INT_MAX, 0, "ft_load_flags" },
114
+{NULL},
115
+};
116
+
117
+static const char *drawtext_get_name(void *ctx)
118
+{
119
+    return "drawtext";
120
+}
121
+
122
+static const AVClass drawtext_class = {
123
+    "DrawTextContext",
124
+    drawtext_get_name,
125
+    drawtext_options
126
+};
127
+
128
+#undef __FTERRORS_H__
129
+#define FT_ERROR_START_LIST {
130
+#define FT_ERRORDEF(e, v, s) { (e), (s) },
131
+#define FT_ERROR_END_LIST { 0, NULL } };
132
+
133
+struct ft_error
134
+{
135
+    int err;
136
+    const char *err_msg;
137
+} static ft_errors[] =
138
+#include FT_ERRORS_H
139
+
140
+#define FT_ERRMSG(e) ft_errors[e].err_msg
141
+
142
+typedef struct {
143
+    FT_Glyph *glyph;
144
+    uint32_t code;
145
+    FT_Bitmap bitmap; ///< array holding bitmaps of font
146
+    FT_BBox bbox;
147
+    int advance;
148
+    int bitmap_left;
149
+    int bitmap_top;
150
+} Glyph;
151
+
152
+static int glyph_cmp(const Glyph *a, const Glyph *b)
153
+{
154
+    int64_t diff = (int64_t)a->code - (int64_t)b->code;
155
+    return diff > 0 ? 1 : diff < 0 ? -1 : 0;
156
+}
157
+
158
+/**
159
+ * Load glyphs corresponding to the UTF-32 codepoint code.
160
+ */
161
+static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
162
+{
163
+    DrawTextContext *dtext = ctx->priv;
164
+    Glyph *glyph = av_mallocz(sizeof(Glyph));
165
+    struct AVTreeNode *node = NULL;
166
+    int ret;
167
+
168
+    /* load glyph into dtext->face->glyph */
169
+    ret = FT_Load_Char(dtext->face, code, dtext->ft_load_flags);
170
+    if (ret)
171
+        return AVERROR(EINVAL);
172
+
173
+    /* save glyph */
174
+    glyph->code  = code;
175
+    glyph->glyph = av_mallocz(sizeof(FT_Glyph));
176
+    ret = FT_Get_Glyph(dtext->face->glyph, glyph->glyph);
177
+    if (ret)
178
+        return AVERROR(EINVAL);
179
+
180
+    glyph->bitmap      = dtext->face->glyph->bitmap;
181
+    glyph->bitmap_left = dtext->face->glyph->bitmap_left;
182
+    glyph->bitmap_top  = dtext->face->glyph->bitmap_top;
183
+    glyph->advance     = dtext->face->glyph->advance.x >> 6;
184
+
185
+    /* measure text height to calculate text_height (or the maximum text height) */
186
+    FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
187
+
188
+    /* cache the newly created glyph */
189
+    if (!node)
190
+        node = av_mallocz(av_tree_node_size);
191
+    av_tree_insert(&dtext->glyphs, glyph, (void *)glyph_cmp, &node);
192
+
193
+    if (glyph_ptr)
194
+        *glyph_ptr = glyph;
195
+    return 0;
196
+}
197
+
198
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
199
+{
200
+    int err;
201
+    DrawTextContext *dtext = ctx->priv;
202
+
203
+    dtext->class = &drawtext_class;
204
+    av_opt_set_defaults2(dtext, 0, 0);
205
+    dtext->fontcolor_string = av_strdup("black");
206
+    dtext->boxcolor_string = av_strdup("white");
207
+
208
+    if ((err = (av_set_options_string(dtext, args, "=", ":"))) < 0) {
209
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
210
+        return err;
211
+    }
212
+
213
+    if (!dtext->fontfile) {
214
+        av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
215
+        return AVERROR(EINVAL);
216
+    }
217
+
218
+    if (dtext->textfile) {
219
+        uint8_t *textbuf;
220
+        size_t textbuf_size;
221
+
222
+        if (dtext->text) {
223
+            av_log(ctx, AV_LOG_ERROR,
224
+                   "Both text and text file provided. Please provide only one\n");
225
+            return AVERROR(EINVAL);
226
+        }
227
+        if ((err = av_file_map(dtext->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
228
+            av_log(ctx, AV_LOG_ERROR,
229
+                   "The text file '%s' could not be read or is empty\n",
230
+                   dtext->textfile);
231
+            return err;
232
+        }
233
+
234
+        if (!(dtext->text = av_malloc(textbuf_size+1)))
235
+            return AVERROR(ENOMEM);
236
+        memcpy(dtext->text, textbuf, textbuf_size);
237
+        dtext->text[textbuf_size] = 0;
238
+        av_file_unmap(textbuf, textbuf_size);
239
+    }
240
+
241
+    if (!dtext->text) {
242
+        av_log(ctx, AV_LOG_ERROR,
243
+               "Either text or a valid file must be provided\n");
244
+        return AVERROR(EINVAL);
245
+    }
246
+
247
+    if ((err = av_parse_color(dtext->fontcolor_rgba, dtext->fontcolor_string, -1, ctx))) {
248
+        av_log(ctx, AV_LOG_ERROR,
249
+               "Invalid font color '%s'\n", dtext->fontcolor_string);
250
+        return err;
251
+    }
252
+
253
+    if ((err = av_parse_color(dtext->boxcolor_rgba, dtext->boxcolor_string, -1, ctx))) {
254
+        av_log(ctx, AV_LOG_ERROR,
255
+               "Invalid box color '%s'\n", dtext->boxcolor_string);
256
+        return err;
257
+    }
258
+
259
+    if ((err = FT_Init_FreeType(&(dtext->library)))) {
260
+        av_log(ctx, AV_LOG_ERROR,
261
+               "Could not load FreeType: %s\n", FT_ERRMSG(err));
262
+        return AVERROR(EINVAL);
263
+    }
264
+
265
+    /* load the face, and set up the encoding, which is by default UTF-8 */
266
+    if ((err = FT_New_Face(dtext->library, dtext->fontfile, 0, &dtext->face))) {
267
+        av_log(ctx, AV_LOG_ERROR, "Could not load fontface from file '%s': %s\n",
268
+               dtext->fontfile, FT_ERRMSG(err));
269
+        return AVERROR(EINVAL);
270
+    }
271
+    if ((err = FT_Set_Pixel_Sizes(dtext->face, 0, dtext->fontsize))) {
272
+        av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
273
+               dtext->fontsize, FT_ERRMSG(err));
274
+        return AVERROR(EINVAL);
275
+    }
276
+
277
+    dtext->use_kerning = FT_HAS_KERNING(dtext->face);
278
+
279
+    /* load the fallback glyph with code 0 */
280
+    load_glyph(ctx, NULL, 0);
281
+
282
+#if !HAVE_LOCALTIME_R
283
+    av_log(ctx, AV_LOG_WARNING, "strftime() expansion unavailable!\n");
284
+#else
285
+    if (strlen(dtext->text) >= MAX_EXPANDED_TEXT_SIZE) {
286
+        av_log(ctx, AV_LOG_ERROR,
287
+               "Impossible to print text, string is too big\n");
288
+        return AVERROR(EINVAL);
289
+    }
290
+#endif
291
+
292
+    return 0;
293
+}
294
+
295
+static int query_formats(AVFilterContext *ctx)
296
+{
297
+    static const enum PixelFormat pix_fmts[] = {
298
+        PIX_FMT_ARGB,    PIX_FMT_RGBA,
299
+        PIX_FMT_ABGR,    PIX_FMT_BGRA,
300
+        PIX_FMT_RGB24,   PIX_FMT_BGR24,
301
+        PIX_FMT_YUV420P, PIX_FMT_YUV444P,
302
+        PIX_FMT_YUV422P, PIX_FMT_YUV411P,
303
+        PIX_FMT_YUV410P, PIX_FMT_YUV440P,
304
+        PIX_FMT_NONE
305
+    };
306
+
307
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
308
+    return 0;
309
+}
310
+
311
+static int glyph_enu_free(void *opaque, void *elem)
312
+{
313
+    av_free(elem);
314
+    return 0;
315
+}
316
+
317
+static av_cold void uninit(AVFilterContext *ctx)
318
+{
319
+    DrawTextContext *dtext = ctx->priv;
320
+    int i;
321
+
322
+    av_freep(&dtext->fontfile);
323
+    av_freep(&dtext->text);
324
+    av_freep(&dtext->fontcolor_string);
325
+    av_freep(&dtext->boxcolor_string);
326
+    av_tree_enumerate(dtext->glyphs, NULL, NULL, glyph_enu_free);
327
+    av_tree_destroy(dtext->glyphs);
328
+    dtext->glyphs = 0;
329
+    FT_Done_Face(dtext->face);
330
+    FT_Done_FreeType(dtext->library);
331
+
332
+    for (i = 0; i < 4; i++) {
333
+        av_freep(&dtext->box_line[i]);
334
+        dtext->pixel_step[i] = 0;
335
+    }
336
+
337
+}
338
+
339
+static int config_input(AVFilterLink *inlink)
340
+{
341
+    DrawTextContext *dtext = inlink->dst->priv;
342
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
343
+    int ret;
344
+
345
+    dtext->hsub = pix_desc->log2_chroma_w;
346
+    dtext->vsub = pix_desc->log2_chroma_h;
347
+
348
+    if ((ret =
349
+         ff_fill_line_with_color(dtext->box_line, dtext->pixel_step,
350
+                                 inlink->w, dtext->boxcolor,
351
+                                 inlink->format, dtext->boxcolor_rgba,
352
+                                 &dtext->is_packed_rgb, dtext->rgba_map)) < 0)
353
+        return ret;
354
+
355
+    if (!dtext->is_packed_rgb) {
356
+        uint8_t *rgba = dtext->fontcolor_rgba;
357
+        dtext->fontcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
358
+        dtext->fontcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
359
+        dtext->fontcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
360
+        dtext->fontcolor[3] = rgba[3];
361
+    }
362
+
363
+    return 0;
364
+}
365
+
366
+#define GET_BITMAP_VAL(r, c)                                            \
367
+    bitmap->pixel_mode == FT_PIXEL_MODE_MONO ?                          \
368
+        (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
369
+         bitmap->buffer[(r) * bitmap->pitch +  (c)]
370
+
371
+#define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub) {           \
372
+    luma_pos    = ((x)          ) + ((y)          ) * picref->linesize[0]; \
373
+    chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[1]; \
374
+    chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[2]; \
375
+    alpha = (yuva_color[3] * (val)) / 255;                               \
376
+    picref->data[0][luma_pos] = (alpha * yuva_color[0] + (255 - alpha) * picref->data[0][luma_pos]) >> 8; \
377
+    alpha = (yuva_color[3] * (val)) / 224;                               \
378
+    picref->data[1][chroma_pos1] = 16 + (alpha * (yuva_color[1]-16) + (224 - alpha) * (picref->data[1][chroma_pos1]-16)) / 224; \
379
+    picref->data[2][chroma_pos2] = 16 + (alpha * (yuva_color[2]-16) + (224 - alpha) * (picref->data[2][chroma_pos2]-16)) / 224; \
380
+}
381
+
382
+static inline int draw_glyph_yuv(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x,
383
+                                 unsigned int y, unsigned int width, unsigned int height,
384
+                                 unsigned char yuva_color[4], int hsub, int vsub)
385
+{
386
+    int r, c, alpha;
387
+    unsigned int luma_pos, chroma_pos1, chroma_pos2;
388
+    uint8_t src_val, dst_pixel[4];
389
+
390
+    for (r = 0; r < bitmap->rows && r+y < height; r++) {
391
+        for (c = 0; c < bitmap->width && c+x < width; c++) {
392
+            /* get pixel in the picref (destination) */
393
+            dst_pixel[0] = picref->data[0][  c+x           +  (y+r)          * picref->linesize[0]];
394
+            dst_pixel[1] = picref->data[1][((c+x) >> hsub) + ((y+r) >> vsub) * picref->linesize[1]];
395
+            dst_pixel[2] = picref->data[2][((c+x) >> hsub) + ((y+r) >> vsub) * picref->linesize[2]];
396
+
397
+            /* get intensity value in the glyph bitmap (source) */
398
+            src_val = GET_BITMAP_VAL(r, c);
399
+            if (!src_val)
400
+                continue;
401
+
402
+            SET_PIXEL_YUV(picref, yuva_color, src_val, c+x, y+r, hsub, vsub);
403
+        }
404
+    }
405
+
406
+    return 0;
407
+}
408
+
409
+#define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
410
+    p   = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
411
+    alpha = (rgba_color[3] * (val)) / 255;                              \
412
+    *(p+r_off) = (alpha * rgba_color[0] + (255 - alpha) * *(p+r_off)) >> 8; \
413
+    *(p+g_off) = (alpha * rgba_color[1] + (255 - alpha) * *(p+g_off)) >> 8; \
414
+    *(p+b_off) = (alpha * rgba_color[2] + (255 - alpha) * *(p+b_off)) >> 8; \
415
+}
416
+
417
+static inline int draw_glyph_rgb(AVFilterBufferRef *picref, FT_Bitmap *bitmap,
418
+                                 unsigned int x, unsigned int y,
419
+                                 unsigned int width, unsigned int height, int pixel_step,
420
+                                 unsigned char rgba_color[4], uint8_t rgba_map[4])
421
+{
422
+    int r, c, alpha;
423
+    uint8_t *p;
424
+    uint8_t src_val, dst_pixel[4];
425
+
426
+    for (r = 0; r < bitmap->rows && r+y < height; r++) {
427
+        for (c = 0; c < bitmap->width && c+x < width; c++) {
428
+            /* get pixel in the picref (destination) */
429
+            dst_pixel[0] = picref->data[0][(c+x + rgba_map[0]) * pixel_step +
430
+                                           (y+r) * picref->linesize[0]];
431
+            dst_pixel[1] = picref->data[0][(c+x + rgba_map[1]) * pixel_step +
432
+                                           (y+r) * picref->linesize[0]];
433
+            dst_pixel[2] = picref->data[0][(c+x + rgba_map[2]) * pixel_step +
434
+                                           (y+r) * picref->linesize[0]];
435
+
436
+            /* get intensity value in the glyph bitmap (source) */
437
+            src_val = GET_BITMAP_VAL(r, c);
438
+            if (!src_val)
439
+                continue;
440
+
441
+            SET_PIXEL_RGB(picref, rgba_color, src_val, c+x, y+r, pixel_step,
442
+                          rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
443
+        }
444
+    }
445
+
446
+    return 0;
447
+}
448
+
449
+static inline void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned int y,
450
+                           unsigned int width, unsigned int height,
451
+                           uint8_t *line[4], int pixel_step[4], uint8_t color[4],
452
+                           int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
453
+{
454
+    int i, j, alpha;
455
+
456
+    if (color[3] != 0xFF) {
457
+        if (is_rgba_packed) {
458
+            uint8_t *p;
459
+            for (j = 0; j < height; j++)
460
+                for (i = 0; i < width; i++)
461
+                    SET_PIXEL_RGB(picref, color, 255, i+x, y+j, pixel_step[0],
462
+                                  rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
463
+        } else {
464
+            unsigned int luma_pos, chroma_pos1, chroma_pos2;
465
+            for (j = 0; j < height; j++)
466
+                for (i = 0; i < width; i++)
467
+                    SET_PIXEL_YUV(picref, color, 255, i+x, y+j, hsub, vsub);
468
+        }
469
+    } else {
470
+        ff_draw_rectangle(picref->data, picref->linesize,
471
+                          line, pixel_step, hsub, vsub,
472
+                          x, y, width, height);
473
+    }
474
+}
475
+
476
+static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
477
+                     int width, int height)
478
+{
479
+    DrawTextContext *dtext = ctx->priv;
480
+    char *text = dtext->text;
481
+    uint32_t code = 0, prev_code = 0;
482
+    int x = 0, y = 0, i = 0;
483
+    int text_height, baseline;
484
+    uint8_t *p;
485
+    int str_w, str_w_max;
486
+    int y_min = 32000, y_max = -32000;
487
+    FT_Vector delta;
488
+    Glyph *glyph = NULL, *prev_glyph = NULL;
489
+    Glyph dummy = { 0 };
490
+
491
+#if HAVE_LOCALTIME_R
492
+    time_t now = time(0);
493
+    struct tm ltime;
494
+    size_t expanded_text_len;
495
+
496
+    dtext->expanded_text[0] = '\1';
497
+    expanded_text_len = strftime(dtext->expanded_text, MAX_EXPANDED_TEXT_SIZE,
498
+                                 text, localtime_r(&now, &ltime));
499
+    text = dtext->expanded_text;
500
+    if (expanded_text_len == 0 && dtext->expanded_text[0] != '\0') {
501
+        av_log(ctx, AV_LOG_ERROR,
502
+               "Impossible to print text, string is too big\n");
503
+        return AVERROR(EINVAL);
504
+    }
505
+#endif
506
+
507
+    str_w = str_w_max = 0;
508
+    x = dtext->x;
509
+    y = dtext->y;
510
+
511
+    /* load and cache glyphs */
512
+    for (i = 0, p = text; *p; i++) {
513
+        GET_UTF8(code, *p++, continue;);
514
+
515
+        /* get glyph */
516
+        dummy.code = code;
517
+        glyph = av_tree_find(dtext->glyphs, &dummy, (void *)glyph_cmp, NULL);
518
+        if (!glyph)
519
+            load_glyph(ctx, &glyph, code);
520
+
521
+        y_min = FFMIN(glyph->bbox.yMin, y_min);
522
+        y_max = FFMAX(glyph->bbox.yMax, y_max);
523
+    }
524
+    text_height = y_max - y_min;
525
+    baseline    = y_max;
526
+
527
+    /* compute and save position for each glyph */
528
+    glyph = NULL;
529
+    for (i = 0, p = text; *p; i++) {
530
+        GET_UTF8(code, *p++, continue;);
531
+
532
+        /* skip the \n in the sequence \r\n */
533
+        if (prev_code == '\r' && code == '\n')
534
+            continue;
535
+
536
+        /* get glyph */
537
+        prev_glyph = glyph;
538
+        dummy.code = code;
539
+        glyph = av_tree_find(dtext->glyphs, &dummy, (void *)glyph_cmp, NULL);
540
+
541
+        /* kerning */
542
+        if (dtext->use_kerning && prev_glyph && glyph->code) {
543
+            FT_Get_Kerning(dtext->face, prev_glyph->code, glyph->code,
544
+                           ft_kerning_default, &delta);
545
+            x += delta.x >> 6;
546
+        }
547
+
548
+        if (x + glyph->advance >= width || code == '\r' || code == '\n') {
549
+            if (x + glyph->advance >= width)
550
+                str_w_max = width - dtext->x - 1;
551
+            y += text_height;
552
+            x = dtext->x;
553
+        }
554
+
555
+        /* save position */
556
+        dtext->positions[i].x = x + glyph->bitmap_left;
557
+        dtext->positions[i].y = y - glyph->bitmap_top + baseline;
558
+        if (code != '\n' && code != '\r') {
559
+            int advance = glyph->advance;
560
+            if (code == '\t')
561
+                advance *= dtext->tabsize;
562
+            x     += advance;
563
+            str_w += advance;
564
+        }
565
+        prev_code = code;
566
+    }
567
+
568
+    y += text_height;
569
+    if (str_w_max == 0)
570
+        str_w_max = str_w;
571
+
572
+    /* draw box */
573
+    if (dtext->draw_box) {
574
+        /* check if it doesn't pass the limits */
575
+        str_w_max = FFMIN(str_w_max, width - dtext->x - 1);
576
+        y = FFMIN(y, height - 1);
577
+
578
+        /* draw background */
579
+        drawbox(picref, dtext->x, dtext->y, str_w_max, y-dtext->y,
580
+                dtext->box_line, dtext->pixel_step, dtext->boxcolor,
581
+                dtext->hsub, dtext->vsub, dtext->is_packed_rgb, dtext->rgba_map);
582
+    }
583
+
584
+    /* draw glyphs */
585
+    for (i = 0, p = text; *p; i++) {
586
+        Glyph dummy = { 0 };
587
+        GET_UTF8(code, *p++, continue;);
588
+
589
+        /* skip new line chars, just go to new line */
590
+        if (code == '\n' || code == '\r' || code == '\t')
591
+            continue;
592
+
593
+        dummy.code = code;
594
+        glyph = av_tree_find(dtext->glyphs, &dummy, (void *)glyph_cmp, NULL);
595
+
596
+        if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
597
+            glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
598
+            return AVERROR(EINVAL);
599
+
600
+        if (dtext->is_packed_rgb) {
601
+            draw_glyph_rgb(picref, &glyph->bitmap,
602
+                           dtext->positions[i].x, dtext->positions[i].y, width, height,
603
+                           dtext->pixel_step[0], dtext->fontcolor_rgba, dtext->rgba_map);
604
+        } else {
605
+            draw_glyph_yuv(picref, &glyph->bitmap,
606
+                           dtext->positions[i].x, dtext->positions[i].y, width, height,
607
+                           dtext->fontcolor, dtext->hsub, dtext->vsub);
608
+        }
609
+    }
610
+
611
+    return 0;
612
+}
613
+
614
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
615
+
616
+static void end_frame(AVFilterLink *inlink)
617
+{
618
+    AVFilterLink *outlink = inlink->dst->outputs[0];
619
+    AVFilterBufferRef *picref = inlink->cur_buf;
620
+
621
+    draw_text(inlink->dst, picref, picref->video->w, picref->video->h);
622
+
623
+    avfilter_draw_slice(outlink, 0, picref->video->h, 1);
624
+    avfilter_end_frame(outlink);
625
+}
626
+
627
+AVFilter avfilter_vf_drawtext = {
628
+    .name          = "drawtext",
629
+    .description   = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
630
+    .priv_size     = sizeof(DrawTextContext),
631
+    .init          = init,
632
+    .uninit        = uninit,
633
+    .query_formats = query_formats,
634
+
635
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
636
+                                    .type             = AVMEDIA_TYPE_VIDEO,
637
+                                    .get_video_buffer = avfilter_null_get_video_buffer,
638
+                                    .start_frame      = avfilter_null_start_frame,
639
+                                    .draw_slice       = null_draw_slice,
640
+                                    .end_frame        = end_frame,
641
+                                    .config_props     = config_input,
642
+                                    .min_perms        = AV_PERM_WRITE |
643
+                                                        AV_PERM_READ,
644
+                                    .rej_perms        = AV_PERM_PRESERVE },
645
+                                  { .name = NULL}},
646
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
647
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
648
+                                  { .name = NULL}},
649
+};