Set error parameter for ff_parse
[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 char *error;
208
209 *ctxp = av_mallocz(sizeof(ContextInfo));
210 ci = (ContextInfo *) *ctxp;
211
212 ci->x = 0.0;
213 ci->y = 0.0;
214 ci->expr_x = "0.0";
215 ci->expr_y = "0.0";
216
217 optind = 0;
218
219 /* Use ':' to split FONTPATH */
220 if (fp)
221 while (p = strchr(fp, ':')) {
222 *p = 0;
223 imlib_add_path_to_font_path(fp);
224 fp = p + 1;
225 }
226 if ((fp) && (*fp))
227 imlib_add_path_to_font_path(fp);
228
229
230 while ((c = getopt(argc, argv, "R:G:B:A:C:c:f:F:t:x:y:i:")) > 0) {
231 switch (c) {
232 case 'R':
233 ci->expr_R = av_strdup(optarg);
234 ci->eval_colors = 1;
235 break;
236 case 'G':
237 ci->expr_G = av_strdup(optarg);
238 ci->eval_colors = 1;
239 break;
240 case 'B':
241 ci->expr_B = av_strdup(optarg);
242 ci->eval_colors = 1;
243 break;
244 case 'A':
245 ci->expr_A = av_strdup(optarg);
246 break;
247 case 'C':
248 rgbtxt = optarg;
249 break;
250 case 'c':
251 color = optarg;
252 break;
253 case 'F':
254 font = optarg;
255 break;
256 case 't':
257 ci->text = av_strdup(optarg);
258 break;
259 case 'f':
260 ci->file = av_strdup(optarg);
261 break;
262 case 'x':
263 ci->expr_x = av_strdup(optarg);
264 break;
265 case 'y':
266 ci->expr_y = av_strdup(optarg);
267 break;
268 case 'i':
269 ci->fileImage = av_strdup(optarg);
270 break;
271 case '?':
272 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
273 return -1;
274 }
275 }
276
277 if (ci->eval_colors && !(ci->expr_R && ci->expr_G && ci->expr_B))
278 {
279 fprintf(stderr, "You must specify expressions for all or no colors.\n");
280 return -1;
281 }
282
283 if (ci->text || ci->file) {
284 ci->fn = imlib_load_font(font);
285 if (!ci->fn) {
286 fprintf(stderr, "Failed to load font '%s'\n", font);
287 return -1;
288 }
289 imlib_context_set_font(ci->fn);
290 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
291 }
292
293 if (color) {
294 char buff[256];
295 int done = 0;
296
297 if (ci->eval_colors)
298 {
299 fprintf(stderr, "You must not specify both a color name and expressions for the colors.\n");
300 return -1;
301 }
302
303 if (rgbtxt)
304 f = fopen(rgbtxt, "r");
305 else
306 {
307 f = fopen("/usr/share/X11/rgb.txt", "r");
308 if (!f)
309 f = fopen("/usr/lib/X11/rgb.txt", "r");
310 }
311 if (!f) {
312 fprintf(stderr, "Failed to find RGB color names file\n");
313 return -1;
314 }
315 while (fgets(buff, sizeof(buff), f)) {
316 int r, g, b;
317 char colname[80];
318
319 if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
320 strcasecmp(colname, color) == 0) {
321 ci->r = r;
322 ci->g = g;
323 ci->b = b;
324 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
325 done = 1;
326 break;
327 }
328 }
329 fclose(f);
330 if (!done) {
331 fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
332 return -1;
333 }
334 } else if (ci->eval_colors) {
335 if (!(ci->eval_r = ff_parse(ci->expr_R, const_names, NULL, NULL, NULL, NULL, &error))){
336 av_log(NULL, AV_LOG_ERROR, "Couldn't parse R expression '%s': %s\n", ci->expr_R, error);
337 return -1;
338 }
339 if (!(ci->eval_g = ff_parse(ci->expr_G, const_names, NULL, NULL, NULL, NULL, &error))){
340 av_log(NULL, AV_LOG_ERROR, "Couldn't parse G expression '%s': %s\n", ci->expr_G, error);
341 return -1;
342 }
343 if (!(ci->eval_b = ff_parse(ci->expr_B, const_names, NULL, NULL, NULL, NULL, &error))){
344 av_log(NULL, AV_LOG_ERROR, "Couldn't parse B expression '%s': %s\n", ci->expr_B, error);
345 return -1;
346 }
347 }
348
349 if (ci->expr_A) {
350 if (!(ci->eval_a = ff_parse(ci->expr_A, const_names, NULL, NULL, NULL, NULL, &error))){
351 av_log(NULL, AV_LOG_ERROR, "Couldn't parse A expression '%s': %s\n", ci->expr_A, error);
352 return -1;
353 }
354 } else {
355 ci->a = 255;
356 }
357
358 if (!(ci->eval_colors || ci->eval_a))
359 imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
360
361 /* load the image (for example, credits for a movie) */
362 if (ci->fileImage) {
363 ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
364 if (!(ci->imageOverlaid)){
365 av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
366 return -1;
367 }
368 imlib_context_set_image(ci->imageOverlaid);
369 ci->imageOverlaid_width = imlib_image_get_width();
370 ci->imageOverlaid_height = imlib_image_get_height();
371 }
372
373 if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, &error))){
374 av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s': %s\n", ci->expr_x, error);
375 return -1;
376 }
377
378 if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, &error))){
379 av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s': %s\n", ci->expr_y, error);
380 return -1;
381 }
382
383 return 0;
384 }
385
386 static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
387 {
388 CachedImage *cache;
389
390 for (cache = ci->cache; cache; cache = cache->next) {
391 if (width == cache->width && height == cache->height)
392 return cache->image;
393 }
394
395 return NULL;
396 }
397
398 static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
399 {
400 CachedImage *cache = av_mallocz(sizeof(*cache));
401
402 cache->image = image;
403 cache->width = width;
404 cache->height = height;
405 cache->next = ci->cache;
406 ci->cache = cache;
407 }
408
409 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
410 {
411 ContextInfo *ci = (ContextInfo *) ctx;
412 AVPicture picture1;
413 Imlib_Image image;
414 DATA32 *data;
415
416 image = get_cached_image(ci, width, height);
417
418 if (!image) {
419 image = imlib_create_image(width, height);
420 put_cached_image(ci, image, width, height);
421 }
422
423 imlib_context_set_image(image);
424 data = imlib_image_get_data();
425
426 avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);
427
428 // if we already got a SWS context, let's realloc if is not re-useable
429 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
430 width, height, pix_fmt,
431 width, height, PIX_FMT_RGB32,
432 sws_flags, NULL, NULL, NULL);
433 if (ci->toRGB_convert_ctx == NULL) {
434 av_log(NULL, AV_LOG_ERROR,
435 "Cannot initialize the toRGB conversion context\n");
436 return;
437 }
438
439 // img_convert parameters are 2 first destination, then 4 source
440 // sws_scale parameters are context, 4 first source, then 2 destination
441 sws_scale(ci->toRGB_convert_ctx,
442 picture->data, picture->linesize, 0, height,
443 picture1.data, picture1.linesize);
444
445 imlib_image_set_has_alpha(0);
446
447 {
448 int wid, hig, h_a, v_a;
449 char buff[1000];
450 char tbuff[1000];
451 char *tbp = ci->text;
452 time_t now = time(0);
453 char *p, *q;
454 int y;
455
456 double const_values[]={
457 M_PI,
458 M_E,
459 ci->frame_number, // frame number (starting at zero)
460 height, // frame height
461 width, // frame width
462 ci->imageOverlaid_height, // image height
463 ci->imageOverlaid_width, // image width
464 ci->x, // previous x
465 ci->y, // previous y
466 0
467 };
468
469 if (ci->file) {
470 int fd = open(ci->file, O_RDONLY);
471
472 if (fd < 0) {
473 tbp = "[File not found]";
474 } else {
475 int l = read(fd, tbuff, sizeof(tbuff) - 1);
476
477 if (l >= 0) {
478 tbuff[l] = 0;
479 tbp = tbuff;
480 } else {
481 tbp = "[I/O Error]";
482 }
483 close(fd);
484 }
485 }
486
487 if (tbp)
488 strftime(buff, sizeof(buff), tbp, localtime(&now));
489 else if (!(ci->imageOverlaid))
490 strftime(buff, sizeof(buff), "[No data]", localtime(&now));
491
492 ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
493 ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
494 y = ci->y;
495
496 if (ci->eval_a) {
497 ci->a = ff_parse_eval(ci->eval_a, const_values, ci);
498 }
499
500 if (ci->eval_colors) {
501 ci->r = ff_parse_eval(ci->eval_r, const_values, ci);
502 ci->g = ff_parse_eval(ci->eval_g, const_values, ci);
503 ci->b = ff_parse_eval(ci->eval_b, const_values, ci);
504 }
505
506 if (ci->eval_colors || ci->eval_a) {
507 imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
508 }
509
510 if (!(ci->imageOverlaid))
511 for (p = buff; p; p = q) {
512 q = strchr(p, '\n');
513 if (q)
514 *q++ = 0;
515
516 imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
517 y += v_a;
518 }
519
520 if (ci->imageOverlaid) {
521 imlib_context_set_image(image);
522 imlib_blend_image_onto_image(ci->imageOverlaid, 0,
523 0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
524 ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
525 }
526
527 }
528
529 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
530 width, height, PIX_FMT_RGB32,
531 width, height, pix_fmt,
532 sws_flags, NULL, NULL, NULL);
533 if (ci->fromRGB_convert_ctx == NULL) {
534 av_log(NULL, AV_LOG_ERROR,
535 "Cannot initialize the fromRGB conversion context\n");
536 return;
537 }
538 // img_convert parameters are 2 first destination, then 4 source
539 // sws_scale parameters are context, 4 first source, then 2 destination
540 sws_scale(ci->fromRGB_convert_ctx,
541 picture1.data, picture1.linesize, 0, height,
542 picture->data, picture->linesize);
543
544 ci->frame_number++;
545 }
546