Statistics
| Branch: | Revision:

ffmpeg / vhook / imlib2.c @ 5509bffa

History | View | Annotate | Download (7.51 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 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
8
 * time onto the image.
9
 *
10
 * Options:
11
 *
12
 * -c <color>           The color of the text
13
 * -F <fontname>        The font face and size
14
 * -t <text>            The text
15
 * -f <filename>        The filename to read text from
16
 * -x <num>             X coordinate to start text
17
 * -y <num>             Y coordinate to start text
18
 *
19
 * This module is very much intended as an example of what could be done.
20
 * For example, you could overlay an image (even semi-transparent) like
21
 * TV stations do. You can manipulate the image using imlib2 functions
22 115329f1 Diego Biurrun
 * in any way.
23 26b4bb70 Philip Gladstone
 *
24
 * One caution is that this is an expensive process -- in particular the
25
 * conversion of the image into RGB and back is time consuming. For some
26
 * special cases -- e.g. painting black text -- it would be faster to paint
27
 * the text into a bitmap and then combine it directly into the YUV
28 115329f1 Diego Biurrun
 * image. However, this code is fast enough to handle 10 fps of 320x240 on a
29 26b4bb70 Philip Gladstone
 * 900MHz Duron in maybe 15% of the CPU.
30
 *
31
 * This library is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU Lesser General Public
33
 * License as published by the Free Software Foundation; either
34
 * version 2 of the License, or (at your option) any later version.
35
 *
36
 * This library is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39
 * Lesser General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU Lesser General Public
42
 * License along with this library; if not, write to the Free Software
43 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
44 26b4bb70 Philip Gladstone
 */
45 00a3c8b8 Alex Beregszaszi
46
#include "framehook.h"
47
48 26b4bb70 Philip Gladstone
#include <stdio.h>
49
#include <stdlib.h>
50
#include <fcntl.h>
51
#include <stdarg.h>
52
#include <string.h>
53
#include <unistd.h>
54 00a3c8b8 Alex Beregszaszi
#undef time
55 26b4bb70 Philip Gladstone
#include <sys/time.h>
56 00a3c8b8 Alex Beregszaszi
#include <time.h>
57 26b4bb70 Philip Gladstone
#include <X11/Xlib.h>
58 115329f1 Diego Biurrun
#include <Imlib2.h>
59 26b4bb70 Philip Gladstone
60
typedef struct {
61
    int dummy;
62
    Imlib_Font fn;
63
    char *text;
64
    char *file;
65
    int r, g, b;
66
    int x;
67
    int y;
68
    struct _CachedImage *cache;
69
} ContextInfo;
70
71
typedef struct _CachedImage {
72
    struct _CachedImage *next;
73
    Imlib_Image image;
74
    int width;
75
    int height;
76
} CachedImage;
77
78 6c11d48c Philip Gladstone
void Release(void *ctx)
79
{
80
    ContextInfo *ci;
81
    ci = (ContextInfo *) ctx;
82
83
    if (ci->cache) {
84
        imlib_context_set_image(ci->cache->image);
85
        imlib_free_image();
86
        av_free(ci->cache);
87
    }
88
    if (ctx)
89
        av_free(ctx);
90
}
91 26b4bb70 Philip Gladstone
92
int Configure(void **ctxp, int argc, char *argv[])
93
{
94
    int c;
95
    ContextInfo *ci;
96
    char *font = "LucidaSansDemiBold/16";
97
    char *fp = getenv("FONTPATH");
98
    char *color = 0;
99
    FILE *f;
100
101
    *ctxp = av_mallocz(sizeof(ContextInfo));
102
    ci = (ContextInfo *) *ctxp;
103
104
    optind = 0;
105
106
    if (fp)
107
        imlib_add_path_to_font_path(fp);
108
109
    while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) {
110
        switch (c) {
111
            case 'c':
112
                color = optarg;
113
                break;
114
            case 'F':
115
                font = optarg;
116
                break;
117
            case 't':
118 e9a9e0c2 Fabrice Bellard
                ci->text = av_strdup(optarg);
119 26b4bb70 Philip Gladstone
                break;
120
            case 'f':
121 e9a9e0c2 Fabrice Bellard
                ci->file = av_strdup(optarg);
122 26b4bb70 Philip Gladstone
                break;
123
            case 'x':
124
                ci->x = atoi(optarg);
125
                break;
126
            case 'y':
127
                ci->y = atoi(optarg);
128
                break;
129
            case '?':
130
                fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
131
                return -1;
132
        }
133
    }
134
135
    ci->fn = imlib_load_font(font);
136
    if (!ci->fn) {
137
        fprintf(stderr, "Failed to load font '%s'\n", font);
138
        return -1;
139
    }
140
    imlib_context_set_font(ci->fn);
141 115329f1 Diego Biurrun
    imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
142 26b4bb70 Philip Gladstone
143
    if (color) {
144
        char buff[256];
145
        int done = 0;
146
147
        f = fopen("/usr/lib/X11/rgb.txt", "r");
148
        if (!f) {
149
            fprintf(stderr, "Failed to find rgb.txt\n");
150
            return -1;
151
        }
152
        while (fgets(buff, sizeof(buff), f)) {
153
            int r, g, b;
154
            char colname[80];
155
156
            if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 &&
157
                strcasecmp(colname, color) == 0) {
158
                ci->r = r;
159
                ci->g = g;
160
                ci->b = b;
161
                /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */
162
                done = 1;
163
                break;
164
            }
165
        }
166
        fclose(f);
167
        if (!done) {
168
            fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color);
169
            return -1;
170
        }
171
    }
172
    imlib_context_set_color(ci->r, ci->g, ci->b, 255);
173
    return 0;
174
}
175
176
static Imlib_Image get_cached_image(ContextInfo *ci, int width, int height)
177
{
178
    CachedImage *cache;
179
180
    for (cache = ci->cache; cache; cache = cache->next) {
181
        if (width == cache->width && height == cache->height)
182
            return cache->image;
183
    }
184
185
    return NULL;
186
}
187
188
static void put_cached_image(ContextInfo *ci, Imlib_Image image, int width, int height)
189
{
190
    CachedImage *cache = av_mallocz(sizeof(*cache));
191
192
    cache->image = image;
193
    cache->width = width;
194
    cache->height = height;
195
    cache->next = ci->cache;
196
    ci->cache = cache;
197
}
198
199 0c1a9eda Zdenek Kabelac
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
200 26b4bb70 Philip Gladstone
{
201
    ContextInfo *ci = (ContextInfo *) ctx;
202
    AVPicture picture1;
203
    Imlib_Image image;
204
    DATA32 *data;
205
206
    image = get_cached_image(ci, width, height);
207
208
    if (!image) {
209
        image = imlib_create_image(width, height);
210
        put_cached_image(ci, image, width, height);
211
    }
212
213
    imlib_context_set_image(image);
214
    data = imlib_image_get_data();
215
216 0c1a9eda Zdenek Kabelac
        avpicture_fill(&picture1, (uint8_t *) data, PIX_FMT_RGBA32, width, height);
217 bb4a8772 Michael Niedermayer
    if (pix_fmt != PIX_FMT_RGBA32) {
218 115329f1 Diego Biurrun
        if (img_convert(&picture1, PIX_FMT_RGBA32,
219 26b4bb70 Philip Gladstone
                        picture, pix_fmt, width, height) < 0) {
220
            goto done;
221
        }
222
    } else {
223 115329f1 Diego Biurrun
        img_copy(&picture1, picture, PIX_FMT_RGBA32, width, height);
224 26b4bb70 Philip Gladstone
    }
225
226
    imlib_image_set_has_alpha(0);
227
228
    {
229 115329f1 Diego Biurrun
        int wid, hig, h_a, v_a;
230 26b4bb70 Philip Gladstone
        char buff[1000];
231
        char tbuff[1000];
232
        char *tbp = ci->text;
233
        time_t now = time(0);
234
        char *p, *q;
235
        int x, y;
236
237
        if (ci->file) {
238
            int fd = open(ci->file, O_RDONLY);
239
240
            if (fd < 0) {
241
                tbp = "[File not found]";
242
            } else {
243
                int l = read(fd, tbuff, sizeof(tbuff) - 1);
244
245
                if (l >= 0) {
246
                    tbuff[l] = 0;
247
                    tbp = tbuff;
248
                } else {
249
                    tbp = "[I/O Error]";
250
                }
251
                close(fd);
252
            }
253
        }
254
255 4be3147d Philip Gladstone
        strftime(buff, sizeof(buff), tbp ? tbp : "[No data]", localtime(&now));
256 26b4bb70 Philip Gladstone
257
        x = ci->x;
258
        y = ci->y;
259
260
        for (p = buff; p; p = q) {
261
            q = strchr(p, '\n');
262
            if (q)
263
                *q++ = 0;
264
265
            imlib_text_draw_with_return_metrics(x, y, p, &wid, &hig, &h_a, &v_a);
266
            y += v_a;
267
        }
268
    }
269
270 0e5f8ab1 Fabrice Bellard
    if (pix_fmt != PIX_FMT_RGBA32) {
271 115329f1 Diego Biurrun
        if (img_convert(picture, pix_fmt,
272 0e5f8ab1 Fabrice Bellard
                        &picture1, PIX_FMT_RGBA32, width, height) < 0) {
273 26b4bb70 Philip Gladstone
        }
274 bb4a8772 Michael Niedermayer
    } else {
275 115329f1 Diego Biurrun
        img_copy(picture, &picture1, PIX_FMT_RGBA32, width, height);
276 26b4bb70 Philip Gladstone
    }
277
278
done:
279
    ;
280
}