Change license headers to say 'FFmpeg' instead of 'this program/this library'
[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 * 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
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
53 #include <stdarg.h>
54 #include <string.h>
55 #include <unistd.h>
56 #undef time
57 #include <sys/time.h>
58 #include <time.h>
59 #include <X11/Xlib.h>
60 #include <Imlib2.h>
61
62 typedef struct {
63 int dummy;
64 Imlib_Font fn;
65 char *text;
66 char *file;
67 int r, g, b;
68 int x;
69 int y;
70 struct _CachedImage *cache;
71 } ContextInfo;
72
73 typedef struct _CachedImage {
74 struct _CachedImage *next;
75 Imlib_Image image;
76 int width;
77 int height;
78 } CachedImage;
79
80 void Release(void *ctx)
81 {
82 ContextInfo *ci;
83 ci = (ContextInfo *) ctx;
84
85 if (ci->cache) {
86 imlib_context_set_image(ci->cache->image);
87 imlib_free_image();
88 av_free(ci->cache);
89 }
90 if (ctx)
91 av_free(ctx);
92 }
93
94 int Configure(void **ctxp, int argc, char *argv[])
95 {
96 int c;
97 ContextInfo *ci;
98 char *font = "LucidaSansDemiBold/16";
99 char *fp = getenv("FONTPATH");
100 char *color = 0;
101 FILE *f;
102
103 *ctxp = av_mallocz(sizeof(ContextInfo));
104 ci = (ContextInfo *) *ctxp;
105
106 optind = 0;
107
108 if (fp)
109 imlib_add_path_to_font_path(fp);
110
111 while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
112 switch (c) {
113 case 'c':
114 color = optarg;
115 break;
116 case 'F':
117 font = optarg;
118 break;
119 case 't':
120 ci->text = av_strdup(optarg);
121 break;
122 case 'f':
123 ci->file = av_strdup(optarg);
124 break;
125 case 'x':
126 ci->x = atoi(optarg);
127 break;
128 case 'y':
129 ci->y = atoi(optarg);
130 break;
131 case '?':
132 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
133 return -1;
134 }
135 }
136
137 ci->fn = imlib_load_font(font);
138 if (!ci->fn) {
139 fprintf(stderr, "Failed to load font '%s'\n", font);
140 return -1;
141 }
142 imlib_context_set_font(ci->fn);
143 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
144
145 if (color) {
146 char buff[256];
147 int done = 0;
148
149 f = fopen("/usr/share/X11/rgb.txt", "r");
150 if (!f)
151 f = fopen("/usr/lib/X11/rgb.txt", "r");
152 if (!f) {
153 fprintf(stderr, "Failed to find rgb.txt\n");
154 return -1;
155 }
156 while (fgets(buff, sizeof(buff), f)) {
157 int r, g, b;
158 char colname[80];
159
160 if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
161 strcasecmp(colname, color) == 0) {
162 ci->r = r;
163 ci->g = g;
164 ci->b = b;
165 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
166 done = 1;
167 break;
168 }
169 }
170 fclose(f);
171 if (!done) {
172 fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
173 return -1;
174 }
175 }
176 imlib_context_set_color(ci->r, ci->g, ci->b, 255);
177 return 0;
178 }
179
180 static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
181 {
182 CachedImage *cache;
183
184 for (cache = ci->cache; cache; cache = cache->next) {
185 if (width == cache->width && height == cache->height)
186 return cache->image;
187 }
188
189 return NULL;
190 }
191
192 static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
193 {
194 CachedImage *cache = av_mallocz(sizeof(*cache));
195
196 cache->image = image;
197 cache->width = width;
198 cache->height = height;
199 cache->next = ci->cache;
200 ci->cache = cache;
201 }
202
203 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
204 {
205 ContextInfo *ci = (ContextInfo *) ctx;
206 AVPicture picture1;
207 Imlib_Image image;
208 DATA32 *data;
209
210 image = get_cached_image(ci, width, height);
211
212 if (!image) {
213 image = imlib_create_image(width, height);
214 put_cached_image(ci, image, width, height);
215 }
216
217 imlib_context_set_image(image);
218 data = imlib_image_get_data();
219
220 avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
221 if (pix_fmt != PIX_FMT_RGBA32) {
222 if (img_convert(&picture1, PIX_FMT_RGBA32,
223 picture, pix_fmt, width, height) < 0) {
224 goto done;
225 }
226 } else {
227 img_copy(&picture1, picture, PIX_FMT_RGBA32, width, height);
228 }
229
230 imlib_image_set_has_alpha(0);
231
232 {
233 int wid, hig, h_a, v_a;
234 char buff[1000];
235 char tbuff[1000];
236 char *tbp = ci->text;
237 time_t now = time(0);
238 char *p, *q;
239 int x, y;
240
241 if (ci->file) {
242 int fd = open(ci->file, O_RDONLY);
243
244 if (fd < 0) {
245 tbp = "[File not found]";
246 } else {
247 int l = read(fd, tbuff, sizeof(tbuff) - 1);
248
249 if (l >= 0) {
250 tbuff[l] = 0;
251 tbp = tbuff;
252 } else {
253 tbp = "[I/O Error]";
254 }
255 close(fd);
256 }
257 }
258
259 strftime(buff, sizeof(buff), tbp ? tbp : "[No data]", localtime(&now));
260
261 x = ci->x;
262 y = ci->y;
263
264 for (p = buff; p; p = q) {
265 q = strchr(p, '\n');
266 if (q)
267 *q++ = 0;
268
269 imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
270 y += v_a;
271 }
272 }
273
274 if (pix_fmt != PIX_FMT_RGBA32) {
275 if (img_convert(picture, pix_fmt,
276 &picture1, PIX_FMT_RGBA32, width, height) < 0) {
277 }
278 } else {
279 img_copy(picture, &picture1, PIX_FMT_RGBA32, width, height);
280 }
281
282 done:
283 ;
284 }
285