1/*
2 * imlib2 based hook
3 * Copyright (c) 2002 Philip Gladstone
4 *
5 * 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 * image. However, this code is fast enough to handle 10 fps of 320x240 on a
12 * 900MHz Duron in maybe 15% of the CPU.
13
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 *
31 * This file is part of FFmpeg.
32 *
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.
37 *
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.
42 *
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
46 */
47
48#include "libavformat/framehook.h"
49#include "libswscale/swscale.h"
50
51#include <stdio.h>
52#include <stdlib.h>
53#include <fcntl.h>
54#include <stdarg.h>
55#include <string.h>
56#include <strings.h>
57#include <unistd.h>
58#undef time
59#include <sys/time.h>
60#include <time.h>
61#include <Imlib2.h>
62#include "libavcodec/eval.h"
63
64const char *const_names[]={
65    "PI",
66    "E",
67    "N",  // frame number (starting at zero)
68    "H",  // frame height
69    "W",  // frame width
70    "h",  // image height
71    "w",  // image width
72    "X",  // previous x
73    "Y",  // previous y
74    NULL
75};
76
77static int sws_flags = SWS_BICUBIC;
78
79typedef struct {
80    int dummy;
81    Imlib_Font fn;
82    char *text;
83    char *file;
84    int r, g, b, a;
85    AVEvalExpr *eval_r, *eval_g, *eval_b, *eval_a;
86    char *expr_R, *expr_G, *expr_B, *expr_A;
87    int eval_colors;
88    double x, y;
89    char *fileImage;
90    struct CachedImage *cache;
91    Imlib_Image imageOverlaid;
92    AVEvalExpr *eval_x, *eval_y;
93    char *expr_x, *expr_y;
94    int frame_number;
95    int imageOverlaid_width, imageOverlaid_height;
96
97    // This vhook first converts frame to RGB ...
98    struct SwsContext *toRGB_convert_ctx;
99    // ... and then converts back frame from RGB to initial format
100    struct SwsContext *fromRGB_convert_ctx;
101} ContextInfo;
102
103typedef struct CachedImage {
104    struct CachedImage *next;
105    Imlib_Image image;
106    int width;
107    int height;
108} CachedImage;
109
110void Release(void *ctx)
111{
112    ContextInfo *ci;
113    ci = (ContextInfo *) ctx;
114
115    if (ci->cache) {
116        imlib_context_set_image(ci->cache->image);
117        imlib_free_image();
118        av_free(ci->cache);
119    }
120    if (ctx) {
121        if (ci->imageOverlaid) {
122            imlib_context_set_image(ci->imageOverlaid);
123            imlib_free_image();
124        }
125        ff_eval_free(ci->eval_x);
126        ff_eval_free(ci->eval_y);
127        ff_eval_free(ci->eval_r);
128        ff_eval_free(ci->eval_g);
129        ff_eval_free(ci->eval_b);
130        ff_eval_free(ci->eval_a);
131
132        av_free(ci->expr_x);
133        av_free(ci->expr_y);
134        av_free(ci->expr_R);
135        av_free(ci->expr_G);
136        av_free(ci->expr_B);
137        av_free(ci->expr_A);
138        sws_freeContext(ci->toRGB_convert_ctx);
139        sws_freeContext(ci->fromRGB_convert_ctx);
140        av_free(ctx);
141    }
142}
143
144int Configure(void **ctxp, int argc, char *argv[])
145{
146    int c;
147    ContextInfo *ci;
148    char *rgbtxt = 0;
149    const char *font = "LucidaSansDemiBold/16";
150    char *fp = getenv("FONTPATH");
151    char *color = 0;
152    FILE *f;
153    char *p;
154    const char *error;
155
156    *ctxp = av_mallocz(sizeof(ContextInfo));
157    ci = (ContextInfo *) *ctxp;
158
159    ci->x = 0.0;
160    ci->y = 0.0;
161    ci->expr_x = "0.0";
162    ci->expr_y = "0.0";
163
164    optind = 1;
165
166    /* Use ':' to split FONTPATH */
167    if (fp)
168        while ((p = strchr(fp, ':'))) {
169            *p = 0;
170            imlib_add_path_to_font_path(fp);
171            fp = p + 1;
172        }
173    if ((fp) && (*fp))
174        imlib_add_path_to_font_path(fp);
175
176
177    while ((c = getopt(argc, argv, "R:G:B:A:C:c:f:F:t:x:y:i:")) > 0) {
178        switch (c) {
179            case 'R':
180                ci->expr_R = av_strdup(optarg);
181                ci->eval_colors = 1;
182                break;
183            case 'G':
184                ci->expr_G = av_strdup(optarg);
185                ci->eval_colors = 1;
186                break;
187            case 'B':
188                ci->expr_B = av_strdup(optarg);
189                ci->eval_colors = 1;
190                break;
191            case 'A':
192                ci->expr_A = av_strdup(optarg);
193                break;
194            case 'C':
195                rgbtxt = optarg;
196                break;
197            case 'c':
198                color = optarg;
199                break;
200            case 'F':
201                font = optarg;
202                break;
203            case 't':
204                ci->text = av_strdup(optarg);
205                break;
206            case 'f':
207                ci->file = av_strdup(optarg);
208                break;
209            case 'x':
210                ci->expr_x = av_strdup(optarg);
211                break;
212            case 'y':
213                ci->expr_y = av_strdup(optarg);
214                break;
215            case 'i':
216                ci->fileImage = av_strdup(optarg);
217                break;
218            case '?':
219                av_log(NULL, AV_LOG_ERROR, "Unrecognized argument '%s'\n", argv[optind]);
220                return -1;
221        }
222    }
223
224    if (ci->eval_colors && !(ci->expr_R && ci->expr_G && ci->expr_B))
225    {
226        av_log(NULL, AV_LOG_ERROR, "You must specify expressions for all or no colors.\n");
227        return -1;
228    }
229
230    if (ci->text || ci->file) {
231        ci->fn = imlib_load_font(font);
232        if (!ci->fn) {
233            av_log(NULL, AV_LOG_ERROR, "Failed to load font '%s'\n", font);
234            return -1;
235        }
236        imlib_context_set_font(ci->fn);
237        imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
238    }
239
240    if (color) {
241        char buff[256];
242        int done = 0;
243
244        if (ci->eval_colors)
245        {
246            av_log(NULL, AV_LOG_ERROR, "You must not specify both a color name and expressions for the colors.\n");
247            return -1;
248        }
249
250        if (rgbtxt)
251            f = fopen(rgbtxt, "r");
252        else
253        {
254            f = fopen("/usr/share/X11/rgb.txt", "r");
255            if (!f)
256                f = fopen("/usr/lib/X11/rgb.txt", "r");
257        }
258        if (!f) {
259            av_log(NULL, AV_LOG_ERROR, "Failed to find RGB color names file\n");
260            return -1;
261        }
262        while (fgets(buff, sizeof(buff), f)) {
263            int r, g, b;
264            char colname[80];
265
266            if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
267                strcasecmp(colname, color) == 0) {
268                ci->r = r;
269                ci->g = g;
270                ci->b = b;
271                /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
272                done = 1;
273                break;
274            }
275        }
276        fclose(f);
277        if (!done) {
278            av_log(NULL, AV_LOG_ERROR, "Unable to find color '%s' in rgb.txt\n", color);
279            return -1;
280        }
281    } else if (ci->eval_colors) {
282        if (!(ci->eval_r = ff_parse(ci->expr_R, const_names, NULL, NULL, NULL, NULL, &error))){
283            av_log(NULL, AV_LOG_ERROR, "Couldn't parse R expression '%s': %s\n", ci->expr_R, error);
284            return -1;
285        }
286        if (!(ci->eval_g = ff_parse(ci->expr_G, const_names, NULL, NULL, NULL, NULL, &error))){
287            av_log(NULL, AV_LOG_ERROR, "Couldn't parse G expression '%s': %s\n", ci->expr_G, error);
288            return -1;
289        }
290        if (!(ci->eval_b = ff_parse(ci->expr_B, const_names, NULL, NULL, NULL, NULL, &error))){
291            av_log(NULL, AV_LOG_ERROR, "Couldn't parse B expression '%s': %s\n", ci->expr_B, error);
292            return -1;
293        }
294    }
295
296    if (ci->expr_A) {
297        if (!(ci->eval_a = ff_parse(ci->expr_A, const_names, NULL, NULL, NULL, NULL, &error))){
298            av_log(NULL, AV_LOG_ERROR, "Couldn't parse A expression '%s': %s\n", ci->expr_A, error);
299            return -1;
300        }
301    } else {
302        ci->a = 255;
303    }
304
305    if (!(ci->eval_colors || ci->eval_a))
306        imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
307
308    /* load the image (for example, credits for a movie) */
309    if (ci->fileImage) {
310        ci->imageOverlaid = imlib_load_image_immediately(ci->fileImage);
311        if (!(ci->imageOverlaid)){
312            av_log(NULL, AV_LOG_ERROR, "Couldn't load image '%s'\n", ci->fileImage);
313            return -1;
314        }
315        imlib_context_set_image(ci->imageOverlaid);
316        ci->imageOverlaid_width  = imlib_image_get_width();
317        ci->imageOverlaid_height = imlib_image_get_height();
318    }
319
320    if (!(ci->eval_x = ff_parse(ci->expr_x, const_names, NULL, NULL, NULL, NULL, &error))){
321        av_log(NULL, AV_LOG_ERROR, "Couldn't parse x expression '%s': %s\n", ci->expr_x, error);
322        return -1;
323    }
324
325    if (!(ci->eval_y = ff_parse(ci->expr_y, const_names, NULL, NULL, NULL, NULL, &error))){
326        av_log(NULL, AV_LOG_ERROR, "Couldn't parse y expression '%s': %s\n", ci->expr_y, error);
327        return -1;
328    }
329
330    return 0;
331}
332
333static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
334{
335    CachedImage *cache;
336
337    for (cache = ci->cache; cache; cache = cache->next) {
338        if (width == cache->width && height == cache->height)
339            return cache->image;
340    }
341
342    return NULL;
343}
344
345static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
346{
347    CachedImage *cache = av_mallocz(sizeof(*cache));
348
349    cache->image = image;
350    cache->width = width;
351    cache->height = height;
352    cache->next = ci->cache;
353    ci->cache = cache;
354}
355
356void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
357{
358    ContextInfo *ci = (ContextInfo *) ctx;
359    AVPicture picture1;
360    Imlib_Image image;
361    DATA32 *data;
362
363    image = get_cached_image(ci, width, height);
364
365    if (!image) {
366        image = imlib_create_image(width, height);
367        put_cached_image(ci, image, width, height);
368    }
369
370    imlib_context_set_image(image);
371    data = imlib_image_get_data();
372
373    avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGB32, width, height);
374
375    // if we already got a SWS context, let's realloc if is not re-useable
376    ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
377                                width, height, pix_fmt,
378                                width, height, PIX_FMT_RGB32,
379                                sws_flags, NULL, NULL, NULL);
380    if (ci->toRGB_convert_ctx == NULL) {
381        av_log(NULL, AV_LOG_ERROR,
382               "Cannot initialize the toRGB conversion context\n");
383        return;
384    }
385
386// img_convert parameters are          2 first destination, then 4 source
387// sws_scale   parameters are context, 4 first source,      then 2 destination
388    sws_scale(ci->toRGB_convert_ctx,
389             picture->data, picture->linesize, 0, height,
390             picture1.data, picture1.linesize);
391
392    imlib_image_set_has_alpha(0);
393
394    {
395        int wid, hig, h_a, v_a;
396        char buff[1000];
397        char tbuff[1000];
398        const char *tbp = ci->text;
399        time_t now = time(0);
400        char *p, *q;
401        int y;
402
403        double const_values[]={
404            M_PI,
405            M_E,
406            ci->frame_number,         // frame number (starting at zero)
407            height,                   // frame height
408            width,                    // frame width
409            ci->imageOverlaid_height, // image height
410            ci->imageOverlaid_width,  // image width
411            ci->x,                    // previous x
412            ci->y,                    // previous y
413            0
414        };
415
416        if (ci->file) {
417            int fd = open(ci->file, O_RDONLY);
418
419            if (fd < 0) {
420                tbp = "[File not found]";
421            } else {
422                int l = read(fd, tbuff, sizeof(tbuff) - 1);
423
424                if (l >= 0) {
425                    tbuff[l] = 0;
426                    tbp = tbuff;
427                } else {
428                    tbp = "[I/O Error]";
429                }
430                close(fd);
431            }
432        }
433
434        if (tbp)
435            strftime(buff, sizeof(buff), tbp, localtime(&now));
436        else if (!(ci->imageOverlaid))
437            strftime(buff, sizeof(buff), "[No data]", localtime(&now));
438
439        ci->x = ff_parse_eval(ci->eval_x, const_values, ci);
440        ci->y = ff_parse_eval(ci->eval_y, const_values, ci);
441        y = ci->y;
442
443        if (ci->eval_a) {
444            ci->a = ff_parse_eval(ci->eval_a, const_values, ci);
445        }
446
447        if (ci->eval_colors) {
448            ci->r = ff_parse_eval(ci->eval_r, const_values, ci);
449            ci->g = ff_parse_eval(ci->eval_g, const_values, ci);
450            ci->b = ff_parse_eval(ci->eval_b, const_values, ci);
451        }
452
453        if (ci->eval_colors || ci->eval_a) {
454            imlib_context_set_color(ci->r, ci->g, ci->b, ci->a);
455        }
456
457        if (!(ci->imageOverlaid))
458        for (p = buff; p; p = q) {
459            q = strchr(p, '\n');
460            if (q)
461                *q++ = 0;
462
463            imlib_text_draw_with_return_metrics(ci->x, y, p, &wid, &hig, &h_a, &v_a);
464            y += v_a;
465        }
466
467        if (ci->imageOverlaid) {
468            imlib_context_set_image(image);
469            imlib_blend_image_onto_image(ci->imageOverlaid, 0,
470                0, 0, ci->imageOverlaid_width, ci->imageOverlaid_height,
471                ci->x, ci->y, ci->imageOverlaid_width, ci->imageOverlaid_height);
472        }
473
474    }
475
476    ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
477                                    width, height, PIX_FMT_RGB32,
478                                    width, height, pix_fmt,
479                                    sws_flags, NULL, NULL, NULL);
480    if (ci->fromRGB_convert_ctx == NULL) {
481        av_log(NULL, AV_LOG_ERROR,
482               "Cannot initialize the fromRGB conversion context\n");
483        return;
484    }
485// img_convert parameters are          2 first destination, then 4 source
486// sws_scale   parameters are context, 4 first source,      then 2 destination
487    sws_scale(ci->fromRGB_convert_ctx,
488             picture1.data, picture1.linesize, 0, height,
489             picture->data, picture->linesize);
490
491    ci->frame_number++;
492}
493
494