Commit | Line | Data |
---|---|---|
21754ce6 | 1 | /* |
115329f1 | 2 | * Watermark Hook |
21754ce6 MN |
3 | * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se |
4 | * | |
ed818e25 ME |
5 | * flags to 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 filename of watermark image. You must specify this! | |
9 | * | |
10 | * MODE 0: | |
21754ce6 MN |
11 | * The watermarkpicture works like this. (Assuming colorintencities 0..0xff) |
12 | * Per color do this: | |
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 | |
18 | * | |
ed818e25 ME |
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. | |
21 | * | |
21754ce6 | 22 | * This way a mask that is visible both in light pictures and in dark can be |
115329f1 | 23 | * made (fex by using a picture generated by gimp and the bump map tool). |
21754ce6 MN |
24 | * |
25 | * An example watermark file is at | |
26 | * http://engene.se/ffmpeg_watermark.gif | |
27 | * | |
ed818e25 ME |
28 | * MODE 1: |
29 | * Per color do this: | |
30 | * If mask color > threshold color, watermark pixel is going to be used. | |
31 | * | |
ded78ac0 | 32 | * Example usage: |
ed818e25 ME |
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 | |
ded78ac0 ME |
35 | * |
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. | |
38 | * | |
21754ce6 MN |
39 | * This library is free software; you can redistribute it and/or |
40 | * modify it under the terms of the GNU Lesser General Public | |
41 | * License as published by the Free Software Foundation; either | |
42 | * version 2 of the License, or (at your option) any later version. | |
43 | * | |
44 | * This library is distributed in the hope that it will be useful, | |
45 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
46 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
47 | * Lesser General Public License for more details. | |
48 | * | |
49 | * You should have received a copy of the GNU Lesser General Public | |
50 | * License along with this library; if not, write to the Free Software | |
5509bffa | 51 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21754ce6 MN |
52 | */ |
53 | ||
ed818e25 | 54 | #include <stdlib.h> |
21754ce6 MN |
55 | //#include <fcntl.h> |
56 | #include <unistd.h> | |
57 | #include <stdarg.h> | |
115329f1 | 58 | |
21754ce6 MN |
59 | #include "common.h" |
60 | #include "avformat.h" | |
61 | ||
62 | #include "framehook.h" | |
a851b8e8 | 63 | #include "cmdutils.h" |
21754ce6 MN |
64 | |
65 | typedef struct { | |
66 | char filename[2000]; | |
67 | int x_size; | |
68 | int y_size; | |
69 | ||
70 | /* get_watermark_picture() variables */ | |
71 | AVFormatContext *pFormatCtx; | |
72 | const char *p_ext; | |
73 | int videoStream; | |
74 | int frameFinished; | |
115329f1 DB |
75 | AVCodecContext *pCodecCtx; |
76 | AVCodec *pCodec; | |
21754ce6 MN |
77 | AVFrame *pFrame; |
78 | AVPacket packet; | |
79 | int numBytes; | |
80 | uint8_t *buffer; | |
81 | int i; | |
82 | AVInputFormat *file_iformat; | |
83 | AVStream *st; | |
115329f1 | 84 | int is_done; |
21754ce6 | 85 | AVFrame *pFrameRGB; |
ed818e25 ME |
86 | int thrR; |
87 | int thrG; | |
88 | int thrB; | |
89 | int mode; | |
21754ce6 MN |
90 | } ContextInfo; |
91 | ||
92 | int get_watermark_picture(ContextInfo *ci, int cleanup); | |
93 | ||
94 | ||
95 | /**************************************************************************** | |
115329f1 | 96 | * |
21754ce6 MN |
97 | ****************************************************************************/ |
98 | void Release(void *ctx) | |
99 | { | |
100 | ContextInfo *ci; | |
101 | ci = (ContextInfo *) ctx; | |
102 | ||
103 | if (ci) get_watermark_picture(ci, 1); | |
115329f1 | 104 | |
21754ce6 MN |
105 | if (ctx) |
106 | av_free(ctx); | |
107 | } | |
108 | ||
109 | ||
110 | /**************************************************************************** | |
115329f1 | 111 | * |
21754ce6 MN |
112 | ****************************************************************************/ |
113 | int Configure(void **ctxp, int argc, char *argv[]) | |
114 | { | |
115 | ContextInfo *ci; | |
116 | int c; | |
ed818e25 | 117 | int tmp = 0; |
21754ce6 MN |
118 | |
119 | if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1; | |
120 | ci = (ContextInfo *) *ctxp; | |
121 | ||
f368375c | 122 | optind = 1; |
115329f1 | 123 | |
21754ce6 | 124 | // Struct is mallocz:ed so no need to reset. |
ed818e25 ME |
125 | ci->thrR = 0x80; |
126 | ci->thrG = 0x80; | |
127 | ci->thrB = 0x80; | |
115329f1 | 128 | |
ed818e25 | 129 | while ((c = getopt(argc, argv, "f:m:t:")) > 0) { |
21754ce6 MN |
130 | switch (c) { |
131 | case 'f': | |
132 | strncpy(ci->filename, optarg, 1999); | |
133 | ci->filename[1999] = 0; | |
134 | break; | |
ed818e25 ME |
135 | case 'm': |
136 | ci->mode = atoi(optarg); | |
137 | break; | |
138 | case 't': | |
139 | if (1 != sscanf(optarg, "%x", &tmp)) { | |
140 | av_log(NULL, AV_LOG_ERROR, "Watermark: argument to -t must be a 6 digit hex number\n"); | |
141 | return -1; | |
142 | } | |
143 | ci->thrR = (tmp >> 16) & 0xff; | |
144 | ci->thrG = (tmp >> 8) & 0xff; | |
145 | ci->thrB = (tmp >> 0) & 0xff; | |
146 | break; | |
21754ce6 | 147 | default: |
f368375c | 148 | av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]); |
21754ce6 MN |
149 | return -1; |
150 | } | |
151 | } | |
115329f1 | 152 | |
21754ce6 | 153 | // |
f368375c ME |
154 | if (0 == ci->filename[0]) { |
155 | av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n"); | |
156 | return -1; | |
157 | } | |
115329f1 | 158 | |
21754ce6 MN |
159 | av_register_all(); |
160 | return get_watermark_picture(ci, 0); | |
21754ce6 MN |
161 | } |
162 | ||
163 | ||
164 | /**************************************************************************** | |
ed818e25 | 165 | * For mode 0 (the original one) |
21754ce6 | 166 | ****************************************************************************/ |
ed818e25 ME |
167 | void Process0(void *ctx, |
168 | AVPicture *picture, | |
169 | enum PixelFormat pix_fmt, | |
170 | int src_width, | |
171 | int src_height, | |
172 | int64_t pts) | |
21754ce6 MN |
173 | { |
174 | ContextInfo *ci = (ContextInfo *) ctx; | |
175 | char *buf = 0; | |
176 | AVPicture picture1; | |
177 | AVPicture *pict = picture; | |
115329f1 | 178 | |
21754ce6 MN |
179 | AVFrame *pFrameRGB; |
180 | int xm_size; | |
181 | int ym_size; | |
182 | ||
21754ce6 MN |
183 | int x; |
184 | int y; | |
185 | int offs, offsm; | |
186 | int mpoffs; | |
187 | uint32_t *p_pixel = 0; | |
188 | uint32_t pixel_meck; | |
189 | uint32_t pixel; | |
190 | uint32_t pixelm; | |
115329f1 | 191 | int tmp; |
ed818e25 ME |
192 | int thrR = ci->thrR; |
193 | int thrG = ci->thrG; | |
194 | int thrB = ci->thrB; | |
21754ce6 MN |
195 | |
196 | if (pix_fmt != PIX_FMT_RGBA32) { | |
197 | int size; | |
198 | ||
199 | size = avpicture_get_size(PIX_FMT_RGBA32, src_width, src_height); | |
200 | buf = av_malloc(size); | |
201 | ||
202 | avpicture_fill(&picture1, buf, PIX_FMT_RGBA32, src_width, src_height); | |
115329f1 | 203 | if (img_convert(&picture1, PIX_FMT_RGBA32, |
21754ce6 MN |
204 | picture, pix_fmt, src_width, src_height) < 0) { |
205 | av_free(buf); | |
206 | return; | |
207 | } | |
208 | pict = &picture1; | |
209 | } | |
210 | ||
211 | /* Insert filter code here */ /* ok */ | |
212 | ||
115329f1 | 213 | // Get me next frame |
21754ce6 MN |
214 | if (0 > get_watermark_picture(ci, 0)) { |
215 | return; | |
115329f1 | 216 | } |
21754ce6 MN |
217 | // These are the three original static variables in the ffmpeg hack. |
218 | pFrameRGB = ci->pFrameRGB; | |
219 | xm_size = ci->x_size; | |
220 | ym_size = ci->y_size; | |
115329f1 | 221 | |
21754ce6 MN |
222 | // I'll do the *4 => <<2 crap later. Most compilers understand that anyway. |
223 | // According to avcodec.h PIX_FMT_RGBA32 is handled in endian specific manner. | |
224 | for (y=0; y<src_height; y++) { | |
225 | offs = y * (src_width * 4); | |
226 | offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs! | |
227 | for (x=0; x<src_width; x++) { | |
228 | mpoffs = offsm + (((x * xm_size) / src_width) * 4); | |
229 | p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]); | |
230 | pixelm = *p_pixel; | |
231 | p_pixel = (uint32_t *)&((pict->data[0])[offs]); | |
232 | pixel = *p_pixel; | |
233 | // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs])); | |
234 | pixel_meck = pixel & 0xff000000; | |
235 | ||
236 | // R | |
ed818e25 | 237 | tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - thrR; |
21754ce6 MN |
238 | if (tmp > 255) tmp = 255; |
239 | if (tmp < 0) tmp = 0; | |
240 | pixel_meck |= (tmp << 16) & 0xff0000; | |
241 | // G | |
ed818e25 | 242 | tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - thrG; |
21754ce6 MN |
243 | if (tmp > 255) tmp = 255; |
244 | if (tmp < 0) tmp = 0; | |
245 | pixel_meck |= (tmp << 8) & 0xff00; | |
246 | // B | |
ed818e25 | 247 | tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - thrB; |
21754ce6 MN |
248 | if (tmp > 255) tmp = 255; |
249 | if (tmp < 0) tmp = 0; | |
250 | pixel_meck |= (tmp << 0) & 0xff; | |
115329f1 DB |
251 | |
252 | ||
21754ce6 MN |
253 | // test: |
254 | //pixel_meck = pixel & 0xff000000; | |
255 | //pixel_meck |= (pixelm & 0x00ffffff); | |
256 | ||
257 | *p_pixel = pixel_meck; | |
258 | ||
115329f1 | 259 | offs += 4; |
21754ce6 | 260 | } // foreach X |
115329f1 DB |
261 | } // foreach Y |
262 | ||
263 | ||
264 | ||
21754ce6 MN |
265 | |
266 | if (pix_fmt != PIX_FMT_RGBA32) { | |
115329f1 | 267 | if (img_convert(picture, pix_fmt, |
21754ce6 MN |
268 | &picture1, PIX_FMT_RGBA32, src_width, src_height) < 0) { |
269 | } | |
270 | } | |
271 | ||
272 | av_free(buf); | |
273 | } | |
274 | ||
275 | ||
276 | /**************************************************************************** | |
ed818e25 ME |
277 | * For mode 1 (the original one) |
278 | ****************************************************************************/ | |
279 | void Process1(void *ctx, | |
280 | AVPicture *picture, | |
281 | enum PixelFormat pix_fmt, | |
282 | int src_width, | |
283 | int src_height, | |
284 | int64_t pts) | |
285 | { | |
286 | ContextInfo *ci = (ContextInfo *) ctx; | |
287 | char *buf = 0; | |
288 | AVPicture picture1; | |
289 | AVPicture *pict = picture; | |
290 | ||
291 | AVFrame *pFrameRGB; | |
292 | int xm_size; | |
293 | int ym_size; | |
294 | ||
295 | int x; | |
296 | int y; | |
297 | int offs, offsm; | |
298 | int mpoffs; | |
299 | uint32_t *p_pixel = 0; | |
300 | uint32_t pixel; | |
301 | uint32_t pixelm; | |
302 | ||
303 | if (pix_fmt != PIX_FMT_RGBA32) { | |
304 | int size; | |
305 | ||
306 | size = avpicture_get_size(PIX_FMT_RGBA32, src_width, src_height); | |
307 | buf = av_malloc(size); | |
308 | ||
309 | avpicture_fill(&picture1, buf, PIX_FMT_RGBA32, src_width, src_height); | |
310 | if (img_convert(&picture1, PIX_FMT_RGBA32, | |
311 | picture, pix_fmt, src_width, src_height) < 0) { | |
312 | av_free(buf); | |
313 | return; | |
314 | } | |
315 | pict = &picture1; | |
316 | } | |
317 | ||
318 | /* Insert filter code here */ /* ok */ | |
319 | ||
320 | // Get me next frame | |
321 | if (0 > get_watermark_picture(ci, 0)) { | |
322 | return; | |
323 | } | |
324 | // These are the three original static variables in the ffmpeg hack. | |
325 | pFrameRGB = ci->pFrameRGB; | |
326 | xm_size = ci->x_size; | |
327 | ym_size = ci->y_size; | |
328 | ||
329 | // I'll do the *4 => <<2 crap later. Most compilers understand that anyway. | |
330 | // According to avcodec.h PIX_FMT_RGBA32 is handled in endian specific manner. | |
331 | for (y=0; y<src_height; y++) { | |
332 | offs = y * (src_width * 4); | |
333 | offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs! | |
334 | for (x=0; x<src_width; x++) { | |
335 | mpoffs = offsm + (((x * xm_size) / src_width) * 4); | |
336 | p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]); | |
337 | pixelm = *p_pixel; /* watermark pixel */ | |
338 | p_pixel = (uint32_t *)&((pict->data[0])[offs]); | |
339 | pixel = *p_pixel; | |
340 | ||
341 | if (((pixelm >> 16) & 0xff) > ci->thrR || | |
342 | ((pixelm >> 8) & 0xff) > ci->thrG || | |
343 | ((pixelm >> 0) & 0xff) > ci->thrB) | |
344 | { | |
345 | *p_pixel = pixelm; | |
346 | } else { | |
347 | *p_pixel = pixel; | |
348 | } | |
349 | offs += 4; | |
350 | } // foreach X | |
351 | } // foreach Y | |
352 | ||
353 | if (pix_fmt != PIX_FMT_RGBA32) { | |
354 | if (img_convert(picture, pix_fmt, | |
355 | &picture1, PIX_FMT_RGBA32, src_width, src_height) < 0) { | |
356 | } | |
357 | } | |
358 | ||
359 | av_free(buf); | |
360 | } | |
361 | ||
362 | ||
363 | /**************************************************************************** | |
364 | * This is the function ffmpeg.c callbacks. | |
365 | ****************************************************************************/ | |
366 | void Process(void *ctx, | |
367 | AVPicture *picture, | |
368 | enum PixelFormat pix_fmt, | |
369 | int src_width, | |
370 | int src_height, | |
371 | int64_t pts) | |
372 | { | |
373 | ContextInfo *ci = (ContextInfo *) ctx; | |
374 | if (1 == ci->mode) { | |
375 | return Process1(ctx, picture, pix_fmt, src_width, src_height, pts); | |
376 | } else { | |
377 | return Process0(ctx, picture, pix_fmt, src_width, src_height, pts); | |
378 | } | |
379 | } | |
380 | ||
381 | ||
382 | /**************************************************************************** | |
21754ce6 MN |
383 | * When cleanup == 0, we try to get the next frame. If no next frame, nothing |
384 | * is done. | |
385 | * | |
115329f1 | 386 | * This code follows the example on |
21754ce6 MN |
387 | * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html |
388 | * | |
389 | * 0 = ok, -1 = error | |
390 | ****************************************************************************/ | |
391 | int get_watermark_picture(ContextInfo *ci, int cleanup) | |
392 | { | |
393 | if (1 == ci->is_done && 0 == cleanup) return 0; | |
394 | ||
395 | // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good.. | |
396 | // This block is only executed the first time we enter this function. | |
115329f1 DB |
397 | if (0 == ci->pFrameRGB && |
398 | 0 == cleanup) | |
21754ce6 | 399 | { |
115329f1 DB |
400 | |
401 | /* | |
21754ce6 MN |
402 | * The last three parameters specify the file format, buffer size and format |
403 | * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect | |
404 | * the format and use a default buffer size. (Didn't work!) | |
405 | */ | |
406 | if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) { | |
407 | ||
408 | // Martin says this should not be necessary but it failed for me sending in | |
409 | // NULL instead of file_iformat to av_open_input_file() | |
410 | ci->i = strlen(ci->filename); | |
411 | if (0 == ci->i) { | |
f368375c | 412 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n"); |
21754ce6 MN |
413 | return -1; |
414 | } | |
415 | while (ci->i > 0) { | |
416 | if (ci->filename[ci->i] == '.') { | |
417 | ci->i++; | |
418 | break; | |
419 | } | |
420 | ci->i--; | |
421 | } | |
422 | ci->p_ext = &(ci->filename[ci->i]); | |
423 | ci->file_iformat = av_find_input_format (ci->p_ext); | |
424 | if (0 == ci->file_iformat) { | |
f368375c | 425 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext); |
21754ce6 MN |
426 | return -1; |
427 | } | |
115329f1 DB |
428 | // now continues the Martin template. |
429 | ||
21754ce6 | 430 | if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) { |
f368375c | 431 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename); |
21754ce6 | 432 | return -1; |
115329f1 | 433 | } |
21754ce6 | 434 | } |
115329f1 DB |
435 | |
436 | /* | |
21754ce6 MN |
437 | * This fills the streams field of the AVFormatContext with valid information. |
438 | */ | |
439 | if(av_find_stream_info(ci->pFormatCtx)<0) { | |
f368375c | 440 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n"); |
21754ce6 MN |
441 | return -1; |
442 | } | |
115329f1 | 443 | |
21754ce6 | 444 | /* |
115329f1 | 445 | * As mentioned in the introduction, we'll handle only video streams, not audio |
21754ce6 MN |
446 | * streams. To make things nice and easy, we simply use the first video stream we |
447 | * find. | |
448 | */ | |
449 | ci->videoStream=-1; | |
450 | for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++) | |
a2cfc4d6 | 451 | if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO) |
21754ce6 MN |
452 | { |
453 | ci->videoStream = ci->i; | |
454 | break; | |
455 | } | |
456 | if(ci->videoStream == -1) { | |
f368375c | 457 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n"); |
21754ce6 MN |
458 | return -1; |
459 | } | |
115329f1 | 460 | |
21754ce6 | 461 | ci->st = ci->pFormatCtx->streams[ci->videoStream]; |
a2cfc4d6 MM |
462 | ci->x_size = ci->st->codec->width; |
463 | ci->y_size = ci->st->codec->height; | |
115329f1 | 464 | |
21754ce6 | 465 | // Get a pointer to the codec context for the video stream |
a2cfc4d6 | 466 | ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec; |
115329f1 DB |
467 | |
468 | ||
21754ce6 MN |
469 | /* |
470 | * OK, so now we've got a pointer to the so-called codec context for our video | |
471 | * stream, but we still have to find the actual codec and open it. | |
115329f1 | 472 | */ |
21754ce6 MN |
473 | // Find the decoder for the video stream |
474 | ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id); | |
475 | if(ci->pCodec == NULL) { | |
f368375c | 476 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n"); |
21754ce6 MN |
477 | return -1; |
478 | } | |
115329f1 | 479 | |
21754ce6 MN |
480 | // Inform the codec that we can handle truncated bitstreams -- i.e., |
481 | // bitstreams where frame boundaries can fall in the middle of packets | |
482 | if (ci->pCodec->capabilities & CODEC_CAP_TRUNCATED) | |
483 | ci->pCodecCtx->flags|=CODEC_FLAG_TRUNCATED; | |
115329f1 | 484 | |
21754ce6 MN |
485 | // Open codec |
486 | if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) { | |
f368375c | 487 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n"); |
21754ce6 MN |
488 | return -1; |
489 | } | |
115329f1 DB |
490 | |
491 | // Hack to correct wrong frame rates that seem to be generated by some | |
21754ce6 | 492 | // codecs |
c0df9d75 | 493 | if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1) |
115329f1 DB |
494 | ci->pCodecCtx->time_base.num=1000; |
495 | ||
21754ce6 MN |
496 | /* |
497 | * Allocate a video frame to store the decoded images in. | |
498 | */ | |
499 | ci->pFrame = avcodec_alloc_frame(); | |
115329f1 DB |
500 | |
501 | ||
21754ce6 MN |
502 | /* |
503 | * The RGB image pFrameRGB (of type AVFrame *) is allocated like this: | |
504 | */ | |
505 | // Allocate an AVFrame structure | |
506 | ci->pFrameRGB=avcodec_alloc_frame(); | |
507 | if(ci->pFrameRGB==NULL) { | |
f368375c | 508 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n"); |
21754ce6 MN |
509 | return -1; |
510 | } | |
115329f1 | 511 | |
21754ce6 MN |
512 | // Determine required buffer size and allocate buffer |
513 | ci->numBytes = avpicture_get_size(PIX_FMT_RGBA32, ci->pCodecCtx->width, | |
514 | ci->pCodecCtx->height); | |
515 | ci->buffer = av_malloc(ci->numBytes); | |
115329f1 | 516 | |
21754ce6 MN |
517 | // Assign appropriate parts of buffer to image planes in pFrameRGB |
518 | avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGBA32, | |
115329f1 | 519 | ci->pCodecCtx->width, ci->pCodecCtx->height); |
21754ce6 MN |
520 | } |
521 | // TODO loop, pingpong etc? | |
115329f1 DB |
522 | if (0 == cleanup) |
523 | { | |
21754ce6 MN |
524 | // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n"); |
525 | while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0) | |
526 | { | |
527 | // Is this a packet from the video stream? | |
528 | if(ci->packet.stream_index == ci->videoStream) | |
529 | { | |
530 | // Decode video frame | |
115329f1 | 531 | avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished, |
21754ce6 | 532 | ci->packet.data, ci->packet.size); |
115329f1 | 533 | |
21754ce6 MN |
534 | // Did we get a video frame? |
535 | if(ci->frameFinished) | |
536 | { | |
537 | // Convert the image from its native format to RGBA32 | |
115329f1 DB |
538 | img_convert((AVPicture *)ci->pFrameRGB, PIX_FMT_RGBA32, |
539 | (AVPicture*)(ci->pFrame), ci->pCodecCtx->pix_fmt, ci->pCodecCtx->width, | |
21754ce6 | 540 | ci->pCodecCtx->height); |
115329f1 | 541 | |
21754ce6 MN |
542 | // Process the video frame (save to disk etc.) |
543 | //fprintf(stderr,"banan() New frame!\n"); | |
544 | //DoSomethingWithTheImage(ci->pFrameRGB); | |
545 | return 0; | |
546 | } | |
547 | } | |
115329f1 | 548 | |
21754ce6 MN |
549 | // Free the packet that was allocated by av_read_frame |
550 | av_free_packet(&ci->packet); | |
551 | } | |
115329f1 | 552 | ci->is_done = 1; |
21754ce6 MN |
553 | return 0; |
554 | } // if 0 != cleanup | |
115329f1 DB |
555 | |
556 | if (0 != cleanup) | |
21754ce6 MN |
557 | { |
558 | // Free the RGB image | |
559 | if (0 != ci->buffer) { | |
560 | av_free(ci->buffer); | |
561 | ci->buffer = 0; | |
562 | } | |
563 | if (0 != ci->pFrameRGB) { | |
564 | av_free(ci->pFrameRGB); | |
565 | ci->pFrameRGB = 0; | |
115329f1 DB |
566 | } |
567 | ||
21754ce6 MN |
568 | // Close the codec |
569 | if (0 != ci->pCodecCtx) { | |
570 | avcodec_close(ci->pCodecCtx); | |
571 | ci->pCodecCtx = 0; | |
572 | } | |
115329f1 | 573 | |
21754ce6 MN |
574 | // Close the video file |
575 | if (0 != ci->pFormatCtx) { | |
115329f1 | 576 | av_close_input_file(ci->pFormatCtx); |
21754ce6 MN |
577 | ci->pFormatCtx = 0; |
578 | } | |
115329f1 | 579 | |
21754ce6 MN |
580 | ci->is_done = 0; |
581 | } | |
582 | return 0; | |
583 | } | |
584 | ||
585 | ||
586 | void parse_arg_file(const char *filename) | |
587 | { | |
588 | } |