3 * Copyright (c) 2002 Philip Gladstone
5 * This module is very much intended as an example of what could be done.
7 * One caution is that this is an expensive process -- in particular the
8 * conversion of the image into RGB and back is time consuming. For some
9 * special cases -- e.g. painting black text -- it would be faster to paint
10 * the text into a bitmap and then combine it directly into the YUV
11 * image. However, this code is fast enough to handle 10 fps of 320x240 on a
12 * 900MHz Duron in maybe 15% of the CPU.
14 * See further statistics on Pentium4, 3GHz, FFMpeg is SVN-r6798
15 * Input movie is 20.2 seconds of PAL DV on AVI
16 * Output movie is DVD compliant VOB.
18 ffmpeg -i input.avi -target pal-dvd out.vob
19 # 13.516s just transcode
20 ffmpeg -i input.avi -vhook /usr/local/bin/vhook/null.dll -target pal-dvd out.vob
21 # 23.546s transcode and img_convert
22 ffmpeg -i input.avi -vhook \
23 'vhook/imlib2.dll -c red -F Vera/20 -x 150-0.5*N -y 70+0.25*N -t Hello_person' \
24 -target pal-dvd out.vob
25 # 21.454s transcode, img_convert and move text around
26 ffmpeg -i input.avi -vhook \
27 'vhook/imlib2.dll -x 150-0.5*N -y 70+0.25*N -i /usr/share/imlib2/data/images/bulb.png' \
28 -target pal-dvd out.vob
29 # 20.828s transcode, img_convert and move image around
31 * This file is part of FFmpeg.
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.
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.
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
48 #include "framehook.h"
63 const char *const_names[]={
66 "N", // frame number (starting at zero)
76 static int sws_flags = SWS_BICUBIC;
84 AVEvalExpr *eval_r, *eval_g, *eval_b, *eval_a;
85 char *expr_R, *expr_G, *expr_B, *expr_A;
89 struct CachedImage *cache;
90 Imlib_Image imageOverlaid;
91 AVEvalExpr *eval_x, *eval_y;
92 char *expr_x, *expr_y;
94 int imageOverlaid_width, imageOverlaid_height;
96 // This vhook first converts frame to RGB ...
97 struct SwsContext *toRGB_convert_ctx;
98 // ... and then converts back frame from RGB to initial format
99 struct SwsContext *fromRGB_convert_ctx;
102 typedef struct CachedImage {
103 struct CachedImage *next;
109 void Release(void *ctx)
112 ci = (ContextInfo *) ctx;
115 imlib_context_set_image(ci->cache->image);
120 if (ci->imageOverlaid) {
121 imlib_context_set_image(ci->imageOverlaid);
124 ff_eval_free(ci->eval_x);
125 ff_eval_free(ci->eval_y);
126 ff_eval_free(ci->eval_r);
127 ff_eval_free(ci->eval_g);
128 ff_eval_free(ci->eval_b);
129 ff_eval_free(ci->eval_a);
137 sws_freeContext(ci->toRGB_convert_ctx);
138 sws_freeContext(ci->fromRGB_convert_ctx);
143 int Configure(void **ctxp, int argc, char *argv[])
148 char *font = "LucidaSansDemiBold/16";
149 char *fp = getenv("FONTPATH");
155 *ctxp = av_mallocz(sizeof(ContextInfo));
156 ci = (ContextInfo *) *ctxp;
165 /* Use ':' to split FONTPATH */
167 while (p = strchr(fp, ':')) {
169 imlib_add_path_to_font_path(fp);
173 imlib_add_path_to_font_path(fp);
176 while ((c = getopt(argc, argv, "R:G:B:A:C:c:f:F:t:x:y:i:")) > 0) {
179 ci->expr_R = av_strdup(optarg);
183 ci->expr_G = av_strdup(optarg);
187 ci->expr_B = av_strdup(optarg);
191 ci->expr_A = av_strdup(optarg);
203 ci->text = av_strdup(optarg);
206 ci->file = av_strdup(optarg);
209 ci->expr_x = av_strdup(optarg);
212 ci->expr_y = av_strdup(optarg);
215 ci->fileImage = av_strdup(optarg);
218 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
223 if (ci->eval_colors && !(ci->expr_R && ci->expr_G && ci->expr_B))
225 fprintf(stderr, "You must specify expressions for all or no colors.\n");
229 if (ci->text || ci->file) {
230 ci->fn = imlib_load_font(font);
232 fprintf(stderr, "Failed to load font '%s'\n", font);
235 imlib_context_set_font(ci->fn);
236 imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
245 fprintf(stderr, "You must not specify both a color name and expressions for the colors.\n");
250 f = fopen(rgbtxt, "r");
253 f = fopen("/usr/share/X11/rgb.txt", "r");
255 f = fopen("/usr/lib/X11/rgb.txt", "r");
258 fprintf(stderr, "Failed to find RGB color names file\n");
261 while (fgets(buff, sizeof(buff), f)) {
265 if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
266 strcasecmp(colname, color) == 0) {
270 /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
277 fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
280 } else if (ci->eval_colors) {
281 if (!(ci->eval_r = ff_parse(ci->expr_R, const_names, NULL, NULL, NULL, NULL, &error))){
282 av_log(NULL, AV_LOG_ERROR, "Couldn't parse R expression '%s': %s\n", ci->expr_R, error);
285 if (!(ci->eval_g = ff_parse(ci->expr_G, const_names, NULL, NULL, NULL, NULL, &error))){
286 av_log(NULL, AV_LOG_ERROR, "Couldn't parse G expression '%s': %s\n", ci->expr_G, error);
289 if (!(ci->eval_b = ff_parse(ci->expr_B, const_names, NULL, NULL, NULL, NULL, &error))){
290 av_log(NULL, AV_LOG_ERROR, "Couldn't parse B expression '%s': %s\n", ci->expr_B, error);
296 if (!(ci->eval_a = ff_parse(ci->expr_A, const_names, NULL, NULL, NULL, NULL, &error))){
297 av_log(NULL, AV_LOG_ERROR, "Couldn't parse A expression '%s': %s\n", ci->expr_A, error);
304 if (!(ci->eval_colors || ci->eval_a))
305 imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
307 /* load the image (for example, credits for a movie) */
309 ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
310 if (!(ci->imageOverlaid)){
311 av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
314 imlib_context_set_image(ci->imageOverlaid);
315 ci->imageOverlaid_width = imlib_image_get_width();
316 ci->imageOverlaid_height = imlib_image_get_height();
319 if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, &error))){
320 av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s': %s\n", ci->expr_x, error);
324 if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, &error))){
325 av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s': %s\n", ci->expr_y, error);
332 static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
336 for (cache = ci->cache; cache; cache = cache->next) {
337 if (width == cache->width && height == cache->height)
344 static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
346 CachedImage *cache = av_mallocz(sizeof(*cache));
348 cache->image = image;
349 cache->width = width;
350 cache->height = height;
351 cache->next = ci->cache;
355 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
357 ContextInfo *ci = (ContextInfo *) ctx;
362 image = get_cached_image(ci, width, height);
365 image = imlib_create_image(width, height);
366 put_cached_image(ci, image, width, height);
369 imlib_context_set_image(image);
370 data = imlib_image_get_data();
372 avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);
374 // if we already got a SWS context, let's realloc if is not re-useable
375 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
376 width, height, pix_fmt,
377 width, height, PIX_FMT_RGB32,
378 sws_flags, NULL, NULL, NULL);
379 if (ci->toRGB_convert_ctx == NULL) {
380 av_log(NULL, AV_LOG_ERROR,
381 "Cannot initialize the toRGB conversion context\n");
385 // img_convert parameters are 2 first destination, then 4 source
386 // sws_scale parameters are context, 4 first source, then 2 destination
387 sws_scale(ci->toRGB_convert_ctx,
388 picture->data, picture->linesize, 0, height,
389 picture1.data, picture1.linesize);
391 imlib_image_set_has_alpha(0);
394 int wid, hig, h_a, v_a;
397 char *tbp = ci->text;
398 time_t now = time(0);
402 double const_values[]={
405 ci->frame_number, // frame number (starting at zero)
406 height, // frame height
407 width, // frame width
408 ci->imageOverlaid_height, // image height
409 ci->imageOverlaid_width, // image width
416 int fd = open(ci->file, O_RDONLY);
419 tbp = "[File not found]";
421 int l = read(fd, tbuff, sizeof(tbuff) - 1);
434 strftime(buff, sizeof(buff), tbp, localtime(&now));
435 else if (!(ci->imageOverlaid))
436 strftime(buff, sizeof(buff), "[No data]", localtime(&now));
438 ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
439 ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
443 ci->a = ff_parse_eval(ci->eval_a, const_values, ci);
446 if (ci->eval_colors) {
447 ci->r = ff_parse_eval(ci->eval_r, const_values, ci);
448 ci->g = ff_parse_eval(ci->eval_g, const_values, ci);
449 ci->b = ff_parse_eval(ci->eval_b, const_values, ci);
452 if (ci->eval_colors || ci->eval_a) {
453 imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
456 if (!(ci->imageOverlaid))
457 for (p = buff; p; p = q) {
462 imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
466 if (ci->imageOverlaid) {
467 imlib_context_set_image(image);
468 imlib_blend_image_onto_image(ci->imageOverlaid, 0,
469 0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
470 ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
475 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
476 width, height, PIX_FMT_RGB32,
477 width, height, pix_fmt,
478 sws_flags, NULL, NULL, NULL);
479 if (ci->fromRGB_convert_ctx == NULL) {
480 av_log(NULL, AV_LOG_ERROR,
481 "Cannot initialize the fromRGB conversion context\n");
484 // img_convert parameters are 2 first destination, then 4 source
485 // sws_scale parameters are context, 4 first source, then 2 destination
486 sws_scale(ci->fromRGB_convert_ctx,
487 picture1.data, picture1.linesize, 0, height,
488 picture->data, picture->linesize);