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 <color> The color of the text
19 * -F <fontname> The font face and size
21 * -f <filename> The filename to read text from
22 * -x <expresion> X coordinate of text or image
23 * -y <expresion> Y coordinate of text or image
24 * -i <filename> The filename to read a image from
26 * Expresions are functions of:
27 * N // frame number (starting at zero)
38 FONTPATH="/cygdrive/c/WINDOWS/Fonts/"
39 FONTPATH="$FONTPATH:/usr/share/imlib2/data/fonts/"
40 FONTPATH="$FONTPATH:/usr/X11R6/lib/X11/fonts/TTF/"
43 ffmpeg -i input.avi -vhook \
44 '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'
45 -acodec copy -sameq output.avi
47 ffmpeg -i input.avi -vhook \
48 'vhook/imlib2.dll -c red -F Vera.ttf/20 -x 150+0.5*N -y 70+0.25*N -t Hello'
49 -acodec copy -sameq output.avi
51 * This module is very much intended as an example of what could be done.
53 * One caution is that this is an expensive process -- in particular the
54 * conversion of the image into RGB and back is time consuming. For some
55 * special cases -- e.g. painting black text -- it would be faster to paint
56 * the text into a bitmap and then combine it directly into the YUV
57 * image. However, this code is fast enough to handle 10 fps of 320x240 on a
58 * 900MHz Duron in maybe 15% of the CPU.
60 * See further statistics on Pentium4, 3GHz, FFMpeg is SVN-r6798
61 * Input movie is 20.2 seconds of PAL DV on AVI
62 * Output movie is DVD compliant VOB.
64 ffmpeg -i input.avi -target pal-dvd out.vob
65 # 13.516s just transcode
66 ffmpeg -i input.avi -vhook /usr/local/bin/vhook/null.dll -target pal-dvd out.vob
67 # 23.546s transcode and img_convert
68 ffmpeg -i input.avi -vhook \
69 'vhook/imlib2.dll -c red -F Vera/20 -x 150-0.5*N -y 70+0.25*N -t Hello_person' \
70 -target pal-dvd out.vob
71 # 21.454s transcode, img_convert and move text around
72 ffmpeg -i input.avi -vhook \
73 'vhook/imlib2.dll -x 150-0.5*N -y 70+0.25*N -i /usr/share/imlib2/data/images/bulb.png' \
74 -target pal-dvd out.vob
75 # 20.828s transcode, img_convert and move image around
77 * This file is part of FFmpeg.
79 * FFmpeg is free software; you can redistribute it and/or
80 * modify it under the terms of the GNU Lesser General Public
81 * License as published by the Free Software Foundation; either
82 * version 2.1 of the License, or (at your option) any later version.
84 * FFmpeg is distributed in the hope that it will be useful,
85 * but WITHOUT ANY WARRANTY; without even the implied warranty of
86 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
87 * Lesser General Public License for more details.
89 * You should have received a copy of the GNU Lesser General Public
90 * License along with FFmpeg; if not, write to the Free Software
91 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
94 #include "framehook.h"
104 #include <sys/time.h>
109 const char *const_names
[]={
112 "N", // frame number (starting at zero)
122 static int sws_flags
= SWS_BICUBIC
;
132 struct _CachedImage
*cache
;
133 Imlib_Image imageOverlaid
;
134 AVEvalExpr
*eval_x
, *eval_y
;
135 char *expr_x
, *expr_y
;
137 int imageOverlaid_width
, imageOverlaid_height
;
139 // This vhook first converts frame to RGB ...
140 struct SwsContext
*toRGB_convert_ctx
;
141 // ... and then converts back frame from RGB to initial format
142 struct SwsContext
*fromRGB_convert_ctx
;
145 typedef struct _CachedImage
{
146 struct _CachedImage
*next
;
152 void Release(void *ctx
)
155 ci
= (ContextInfo
*) ctx
;
158 imlib_context_set_image(ci
->cache
->image
);
163 if (ci
->imageOverlaid
) {
164 imlib_context_set_image(ci
->imageOverlaid
);
167 ff_eval_free(ci
->expr_x
);
168 ff_eval_free(ci
->expr_y
);
169 sws_freeContext(ci
->toRGB_convert_ctx
);
170 sws_freeContext(ci
->fromRGB_convert_ctx
);
175 int Configure(void **ctxp
, int argc
, char *argv
[])
179 char *font
= "LucidaSansDemiBold/16";
180 char *fp
= getenv("FONTPATH");
185 *ctxp
= av_mallocz(sizeof(ContextInfo
));
186 ci
= (ContextInfo
*) *ctxp
;
195 /* Use ':' to split FONTPATH */
197 while (p
= strchr(fp
, ':')) {
199 imlib_add_path_to_font_path(fp
);
203 imlib_add_path_to_font_path(fp
);
206 while ((c
= getopt(argc
, argv
, "c:f:F:t:x:y:i:")) > 0) {
215 ci
->text
= av_strdup(optarg
);
218 ci
->file
= av_strdup(optarg
);
221 ci
->expr_x
= av_strdup(optarg
);
224 ci
->expr_y
= av_strdup(optarg
);
227 ci
->fileImage
= av_strdup(optarg
);
230 fprintf(stderr
, "Unrecognized argument '%s'\n", argv
[optind
]);
235 if (ci
->text
|| ci
->file
) {
236 ci
->fn
= imlib_load_font(font
);
238 fprintf(stderr
, "Failed to load font '%s'\n", font
);
241 imlib_context_set_font(ci
->fn
);
242 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT
);
249 f
= fopen("/usr/share/X11/rgb.txt", "r");
251 f
= fopen("/usr/lib/X11/rgb.txt", "r");
253 fprintf(stderr
, "Failed to find rgb.txt\n");
256 while (fgets(buff
, sizeof(buff
), f
)) {
260 if (sscanf(buff
, "%d %d %d %64s", &r
, &g
, &b
, colname
) == 4 &&
261 strcasecmp(colname
, color
) == 0) {
265 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
272 fprintf(stderr
, "Unable to find color '%s' in rgb.txt\n", color
);
276 imlib_context_set_color(ci
->r
, ci
->g
, ci
->b
, 255);
278 /* load the image (for example, credits for a movie) */
280 ci
->imageOverlaid
= imlib_load_image_immediately(ci
->fileImage
);
281 if (!(ci
->imageOverlaid
)){
282 av_log(NULL
, AV_LOG_ERROR
, "Couldn't load image '%s'\n", ci
->fileImage
);
285 imlib_context_set_image(ci
->imageOverlaid
);
286 ci
->imageOverlaid_width
= imlib_image_get_width();
287 ci
->imageOverlaid_height
= imlib_image_get_height();
290 if (!(ci
->eval_x
= ff_parse(ci
->expr_x
, const_names
, NULL
, NULL
, NULL
, NULL
, NULL
))){
291 av_log(NULL
, AV_LOG_ERROR
, "Couldn't parse x expression '%s'\n", ci
->expr_x
);
295 if (!(ci
->eval_y
= ff_parse(ci
->expr_y
, const_names
, NULL
, NULL
, NULL
, NULL
, NULL
))){
296 av_log(NULL
, AV_LOG_ERROR
, "Couldn't parse y expression '%s'\n", ci
->expr_y
);
303 static Imlib_Image
get_cached_image(ContextInfo
*ci
, int width
, int height
)
307 for (cache
= ci
->cache
; cache
; cache
= cache
->next
) {
308 if (width
== cache
->width
&& height
== cache
->height
)
315 static void put_cached_image(ContextInfo
*ci
, Imlib_Image image
, int width
, int height
)
317 CachedImage
*cache
= av_mallocz(sizeof(*cache
));
319 cache
->image
= image
;
320 cache
->width
= width
;
321 cache
->height
= height
;
322 cache
->next
= ci
->cache
;
326 void Process(void *ctx
, AVPicture
*picture
, enum PixelFormat pix_fmt
, int width
, int height
, int64_t pts
)
328 ContextInfo
*ci
= (ContextInfo
*) ctx
;
333 image
= get_cached_image(ci
, width
, height
);
336 image
= imlib_create_image(width
, height
);
337 put_cached_image(ci
, image
, width
, height
);
340 imlib_context_set_image(image
);
341 data
= imlib_image_get_data();
343 avpicture_fill(&picture1
, (uint8_t *) data
, PIX_FMT_RGBA32
, width
, height
);
345 // if we already got a SWS context, let's realloc if is not re-useable
346 ci
->toRGB_convert_ctx
= sws_getCachedContext(ci
->toRGB_convert_ctx
,
347 width
, height
, pix_fmt
,
348 width
, height
, PIX_FMT_RGBA32
,
349 sws_flags
, NULL
, NULL
, NULL
);
350 if (ci
->toRGB_convert_ctx
== NULL
) {
351 av_log(NULL
, AV_LOG_ERROR
,
352 "Cannot initialize the toRGB conversion context\n");
356 // img_convert parameters are 2 first destination, then 4 source
357 // sws_scale parameters are context, 4 first source, then 2 destination
358 sws_scale(ci
->toRGB_convert_ctx
,
359 picture
->data
, picture
->linesize
, 0, height
,
360 picture1
.data
, picture1
.linesize
);
362 imlib_image_set_has_alpha(0);
365 int wid
, hig
, h_a
, v_a
;
368 char *tbp
= ci
->text
;
369 time_t now
= time(0);
373 double const_values
[]={
376 ci
->frame_number
, // frame number (starting at zero)
377 height
, // frame height
378 width
, // frame width
379 ci
->imageOverlaid_height
, // image height
380 ci
->imageOverlaid_width
, // image width
387 int fd
= open(ci
->file
, O_RDONLY
);
390 tbp
= "[File not found]";
392 int l
= read(fd
, tbuff
, sizeof(tbuff
) - 1);
405 strftime(buff
, sizeof(buff
), tbp
, localtime(&now
));
406 else if (!(ci
->imageOverlaid
))
407 strftime(buff
, sizeof(buff
), "[No data]", localtime(&now
));
409 ci
->x
= ff_parse_eval(ci
->eval_x
, const_values
, ci
);
410 ci
->y
= ff_parse_eval(ci
->eval_y
, const_values
, ci
);
413 if (!(ci
->imageOverlaid
))
414 for (p
= buff
; p
; p
= q
) {
419 imlib_text_draw_with_return_metrics(ci
->x
, y
, p
, &wid
, &hig
, &h_a
, &v_a
);
423 if (ci
->imageOverlaid
) {
424 imlib_context_set_image(image
);
425 imlib_blend_image_onto_image(ci
->imageOverlaid
, 0,
426 0, 0, ci
->imageOverlaid_width
, ci
->imageOverlaid_height
,
427 ci
->x
, ci
->y
, ci
->imageOverlaid_width
, ci
->imageOverlaid_height
);
432 ci
->fromRGB_convert_ctx
= sws_getCachedContext(ci
->fromRGB_convert_ctx
,
433 width
, height
, PIX_FMT_RGBA32
,
434 width
, height
, pix_fmt
,
435 sws_flags
, NULL
, NULL
, NULL
);
436 if (ci
->fromRGB_convert_ctx
== NULL
) {
437 av_log(NULL
, AV_LOG_ERROR
,
438 "Cannot initialize the fromRGB conversion context\n");
441 // img_convert parameters are 2 first destination, then 4 source
442 // sws_scale parameters are context, 4 first source, then 2 destination
443 sws_scale(ci
->fromRGB_convert_ctx
,
444 picture1
.data
, picture1
.linesize
, 0, height
,
445 picture
->data
, picture
->linesize
);