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.

524 lines
16KB

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