watermark.c 23 KB

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