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.

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