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