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