Statistics
| Branch: | Revision:

ffmpeg / vhook / watermark.c @ 5509bffa

History | View | Annotate | Download (14.4 KB)

1 21754ce6 Michael Niedermayer
/*
2 115329f1 Diego Biurrun
 * Watermark Hook
3 21754ce6 Michael Niedermayer
 * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se
4
 *
5
 * The watermarkpicture works like this. (Assuming colorintencities 0..0xff)
6
 * Per color do this:
7
 * If mask color is 0x80, no change to original frame.
8
 * If mask color is < 0x80 the abs difference is subtracted from frame. If
9
 * result < 0, result = 0
10
 * If mask color is > 0x80 the abs difference is added to frame. If result
11
 * > 0xff, result = 0xff
12
 *
13
 * This way a mask that is visible both in light pictures and in dark can be
14 115329f1 Diego Biurrun
 * made (fex by using a picture generated by gimp and the bump map tool).
15 21754ce6 Michael Niedermayer
 *
16
 * An example watermark file is at
17
 * http://engene.se/ffmpeg_watermark.gif
18
 *
19 ded78ac0 Marcus Engene
 * Example usage:
20
 *  ffmpeg -i infile -vhook '/path/watermark.so -f wm.gif' out.mov
21
 *
22
 * Note that the entire vhook argument is encapsulated in ''. This
23
 * way, arguments to the vhook won't be mixed up with those to ffmpeg.
24
 *
25 21754ce6 Michael Niedermayer
 * This library is free software; you can redistribute it and/or
26
 * modify it under the terms of the GNU Lesser General Public
27
 * License as published by the Free Software Foundation; either
28
 * version 2 of the License, or (at your option) any later version.
29
 *
30
 * This library is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33
 * Lesser General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU Lesser General Public
36
 * License along with this library; if not, write to the Free Software
37 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 21754ce6 Michael Niedermayer
 */
39
40
//#include <stdlib.h>
41
//#include <fcntl.h>
42
#include <unistd.h>
43
#include <stdarg.h>
44 115329f1 Diego Biurrun
45 21754ce6 Michael Niedermayer
#include "common.h"
46
#include "avformat.h"
47
48
#include "framehook.h"
49
50
typedef struct {
51
    char            filename[2000];
52
    int             x_size;
53
    int             y_size;
54
55
    /* get_watermark_picture() variables */
56
    AVFormatContext *pFormatCtx;
57
    const char     *p_ext;
58
    int             videoStream;
59
    int             frameFinished;
60 115329f1 Diego Biurrun
    AVCodecContext *pCodecCtx;
61
    AVCodec        *pCodec;
62 21754ce6 Michael Niedermayer
    AVFrame        *pFrame;
63
    AVPacket        packet;
64
    int             numBytes;
65
    uint8_t        *buffer;
66
    int             i;
67
    AVInputFormat  *file_iformat;
68
    AVStream       *st;
69 115329f1 Diego Biurrun
    int             is_done;
70 21754ce6 Michael Niedermayer
    AVFrame        *pFrameRGB;
71
} ContextInfo;
72
73
int get_watermark_picture(ContextInfo *ci, int cleanup);
74
75
76
/****************************************************************************
77 115329f1 Diego Biurrun
 *
78 21754ce6 Michael Niedermayer
 ****************************************************************************/
79
void Release(void *ctx)
80
{
81
    ContextInfo *ci;
82
    ci = (ContextInfo *) ctx;
83
84
    if (ci) get_watermark_picture(ci, 1);
85 115329f1 Diego Biurrun
86 21754ce6 Michael Niedermayer
    if (ctx)
87
        av_free(ctx);
88
}
89
90
91
/****************************************************************************
92 115329f1 Diego Biurrun
 *
93 21754ce6 Michael Niedermayer
 ****************************************************************************/
94
int Configure(void **ctxp, int argc, char *argv[])
95
{
96
    ContextInfo *ci;
97
    int c;
98
99
    if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1;
100
    ci = (ContextInfo *) *ctxp;
101
102 f368375c Marcus Engene
    optind = 1;
103 115329f1 Diego Biurrun
104 21754ce6 Michael Niedermayer
    // Struct is mallocz:ed so no need to reset.
105 115329f1 Diego Biurrun
106 21754ce6 Michael Niedermayer
    while ((c = getopt(argc, argv, "f:")) > 0) {
107
        switch (c) {
108
            case 'f':
109
                strncpy(ci->filename, optarg, 1999);
110
                ci->filename[1999] = 0;
111
                break;
112
            default:
113 f368375c Marcus Engene
                av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]);
114 21754ce6 Michael Niedermayer
                return -1;
115
        }
116
    }
117 115329f1 Diego Biurrun
118 21754ce6 Michael Niedermayer
    //
119 f368375c Marcus Engene
    if (0 == ci->filename[0]) {
120
        av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n");
121
        return -1;
122
    }
123 115329f1 Diego Biurrun
124 21754ce6 Michael Niedermayer
    av_register_all();
125
    return get_watermark_picture(ci, 0);
126
}
127
128
129
/****************************************************************************
130
 * Why is this a void returning functions? I want to be able to go wrong!
131
 ****************************************************************************/
132 115329f1 Diego Biurrun
void Process(void *ctx,
133
             AVPicture *picture,
134
             enum PixelFormat pix_fmt,
135
             int src_width,
136
             int src_height,
137 21754ce6 Michael Niedermayer
             int64_t pts)
138
{
139
    ContextInfo *ci = (ContextInfo *) ctx;
140
    char *buf = 0;
141
    AVPicture picture1;
142
    AVPicture *pict = picture;
143 115329f1 Diego Biurrun
144 21754ce6 Michael Niedermayer
    AVFrame *pFrameRGB;
145
    int xm_size;
146
    int ym_size;
147
148
//    int retval = -1;
149
    int x;
150
    int y;
151
    int offs, offsm;
152
    int mpoffs;
153
    uint32_t *p_pixel = 0;
154
    uint32_t pixel_meck;
155
    uint32_t pixel;
156
    uint32_t pixelm;
157 115329f1 Diego Biurrun
    int tmp;
158
159
160 21754ce6 Michael Niedermayer
//??    (void) ci;
161
162
    if (pix_fmt != PIX_FMT_RGBA32) {
163
        int size;
164
165
        size = avpicture_get_size(PIX_FMT_RGBA32, src_width, src_height);
166
        buf = av_malloc(size);
167
168
        avpicture_fill(&picture1, buf, PIX_FMT_RGBA32, src_width, src_height);
169 115329f1 Diego Biurrun
        if (img_convert(&picture1, PIX_FMT_RGBA32,
170 21754ce6 Michael Niedermayer
                        picture, pix_fmt, src_width, src_height) < 0) {
171
            av_free(buf);
172
            return;
173
        }
174
        pict = &picture1;
175
    }
176
177
    /* Insert filter code here */ /* ok */
178
179 115329f1 Diego Biurrun
    // Get me next frame
180 21754ce6 Michael Niedermayer
    if (0 > get_watermark_picture(ci, 0)) {
181
        return;
182 115329f1 Diego Biurrun
    }
183 21754ce6 Michael Niedermayer
    // These are the three original static variables in the ffmpeg hack.
184
    pFrameRGB = ci->pFrameRGB;
185
    xm_size = ci->x_size;
186
    ym_size = ci->y_size;
187 115329f1 Diego Biurrun
188 21754ce6 Michael Niedermayer
    // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
189
    // According to avcodec.h PIX_FMT_RGBA32 is handled in endian specific manner.
190
    for (y=0; y<src_height; y++) {
191
        offs = y * (src_width * 4);
192
        offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
193
        for (x=0; x<src_width; x++) {
194
            mpoffs = offsm + (((x * xm_size) / src_width) * 4);
195
            p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
196
            pixelm = *p_pixel;
197
            p_pixel = (uint32_t *)&((pict->data[0])[offs]);
198
            pixel = *p_pixel;
199
//          pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
200
            pixel_meck = pixel & 0xff000000;
201
202
            // R
203
            tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - 0x80;
204
            if (tmp > 255) tmp = 255;
205
            if (tmp < 0) tmp = 0;
206
            pixel_meck |= (tmp << 16) & 0xff0000;
207
            // G
208
            tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - 0x80;
209
            if (tmp > 255) tmp = 255;
210
            if (tmp < 0) tmp = 0;
211
            pixel_meck |= (tmp << 8) & 0xff00;
212
            // B
213
            tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - 0x80;
214
            if (tmp > 255) tmp = 255;
215
            if (tmp < 0) tmp = 0;
216
            pixel_meck |= (tmp << 0) & 0xff;
217 115329f1 Diego Biurrun
218
219 21754ce6 Michael Niedermayer
            // test:
220
            //pixel_meck = pixel & 0xff000000;
221
            //pixel_meck |= (pixelm & 0x00ffffff);
222
223
            *p_pixel = pixel_meck;
224
225 115329f1 Diego Biurrun
            offs += 4;
226 21754ce6 Michael Niedermayer
        } // foreach X
227 115329f1 Diego Biurrun
    } // foreach Y
228
229
230
231 21754ce6 Michael Niedermayer
232
    if (pix_fmt != PIX_FMT_RGBA32) {
233 115329f1 Diego Biurrun
        if (img_convert(picture, pix_fmt,
234 21754ce6 Michael Niedermayer
                        &picture1, PIX_FMT_RGBA32, src_width, src_height) < 0) {
235
        }
236
    }
237
238
    av_free(buf);
239
}
240
241
242
/****************************************************************************
243
 * When cleanup == 0, we try to get the next frame. If no next frame, nothing
244
 * is done.
245
 *
246 115329f1 Diego Biurrun
 * This code follows the example on
247 21754ce6 Michael Niedermayer
 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
248
 *
249
 * 0 = ok, -1 = error
250
 ****************************************************************************/
251
int get_watermark_picture(ContextInfo *ci, int cleanup)
252
{
253
    if (1 == ci->is_done && 0 == cleanup) return 0;
254
255
    // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..
256
    // This block is only executed the first time we enter this function.
257 115329f1 Diego Biurrun
    if (0 == ci->pFrameRGB &&
258
        0 == cleanup)
259 21754ce6 Michael Niedermayer
    {
260 115329f1 Diego Biurrun
261
        /*
262 21754ce6 Michael Niedermayer
         * The last three parameters specify the file format, buffer size and format
263
         * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect
264
         * the format and use a default buffer size. (Didn't work!)
265
         */
266
        if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {
267
268
            // Martin says this should not be necessary but it failed for me sending in
269
            // NULL instead of file_iformat to av_open_input_file()
270
            ci->i = strlen(ci->filename);
271
            if (0 == ci->i) {
272 f368375c Marcus Engene
                av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n");
273 21754ce6 Michael Niedermayer
                return -1;
274
            }
275
            while (ci->i > 0) {
276
                if (ci->filename[ci->i] == '.') {
277
                    ci->i++;
278
                    break;
279
                }
280
                ci->i--;
281
            }
282
               ci->p_ext = &(ci->filename[ci->i]);
283
            ci->file_iformat = av_find_input_format (ci->p_ext);
284
            if (0 == ci->file_iformat) {
285 f368375c Marcus Engene
                av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);
286 21754ce6 Michael Niedermayer
                return -1;
287
            }
288 115329f1 Diego Biurrun
            // now continues the Martin template.
289
290 21754ce6 Michael Niedermayer
            if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {
291 f368375c Marcus Engene
                av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
292 21754ce6 Michael Niedermayer
                return -1;
293 115329f1 Diego Biurrun
            }
294 21754ce6 Michael Niedermayer
        }
295 115329f1 Diego Biurrun
296
        /*
297 21754ce6 Michael Niedermayer
         * This fills the streams field of the AVFormatContext with valid information.
298
         */
299
        if(av_find_stream_info(ci->pFormatCtx)<0) {
300 f368375c Marcus Engene
            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n");
301 21754ce6 Michael Niedermayer
            return -1;
302
        }
303 115329f1 Diego Biurrun
304 21754ce6 Michael Niedermayer
        /*
305 115329f1 Diego Biurrun
         * As mentioned in the introduction, we'll handle only video streams, not audio
306 21754ce6 Michael Niedermayer
         * streams. To make things nice and easy, we simply use the first video stream we
307
         * find.
308
         */
309
        ci->videoStream=-1;
310
        for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)
311 a2cfc4d6 Mike Melanson
            if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)
312 21754ce6 Michael Niedermayer
            {
313
                ci->videoStream = ci->i;
314
                break;
315
            }
316
        if(ci->videoStream == -1) {
317 f368375c Marcus Engene
            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n");
318 21754ce6 Michael Niedermayer
            return -1;
319
        }
320 115329f1 Diego Biurrun
321 21754ce6 Michael Niedermayer
        ci->st = ci->pFormatCtx->streams[ci->videoStream];
322 a2cfc4d6 Mike Melanson
        ci->x_size = ci->st->codec->width;
323
        ci->y_size = ci->st->codec->height;
324 115329f1 Diego Biurrun
325 21754ce6 Michael Niedermayer
        // Get a pointer to the codec context for the video stream
326 a2cfc4d6 Mike Melanson
        ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;
327 115329f1 Diego Biurrun
328
329 21754ce6 Michael Niedermayer
        /*
330
         * OK, so now we've got a pointer to the so-called codec context for our video
331
         * stream, but we still have to find the actual codec and open it.
332 115329f1 Diego Biurrun
         */
333 21754ce6 Michael Niedermayer
        // Find the decoder for the video stream
334
        ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);
335
        if(ci->pCodec == NULL) {
336 f368375c Marcus Engene
            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n");
337 21754ce6 Michael Niedermayer
            return -1;
338
        }
339 115329f1 Diego Biurrun
340 21754ce6 Michael Niedermayer
        // Inform the codec that we can handle truncated bitstreams -- i.e.,
341
        // bitstreams where frame boundaries can fall in the middle of packets
342
        if (ci->pCodec->capabilities & CODEC_CAP_TRUNCATED)
343
            ci->pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
344 115329f1 Diego Biurrun
345 21754ce6 Michael Niedermayer
        // Open codec
346
        if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {
347 f368375c Marcus Engene
            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n");
348 21754ce6 Michael Niedermayer
            return -1;
349
        }
350 115329f1 Diego Biurrun
351
        // Hack to correct wrong frame rates that seem to be generated by some
352 21754ce6 Michael Niedermayer
        // codecs
353 c0df9d75 Michael Niedermayer
        if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)
354 115329f1 Diego Biurrun
            ci->pCodecCtx->time_base.num=1000;
355
356 21754ce6 Michael Niedermayer
        /*
357
         * Allocate a video frame to store the decoded images in.
358
         */
359
        ci->pFrame = avcodec_alloc_frame();
360 115329f1 Diego Biurrun
361
362 21754ce6 Michael Niedermayer
        /*
363
         * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
364
         */
365
        // Allocate an AVFrame structure
366
        ci->pFrameRGB=avcodec_alloc_frame();
367
        if(ci->pFrameRGB==NULL) {
368 f368375c Marcus Engene
            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n");
369 21754ce6 Michael Niedermayer
            return -1;
370
        }
371 115329f1 Diego Biurrun
372 21754ce6 Michael Niedermayer
        // Determine required buffer size and allocate buffer
373
        ci->numBytes = avpicture_get_size(PIX_FMT_RGBA32, ci->pCodecCtx->width,
374
            ci->pCodecCtx->height);
375
        ci->buffer = av_malloc(ci->numBytes);
376 115329f1 Diego Biurrun
377 21754ce6 Michael Niedermayer
        // Assign appropriate parts of buffer to image planes in pFrameRGB
378
        avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGBA32,
379 115329f1 Diego Biurrun
            ci->pCodecCtx->width, ci->pCodecCtx->height);
380 21754ce6 Michael Niedermayer
    }
381
    // TODO loop, pingpong etc?
382 115329f1 Diego Biurrun
    if (0 == cleanup)
383
    {
384 21754ce6 Michael Niedermayer
//        av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
385
        while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)
386
        {
387
            // Is this a packet from the video stream?
388
            if(ci->packet.stream_index == ci->videoStream)
389
            {
390
                // Decode video frame
391 115329f1 Diego Biurrun
                avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,
392 21754ce6 Michael Niedermayer
                    ci->packet.data, ci->packet.size);
393 115329f1 Diego Biurrun
394 21754ce6 Michael Niedermayer
                // Did we get a video frame?
395
                if(ci->frameFinished)
396
                {
397
                    // Convert the image from its native format to RGBA32
398 115329f1 Diego Biurrun
                    img_convert((AVPicture *)ci->pFrameRGB, PIX_FMT_RGBA32,
399
                        (AVPicture*)(ci->pFrame), ci->pCodecCtx->pix_fmt, ci->pCodecCtx->width,
400 21754ce6 Michael Niedermayer
                        ci->pCodecCtx->height);
401 115329f1 Diego Biurrun
402 21754ce6 Michael Niedermayer
                    // Process the video frame (save to disk etc.)
403
                    //fprintf(stderr,"banan() New frame!\n");
404
                    //DoSomethingWithTheImage(ci->pFrameRGB);
405
                    return 0;
406
                }
407
            }
408 115329f1 Diego Biurrun
409 21754ce6 Michael Niedermayer
            // Free the packet that was allocated by av_read_frame
410
            av_free_packet(&ci->packet);
411
        }
412 115329f1 Diego Biurrun
        ci->is_done = 1;
413 21754ce6 Michael Niedermayer
        return 0;
414
    } // if 0 != cleanup
415 115329f1 Diego Biurrun
416
    if (0 != cleanup)
417 21754ce6 Michael Niedermayer
    {
418
        // Free the RGB image
419
        if (0 != ci->buffer) {
420
            av_free(ci->buffer);
421
            ci->buffer = 0;
422
        }
423
        if (0 != ci->pFrameRGB) {
424
            av_free(ci->pFrameRGB);
425
            ci->pFrameRGB = 0;
426 115329f1 Diego Biurrun
        }
427
428 21754ce6 Michael Niedermayer
        // Close the codec
429
        if (0 != ci->pCodecCtx) {
430
            avcodec_close(ci->pCodecCtx);
431
            ci->pCodecCtx = 0;
432
        }
433 115329f1 Diego Biurrun
434 21754ce6 Michael Niedermayer
        // Close the video file
435
        if (0 != ci->pFormatCtx) {
436 115329f1 Diego Biurrun
            av_close_input_file(ci->pFormatCtx);
437 21754ce6 Michael Niedermayer
            ci->pFormatCtx = 0;
438
        }
439 115329f1 Diego Biurrun
440 21754ce6 Michael Niedermayer
        ci->is_done = 0;
441
    }
442
    return 0;
443
}
444
445
446
void parse_arg_file(const char *filename)
447
{
448
}