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