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.

450 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 <Imlib2.h>
  99. #include "eval.h"
  100. const char *const_names[]={
  101. "PI",
  102. "E",
  103. "N", // frame number (starting at zero)
  104. "H", // frame height
  105. "W", // frame width
  106. "h", // image height
  107. "w", // image width
  108. "X", // previous x
  109. "Y", // previous y
  110. NULL
  111. };
  112. static int sws_flags = SWS_BICUBIC;
  113. typedef struct {
  114. int dummy;
  115. Imlib_Font fn;
  116. char *text;
  117. char *file;
  118. int r, g, b;
  119. double x, y;
  120. char *fileImage;
  121. struct _CachedImage *cache;
  122. Imlib_Image imageOverlaid;
  123. AVEvalExpr *eval_x, *eval_y;
  124. char *expr_x, *expr_y;
  125. int frame_number;
  126. int imageOverlaid_width, imageOverlaid_height;
  127. // This vhook first converts frame to RGB ...
  128. struct SwsContext *toRGB_convert_ctx;
  129. // ... and then converts back frame from RGB to initial format
  130. struct SwsContext *fromRGB_convert_ctx;
  131. } ContextInfo;
  132. typedef struct _CachedImage {
  133. struct _CachedImage *next;
  134. Imlib_Image image;
  135. int width;
  136. int height;
  137. } CachedImage;
  138. void Release(void *ctx)
  139. {
  140. ContextInfo *ci;
  141. ci = (ContextInfo *) ctx;
  142. if (ci->cache) {
  143. imlib_context_set_image(ci->cache->image);
  144. imlib_free_image();
  145. av_free(ci->cache);
  146. }
  147. if (ctx) {
  148. if (ci->imageOverlaid) {
  149. imlib_context_set_image(ci->imageOverlaid);
  150. imlib_free_image();
  151. }
  152. ff_eval_free(ci->expr_x);
  153. ff_eval_free(ci->expr_y);
  154. sws_freeContext(ci->toRGB_convert_ctx);
  155. sws_freeContext(ci->fromRGB_convert_ctx);
  156. av_free(ctx);
  157. }
  158. }
  159. int Configure(void **ctxp, int argc, char *argv[])
  160. {
  161. int c;
  162. ContextInfo *ci;
  163. char *font = "LucidaSansDemiBold/16";
  164. char *fp = getenv("FONTPATH");
  165. char *color = 0;
  166. FILE *f;
  167. char *p;
  168. *ctxp = av_mallocz(sizeof(ContextInfo));
  169. ci = (ContextInfo *) *ctxp;
  170. ci->x = 0.0;
  171. ci->y = 0.0;
  172. ci->expr_x = "0.0";
  173. ci->expr_y = "0.0";
  174. optind = 0;
  175. /* Use ':' to split FONTPATH */
  176. if (fp)
  177. while (p = strchr(fp, ':')) {
  178. *p = 0;
  179. imlib_add_path_to_font_path(fp);
  180. fp = p + 1;
  181. }
  182. if ((fp) && (*fp))
  183. imlib_add_path_to_font_path(fp);
  184. while ((c = getopt(argc, argv, "c:f:F:t:x:y:i:")) > 0) {
  185. switch (c) {
  186. case 'c':
  187. color = optarg;
  188. break;
  189. case 'F':
  190. font = optarg;
  191. break;
  192. case 't':
  193. ci->text = av_strdup(optarg);
  194. break;
  195. case 'f':
  196. ci->file = av_strdup(optarg);
  197. break;
  198. case 'x':
  199. ci->expr_x = av_strdup(optarg);
  200. break;
  201. case 'y':
  202. ci->expr_y = av_strdup(optarg);
  203. break;
  204. case 'i':
  205. ci->fileImage = av_strdup(optarg);
  206. break;
  207. case '?':
  208. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  209. return -1;
  210. }
  211. }
  212. if (ci->text || ci->file) {
  213. ci->fn = imlib_load_font(font);
  214. if (!ci->fn) {
  215. fprintf(stderr, "Failed to load font '%s'\n", font);
  216. return -1;
  217. }
  218. imlib_context_set_font(ci->fn);
  219. imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
  220. }
  221. if (color) {
  222. char buff[256];
  223. int done = 0;
  224. f = fopen("/usr/share/X11/rgb.txt", "r");
  225. if (!f)
  226. f = fopen("/usr/lib/X11/rgb.txt", "r");
  227. if (!f) {
  228. fprintf(stderr, "Failed to find rgb.txt\n");
  229. return -1;
  230. }
  231. while (fgets(buff, sizeof(buff), f)) {
  232. int r, g, b;
  233. char colname[80];
  234. if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
  235. strcasecmp(colname, color) == 0) {
  236. ci->r = r;
  237. ci->g = g;
  238. ci->b = b;
  239. /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
  240. done = 1;
  241. break;
  242. }
  243. }
  244. fclose(f);
  245. if (!done) {
  246. fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
  247. return -1;
  248. }
  249. }
  250. imlib_context_set_color(ci->r, ci->g, ci->b, 255);
  251. /* load the image (for example, credits for a movie) */
  252. if (ci->fileImage) {
  253. ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
  254. if (!(ci->imageOverlaid)){
  255. av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
  256. return -1;
  257. }
  258. imlib_context_set_image(ci->imageOverlaid);
  259. ci->imageOverlaid_width = imlib_image_get_width();
  260. ci->imageOverlaid_height = imlib_image_get_height();
  261. }
  262. if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, NULL))){
  263. av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s'\n", ci->expr_x);
  264. return -1;
  265. }
  266. if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, NULL))){
  267. av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s'\n", ci->expr_y);
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
  273. {
  274. CachedImage *cache;
  275. for (cache = ci->cache; cache; cache = cache->next) {
  276. if (width == cache->width && height == cache->height)
  277. return cache->image;
  278. }
  279. return NULL;
  280. }
  281. static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
  282. {
  283. CachedImage *cache = av_mallocz(sizeof(*cache));
  284. cache->image = image;
  285. cache->width = width;
  286. cache->height = height;
  287. cache->next = ci->cache;
  288. ci->cache = cache;
  289. }
  290. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  291. {
  292. ContextInfo *ci = (ContextInfo *) ctx;
  293. AVPicture picture1;
  294. Imlib_Image image;
  295. DATA32 *data;
  296. image = get_cached_image(ci, width, height);
  297. if (!image) {
  298. image = imlib_create_image(width, height);
  299. put_cached_image(ci, image, width, height);
  300. }
  301. imlib_context_set_image(image);
  302. data = imlib_image_get_data();
  303. avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);
  304. // if we already got a SWS context, let's realloc if is not re-useable
  305. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  306. width, height, pix_fmt,
  307. width, height, PIX_FMT_RGB32,
  308. sws_flags, NULL, NULL, NULL);
  309. if (ci->toRGB_convert_ctx == NULL) {
  310. av_log(NULL, AV_LOG_ERROR,
  311. "Cannot initialize the toRGB conversion context\n");
  312. exit(1);
  313. }
  314. // img_convert parameters are 2 first destination, then 4 source
  315. // sws_scale parameters are context, 4 first source, then 2 destination
  316. sws_scale(ci->toRGB_convert_ctx,
  317. picture->data, picture->linesize, 0, height,
  318. picture1.data, picture1.linesize);
  319. imlib_image_set_has_alpha(0);
  320. {
  321. int wid, hig, h_a, v_a;
  322. char buff[1000];
  323. char tbuff[1000];
  324. char *tbp = ci->text;
  325. time_t now = time(0);
  326. char *p, *q;
  327. int y;
  328. double const_values[]={
  329. M_PI,
  330. M_E,
  331. ci->frame_number, // frame number (starting at zero)
  332. height, // frame height
  333. width, // frame width
  334. ci->imageOverlaid_height, // image height
  335. ci->imageOverlaid_width, // image width
  336. ci->x, // previous x
  337. ci->y, // previous y
  338. 0
  339. };
  340. if (ci->file) {
  341. int fd = open(ci->file, O_RDONLY);
  342. if (fd < 0) {
  343. tbp = "[File not found]";
  344. } else {
  345. int l = read(fd, tbuff, sizeof(tbuff) - 1);
  346. if (l >= 0) {
  347. tbuff[l] = 0;
  348. tbp = tbuff;
  349. } else {
  350. tbp = "[I/O Error]";
  351. }
  352. close(fd);
  353. }
  354. }
  355. if (tbp)
  356. strftime(buff, sizeof(buff), tbp, localtime(&now));
  357. else if (!(ci->imageOverlaid))
  358. strftime(buff, sizeof(buff), "[No data]", localtime(&now));
  359. ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
  360. ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
  361. y = ci->y;
  362. if (!(ci->imageOverlaid))
  363. for (p = buff; p; p = q) {
  364. q = strchr(p, '\n');
  365. if (q)
  366. *q++ = 0;
  367. imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
  368. y += v_a;
  369. }
  370. if (ci->imageOverlaid) {
  371. imlib_context_set_image(image);
  372. imlib_blend_image_onto_image(ci->imageOverlaid, 0,
  373. 0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
  374. ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
  375. }
  376. }
  377. ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
  378. width, height, PIX_FMT_RGB32,
  379. width, height, pix_fmt,
  380. sws_flags, NULL, NULL, NULL);
  381. if (ci->fromRGB_convert_ctx == NULL) {
  382. av_log(NULL, AV_LOG_ERROR,
  383. "Cannot initialize the fromRGB conversion context\n");
  384. exit(1);
  385. }
  386. // img_convert parameters are 2 first destination, then 4 source
  387. // sws_scale parameters are context, 4 first source, then 2 destination
  388. sws_scale(ci->fromRGB_convert_ctx,
  389. picture1.data, picture1.linesize, 0, height,
  390. picture->data, picture->linesize);
  391. ci->frame_number++;
  392. }