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!
11 * The watermark picture works like this (assuming color intensities 0..0xff):
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
30 * If mask color > threshold color then the watermark pixel is used.
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
61 #include "libavutil/common.h"
62 #include "libavformat/avformat.h"
63 #include "libavformat/framehook.h"
64 #include "libswscale/swscale.h"
66 static int sws_flags = SWS_BICUBIC;
73 /* get_watermark_picture() variables */
74 AVFormatContext *pFormatCtx;
78 AVCodecContext *pCodecCtx;
85 AVInputFormat *file_iformat;
94 // This vhook first converts frame to RGB ...
95 struct SwsContext *toRGB_convert_ctx;
96 // ... then converts a watermark and applies it to the RGB frame ...
97 struct SwsContext *watermark_convert_ctx;
98 // ... and finally converts back frame from RGB to initial format
99 struct SwsContext *fromRGB_convert_ctx;
102 int get_watermark_picture(ContextInfo *ci, int cleanup);
105 /****************************************************************************
107 ****************************************************************************/
108 void Release(void *ctx)
111 ci = (ContextInfo *) ctx;
114 get_watermark_picture(ci, 1);
115 sws_freeContext(ci->toRGB_convert_ctx);
116 sws_freeContext(ci->watermark_convert_ctx);
117 sws_freeContext(ci->fromRGB_convert_ctx);
123 /****************************************************************************
125 ****************************************************************************/
126 int Configure(void **ctxp, int argc, char *argv[])
132 if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1;
133 ci = (ContextInfo *) *ctxp;
137 // Struct is mallocz:ed so no need to reset.
142 while ((c = getopt(argc, argv, "f:m:t:")) > 0) {
145 strncpy(ci->filename, optarg, 1999);
146 ci->filename[1999] = 0;
149 ci->mode = atoi(optarg);
152 if (1 != sscanf(optarg, "%x", &tmp)) {
153 av_log(NULL, AV_LOG_ERROR, "Watermark: argument to -t must be a 6 digit hex number\n");
156 ci->thrR = (tmp >> 16) & 0xff;
157 ci->thrG = (tmp >> 8) & 0xff;
158 ci->thrB = (tmp >> 0) & 0xff;
161 av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]);
167 if (0 == ci->filename[0]) {
168 av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n");
173 return get_watermark_picture(ci, 0);
177 /****************************************************************************
178 * For mode 0 (the original one)
179 ****************************************************************************/
180 static void Process0(void *ctx,
182 enum PixelFormat pix_fmt,
187 ContextInfo *ci = (ContextInfo *) ctx;
190 AVPicture *pict = picture;
200 uint32_t *p_pixel = 0;
209 if (pix_fmt != PIX_FMT_RGB32) {
212 size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height);
213 buf = av_malloc(size);
215 avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height);
217 // if we already got a SWS context, let's realloc if is not re-useable
218 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
219 src_width, src_height, pix_fmt,
220 src_width, src_height, PIX_FMT_RGB32,
221 sws_flags, NULL, NULL, NULL);
222 if (ci->toRGB_convert_ctx == NULL) {
223 av_log(NULL, AV_LOG_ERROR,
224 "Cannot initialize the toRGB conversion context\n");
228 // img_convert parameters are 2 first destination, then 4 source
229 // sws_scale parameters are context, 4 first source, then 2 destination
230 sws_scale(ci->toRGB_convert_ctx,
231 picture->data, picture->linesize, 0, src_height,
232 picture1.data, picture1.linesize);
237 /* Insert filter code here */ /* ok */
240 if (0 > get_watermark_picture(ci, 0)) {
243 // These are the three original static variables in the ffmpeg hack.
244 pFrameRGB = ci->pFrameRGB;
245 xm_size = ci->x_size;
246 ym_size = ci->y_size;
248 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
249 // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
250 for (y=0; y<src_height; y++) {
251 offs = y * (src_width * 4);
252 offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
253 for (x=0; x<src_width; x++) {
254 mpoffs = offsm + (((x * xm_size) / src_width) * 4);
255 p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
257 p_pixel = (uint32_t *)&((pict->data[0])[offs]);
259 // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
260 pixel_meck = pixel & 0xff000000;
263 tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - thrR;
264 if (tmp > 255) tmp = 255;
265 if (tmp < 0) tmp = 0;
266 pixel_meck |= (tmp << 16) & 0xff0000;
268 tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - thrG;
269 if (tmp > 255) tmp = 255;
270 if (tmp < 0) tmp = 0;
271 pixel_meck |= (tmp << 8) & 0xff00;
273 tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - thrB;
274 if (tmp > 255) tmp = 255;
275 if (tmp < 0) tmp = 0;
276 pixel_meck |= (tmp << 0) & 0xff;
280 //pixel_meck = pixel & 0xff000000;
281 //pixel_meck |= (pixelm & 0x00ffffff);
283 *p_pixel = pixel_meck;
292 if (pix_fmt != PIX_FMT_RGB32) {
293 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
294 src_width, src_height, PIX_FMT_RGB32,
295 src_width, src_height, pix_fmt,
296 sws_flags, NULL, NULL, NULL);
297 if (ci->fromRGB_convert_ctx == NULL) {
298 av_log(NULL, AV_LOG_ERROR,
299 "Cannot initialize the fromRGB conversion context\n");
302 // img_convert parameters are 2 first destination, then 4 source
303 // sws_scale parameters are context, 4 first source, then 2 destination
304 sws_scale(ci->fromRGB_convert_ctx,
305 picture1.data, picture1.linesize, 0, src_height,
306 picture->data, picture->linesize);
313 /****************************************************************************
314 * For mode 1 (the original one)
315 ****************************************************************************/
316 static void Process1(void *ctx,
318 enum PixelFormat pix_fmt,
323 ContextInfo *ci = (ContextInfo *) ctx;
326 AVPicture *pict = picture;
336 uint32_t *p_pixel = 0;
340 if (pix_fmt != PIX_FMT_RGB32) {
343 size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height);
344 buf = av_malloc(size);
346 avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height);
348 // if we already got a SWS context, let's realloc if is not re-useable
349 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
350 src_width, src_height, pix_fmt,
351 src_width, src_height, PIX_FMT_RGB32,
352 sws_flags, NULL, NULL, NULL);
353 if (ci->toRGB_convert_ctx == NULL) {
354 av_log(NULL, AV_LOG_ERROR,
355 "Cannot initialize the toRGB conversion context\n");
359 // img_convert parameters are 2 first destination, then 4 source
360 // sws_scale parameters are context, 4 first source, then 2 destination
361 sws_scale(ci->toRGB_convert_ctx,
362 picture->data, picture->linesize, 0, src_height,
363 picture1.data, picture1.linesize);
368 /* Insert filter code here */ /* ok */
371 if (0 > get_watermark_picture(ci, 0)) {
374 // These are the three original static variables in the ffmpeg hack.
375 pFrameRGB = ci->pFrameRGB;
376 xm_size = ci->x_size;
377 ym_size = ci->y_size;
379 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
380 // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
381 for (y=0; y<src_height; y++) {
382 offs = y * (src_width * 4);
383 offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
384 for (x=0; x<src_width; x++) {
385 mpoffs = offsm + (((x * xm_size) / src_width) * 4);
386 p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
387 pixelm = *p_pixel; /* watermark pixel */
388 p_pixel = (uint32_t *)&((pict->data[0])[offs]);
391 if (((pixelm >> 16) & 0xff) > ci->thrR ||
392 ((pixelm >> 8) & 0xff) > ci->thrG ||
393 ((pixelm >> 0) & 0xff) > ci->thrB)
403 if (pix_fmt != PIX_FMT_RGB32) {
404 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
405 src_width, src_height, PIX_FMT_RGB32,
406 src_width, src_height, pix_fmt,
407 sws_flags, NULL, NULL, NULL);
408 if (ci->fromRGB_convert_ctx == NULL) {
409 av_log(NULL, AV_LOG_ERROR,
410 "Cannot initialize the fromRGB conversion context\n");
413 // img_convert parameters are 2 first destination, then 4 source
414 // sws_scale parameters are context, 4 first source, then 2 destination
415 sws_scale(ci->fromRGB_convert_ctx,
416 picture1.data, picture1.linesize, 0, src_height,
417 picture->data, picture->linesize);
424 /****************************************************************************
425 * This is the function ffmpeg.c callbacks.
426 ****************************************************************************/
427 void Process(void *ctx,
429 enum PixelFormat pix_fmt,
434 ContextInfo *ci = (ContextInfo *) ctx;
436 Process1(ctx, picture, pix_fmt, src_width, src_height, pts);
438 Process0(ctx, picture, pix_fmt, src_width, src_height, pts);
443 /****************************************************************************
444 * When cleanup == 0, we try to get the next frame. If no next frame, nothing
447 * This code follows the example on
448 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
451 ****************************************************************************/
452 int get_watermark_picture(ContextInfo *ci, int cleanup)
454 if (1 == ci->is_done && 0 == cleanup) return 0;
456 // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..
457 // This block is only executed the first time we enter this function.
458 if (0 == ci->pFrameRGB &&
463 * The last three parameters specify the file format, buffer size and format
464 * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect
465 * the format and use a default buffer size. (Didn't work!)
467 if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {
469 // Martin says this should not be necessary but it failed for me sending in
470 // NULL instead of file_iformat to av_open_input_file()
471 ci->i = strlen(ci->filename);
473 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n");
477 if (ci->filename[ci->i] == '.') {
483 ci->p_ext = &(ci->filename[ci->i]);
484 ci->file_iformat = av_find_input_format (ci->p_ext);
485 if (0 == ci->file_iformat) {
486 av_log(NULL, AV_LOG_INFO, "get_watermark_picture() attempt to use image2 for [%s]\n", ci->p_ext);
487 ci->file_iformat = av_find_input_format ("image2");
489 if (0 == ci->file_iformat) {
490 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);
493 // now continues the Martin template.
495 if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {
496 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
502 * This fills the streams field of the AVFormatContext with valid information.
504 if(av_find_stream_info(ci->pFormatCtx)<0) {
505 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n");
510 * As mentioned in the introduction, we'll handle only video streams, not audio
511 * streams. To make things nice and easy, we simply use the first video stream we
515 for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)
516 if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)
518 ci->videoStream = ci->i;
521 if(ci->videoStream == -1) {
522 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n");
526 ci->st = ci->pFormatCtx->streams[ci->videoStream];
527 ci->x_size = ci->st->codec->width;
528 ci->y_size = ci->st->codec->height;
530 // Get a pointer to the codec context for the video stream
531 ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;
535 * OK, so now we've got a pointer to the so-called codec context for our video
536 * stream, but we still have to find the actual codec and open it.
538 // Find the decoder for the video stream
539 ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);
540 if(ci->pCodec == NULL) {
541 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n");
547 if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {
548 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n");
552 // Hack to correct wrong frame rates that seem to be generated by some
554 if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)
555 ci->pCodecCtx->time_base.num=1000;
558 * Allocate a video frame to store the decoded images in.
560 ci->pFrame = avcodec_alloc_frame();
564 * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
566 // Allocate an AVFrame structure
567 ci->pFrameRGB=avcodec_alloc_frame();
568 if(ci->pFrameRGB==NULL) {
569 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n");
573 // Determine required buffer size and allocate buffer
574 ci->numBytes = avpicture_get_size(PIX_FMT_RGB32, ci->pCodecCtx->width,
575 ci->pCodecCtx->height);
576 ci->buffer = av_malloc(ci->numBytes);
578 // Assign appropriate parts of buffer to image planes in pFrameRGB
579 avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGB32,
580 ci->pCodecCtx->width, ci->pCodecCtx->height);
582 // TODO loop, pingpong etc?
585 // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
586 while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)
588 // Is this a packet from the video stream?
589 if(ci->packet.stream_index == ci->videoStream)
591 // Decode video frame
592 avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,
593 ci->packet.data, ci->packet.size);
595 // Did we get a video frame?
596 if(ci->frameFinished)
598 // Convert the image from its native format to RGB32
599 ci->watermark_convert_ctx =
600 sws_getCachedContext(ci->watermark_convert_ctx,
601 ci->pCodecCtx->width, ci->pCodecCtx->height, ci->pCodecCtx->pix_fmt,
602 ci->pCodecCtx->width, ci->pCodecCtx->height, PIX_FMT_RGB32,
603 sws_flags, NULL, NULL, NULL);
604 if (ci->watermark_convert_ctx == NULL) {
605 av_log(NULL, AV_LOG_ERROR,
606 "Cannot initialize the watermark conversion context\n");
609 // img_convert parameters are 2 first destination, then 4 source
610 // sws_scale parameters are context, 4 first source, then 2 destination
611 sws_scale(ci->watermark_convert_ctx,
612 ci->pFrame->data, ci->pFrame->linesize, 0, ci->pCodecCtx->height,
613 ci->pFrameRGB->data, ci->pFrameRGB->linesize);
615 // Process the video frame (save to disk etc.)
616 //fprintf(stderr,"banan() New frame!\n");
617 //DoSomethingWithTheImage(ci->pFrameRGB);
622 // Free the packet that was allocated by av_read_frame
623 av_free_packet(&ci->packet);
631 // Free the RGB image
632 av_freep(&ci->buffer);
633 av_freep(&ci->pFrameRGB);
636 if (0 != ci->pCodecCtx) {
637 avcodec_close(ci->pCodecCtx);
641 // Close the video file
642 if (0 != ci->pFormatCtx) {
643 av_close_input_file(ci->pFormatCtx);
653 void parse_arg_file(const char *filename)