5b113a476261ef25b2e487f200bd5df97e11cbb2
3 * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se
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!
11 * The watermarkpicture works like this. (Assuming colorintencities 0..0xff)
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
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.
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, watermark pixel is going to be 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 to 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"
72 /* get_watermark_picture() variables */
73 AVFormatContext
*pFormatCtx
;
77 AVCodecContext
*pCodecCtx
;
84 AVInputFormat
*file_iformat
;
94 int get_watermark_picture(ContextInfo
*ci
, int cleanup
);
97 /****************************************************************************
99 ****************************************************************************/
100 void Release(void *ctx
)
103 ci
= (ContextInfo
*) ctx
;
105 if (ci
) get_watermark_picture(ci
, 1);
111 /****************************************************************************
113 ****************************************************************************/
114 int Configure(void **ctxp
, int argc
, char *argv
[])
120 if (0 == (*ctxp
= av_mallocz(sizeof(ContextInfo
)))) return -1;
121 ci
= (ContextInfo
*) *ctxp
;
125 // Struct is mallocz:ed so no need to reset.
130 while ((c
= getopt(argc
, argv
, "f:m:t:")) > 0) {
133 strncpy(ci
->filename
, optarg
, 1999);
134 ci
->filename
[1999] = 0;
137 ci
->mode
= atoi(optarg
);
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");
144 ci
->thrR
= (tmp
>> 16) & 0xff;
145 ci
->thrG
= (tmp
>> 8) & 0xff;
146 ci
->thrB
= (tmp
>> 0) & 0xff;
149 av_log(NULL
, AV_LOG_ERROR
, "Watermark: Unrecognized argument '%s'\n", argv
[optind
]);
155 if (0 == ci
->filename
[0]) {
156 av_log(NULL
, AV_LOG_ERROR
, "Watermark: There is no filename specified.\n");
161 return get_watermark_picture(ci
, 0);
165 /****************************************************************************
166 * For mode 0 (the original one)
167 ****************************************************************************/
168 static void Process0(void *ctx
,
170 enum PixelFormat pix_fmt
,
175 ContextInfo
*ci
= (ContextInfo
*) ctx
;
178 AVPicture
*pict
= picture
;
188 uint32_t *p_pixel
= 0;
197 if (pix_fmt
!= PIX_FMT_RGBA32
) {
200 size
= avpicture_get_size(PIX_FMT_RGBA32
, src_width
, src_height
);
201 buf
= av_malloc(size
);
203 avpicture_fill(&picture1
, buf
, PIX_FMT_RGBA32
, src_width
, src_height
);
204 if (img_convert(&picture1
, PIX_FMT_RGBA32
,
205 picture
, pix_fmt
, src_width
, src_height
) < 0) {
212 /* Insert filter code here */ /* ok */
215 if (0 > get_watermark_picture(ci
, 0)) {
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
;
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
]);
232 p_pixel
= (uint32_t *)&((pict
->data
[0])[offs
]);
234 // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
235 pixel_meck
= pixel
& 0xff000000;
238 tmp
= (int)((pixel
>> 16) & 0xff) + (int)((pixelm
>> 16) & 0xff) - thrR
;
239 if (tmp
> 255) tmp
= 255;
240 if (tmp
< 0) tmp
= 0;
241 pixel_meck
|= (tmp
<< 16) & 0xff0000;
243 tmp
= (int)((pixel
>> 8) & 0xff) + (int)((pixelm
>> 8) & 0xff) - thrG
;
244 if (tmp
> 255) tmp
= 255;
245 if (tmp
< 0) tmp
= 0;
246 pixel_meck
|= (tmp
<< 8) & 0xff00;
248 tmp
= (int)((pixel
>> 0) & 0xff) + (int)((pixelm
>> 0) & 0xff) - thrB
;
249 if (tmp
> 255) tmp
= 255;
250 if (tmp
< 0) tmp
= 0;
251 pixel_meck
|= (tmp
<< 0) & 0xff;
255 //pixel_meck = pixel & 0xff000000;
256 //pixel_meck |= (pixelm & 0x00ffffff);
258 *p_pixel
= pixel_meck
;
267 if (pix_fmt
!= PIX_FMT_RGBA32
) {
268 if (img_convert(picture
, pix_fmt
,
269 &picture1
, PIX_FMT_RGBA32
, src_width
, src_height
) < 0) {
277 /****************************************************************************
278 * For mode 1 (the original one)
279 ****************************************************************************/
280 static void Process1(void *ctx
,
282 enum PixelFormat pix_fmt
,
287 ContextInfo
*ci
= (ContextInfo
*) ctx
;
290 AVPicture
*pict
= picture
;
300 uint32_t *p_pixel
= 0;
304 if (pix_fmt
!= PIX_FMT_RGBA32
) {
307 size
= avpicture_get_size(PIX_FMT_RGBA32
, src_width
, src_height
);
308 buf
= av_malloc(size
);
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) {
319 /* Insert filter code here */ /* ok */
322 if (0 > get_watermark_picture(ci
, 0)) {
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
;
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
]);
342 if (((pixelm
>> 16) & 0xff) > ci
->thrR
||
343 ((pixelm
>> 8) & 0xff) > ci
->thrG
||
344 ((pixelm
>> 0) & 0xff) > ci
->thrB
)
354 if (pix_fmt
!= PIX_FMT_RGBA32
) {
355 if (img_convert(picture
, pix_fmt
,
356 &picture1
, PIX_FMT_RGBA32
, src_width
, src_height
) < 0) {
364 /****************************************************************************
365 * This is the function ffmpeg.c callbacks.
366 ****************************************************************************/
367 void Process(void *ctx
,
369 enum PixelFormat pix_fmt
,
374 ContextInfo
*ci
= (ContextInfo
*) ctx
;
376 return Process1(ctx
, picture
, pix_fmt
, src_width
, src_height
, pts
);
378 return Process0(ctx
, picture
, pix_fmt
, src_width
, src_height
, pts
);
383 /****************************************************************************
384 * When cleanup == 0, we try to get the next frame. If no next frame, nothing
387 * This code follows the example on
388 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
391 ****************************************************************************/
392 int get_watermark_picture(ContextInfo
*ci
, int cleanup
)
394 if (1 == ci
->is_done
&& 0 == cleanup
) return 0;
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.
398 if (0 == ci
->pFrameRGB
&&
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!)
407 if (av_open_input_file(&ci
->pFormatCtx
, ci
->filename
, NULL
, 0, NULL
) != 0) {
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
);
413 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() No filename to watermark vhook\n");
417 if (ci
->filename
[ci
->i
] == '.') {
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
) {
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");
429 if (0 == ci
->file_iformat
) {
430 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Really failed to find iformat [%s]\n", ci
->p_ext
);
433 // now continues the Martin template.
435 if (av_open_input_file(&ci
->pFormatCtx
, ci
->filename
, ci
->file_iformat
, 0, NULL
)!=0) {
436 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to open input file [%s]\n", ci
->filename
);
442 * This fills the streams field of the AVFormatContext with valid information.
444 if(av_find_stream_info(ci
->pFormatCtx
)<0) {
445 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find stream info\n");
450 * As mentioned in the introduction, we'll handle only video streams, not audio
451 * streams. To make things nice and easy, we simply use the first video stream we
455 for(ci
->i
= 0; ci
->i
< ci
->pFormatCtx
->nb_streams
; ci
->i
++)
456 if(ci
->pFormatCtx
->streams
[ci
->i
]->codec
->codec_type
==CODEC_TYPE_VIDEO
)
458 ci
->videoStream
= ci
->i
;
461 if(ci
->videoStream
== -1) {
462 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find any video stream\n");
466 ci
->st
= ci
->pFormatCtx
->streams
[ci
->videoStream
];
467 ci
->x_size
= ci
->st
->codec
->width
;
468 ci
->y_size
= ci
->st
->codec
->height
;
470 // Get a pointer to the codec context for the video stream
471 ci
->pCodecCtx
= ci
->pFormatCtx
->streams
[ci
->videoStream
]->codec
;
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.
478 // Find the decoder for the video stream
479 ci
->pCodec
= avcodec_find_decoder(ci
->pCodecCtx
->codec_id
);
480 if(ci
->pCodec
== NULL
) {
481 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to find any codec\n");
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
;
491 if(avcodec_open(ci
->pCodecCtx
, ci
->pCodec
)<0) {
492 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to open codec\n");
496 // Hack to correct wrong frame rates that seem to be generated by some
498 if (ci
->pCodecCtx
->time_base
.den
>1000 && ci
->pCodecCtx
->time_base
.num
==1)
499 ci
->pCodecCtx
->time_base
.num
=1000;
502 * Allocate a video frame to store the decoded images in.
504 ci
->pFrame
= avcodec_alloc_frame();
508 * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
510 // Allocate an AVFrame structure
511 ci
->pFrameRGB
=avcodec_alloc_frame();
512 if(ci
->pFrameRGB
==NULL
) {
513 av_log(NULL
, AV_LOG_ERROR
, "get_watermark_picture() Failed to alloc pFrameRGB\n");
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
);
522 // Assign appropriate parts of buffer to image planes in pFrameRGB
523 avpicture_fill((AVPicture
*)ci
->pFrameRGB
, ci
->buffer
, PIX_FMT_RGBA32
,
524 ci
->pCodecCtx
->width
, ci
->pCodecCtx
->height
);
526 // TODO loop, pingpong etc?
529 // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
530 while(av_read_frame(ci
->pFormatCtx
, &ci
->packet
)>=0)
532 // Is this a packet from the video stream?
533 if(ci
->packet
.stream_index
== ci
->videoStream
)
535 // Decode video frame
536 avcodec_decode_video(ci
->pCodecCtx
, ci
->pFrame
, &ci
->frameFinished
,
537 ci
->packet
.data
, ci
->packet
.size
);
539 // Did we get a video frame?
540 if(ci
->frameFinished
)
542 // Convert the image from its native format to RGBA32
543 img_convert((AVPicture
*)ci
->pFrameRGB
, PIX_FMT_RGBA32
,
544 (AVPicture
*)(ci
->pFrame
), ci
->pCodecCtx
->pix_fmt
, ci
->pCodecCtx
->width
,
545 ci
->pCodecCtx
->height
);
547 // Process the video frame (save to disk etc.)
548 //fprintf(stderr,"banan() New frame!\n");
549 //DoSomethingWithTheImage(ci->pFrameRGB);
554 // Free the packet that was allocated by av_read_frame
555 av_free_packet(&ci
->packet
);
563 // Free the RGB image
564 av_freep(&ci
->buffer
);
565 av_freep(&ci
->pFrameRGB
);
568 if (0 != ci
->pCodecCtx
) {
569 avcodec_close(ci
->pCodecCtx
);
573 // Close the video file
574 if (0 != ci
->pFormatCtx
) {
575 av_close_input_file(ci
->pFormatCtx
);
585 void parse_arg_file(const char *filename
)