drawtext.c 14 KB

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