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.

277 lines
7.5KB

  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. void Release(void *ctx)
  72. {
  73. ContextInfo *ci;
  74. ci = (ContextInfo *) ctx;
  75. if (ci->cache) {
  76. imlib_context_set_image(ci->cache->image);
  77. imlib_free_image();
  78. av_free(ci->cache);
  79. }
  80. if (ctx)
  81. av_free(ctx);
  82. }
  83. int Configure(void **ctxp, int argc, char *argv[])
  84. {
  85. int c;
  86. ContextInfo *ci;
  87. char *font = "LucidaSansDemiBold/16";
  88. char *fp = getenv("FONTPATH");
  89. char *color = 0;
  90. FILE *f;
  91. *ctxp = av_mallocz(sizeof(ContextInfo));
  92. ci = (ContextInfo *) *ctxp;
  93. optind = 0;
  94. if (fp)
  95. imlib_add_path_to_font_path(fp);
  96. while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
  97. switch (c) {
  98. case 'c':
  99. color = optarg;
  100. break;
  101. case 'F':
  102. font = optarg;
  103. break;
  104. case 't':
  105. ci->text = av_strdup(optarg);
  106. break;
  107. case 'f':
  108. ci->file = av_strdup(optarg);
  109. break;
  110. case 'x':
  111. ci->x = atoi(optarg);
  112. break;
  113. case 'y':
  114. ci->y = atoi(optarg);
  115. break;
  116. case '?':
  117. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  118. return -1;
  119. }
  120. }
  121. ci->fn = imlib_load_font(font);
  122. if (!ci->fn) {
  123. fprintf(stderr, "Failed to load font '%s'\n", font);
  124. return -1;
  125. }
  126. imlib_context_set_font(ci->fn);
  127. imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
  128. if (color) {
  129. char buff[256];
  130. int done = 0;
  131. f = fopen("/usr/lib/X11/rgb.txt", "r");
  132. if (!f) {
  133. fprintf(stderr, "Failed to find rgb.txt\n");
  134. return -1;
  135. }
  136. while (fgets(buff, sizeof(buff), f)) {
  137. int r, g, b;
  138. char colname[80];
  139. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  140. strcasecmp(colname, color) == 0) {
  141. ci->r = r;
  142. ci->g = g;
  143. ci->b = b;
  144. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  145. done = 1;
  146. break;
  147. }
  148. }
  149. fclose(f);
  150. if (!done) {
  151. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  152. return -1;
  153. }
  154. }
  155. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  156. return 0;
  157. }
  158. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  159. {
  160. CachedImage *cache;
  161. for (cache = ci->cache; cache; cache = cache->next) {
  162. if (width == cache->width && height == cache->height)
  163. return cache->image;
  164. }
  165. return NULL;
  166. }
  167. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  168. {
  169. CachedImage *cache = av_mallocz(sizeof(*cache));
  170. cache->image = image;
  171. cache->width = width;
  172. cache->height = height;
  173. cache->next = ci->cache;
  174. ci->cache = cache;
  175. }
  176. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  177. {
  178. ContextInfo *ci = (ContextInfo *) ctx;
  179. AVPicture picture1;
  180. Imlib_Image image;
  181. DATA32 *data;
  182. image = get_cached_image(ci, width, height);
  183. if (!image) {
  184. image = imlib_create_image(width, height);
  185. put_cached_image(ci, image, width, height);
  186. }
  187. imlib_context_set_image(image);
  188. data = imlib_image_get_data();
  189. if (pix_fmt != PIX_FMT_RGBA32) {
  190. avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
  191. if (img_convert(&picture1, PIX_FMT_RGBA32,
  192. picture, pix_fmt, width, height) < 0) {
  193. goto done;
  194. }
  195. } else {
  196. av_abort();
  197. }
  198. imlib_image_set_has_alpha(0);
  199. {
  200. int wid, hig, h_a, v_a;
  201. char buff[1000];
  202. char tbuff[1000];
  203. char *tbp = ci->text;
  204. time_t now = time(0);
  205. char *p, *q;
  206. int x, y;
  207. if (ci->file) {
  208. int fd = open(ci->file, O_RDONLY);
  209. if (fd < 0) {
  210. tbp = "[File not found]";
  211. } else {
  212. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  213. if (l >= 0) {
  214. tbuff[l] = 0;
  215. tbp = tbuff;
  216. } else {
  217. tbp = "[I/O Error]";
  218. }
  219. close(fd);
  220. }
  221. }
  222. strftime(buff, sizeof(buff), tbp ? tbp : "[No data]", localtime(&now));
  223. x = ci->x;
  224. y = ci->y;
  225. for (p = buff; p; p = q) {
  226. q = strchr(p, '\n');
  227. if (q)
  228. *q++ = 0;
  229. imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
  230. y += v_a;
  231. }
  232. }
  233. if (pix_fmt != PIX_FMT_RGBA32) {
  234. if (img_convert(picture, pix_fmt,
  235. &picture1, PIX_FMT_RGBA32, width, height) < 0) {
  236. }
  237. }
  238. done:
  239. ;
  240. }