Simplify eq()
[ffmpeg-lucabe.git] / vhook / watermark.c
blob74f50953139094f3cda6a9b446820e5992544467
1 /*
2 * Watermark Hook
3 * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se
5 * parameters for 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 watermark image filename. You must specify this!
10 * MODE 0:
11 * The watermark picture works like this (assuming color intensities 0..0xff):
12 * Per color do this:
13 * If mask color is 0x80, no change to the original frame.
14 * If mask color is < 0x80 the abs difference is subtracted from the frame. If
15 * result < 0, result = 0
16 * If mask color is > 0x80 the abs difference is added to the frame. If result
17 * > 0xff, result = 0xff
19 * You can override the 0x80 level with the -t flag. E.g. if threshold is
20 * 000000 the color value of watermark is added to the destination.
22 * This way a mask that is visible both in light pictures and in dark can be
23 * made (fex by using a picture generated by Gimp and the bump map tool).
25 * An example watermark file is at
26 * http://engene.se/ffmpeg_watermark.gif
28 * MODE 1:
29 * Per color do this:
30 * If mask color > threshold color then the watermark pixel is used.
32 * Example usage:
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
36 * Note that the entire vhook argument is encapsulated in ''. This
37 * way, arguments to the vhook won't be mixed up with those for ffmpeg.
39 * This file is part of FFmpeg.
41 * FFmpeg is free software; you can redistribute it and/or
42 * modify it under the terms of the GNU Lesser General Public
43 * License as published by the Free Software Foundation; either
44 * version 2.1 of the License, or (at your option) any later version.
46 * FFmpeg is distributed in the hope that it will be useful,
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.
51 * You should have received a copy of the GNU Lesser General Public
52 * License along with FFmpeg; if not, write to the Free Software
53 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
56 #include <stdlib.h>
57 //#include <fcntl.h>
58 #include <unistd.h>
59 #include <stdarg.h>
61 #include "libavutil/common.h"
62 #include "libavformat/avformat.h"
63 #include "libavformat/framehook.h"
64 #include "libswscale/swscale.h"
65 #include "cmdutils.h"
67 static int sws_flags = SWS_BICUBIC;
69 typedef struct {
70 char filename[2000];
71 int x_size;
72 int y_size;
74 /* get_watermark_picture() variables */
75 AVFormatContext *pFormatCtx;
76 const char *p_ext;
77 int videoStream;
78 int frameFinished;
79 AVCodecContext *pCodecCtx;
80 AVCodec *pCodec;
81 AVFrame *pFrame;
82 AVPacket packet;
83 int numBytes;
84 uint8_t *buffer;
85 int i;
86 AVInputFormat *file_iformat;
87 AVStream *st;
88 int is_done;
89 AVFrame *pFrameRGB;
90 int thrR;
91 int thrG;
92 int thrB;
93 int mode;
95 // This vhook first converts frame to RGB ...
96 struct SwsContext *toRGB_convert_ctx;
97 // ... then converts a watermark and applies it to the RGB frame ...
98 struct SwsContext *watermark_convert_ctx;
99 // ... and finally converts back frame from RGB to initial format
100 struct SwsContext *fromRGB_convert_ctx;
101 } ContextInfo;
103 int get_watermark_picture(ContextInfo *ci, int cleanup);
106 /****************************************************************************
108 ****************************************************************************/
109 void Release(void *ctx)
111 ContextInfo *ci;
112 ci = (ContextInfo *) ctx;
114 if (ci) {
115 get_watermark_picture(ci, 1);
116 sws_freeContext(ci->toRGB_convert_ctx);
117 sws_freeContext(ci->watermark_convert_ctx);
118 sws_freeContext(ci->fromRGB_convert_ctx);
120 av_free(ctx);
124 /****************************************************************************
126 ****************************************************************************/
127 int Configure(void **ctxp, int argc, char *argv[])
129 ContextInfo *ci;
130 int c;
131 int tmp = 0;
133 if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1;
134 ci = (ContextInfo *) *ctxp;
136 optind = 1;
138 // Struct is mallocz:ed so no need to reset.
139 ci->thrR = 0x80;
140 ci->thrG = 0x80;
141 ci->thrB = 0x80;
143 while ((c = getopt(argc, argv, "f:m:t:")) > 0) {
144 switch (c) {
145 case 'f':
146 strncpy(ci->filename, optarg, 1999);
147 ci->filename[1999] = 0;
148 break;
149 case 'm':
150 ci->mode = atoi(optarg);
151 break;
152 case 't':
153 if (1 != sscanf(optarg, "%x", &tmp)) {
154 av_log(NULL, AV_LOG_ERROR, "Watermark: argument to -t must be a 6 digit hex number\n");
155 return -1;
157 ci->thrR = (tmp >> 16) & 0xff;
158 ci->thrG = (tmp >> 8) & 0xff;
159 ci->thrB = (tmp >> 0) & 0xff;
160 break;
161 default:
162 av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]);
163 return -1;
168 if (0 == ci->filename[0]) {
169 av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n");
170 return -1;
173 av_register_all();
174 return get_watermark_picture(ci, 0);
178 /****************************************************************************
179 * For mode 0 (the original one)
180 ****************************************************************************/
181 static void Process0(void *ctx,
182 AVPicture *picture,
183 enum PixelFormat pix_fmt,
184 int src_width,
185 int src_height,
186 int64_t pts)
188 ContextInfo *ci = (ContextInfo *) ctx;
189 char *buf = 0;
190 AVPicture picture1;
191 AVPicture *pict = picture;
193 AVFrame *pFrameRGB;
194 int xm_size;
195 int ym_size;
197 int x;
198 int y;
199 int offs, offsm;
200 int mpoffs;
201 uint32_t *p_pixel = 0;
202 uint32_t pixel_meck;
203 uint32_t pixel;
204 uint32_t pixelm;
205 int tmp;
206 int thrR = ci->thrR;
207 int thrG = ci->thrG;
208 int thrB = ci->thrB;
210 if (pix_fmt != PIX_FMT_RGB32) {
211 int size;
213 size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height);
214 buf = av_malloc(size);
216 avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height);
218 // if we already got a SWS context, let's realloc if is not re-useable
219 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
220 src_width, src_height, pix_fmt,
221 src_width, src_height, PIX_FMT_RGB32,
222 sws_flags, NULL, NULL, NULL);
223 if (ci->toRGB_convert_ctx == NULL) {
224 av_log(NULL, AV_LOG_ERROR,
225 "Cannot initialize the toRGB conversion context\n");
226 return;
229 // img_convert parameters are 2 first destination, then 4 source
230 // sws_scale parameters are context, 4 first source, then 2 destination
231 sws_scale(ci->toRGB_convert_ctx,
232 picture->data, picture->linesize, 0, src_height,
233 picture1.data, picture1.linesize);
235 pict = &picture1;
238 /* Insert filter code here */ /* ok */
240 // Get me next frame
241 if (0 > get_watermark_picture(ci, 0)) {
242 return;
244 // These are the three original static variables in the ffmpeg hack.
245 pFrameRGB = ci->pFrameRGB;
246 xm_size = ci->x_size;
247 ym_size = ci->y_size;
249 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
250 // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
251 for (y=0; y<src_height; y++) {
252 offs = y * (src_width * 4);
253 offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
254 for (x=0; x<src_width; x++) {
255 mpoffs = offsm + (((x * xm_size) / src_width) * 4);
256 p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
257 pixelm = *p_pixel;
258 p_pixel = (uint32_t *)&((pict->data[0])[offs]);
259 pixel = *p_pixel;
260 // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
261 pixel_meck = pixel & 0xff000000;
263 // R
264 tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - thrR;
265 if (tmp > 255) tmp = 255;
266 if (tmp < 0) tmp = 0;
267 pixel_meck |= (tmp << 16) & 0xff0000;
268 // G
269 tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - thrG;
270 if (tmp > 255) tmp = 255;
271 if (tmp < 0) tmp = 0;
272 pixel_meck |= (tmp << 8) & 0xff00;
273 // B
274 tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - thrB;
275 if (tmp > 255) tmp = 255;
276 if (tmp < 0) tmp = 0;
277 pixel_meck |= (tmp << 0) & 0xff;
280 // test:
281 //pixel_meck = pixel & 0xff000000;
282 //pixel_meck |= (pixelm & 0x00ffffff);
284 *p_pixel = pixel_meck;
286 offs += 4;
287 } // foreach X
288 } // foreach Y
293 if (pix_fmt != PIX_FMT_RGB32) {
294 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
295 src_width, src_height, PIX_FMT_RGB32,
296 src_width, src_height, pix_fmt,
297 sws_flags, NULL, NULL, NULL);
298 if (ci->fromRGB_convert_ctx == NULL) {
299 av_log(NULL, AV_LOG_ERROR,
300 "Cannot initialize the fromRGB conversion context\n");
301 return;
303 // img_convert parameters are 2 first destination, then 4 source
304 // sws_scale parameters are context, 4 first source, then 2 destination
305 sws_scale(ci->fromRGB_convert_ctx,
306 picture1.data, picture1.linesize, 0, src_height,
307 picture->data, picture->linesize);
310 av_free(buf);
314 /****************************************************************************
315 * For mode 1 (the original one)
316 ****************************************************************************/
317 static void Process1(void *ctx,
318 AVPicture *picture,
319 enum PixelFormat pix_fmt,
320 int src_width,
321 int src_height,
322 int64_t pts)
324 ContextInfo *ci = (ContextInfo *) ctx;
325 char *buf = 0;
326 AVPicture picture1;
327 AVPicture *pict = picture;
329 AVFrame *pFrameRGB;
330 int xm_size;
331 int ym_size;
333 int x;
334 int y;
335 int offs, offsm;
336 int mpoffs;
337 uint32_t *p_pixel = 0;
338 uint32_t pixel;
339 uint32_t pixelm;
341 if (pix_fmt != PIX_FMT_RGB32) {
342 int size;
344 size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height);
345 buf = av_malloc(size);
347 avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height);
349 // if we already got a SWS context, let's realloc if is not re-useable
350 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
351 src_width, src_height, pix_fmt,
352 src_width, src_height, PIX_FMT_RGB32,
353 sws_flags, NULL, NULL, NULL);
354 if (ci->toRGB_convert_ctx == NULL) {
355 av_log(NULL, AV_LOG_ERROR,
356 "Cannot initialize the toRGB conversion context\n");
357 return;
360 // img_convert parameters are 2 first destination, then 4 source
361 // sws_scale parameters are context, 4 first source, then 2 destination
362 sws_scale(ci->toRGB_convert_ctx,
363 picture->data, picture->linesize, 0, src_height,
364 picture1.data, picture1.linesize);
366 pict = &picture1;
369 /* Insert filter code here */ /* ok */
371 // Get me next frame
372 if (0 > get_watermark_picture(ci, 0)) {
373 return;
375 // These are the three original static variables in the ffmpeg hack.
376 pFrameRGB = ci->pFrameRGB;
377 xm_size = ci->x_size;
378 ym_size = ci->y_size;
380 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
381 // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
382 for (y=0; y<src_height; y++) {
383 offs = y * (src_width * 4);
384 offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
385 for (x=0; x<src_width; x++) {
386 mpoffs = offsm + (((x * xm_size) / src_width) * 4);
387 p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
388 pixelm = *p_pixel; /* watermark pixel */
389 p_pixel = (uint32_t *)&((pict->data[0])[offs]);
390 pixel = *p_pixel;
392 if (((pixelm >> 16) & 0xff) > ci->thrR ||
393 ((pixelm >> 8) & 0xff) > ci->thrG ||
394 ((pixelm >> 0) & 0xff) > ci->thrB)
396 *p_pixel = pixelm;
397 } else {
398 *p_pixel = pixel;
400 offs += 4;
401 } // foreach X
402 } // foreach Y
404 if (pix_fmt != PIX_FMT_RGB32) {
405 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
406 src_width, src_height, PIX_FMT_RGB32,
407 src_width, src_height, pix_fmt,
408 sws_flags, NULL, NULL, NULL);
409 if (ci->fromRGB_convert_ctx == NULL) {
410 av_log(NULL, AV_LOG_ERROR,
411 "Cannot initialize the fromRGB conversion context\n");
412 return;
414 // img_convert parameters are 2 first destination, then 4 source
415 // sws_scale parameters are context, 4 first source, then 2 destination
416 sws_scale(ci->fromRGB_convert_ctx,
417 picture1.data, picture1.linesize, 0, src_height,
418 picture->data, picture->linesize);
421 av_free(buf);
425 /****************************************************************************
426 * This is the function ffmpeg.c callbacks.
427 ****************************************************************************/
428 void Process(void *ctx,
429 AVPicture *picture,
430 enum PixelFormat pix_fmt,
431 int src_width,
432 int src_height,
433 int64_t pts)
435 ContextInfo *ci = (ContextInfo *) ctx;
436 if (1 == ci->mode) {
437 Process1(ctx, picture, pix_fmt, src_width, src_height, pts);
438 } else {
439 Process0(ctx, picture, pix_fmt, src_width, src_height, pts);
444 /****************************************************************************
445 * When cleanup == 0, we try to get the next frame. If no next frame, nothing
446 * is done.
448 * This code follows the example on
449 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
451 * 0 = ok, -1 = error
452 ****************************************************************************/
453 int get_watermark_picture(ContextInfo *ci, int cleanup)
455 if (1 == ci->is_done && 0 == cleanup) return 0;
457 // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..
458 // This block is only executed the first time we enter this function.
459 if (0 == ci->pFrameRGB &&
460 0 == cleanup)
464 * The last three parameters specify the file format, buffer size and format
465 * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect
466 * the format and use a default buffer size. (Didn't work!)
468 if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {
470 // Martin says this should not be necessary but it failed for me sending in
471 // NULL instead of file_iformat to av_open_input_file()
472 ci->i = strlen(ci->filename);
473 if (0 == ci->i) {
474 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n");
475 return -1;
477 while (ci->i > 0) {
478 if (ci->filename[ci->i] == '.') {
479 ci->i++;
480 break;
482 ci->i--;
484 ci->p_ext = &(ci->filename[ci->i]);
485 ci->file_iformat = av_find_input_format (ci->p_ext);
486 if (0 == ci->file_iformat) {
487 av_log(NULL, AV_LOG_INFO, "get_watermark_picture() attempt to use image2 for [%s]\n", ci->p_ext);
488 ci->file_iformat = av_find_input_format ("image2");
490 if (0 == ci->file_iformat) {
491 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);
492 return -1;
494 // now continues the Martin template.
496 if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {
497 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
498 return -1;
503 * This fills the streams field of the AVFormatContext with valid information.
505 if(av_find_stream_info(ci->pFormatCtx)<0) {
506 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n");
507 return -1;
511 * As mentioned in the introduction, we'll handle only video streams, not audio
512 * streams. To make things nice and easy, we simply use the first video stream we
513 * find.
515 ci->videoStream=-1;
516 for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)
517 if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)
519 ci->videoStream = ci->i;
520 break;
522 if(ci->videoStream == -1) {
523 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n");
524 return -1;
527 ci->st = ci->pFormatCtx->streams[ci->videoStream];
528 ci->x_size = ci->st->codec->width;
529 ci->y_size = ci->st->codec->height;
531 // Get a pointer to the codec context for the video stream
532 ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;
536 * OK, so now we've got a pointer to the so-called codec context for our video
537 * stream, but we still have to find the actual codec and open it.
539 // Find the decoder for the video stream
540 ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);
541 if(ci->pCodec == NULL) {
542 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n");
543 return -1;
547 // Open codec
548 if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {
549 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n");
550 return -1;
553 // Hack to correct wrong frame rates that seem to be generated by some
554 // codecs
555 if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)
556 ci->pCodecCtx->time_base.num=1000;
559 * Allocate a video frame to store the decoded images in.
561 ci->pFrame = avcodec_alloc_frame();
565 * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
567 // Allocate an AVFrame structure
568 ci->pFrameRGB=avcodec_alloc_frame();
569 if(ci->pFrameRGB==NULL) {
570 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n");
571 return -1;
574 // Determine required buffer size and allocate buffer
575 ci->numBytes = avpicture_get_size(PIX_FMT_RGB32, ci->pCodecCtx->width,
576 ci->pCodecCtx->height);
577 ci->buffer = av_malloc(ci->numBytes);
579 // Assign appropriate parts of buffer to image planes in pFrameRGB
580 avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGB32,
581 ci->pCodecCtx->width, ci->pCodecCtx->height);
583 // TODO loop, pingpong etc?
584 if (0 == cleanup)
586 // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
587 while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)
589 // Is this a packet from the video stream?
590 if(ci->packet.stream_index == ci->videoStream)
592 // Decode video frame
593 avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,
594 ci->packet.data, ci->packet.size);
596 // Did we get a video frame?
597 if(ci->frameFinished)
599 // Convert the image from its native format to RGB32
600 ci->watermark_convert_ctx =
601 sws_getCachedContext(ci->watermark_convert_ctx,
602 ci->pCodecCtx->width, ci->pCodecCtx->height, ci->pCodecCtx->pix_fmt,
603 ci->pCodecCtx->width, ci->pCodecCtx->height, PIX_FMT_RGB32,
604 sws_flags, NULL, NULL, NULL);
605 if (ci->watermark_convert_ctx == NULL) {
606 av_log(NULL, AV_LOG_ERROR,
607 "Cannot initialize the watermark conversion context\n");
608 return -1;
610 // img_convert parameters are 2 first destination, then 4 source
611 // sws_scale parameters are context, 4 first source, then 2 destination
612 sws_scale(ci->watermark_convert_ctx,
613 ci->pFrame->data, ci->pFrame->linesize, 0, ci->pCodecCtx->height,
614 ci->pFrameRGB->data, ci->pFrameRGB->linesize);
616 // Process the video frame (save to disk etc.)
617 //fprintf(stderr,"banan() New frame!\n");
618 //DoSomethingWithTheImage(ci->pFrameRGB);
619 return 0;
623 // Free the packet that was allocated by av_read_frame
624 av_free_packet(&ci->packet);
626 ci->is_done = 1;
627 return 0;
628 } // if 0 != cleanup
630 if (0 != cleanup)
632 // Free the RGB image
633 av_freep(&ci->buffer);
634 av_freep(&ci->pFrameRGB);
636 // Close the codec
637 if (0 != ci->pCodecCtx) {
638 avcodec_close(ci->pCodecCtx);
639 ci->pCodecCtx = 0;
642 // Close the video file
643 if (0 != ci->pFormatCtx) {
644 av_close_input_file(ci->pFormatCtx);
645 ci->pFormatCtx = 0;
648 ci->is_done = 0;
650 return 0;
654 void parse_arg_file(const char *filename)