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.

547 lines
17KB

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