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.

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