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