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"
67 static int sws_flags
= SWS_BICUBIC
;
74 /* get_watermark_picture() variables */
75 AVFormatContext
*pFormatCtx
;
79 AVCodecContext
*pCodecCtx
;
86 AVInputFormat
*file_iformat
;
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
;
103 int get_watermark_picture(ContextInfo
*ci
, int cleanup
);
106 /****************************************************************************
108 ****************************************************************************/
109 void Release(void *ctx
)
112 ci
= (ContextInfo
*) ctx
;
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
);
124 /****************************************************************************
126 ****************************************************************************/
127 int Configure(void **ctxp
, int argc
, char *argv
[])
133 if (0 == (*ctxp
= av_mallocz(sizeof(ContextInfo
)))) return -1;
134 ci
= (ContextInfo
*) *ctxp
;
138 // Struct is mallocz:ed so no need to reset.
143 while ((c
= getopt(argc
, argv
, "f:m:t:")) > 0) {
146 strncpy(ci
->filename
, optarg
, 1999);
147 ci
->filename
[1999] = 0;
150 ci
->mode
= atoi(optarg
);
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");
157 ci
->thrR
= (tmp
>> 16) & 0xff;
158 ci
->thrG
= (tmp
>> 8) & 0xff;
159 ci
->thrB
= (tmp
>> 0) & 0xff;
162 av_log(NULL
, AV_LOG_ERROR
, "Watermark: Unrecognized argument '%s'\n", argv
[optind
]);
168 if (0 == ci
->filename
[0]) {
169 av_log(NULL
, AV_LOG_ERROR
, "Watermark: There is no filename specified.\n");
174 return get_watermark_picture(ci
, 0);
178 /****************************************************************************
179 * For mode 0 (the original one)
180 ****************************************************************************/
181 static void Process0(void *ctx
,
183 enum PixelFormat pix_fmt
,
188 ContextInfo
*ci
= (ContextInfo
*) ctx
;
191 AVPicture
*pict
= picture
;
201 uint32_t *p_pixel
= 0;
210 if (pix_fmt
!= PIX_FMT_RGB32
) {
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");
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
);
238 /* Insert filter code here */ /* ok */
241 if (0 > get_watermark_picture(ci
, 0)) {
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
]);
258 p_pixel
= (uint32_t *)&((pict
->data
[0])[offs
]);
260 // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
261 pixel_meck
= pixel
& 0xff000000;
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;
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;
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;
281 //pixel_meck = pixel & 0xff000000;
282 //pixel_meck |= (pixelm & 0x00ffffff);
284 *p_pixel
= pixel_meck
;
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");
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
);
314 /****************************************************************************
315 * For mode 1 (the original one)
316 ****************************************************************************/
317 static void Process1(void *ctx
,
319 enum PixelFormat pix_fmt
,
324 ContextInfo
*ci
= (ContextInfo
*) ctx
;
327 AVPicture
*pict
= picture
;
337 uint32_t *p_pixel
= 0;
341 if (pix_fmt
!= PIX_FMT_RGB32
) {
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");
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
);
369 /* Insert filter code here */ /* ok */
372 if (0 > get_watermark_picture(ci
, 0)) {
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
]);
392 if (((pixelm
>> 16) & 0xff) > ci
->thrR
||
393 ((pixelm
>> 8) & 0xff) > ci
->thrG
||
394 ((pixelm
>> 0) & 0xff) > ci
->thrB
)
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");
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
);
425 /****************************************************************************
426 * This is the function ffmpeg.c callbacks.
427 ****************************************************************************/
428 void Process(void *ctx
,
430 enum PixelFormat pix_fmt
,
435 ContextInfo
*ci
= (ContextInfo
*) ctx
;
437 Process1(ctx
, picture
, pix_fmt
, src_width
, src_height
, pts
);
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
448 * This code follows the example on
449 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
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
&&
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
);
474 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() No filename to watermark vhook\n");
478 if (ci
->filename
[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
);
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
);
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");
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
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
;
522 if(ci
->videoStream
== -1) {
523 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find any video stream\n");
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");
548 if(avcodec_open(ci
->pCodecCtx
, ci
->pCodec
)<0) {
549 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to open codec\n");
553 // Hack to correct wrong frame rates that seem to be generated by some
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");
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?
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");
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);
623 // Free the packet that was allocated by av_read_frame
624 av_free_packet(&ci
->packet
);
632 // Free the RGB image
633 av_freep(&ci
->buffer
);
634 av_freep(&ci
->pFrameRGB
);
637 if (0 != ci
->pCodecCtx
) {
638 avcodec_close(ci
->pCodecCtx
);
642 // Close the video file
643 if (0 != ci
->pFormatCtx
) {
644 av_close_input_file(ci
->pFormatCtx
);
654 void parse_arg_file(const char *filename
)