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