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