Statistics
| Branch: | Revision:

ffmpeg / vhook / imlib2.c @ 61dbf4e5

History | View | Annotate | Download (15 KB)

1 26b4bb70 Philip Gladstone
/*
2 115329f1 Diego Biurrun
 * imlib2 based hook
3 26b4bb70 Philip Gladstone
 * Copyright (c) 2002 Philip Gladstone
4 115329f1 Diego Biurrun
 *
5 26b4bb70 Philip Gladstone
 * This module is very much intended as an example of what could be done.
6
 *
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 115329f1 Diego Biurrun
 * image. However, this code is fast enough to handle 10 fps of 320x240 on a
12 26b4bb70 Philip Gladstone
 * 900MHz Duron in maybe 15% of the CPU.
13 718eeb6a Víctor Paesa

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.
17
 *
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
30 26b4bb70 Philip Gladstone
 *
31 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
32
 *
33
 * FFmpeg is free software; you can redistribute it and/or
34 26b4bb70 Philip Gladstone
 * modify it under the terms of the GNU Lesser General Public
35
 * License as published by the Free Software Foundation; either
36 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
37 26b4bb70 Philip Gladstone
 *
38 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
39 26b4bb70 Philip Gladstone
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
45 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46 26b4bb70 Philip Gladstone
 */
47 00a3c8b8 Alex Beregszaszi
48
#include "framehook.h"
49 67d0436c Víctor Paesa
#include "swscale.h"
50 00a3c8b8 Alex Beregszaszi
51 26b4bb70 Philip Gladstone
#include <stdio.h>
52
#include <stdlib.h>
53
#include <fcntl.h>
54
#include <stdarg.h>
55
#include <string.h>
56
#include <unistd.h>
57 00a3c8b8 Alex Beregszaszi
#undef time
58 26b4bb70 Philip Gladstone
#include <sys/time.h>
59 00a3c8b8 Alex Beregszaszi
#include <time.h>
60 115329f1 Diego Biurrun
#include <Imlib2.h>
61 718eeb6a Víctor Paesa
#include "eval.h"
62
63
const char *const_names[]={
64
    "PI",
65
    "E",
66
    "N",  // frame number (starting at zero)
67
    "H",  // frame height
68
    "W",  // frame width
69
    "h",  // image height
70
    "w",  // image width
71
    "X",  // previous x
72
    "Y",  // previous y
73
    NULL
74
};
75 26b4bb70 Philip Gladstone
76 67d0436c Víctor Paesa
static int sws_flags = SWS_BICUBIC;
77
78 26b4bb70 Philip Gladstone
typedef struct {
79
    int dummy;
80
    Imlib_Font fn;
81
    char *text;
82
    char *file;
83 6f176376 Ramiro Polla
    int r, g, b, a;
84
    AVEvalExpr *eval_r, *eval_g, *eval_b, *eval_a;
85
    char *expr_R, *expr_G, *expr_B, *expr_A;
86 41e4c556 Ramiro Polla
    int eval_colors;
87 718eeb6a Víctor Paesa
    double x, y;
88
    char *fileImage;
89 4514a1d5 Diego Biurrun
    struct CachedImage *cache;
90 718eeb6a Víctor Paesa
    Imlib_Image imageOverlaid;
91
    AVEvalExpr *eval_x, *eval_y;
92
    char *expr_x, *expr_y;
93
    int frame_number;
94
    int imageOverlaid_width, imageOverlaid_height;
95 67d0436c Víctor Paesa
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;
100 26b4bb70 Philip Gladstone
} ContextInfo;
101
102 4514a1d5 Diego Biurrun
typedef struct CachedImage {
103
    struct CachedImage *next;
104 26b4bb70 Philip Gladstone
    Imlib_Image image;
105
    int width;
106
    int height;
107
} CachedImage;
108
109 6c11d48c Philip Gladstone
void Release(void *ctx)
110
{
111
    ContextInfo *ci;
112
    ci = (ContextInfo *) ctx;
113
114
    if (ci->cache) {
115
        imlib_context_set_image(ci->cache->image);
116
        imlib_free_image();
117
        av_free(ci->cache);
118
    }
119 67d0436c Víctor Paesa
    if (ctx) {
120 718eeb6a Víctor Paesa
        if (ci->imageOverlaid) {
121
            imlib_context_set_image(ci->imageOverlaid);
122
            imlib_free_image();
123
        }
124 4d321bff Limin Wang
        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 6f176376 Ramiro Polla
        ff_eval_free(ci->eval_a);
130 4d321bff Limin Wang
131
        av_free(ci->expr_x);
132
        av_free(ci->expr_y);
133
        av_free(ci->expr_R);
134
        av_free(ci->expr_G);
135
        av_free(ci->expr_B);
136 6f176376 Ramiro Polla
        av_free(ci->expr_A);
137 67d0436c Víctor Paesa
        sws_freeContext(ci->toRGB_convert_ctx);
138
        sws_freeContext(ci->fromRGB_convert_ctx);
139 6c11d48c Philip Gladstone
        av_free(ctx);
140 67d0436c Víctor Paesa
    }
141 6c11d48c Philip Gladstone
}
142 26b4bb70 Philip Gladstone
143
int Configure(void **ctxp, int argc, char *argv[])
144
{
145
    int c;
146
    ContextInfo *ci;
147 6100cb47 Ramiro Polla
    char *rgbtxt = 0;
148 26b4bb70 Philip Gladstone
    char *font = "LucidaSansDemiBold/16";
149
    char *fp = getenv("FONTPATH");
150
    char *color = 0;
151
    FILE *f;
152 718eeb6a Víctor Paesa
    char *p;
153 39e2b029 Ramiro Polla
    char *error;
154 26b4bb70 Philip Gladstone
155
    *ctxp = av_mallocz(sizeof(ContextInfo));
156
    ci = (ContextInfo *) *ctxp;
157
158 718eeb6a Víctor Paesa
    ci->x = 0.0;
159
    ci->y = 0.0;
160
    ci->expr_x = "0.0";
161
    ci->expr_y = "0.0";
162
163 a20db8a3 Eduardo Bragatto
    optind = 1;
164 26b4bb70 Philip Gladstone
165 718eeb6a Víctor Paesa
    /* Use ':' to split FONTPATH */
166 26b4bb70 Philip Gladstone
    if (fp)
167 718eeb6a Víctor Paesa
        while (p = strchr(fp, ':')) {
168
            *p = 0;
169
            imlib_add_path_to_font_path(fp);
170
            fp = p + 1;
171
        }
172
    if ((fp) && (*fp))
173 26b4bb70 Philip Gladstone
        imlib_add_path_to_font_path(fp);
174
175 718eeb6a Víctor Paesa
176 6f176376 Ramiro Polla
    while ((c = getopt(argc, argv, "R:G:B:A:C:c:f:F:t:x:y:i:")) > 0) {
177 26b4bb70 Philip Gladstone
        switch (c) {
178 41e4c556 Ramiro Polla
            case 'R':
179
                ci->expr_R = av_strdup(optarg);
180
                ci->eval_colors = 1;
181
                break;
182
            case 'G':
183
                ci->expr_G = av_strdup(optarg);
184
                ci->eval_colors = 1;
185
                break;
186
            case 'B':
187
                ci->expr_B = av_strdup(optarg);
188
                ci->eval_colors = 1;
189
                break;
190 6f176376 Ramiro Polla
            case 'A':
191
                ci->expr_A = av_strdup(optarg);
192
                break;
193 6100cb47 Ramiro Polla
            case 'C':
194
                rgbtxt = optarg;
195
                break;
196 26b4bb70 Philip Gladstone
            case 'c':
197
                color = optarg;
198
                break;
199
            case 'F':
200
                font = optarg;
201
                break;
202
            case 't':
203 e9a9e0c2 Fabrice Bellard
                ci->text = av_strdup(optarg);
204 26b4bb70 Philip Gladstone
                break;
205
            case 'f':
206 e9a9e0c2 Fabrice Bellard
                ci->file = av_strdup(optarg);
207 26b4bb70 Philip Gladstone
                break;
208
            case 'x':
209 718eeb6a Víctor Paesa
                ci->expr_x = av_strdup(optarg);
210 26b4bb70 Philip Gladstone
                break;
211
            case 'y':
212 718eeb6a Víctor Paesa
                ci->expr_y = av_strdup(optarg);
213
                break;
214
            case 'i':
215
                ci->fileImage = av_strdup(optarg);
216 26b4bb70 Philip Gladstone
                break;
217
            case '?':
218
                fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
219
                return -1;
220
        }
221
    }
222
223 41e4c556 Ramiro Polla
    if (ci->eval_colors && !(ci->expr_R && ci->expr_G && ci->expr_B))
224
    {
225
        fprintf(stderr, "You must specify expressions for all or no colors.\n");
226
        return -1;
227
    }
228
229 718eeb6a Víctor Paesa
    if (ci->text || ci->file) {
230 ef4c82cb Ramiro Polla
        ci->fn = imlib_load_font(font);
231
        if (!ci->fn) {
232
            fprintf(stderr, "Failed to load font '%s'\n", font);
233
            return -1;
234
        }
235
        imlib_context_set_font(ci->fn);
236
        imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
237 718eeb6a Víctor Paesa
    }
238 26b4bb70 Philip Gladstone
239
    if (color) {
240
        char buff[256];
241
        int done = 0;
242
243 41e4c556 Ramiro Polla
        if (ci->eval_colors)
244
        {
245
            fprintf(stderr, "You must not specify both a color name and expressions for the colors.\n");
246
            return -1;
247
        }
248
249 6100cb47 Ramiro Polla
        if (rgbtxt)
250
            f = fopen(rgbtxt, "r");
251
        else
252
        {
253 a661ade7 Ramiro Polla
            f = fopen("/usr/share/X11/rgb.txt", "r");
254
            if (!f)
255
                f = fopen("/usr/lib/X11/rgb.txt", "r");
256 6100cb47 Ramiro Polla
        }
257 26b4bb70 Philip Gladstone
        if (!f) {
258 6100cb47 Ramiro Polla
            fprintf(stderr, "Failed to find RGB color names file\n");
259 26b4bb70 Philip Gladstone
            return -1;
260
        }
261
        while (fgets(buff, sizeof(buff), f)) {
262
            int r, g, b;
263
            char colname[80];
264
265
            if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
266
                strcasecmp(colname, color) == 0) {
267
                ci->r = r;
268
                ci->g = g;
269
                ci->b = b;
270
                /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
271
                done = 1;
272
                break;
273
            }
274
        }
275
        fclose(f);
276
        if (!done) {
277
            fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
278
            return -1;
279
        }
280 41e4c556 Ramiro Polla
    } else if (ci->eval_colors) {
281 39e2b029 Ramiro Polla
        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);
283 41e4c556 Ramiro Polla
            return -1;
284
        }
285 39e2b029 Ramiro Polla
        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);
287 41e4c556 Ramiro Polla
            return -1;
288
        }
289 39e2b029 Ramiro Polla
        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);
291 41e4c556 Ramiro Polla
            return -1;
292
        }
293 26b4bb70 Philip Gladstone
    }
294 41e4c556 Ramiro Polla
295 6f176376 Ramiro Polla
    if (ci->expr_A) {
296 39e2b029 Ramiro Polla
        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);
298 6f176376 Ramiro Polla
            return -1;
299
        }
300
    } else {
301
        ci->a = 255;
302
    }
303
304
    if (!(ci->eval_colors || ci->eval_a))
305
        imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
306 718eeb6a Víctor Paesa
307
    /* load the image (for example, credits for a movie) */
308
    if (ci->fileImage) {
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);
312
            return -1;
313
        }
314
        imlib_context_set_image(ci->imageOverlaid);
315
        ci->imageOverlaid_width  = imlib_image_get_width();
316
        ci->imageOverlaid_height = imlib_image_get_height();
317
    }
318
319 39e2b029 Ramiro Polla
    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);
321 718eeb6a Víctor Paesa
        return -1;
322
    }
323
324 39e2b029 Ramiro Polla
    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);
326 718eeb6a Víctor Paesa
        return -1;
327
    }
328
329 26b4bb70 Philip Gladstone
    return 0;
330
}
331
332
static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
333
{
334
    CachedImage *cache;
335
336
    for (cache = ci->cache; cache; cache = cache->next) {
337
        if (width == cache->width && height == cache->height)
338
            return cache->image;
339
    }
340
341
    return NULL;
342
}
343
344
static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
345
{
346
    CachedImage *cache = av_mallocz(sizeof(*cache));
347
348
    cache->image = image;
349
    cache->width = width;
350
    cache->height = height;
351
    cache->next = ci->cache;
352
    ci->cache = cache;
353
}
354
355 0c1a9eda Zdenek Kabelac
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
356 26b4bb70 Philip Gladstone
{
357
    ContextInfo *ci = (ContextInfo *) ctx;
358
    AVPicture picture1;
359
    Imlib_Image image;
360
    DATA32 *data;
361
362
    image = get_cached_image(ci, width, height);
363
364
    if (!image) {
365
        image = imlib_create_image(width, height);
366
        put_cached_image(ci, image, width, height);
367
    }
368
369
    imlib_context_set_image(image);
370
    data = imlib_image_get_data();
371
372 ef4c82cb Ramiro Polla
    avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);
373 67d0436c Víctor Paesa
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 71e445fc Diego Biurrun
                                width, height, PIX_FMT_RGB32,
378 67d0436c Víctor Paesa
                                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");
382 ca345e44 Diego Biurrun
        return;
383 26b4bb70 Philip Gladstone
    }
384
385 67d0436c Víctor Paesa
// 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);
390
391 26b4bb70 Philip Gladstone
    imlib_image_set_has_alpha(0);
392
393
    {
394 115329f1 Diego Biurrun
        int wid, hig, h_a, v_a;
395 26b4bb70 Philip Gladstone
        char buff[1000];
396
        char tbuff[1000];
397
        char *tbp = ci->text;
398
        time_t now = time(0);
399
        char *p, *q;
400 718eeb6a Víctor Paesa
        int y;
401
402
        double const_values[]={
403
            M_PI,
404
            M_E,
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
410
            ci->x,                    // previous x
411
            ci->y,                    // previous y
412
            0
413
        };
414 26b4bb70 Philip Gladstone
415
        if (ci->file) {
416
            int fd = open(ci->file, O_RDONLY);
417
418
            if (fd < 0) {
419
                tbp = "[File not found]";
420
            } else {
421
                int l = read(fd, tbuff, sizeof(tbuff) - 1);
422
423
                if (l >= 0) {
424
                    tbuff[l] = 0;
425
                    tbp = tbuff;
426
                } else {
427
                    tbp = "[I/O Error]";
428
                }
429
                close(fd);
430
            }
431
        }
432
433 718eeb6a Víctor Paesa
        if (tbp)
434
            strftime(buff, sizeof(buff), tbp, localtime(&now));
435
        else if (!(ci->imageOverlaid))
436
            strftime(buff, sizeof(buff), "[No data]", localtime(&now));
437 26b4bb70 Philip Gladstone
438 718eeb6a Víctor Paesa
        ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
439
        ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
440 26b4bb70 Philip Gladstone
        y = ci->y;
441
442 6f176376 Ramiro Polla
        if (ci->eval_a) {
443
            ci->a = ff_parse_eval(ci->eval_a, const_values, ci);
444
        }
445
446 41e4c556 Ramiro Polla
        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);
450 6f176376 Ramiro Polla
        }
451
452
        if (ci->eval_colors || ci->eval_a) {
453
            imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
454 41e4c556 Ramiro Polla
        }
455
456 718eeb6a Víctor Paesa
        if (!(ci->imageOverlaid))
457 26b4bb70 Philip Gladstone
        for (p = buff; p; p = q) {
458
            q = strchr(p, '\n');
459
            if (q)
460
                *q++ = 0;
461
462 718eeb6a Víctor Paesa
            imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
463 26b4bb70 Philip Gladstone
            y += v_a;
464
        }
465 718eeb6a Víctor Paesa
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);
471
        }
472
473 26b4bb70 Philip Gladstone
    }
474
475 67d0436c Víctor Paesa
    ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
476 71e445fc Diego Biurrun
                                    width, height, PIX_FMT_RGB32,
477 67d0436c Víctor Paesa
                                    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");
482 ca345e44 Diego Biurrun
        return;
483 26b4bb70 Philip Gladstone
    }
484 67d0436c Víctor Paesa
// 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);
489 26b4bb70 Philip Gladstone
490 718eeb6a Víctor Paesa
    ci->frame_number++;
491 26b4bb70 Philip Gladstone
}