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.

518 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->expr_x);
  163. ff_eval_free(ci->expr_y);
  164. ff_eval_free(ci->expr_R);
  165. ff_eval_free(ci->expr_G);
  166. ff_eval_free(ci->expr_B);
  167. sws_freeContext(ci->toRGB_convert_ctx);
  168. sws_freeContext(ci->fromRGB_convert_ctx);
  169. av_free(ctx);
  170. }
  171. }
  172. int Configure(void **ctxp, int argc, char *argv[])
  173. {
  174. int c;
  175. ContextInfo *ci;
  176. char *rgbtxt = 0;
  177. char *font = "LucidaSansDemiBold/16";
  178. char *fp = getenv("FONTPATH");
  179. char *color = 0;
  180. FILE *f;
  181. char *p;
  182. *ctxp = av_mallocz(sizeof(ContextInfo));
  183. ci = (ContextInfo *) *ctxp;
  184. ci->x = 0.0;
  185. ci->y = 0.0;
  186. ci->expr_x = "0.0";
  187. ci->expr_y = "0.0";
  188. optind = 0;
  189. /* Use ':' to split FONTPATH */
  190. if (fp)
  191. while (p = strchr(fp, ':')) {
  192. *p = 0;
  193. imlib_add_path_to_font_path(fp);
  194. fp = p + 1;
  195. }
  196. if ((fp) && (*fp))
  197. imlib_add_path_to_font_path(fp);
  198. while ((c = getopt(argc, argv, "R:G:B:C:c:f:F:t:x:y:i:")) > 0) {
  199. switch (c) {
  200. case 'R':
  201. ci->expr_R = av_strdup(optarg);
  202. ci->eval_colors = 1;
  203. break;
  204. case 'G':
  205. ci->expr_G = av_strdup(optarg);
  206. ci->eval_colors = 1;
  207. break;
  208. case 'B':
  209. ci->expr_B = av_strdup(optarg);
  210. ci->eval_colors = 1;
  211. break;
  212. case 'C':
  213. rgbtxt = optarg;
  214. break;
  215. case 'c':
  216. color = optarg;
  217. break;
  218. case 'F':
  219. font = optarg;
  220. break;
  221. case 't':
  222. ci->text = av_strdup(optarg);
  223. break;
  224. case 'f':
  225. ci->file = av_strdup(optarg);
  226. break;
  227. case 'x':
  228. ci->expr_x = av_strdup(optarg);
  229. break;
  230. case 'y':
  231. ci->expr_y = av_strdup(optarg);
  232. break;
  233. case 'i':
  234. ci->fileImage = av_strdup(optarg);
  235. break;
  236. case '?':
  237. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  238. return -1;
  239. }
  240. }
  241. if (ci->eval_colors && !(ci->expr_R && ci->expr_G && ci->expr_B))
  242. {
  243. fprintf(stderr, "You must specify expressions for all or no colors.\n");
  244. return -1;
  245. }
  246. if (ci->text || ci->file) {
  247. ci->fn = imlib_load_font(font);
  248. if (!ci->fn) {
  249. fprintf(stderr, "Failed to load font '%s'\n", font);
  250. return -1;
  251. }
  252. imlib_context_set_font(ci->fn);
  253. imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
  254. }
  255. if (color) {
  256. char buff[256];
  257. int done = 0;
  258. if (ci->eval_colors)
  259. {
  260. fprintf(stderr, "You must not specify both a color name and expressions for the colors.\n");
  261. return -1;
  262. }
  263. if (rgbtxt)
  264. f = fopen(rgbtxt, "r");
  265. else
  266. {
  267. f = fopen("/usr/share/X11/rgb.txt", "r");
  268. if (!f)
  269. f = fopen("/usr/lib/X11/rgb.txt", "r");
  270. }
  271. if (!f) {
  272. fprintf(stderr, "Failed to find RGB color names file\n");
  273. return -1;
  274. }
  275. while (fgets(buff, sizeof(buff), f)) {
  276. int r, g, b;
  277. char colname[80];
  278. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  279. strcasecmp(colname, color) == 0) {
  280. ci->r = r;
  281. ci->g = g;
  282. ci->b = b;
  283. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  284. done = 1;
  285. break;
  286. }
  287. }
  288. fclose(f);
  289. if (!done) {
  290. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  291. return -1;
  292. }
  293. } else if (ci->eval_colors) {
  294. if (!(ci->eval_r = ff_parse(ci->expr_R, const_names, NULL, NULL, NULL, NULL, NULL))){
  295. av_log(NULL, AV_LOG_ERROR, "Couldn't parse R expression '%s'\n", ci->expr_R);
  296. return -1;
  297. }
  298. if (!(ci->eval_g = ff_parse(ci->expr_G, const_names, NULL, NULL, NULL, NULL, NULL))){
  299. av_log(NULL, AV_LOG_ERROR, "Couldn't parse G expression '%s'\n", ci->expr_G);
  300. return -1;
  301. }
  302. if (!(ci->eval_b = ff_parse(ci->expr_B, const_names, NULL, NULL, NULL, NULL, NULL))){
  303. av_log(NULL, AV_LOG_ERROR, "Couldn't parse B expression '%s'\n", ci->expr_B);
  304. return -1;
  305. }
  306. }
  307. if (!ci->eval_colors)
  308. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  309. /* load the image (for example, credits for a movie) */
  310. if (ci->fileImage) {
  311. ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
  312. if (!(ci->imageOverlaid)){
  313. av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
  314. return -1;
  315. }
  316. imlib_context_set_image(ci->imageOverlaid);
  317. ci->imageOverlaid_width = imlib_image_get_width();
  318. ci->imageOverlaid_height = imlib_image_get_height();
  319. }
  320. if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, NULL))){
  321. av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s'\n", ci->expr_x);
  322. return -1;
  323. }
  324. if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, NULL))){
  325. av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s'\n", ci->expr_y);
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  331. {
  332. CachedImage *cache;
  333. for (cache = ci->cache; cache; cache = cache->next) {
  334. if (width == cache->width && height == cache->height)
  335. return cache->image;
  336. }
  337. return NULL;
  338. }
  339. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  340. {
  341. CachedImage *cache = av_mallocz(sizeof(*cache));
  342. cache->image = image;
  343. cache->width = width;
  344. cache->height = height;
  345. cache->next = ci->cache;
  346. ci->cache = cache;
  347. }
  348. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  349. {
  350. ContextInfo *ci = (ContextInfo *) ctx;
  351. AVPicture picture1;
  352. Imlib_Image image;
  353. DATA32 *data;
  354. image = get_cached_image(ci, width, height);
  355. if (!image) {
  356. image = imlib_create_image(width, height);
  357. put_cached_image(ci, image, width, height);
  358. }
  359. imlib_context_set_image(image);
  360. data = imlib_image_get_data();
  361. avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);
  362. // if we already got a SWS context, let's realloc if is not re-useable
  363. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  364. width, height, pix_fmt,
  365. width, height, PIX_FMT_RGB32,
  366. sws_flags, NULL, NULL, NULL);
  367. if (ci->toRGB_convert_ctx == NULL) {
  368. av_log(NULL, AV_LOG_ERROR,
  369. "Cannot initialize the toRGB conversion context\n");
  370. return;
  371. }
  372. // img_convert parameters are 2 first destination, then 4 source
  373. // sws_scale parameters are context, 4 first source, then 2 destination
  374. sws_scale(ci->toRGB_convert_ctx,
  375. picture->data, picture->linesize, 0, height,
  376. picture1.data, picture1.linesize);
  377. imlib_image_set_has_alpha(0);
  378. {
  379. int wid, hig, h_a, v_a;
  380. char buff[1000];
  381. char tbuff[1000];
  382. char *tbp = ci->text;
  383. time_t now = time(0);
  384. char *p, *q;
  385. int y;
  386. double const_values[]={
  387. M_PI,
  388. M_E,
  389. ci->frame_number, // frame number (starting at zero)
  390. height, // frame height
  391. width, // frame width
  392. ci->imageOverlaid_height, // image height
  393. ci->imageOverlaid_width, // image width
  394. ci->x, // previous x
  395. ci->y, // previous y
  396. 0
  397. };
  398. if (ci->file) {
  399. int fd = open(ci->file, O_RDONLY);
  400. if (fd < 0) {
  401. tbp = "[File not found]";
  402. } else {
  403. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  404. if (l >= 0) {
  405. tbuff[l] = 0;
  406. tbp = tbuff;
  407. } else {
  408. tbp = "[I/O Error]";
  409. }
  410. close(fd);
  411. }
  412. }
  413. if (tbp)
  414. strftime(buff, sizeof(buff), tbp, localtime(&now));
  415. else if (!(ci->imageOverlaid))
  416. strftime(buff, sizeof(buff), "[No data]", localtime(&now));
  417. ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
  418. ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
  419. y = ci->y;
  420. if (ci->eval_colors) {
  421. ci->r = ff_parse_eval(ci->eval_r, const_values, ci);
  422. ci->g = ff_parse_eval(ci->eval_g, const_values, ci);
  423. ci->b = ff_parse_eval(ci->eval_b, const_values, ci);
  424. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  425. }
  426. if (!(ci->imageOverlaid))
  427. for (p = buff; p; p = q) {
  428. q = strchr(p, '\n');
  429. if (q)
  430. *q++ = 0;
  431. imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
  432. y += v_a;
  433. }
  434. if (ci->imageOverlaid) {
  435. imlib_context_set_image(image);
  436. imlib_blend_image_onto_image(ci->imageOverlaid, 0,
  437. 0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
  438. ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
  439. }
  440. }
  441. ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
  442. width, height, PIX_FMT_RGB32,
  443. width, height, pix_fmt,
  444. sws_flags, NULL, NULL, NULL);
  445. if (ci->fromRGB_convert_ctx == NULL) {
  446. av_log(NULL, AV_LOG_ERROR,
  447. "Cannot initialize the fromRGB conversion context\n");
  448. return;
  449. }
  450. // img_convert parameters are 2 first destination, then 4 source
  451. // sws_scale parameters are context, 4 first source, then 2 destination
  452. sws_scale(ci->fromRGB_convert_ctx,
  453. picture1.data, picture1.linesize, 0, height,
  454. picture->data, picture->linesize);
  455. ci->frame_number++;
  456. }