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