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.

715 lines
28KB

  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 FFmpeg vhook/drawtext.c
  25. * filter by Gustavo Sverzut Barbieri
  26. */
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include "libavutil/colorspace.h"
  30. #include "libavutil/file.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/parseutils.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/tree.h"
  35. #include "avfilter.h"
  36. #include "drawutils.h"
  37. #undef time
  38. #include <ft2build.h>
  39. #include <freetype/config/ftheader.h>
  40. #include FT_FREETYPE_H
  41. #include FT_GLYPH_H
  42. typedef struct {
  43. const AVClass *class;
  44. uint8_t *fontfile; ///< font to be used
  45. uint8_t *text; ///< text to be drawn
  46. uint8_t *text_priv; ///< used to detect whether text changed
  47. int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_*
  48. FT_Vector *positions; ///< positions for each element in the text
  49. char *textfile; ///< file with text to be drawn
  50. unsigned int x; ///< x position to start drawing text
  51. unsigned int y; ///< y position to start drawing text
  52. int shadowx, shadowy;
  53. unsigned int fontsize; ///< font size to use
  54. char *fontcolor_string; ///< font color as string
  55. char *boxcolor_string; ///< box color as string
  56. char *shadowcolor_string; ///< shadow color as string
  57. uint8_t fontcolor[4]; ///< foreground color
  58. uint8_t boxcolor[4]; ///< background color
  59. uint8_t shadowcolor[4]; ///< shadow color
  60. uint8_t fontcolor_rgba[4]; ///< foreground color in RGBA
  61. uint8_t boxcolor_rgba[4]; ///< background color in RGBA
  62. uint8_t shadowcolor_rgba[4]; ///< shadow color in RGBA
  63. short int draw_box; ///< draw box around text - true or false
  64. int use_kerning; ///< font kerning is used - true/false
  65. int tabsize; ///< tab size
  66. FT_Library library; ///< freetype font library handle
  67. FT_Face face; ///< freetype font face handle
  68. struct AVTreeNode *glyphs; ///< rendered glyphs, stored using the UTF-32 char code
  69. int hsub, vsub; ///< chroma subsampling values
  70. int is_packed_rgb;
  71. int pixel_step[4]; ///< distance in bytes between the component of each pixel
  72. uint8_t rgba_map[4]; ///< map RGBA offsets to the positions in the packed RGBA format
  73. uint8_t *box_line[4]; ///< line used for filling the box background
  74. } DrawTextContext;
  75. #define OFFSET(x) offsetof(DrawTextContext, x)
  76. static const AVOption drawtext_options[]= {
  77. {"fontfile", "set font file", OFFSET(fontfile), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  78. {"text", "set text", OFFSET(text), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  79. {"textfile", "set text file", OFFSET(textfile), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  80. {"fontcolor","set foreground color", OFFSET(fontcolor_string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  81. {"boxcolor", "set box color", OFFSET(boxcolor_string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  82. {"shadowcolor", "set shadow color", OFFSET(shadowcolor_string), FF_OPT_TYPE_STRING, 0, CHAR_MIN, CHAR_MAX },
  83. {"box", "set box", OFFSET(draw_box), FF_OPT_TYPE_INT, 0, 0, 1 },
  84. {"fontsize", "set font size", OFFSET(fontsize), FF_OPT_TYPE_INT, 16, 1, 72 },
  85. {"x", "set x", OFFSET(x), FF_OPT_TYPE_INT, 0, 0, INT_MAX },
  86. {"y", "set y", OFFSET(y), FF_OPT_TYPE_INT, 0, 0, INT_MAX },
  87. {"shadowx", "set x", OFFSET(shadowx), FF_OPT_TYPE_INT, 0, INT_MIN, INT_MAX },
  88. {"shadowy", "set y", OFFSET(shadowy), FF_OPT_TYPE_INT, 0, INT_MIN, INT_MAX },
  89. {"tabsize", "set tab size", OFFSET(tabsize), FF_OPT_TYPE_INT, 4, 0, INT_MAX },
  90. /* FT_LOAD_* flags */
  91. {"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" },
  92. {"default", "set default", 0, FF_OPT_TYPE_CONST, FT_LOAD_DEFAULT, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  93. {"no_scale", "set no_scale", 0, FF_OPT_TYPE_CONST, FT_LOAD_NO_SCALE, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  94. {"no_hinting", "set no_hinting", 0, FF_OPT_TYPE_CONST, FT_LOAD_NO_HINTING, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  95. {"render", "set render", 0, FF_OPT_TYPE_CONST, FT_LOAD_RENDER, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  96. {"no_bitmap", "set no_bitmap", 0, FF_OPT_TYPE_CONST, FT_LOAD_NO_BITMAP, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  97. {"vertical_layout", "set vertical_layout", 0, FF_OPT_TYPE_CONST, FT_LOAD_VERTICAL_LAYOUT, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  98. {"force_autohint", "set force_autohint", 0, FF_OPT_TYPE_CONST, FT_LOAD_FORCE_AUTOHINT, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  99. {"crop_bitmap", "set crop_bitmap", 0, FF_OPT_TYPE_CONST, FT_LOAD_CROP_BITMAP, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  100. {"pedantic", "set pedantic", 0, FF_OPT_TYPE_CONST, FT_LOAD_PEDANTIC, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  101. {"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" },
  102. {"no_recurse", "set no_recurse", 0, FF_OPT_TYPE_CONST, FT_LOAD_NO_RECURSE, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  103. {"ignore_transform", "set ignore_transform", 0, FF_OPT_TYPE_CONST, FT_LOAD_IGNORE_TRANSFORM, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  104. {"monochrome", "set monochrome", 0, FF_OPT_TYPE_CONST, FT_LOAD_MONOCHROME, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  105. {"linear_design", "set linear_design", 0, FF_OPT_TYPE_CONST, FT_LOAD_LINEAR_DESIGN, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  106. {"no_autohint", "set no_autohint", 0, FF_OPT_TYPE_CONST, FT_LOAD_NO_AUTOHINT, INT_MIN, INT_MAX, 0, "ft_load_flags" },
  107. {NULL},
  108. };
  109. static const char *drawtext_get_name(void *ctx)
  110. {
  111. return "drawtext";
  112. }
  113. static const AVClass drawtext_class = {
  114. "DrawTextContext",
  115. drawtext_get_name,
  116. drawtext_options
  117. };
  118. #undef __FTERRORS_H__
  119. #define FT_ERROR_START_LIST {
  120. #define FT_ERRORDEF(e, v, s) { (e), (s) },
  121. #define FT_ERROR_END_LIST { 0, NULL } };
  122. struct ft_error
  123. {
  124. int err;
  125. const char *err_msg;
  126. } static ft_errors[] =
  127. #include FT_ERRORS_H
  128. #define FT_ERRMSG(e) ft_errors[e].err_msg
  129. typedef struct {
  130. FT_Glyph *glyph;
  131. uint32_t code;
  132. FT_Bitmap bitmap; ///< array holding bitmaps of font
  133. FT_BBox bbox;
  134. int advance;
  135. int bitmap_left;
  136. int bitmap_top;
  137. } Glyph;
  138. static int glyph_cmp(void *key, const void *b)
  139. {
  140. const Glyph *a = key, *bb = b;
  141. int64_t diff = (int64_t)a->code - (int64_t)bb->code;
  142. return diff > 0 ? 1 : diff < 0 ? -1 : 0;
  143. }
  144. /**
  145. * Load glyphs corresponding to the UTF-32 codepoint code.
  146. */
  147. static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
  148. {
  149. DrawTextContext *dtext = ctx->priv;
  150. Glyph *glyph;
  151. struct AVTreeNode *node = NULL;
  152. int ret;
  153. /* load glyph into dtext->face->glyph */
  154. if (FT_Load_Char(dtext->face, code, dtext->ft_load_flags))
  155. return AVERROR(EINVAL);
  156. /* save glyph */
  157. if (!(glyph = av_mallocz(sizeof(*glyph))) ||
  158. !(glyph->glyph = av_mallocz(sizeof(*glyph->glyph)))) {
  159. ret = AVERROR(ENOMEM);
  160. goto error;
  161. }
  162. glyph->code = code;
  163. if (FT_Get_Glyph(dtext->face->glyph, glyph->glyph)) {
  164. ret = AVERROR(EINVAL);
  165. goto error;
  166. }
  167. glyph->bitmap = dtext->face->glyph->bitmap;
  168. glyph->bitmap_left = dtext->face->glyph->bitmap_left;
  169. glyph->bitmap_top = dtext->face->glyph->bitmap_top;
  170. glyph->advance = dtext->face->glyph->advance.x >> 6;
  171. /* measure text height to calculate text_height (or the maximum text height) */
  172. FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
  173. /* cache the newly created glyph */
  174. if (!(node = av_mallocz(av_tree_node_size))) {
  175. ret = AVERROR(ENOMEM);
  176. goto error;
  177. }
  178. av_tree_insert(&dtext->glyphs, glyph, glyph_cmp, &node);
  179. if (glyph_ptr)
  180. *glyph_ptr = glyph;
  181. return 0;
  182. error:
  183. if (glyph)
  184. av_freep(&glyph->glyph);
  185. av_freep(&glyph);
  186. av_freep(&node);
  187. return ret;
  188. }
  189. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  190. {
  191. int err;
  192. DrawTextContext *dtext = ctx->priv;
  193. Glyph *glyph;
  194. dtext->class = &drawtext_class;
  195. av_opt_set_defaults2(dtext, 0, 0);
  196. dtext->fontcolor_string = av_strdup("black");
  197. dtext->boxcolor_string = av_strdup("white");
  198. dtext->shadowcolor_string = av_strdup("black");
  199. if ((err = (av_set_options_string(dtext, args, "=", ":"))) < 0) {
  200. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  201. return err;
  202. }
  203. if (!dtext->fontfile) {
  204. av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
  205. return AVERROR(EINVAL);
  206. }
  207. if (dtext->textfile) {
  208. uint8_t *textbuf;
  209. size_t textbuf_size;
  210. if (dtext->text) {
  211. av_log(ctx, AV_LOG_ERROR,
  212. "Both text and text file provided. Please provide only one\n");
  213. return AVERROR(EINVAL);
  214. }
  215. if ((err = av_file_map(dtext->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) {
  216. av_log(ctx, AV_LOG_ERROR,
  217. "The text file '%s' could not be read or is empty\n",
  218. dtext->textfile);
  219. return err;
  220. }
  221. if (!(dtext->text = av_malloc(textbuf_size+1)))
  222. return AVERROR(ENOMEM);
  223. memcpy(dtext->text, textbuf, textbuf_size);
  224. dtext->text[textbuf_size] = 0;
  225. av_file_unmap(textbuf, textbuf_size);
  226. }
  227. if (!dtext->text) {
  228. av_log(ctx, AV_LOG_ERROR,
  229. "Either text or a valid file must be provided\n");
  230. return AVERROR(EINVAL);
  231. }
  232. if ((err = av_parse_color(dtext->fontcolor_rgba, dtext->fontcolor_string, -1, ctx))) {
  233. av_log(ctx, AV_LOG_ERROR,
  234. "Invalid font color '%s'\n", dtext->fontcolor_string);
  235. return err;
  236. }
  237. if ((err = av_parse_color(dtext->boxcolor_rgba, dtext->boxcolor_string, -1, ctx))) {
  238. av_log(ctx, AV_LOG_ERROR,
  239. "Invalid box color '%s'\n", dtext->boxcolor_string);
  240. return err;
  241. }
  242. if ((err = av_parse_color(dtext->shadowcolor_rgba, dtext->shadowcolor_string, -1, ctx))) {
  243. av_log(ctx, AV_LOG_ERROR,
  244. "Invalid shadow color '%s'\n", dtext->shadowcolor_string);
  245. return err;
  246. }
  247. if ((err = FT_Init_FreeType(&(dtext->library)))) {
  248. av_log(ctx, AV_LOG_ERROR,
  249. "Could not load FreeType: %s\n", FT_ERRMSG(err));
  250. return AVERROR(EINVAL);
  251. }
  252. /* load the face, and set up the encoding, which is by default UTF-8 */
  253. if ((err = FT_New_Face(dtext->library, dtext->fontfile, 0, &dtext->face))) {
  254. av_log(ctx, AV_LOG_ERROR, "Could not load fontface from file '%s': %s\n",
  255. dtext->fontfile, FT_ERRMSG(err));
  256. return AVERROR(EINVAL);
  257. }
  258. if ((err = FT_Set_Pixel_Sizes(dtext->face, 0, dtext->fontsize))) {
  259. av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
  260. dtext->fontsize, FT_ERRMSG(err));
  261. return AVERROR(EINVAL);
  262. }
  263. dtext->use_kerning = FT_HAS_KERNING(dtext->face);
  264. /* load the fallback glyph with code 0 */
  265. load_glyph(ctx, NULL, 0);
  266. /* set the tabsize in pixels */
  267. if ((err = load_glyph(ctx, &glyph, ' ') < 0)) {
  268. av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
  269. return err;
  270. }
  271. dtext->tabsize *= glyph->advance;
  272. #if !HAVE_LOCALTIME_R
  273. av_log(ctx, AV_LOG_WARNING, "strftime() expansion unavailable!\n");
  274. #endif
  275. return 0;
  276. }
  277. static int query_formats(AVFilterContext *ctx)
  278. {
  279. static const enum PixelFormat pix_fmts[] = {
  280. PIX_FMT_ARGB, PIX_FMT_RGBA,
  281. PIX_FMT_ABGR, PIX_FMT_BGRA,
  282. PIX_FMT_RGB24, PIX_FMT_BGR24,
  283. PIX_FMT_YUV420P, PIX_FMT_YUV444P,
  284. PIX_FMT_YUV422P, PIX_FMT_YUV411P,
  285. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  286. PIX_FMT_NONE
  287. };
  288. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  289. return 0;
  290. }
  291. static int glyph_enu_free(void *opaque, void *elem)
  292. {
  293. av_free(elem);
  294. return 0;
  295. }
  296. static av_cold void uninit(AVFilterContext *ctx)
  297. {
  298. DrawTextContext *dtext = ctx->priv;
  299. int i;
  300. av_freep(&dtext->fontfile);
  301. av_freep(&dtext->text);
  302. av_freep(&dtext->fontcolor_string);
  303. av_freep(&dtext->boxcolor_string);
  304. av_freep(&dtext->positions);
  305. av_freep(&dtext->shadowcolor_string);
  306. av_tree_enumerate(dtext->glyphs, NULL, NULL, glyph_enu_free);
  307. av_tree_destroy(dtext->glyphs);
  308. dtext->glyphs = 0;
  309. FT_Done_Face(dtext->face);
  310. FT_Done_FreeType(dtext->library);
  311. for (i = 0; i < 4; i++) {
  312. av_freep(&dtext->box_line[i]);
  313. dtext->pixel_step[i] = 0;
  314. }
  315. }
  316. static int config_input(AVFilterLink *inlink)
  317. {
  318. DrawTextContext *dtext = inlink->dst->priv;
  319. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  320. int ret;
  321. dtext->hsub = pix_desc->log2_chroma_w;
  322. dtext->vsub = pix_desc->log2_chroma_h;
  323. if ((ret =
  324. ff_fill_line_with_color(dtext->box_line, dtext->pixel_step,
  325. inlink->w, dtext->boxcolor,
  326. inlink->format, dtext->boxcolor_rgba,
  327. &dtext->is_packed_rgb, dtext->rgba_map)) < 0)
  328. return ret;
  329. if (!dtext->is_packed_rgb) {
  330. uint8_t *rgba = dtext->fontcolor_rgba;
  331. dtext->fontcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  332. dtext->fontcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  333. dtext->fontcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  334. dtext->fontcolor[3] = rgba[3];
  335. rgba = dtext->shadowcolor_rgba;
  336. dtext->shadowcolor[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  337. dtext->shadowcolor[1] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  338. dtext->shadowcolor[2] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  339. dtext->shadowcolor[3] = rgba[3];
  340. }
  341. return 0;
  342. }
  343. #define GET_BITMAP_VAL(r, c) \
  344. bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
  345. (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
  346. bitmap->buffer[(r) * bitmap->pitch + (c)]
  347. #define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub) { \
  348. luma_pos = ((x) ) + ((y) ) * picref->linesize[0]; \
  349. alpha = yuva_color[3] * (val) * 129; \
  350. picref->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * picref->data[0][luma_pos] ) >> 23; \
  351. if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
  352. chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[1]; \
  353. chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[2]; \
  354. picref->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * picref->data[1][chroma_pos1]) >> 23; \
  355. picref->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * picref->data[2][chroma_pos2]) >> 23; \
  356. }\
  357. }
  358. static inline int draw_glyph_yuv(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x,
  359. unsigned int y, unsigned int width, unsigned int height,
  360. const uint8_t yuva_color[4], int hsub, int vsub)
  361. {
  362. int r, c, alpha;
  363. unsigned int luma_pos, chroma_pos1, chroma_pos2;
  364. uint8_t src_val, dst_pixel[4];
  365. for (r = 0; r < bitmap->rows && r+y < height; r++) {
  366. for (c = 0; c < bitmap->width && c+x < width; c++) {
  367. /* get pixel in the picref (destination) */
  368. dst_pixel[0] = picref->data[0][ c+x + (y+r) * picref->linesize[0]];
  369. dst_pixel[1] = picref->data[1][((c+x) >> hsub) + ((y+r) >> vsub) * picref->linesize[1]];
  370. dst_pixel[2] = picref->data[2][((c+x) >> hsub) + ((y+r) >> vsub) * picref->linesize[2]];
  371. /* get intensity value in the glyph bitmap (source) */
  372. src_val = GET_BITMAP_VAL(r, c);
  373. if (!src_val)
  374. continue;
  375. SET_PIXEL_YUV(picref, yuva_color, src_val, c+x, y+r, hsub, vsub);
  376. }
  377. }
  378. return 0;
  379. }
  380. #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
  381. p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
  382. alpha = rgba_color[3] * (val) * 129; \
  383. *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
  384. *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
  385. *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
  386. }
  387. static inline int draw_glyph_rgb(AVFilterBufferRef *picref, FT_Bitmap *bitmap,
  388. unsigned int x, unsigned int y,
  389. unsigned int width, unsigned int height, int pixel_step,
  390. const uint8_t rgba_color[4], const uint8_t rgba_map[4])
  391. {
  392. int r, c, alpha;
  393. uint8_t *p;
  394. uint8_t src_val, dst_pixel[4];
  395. for (r = 0; r < bitmap->rows && r+y < height; r++) {
  396. for (c = 0; c < bitmap->width && c+x < width; c++) {
  397. /* get pixel in the picref (destination) */
  398. dst_pixel[0] = picref->data[0][(c+x + rgba_map[0]) * pixel_step +
  399. (y+r) * picref->linesize[0]];
  400. dst_pixel[1] = picref->data[0][(c+x + rgba_map[1]) * pixel_step +
  401. (y+r) * picref->linesize[0]];
  402. dst_pixel[2] = picref->data[0][(c+x + rgba_map[2]) * pixel_step +
  403. (y+r) * picref->linesize[0]];
  404. /* get intensity value in the glyph bitmap (source) */
  405. src_val = GET_BITMAP_VAL(r, c);
  406. if (!src_val)
  407. continue;
  408. SET_PIXEL_RGB(picref, rgba_color, src_val, c+x, y+r, pixel_step,
  409. rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
  410. }
  411. }
  412. return 0;
  413. }
  414. static inline void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned int y,
  415. unsigned int width, unsigned int height,
  416. uint8_t *line[4], int pixel_step[4], uint8_t color[4],
  417. int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
  418. {
  419. int i, j, alpha;
  420. if (color[3] != 0xFF) {
  421. if (is_rgba_packed) {
  422. uint8_t *p;
  423. for (j = 0; j < height; j++)
  424. for (i = 0; i < width; i++)
  425. SET_PIXEL_RGB(picref, color, 255, i+x, y+j, pixel_step[0],
  426. rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
  427. } else {
  428. unsigned int luma_pos, chroma_pos1, chroma_pos2;
  429. for (j = 0; j < height; j++)
  430. for (i = 0; i < width; i++)
  431. SET_PIXEL_YUV(picref, color, 255, i+x, y+j, hsub, vsub);
  432. }
  433. } else {
  434. ff_draw_rectangle(picref->data, picref->linesize,
  435. line, pixel_step, hsub, vsub,
  436. x, y, width, height);
  437. }
  438. }
  439. static inline int is_newline(uint32_t c)
  440. {
  441. return (c == '\n' || c == '\r' || c == '\f' || c == '\v');
  442. }
  443. static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref,
  444. int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
  445. {
  446. char *text = dtext->text;
  447. uint32_t code = 0;
  448. int i;
  449. uint8_t *p;
  450. Glyph *glyph = NULL;
  451. for (i = 0, p = text; *p; i++) {
  452. Glyph dummy = { 0 };
  453. GET_UTF8(code, *p++, continue;);
  454. /* skip new line chars, just go to new line */
  455. if (code == '\n' || code == '\r' || code == '\t')
  456. continue;
  457. dummy.code = code;
  458. glyph = av_tree_find(dtext->glyphs, &dummy, (void *)glyph_cmp, NULL);
  459. if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
  460. glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
  461. return AVERROR(EINVAL);
  462. if (dtext->is_packed_rgb) {
  463. draw_glyph_rgb(picref, &glyph->bitmap,
  464. dtext->positions[i].x+x, dtext->positions[i].y+y, width, height,
  465. dtext->pixel_step[0], rgbcolor, dtext->rgba_map);
  466. } else {
  467. draw_glyph_yuv(picref, &glyph->bitmap,
  468. dtext->positions[i].x+x, dtext->positions[i].y+y, width, height,
  469. yuvcolor, dtext->hsub, dtext->vsub);
  470. }
  471. }
  472. return 0;
  473. }
  474. static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
  475. int width, int height)
  476. {
  477. DrawTextContext *dtext = ctx->priv;
  478. uint32_t code = 0, prev_code = 0;
  479. int x = 0, y = 0, i = 0, ret;
  480. int text_height, baseline;
  481. uint8_t *p;
  482. int str_w = 0;
  483. int y_min = 32000, y_max = -32000;
  484. FT_Vector delta;
  485. Glyph *glyph = NULL, *prev_glyph = NULL;
  486. Glyph dummy = { 0 };
  487. if (dtext->text != dtext->text_priv) {
  488. #if HAVE_LOCALTIME_R
  489. time_t now = time(0);
  490. struct tm ltime;
  491. uint8_t *buf = NULL;
  492. int buflen = 2*strlen(dtext->text) + 1, len;
  493. localtime_r(&now, &ltime);
  494. while ((buf = av_realloc(buf, buflen))) {
  495. *buf = 1;
  496. if ((len = strftime(buf, buflen, dtext->text, &ltime)) != 0 || *buf == 0)
  497. break;
  498. buflen *= 2;
  499. }
  500. if (!buf)
  501. return AVERROR(ENOMEM);
  502. av_freep(&dtext->text);
  503. dtext->text = dtext->text_priv = buf;
  504. #else
  505. dtext->text_priv = dtext->text;
  506. #endif
  507. if (!(dtext->positions = av_realloc(dtext->positions,
  508. strlen(dtext->text)*sizeof(*dtext->positions))))
  509. return AVERROR(ENOMEM);
  510. }
  511. x = dtext->x;
  512. y = dtext->y;
  513. /* load and cache glyphs */
  514. for (i = 0, p = dtext->text; *p; i++) {
  515. GET_UTF8(code, *p++, continue;);
  516. /* get glyph */
  517. dummy.code = code;
  518. glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
  519. if (!glyph)
  520. load_glyph(ctx, &glyph, code);
  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. /* compute and save position for each glyph */
  527. glyph = NULL;
  528. for (i = 0, p = dtext->text; *p; i++) {
  529. GET_UTF8(code, *p++, continue;);
  530. /* skip the \n in the sequence \r\n */
  531. if (prev_code == '\r' && code == '\n')
  532. continue;
  533. prev_code = code;
  534. if (is_newline(code)) {
  535. str_w = FFMAX(str_w, x - dtext->x);
  536. y += text_height;
  537. x = dtext->x;
  538. continue;
  539. }
  540. /* get glyph */
  541. prev_glyph = glyph;
  542. dummy.code = code;
  543. glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
  544. /* kerning */
  545. if (dtext->use_kerning && prev_glyph && glyph->code) {
  546. FT_Get_Kerning(dtext->face, prev_glyph->code, glyph->code,
  547. ft_kerning_default, &delta);
  548. x += delta.x >> 6;
  549. }
  550. if (x + glyph->bbox.xMax >= width) {
  551. str_w = FFMAX(str_w, x - dtext->x);
  552. y += text_height;
  553. x = dtext->x;
  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 == '\t') x = (x / dtext->tabsize + 1)*dtext->tabsize;
  559. else x += glyph->advance;
  560. }
  561. str_w = FFMIN(width - dtext->x - 1, FFMAX(str_w, x - dtext->x));
  562. y = FFMIN(y + text_height, height - 1);
  563. /* draw box */
  564. if (dtext->draw_box)
  565. drawbox(picref, dtext->x, dtext->y, str_w, y-dtext->y,
  566. dtext->box_line, dtext->pixel_step, dtext->boxcolor,
  567. dtext->hsub, dtext->vsub, dtext->is_packed_rgb, dtext->rgba_map);
  568. if (dtext->shadowx || dtext->shadowy) {
  569. if ((ret = draw_glyphs(dtext, picref, width, height, dtext->shadowcolor_rgba,
  570. dtext->shadowcolor, dtext->shadowx, dtext->shadowy)) < 0)
  571. return ret;
  572. }
  573. if ((ret = draw_glyphs(dtext, picref, width, height, dtext->fontcolor_rgba,
  574. dtext->fontcolor, 0, 0)) < 0)
  575. return ret;
  576. return 0;
  577. }
  578. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  579. static void end_frame(AVFilterLink *inlink)
  580. {
  581. AVFilterLink *outlink = inlink->dst->outputs[0];
  582. AVFilterBufferRef *picref = inlink->cur_buf;
  583. draw_text(inlink->dst, picref, picref->video->w, picref->video->h);
  584. avfilter_draw_slice(outlink, 0, picref->video->h, 1);
  585. avfilter_end_frame(outlink);
  586. }
  587. AVFilter avfilter_vf_drawtext = {
  588. .name = "drawtext",
  589. .description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
  590. .priv_size = sizeof(DrawTextContext),
  591. .init = init,
  592. .uninit = uninit,
  593. .query_formats = query_formats,
  594. .inputs = (AVFilterPad[]) {{ .name = "default",
  595. .type = AVMEDIA_TYPE_VIDEO,
  596. .get_video_buffer = avfilter_null_get_video_buffer,
  597. .start_frame = avfilter_null_start_frame,
  598. .draw_slice = null_draw_slice,
  599. .end_frame = end_frame,
  600. .config_props = config_input,
  601. .min_perms = AV_PERM_WRITE |
  602. AV_PERM_READ,
  603. .rej_perms = AV_PERM_PRESERVE },
  604. { .name = NULL}},
  605. .outputs = (AVFilterPad[]) {{ .name = "default",
  606. .type = AVMEDIA_TYPE_VIDEO, },
  607. { .name = NULL}},
  608. };