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.

311 lines
8.9KB

  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 file is part of FFmpeg.
  32. *
  33. * FFmpeg is free software; you can redistribute it and/or
  34. * modify it under the terms of the GNU Lesser General Public
  35. * License as published by the Free Software Foundation; either
  36. * version 2.1 of the License, or (at your option) any later version.
  37. *
  38. * FFmpeg is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  41. * Lesser General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU Lesser General Public
  44. * License along with FFmpeg; if not, write to the Free Software
  45. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  46. */
  47. #include "framehook.h"
  48. #include "swscale.h"
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <fcntl.h>
  52. #include <stdarg.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #undef time
  56. #include <sys/time.h>
  57. #include <time.h>
  58. #include <X11/Xlib.h>
  59. #include <Imlib2.h>
  60. static int sws_flags = SWS_BICUBIC;
  61. typedef struct {
  62. int dummy;
  63. Imlib_Font fn;
  64. char *text;
  65. char *file;
  66. int r, g, b;
  67. int x;
  68. int y;
  69. struct _CachedImage *cache;
  70. // This vhook first converts frame to RGB ...
  71. struct SwsContext *toRGB_convert_ctx;
  72. // ... and then converts back frame from RGB to initial format
  73. struct SwsContext *fromRGB_convert_ctx;
  74. } ContextInfo;
  75. typedef struct _CachedImage {
  76. struct _CachedImage *next;
  77. Imlib_Image image;
  78. int width;
  79. int height;
  80. } CachedImage;
  81. void Release(void *ctx)
  82. {
  83. ContextInfo *ci;
  84. ci = (ContextInfo *) ctx;
  85. if (ci->cache) {
  86. imlib_context_set_image(ci->cache->image);
  87. imlib_free_image();
  88. av_free(ci->cache);
  89. }
  90. if (ctx) {
  91. sws_freeContext(ci->toRGB_convert_ctx);
  92. sws_freeContext(ci->fromRGB_convert_ctx);
  93. av_free(ctx);
  94. }
  95. }
  96. int Configure(void **ctxp, int argc, char *argv[])
  97. {
  98. int c;
  99. ContextInfo *ci;
  100. char *font = "LucidaSansDemiBold/16";
  101. char *fp = getenv("FONTPATH");
  102. char *color = 0;
  103. FILE *f;
  104. *ctxp = av_mallocz(sizeof(ContextInfo));
  105. ci = (ContextInfo *) *ctxp;
  106. optind = 0;
  107. if (fp)
  108. imlib_add_path_to_font_path(fp);
  109. while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
  110. switch (c) {
  111. case 'c':
  112. color = optarg;
  113. break;
  114. case 'F':
  115. font = optarg;
  116. break;
  117. case 't':
  118. ci->text = av_strdup(optarg);
  119. break;
  120. case 'f':
  121. ci->file = av_strdup(optarg);
  122. break;
  123. case 'x':
  124. ci->x = atoi(optarg);
  125. break;
  126. case 'y':
  127. ci->y = atoi(optarg);
  128. break;
  129. case '?':
  130. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  131. return -1;
  132. }
  133. }
  134. ci->fn = imlib_load_font(font);
  135. if (!ci->fn) {
  136. fprintf(stderr, "Failed to load font '%s'\n", font);
  137. return -1;
  138. }
  139. imlib_context_set_font(ci->fn);
  140. imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
  141. if (color) {
  142. char buff[256];
  143. int done = 0;
  144. f = fopen("/usr/share/X11/rgb.txt", "r");
  145. if (!f)
  146. f = fopen("/usr/lib/X11/rgb.txt", "r");
  147. if (!f) {
  148. fprintf(stderr, "Failed to find rgb.txt\n");
  149. return -1;
  150. }
  151. while (fgets(buff, sizeof(buff), f)) {
  152. int r, g, b;
  153. char colname[80];
  154. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  155. strcasecmp(colname, color) == 0) {
  156. ci->r = r;
  157. ci->g = g;
  158. ci->b = b;
  159. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  160. done = 1;
  161. break;
  162. }
  163. }
  164. fclose(f);
  165. if (!done) {
  166. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  167. return -1;
  168. }
  169. }
  170. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  171. return 0;
  172. }
  173. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  174. {
  175. CachedImage *cache;
  176. for (cache = ci->cache; cache; cache = cache->next) {
  177. if (width == cache->width && height == cache->height)
  178. return cache->image;
  179. }
  180. return NULL;
  181. }
  182. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  183. {
  184. CachedImage *cache = av_mallocz(sizeof(*cache));
  185. cache->image = image;
  186. cache->width = width;
  187. cache->height = height;
  188. cache->next = ci->cache;
  189. ci->cache = cache;
  190. }
  191. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  192. {
  193. ContextInfo *ci = (ContextInfo *) ctx;
  194. AVPicture picture1;
  195. Imlib_Image image;
  196. DATA32 *data;
  197. image = get_cached_image(ci, width, height);
  198. if (!image) {
  199. image = imlib_create_image(width, height);
  200. put_cached_image(ci, image, width, height);
  201. }
  202. imlib_context_set_image(image);
  203. data = imlib_image_get_data();
  204. avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
  205. // if we already got a SWS context, let's realloc if is not re-useable
  206. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  207. width, height, pix_fmt,
  208. width, height, PIX_FMT_RGBA32,
  209. sws_flags, NULL, NULL, NULL);
  210. if (ci->toRGB_convert_ctx == NULL) {
  211. av_log(NULL, AV_LOG_ERROR,
  212. "Cannot initialize the toRGB conversion context\n");
  213. exit(1);
  214. }
  215. // img_convert parameters are 2 first destination, then 4 source
  216. // sws_scale parameters are context, 4 first source, then 2 destination
  217. sws_scale(ci->toRGB_convert_ctx,
  218. picture->data, picture->linesize, 0, height,
  219. picture1.data, picture1.linesize);
  220. imlib_image_set_has_alpha(0);
  221. {
  222. int wid, hig, h_a, v_a;
  223. char buff[1000];
  224. char tbuff[1000];
  225. char *tbp = ci->text;
  226. time_t now = time(0);
  227. char *p, *q;
  228. int x, y;
  229. if (ci->file) {
  230. int fd = open(ci->file, O_RDONLY);
  231. if (fd < 0) {
  232. tbp = "[File not found]";
  233. } else {
  234. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  235. if (l >= 0) {
  236. tbuff[l] = 0;
  237. tbp = tbuff;
  238. } else {
  239. tbp = "[I/O Error]";
  240. }
  241. close(fd);
  242. }
  243. }
  244. strftime(buff, sizeof(buff), tbp ? tbp : "[No data]", localtime(&now));
  245. x = ci->x;
  246. y = ci->y;
  247. for (p = buff; p; p = q) {
  248. q = strchr(p, '\n');
  249. if (q)
  250. *q++ = 0;
  251. imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
  252. y += v_a;
  253. }
  254. }
  255. ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
  256. width, height, PIX_FMT_RGBA32,
  257. width, height, pix_fmt,
  258. sws_flags, NULL, NULL, NULL);
  259. if (ci->fromRGB_convert_ctx == NULL) {
  260. av_log(NULL, AV_LOG_ERROR,
  261. "Cannot initialize the fromRGB conversion context\n");
  262. exit(1);
  263. }
  264. // img_convert parameters are 2 first destination, then 4 source
  265. // sws_scale parameters are context, 4 first source, then 2 destination
  266. sws_scale(ci->fromRGB_convert_ctx,
  267. picture1.data, picture1.linesize, 0, height,
  268. picture->data, picture->linesize);
  269. }