]> git.sesse.net Git - ffmpeg/blob - vhook/imlib2.c
Added three sample video hooks. See the (rudimentary) documentation on what
[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  * Options:
11  *
12  * -c <color>           The color of the text
13  * -F <fontname>        The font face and size
14  * -t <text>            The text
15  * -f <filename>        The filename to read text from
16  * -x <num>             X coordinate to start text
17  * -y <num>             Y coordinate to start text
18  *
19  * This module is very much intended as an example of what could be done.
20  * For example, you could overlay an image (even semi-transparent) like
21  * TV stations do. You can manipulate the image using imlib2 functions
22  * in any way. 
23  *
24  * One caution is that this is an expensive process -- in particular the
25  * conversion of the image into RGB and back is time consuming. For some
26  * special cases -- e.g. painting black text -- it would be faster to paint
27  * the text into a bitmap and then combine it directly into the YUV
28  * image. However, this code is fast enough to handle 10 fps of 320x240 on a 
29  * 900MHz Duron in maybe 15% of the CPU.
30  *
31  * This library is free software; you can redistribute it and/or
32  * modify it under the terms of the GNU Lesser General Public
33  * License as published by the Free Software Foundation; either
34  * version 2 of the License, or (at your option) any later version.
35  *
36  * This library is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39  * Lesser General Public License for more details.
40  *
41  * You should have received a copy of the GNU Lesser General Public
42  * License along with this library; if not, write to the Free Software
43  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
44  */
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <fcntl.h>
48 #include <getopt.h>
49 #include <stdarg.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <sys/time.h>
53 #include <X11/Xlib.h>
54 #include <Imlib2.h>                                                             
55
56 #include "framehook.h"
57
58 typedef struct {
59     int dummy;
60     Imlib_Font fn;
61     char *text;
62     char *file;
63     int r, g, b;
64     int x;
65     int y;
66     struct _CachedImage *cache;
67 } ContextInfo;
68
69 typedef struct _CachedImage {
70     struct _CachedImage *next;
71     Imlib_Image image;
72     int width;
73     int height;
74 } CachedImage;
75
76
77 int Configure(void **ctxp, int argc, char *argv[])
78 {
79     int c;
80     ContextInfo *ci;
81     char *font = "LucidaSansDemiBold/16";
82     char *fp = getenv("FONTPATH");
83     char *color = 0;
84     FILE *f;
85
86     *ctxp = av_mallocz(sizeof(ContextInfo));
87     ci = (ContextInfo *) *ctxp;
88
89     optind = 0;
90
91     if (fp)
92         imlib_add_path_to_font_path(fp);
93
94     while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
95         switch (c) {
96             case 'c':
97                 color = optarg;
98                 break;
99             case 'F':
100                 font = optarg;
101                 break;
102             case 't':
103                 ci->text = strdup(optarg);
104                 break;
105             case 'f':
106                 ci->file = strdup(optarg);
107                 break;
108             case 'x':
109                 ci->x = atoi(optarg);
110                 break;
111             case 'y':
112                 ci->y = atoi(optarg);
113                 break;
114             case '?':
115                 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
116                 return -1;
117         }
118     }
119
120     ci->fn = imlib_load_font(font);
121     if (!ci->fn) {
122         fprintf(stderr, "Failed to load font '%s'\n", font);
123         return -1;
124     }
125     imlib_context_set_font(ci->fn);
126     imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);                           
127
128     if (color) {
129         char buff[256];
130         int done = 0;
131
132         f = fopen("/usr/lib/X11/rgb.txt", "r");
133         if (!f) {
134             fprintf(stderr, "Failed to find rgb.txt\n");
135             return -1;
136         }
137         while (fgets(buff, sizeof(buff), f)) {
138             int r, g, b;
139             char colname[80];
140
141             if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
142                 strcasecmp(colname, color) == 0) {
143                 ci->r = r;
144                 ci->g = g;
145                 ci->b = b;
146                 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
147                 done = 1;
148                 break;
149             }
150         }
151         fclose(f);
152         if (!done) {
153             fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
154             return -1;
155         }
156     }
157     imlib_context_set_color(ci->r, ci->g, ci->b, 255);
158     return 0;
159 }
160
161 static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
162 {
163     CachedImage *cache;
164
165     for (cache = ci->cache; cache; cache = cache->next) {
166         if (width == cache->width && height == cache->height)
167             return cache->image;
168     }
169
170     return NULL;
171 }
172
173 static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
174 {
175     CachedImage *cache = av_mallocz(sizeof(*cache));
176
177     cache->image = image;
178     cache->width = width;
179     cache->height = height;
180     cache->next = ci->cache;
181     ci->cache = cache;
182 }
183
184 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts)
185 {
186     ContextInfo *ci = (ContextInfo *) ctx;
187     AVPicture picture1;
188     Imlib_Image image;
189     DATA32 *data;
190
191     image = get_cached_image(ci, width, height);
192
193     if (!image) {
194         image = imlib_create_image(width, height);
195         put_cached_image(ci, image, width, height);
196     }
197
198     imlib_context_set_image(image);
199     data = imlib_image_get_data();
200
201     if (pix_fmt != PIX_FMT_BGRA32) {
202         avpicture_fill(&picture1, (UINT8 *) data, PIX_FMT_BGRA32, width, height);
203         if (img_convert(&picture1, PIX_FMT_BGRA32, 
204                         picture, pix_fmt, width, height) < 0) {
205             goto done;
206         }
207     } else {
208         av_abort();
209     }
210
211     imlib_image_set_has_alpha(0);
212
213     {
214         int wid, hig, h_a, v_a;                                                   
215         char buff[1000];
216         char tbuff[1000];
217         char *tbp = ci->text;
218         time_t now = time(0);
219         char *p, *q;
220         int x, y;
221
222         if (ci->file) {
223             int fd = open(ci->file, O_RDONLY);
224
225             if (fd < 0) {
226                 tbp = "[File not found]";
227             } else {
228                 int l = read(fd, tbuff, sizeof(tbuff) - 1);
229
230                 if (l >= 0) {
231                     tbuff[l] = 0;
232                     tbp = tbuff;
233                 } else {
234                     tbp = "[I/O Error]";
235                 }
236                 close(fd);
237             }
238         }
239
240         strftime(buff, sizeof(buff), tbp, localtime(&now));
241
242         x = ci->x;
243         y = ci->y;
244
245         for (p = buff; p; p = q) {
246             q = strchr(p, '\n');
247             if (q)
248                 *q++ = 0;
249
250             imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
251             y += v_a;
252         }
253     }
254
255     if (pix_fmt != PIX_FMT_BGRA32) {
256         if (img_convert(picture, pix_fmt, 
257                         &picture1, PIX_FMT_BGRA32, width, height) < 0) {
258         }
259     }
260
261 done:
262     ;
263 }
264
265 /* To ensure correct typing */
266 FrameHookConfigureFn ConfigureFn = Configure;
267 FrameHookProcessFn ProcessFn = Process;