Commit | Line | Data |
---|---|---|
21754ce6 | 1 | /* |
115329f1 | 2 | * Watermark Hook |
21754ce6 MN |
3 | * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se |
4 | * | |
8dfcf67e | 5 | * parameters for watermark: |
ed818e25 ME |
6 | * -m nbr = nbr is 0..1. 0 is the default mode, see below. |
7 | * -t nbr = nbr is six digit hex. Threshold. | |
8dfcf67e | 8 | * -f file = file is the watermark image filename. You must specify this! |
ed818e25 ME |
9 | * |
10 | * MODE 0: | |
8dfcf67e | 11 | * The watermark picture works like this (assuming color intensities 0..0xff): |
21754ce6 | 12 | * Per color do this: |
8dfcf67e DB |
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 | |
21754ce6 | 15 | * result < 0, result = 0 |
8dfcf67e | 16 | * If mask color is > 0x80 the abs difference is added to the frame. If result |
21754ce6 MN |
17 | * > 0xff, result = 0xff |
18 | * | |
8dfcf67e DB |
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. | |
ed818e25 | 21 | * |
21754ce6 | 22 | * This way a mask that is visible both in light pictures and in dark can be |
8dfcf67e | 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: | |
8dfcf67e | 30 | * If mask color > threshold color then the watermark pixel is used. |
ed818e25 | 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 | |
8dfcf67e | 37 | * way, arguments to the vhook won't be mixed up with those for ffmpeg. |
ded78ac0 | 38 | * |
b78e7197 DB |
39 | * This file is part of FFmpeg. |
40 | * | |
41 | * FFmpeg is free software; you can redistribute it and/or | |
21754ce6 MN |
42 | * modify it under the terms of the GNU Lesser General Public |
43 | * License as published by the Free Software Foundation; either | |
b78e7197 | 44 | * version 2.1 of the License, or (at your option) any later version. |
21754ce6 | 45 | * |
b78e7197 | 46 | * FFmpeg is distributed in the hope that it will be useful, |
21754ce6 MN |
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. | |
50 | * | |
51 | * You should have received a copy of the GNU Lesser General Public | |
b78e7197 | 52 | * License along with FFmpeg; if not, write to the Free Software |
5509bffa | 53 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21754ce6 MN |
54 | */ |
55 | ||
ed818e25 | 56 | #include <stdlib.h> |
21754ce6 MN |
57 | //#include <fcntl.h> |
58 | #include <unistd.h> | |
59 | #include <stdarg.h> | |
115329f1 | 60 | |
21754ce6 MN |
61 | #include "common.h" |
62 | #include "avformat.h" | |
63 | ||
64 | #include "framehook.h" | |
a851b8e8 | 65 | #include "cmdutils.h" |
2d7490fc VP |
66 | #include "swscale.h" |
67 | ||
68 | static int sws_flags = SWS_BICUBIC; | |
21754ce6 MN |
69 | |
70 | typedef struct { | |
71 | char filename[2000]; | |
72 | int x_size; | |
73 | int y_size; | |
74 | ||
75 | /* get_watermark_picture() variables */ | |
76 | AVFormatContext *pFormatCtx; | |
77 | const char *p_ext; | |
78 | int videoStream; | |
79 | int frameFinished; | |
115329f1 DB |
80 | AVCodecContext *pCodecCtx; |
81 | AVCodec *pCodec; | |
21754ce6 MN |
82 | AVFrame *pFrame; |
83 | AVPacket packet; | |
84 | int numBytes; | |
85 | uint8_t *buffer; | |
86 | int i; | |
87 | AVInputFormat *file_iformat; | |
88 | AVStream *st; | |
115329f1 | 89 | int is_done; |
21754ce6 | 90 | AVFrame *pFrameRGB; |
ed818e25 ME |
91 | int thrR; |
92 | int thrG; | |
93 | int thrB; | |
94 | int mode; | |
2d7490fc VP |
95 | |
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; | |
21754ce6 MN |
102 | } ContextInfo; |
103 | ||
104 | int get_watermark_picture(ContextInfo *ci, int cleanup); | |
105 | ||
106 | ||
107 | /**************************************************************************** | |
115329f1 | 108 | * |
21754ce6 MN |
109 | ****************************************************************************/ |
110 | void Release(void *ctx) | |
111 | { | |
112 | ContextInfo *ci; | |
113 | ci = (ContextInfo *) ctx; | |
114 | ||
2d7490fc VP |
115 | if (ci) { |
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); | |
120 | } | |
b4902c11 | 121 | av_free(ctx); |
21754ce6 MN |
122 | } |
123 | ||
124 | ||
125 | /**************************************************************************** | |
115329f1 | 126 | * |
21754ce6 MN |
127 | ****************************************************************************/ |
128 | int Configure(void **ctxp, int argc, char *argv[]) | |
129 | { | |
130 | ContextInfo *ci; | |
131 | int c; | |
ed818e25 | 132 | int tmp = 0; |
21754ce6 MN |
133 | |
134 | if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1; | |
135 | ci = (ContextInfo *) *ctxp; | |
136 | ||
f368375c | 137 | optind = 1; |
115329f1 | 138 | |
21754ce6 | 139 | // Struct is mallocz:ed so no need to reset. |
ed818e25 ME |
140 | ci->thrR = 0x80; |
141 | ci->thrG = 0x80; | |
142 | ci->thrB = 0x80; | |
115329f1 | 143 | |
ed818e25 | 144 | while ((c = getopt(argc, argv, "f:m:t:")) > 0) { |
21754ce6 MN |
145 | switch (c) { |
146 | case 'f': | |
147 | strncpy(ci->filename, optarg, 1999); | |
148 | ci->filename[1999] = 0; | |
149 | break; | |
ed818e25 ME |
150 | case 'm': |
151 | ci->mode = atoi(optarg); | |
152 | break; | |
153 | case 't': | |
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"); | |
156 | return -1; | |
157 | } | |
158 | ci->thrR = (tmp >> 16) & 0xff; | |
159 | ci->thrG = (tmp >> 8) & 0xff; | |
160 | ci->thrB = (tmp >> 0) & 0xff; | |
161 | break; | |
21754ce6 | 162 | default: |
f368375c | 163 | av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]); |
21754ce6 MN |
164 | return -1; |
165 | } | |
166 | } | |
115329f1 | 167 | |
21754ce6 | 168 | // |
f368375c ME |
169 | if (0 == ci->filename[0]) { |
170 | av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n"); | |
171 | return -1; | |
172 | } | |
115329f1 | 173 | |
21754ce6 MN |
174 | av_register_all(); |
175 | return get_watermark_picture(ci, 0); | |
21754ce6 MN |
176 | } |
177 | ||
178 | ||
179 | /**************************************************************************** | |
ed818e25 | 180 | * For mode 0 (the original one) |
21754ce6 | 181 | ****************************************************************************/ |
7b49ce2e | 182 | static void Process0(void *ctx, |
ed818e25 ME |
183 | AVPicture *picture, |
184 | enum PixelFormat pix_fmt, | |
185 | int src_width, | |
186 | int src_height, | |
187 | int64_t pts) | |
21754ce6 MN |
188 | { |
189 | ContextInfo *ci = (ContextInfo *) ctx; | |
190 | char *buf = 0; | |
191 | AVPicture picture1; | |
192 | AVPicture *pict = picture; | |
115329f1 | 193 | |
21754ce6 MN |
194 | AVFrame *pFrameRGB; |
195 | int xm_size; | |
196 | int ym_size; | |
197 | ||
21754ce6 MN |
198 | int x; |
199 | int y; | |
200 | int offs, offsm; | |
201 | int mpoffs; | |
202 | uint32_t *p_pixel = 0; | |
203 | uint32_t pixel_meck; | |
204 | uint32_t pixel; | |
205 | uint32_t pixelm; | |
115329f1 | 206 | int tmp; |
ed818e25 ME |
207 | int thrR = ci->thrR; |
208 | int thrG = ci->thrG; | |
209 | int thrB = ci->thrB; | |
21754ce6 | 210 | |
71e445fc | 211 | if (pix_fmt != PIX_FMT_RGB32) { |
21754ce6 MN |
212 | int size; |
213 | ||
71e445fc | 214 | size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height); |
21754ce6 MN |
215 | buf = av_malloc(size); |
216 | ||
71e445fc | 217 | avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height); |
2d7490fc VP |
218 | |
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, | |
71e445fc | 222 | src_width, src_height, PIX_FMT_RGB32, |
2d7490fc VP |
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"); | |
ca345e44 | 227 | return; |
21754ce6 | 228 | } |
2d7490fc VP |
229 | |
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); | |
235 | ||
21754ce6 MN |
236 | pict = &picture1; |
237 | } | |
238 | ||
239 | /* Insert filter code here */ /* ok */ | |
240 | ||
115329f1 | 241 | // Get me next frame |
21754ce6 MN |
242 | if (0 > get_watermark_picture(ci, 0)) { |
243 | return; | |
115329f1 | 244 | } |
21754ce6 MN |
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; | |
115329f1 | 249 | |
21754ce6 | 250 | // I'll do the *4 => <<2 crap later. Most compilers understand that anyway. |
71e445fc | 251 | // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner. |
21754ce6 MN |
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]); | |
258 | pixelm = *p_pixel; | |
259 | p_pixel = (uint32_t *)&((pict->data[0])[offs]); | |
260 | pixel = *p_pixel; | |
261 | // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs])); | |
262 | pixel_meck = pixel & 0xff000000; | |
263 | ||
264 | // R | |
ed818e25 | 265 | tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - thrR; |
21754ce6 MN |
266 | if (tmp > 255) tmp = 255; |
267 | if (tmp < 0) tmp = 0; | |
268 | pixel_meck |= (tmp << 16) & 0xff0000; | |
269 | // G | |
ed818e25 | 270 | tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - thrG; |
21754ce6 MN |
271 | if (tmp > 255) tmp = 255; |
272 | if (tmp < 0) tmp = 0; | |
273 | pixel_meck |= (tmp << 8) & 0xff00; | |
274 | // B | |
ed818e25 | 275 | tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - thrB; |
21754ce6 MN |
276 | if (tmp > 255) tmp = 255; |
277 | if (tmp < 0) tmp = 0; | |
278 | pixel_meck |= (tmp << 0) & 0xff; | |
115329f1 DB |
279 | |
280 | ||
21754ce6 MN |
281 | // test: |
282 | //pixel_meck = pixel & 0xff000000; | |
283 | //pixel_meck |= (pixelm & 0x00ffffff); | |
284 | ||
285 | *p_pixel = pixel_meck; | |
286 | ||
115329f1 | 287 | offs += 4; |
21754ce6 | 288 | } // foreach X |
115329f1 DB |
289 | } // foreach Y |
290 | ||
291 | ||
292 | ||
21754ce6 | 293 | |
71e445fc | 294 | if (pix_fmt != PIX_FMT_RGB32) { |
2d7490fc | 295 | ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx, |
71e445fc | 296 | src_width, src_height, PIX_FMT_RGB32, |
2d7490fc VP |
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"); | |
ca345e44 | 302 | return; |
21754ce6 | 303 | } |
2d7490fc VP |
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); | |
21754ce6 MN |
309 | } |
310 | ||
311 | av_free(buf); | |
312 | } | |
313 | ||
314 | ||
315 | /**************************************************************************** | |
ed818e25 ME |
316 | * For mode 1 (the original one) |
317 | ****************************************************************************/ | |
7b49ce2e | 318 | static void Process1(void *ctx, |
ed818e25 ME |
319 | AVPicture *picture, |
320 | enum PixelFormat pix_fmt, | |
321 | int src_width, | |
322 | int src_height, | |
323 | int64_t pts) | |
324 | { | |
325 | ContextInfo *ci = (ContextInfo *) ctx; | |
326 | char *buf = 0; | |
327 | AVPicture picture1; | |
328 | AVPicture *pict = picture; | |
329 | ||
330 | AVFrame *pFrameRGB; | |
331 | int xm_size; | |
332 | int ym_size; | |
333 | ||
334 | int x; | |
335 | int y; | |
336 | int offs, offsm; | |
337 | int mpoffs; | |
338 | uint32_t *p_pixel = 0; | |
339 | uint32_t pixel; | |
340 | uint32_t pixelm; | |
341 | ||
71e445fc | 342 | if (pix_fmt != PIX_FMT_RGB32) { |
ed818e25 ME |
343 | int size; |
344 | ||
71e445fc | 345 | size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height); |
ed818e25 ME |
346 | buf = av_malloc(size); |
347 | ||
71e445fc | 348 | avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height); |
2d7490fc VP |
349 | |
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, | |
71e445fc | 353 | src_width, src_height, PIX_FMT_RGB32, |
2d7490fc VP |
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"); | |
ca345e44 | 358 | return; |
ed818e25 | 359 | } |
2d7490fc VP |
360 | |
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); | |
366 | ||
ed818e25 ME |
367 | pict = &picture1; |
368 | } | |
369 | ||
370 | /* Insert filter code here */ /* ok */ | |
371 | ||
372 | // Get me next frame | |
373 | if (0 > get_watermark_picture(ci, 0)) { | |
374 | return; | |
375 | } | |
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; | |
380 | ||
381 | // I'll do the *4 => <<2 crap later. Most compilers understand that anyway. | |
71e445fc | 382 | // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner. |
ed818e25 ME |
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]); | |
391 | pixel = *p_pixel; | |
392 | ||
393 | if (((pixelm >> 16) & 0xff) > ci->thrR || | |
394 | ((pixelm >> 8) & 0xff) > ci->thrG || | |
395 | ((pixelm >> 0) & 0xff) > ci->thrB) | |
396 | { | |
397 | *p_pixel = pixelm; | |
398 | } else { | |
399 | *p_pixel = pixel; | |
400 | } | |
401 | offs += 4; | |
402 | } // foreach X | |
403 | } // foreach Y | |
404 | ||
71e445fc | 405 | if (pix_fmt != PIX_FMT_RGB32) { |
2d7490fc | 406 | ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx, |
71e445fc | 407 | src_width, src_height, PIX_FMT_RGB32, |
2d7490fc VP |
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"); | |
ca345e44 | 413 | return; |
ed818e25 | 414 | } |
2d7490fc VP |
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); | |
ed818e25 ME |
420 | } |
421 | ||
422 | av_free(buf); | |
423 | } | |
424 | ||
425 | ||
426 | /**************************************************************************** | |
427 | * This is the function ffmpeg.c callbacks. | |
428 | ****************************************************************************/ | |
429 | void Process(void *ctx, | |
430 | AVPicture *picture, | |
431 | enum PixelFormat pix_fmt, | |
432 | int src_width, | |
433 | int src_height, | |
434 | int64_t pts) | |
435 | { | |
436 | ContextInfo *ci = (ContextInfo *) ctx; | |
437 | if (1 == ci->mode) { | |
3a136a06 | 438 | Process1(ctx, picture, pix_fmt, src_width, src_height, pts); |
ed818e25 | 439 | } else { |
3a136a06 | 440 | Process0(ctx, picture, pix_fmt, src_width, src_height, pts); |
ed818e25 ME |
441 | } |
442 | } | |
443 | ||
444 | ||
445 | /**************************************************************************** | |
21754ce6 MN |
446 | * When cleanup == 0, we try to get the next frame. If no next frame, nothing |
447 | * is done. | |
448 | * | |
115329f1 | 449 | * This code follows the example on |
21754ce6 MN |
450 | * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html |
451 | * | |
452 | * 0 = ok, -1 = error | |
453 | ****************************************************************************/ | |
454 | int get_watermark_picture(ContextInfo *ci, int cleanup) | |
455 | { | |
456 | if (1 == ci->is_done && 0 == cleanup) return 0; | |
457 | ||
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. | |
115329f1 DB |
460 | if (0 == ci->pFrameRGB && |
461 | 0 == cleanup) | |
21754ce6 | 462 | { |
115329f1 DB |
463 | |
464 | /* | |
21754ce6 MN |
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!) | |
468 | */ | |
469 | if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) { | |
470 | ||
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); | |
474 | if (0 == ci->i) { | |
f368375c | 475 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n"); |
21754ce6 MN |
476 | return -1; |
477 | } | |
478 | while (ci->i > 0) { | |
479 | if (ci->filename[ci->i] == '.') { | |
480 | ci->i++; | |
481 | break; | |
482 | } | |
483 | ci->i--; | |
484 | } | |
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) { | |
dd933153 VP |
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"); | |
490 | } | |
491 | if (0 == ci->file_iformat) { | |
f368375c | 492 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext); |
21754ce6 MN |
493 | return -1; |
494 | } | |
115329f1 DB |
495 | // now continues the Martin template. |
496 | ||
21754ce6 | 497 | if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) { |
f368375c | 498 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename); |
21754ce6 | 499 | return -1; |
115329f1 | 500 | } |
21754ce6 | 501 | } |
115329f1 DB |
502 | |
503 | /* | |
21754ce6 MN |
504 | * This fills the streams field of the AVFormatContext with valid information. |
505 | */ | |
506 | if(av_find_stream_info(ci->pFormatCtx)<0) { | |
f368375c | 507 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n"); |
21754ce6 MN |
508 | return -1; |
509 | } | |
115329f1 | 510 | |
21754ce6 | 511 | /* |
115329f1 | 512 | * As mentioned in the introduction, we'll handle only video streams, not audio |
21754ce6 MN |
513 | * streams. To make things nice and easy, we simply use the first video stream we |
514 | * find. | |
515 | */ | |
516 | ci->videoStream=-1; | |
517 | for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++) | |
a2cfc4d6 | 518 | if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO) |
21754ce6 MN |
519 | { |
520 | ci->videoStream = ci->i; | |
521 | break; | |
522 | } | |
523 | if(ci->videoStream == -1) { | |
f368375c | 524 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n"); |
21754ce6 MN |
525 | return -1; |
526 | } | |
115329f1 | 527 | |
21754ce6 | 528 | ci->st = ci->pFormatCtx->streams[ci->videoStream]; |
a2cfc4d6 MM |
529 | ci->x_size = ci->st->codec->width; |
530 | ci->y_size = ci->st->codec->height; | |
115329f1 | 531 | |
21754ce6 | 532 | // Get a pointer to the codec context for the video stream |
a2cfc4d6 | 533 | ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec; |
115329f1 DB |
534 | |
535 | ||
21754ce6 MN |
536 | /* |
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. | |
115329f1 | 539 | */ |
21754ce6 MN |
540 | // Find the decoder for the video stream |
541 | ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id); | |
542 | if(ci->pCodec == NULL) { | |
f368375c | 543 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n"); |
21754ce6 MN |
544 | return -1; |
545 | } | |
115329f1 | 546 | |
21754ce6 MN |
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; | |
115329f1 | 551 | |
21754ce6 MN |
552 | // Open codec |
553 | if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) { | |
f368375c | 554 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n"); |
21754ce6 MN |
555 | return -1; |
556 | } | |
115329f1 DB |
557 | |
558 | // Hack to correct wrong frame rates that seem to be generated by some | |
21754ce6 | 559 | // codecs |
c0df9d75 | 560 | if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1) |
115329f1 DB |
561 | ci->pCodecCtx->time_base.num=1000; |
562 | ||
21754ce6 MN |
563 | /* |
564 | * Allocate a video frame to store the decoded images in. | |
565 | */ | |
566 | ci->pFrame = avcodec_alloc_frame(); | |
115329f1 DB |
567 | |
568 | ||
21754ce6 MN |
569 | /* |
570 | * The RGB image pFrameRGB (of type AVFrame *) is allocated like this: | |
571 | */ | |
572 | // Allocate an AVFrame structure | |
573 | ci->pFrameRGB=avcodec_alloc_frame(); | |
574 | if(ci->pFrameRGB==NULL) { | |
f368375c | 575 | av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n"); |
21754ce6 MN |
576 | return -1; |
577 | } | |
115329f1 | 578 | |
21754ce6 | 579 | // Determine required buffer size and allocate buffer |
71e445fc | 580 | ci->numBytes = avpicture_get_size(PIX_FMT_RGB32, ci->pCodecCtx->width, |
21754ce6 MN |
581 | ci->pCodecCtx->height); |
582 | ci->buffer = av_malloc(ci->numBytes); | |
115329f1 | 583 | |
21754ce6 | 584 | // Assign appropriate parts of buffer to image planes in pFrameRGB |
71e445fc | 585 | avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGB32, |
115329f1 | 586 | ci->pCodecCtx->width, ci->pCodecCtx->height); |
21754ce6 MN |
587 | } |
588 | // TODO loop, pingpong etc? | |
115329f1 DB |
589 | if (0 == cleanup) |
590 | { | |
21754ce6 MN |
591 | // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n"); |
592 | while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0) | |
593 | { | |
594 | // Is this a packet from the video stream? | |
595 | if(ci->packet.stream_index == ci->videoStream) | |
596 | { | |
597 | // Decode video frame | |
115329f1 | 598 | avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished, |
21754ce6 | 599 | ci->packet.data, ci->packet.size); |
115329f1 | 600 | |
21754ce6 MN |
601 | // Did we get a video frame? |
602 | if(ci->frameFinished) | |
603 | { | |
71e445fc | 604 | // Convert the image from its native format to RGB32 |
2d7490fc VP |
605 | ci->watermark_convert_ctx = |
606 | sws_getCachedContext(ci->watermark_convert_ctx, | |
607 | ci->pCodecCtx->width, ci->pCodecCtx->height, ci->pCodecCtx->pix_fmt, | |
71e445fc | 608 | ci->pCodecCtx->width, ci->pCodecCtx->height, PIX_FMT_RGB32, |
2d7490fc VP |
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"); | |
ca345e44 | 613 | return -1; |
2d7490fc VP |
614 | } |
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); | |
115329f1 | 620 | |
21754ce6 MN |
621 | // Process the video frame (save to disk etc.) |
622 | //fprintf(stderr,"banan() New frame!\n"); | |
623 | //DoSomethingWithTheImage(ci->pFrameRGB); | |
624 | return 0; | |
625 | } | |
626 | } | |
115329f1 | 627 | |
21754ce6 MN |
628 | // Free the packet that was allocated by av_read_frame |
629 | av_free_packet(&ci->packet); | |
630 | } | |
115329f1 | 631 | ci->is_done = 1; |
21754ce6 MN |
632 | return 0; |
633 | } // if 0 != cleanup | |
115329f1 DB |
634 | |
635 | if (0 != cleanup) | |
21754ce6 MN |
636 | { |
637 | // Free the RGB image | |
b4902c11 MN |
638 | av_freep(&ci->buffer); |
639 | av_freep(&ci->pFrameRGB); | |
115329f1 | 640 | |
21754ce6 MN |
641 | // Close the codec |
642 | if (0 != ci->pCodecCtx) { | |
643 | avcodec_close(ci->pCodecCtx); | |
644 | ci->pCodecCtx = 0; | |
645 | } | |
115329f1 | 646 | |
21754ce6 MN |
647 | // Close the video file |
648 | if (0 != ci->pFormatCtx) { | |
115329f1 | 649 | av_close_input_file(ci->pFormatCtx); |
21754ce6 MN |
650 | ci->pFormatCtx = 0; |
651 | } | |
115329f1 | 652 | |
21754ce6 MN |
653 | ci->is_done = 0; |
654 | } | |
655 | return 0; | |
656 | } | |
657 | ||
658 | ||
659 | void parse_arg_file(const char *filename) | |
660 | { | |
661 | } |