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
64 #include "framehook.h"
68 static int sws_flags
= SWS_BICUBIC
;
75 /* get_watermark_picture() variables */
76 AVFormatContext
*pFormatCtx
;
80 AVCodecContext
*pCodecCtx
;
87 AVInputFormat
*file_iformat
;
96 // This vhook first converts frame to RGB ...
97 struct SwsContext
*toRGB_convert_ctx
;
98 // ... then converts a watermark and applies it to the RGB frame ...
99 struct SwsContext
*watermark_convert_ctx
;
100 // ... and finally converts back frame from RGB to initial format
101 struct SwsContext
*fromRGB_convert_ctx
;
104 int get_watermark_picture(ContextInfo
*ci
, int cleanup
);
107 /****************************************************************************
109 ****************************************************************************/
110 void Release(void *ctx
)
113 ci
= (ContextInfo
*) ctx
;
116 get_watermark_picture(ci
, 1);
117 sws_freeContext(ci
->toRGB_convert_ctx
);
118 sws_freeContext(ci
->watermark_convert_ctx
);
119 sws_freeContext(ci
->fromRGB_convert_ctx
);
125 /****************************************************************************
127 ****************************************************************************/
128 int Configure(void **ctxp
, int argc
, char *argv
[])
134 if (0 == (*ctxp
= av_mallocz(sizeof(ContextInfo
)))) return -1;
135 ci
= (ContextInfo
*) *ctxp
;
139 // Struct is mallocz:ed so no need to reset.
144 while ((c
= getopt(argc
, argv
, "f:m:t:")) > 0) {
147 strncpy(ci
->filename
, optarg
, 1999);
148 ci
->filename
[1999] = 0;
151 ci
->mode
= atoi(optarg
);
154 if (1 != sscanf(optarg
, "%x", &tmp
)) {
155 av_log(NULL
, AV_LOG_ERROR
, "Watermark: argument to -t must be a 6 digit hex number\n");
158 ci
->thrR
= (tmp
>> 16) & 0xff;
159 ci
->thrG
= (tmp
>> 8) & 0xff;
160 ci
->thrB
= (tmp
>> 0) & 0xff;
163 av_log(NULL
, AV_LOG_ERROR
, "Watermark: Unrecognized argument '%s'\n", argv
[optind
]);
169 if (0 == ci
->filename
[0]) {
170 av_log(NULL
, AV_LOG_ERROR
, "Watermark: There is no filename specified.\n");
175 return get_watermark_picture(ci
, 0);
179 /****************************************************************************
180 * For mode 0 (the original one)
181 ****************************************************************************/
182 static void Process0(void *ctx
,
184 enum PixelFormat pix_fmt
,
189 ContextInfo
*ci
= (ContextInfo
*) ctx
;
192 AVPicture
*pict
= picture
;
202 uint32_t *p_pixel
= 0;
211 if (pix_fmt
!= PIX_FMT_RGB32
) {
214 size
= avpicture_get_size(PIX_FMT_RGB32
, src_width
, src_height
);
215 buf
= av_malloc(size
);
217 avpicture_fill(&picture1
, buf
, PIX_FMT_RGB32
, src_width
, src_height
);
219 // if we already got a SWS context, let's realloc if is not re-useable
220 ci
->toRGB_convert_ctx
= sws_getCachedContext(ci
->toRGB_convert_ctx
,
221 src_width
, src_height
, pix_fmt
,
222 src_width
, src_height
, PIX_FMT_RGB32
,
223 sws_flags
, NULL
, NULL
, NULL
);
224 if (ci
->toRGB_convert_ctx
== NULL
) {
225 av_log(NULL
, AV_LOG_ERROR
,
226 "Cannot initialize the toRGB conversion context\n");
230 // img_convert parameters are 2 first destination, then 4 source
231 // sws_scale parameters are context, 4 first source, then 2 destination
232 sws_scale(ci
->toRGB_convert_ctx
,
233 picture
->data
, picture
->linesize
, 0, src_height
,
234 picture1
.data
, picture1
.linesize
);
239 /* Insert filter code here */ /* ok */
242 if (0 > get_watermark_picture(ci
, 0)) {
245 // These are the three original static variables in the ffmpeg hack.
246 pFrameRGB
= ci
->pFrameRGB
;
247 xm_size
= ci
->x_size
;
248 ym_size
= ci
->y_size
;
250 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
251 // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
252 for (y
=0; y
<src_height
; y
++) {
253 offs
= y
* (src_width
* 4);
254 offsm
= (((y
* ym_size
) / src_height
) * 4) * xm_size
; // offsm first in maskline. byteoffs!
255 for (x
=0; x
<src_width
; x
++) {
256 mpoffs
= offsm
+ (((x
* xm_size
) / src_width
) * 4);
257 p_pixel
= (uint32_t *)&((pFrameRGB
->data
[0])[mpoffs
]);
259 p_pixel
= (uint32_t *)&((pict
->data
[0])[offs
]);
261 // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
262 pixel_meck
= pixel
& 0xff000000;
265 tmp
= (int)((pixel
>> 16) & 0xff) + (int)((pixelm
>> 16) & 0xff) - thrR
;
266 if (tmp
> 255) tmp
= 255;
267 if (tmp
< 0) tmp
= 0;
268 pixel_meck
|= (tmp
<< 16) & 0xff0000;
270 tmp
= (int)((pixel
>> 8) & 0xff) + (int)((pixelm
>> 8) & 0xff) - thrG
;
271 if (tmp
> 255) tmp
= 255;
272 if (tmp
< 0) tmp
= 0;
273 pixel_meck
|= (tmp
<< 8) & 0xff00;
275 tmp
= (int)((pixel
>> 0) & 0xff) + (int)((pixelm
>> 0) & 0xff) - thrB
;
276 if (tmp
> 255) tmp
= 255;
277 if (tmp
< 0) tmp
= 0;
278 pixel_meck
|= (tmp
<< 0) & 0xff;
282 //pixel_meck = pixel & 0xff000000;
283 //pixel_meck |= (pixelm & 0x00ffffff);
285 *p_pixel
= pixel_meck
;
294 if (pix_fmt
!= PIX_FMT_RGB32
) {
295 ci
->fromRGB_convert_ctx
= sws_getCachedContext(ci
->fromRGB_convert_ctx
,
296 src_width
, src_height
, PIX_FMT_RGB32
,
297 src_width
, src_height
, pix_fmt
,
298 sws_flags
, NULL
, NULL
, NULL
);
299 if (ci
->fromRGB_convert_ctx
== NULL
) {
300 av_log(NULL
, AV_LOG_ERROR
,
301 "Cannot initialize the fromRGB conversion context\n");
304 // img_convert parameters are 2 first destination, then 4 source
305 // sws_scale parameters are context, 4 first source, then 2 destination
306 sws_scale(ci
->fromRGB_convert_ctx
,
307 picture1
.data
, picture1
.linesize
, 0, src_height
,
308 picture
->data
, picture
->linesize
);
315 /****************************************************************************
316 * For mode 1 (the original one)
317 ****************************************************************************/
318 static void Process1(void *ctx
,
320 enum PixelFormat pix_fmt
,
325 ContextInfo
*ci
= (ContextInfo
*) ctx
;
328 AVPicture
*pict
= picture
;
338 uint32_t *p_pixel
= 0;
342 if (pix_fmt
!= PIX_FMT_RGB32
) {
345 size
= avpicture_get_size(PIX_FMT_RGB32
, src_width
, src_height
);
346 buf
= av_malloc(size
);
348 avpicture_fill(&picture1
, buf
, PIX_FMT_RGB32
, src_width
, src_height
);
350 // if we already got a SWS context, let's realloc if is not re-useable
351 ci
->toRGB_convert_ctx
= sws_getCachedContext(ci
->toRGB_convert_ctx
,
352 src_width
, src_height
, pix_fmt
,
353 src_width
, src_height
, PIX_FMT_RGB32
,
354 sws_flags
, NULL
, NULL
, NULL
);
355 if (ci
->toRGB_convert_ctx
== NULL
) {
356 av_log(NULL
, AV_LOG_ERROR
,
357 "Cannot initialize the toRGB conversion context\n");
361 // img_convert parameters are 2 first destination, then 4 source
362 // sws_scale parameters are context, 4 first source, then 2 destination
363 sws_scale(ci
->toRGB_convert_ctx
,
364 picture
->data
, picture
->linesize
, 0, src_height
,
365 picture1
.data
, picture1
.linesize
);
370 /* Insert filter code here */ /* ok */
373 if (0 > get_watermark_picture(ci
, 0)) {
376 // These are the three original static variables in the ffmpeg hack.
377 pFrameRGB
= ci
->pFrameRGB
;
378 xm_size
= ci
->x_size
;
379 ym_size
= ci
->y_size
;
381 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
382 // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
383 for (y
=0; y
<src_height
; y
++) {
384 offs
= y
* (src_width
* 4);
385 offsm
= (((y
* ym_size
) / src_height
) * 4) * xm_size
; // offsm first in maskline. byteoffs!
386 for (x
=0; x
<src_width
; x
++) {
387 mpoffs
= offsm
+ (((x
* xm_size
) / src_width
) * 4);
388 p_pixel
= (uint32_t *)&((pFrameRGB
->data
[0])[mpoffs
]);
389 pixelm
= *p_pixel
; /* watermark pixel */
390 p_pixel
= (uint32_t *)&((pict
->data
[0])[offs
]);
393 if (((pixelm
>> 16) & 0xff) > ci
->thrR
||
394 ((pixelm
>> 8) & 0xff) > ci
->thrG
||
395 ((pixelm
>> 0) & 0xff) > ci
->thrB
)
405 if (pix_fmt
!= PIX_FMT_RGB32
) {
406 ci
->fromRGB_convert_ctx
= sws_getCachedContext(ci
->fromRGB_convert_ctx
,
407 src_width
, src_height
, PIX_FMT_RGB32
,
408 src_width
, src_height
, pix_fmt
,
409 sws_flags
, NULL
, NULL
, NULL
);
410 if (ci
->fromRGB_convert_ctx
== NULL
) {
411 av_log(NULL
, AV_LOG_ERROR
,
412 "Cannot initialize the fromRGB conversion context\n");
415 // img_convert parameters are 2 first destination, then 4 source
416 // sws_scale parameters are context, 4 first source, then 2 destination
417 sws_scale(ci
->fromRGB_convert_ctx
,
418 picture1
.data
, picture1
.linesize
, 0, src_height
,
419 picture
->data
, picture
->linesize
);
426 /****************************************************************************
427 * This is the function ffmpeg.c callbacks.
428 ****************************************************************************/
429 void Process(void *ctx
,
431 enum PixelFormat pix_fmt
,
436 ContextInfo
*ci
= (ContextInfo
*) ctx
;
438 Process1(ctx
, picture
, pix_fmt
, src_width
, src_height
, pts
);
440 Process0(ctx
, picture
, pix_fmt
, src_width
, src_height
, pts
);
445 /****************************************************************************
446 * When cleanup == 0, we try to get the next frame. If no next frame, nothing
449 * This code follows the example on
450 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
453 ****************************************************************************/
454 int get_watermark_picture(ContextInfo
*ci
, int cleanup
)
456 if (1 == ci
->is_done
&& 0 == cleanup
) return 0;
458 // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..
459 // This block is only executed the first time we enter this function.
460 if (0 == ci
->pFrameRGB
&&
465 * The last three parameters specify the file format, buffer size and format
466 * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect
467 * the format and use a default buffer size. (Didn't work!)
469 if (av_open_input_file(&ci
->pFormatCtx
, ci
->filename
, NULL
, 0, NULL
) != 0) {
471 // Martin says this should not be necessary but it failed for me sending in
472 // NULL instead of file_iformat to av_open_input_file()
473 ci
->i
= strlen(ci
->filename
);
475 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() No filename to watermark vhook\n");
479 if (ci
->filename
[ci
->i
] == '.') {
485 ci
->p_ext
= &(ci
->filename
[ci
->i
]);
486 ci
->file_iformat
= av_find_input_format (ci
->p_ext
);
487 if (0 == ci
->file_iformat
) {
488 av_log(NULL
, AV_LOG_INFO
, "get_watermark_picture() attempt to use image2 for [%s]\n", ci
->p_ext
);
489 ci
->file_iformat
= av_find_input_format ("image2");
491 if (0 == ci
->file_iformat
) {
492 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Really failed to find iformat [%s]\n", ci
->p_ext
);
495 // now continues the Martin template.
497 if (av_open_input_file(&ci
->pFormatCtx
, ci
->filename
, ci
->file_iformat
, 0, NULL
)!=0) {
498 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to open input file [%s]\n", ci
->filename
);
504 * This fills the streams field of the AVFormatContext with valid information.
506 if(av_find_stream_info(ci
->pFormatCtx
)<0) {
507 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find stream info\n");
512 * As mentioned in the introduction, we'll handle only video streams, not audio
513 * streams. To make things nice and easy, we simply use the first video stream we
517 for(ci
->i
= 0; ci
->i
< ci
->pFormatCtx
->nb_streams
; ci
->i
++)
518 if(ci
->pFormatCtx
->streams
[ci
->i
]->codec
->codec_type
==CODEC_TYPE_VIDEO
)
520 ci
->videoStream
= ci
->i
;
523 if(ci
->videoStream
== -1) {
524 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find any video stream\n");
528 ci
->st
= ci
->pFormatCtx
->streams
[ci
->videoStream
];
529 ci
->x_size
= ci
->st
->codec
->width
;
530 ci
->y_size
= ci
->st
->codec
->height
;
532 // Get a pointer to the codec context for the video stream
533 ci
->pCodecCtx
= ci
->pFormatCtx
->streams
[ci
->videoStream
]->codec
;
537 * OK, so now we've got a pointer to the so-called codec context for our video
538 * stream, but we still have to find the actual codec and open it.
540 // Find the decoder for the video stream
541 ci
->pCodec
= avcodec_find_decoder(ci
->pCodecCtx
->codec_id
);
542 if(ci
->pCodec
== NULL
) {
543 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find any codec\n");
547 // Inform the codec that we can handle truncated bitstreams -- i.e.,
548 // bitstreams where frame boundaries can fall in the middle of packets
549 if (ci
->pCodec
->capabilities
& CODEC_CAP_TRUNCATED
)
550 ci
->pCodecCtx
->flags
|=CODEC_FLAG_TRUNCATED
;
553 if(avcodec_open(ci
->pCodecCtx
, ci
->pCodec
)<0) {
554 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to open codec\n");
558 // Hack to correct wrong frame rates that seem to be generated by some
560 if (ci
->pCodecCtx
->time_base
.den
>1000 && ci
->pCodecCtx
->time_base
.num
==1)
561 ci
->pCodecCtx
->time_base
.num
=1000;
564 * Allocate a video frame to store the decoded images in.
566 ci
->pFrame
= avcodec_alloc_frame();
570 * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
572 // Allocate an AVFrame structure
573 ci
->pFrameRGB
=avcodec_alloc_frame();
574 if(ci
->pFrameRGB
==NULL
) {
575 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to alloc pFrameRGB\n");
579 // Determine required buffer size and allocate buffer
580 ci
->numBytes
= avpicture_get_size(PIX_FMT_RGB32
, ci
->pCodecCtx
->width
,
581 ci
->pCodecCtx
->height
);
582 ci
->buffer
= av_malloc(ci
->numBytes
);
584 // Assign appropriate parts of buffer to image planes in pFrameRGB
585 avpicture_fill((AVPicture
*)ci
->pFrameRGB
, ci
->buffer
, PIX_FMT_RGB32
,
586 ci
->pCodecCtx
->width
, ci
->pCodecCtx
->height
);
588 // TODO loop, pingpong etc?
591 // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
592 while(av_read_frame(ci
->pFormatCtx
, &ci
->packet
)>=0)
594 // Is this a packet from the video stream?
595 if(ci
->packet
.stream_index
== ci
->videoStream
)
597 // Decode video frame
598 avcodec_decode_video(ci
->pCodecCtx
, ci
->pFrame
, &ci
->frameFinished
,
599 ci
->packet
.data
, ci
->packet
.size
);
601 // Did we get a video frame?
602 if(ci
->frameFinished
)
604 // Convert the image from its native format to RGB32
605 ci
->watermark_convert_ctx
=
606 sws_getCachedContext(ci
->watermark_convert_ctx
,
607 ci
->pCodecCtx
->width
, ci
->pCodecCtx
->height
, ci
->pCodecCtx
->pix_fmt
,
608 ci
->pCodecCtx
->width
, ci
->pCodecCtx
->height
, PIX_FMT_RGB32
,
609 sws_flags
, NULL
, NULL
, NULL
);
610 if (ci
->watermark_convert_ctx
== NULL
) {
611 av_log(NULL
, AV_LOG_ERROR
,
612 "Cannot initialize the watermark conversion context\n");
615 // img_convert parameters are 2 first destination, then 4 source
616 // sws_scale parameters are context, 4 first source, then 2 destination
617 sws_scale(ci
->watermark_convert_ctx
,
618 ci
->pFrame
->data
, ci
->pFrame
->linesize
, 0, ci
->pCodecCtx
->height
,
619 ci
->pFrameRGB
->data
, ci
->pFrameRGB
->linesize
);
621 // Process the video frame (save to disk etc.)
622 //fprintf(stderr,"banan() New frame!\n");
623 //DoSomethingWithTheImage(ci->pFrameRGB);
628 // Free the packet that was allocated by av_read_frame
629 av_free_packet(&ci
->packet
);
637 // Free the RGB image
638 av_freep(&ci
->buffer
);
639 av_freep(&ci
->pFrameRGB
);
642 if (0 != ci
->pCodecCtx
) {
643 avcodec_close(ci
->pCodecCtx
);
647 // Close the video file
648 if (0 != ci
->pFormatCtx
) {
649 av_close_input_file(ci
->pFormatCtx
);
659 void parse_arg_file(const char *filename
)