]> git.sesse.net Git - ffmpeg/blob - vhook/imlib2.c
port imlib2 vhook to swscaler
[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 file is part of FFmpeg.
32  *
33  * FFmpeg is free software; you can redistribute it and/or
34  * modify it under the terms of the GNU Lesser General Public
35  * License as published by the Free Software Foundation; either
36  * version 2.1 of the License, or (at your option) any later version.
37  *
38  * FFmpeg is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41  * Lesser General Public License for more details.
42  *
43  * You should have received a copy of the GNU Lesser General Public
44  * License along with FFmpeg; if not, write to the Free Software
45  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46  */
47
48 #include "framehook.h"
49 #include "swscale.h"
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <fcntl.h>
54 #include <stdarg.h>
55 #include <string.h>
56 #include <unistd.h>
57 #undef time
58 #include <sys/time.h>
59 #include <time.h>
60 #include <X11/Xlib.h>
61 #include <Imlib2.h>
62
63 static int sws_flags = SWS_BICUBIC;
64
65 typedef struct {
66     int dummy;
67     Imlib_Font fn;
68     char *text;
69     char *file;
70     int r, g, b;
71     int x;
72     int y;
73     struct _CachedImage *cache;
74
75     // This vhook first converts frame to RGB ...
76     struct SwsContext *toRGB_convert_ctx;
77     // ... and then converts back frame from RGB to initial format
78     struct SwsContext *fromRGB_convert_ctx;
79 } ContextInfo;
80
81 typedef struct _CachedImage {
82     struct _CachedImage *next;
83     Imlib_Image image;
84     int width;
85     int height;
86 } CachedImage;
87
88 void Release(void *ctx)
89 {
90     ContextInfo *ci;
91     ci = (ContextInfo *) ctx;
92
93     if (ci->cache) {
94         imlib_context_set_image(ci->cache->image);
95         imlib_free_image();
96         av_free(ci->cache);
97     }
98     if (ctx) {
99         sws_freeContext(ci->toRGB_convert_ctx);
100         sws_freeContext(ci->fromRGB_convert_ctx);
101         av_free(ctx);
102     }
103 }
104
105 int Configure(void **ctxp, int argc, char *argv[])
106 {
107     int c;
108     ContextInfo *ci;
109     char *font = "LucidaSansDemiBold/16";
110     char *fp = getenv("FONTPATH");
111     char *color = 0;
112     FILE *f;
113
114     *ctxp = av_mallocz(sizeof(ContextInfo));
115     ci = (ContextInfo *) *ctxp;
116
117     optind = 0;
118
119     if (fp)
120         imlib_add_path_to_font_path(fp);
121
122     while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
123         switch (c) {
124             case 'c':
125                 color = optarg;
126                 break;
127             case 'F':
128                 font = optarg;
129                 break;
130             case 't':
131                 ci->text = av_strdup(optarg);
132                 break;
133             case 'f':
134                 ci->file = av_strdup(optarg);
135                 break;
136             case 'x':
137                 ci->x = atoi(optarg);
138                 break;
139             case 'y':
140                 ci->y = atoi(optarg);
141                 break;
142             case '?':
143                 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
144                 return -1;
145         }
146     }
147
148     ci->fn = imlib_load_font(font);
149     if (!ci->fn) {
150         fprintf(stderr, "Failed to load font '%s'\n", font);
151         return -1;
152     }
153     imlib_context_set_font(ci->fn);
154     imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
155
156     if (color) {
157         char buff[256];
158         int done = 0;
159
160         f = fopen("/usr/share/X11/rgb.txt", "r");
161         if (!f)
162             f = fopen("/usr/lib/X11/rgb.txt", "r");
163         if (!f) {
164             fprintf(stderr, "Failed to find rgb.txt\n");
165             return -1;
166         }
167         while (fgets(buff, sizeof(buff), f)) {
168             int r, g, b;
169             char colname[80];
170
171             if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
172                 strcasecmp(colname, color) == 0) {
173                 ci->r = r;
174                 ci->g = g;
175                 ci->b = b;
176                 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
177                 done = 1;
178                 break;
179             }
180         }
181         fclose(f);
182         if (!done) {
183             fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
184             return -1;
185         }
186     }
187     imlib_context_set_color(ci->r, ci->g, ci->b, 255);
188     return 0;
189 }
190
191 static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
192 {
193     CachedImage *cache;
194
195     for (cache = ci->cache; cache; cache = cache->next) {
196         if (width == cache->width && height == cache->height)
197             return cache->image;
198     }
199
200     return NULL;
201 }
202
203 static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
204 {
205     CachedImage *cache = av_mallocz(sizeof(*cache));
206
207     cache->image = image;
208     cache->width = width;
209     cache->height = height;
210     cache->next = ci->cache;
211     ci->cache = cache;
212 }
213
214 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
215 {
216     ContextInfo *ci = (ContextInfo *) ctx;
217     AVPicture picture1;
218     Imlib_Image image;
219     DATA32 *data;
220
221     image = get_cached_image(ci, width, height);
222
223     if (!image) {
224         image = imlib_create_image(width, height);
225         put_cached_image(ci, image, width, height);
226     }
227
228     imlib_context_set_image(image);
229     data = imlib_image_get_data();
230
231         avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
232
233     // if we already got a SWS context, let's realloc if is not re-useable
234     ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
235                                 width, height, pix_fmt,
236                                 width, height, PIX_FMT_RGBA32,
237                                 sws_flags, NULL, NULL, NULL);
238     if (ci->toRGB_convert_ctx == NULL) {
239         av_log(NULL, AV_LOG_ERROR,
240                "Cannot initialize the toRGB conversion context\n");
241         exit(1);
242     }
243
244 // img_convert parameters are          2 first destination, then 4 source
245 // sws_scale   parameters are context, 4 first source,      then 2 destination
246     sws_scale(ci->toRGB_convert_ctx,
247              picture->data, picture->linesize, 0, height,
248              picture1.data, picture1.linesize);
249
250     imlib_image_set_has_alpha(0);
251
252     {
253         int wid, hig, h_a, v_a;
254         char buff[1000];
255         char tbuff[1000];
256         char *tbp = ci->text;
257         time_t now = time(0);
258         char *p, *q;
259         int x, y;
260
261         if (ci->file) {
262             int fd = open(ci->file, O_RDONLY);
263
264             if (fd < 0) {
265                 tbp = "[File not found]";
266             } else {
267                 int l = read(fd, tbuff, sizeof(tbuff) - 1);
268
269                 if (l >= 0) {
270                     tbuff[l] = 0;
271                     tbp = tbuff;
272                 } else {
273                     tbp = "[I/O Error]";
274                 }
275                 close(fd);
276             }
277         }
278
279         strftime(buff, sizeof(buff), tbp ? tbp : "[No data]", localtime(&now));
280
281         x = ci->x;
282         y = ci->y;
283
284         for (p = buff; p; p = q) {
285             q = strchr(p, '\n');
286             if (q)
287                 *q++ = 0;
288
289             imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
290             y += v_a;
291         }
292     }
293
294     ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
295                                     width, height, PIX_FMT_RGBA32,
296                                     width, height, pix_fmt,
297                                     sws_flags, NULL, NULL, NULL);
298     if (ci->fromRGB_convert_ctx == NULL) {
299         av_log(NULL, AV_LOG_ERROR,
300                "Cannot initialize the fromRGB conversion context\n");
301         exit(1);
302     }
303 // img_convert parameters are          2 first destination, then 4 source
304 // sws_scale   parameters are context, 4 first source,      then 2 destination
305     sws_scale(ci->fromRGB_convert_ctx,
306              picture1.data, picture1.linesize, 0, height,
307              picture->data, picture->linesize);
308
309 }
310