watermark.c 23 KB

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