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.

451 lines
14KB

  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. * You may also overlay an image (even semi-transparent) like TV stations do.
  11. * You may move either the text or the image around your video to create
  12. * scrolling credits, for example.
  13. *
  14. * Text fonts are being looked for in FONTPATH
  15. *
  16. * Options:
  17. *
  18. * -c <color> The color of the text
  19. * -F <fontname> The font face and size
  20. * -t <text> The text
  21. * -f <filename> The filename to read text from
  22. * -x <expresion> X coordinate of text or image
  23. * -y <expresion> Y coordinate of text or image
  24. * -i <filename> The filename to read a image from
  25. *
  26. * Expresions are functions of:
  27. * N // frame number (starting at zero)
  28. * H // frame height
  29. * W // frame width
  30. * h // image height
  31. * w // image width
  32. * X // previous x
  33. * Y // previous y
  34. *
  35. Examples:
  36. FONTPATH="/cygdrive/c/WINDOWS/Fonts/"
  37. FONTPATH="$FONTPATH:/usr/share/imlib2/data/fonts/"
  38. FONTPATH="$FONTPATH:/usr/X11R6/lib/X11/fonts/TTF/"
  39. export FONTPATH
  40. ffmpeg -i input.avi -vhook \
  41. 'vhook/imlib2.dll -x W*(0.5+0.25*sin(N/47*PI))-w/2 -y H*(0.5+0.50*cos(N/97*PI))-h/2 -i /usr/share/imlib2/data/images/bulb.png'
  42. -acodec copy -sameq output.avi
  43. ffmpeg -i input.avi -vhook \
  44. 'vhook/imlib2.dll -c red -F Vera.ttf/20 -x 150+0.5*N -y 70+0.25*N -t Hello'
  45. -acodec copy -sameq output.avi
  46. * This module is very much intended as an example of what could be done.
  47. *
  48. * One caution is that this is an expensive process -- in particular the
  49. * conversion of the image into RGB and back is time consuming. For some
  50. * special cases -- e.g. painting black text -- it would be faster to paint
  51. * the text into a bitmap and then combine it directly into the YUV
  52. * image. However, this code is fast enough to handle 10 fps of 320x240 on a
  53. * 900MHz Duron in maybe 15% of the CPU.
  54. * See further statistics on Pentium4, 3GHz, FFMpeg is SVN-r6798
  55. * Input movie is 20.2 seconds of PAL DV on AVI
  56. * Output movie is DVD compliant VOB.
  57. *
  58. ffmpeg -i input.avi -target pal-dvd out.vob
  59. # 13.516s just transcode
  60. ffmpeg -i input.avi -vhook /usr/local/bin/vhook/null.dll -target pal-dvd out.vob
  61. # 23.546s transcode and img_convert
  62. ffmpeg -i input.avi -vhook \
  63. 'vhook/imlib2.dll -c red -F Vera/20 -x 150-0.5*N -y 70+0.25*N -t Hello_person' \
  64. -target pal-dvd out.vob
  65. # 21.454s transcode, img_convert and move text around
  66. ffmpeg -i input.avi -vhook \
  67. 'vhook/imlib2.dll -x 150-0.5*N -y 70+0.25*N -i /usr/share/imlib2/data/images/bulb.png' \
  68. -target pal-dvd out.vob
  69. # 20.828s transcode, img_convert and move image around
  70. *
  71. * This file is part of FFmpeg.
  72. *
  73. * FFmpeg is free software; you can redistribute it and/or
  74. * modify it under the terms of the GNU Lesser General Public
  75. * License as published by the Free Software Foundation; either
  76. * version 2.1 of the License, or (at your option) any later version.
  77. *
  78. * FFmpeg is distributed in the hope that it will be useful,
  79. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  80. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  81. * Lesser General Public License for more details.
  82. *
  83. * You should have received a copy of the GNU Lesser General Public
  84. * License along with FFmpeg; if not, write to the Free Software
  85. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  86. */
  87. #include "framehook.h"
  88. #include "swscale.h"
  89. #include <stdio.h>
  90. #include <stdlib.h>
  91. #include <fcntl.h>
  92. #include <stdarg.h>
  93. #include <string.h>
  94. #include <unistd.h>
  95. #undef time
  96. #include <sys/time.h>
  97. #include <time.h>
  98. #include <X11/Xlib.h>
  99. #include <Imlib2.h>
  100. #include "eval.h"
  101. const char *const_names[]={
  102. "PI",
  103. "E",
  104. "N", // frame number (starting at zero)
  105. "H", // frame height
  106. "W", // frame width
  107. "h", // image height
  108. "w", // image width
  109. "X", // previous x
  110. "Y", // previous y
  111. NULL
  112. };
  113. static int sws_flags = SWS_BICUBIC;
  114. typedef struct {
  115. int dummy;
  116. Imlib_Font fn;
  117. char *text;
  118. char *file;
  119. int r, g, b;
  120. double x, y;
  121. char *fileImage;
  122. struct _CachedImage *cache;
  123. Imlib_Image imageOverlaid;
  124. AVEvalExpr *eval_x, *eval_y;
  125. char *expr_x, *expr_y;
  126. int frame_number;
  127. int imageOverlaid_width, imageOverlaid_height;
  128. // This vhook first converts frame to RGB ...
  129. struct SwsContext *toRGB_convert_ctx;
  130. // ... and then converts back frame from RGB to initial format
  131. struct SwsContext *fromRGB_convert_ctx;
  132. } ContextInfo;
  133. typedef struct _CachedImage {
  134. struct _CachedImage *next;
  135. Imlib_Image image;
  136. int width;
  137. int height;
  138. } CachedImage;
  139. void Release(void *ctx)
  140. {
  141. ContextInfo *ci;
  142. ci = (ContextInfo *) ctx;
  143. if (ci->cache) {
  144. imlib_context_set_image(ci->cache->image);
  145. imlib_free_image();
  146. av_free(ci->cache);
  147. }
  148. if (ctx) {
  149. if (ci->imageOverlaid) {
  150. imlib_context_set_image(ci->imageOverlaid);
  151. imlib_free_image();
  152. }
  153. ff_eval_free(ci->expr_x);
  154. ff_eval_free(ci->expr_y);
  155. sws_freeContext(ci->toRGB_convert_ctx);
  156. sws_freeContext(ci->fromRGB_convert_ctx);
  157. av_free(ctx);
  158. }
  159. }
  160. int Configure(void **ctxp, int argc, char *argv[])
  161. {
  162. int c;
  163. ContextInfo *ci;
  164. char *font = "LucidaSansDemiBold/16";
  165. char *fp = getenv("FONTPATH");
  166. char *color = 0;
  167. FILE *f;
  168. char *p;
  169. *ctxp = av_mallocz(sizeof(ContextInfo));
  170. ci = (ContextInfo *) *ctxp;
  171. ci->x = 0.0;
  172. ci->y = 0.0;
  173. ci->expr_x = "0.0";
  174. ci->expr_y = "0.0";
  175. optind = 0;
  176. /* Use ':' to split FONTPATH */
  177. if (fp)
  178. while (p = strchr(fp, ':')) {
  179. *p = 0;
  180. imlib_add_path_to_font_path(fp);
  181. fp = p + 1;
  182. }
  183. if ((fp) && (*fp))
  184. imlib_add_path_to_font_path(fp);
  185. while ((c = getopt(argc, argv, "c:f:F:t:x:y:i:")) > 0) {
  186. switch (c) {
  187. case 'c':
  188. color = optarg;
  189. break;
  190. case 'F':
  191. font = optarg;
  192. break;
  193. case 't':
  194. ci->text = av_strdup(optarg);
  195. break;
  196. case 'f':
  197. ci->file = av_strdup(optarg);
  198. break;
  199. case 'x':
  200. ci->expr_x = av_strdup(optarg);
  201. break;
  202. case 'y':
  203. ci->expr_y = av_strdup(optarg);
  204. break;
  205. case 'i':
  206. ci->fileImage = av_strdup(optarg);
  207. break;
  208. case '?':
  209. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  210. return -1;
  211. }
  212. }
  213. if (ci->text || ci->file) {
  214. ci->fn = imlib_load_font(font);
  215. if (!ci->fn) {
  216. fprintf(stderr, "Failed to load font '%s'\n", font);
  217. return -1;
  218. }
  219. imlib_context_set_font(ci->fn);
  220. imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
  221. }
  222. if (color) {
  223. char buff[256];
  224. int done = 0;
  225. f = fopen("/usr/share/X11/rgb.txt", "r");
  226. if (!f)
  227. f = fopen("/usr/lib/X11/rgb.txt", "r");
  228. if (!f) {
  229. fprintf(stderr, "Failed to find rgb.txt\n");
  230. return -1;
  231. }
  232. while (fgets(buff, sizeof(buff), f)) {
  233. int r, g, b;
  234. char colname[80];
  235. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  236. strcasecmp(colname, color) == 0) {
  237. ci->r = r;
  238. ci->g = g;
  239. ci->b = b;
  240. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  241. done = 1;
  242. break;
  243. }
  244. }
  245. fclose(f);
  246. if (!done) {
  247. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  248. return -1;
  249. }
  250. }
  251. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  252. /* load the image (for example, credits for a movie) */
  253. if (ci->fileImage) {
  254. ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
  255. if (!(ci->imageOverlaid)){
  256. av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
  257. return -1;
  258. }
  259. imlib_context_set_image(ci->imageOverlaid);
  260. ci->imageOverlaid_width = imlib_image_get_width();
  261. ci->imageOverlaid_height = imlib_image_get_height();
  262. }
  263. if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, NULL))){
  264. av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s'\n", ci->expr_x);
  265. return -1;
  266. }
  267. if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, NULL))){
  268. av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s'\n", ci->expr_y);
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  274. {
  275. CachedImage *cache;
  276. for (cache = ci->cache; cache; cache = cache->next) {
  277. if (width == cache->width && height == cache->height)
  278. return cache->image;
  279. }
  280. return NULL;
  281. }
  282. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  283. {
  284. CachedImage *cache = av_mallocz(sizeof(*cache));
  285. cache->image = image;
  286. cache->width = width;
  287. cache->height = height;
  288. cache->next = ci->cache;
  289. ci->cache = cache;
  290. }
  291. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  292. {
  293. ContextInfo *ci = (ContextInfo *) ctx;
  294. AVPicture picture1;
  295. Imlib_Image image;
  296. DATA32 *data;
  297. image = get_cached_image(ci, width, height);
  298. if (!image) {
  299. image = imlib_create_image(width, height);
  300. put_cached_image(ci, image, width, height);
  301. }
  302. imlib_context_set_image(image);
  303. data = imlib_image_get_data();
  304. avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
  305. // if we already got a SWS context, let's realloc if is not re-useable
  306. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  307. width, height, pix_fmt,
  308. width, height, PIX_FMT_RGBA32,
  309. sws_flags, NULL, NULL, NULL);
  310. if (ci->toRGB_convert_ctx == NULL) {
  311. av_log(NULL, AV_LOG_ERROR,
  312. "Cannot initialize the toRGB conversion context\n");
  313. exit(1);
  314. }
  315. // img_convert parameters are 2 first destination, then 4 source
  316. // sws_scale parameters are context, 4 first source, then 2 destination
  317. sws_scale(ci->toRGB_convert_ctx,
  318. picture->data, picture->linesize, 0, height,
  319. picture1.data, picture1.linesize);
  320. imlib_image_set_has_alpha(0);
  321. {
  322. int wid, hig, h_a, v_a;
  323. char buff[1000];
  324. char tbuff[1000];
  325. char *tbp = ci->text;
  326. time_t now = time(0);
  327. char *p, *q;
  328. int y;
  329. double const_values[]={
  330. M_PI,
  331. M_E,
  332. ci->frame_number, // frame number (starting at zero)
  333. height, // frame height
  334. width, // frame width
  335. ci->imageOverlaid_height, // image height
  336. ci->imageOverlaid_width, // image width
  337. ci->x, // previous x
  338. ci->y, // previous y
  339. 0
  340. };
  341. if (ci->file) {
  342. int fd = open(ci->file, O_RDONLY);
  343. if (fd < 0) {
  344. tbp = "[File not found]";
  345. } else {
  346. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  347. if (l >= 0) {
  348. tbuff[l] = 0;
  349. tbp = tbuff;
  350. } else {
  351. tbp = "[I/O Error]";
  352. }
  353. close(fd);
  354. }
  355. }
  356. if (tbp)
  357. strftime(buff, sizeof(buff), tbp, localtime(&now));
  358. else if (!(ci->imageOverlaid))
  359. strftime(buff, sizeof(buff), "[No data]", localtime(&now));
  360. ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
  361. ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
  362. y = ci->y;
  363. if (!(ci->imageOverlaid))
  364. for (p = buff; p; p = q) {
  365. q = strchr(p, '\n');
  366. if (q)
  367. *q++ = 0;
  368. imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
  369. y += v_a;
  370. }
  371. if (ci->imageOverlaid) {
  372. imlib_context_set_image(image);
  373. imlib_blend_image_onto_image(ci->imageOverlaid, 0,
  374. 0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
  375. ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
  376. }
  377. }
  378. ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
  379. width, height, PIX_FMT_RGBA32,
  380. width, height, pix_fmt,
  381. sws_flags, NULL, NULL, NULL);
  382. if (ci->fromRGB_convert_ctx == NULL) {
  383. av_log(NULL, AV_LOG_ERROR,
  384. "Cannot initialize the fromRGB conversion context\n");
  385. exit(1);
  386. }
  387. // img_convert parameters are 2 first destination, then 4 source
  388. // sws_scale parameters are context, 4 first source, then 2 destination
  389. sws_scale(ci->fromRGB_convert_ctx,
  390. picture1.data, picture1.linesize, 0, height,
  391. picture->data, picture->linesize);
  392. ci->frame_number++;
  393. }