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.

284 lines
7.6KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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/share/X11/rgb.txt", "r");
  134. if (!f)
  135. f = fopen("/usr/lib/X11/rgb.txt", "r");
  136. if (!f) {
  137. fprintf(stderr, "Failed to find rgb.txt\n");
  138. return -1;
  139. }
  140. while (fgets(buff, sizeof(buff), f)) {
  141. int r, g, b;
  142. char colname[80];
  143. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  144. strcasecmp(colname, color) == 0) {
  145. ci->r = r;
  146. ci->g = g;
  147. ci->b = b;
  148. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  149. done = 1;
  150. break;
  151. }
  152. }
  153. fclose(f);
  154. if (!done) {
  155. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  156. return -1;
  157. }
  158. }
  159. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  160. return 0;
  161. }
  162. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  163. {
  164. CachedImage *cache;
  165. for (cache = ci->cache; cache; cache = cache->next) {
  166. if (width == cache->width && height == cache->height)
  167. return cache->image;
  168. }
  169. return NULL;
  170. }
  171. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  172. {
  173. CachedImage *cache = av_mallocz(sizeof(*cache));
  174. cache->image = image;
  175. cache->width = width;
  176. cache->height = height;
  177. cache->next = ci->cache;
  178. ci->cache = cache;
  179. }
  180. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  181. {
  182. ContextInfo *ci = (ContextInfo *) ctx;
  183. AVPicture picture1;
  184. Imlib_Image image;
  185. DATA32 *data;
  186. image = get_cached_image(ci, width, height);
  187. if (!image) {
  188. image = imlib_create_image(width, height);
  189. put_cached_image(ci, image, width, height);
  190. }
  191. imlib_context_set_image(image);
  192. data = imlib_image_get_data();
  193. avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
  194. if (pix_fmt != PIX_FMT_RGBA32) {
  195. if (img_convert(&picture1, PIX_FMT_RGBA32,
  196. picture, pix_fmt, width, height) < 0) {
  197. goto done;
  198. }
  199. } else {
  200. img_copy(&picture1, picture, PIX_FMT_RGBA32, width, height);
  201. }
  202. imlib_image_set_has_alpha(0);
  203. {
  204. int wid, hig, h_a, v_a;
  205. char buff[1000];
  206. char tbuff[1000];
  207. char *tbp = ci->text;
  208. time_t now = time(0);
  209. char *p, *q;
  210. int x, y;
  211. if (ci->file) {
  212. int fd = open(ci->file, O_RDONLY);
  213. if (fd < 0) {
  214. tbp = "[File not found]";
  215. } else {
  216. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  217. if (l >= 0) {
  218. tbuff[l] = 0;
  219. tbp = tbuff;
  220. } else {
  221. tbp = "[I/O Error]";
  222. }
  223. close(fd);
  224. }
  225. }
  226. strftime(buff, sizeof(buff), tbp ? tbp : "[No data]", localtime(&now));
  227. x = ci->x;
  228. y = ci->y;
  229. for (p = buff; p; p = q) {
  230. q = strchr(p, '\n');
  231. if (q)
  232. *q++ = 0;
  233. imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
  234. y += v_a;
  235. }
  236. }
  237. if (pix_fmt != PIX_FMT_RGBA32) {
  238. if (img_convert(picture, pix_fmt,
  239. &picture1, PIX_FMT_RGBA32, width, height) < 0) {
  240. }
  241. } else {
  242. img_copy(picture, &picture1, PIX_FMT_RGBA32, width, height);
  243. }
  244. done:
  245. ;
  246. }