You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
19KB

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