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.

528 lines
14KB

  1. /*
  2. * drawtext.c: print text over the screen
  3. ******************************************************************************
  4. * Options:
  5. * -f <filename> font filename (MANDATORY!!!)
  6. * -s <pixel_size> font size in pixels [default 16]
  7. * -b print background
  8. * -o outline glyphs (use the bg color)
  9. * -x <pos> x position ( >= 0) [default 0]
  10. * -y <pos> y position ( >= 0) [default 0]
  11. * -t <text> text to print (will be passed to strftime())
  12. * MANDATORY: will be used even when -T is used.
  13. * in this case, -t will be used if some error
  14. * occurs
  15. * -T <filename> file with the text (re-read every frame)
  16. * -c <#RRGGBB> foreground color ('internet' way) [default #ffffff]
  17. * -C <#RRGGBB> background color ('internet' way) [default #000000]
  18. *
  19. ******************************************************************************
  20. * Features:
  21. * - True Type, Type1 and others via FreeType2 library
  22. * - Font kerning (better output)
  23. * - Line Wrap (if the text doesn't fit, the next char go to the next line)
  24. * - Background box
  25. * - Outline
  26. ******************************************************************************
  27. * Author: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
  28. *
  29. * This file is part of FFmpeg.
  30. *
  31. * FFmpeg is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU Lesser General Public
  33. * License as published by the Free Software Foundation; either
  34. * version 2.1 of the License, or (at your option) any later version.
  35. *
  36. * FFmpeg is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  39. * Lesser General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU Lesser General Public
  42. * License along with FFmpeg; if not, write to the Free Software
  43. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  44. */
  45. #define MAXSIZE_TEXT 1024
  46. #include "framehook.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <fcntl.h>
  50. #include <stdarg.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. #undef time
  54. #include <sys/time.h>
  55. #include <time.h>
  56. #include <ft2build.h>
  57. #include FT_FREETYPE_H
  58. #include FT_GLYPH_H
  59. #define RGB_TO_YUV(rgb_color, yuv_color) { \
  60. yuv_color[0] = ( 0.257 * rgb_color[0]) + (0.504 * rgb_color[1]) + (0.098 * rgb_color[2]) + 16; \
  61. yuv_color[2] = ( 0.439 * rgb_color[0]) - (0.368 * rgb_color[1]) - (0.071 * rgb_color[2]) + 128; \
  62. yuv_color[1] = (-0.148 * rgb_color[0]) - (0.291 * rgb_color[1]) + (0.439 * rgb_color[2]) + 128; \
  63. }
  64. #define COPY_3(dst,src) { \
  65. dst[0]=src[0]; \
  66. dst[1]=src[1]; \
  67. dst[2]=src[2]; \
  68. }
  69. #define SET_PIXEL(picture, yuv_color, x, y) { \
  70. picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
  71. picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
  72. picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
  73. }
  74. #define GET_PIXEL(picture, yuv_color, x, y) { \
  75. yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
  76. yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
  77. yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
  78. }
  79. typedef struct {
  80. unsigned char *text;
  81. unsigned char *file;
  82. unsigned int x;
  83. unsigned int y;
  84. int bg;
  85. int outline;
  86. unsigned char bgcolor[3]; /* YUV */
  87. unsigned char fgcolor[3]; /* YUV */
  88. FT_Library library;
  89. FT_Face face;
  90. FT_Glyph glyphs[ 255 ];
  91. FT_Bitmap bitmaps[ 255 ];
  92. int advance[ 255 ];
  93. int bitmap_left[ 255 ];
  94. int bitmap_top[ 255 ];
  95. unsigned int glyphs_index[ 255 ];
  96. int text_height;
  97. int baseline;
  98. int use_kerning;
  99. } ContextInfo;
  100. void Release(void *ctx)
  101. {
  102. if (ctx)
  103. av_free(ctx);
  104. }
  105. static int ParseColor(char *text, unsigned char yuv_color[3])
  106. {
  107. char tmp[3];
  108. unsigned char rgb_color[3];
  109. int i;
  110. tmp[2] = '\0';
  111. if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
  112. return -1;
  113. for (i=0; i < 3; i++)
  114. {
  115. tmp[0] = text[i*2+1];
  116. tmp[1] = text[i*2+2];
  117. rgb_color[i] = strtol(tmp, NULL, 16);
  118. }
  119. RGB_TO_YUV(rgb_color, yuv_color);
  120. return 0;
  121. }
  122. int Configure(void **ctxp, int argc, char *argv[])
  123. {
  124. int c;
  125. int error;
  126. ContextInfo *ci=NULL;
  127. char *font=NULL;
  128. unsigned int size=16;
  129. FT_BBox bbox;
  130. int yMax, yMin;
  131. *ctxp = av_mallocz(sizeof(ContextInfo));
  132. ci = (ContextInfo *) *ctxp;
  133. /* configure Context Info */
  134. ci->text = NULL;
  135. ci->file = NULL;
  136. ci->x = ci->y = 0;
  137. ci->fgcolor[0]=255;
  138. ci->fgcolor[1]=128;
  139. ci->fgcolor[2]=128;
  140. ci->bgcolor[0]=0;
  141. ci->fgcolor[1]=128;
  142. ci->fgcolor[2]=128;
  143. ci->bg = 0;
  144. ci->outline = 0;
  145. ci->text_height = 0;
  146. optind = 0;
  147. while ((c = getopt(argc, argv, "f:t:T:x:y:s:c:C:bo")) > 0) {
  148. switch (c) {
  149. case 'f':
  150. font = optarg;
  151. break;
  152. case 't':
  153. ci->text = av_strdup(optarg);
  154. break;
  155. case 'T':
  156. ci->file = av_strdup(optarg);
  157. break;
  158. case 'x':
  159. ci->x = (unsigned int) atoi(optarg);
  160. break;
  161. case 'y':
  162. ci->y = (unsigned int) atoi(optarg);
  163. break;
  164. case 's':
  165. size = (unsigned int) atoi(optarg);
  166. break;
  167. case 'c':
  168. if (ParseColor(optarg, ci->fgcolor) == -1)
  169. {
  170. av_log(NULL, AV_LOG_ERROR, "Invalid foreground color: '%s'. You must specify the color in the internet way(packaged hex): #RRGGBB, ie: -c #ffffff (for white foreground)\n", optarg);
  171. return -1;
  172. }
  173. break;
  174. case 'C':
  175. if (ParseColor(optarg, ci->bgcolor) == -1)
  176. {
  177. av_log(NULL, AV_LOG_ERROR, "Invalid foreground color: '%s'. You must specify the color in the internet way(packaged hex): #RRGGBB, ie: -c #ffffff (for white foreground)\n", optarg);
  178. return -1;
  179. }
  180. break;
  181. case 'b':
  182. ci->bg=1;
  183. break;
  184. case 'o':
  185. ci->outline=1;
  186. break;
  187. case '?':
  188. av_log(NULL, AV_LOG_ERROR, "Unrecognized argument '%s'\n", argv[optind]);
  189. return -1;
  190. }
  191. }
  192. if (!ci->text)
  193. {
  194. av_log(NULL, AV_LOG_ERROR, "No text provided (-t text)\n");
  195. return -1;
  196. }
  197. if (ci->file)
  198. {
  199. FILE *fp;
  200. if ((fp=fopen(ci->file, "r")) == NULL)
  201. {
  202. av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened. Using text provided with -t switch: %s", strerror(errno));
  203. }
  204. else
  205. {
  206. fclose(fp);
  207. }
  208. }
  209. if (!font)
  210. {
  211. av_log(NULL, AV_LOG_ERROR, "No font file provided! (-f filename)\n");
  212. return -1;
  213. }
  214. if ((error = FT_Init_FreeType(&(ci->library))) != 0)
  215. {
  216. av_log(NULL, AV_LOG_ERROR, "Could not load FreeType (error# %d).\n", error);
  217. return -1;
  218. }
  219. if ((error = FT_New_Face( ci->library, font, 0, &(ci->face) )) != 0)
  220. {
  221. av_log(NULL, AV_LOG_ERROR, "Could not load face: %s (error# %d).\n", font, error);
  222. return -1;
  223. }
  224. if ((error = FT_Set_Pixel_Sizes( ci->face, 0, size)) != 0)
  225. {
  226. av_log(NULL, AV_LOG_ERROR, "Could not set font size to %d pixels (error# %d).\n", size, error);
  227. return -1;
  228. }
  229. ci->use_kerning = FT_HAS_KERNING(ci->face);
  230. /* load and cache glyphs */
  231. yMax = -32000;
  232. yMin = 32000;
  233. for (c=0; c < 256; c++)
  234. {
  235. /* Load char */
  236. error = FT_Load_Char( ci->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
  237. if (error) continue; /* ignore errors */
  238. /* Save bitmap */
  239. ci->bitmaps[c] = ci->face->glyph->bitmap;
  240. /* Save bitmap left */
  241. ci->bitmap_left[c] = ci->face->glyph->bitmap_left;
  242. /* Save bitmap top */
  243. ci->bitmap_top[c] = ci->face->glyph->bitmap_top;
  244. /* Save advance */
  245. ci->advance[c] = ci->face->glyph->advance.x >> 6;
  246. /* Save glyph */
  247. error = FT_Get_Glyph( ci->face->glyph, &(ci->glyphs[c]) );
  248. /* Save glyph index */
  249. ci->glyphs_index[c] = FT_Get_Char_Index( ci->face, (unsigned char) c );
  250. /* Measure text height to calculate text_height (or the maximum text height) */
  251. FT_Glyph_Get_CBox( ci->glyphs[ c ], ft_glyph_bbox_pixels, &bbox );
  252. if (bbox.yMax > yMax)
  253. yMax = bbox.yMax;
  254. if (bbox.yMin < yMin)
  255. yMin = bbox.yMin;
  256. }
  257. ci->text_height = yMax - yMin;
  258. ci->baseline = yMax;
  259. return 0;
  260. }
  261. static inline void draw_glyph(AVPicture *picture, FT_Bitmap *bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_fgcolor[3], unsigned char yuv_bgcolor[3], int outline)
  262. {
  263. int r, c;
  264. int spixel, dpixel[3], in_glyph=0;
  265. if (bitmap->pixel_mode == ft_pixel_mode_mono)
  266. {
  267. in_glyph = 0;
  268. for (r=0; (r < bitmap->rows) && (r+y < height); r++)
  269. {
  270. for (c=0; (c < bitmap->width) && (c+x < width); c++)
  271. {
  272. /* pixel in the picture (destination) */
  273. GET_PIXEL(picture, dpixel, (c+x), (y+r));
  274. /* pixel in the glyph bitmap (source) */
  275. spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
  276. if (spixel)
  277. COPY_3(dpixel, yuv_fgcolor);
  278. if (outline)
  279. {
  280. /* border detection: */
  281. if ( (!in_glyph) && (spixel) )
  282. /* left border detected */
  283. {
  284. in_glyph = 1;
  285. /* draw left pixel border */
  286. if (c-1 >= 0)
  287. SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
  288. }
  289. else if ( (in_glyph) && (!spixel) )
  290. /* right border detected */
  291. {
  292. in_glyph = 0;
  293. /* 'draw' right pixel border */
  294. COPY_3(dpixel, yuv_bgcolor);
  295. }
  296. if (in_glyph)
  297. /* see if we have a top/bottom border */
  298. {
  299. /* top */
  300. if ( (r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
  301. /* we have a top border */
  302. SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r-1));
  303. /* bottom */
  304. if ( (r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
  305. /* we have a bottom border */
  306. SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r+1));
  307. }
  308. }
  309. SET_PIXEL(picture, dpixel, (c+x), (y+r));
  310. }
  311. }
  312. }
  313. }
  314. static inline void draw_box(AVPicture *picture, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_color[3])
  315. {
  316. int i, j;
  317. for (j = 0; (j < height); j++)
  318. for (i = 0; (i < width); i++)
  319. {
  320. SET_PIXEL(picture, yuv_color, (i+x), (y+j));
  321. }
  322. }
  323. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  324. {
  325. ContextInfo *ci = (ContextInfo *) ctx;
  326. FT_Face face = ci->face;
  327. FT_GlyphSlot slot = face->glyph;
  328. unsigned char *text = ci->text;
  329. unsigned char c;
  330. int x = 0, y = 0, i=0, size=0;
  331. unsigned char buff[MAXSIZE_TEXT];
  332. unsigned char tbuff[MAXSIZE_TEXT];
  333. time_t now = time(0);
  334. int str_w, str_w_max;
  335. FT_Vector pos[MAXSIZE_TEXT];
  336. FT_Vector delta;
  337. if (ci->file)
  338. {
  339. int fd = open(ci->file, O_RDONLY);
  340. if (fd < 0)
  341. {
  342. text = ci->text;
  343. av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened. Using text provided with -t switch: %s", strerror(errno));
  344. }
  345. else
  346. {
  347. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  348. if (l >= 0)
  349. {
  350. tbuff[l] = 0;
  351. text = tbuff;
  352. }
  353. else
  354. {
  355. text = ci->text;
  356. av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be read. Using text provided with -t switch: %s", strerror(errno));
  357. }
  358. close(fd);
  359. }
  360. }
  361. else
  362. {
  363. text = ci->text;
  364. }
  365. strftime(buff, sizeof(buff), text, localtime(&now));
  366. text = buff;
  367. size = strlen(text);
  368. /* measure string size and save glyphs position*/
  369. str_w = str_w_max = 0;
  370. x = ci->x;
  371. y = ci->y;
  372. for (i=0; i < size; i++)
  373. {
  374. c = text[i];
  375. /* kerning */
  376. if ( (ci->use_kerning) && (i > 0) && (ci->glyphs_index[c]) )
  377. {
  378. FT_Get_Kerning( ci->face,
  379. ci->glyphs_index[ text[i-1] ],
  380. ci->glyphs_index[c],
  381. ft_kerning_default,
  382. &delta );
  383. x += delta.x >> 6;
  384. }
  385. if (( (x + ci->advance[ c ]) >= width ) || ( c == '\n' ))
  386. {
  387. str_w = width - ci->x - 1;
  388. y += ci->text_height;
  389. x = ci->x;
  390. }
  391. /* save position */
  392. pos[i].x = x + ci->bitmap_left[c];
  393. pos[i].y = y - ci->bitmap_top[c] + ci->baseline;
  394. x += ci->advance[c];
  395. if (str_w > str_w_max)
  396. str_w_max = str_w;
  397. }
  398. if (ci->bg)
  399. {
  400. /* Check if it doesn't pass the limits */
  401. if ( str_w_max + ci->x >= width )
  402. str_w_max = width - ci->x - 1;
  403. if ( y >= height )
  404. y = height - 1 - 2*ci->y;
  405. /* Draw Background */
  406. draw_box( picture, ci->x, ci->y, str_w_max, y - ci->y, ci->bgcolor );
  407. }
  408. /* Draw Glyphs */
  409. for (i=0; i < size; i++)
  410. {
  411. c = text[i];
  412. if (
  413. ( (c == '_') && (text == ci->text) ) || /* skip '_' (consider as space)
  414. IF text was specified in cmd line
  415. (which doesn't like neasted quotes) */
  416. ( c == '\n' ) /* Skip new line char, just go to new line */
  417. )
  418. continue;
  419. /* now, draw to our target surface */
  420. draw_glyph( picture,
  421. &(ci->bitmaps[ c ]),
  422. pos[i].x,
  423. pos[i].y,
  424. width,
  425. height,
  426. ci->fgcolor,
  427. ci->bgcolor,
  428. ci->outline );
  429. /* increment pen position */
  430. x += slot->advance.x >> 6;
  431. }
  432. }