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.

525 lines
13KB

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