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.

450 lines
14KB

  1. /*
  2. * Watermark Hook
  3. * Copyright (c) 2005 Marcus Engene myfirstname(at)mylastname.se
  4. *
  5. * The watermarkpicture works like this. (Assuming colorintencities 0..0xff)
  6. * Per color do this:
  7. * If mask color is 0x80, no change to original frame.
  8. * If mask color is < 0x80 the abs difference is subtracted from frame. If
  9. * result < 0, result = 0
  10. * If mask color is > 0x80 the abs difference is added to frame. If result
  11. * > 0xff, result = 0xff
  12. *
  13. * This way a mask that is visible both in light pictures and in dark can be
  14. * made (fex by using a picture generated by gimp and the bump map tool).
  15. *
  16. * An example watermark file is at
  17. * http://engene.se/ffmpeg_watermark.gif
  18. *
  19. * Example usage:
  20. * ffmpeg -i infile -vhook '/path/watermark.so -f wm.gif' out.mov
  21. *
  22. * Note that the entire vhook argument is encapsulated in ''. This
  23. * way, arguments to the vhook won't be mixed up with those to ffmpeg.
  24. *
  25. * This library is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU Lesser General Public
  27. * License as published by the Free Software Foundation; either
  28. * version 2 of the License, or (at your option) any later version.
  29. *
  30. * This library is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. * Lesser General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Lesser General Public
  36. * License along with this library; if not, write to the Free Software
  37. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  38. */
  39. //#include <stdlib.h>
  40. //#include <fcntl.h>
  41. #include <unistd.h>
  42. #include <stdarg.h>
  43. #include "common.h"
  44. #include "avformat.h"
  45. #include "framehook.h"
  46. #include "cmdutils.h"
  47. typedef struct {
  48. char filename[2000];
  49. int x_size;
  50. int y_size;
  51. /* get_watermark_picture() variables */
  52. AVFormatContext *pFormatCtx;
  53. const char *p_ext;
  54. int videoStream;
  55. int frameFinished;
  56. AVCodecContext *pCodecCtx;
  57. AVCodec *pCodec;
  58. AVFrame *pFrame;
  59. AVPacket packet;
  60. int numBytes;
  61. uint8_t *buffer;
  62. int i;
  63. AVInputFormat *file_iformat;
  64. AVStream *st;
  65. int is_done;
  66. AVFrame *pFrameRGB;
  67. } ContextInfo;
  68. int get_watermark_picture(ContextInfo *ci, int cleanup);
  69. /****************************************************************************
  70. *
  71. ****************************************************************************/
  72. void Release(void *ctx)
  73. {
  74. ContextInfo *ci;
  75. ci = (ContextInfo *) ctx;
  76. if (ci) get_watermark_picture(ci, 1);
  77. if (ctx)
  78. av_free(ctx);
  79. }
  80. /****************************************************************************
  81. *
  82. ****************************************************************************/
  83. int Configure(void **ctxp, int argc, char *argv[])
  84. {
  85. ContextInfo *ci;
  86. int c;
  87. if (0 == (*ctxp = av_mallocz(sizeof(ContextInfo)))) return -1;
  88. ci = (ContextInfo *) *ctxp;
  89. optind = 1;
  90. // Struct is mallocz:ed so no need to reset.
  91. while ((c = getopt(argc, argv, "f:")) > 0) {
  92. switch (c) {
  93. case 'f':
  94. strncpy(ci->filename, optarg, 1999);
  95. ci->filename[1999] = 0;
  96. break;
  97. default:
  98. av_log(NULL, AV_LOG_ERROR, "Watermark: Unrecognized argument '%s'\n", argv[optind]);
  99. return -1;
  100. }
  101. }
  102. //
  103. if (0 == ci->filename[0]) {
  104. av_log(NULL, AV_LOG_ERROR, "Watermark: There is no filename specified.\n");
  105. return -1;
  106. }
  107. av_register_all();
  108. return get_watermark_picture(ci, 0);
  109. }
  110. /****************************************************************************
  111. * Why is this a void returning functions? I want to be able to go wrong!
  112. ****************************************************************************/
  113. void Process(void *ctx,
  114. AVPicture *picture,
  115. enum PixelFormat pix_fmt,
  116. int src_width,
  117. int src_height,
  118. int64_t pts)
  119. {
  120. ContextInfo *ci = (ContextInfo *) ctx;
  121. char *buf = 0;
  122. AVPicture picture1;
  123. AVPicture *pict = picture;
  124. AVFrame *pFrameRGB;
  125. int xm_size;
  126. int ym_size;
  127. // int retval = -1;
  128. int x;
  129. int y;
  130. int offs, offsm;
  131. int mpoffs;
  132. uint32_t *p_pixel = 0;
  133. uint32_t pixel_meck;
  134. uint32_t pixel;
  135. uint32_t pixelm;
  136. int tmp;
  137. //?? (void) ci;
  138. if (pix_fmt != PIX_FMT_RGBA32) {
  139. int size;
  140. size = avpicture_get_size(PIX_FMT_RGBA32, src_width, src_height);
  141. buf = av_malloc(size);
  142. avpicture_fill(&picture1, buf, PIX_FMT_RGBA32, src_width, src_height);
  143. if (img_convert(&picture1, PIX_FMT_RGBA32,
  144. picture, pix_fmt, src_width, src_height) < 0) {
  145. av_free(buf);
  146. return;
  147. }
  148. pict = &picture1;
  149. }
  150. /* Insert filter code here */ /* ok */
  151. // Get me next frame
  152. if (0 > get_watermark_picture(ci, 0)) {
  153. return;
  154. }
  155. // These are the three original static variables in the ffmpeg hack.
  156. pFrameRGB = ci->pFrameRGB;
  157. xm_size = ci->x_size;
  158. ym_size = ci->y_size;
  159. // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
  160. // According to avcodec.h PIX_FMT_RGBA32 is handled in endian specific manner.
  161. for (y=0; y<src_height; y++) {
  162. offs = y * (src_width * 4);
  163. offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
  164. for (x=0; x<src_width; x++) {
  165. mpoffs = offsm + (((x * xm_size) / src_width) * 4);
  166. p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
  167. pixelm = *p_pixel;
  168. p_pixel = (uint32_t *)&((pict->data[0])[offs]);
  169. pixel = *p_pixel;
  170. // pixelm = *((uint32_t *)&(pFrameRGB->data[mpoffs]));
  171. pixel_meck = pixel & 0xff000000;
  172. // R
  173. tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - 0x80;
  174. if (tmp > 255) tmp = 255;
  175. if (tmp < 0) tmp = 0;
  176. pixel_meck |= (tmp << 16) & 0xff0000;
  177. // G
  178. tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - 0x80;
  179. if (tmp > 255) tmp = 255;
  180. if (tmp < 0) tmp = 0;
  181. pixel_meck |= (tmp << 8) & 0xff00;
  182. // B
  183. tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - 0x80;
  184. if (tmp > 255) tmp = 255;
  185. if (tmp < 0) tmp = 0;
  186. pixel_meck |= (tmp << 0) & 0xff;
  187. // test:
  188. //pixel_meck = pixel & 0xff000000;
  189. //pixel_meck |= (pixelm & 0x00ffffff);
  190. *p_pixel = pixel_meck;
  191. offs += 4;
  192. } // foreach X
  193. } // foreach Y
  194. if (pix_fmt != PIX_FMT_RGBA32) {
  195. if (img_convert(picture, pix_fmt,
  196. &picture1, PIX_FMT_RGBA32, src_width, src_height) < 0) {
  197. }
  198. }
  199. av_free(buf);
  200. }
  201. /****************************************************************************
  202. * When cleanup == 0, we try to get the next frame. If no next frame, nothing
  203. * is done.
  204. *
  205. * This code follows the example on
  206. * http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
  207. *
  208. * 0 = ok, -1 = error
  209. ****************************************************************************/
  210. int get_watermark_picture(ContextInfo *ci, int cleanup)
  211. {
  212. if (1 == ci->is_done && 0 == cleanup) return 0;
  213. // Yes, *pFrameRGB arguments must be null the first time otherwise it's not good..
  214. // This block is only executed the first time we enter this function.
  215. if (0 == ci->pFrameRGB &&
  216. 0 == cleanup)
  217. {
  218. /*
  219. * The last three parameters specify the file format, buffer size and format
  220. * parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect
  221. * the format and use a default buffer size. (Didn't work!)
  222. */
  223. if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {
  224. // Martin says this should not be necessary but it failed for me sending in
  225. // NULL instead of file_iformat to av_open_input_file()
  226. ci->i = strlen(ci->filename);
  227. if (0 == ci->i) {
  228. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\n");
  229. return -1;
  230. }
  231. while (ci->i > 0) {
  232. if (ci->filename[ci->i] == '.') {
  233. ci->i++;
  234. break;
  235. }
  236. ci->i--;
  237. }
  238. ci->p_ext = &(ci->filename[ci->i]);
  239. ci->file_iformat = av_find_input_format (ci->p_ext);
  240. if (0 == ci->file_iformat) {
  241. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\n", ci->p_ext);
  242. return -1;
  243. }
  244. // now continues the Martin template.
  245. if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {
  246. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\n", ci->filename);
  247. return -1;
  248. }
  249. }
  250. /*
  251. * This fills the streams field of the AVFormatContext with valid information.
  252. */
  253. if(av_find_stream_info(ci->pFormatCtx)<0) {
  254. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\n");
  255. return -1;
  256. }
  257. /*
  258. * As mentioned in the introduction, we'll handle only video streams, not audio
  259. * streams. To make things nice and easy, we simply use the first video stream we
  260. * find.
  261. */
  262. ci->videoStream=-1;
  263. for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)
  264. if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)
  265. {
  266. ci->videoStream = ci->i;
  267. break;
  268. }
  269. if(ci->videoStream == -1) {
  270. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\n");
  271. return -1;
  272. }
  273. ci->st = ci->pFormatCtx->streams[ci->videoStream];
  274. ci->x_size = ci->st->codec->width;
  275. ci->y_size = ci->st->codec->height;
  276. // Get a pointer to the codec context for the video stream
  277. ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;
  278. /*
  279. * OK, so now we've got a pointer to the so-called codec context for our video
  280. * stream, but we still have to find the actual codec and open it.
  281. */
  282. // Find the decoder for the video stream
  283. ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);
  284. if(ci->pCodec == NULL) {
  285. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\n");
  286. return -1;
  287. }
  288. // Inform the codec that we can handle truncated bitstreams -- i.e.,
  289. // bitstreams where frame boundaries can fall in the middle of packets
  290. if (ci->pCodec->capabilities & CODEC_CAP_TRUNCATED)
  291. ci->pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
  292. // Open codec
  293. if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {
  294. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\n");
  295. return -1;
  296. }
  297. // Hack to correct wrong frame rates that seem to be generated by some
  298. // codecs
  299. if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)
  300. ci->pCodecCtx->time_base.num=1000;
  301. /*
  302. * Allocate a video frame to store the decoded images in.
  303. */
  304. ci->pFrame = avcodec_alloc_frame();
  305. /*
  306. * The RGB image pFrameRGB (of type AVFrame *) is allocated like this:
  307. */
  308. // Allocate an AVFrame structure
  309. ci->pFrameRGB=avcodec_alloc_frame();
  310. if(ci->pFrameRGB==NULL) {
  311. av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\n");
  312. return -1;
  313. }
  314. // Determine required buffer size and allocate buffer
  315. ci->numBytes = avpicture_get_size(PIX_FMT_RGBA32, ci->pCodecCtx->width,
  316. ci->pCodecCtx->height);
  317. ci->buffer = av_malloc(ci->numBytes);
  318. // Assign appropriate parts of buffer to image planes in pFrameRGB
  319. avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGBA32,
  320. ci->pCodecCtx->width, ci->pCodecCtx->height);
  321. }
  322. // TODO loop, pingpong etc?
  323. if (0 == cleanup)
  324. {
  325. // av_log(NULL, AV_LOG_DEBUG, "get_watermark_picture() Get a frame\n");
  326. while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)
  327. {
  328. // Is this a packet from the video stream?
  329. if(ci->packet.stream_index == ci->videoStream)
  330. {
  331. // Decode video frame
  332. avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,
  333. ci->packet.data, ci->packet.size);
  334. // Did we get a video frame?
  335. if(ci->frameFinished)
  336. {
  337. // Convert the image from its native format to RGBA32
  338. img_convert((AVPicture *)ci->pFrameRGB, PIX_FMT_RGBA32,
  339. (AVPicture*)(ci->pFrame), ci->pCodecCtx->pix_fmt, ci->pCodecCtx->width,
  340. ci->pCodecCtx->height);
  341. // Process the video frame (save to disk etc.)
  342. //fprintf(stderr,"banan() New frame!\n");
  343. //DoSomethingWithTheImage(ci->pFrameRGB);
  344. return 0;
  345. }
  346. }
  347. // Free the packet that was allocated by av_read_frame
  348. av_free_packet(&ci->packet);
  349. }
  350. ci->is_done = 1;
  351. return 0;
  352. } // if 0 != cleanup
  353. if (0 != cleanup)
  354. {
  355. // Free the RGB image
  356. if (0 != ci->buffer) {
  357. av_free(ci->buffer);
  358. ci->buffer = 0;
  359. }
  360. if (0 != ci->pFrameRGB) {
  361. av_free(ci->pFrameRGB);
  362. ci->pFrameRGB = 0;
  363. }
  364. // Close the codec
  365. if (0 != ci->pCodecCtx) {
  366. avcodec_close(ci->pCodecCtx);
  367. ci->pCodecCtx = 0;
  368. }
  369. // Close the video file
  370. if (0 != ci->pFormatCtx) {
  371. av_close_input_file(ci->pFormatCtx);
  372. ci->pFormatCtx = 0;
  373. }
  374. ci->is_done = 0;
  375. }
  376. return 0;
  377. }
  378. void parse_arg_file(const char *filename)
  379. {
  380. }