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.

352 lines
9.1KB

  1. /*
  2. * drawtext.c: print text over the screen
  3. ******************************************************************
  4. * Options:
  5. * -f <filename> font filename
  6. * -s <pixel_size> font size in pixels
  7. * -b print background
  8. * -o outline glyphs (use the bg color)
  9. * -x <pos> x position ( > 0)
  10. * -y <pos> y position ( > 0)
  11. * -t <text> text to print (will be passed to strftime())
  12. * -c <#RRGGBB> foreground color ('internet' way)
  13. * -C <#RRGGBB> background color ('internet' way)
  14. *
  15. ******************************************************************
  16. *
  17. * Author: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <fcntl.h>
  36. #include <stdarg.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <sys/time.h>
  40. #include <time.h>
  41. #include "framehook.h"
  42. #include <ft2build.h>
  43. #include FT_FREETYPE_H
  44. #define RGB_TO_YUV(rgb_color, yuv_color) { \
  45. yuv_color[0] = ( 0.257 * rgb_color[0]) + (0.504 * rgb_color[1]) + (0.098 * rgb_color[2]) + 16; \
  46. yuv_color[2] = ( 0.439 * rgb_color[0]) - (0.368 * rgb_color[1]) - (0.071 * rgb_color[2]) + 128; \
  47. yuv_color[1] = (-0.148 * rgb_color[0]) - (0.291 * rgb_color[1]) + (0.439 * rgb_color[2]) + 128; \
  48. }
  49. #define COPY_3(dst,src) { \
  50. dst[0]=src[0]; \
  51. dst[1]=src[1]; \
  52. dst[2]=src[2]; \
  53. }
  54. #define SET_PIXEL(picture, yuv_color, x, y) { \
  55. picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
  56. picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
  57. picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
  58. }
  59. #define GET_PIXEL(picture, yuv_color, x, y) { \
  60. yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
  61. yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
  62. yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
  63. }
  64. typedef struct {
  65. char *text;
  66. unsigned int x;
  67. unsigned int y;
  68. int bg;
  69. int outline;
  70. unsigned char bgcolor[3]; /* YUV */
  71. unsigned char fgcolor[3]; /* YUV */
  72. FT_Library ft_lib;
  73. FT_Face ft_face;
  74. } ContextInfo;
  75. void Release(void *ctx)
  76. {
  77. if (ctx)
  78. av_free(ctx);
  79. }
  80. int ParseColor(char *text, unsigned char yuv_color[3])
  81. {
  82. char tmp[3];
  83. unsigned char rgb_color[3];
  84. int i;
  85. tmp[2] = '\0';
  86. if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
  87. return -1;
  88. for (i=0; i < 3; i++)
  89. {
  90. tmp[0] = text[i*2+1];
  91. tmp[1] = text[i*2+2];
  92. rgb_color[i] = strtol(tmp, NULL, 16);
  93. }
  94. RGB_TO_YUV(rgb_color, yuv_color);
  95. printf("RGB=%d,%d,%d YUV=%d,%d,%d\n",rgb_color[0],rgb_color[1],rgb_color[2],
  96. yuv_color[0],yuv_color[1],yuv_color[2]);
  97. return 0;
  98. }
  99. int Configure(void **ctxp, int argc, char *argv[])
  100. {
  101. int c;
  102. int error;
  103. ContextInfo *ci=NULL;
  104. char *font=NULL;
  105. unsigned int size=16;
  106. *ctxp = av_mallocz(sizeof(ContextInfo));
  107. ci = (ContextInfo *) *ctxp;
  108. /* configure Context Info */
  109. ci->text = NULL;
  110. ci->x = ci->y = 0;
  111. ci->fgcolor[0]=255;
  112. ci->fgcolor[1]=128;
  113. ci->fgcolor[2]=128;
  114. ci->bgcolor[0]=0;
  115. ci->fgcolor[1]=128;
  116. ci->fgcolor[2]=128;
  117. ci->bg = 0;
  118. ci->outline = 0;
  119. optind = 0;
  120. while ((c = getopt(argc, argv, "f:t:x:y:s:c:C:bo")) > 0) {
  121. switch (c) {
  122. case 'f':
  123. font = optarg;
  124. break;
  125. case 't':
  126. ci->text = av_strdup(optarg);
  127. break;
  128. case 'x':
  129. ci->x = (unsigned int) atoi(optarg);
  130. break;
  131. case 'y':
  132. ci->y = (unsigned int) atoi(optarg);
  133. break;
  134. case 's':
  135. size = (unsigned int) atoi(optarg);
  136. break;
  137. case 'c':
  138. if (ParseColor(optarg, ci->fgcolor) == -1)
  139. {
  140. 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);
  141. return -1;
  142. }
  143. break;
  144. case 'C':
  145. if (ParseColor(optarg, ci->bgcolor) == -1)
  146. {
  147. 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);
  148. return -1;
  149. }
  150. break;
  151. case 'b':
  152. ci->bg=1;
  153. break;
  154. case 'o':
  155. ci->outline=1;
  156. break;
  157. case '?':
  158. fprintf(stderr, "ERROR: Unrecognized argument '%s'\n", argv[optind]);
  159. return -1;
  160. }
  161. }
  162. if (!ci->text)
  163. {
  164. fprintf(stderr,"ERROR: No text provided (-t text)\n");
  165. return -1;
  166. }
  167. if (!font)
  168. {
  169. fprintf(stderr,"ERROR: No font file provided! (-f filename)\n");
  170. return -1;
  171. }
  172. if ((error = FT_Init_FreeType(&(ci->ft_lib))) != 0)
  173. {
  174. fprintf(stderr,"ERROR: Could not load FreeType (error# %d)\n",error);
  175. return -1;
  176. }
  177. if ((error = FT_New_Face( ci->ft_lib, font, 0, &(ci->ft_face) )) != 0)
  178. {
  179. fprintf(stderr,"ERROR: Could not load face: %s (error# %d)\n",font, error);
  180. return -1;
  181. }
  182. if ((error = FT_Set_Pixel_Sizes( ci->ft_face, 0, size)) != 0)
  183. {
  184. fprintf(stderr,"ERROR: Could not set font size to %d pixels (error# %d)\n",size, error);
  185. return -1;
  186. }
  187. return 0;
  188. }
  189. 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)
  190. {
  191. int r, c;
  192. int spixel, dpixel[3], in_glyph=0;
  193. if (bitmap->pixel_mode == ft_pixel_mode_mono)
  194. {
  195. in_glyph = 0;
  196. for (r=0; (r < bitmap->rows) && (r+y < height); r++)
  197. {
  198. for (c=0; (c < bitmap->width) && (c+x < width); c++)
  199. {
  200. /* pixel in the picture (destination) */
  201. GET_PIXEL(picture, dpixel, (c+x), (y+r));
  202. /* pixel in the glyph bitmap (source) */
  203. spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
  204. if (spixel)
  205. COPY_3(dpixel, yuv_fgcolor);
  206. if (outline)
  207. {
  208. /* border detection: */
  209. if ( (!in_glyph) && (spixel) )
  210. /* left border detected */
  211. {
  212. in_glyph = 1;
  213. /* draw left pixel border */
  214. if (c-1 >= 0)
  215. SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
  216. }
  217. else if ( (in_glyph) && (!spixel) )
  218. /* right border detected */
  219. {
  220. in_glyph = 0;
  221. /* 'draw' right pixel border */
  222. COPY_3(dpixel, yuv_bgcolor);
  223. }
  224. else if (in_glyph)
  225. /* see if we have a top/bottom border */
  226. {
  227. /* top */
  228. if ( (r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
  229. /* we have a top border */
  230. SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r-1));
  231. /* bottom */
  232. if ( (r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
  233. /* we have a bottom border */
  234. SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r+1));
  235. }
  236. }
  237. SET_PIXEL(picture, dpixel, (c+x), (y+r));
  238. }
  239. }
  240. }
  241. }
  242. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  243. {
  244. ContextInfo *ci = (ContextInfo *) ctx;
  245. FT_Face face = ci->ft_face;
  246. FT_GlyphSlot slot = face->glyph;
  247. char *text = ci->text;
  248. int x = 0, y = 0, i=0, j=0, size=0, error;
  249. int str_w, str_h;
  250. char buff[1000];
  251. time_t now = time(0);
  252. strftime(buff, sizeof(buff), text, localtime(&now));
  253. text = buff;
  254. size = strlen(text);
  255. x = ci->x;
  256. y = ci->y;
  257. /* measure string size */
  258. str_w = str_h = 0;
  259. for (i=0; i < size; i++)
  260. {
  261. /* load glyph image into the slot (erase previous one) */
  262. error = FT_Load_Char( face, text[i], FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
  263. if (error) continue; /* ignore errors */
  264. str_w += slot->advance.x >> 6;
  265. if (slot->bitmap_top > str_h)
  266. str_h = slot->bitmap_top;
  267. }
  268. if (ci->bg)
  269. /* draw background */
  270. for (j = 0; (j < str_h) && (j+y < height); j++)
  271. for (i = 0; (i < str_w) && (i+x < width); i++)
  272. {
  273. SET_PIXEL(picture, ci->bgcolor, (i+x), (y+j));
  274. }
  275. /* Draw Glyphs */
  276. for (i=0; i < size; i++)
  277. {
  278. /* load glyph image into the slot (erase previous one) */
  279. error = FT_Load_Char( face, text[i], FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
  280. if (error) continue; /* ignore errors */
  281. if (text[i] != '_') /* skip '_' (consider as space) */
  282. /* now, draw to our target surface */
  283. draw_glyph( picture, &slot->bitmap,
  284. x + slot->bitmap_left,
  285. y - slot->bitmap_top + str_h,
  286. width, height,
  287. ci->fgcolor, ci->bgcolor,
  288. ci->outline);
  289. /* increment pen position */
  290. x += slot->advance.x >> 6;
  291. }
  292. }