3 * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se
5 * The watermarkpicture works like this. (Assuming colorintencities 0..0xff)
7 * If mask color is 0x80, no change to original frame.
8 * If mask color is < 0x80 the abs difference is subtracted from frame. If
9 * result < 0, result = 0
10 * If mask color is > 0x80 the abs difference is added to frame. If result
11 * > 0xff, result = 0xff
13 * This way a mask that is visible both in light pictures and in dark can be
14 * made (fex by using a picture generated by gimp and the bump map tool).
16 * An example watermark file is at
17 * http://engene.se/ffmpeg_watermark.gif
20 * ffmpeg -i infile -vhook '/path/watermark.so -f wm.gif' out.mov
22 * Note that the entire vhook argument is encapsulated in ''. This
23 * way, arguments to the vhook won't be mixed up with those to ffmpeg.
25 * This library is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU Lesser General Public
27 * License as published by the Free Software Foundation; either
28 * version 2 of the License, or (at your option) any later version.
30 * This library is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * Lesser General Public License for more details.
35 * You should have received a copy of the GNU Lesser General Public
36 * License along with this library; if not, write to the Free Software
37 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48 #include "framehook.h"
56 /* get_watermark_picture() variables */
57 AVFormatContext *pFormatCtx;
61 AVCodecContext *pCodecCtx;
68 AVInputFormat *file_iformat;
74 int get_watermark_picture(ContextInfo *ci, int cleanup);
77 /****************************************************************************
79 ****************************************************************************/
80 void Release(void *ctx)
83 ci = (ContextInfo *) ctx;
85 if (ci) get_watermark_picture(ci, 1);
92 /****************************************************************************
94 ****************************************************************************/
95 int Configure(void **ctxp, int argc, char *argv[])
100 if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1;
101 ci = (ContextInfo *) *ctxp;
105 // Struct is mallocz:ed so no need to reset.
107 while ((c = getopt(argc, argv, "f:")) > 0) {
110 strncpy(ci->filename, optarg, 1999);
111 ci->filename[1999] = 0;
114 av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]);
120 if (0 == ci->filename[0]) {
121 av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n");
126 return get_watermark_picture(ci, 0);
130 /****************************************************************************
131 * Why is this a void returning functions? I want to be able to go wrong!
132 ****************************************************************************/
133 void Process(void *ctx,
135 enum PixelFormat pix_fmt,
140 ContextInfo *ci = (ContextInfo *) ctx;
143 AVPicture *pict = picture;
154 uint32_t *p_pixel = 0;
163 if (pix_fmt != PIX_FMT_RGBA32) {
166 size = avpicture_get_size(PIX_FMT_RGBA32, src_width, src_height);
167 buf = av_malloc(size);
169 avpicture_fill(&picture1, buf, PIX_FMT_RGBA32, src_width, src_height);
170 if (img_convert(&picture1, PIX_FMT_RGBA32,
171 picture, pix_fmt, src_width, src_height) < 0) {
178 /* Insert filter code here */ /* ok */
181 if (0 > get_watermark_picture(ci, 0)) {
184 // These are the three original static variables in the ffmpeg hack.
185 pFrameRGB = ci->pFrameRGB;
186 xm_size = ci->x_size;
187 ym_size = ci->y_size;
189 // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
190 // According to avcodec.h PIX_FMT_RGBA32 is handled in endian specific manner.
191 for (y=0; y<src_height; y++) {
192 offs = y * (src_width * 4);
193 offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
194 for (x=0; x<src_width; x++) {
195 mpoffs = offsm + (((x * xm_size) / src_width) * 4);
196 p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
198 p_pixel = (uint32_t *)&((pict->data[0])[offs]);
200 // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
201 pixel_meck = pixel & 0xff000000;
204 tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - 0x80;
205 if (tmp > 255) tmp = 255;
206 if (tmp < 0) tmp = 0;
207 pixel_meck |= (tmp << 16) & 0xff0000;
209 tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - 0x80;
210 if (tmp > 255) tmp = 255;
211 if (tmp < 0) tmp = 0;
212 pixel_meck |= (tmp << 8) & 0xff00;
214 tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - 0x80;
215 if (tmp > 255) tmp = 255;
216 if (tmp < 0) tmp = 0;
217 pixel_meck |= (tmp << 0) & 0xff;
221 //pixel_meck = pixel & 0xff000000;
222 //pixel_meck |= (pixelm & 0x00ffffff);
224 *p_pixel = pixel_meck;
233 if (pix_fmt != PIX_FMT_RGBA32) {
234 if (img_convert(picture, pix_fmt,
235 &picture1, PIX_FMT_RGBA32, src_width, src_height) < 0) {
243 /****************************************************************************
244 * When cleanup == 0, we try to get the next frame. If no next frame, nothing
247 * This code follows the example on
248 * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
251 ****************************************************************************/
252 int get_watermark_picture(ContextInfo *ci, int cleanup)
254 if (1 == ci->is_done && 0 == cleanup) return 0;
256 // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..
257 // This block is only executed the first time we enter this function.
258 if (0 == ci->pFrameRGB &&
263 * The last three parameters specify the file format, buffer size and format
264 * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect
265 * the format and use a default buffer size. (Didn't work!)
267 if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {
269 // Martin says this should not be necessary but it failed for me sending in
270 // NULL instead of file_iformat to av_open_input_file()
271 ci->i = strlen(ci->filename);
273 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n");
277 if (ci->filename[ci->i] == '.') {
283 ci->p_ext = &(ci->filename[ci->i]);
284 ci->file_iformat = av_find_input_format (ci->p_ext);
285 if (0 == ci->file_iformat) {
286 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);
289 // now continues the Martin template.
291 if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {
292 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
298 * This fills the streams field of the AVFormatContext with valid information.
300 if(av_find_stream_info(ci->pFormatCtx)<0) {
301 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n");
306 * As mentioned in the introduction, we'll handle only video streams, not audio
307 * streams. To make things nice and easy, we simply use the first video stream we
311 for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)
312 if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)
314 ci->videoStream = ci->i;
317 if(ci->videoStream == -1) {
318 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n");
322 ci->st = ci->pFormatCtx->streams[ci->videoStream];
323 ci->x_size = ci->st->codec->width;
324 ci->y_size = ci->st->codec->height;
326 // Get a pointer to the codec context for the video stream
327 ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;
331 * OK, so now we've got a pointer to the so-called codec context for our video
332 * stream, but we still have to find the actual codec and open it.
334 // Find the decoder for the video stream
335 ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);
336 if(ci->pCodec == NULL) {
337 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n");
341 // Inform the codec that we can handle truncated bitstreams -- i.e.,
342 // bitstreams where frame boundaries can fall in the middle of packets
343 if (ci->pCodec->capabilities & CODEC_CAP_TRUNCATED)
344 ci->pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
347 if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {
348 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n");
352 // Hack to correct wrong frame rates that seem to be generated by some
354 if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)
355 ci->pCodecCtx->time_base.num=1000;
358 * Allocate a video frame to store the decoded images in.
360 ci->pFrame = avcodec_alloc_frame();
364 * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
366 // Allocate an AVFrame structure
367 ci->pFrameRGB=avcodec_alloc_frame();
368 if(ci->pFrameRGB==NULL) {
369 av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n");
373 // Determine required buffer size and allocate buffer
374 ci->numBytes = avpicture_get_size(PIX_FMT_RGBA32, ci->pCodecCtx->width,
375 ci->pCodecCtx->height);
376 ci->buffer = av_malloc(ci->numBytes);
378 // Assign appropriate parts of buffer to image planes in pFrameRGB
379 avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGBA32,
380 ci->pCodecCtx->width, ci->pCodecCtx->height);
382 // TODO loop, pingpong etc?
385 // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
386 while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)
388 // Is this a packet from the video stream?
389 if(ci->packet.stream_index == ci->videoStream)
391 // Decode video frame
392 avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,
393 ci->packet.data, ci->packet.size);
395 // Did we get a video frame?
396 if(ci->frameFinished)
398 // Convert the image from its native format to RGBA32
399 img_convert((AVPicture *)ci->pFrameRGB, PIX_FMT_RGBA32,
400 (AVPicture*)(ci->pFrame), ci->pCodecCtx->pix_fmt, ci->pCodecCtx->width,
401 ci->pCodecCtx->height);
403 // Process the video frame (save to disk etc.)
404 //fprintf(stderr,"banan() New frame!\n");
405 //DoSomethingWithTheImage(ci->pFrameRGB);
410 // Free the packet that was allocated by av_read_frame
411 av_free_packet(&ci->packet);
419 // Free the RGB image
420 if (0 != ci->buffer) {
424 if (0 != ci->pFrameRGB) {
425 av_free(ci->pFrameRGB);
430 if (0 != ci->pCodecCtx) {
431 avcodec_close(ci->pCodecCtx);
435 // Close the video file
436 if (0 != ci->pFormatCtx) {
437 av_close_input_file(ci->pFormatCtx);
447 void parse_arg_file(const char *filename)