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