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.

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