add static keyword to some functions
[libav.git] / vhook / watermark.c
CommitLineData
21754ce6 1/*
115329f1 2 * Watermark Hook
21754ce6
MN
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
115329f1 14 * made (fex by using a picture generated by gimp and the bump map tool).
21754ce6
MN
15 *
16 * An example watermark file is at
17 * http://engene.se/ffmpeg_watermark.gif
18 *
ded78ac0
ME
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 *
21754ce6
MN
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
5509bffa 37 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21754ce6
MN
38 */
39
40//#include <stdlib.h>
41//#include <fcntl.h>
42#include <unistd.h>
43#include <stdarg.h>
115329f1 44
21754ce6
MN
45#include "common.h"
46#include "avformat.h"
47
48#include "framehook.h"
a851b8e8 49#include "cmdutils.h"
21754ce6
MN
50
51typedef 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;
115329f1
DB
61 AVCodecContext *pCodecCtx;
62 AVCodec *pCodec;
21754ce6
MN
63 AVFrame *pFrame;
64 AVPacket packet;
65 int numBytes;
66 uint8_t *buffer;
67 int i;
68 AVInputFormat *file_iformat;
69 AVStream *st;
115329f1 70 int is_done;
21754ce6
MN
71 AVFrame *pFrameRGB;
72} ContextInfo;
73
74int get_watermark_picture(ContextInfo *ci, int cleanup);
75
76
77/****************************************************************************
115329f1 78 *
21754ce6
MN
79 ****************************************************************************/
80void Release(void *ctx)
81{
82 ContextInfo *ci;
83 ci = (ContextInfo *) ctx;
84
85 if (ci) get_watermark_picture(ci, 1);
115329f1 86
21754ce6
MN
87 if (ctx)
88 av_free(ctx);
89}
90
91
92/****************************************************************************
115329f1 93 *
21754ce6
MN
94 ****************************************************************************/
95int 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
f368375c 103 optind = 1;
115329f1 104
21754ce6 105 // Struct is mallocz:ed so no need to reset.
115329f1 106
21754ce6
MN
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:
f368375c 114 av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]);
21754ce6
MN
115 return -1;
116 }
117 }
115329f1 118
21754ce6 119 //
f368375c
ME
120 if (0 == ci->filename[0]) {
121 av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n");
122 return -1;
123 }
115329f1 124
21754ce6
MN
125 av_register_all();
126 return get_watermark_picture(ci, 0);
21754ce6
MN
127}
128
129
130/****************************************************************************
131 * Why is this a void returning functions? I want to be able to go wrong!
132 ****************************************************************************/
115329f1
DB
133void Process(void *ctx,
134 AVPicture *picture,
135 enum PixelFormat pix_fmt,
136 int src_width,
137 int src_height,
21754ce6
MN
138 int64_t pts)
139{
140 ContextInfo *ci = (ContextInfo *) ctx;
141 char *buf = 0;
142 AVPicture picture1;
143 AVPicture *pict = picture;
115329f1 144
21754ce6
MN
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;
115329f1
DB
158 int tmp;
159
160
21754ce6
MN
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);
115329f1 170 if (img_convert(&picture1, PIX_FMT_RGBA32,
21754ce6
MN
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
115329f1 180 // Get me next frame
21754ce6
MN
181 if (0 > get_watermark_picture(ci, 0)) {
182 return;
115329f1 183 }
21754ce6
MN
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;
115329f1 188
21754ce6
MN
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;
115329f1
DB
218
219
21754ce6
MN
220 // test:
221 //pixel_meck = pixel & 0xff000000;
222 //pixel_meck |= (pixelm & 0x00ffffff);
223
224 *p_pixel = pixel_meck;
225
115329f1 226 offs += 4;
21754ce6 227 } // foreach X
115329f1
DB
228 } // foreach Y
229
230
231
21754ce6
MN
232
233 if (pix_fmt != PIX_FMT_RGBA32) {
115329f1 234 if (img_convert(picture, pix_fmt,
21754ce6
MN
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 *
115329f1 247 * This code follows the example on
21754ce6
MN
248 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
249 *
250 * 0 = ok, -1 = error
251 ****************************************************************************/
252int 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.
115329f1
DB
258 if (0 == ci->pFrameRGB &&
259 0 == cleanup)
21754ce6 260 {
115329f1
DB
261
262 /*
21754ce6
MN
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) {
f368375c 273 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n");
21754ce6
MN
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) {
f368375c 286 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);
21754ce6
MN
287 return -1;
288 }
115329f1
DB
289 // now continues the Martin template.
290
21754ce6 291 if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {
f368375c 292 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
21754ce6 293 return -1;
115329f1 294 }
21754ce6 295 }
115329f1
DB
296
297 /*
21754ce6
MN
298 * This fills the streams field of the AVFormatContext with valid information.
299 */
300 if(av_find_stream_info(ci->pFormatCtx)<0) {
f368375c 301 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n");
21754ce6
MN
302 return -1;
303 }
115329f1 304
21754ce6 305 /*
115329f1 306 * As mentioned in the introduction, we'll handle only video streams, not audio
21754ce6
MN
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++)
a2cfc4d6 312 if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)
21754ce6
MN
313 {
314 ci->videoStream = ci->i;
315 break;
316 }
317 if(ci->videoStream == -1) {
f368375c 318 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n");
21754ce6
MN
319 return -1;
320 }
115329f1 321
21754ce6 322 ci->st = ci->pFormatCtx->streams[ci->videoStream];
a2cfc4d6
MM
323 ci->x_size = ci->st->codec->width;
324 ci->y_size = ci->st->codec->height;
115329f1 325
21754ce6 326 // Get a pointer to the codec context for the video stream
a2cfc4d6 327 ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;
115329f1
DB
328
329
21754ce6
MN
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.
115329f1 333 */
21754ce6
MN
334 // Find the decoder for the video stream
335 ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);
336 if(ci->pCodec == NULL) {
f368375c 337 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n");
21754ce6
MN
338 return -1;
339 }
115329f1 340
21754ce6
MN
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;
115329f1 345
21754ce6
MN
346 // Open codec
347 if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {
f368375c 348 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n");
21754ce6
MN
349 return -1;
350 }
115329f1
DB
351
352 // Hack to correct wrong frame rates that seem to be generated by some
21754ce6 353 // codecs
c0df9d75 354 if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)
115329f1
DB
355 ci->pCodecCtx->time_base.num=1000;
356
21754ce6
MN
357 /*
358 * Allocate a video frame to store the decoded images in.
359 */
360 ci->pFrame = avcodec_alloc_frame();
115329f1
DB
361
362
21754ce6
MN
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) {
f368375c 369 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n");
21754ce6
MN
370 return -1;
371 }
115329f1 372
21754ce6
MN
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);
115329f1 377
21754ce6
MN
378 // Assign appropriate parts of buffer to image planes in pFrameRGB
379 avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGBA32,
115329f1 380 ci->pCodecCtx->width, ci->pCodecCtx->height);
21754ce6
MN
381 }
382 // TODO loop, pingpong etc?
115329f1
DB
383 if (0 == cleanup)
384 {
21754ce6
MN
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
115329f1 392 avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,
21754ce6 393 ci->packet.data, ci->packet.size);
115329f1 394
21754ce6
MN
395 // Did we get a video frame?
396 if(ci->frameFinished)
397 {
398 // Convert the image from its native format to RGBA32
115329f1
DB
399 img_convert((AVPicture *)ci->pFrameRGB, PIX_FMT_RGBA32,
400 (AVPicture*)(ci->pFrame), ci->pCodecCtx->pix_fmt, ci->pCodecCtx->width,
21754ce6 401 ci->pCodecCtx->height);
115329f1 402
21754ce6
MN
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 }
115329f1 409
21754ce6
MN
410 // Free the packet that was allocated by av_read_frame
411 av_free_packet(&ci->packet);
412 }
115329f1 413 ci->is_done = 1;
21754ce6
MN
414 return 0;
415 } // if 0 != cleanup
115329f1
DB
416
417 if (0 != cleanup)
21754ce6
MN
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;
115329f1
DB
427 }
428
21754ce6
MN
429 // Close the codec
430 if (0 != ci->pCodecCtx) {
431 avcodec_close(ci->pCodecCtx);
432 ci->pCodecCtx = 0;
433 }
115329f1 434
21754ce6
MN
435 // Close the video file
436 if (0 != ci->pFormatCtx) {
115329f1 437 av_close_input_file(ci->pFormatCtx);
21754ce6
MN
438 ci->pFormatCtx = 0;
439 }
115329f1 440
21754ce6
MN
441 ci->is_done = 0;
442 }
443 return 0;
444}
445
446
447void parse_arg_file(const char *filename)
448{
449}