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.

267 lines
7.3KB

  1. /*
  2. * imlib2 based hook
  3. * Copyright (c) 2002 Philip Gladstone
  4. *
  5. * This module implements a text overlay for a video image. Currently it
  6. * supports a fixed overlay or reading the text from a file. The string
  7. * is passed through strftime so that it is easy to imprint the date and
  8. * time onto the image.
  9. *
  10. * Options:
  11. *
  12. * -c <color> The color of the text
  13. * -F <fontname> The font face and size
  14. * -t <text> The text
  15. * -f <filename> The filename to read text from
  16. * -x <num> X coordinate to start text
  17. * -y <num> Y coordinate to start text
  18. *
  19. * This module is very much intended as an example of what could be done.
  20. * For example, you could overlay an image (even semi-transparent) like
  21. * TV stations do. You can manipulate the image using imlib2 functions
  22. * in any way.
  23. *
  24. * One caution is that this is an expensive process -- in particular the
  25. * conversion of the image into RGB and back is time consuming. For some
  26. * special cases -- e.g. painting black text -- it would be faster to paint
  27. * the text into a bitmap and then combine it directly into the YUV
  28. * image. However, this code is fast enough to handle 10 fps of 320x240 on a
  29. * 900MHz Duron in maybe 15% of the CPU.
  30. *
  31. * This library 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 of the License, or (at your option) any later version.
  35. *
  36. * This library 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 this library; if not, write to the Free Software
  43. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  44. */
  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. #include <sys/time.h>
  52. #include <X11/Xlib.h>
  53. #include <Imlib2.h>
  54. #include "framehook.h"
  55. typedef struct {
  56. int dummy;
  57. Imlib_Font fn;
  58. char *text;
  59. char *file;
  60. int r, g, b;
  61. int x;
  62. int y;
  63. struct _CachedImage *cache;
  64. } ContextInfo;
  65. typedef struct _CachedImage {
  66. struct _CachedImage *next;
  67. Imlib_Image image;
  68. int width;
  69. int height;
  70. } CachedImage;
  71. int Configure(void **ctxp, int argc, char *argv[])
  72. {
  73. int c;
  74. ContextInfo *ci;
  75. char *font = "LucidaSansDemiBold/16";
  76. char *fp = getenv("FONTPATH");
  77. char *color = 0;
  78. FILE *f;
  79. *ctxp = av_mallocz(sizeof(ContextInfo));
  80. ci = (ContextInfo *) *ctxp;
  81. optind = 0;
  82. if (fp)
  83. imlib_add_path_to_font_path(fp);
  84. while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
  85. switch (c) {
  86. case 'c':
  87. color = optarg;
  88. break;
  89. case 'F':
  90. font = optarg;
  91. break;
  92. case 't':
  93. ci->text = strdup(optarg);
  94. break;
  95. case 'f':
  96. ci->file = strdup(optarg);
  97. break;
  98. case 'x':
  99. ci->x = atoi(optarg);
  100. break;
  101. case 'y':
  102. ci->y = atoi(optarg);
  103. break;
  104. case '?':
  105. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  106. return -1;
  107. }
  108. }
  109. ci->fn = imlib_load_font(font);
  110. if (!ci->fn) {
  111. fprintf(stderr, "Failed to load font '%s'\n", font);
  112. return -1;
  113. }
  114. imlib_context_set_font(ci->fn);
  115. imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
  116. if (color) {
  117. char buff[256];
  118. int done = 0;
  119. f = fopen("/usr/lib/X11/rgb.txt", "r");
  120. if (!f) {
  121. fprintf(stderr, "Failed to find rgb.txt\n");
  122. return -1;
  123. }
  124. while (fgets(buff, sizeof(buff), f)) {
  125. int r, g, b;
  126. char colname[80];
  127. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  128. strcasecmp(colname, color) == 0) {
  129. ci->r = r;
  130. ci->g = g;
  131. ci->b = b;
  132. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  133. done = 1;
  134. break;
  135. }
  136. }
  137. fclose(f);
  138. if (!done) {
  139. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  140. return -1;
  141. }
  142. }
  143. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  144. return 0;
  145. }
  146. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  147. {
  148. CachedImage *cache;
  149. for (cache = ci->cache; cache; cache = cache->next) {
  150. if (width == cache->width && height == cache->height)
  151. return cache->image;
  152. }
  153. return NULL;
  154. }
  155. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  156. {
  157. CachedImage *cache = av_mallocz(sizeof(*cache));
  158. cache->image = image;
  159. cache->width = width;
  160. cache->height = height;
  161. cache->next = ci->cache;
  162. ci->cache = cache;
  163. }
  164. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts)
  165. {
  166. ContextInfo *ci = (ContextInfo *) ctx;
  167. AVPicture picture1;
  168. Imlib_Image image;
  169. DATA32 *data;
  170. image = get_cached_image(ci, width, height);
  171. if (!image) {
  172. image = imlib_create_image(width, height);
  173. put_cached_image(ci, image, width, height);
  174. }
  175. imlib_context_set_image(image);
  176. data = imlib_image_get_data();
  177. if (pix_fmt != PIX_FMT_BGRA32) {
  178. avpicture_fill(&picture1, (UINT8 *) data, PIX_FMT_BGRA32, width, height);
  179. if (img_convert(&picture1, PIX_FMT_BGRA32,
  180. picture, pix_fmt, width, height) < 0) {
  181. goto done;
  182. }
  183. } else {
  184. av_abort();
  185. }
  186. imlib_image_set_has_alpha(0);
  187. {
  188. int wid, hig, h_a, v_a;
  189. char buff[1000];
  190. char tbuff[1000];
  191. char *tbp = ci->text;
  192. time_t now = time(0);
  193. char *p, *q;
  194. int x, y;
  195. if (ci->file) {
  196. int fd = open(ci->file, O_RDONLY);
  197. if (fd < 0) {
  198. tbp = "[File not found]";
  199. } else {
  200. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  201. if (l >= 0) {
  202. tbuff[l] = 0;
  203. tbp = tbuff;
  204. } else {
  205. tbp = "[I/O Error]";
  206. }
  207. close(fd);
  208. }
  209. }
  210. strftime(buff, sizeof(buff), tbp, localtime(&now));
  211. x = ci->x;
  212. y = ci->y;
  213. for (p = buff; p; p = q) {
  214. q = strchr(p, '\n');
  215. if (q)
  216. *q++ = 0;
  217. imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
  218. y += v_a;
  219. }
  220. }
  221. if (pix_fmt != PIX_FMT_BGRA32) {
  222. if (img_convert(picture, pix_fmt,
  223. &picture1, PIX_FMT_BGRA32, width, height) < 0) {
  224. }
  225. }
  226. done:
  227. ;
  228. }
  229. /* To ensure correct typing */
  230. FrameHookConfigureFn ConfigureFn = Configure;
  231. FrameHookProcessFn ProcessFn = Process;