3 * Copyright (c) 2002 Philip Gladstone
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
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.
14 * Text fonts are being looked for in FONTPATH
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
22 * -c <color> The color of the text
23 * -F <fontname> The font face and size
25 * -f <filename> The filename to read text from
26 * -x <expresion> X coordinate of text or image
27 * -y <expresion> Y coordinate of text or image
28 * -i <filename> The filename to read a image from
30 * Expresions are functions of:
31 * N // frame number (starting at zero)
42 FONTPATH="/cygdrive/c/WINDOWS/Fonts/"
43 FONTPATH="$FONTPATH:/usr/share/imlib2/data/fonts/"
44 FONTPATH="$FONTPATH:/usr/X11R6/lib/X11/fonts/TTF/"
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
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
55 * This module is very much intended as an example of what could be done.
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
61 * image. However, this code is fast enough to handle 10 fps of 320x240 on a
62 * 900MHz Duron in maybe 15% of the CPU.
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.
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
81 * This file is part of FFmpeg.
83 * FFmpeg is free software; you can redistribute it and/or
84 * modify it under the terms of the GNU Lesser General Public
85 * License as published by the Free Software Foundation; either
86 * version 2.1 of the License, or (at your option) any later version.
88 * FFmpeg is distributed in the hope that it will be useful,
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.
93 * You should have received a copy of the GNU Lesser General Public
94 * License along with FFmpeg; if not, write to the Free Software
95 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
98 #include "framehook.h"
108 #include <sys/time.h>
113 const char *const_names
[]={
116 "N", // frame number (starting at zero)
126 static int sws_flags
= SWS_BICUBIC
;
136 struct _CachedImage
*cache
;
137 Imlib_Image imageOverlaid
;
138 AVEvalExpr
*eval_x
, *eval_y
;
139 char *expr_x
, *expr_y
;
141 int imageOverlaid_width
, imageOverlaid_height
;
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
;
149 typedef struct _CachedImage
{
150 struct _CachedImage
*next
;
156 void Release(void *ctx
)
159 ci
= (ContextInfo
*) ctx
;
162 imlib_context_set_image(ci
->cache
->image
);
167 if (ci
->imageOverlaid
) {
168 imlib_context_set_image(ci
->imageOverlaid
);
171 ff_eval_free(ci
->expr_x
);
172 ff_eval_free(ci
->expr_y
);
173 sws_freeContext(ci
->toRGB_convert_ctx
);
174 sws_freeContext(ci
->fromRGB_convert_ctx
);
179 int Configure(void **ctxp
, int argc
, char *argv
[])
184 char *font
= "LucidaSansDemiBold/16";
185 char *fp
= getenv("FONTPATH");
190 *ctxp
= av_mallocz(sizeof(ContextInfo
));
191 ci
= (ContextInfo
*) *ctxp
;
200 /* Use ':' to split FONTPATH */
202 while (p
= strchr(fp
, ':')) {
204 imlib_add_path_to_font_path(fp
);
208 imlib_add_path_to_font_path(fp
);
211 while ((c
= getopt(argc
, argv
, "C:c:f:F:t:x:y:i:")) > 0) {
223 ci
->text
= av_strdup(optarg
);
226 ci
->file
= av_strdup(optarg
);
229 ci
->expr_x
= av_strdup(optarg
);
232 ci
->expr_y
= av_strdup(optarg
);
235 ci
->fileImage
= av_strdup(optarg
);
238 fprintf(stderr
, "Unrecognized argument '%s'\n", argv
[optind
]);
243 if (ci
->text
|| ci
->file
) {
244 ci
->fn
= imlib_load_font(font
);
246 fprintf(stderr
, "Failed to load font '%s'\n", font
);
249 imlib_context_set_font(ci
->fn
);
250 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT
);
258 f
= fopen(rgbtxt
, "r");
261 f
= fopen("/usr/share/X11/rgb.txt", "r");
263 f
= fopen("/usr/lib/X11/rgb.txt", "r");
266 fprintf(stderr
, "Failed to find RGB color names file\n");
269 while (fgets(buff
, sizeof(buff
), f
)) {
273 if (sscanf(buff
, "%d %d %d %64s", &r
, &g
, &b
, colname
) == 4 &&
274 strcasecmp(colname
, color
) == 0) {
278 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
285 fprintf(stderr
, "Unable to find color '%s' in rgb.txt\n", color
);
289 imlib_context_set_color(ci
->r
, ci
->g
, ci
->b
, 255);
291 /* load the image (for example, credits for a movie) */
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
);
298 imlib_context_set_image(ci
->imageOverlaid
);
299 ci
->imageOverlaid_width
= imlib_image_get_width();
300 ci
->imageOverlaid_height
= imlib_image_get_height();
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
);
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
);
316 static Imlib_Image
get_cached_image(ContextInfo
*ci
, int width
, int height
)
320 for (cache
= ci
->cache
; cache
; cache
= cache
->next
) {
321 if (width
== cache
->width
&& height
== cache
->height
)
328 static void put_cached_image(ContextInfo
*ci
, Imlib_Image image
, int width
, int height
)
330 CachedImage
*cache
= av_mallocz(sizeof(*cache
));
332 cache
->image
= image
;
333 cache
->width
= width
;
334 cache
->height
= height
;
335 cache
->next
= ci
->cache
;
339 void Process(void *ctx
, AVPicture
*picture
, enum PixelFormat pix_fmt
, int width
, int height
, int64_t pts
)
341 ContextInfo
*ci
= (ContextInfo
*) ctx
;
346 image
= get_cached_image(ci
, width
, height
);
349 image
= imlib_create_image(width
, height
);
350 put_cached_image(ci
, image
, width
, height
);
353 imlib_context_set_image(image
);
354 data
= imlib_image_get_data();
356 avpicture_fill(&picture1
, (uint8_t *) data
, PIX_FMT_RGB32
, width
, height
);
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
,
361 width
, height
, PIX_FMT_RGB32
,
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");
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
);
375 imlib_image_set_has_alpha(0);
378 int wid
, hig
, h_a
, v_a
;
381 char *tbp
= ci
->text
;
382 time_t now
= time(0);
386 double const_values
[]={
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
400 int fd
= open(ci
->file
, O_RDONLY
);
403 tbp
= "[File not found]";
405 int l
= read(fd
, tbuff
, sizeof(tbuff
) - 1);
418 strftime(buff
, sizeof(buff
), tbp
, localtime(&now
));
419 else if (!(ci
->imageOverlaid
))
420 strftime(buff
, sizeof(buff
), "[No data]", localtime(&now
));
422 ci
->x
= ff_parse_eval(ci
->eval_x
, const_values
, ci
);
423 ci
->y
= ff_parse_eval(ci
->eval_y
, const_values
, ci
);
426 if (!(ci
->imageOverlaid
))
427 for (p
= buff
; p
; p
= q
) {
432 imlib_text_draw_with_return_metrics(ci
->x
, y
, p
, &wid
, &hig
, &h_a
, &v_a
);
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
);
445 ci
->fromRGB_convert_ctx
= sws_getCachedContext(ci
->fromRGB_convert_ctx
,
446 width
, height
, PIX_FMT_RGB32
,
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");
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
);