vfwcap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * VFW capture interface
  3. * Copyright (c) 2006-2008 Ramiro Polla
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavformat/avformat.h"
  22. #include <windows.h>
  23. #include <vfw.h>
  24. //#define DEBUG_VFW
  25. /* Defines for VFW missing from MinGW.
  26. * Remove this when MinGW incorporates them. */
  27. #define HWND_MESSAGE ((HWND)-3)
  28. #define BI_RGB 0
  29. /* End of missing MinGW defines */
  30. struct vfw_ctx {
  31. HWND hwnd;
  32. HANDLE mutex;
  33. HANDLE event;
  34. AVPacketList *pktl;
  35. unsigned int curbufsize;
  36. unsigned int frame_num;
  37. };
  38. static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
  39. {
  40. switch(biCompression) {
  41. case MKTAG('U', 'Y', 'V', 'Y'):
  42. return PIX_FMT_UYVY422;
  43. case MKTAG('Y', 'U', 'Y', '2'):
  44. return PIX_FMT_YUYV422;
  45. case MKTAG('I', '4', '2', '0'):
  46. return PIX_FMT_YUV420P;
  47. case BI_RGB:
  48. switch(biBitCount) { /* 1-8 are untested */
  49. case 1:
  50. return PIX_FMT_MONOWHITE;
  51. case 4:
  52. return PIX_FMT_RGB4;
  53. case 8:
  54. return PIX_FMT_RGB8;
  55. case 16:
  56. return PIX_FMT_RGB555;
  57. case 24:
  58. return PIX_FMT_BGR24;
  59. case 32:
  60. return PIX_FMT_RGB32;
  61. }
  62. }
  63. return PIX_FMT_NONE;
  64. }
  65. static enum CodecID vfw_codecid(DWORD biCompression)
  66. {
  67. switch(biCompression) {
  68. case MKTAG('d', 'v', 's', 'd'):
  69. return CODEC_ID_DVVIDEO;
  70. case MKTAG('M', 'J', 'P', 'G'):
  71. case MKTAG('m', 'j', 'p', 'g'):
  72. return CODEC_ID_MJPEG;
  73. }
  74. return CODEC_ID_NONE;
  75. }
  76. #define dstruct(pctx, sname, var, type) \
  77. av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
  78. static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
  79. {
  80. av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
  81. dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
  82. dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
  83. dstruct(s, cparms, wPercentDropForError, "u");
  84. dstruct(s, cparms, fYield, "d");
  85. dstruct(s, cparms, dwIndexSize, "lu");
  86. dstruct(s, cparms, wChunkGranularity, "u");
  87. dstruct(s, cparms, fUsingDOSMemory, "d");
  88. dstruct(s, cparms, wNumVideoRequested, "u");
  89. dstruct(s, cparms, fCaptureAudio, "d");
  90. dstruct(s, cparms, wNumAudioRequested, "u");
  91. dstruct(s, cparms, vKeyAbort, "u");
  92. dstruct(s, cparms, fAbortLeftMouse, "d");
  93. dstruct(s, cparms, fAbortRightMouse, "d");
  94. dstruct(s, cparms, fLimitEnabled, "d");
  95. dstruct(s, cparms, wTimeLimit, "u");
  96. dstruct(s, cparms, fMCIControl, "d");
  97. dstruct(s, cparms, fStepMCIDevice, "d");
  98. dstruct(s, cparms, dwMCIStartTime, "lu");
  99. dstruct(s, cparms, dwMCIStopTime, "lu");
  100. dstruct(s, cparms, fStepCaptureAt2x, "d");
  101. dstruct(s, cparms, wStepCaptureAverageFrames, "u");
  102. dstruct(s, cparms, dwAudioBufferSize, "lu");
  103. dstruct(s, cparms, fDisableWriteCache, "d");
  104. dstruct(s, cparms, AVStreamMaster, "u");
  105. }
  106. static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
  107. {
  108. #ifdef DEBUG_VFW
  109. av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
  110. dstruct(s, vhdr, lpData, "p");
  111. dstruct(s, vhdr, dwBufferLength, "lu");
  112. dstruct(s, vhdr, dwBytesUsed, "lu");
  113. dstruct(s, vhdr, dwTimeCaptured, "lu");
  114. dstruct(s, vhdr, dwUser, "lu");
  115. dstruct(s, vhdr, dwFlags, "lu");
  116. dstruct(s, vhdr, dwReserved[0], "lu");
  117. dstruct(s, vhdr, dwReserved[1], "lu");
  118. dstruct(s, vhdr, dwReserved[2], "lu");
  119. dstruct(s, vhdr, dwReserved[3], "lu");
  120. #endif
  121. }
  122. static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
  123. {
  124. av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
  125. dstruct(s, bih, biSize, "lu");
  126. dstruct(s, bih, biWidth, "ld");
  127. dstruct(s, bih, biHeight, "ld");
  128. dstruct(s, bih, biPlanes, "d");
  129. dstruct(s, bih, biBitCount, "d");
  130. dstruct(s, bih, biCompression, "lu");
  131. av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
  132. (char*) &bih->biCompression);
  133. dstruct(s, bih, biSizeImage, "lu");
  134. dstruct(s, bih, biXPelsPerMeter, "lu");
  135. dstruct(s, bih, biYPelsPerMeter, "lu");
  136. dstruct(s, bih, biClrUsed, "lu");
  137. dstruct(s, bih, biClrImportant, "lu");
  138. }
  139. static int shall_we_drop(AVFormatContext *s)
  140. {
  141. struct vfw_ctx *ctx = s->priv_data;
  142. const uint8_t dropscore[] = {62, 75, 87, 100};
  143. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  144. unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
  145. if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
  146. av_log(s, AV_LOG_ERROR,
  147. "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
  153. {
  154. AVFormatContext *s;
  155. struct vfw_ctx *ctx;
  156. AVPacketList **ppktl, *pktl_next;
  157. s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  158. ctx = s->priv_data;
  159. dump_videohdr(s, vdhdr);
  160. if(shall_we_drop(s))
  161. return FALSE;
  162. WaitForSingleObject(ctx->mutex, INFINITE);
  163. pktl_next = av_mallocz(sizeof(AVPacketList));
  164. if(!pktl_next)
  165. goto fail;
  166. if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
  167. av_free(pktl_next);
  168. goto fail;
  169. }
  170. pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
  171. memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
  172. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  173. *ppktl = pktl_next;
  174. ctx->curbufsize += vdhdr->dwBytesUsed;
  175. SetEvent(ctx->event);
  176. ReleaseMutex(ctx->mutex);
  177. return TRUE;
  178. fail:
  179. ReleaseMutex(ctx->mutex);
  180. return FALSE;
  181. }
  182. static int vfw_read_close(AVFormatContext *s)
  183. {
  184. struct vfw_ctx *ctx = s->priv_data;
  185. AVPacketList *pktl;
  186. if(ctx->hwnd) {
  187. SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
  188. SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
  189. DestroyWindow(ctx->hwnd);
  190. }
  191. if(ctx->mutex)
  192. CloseHandle(ctx->mutex);
  193. if(ctx->event)
  194. CloseHandle(ctx->event);
  195. pktl = ctx->pktl;
  196. while (pktl) {
  197. AVPacketList *next = pktl->next;
  198. av_destruct_packet(&pktl->pkt);
  199. av_free(pktl);
  200. pktl = next;
  201. }
  202. return 0;
  203. }
  204. static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  205. {
  206. struct vfw_ctx *ctx = s->priv_data;
  207. AVCodecContext *codec;
  208. AVStream *st;
  209. int devnum;
  210. int bisize;
  211. BITMAPINFO *bi;
  212. CAPTUREPARMS cparms;
  213. DWORD biCompression;
  214. WORD biBitCount;
  215. int width;
  216. int height;
  217. int ret;
  218. if (!strcmp(s->filename, "list")) {
  219. for (devnum = 0; devnum <= 9; devnum++) {
  220. char driver_name[256];
  221. char driver_ver[256];
  222. ret = capGetDriverDescription(devnum,
  223. driver_name, sizeof(driver_name),
  224. driver_ver, sizeof(driver_ver));
  225. if (ret) {
  226. av_log(s, AV_LOG_INFO, "Driver %d\n", devnum);
  227. av_log(s, AV_LOG_INFO, " %s\n", driver_name);
  228. av_log(s, AV_LOG_INFO, " %s\n", driver_ver);
  229. }
  230. }
  231. return AVERROR(EIO);
  232. }
  233. if(!ap->time_base.den) {
  234. av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
  235. return AVERROR(EIO);
  236. }
  237. ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
  238. if(!ctx->hwnd) {
  239. av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
  240. return AVERROR(EIO);
  241. }
  242. /* If atoi fails, devnum==0 and the default device is used */
  243. devnum = atoi(s->filename);
  244. ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
  245. if(!ret) {
  246. av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
  247. DestroyWindow(ctx->hwnd);
  248. return AVERROR(ENODEV);
  249. }
  250. SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
  251. SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
  252. ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
  253. (LPARAM) videostream_cb);
  254. if(!ret) {
  255. av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
  256. goto fail_io;
  257. }
  258. SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
  259. st = av_new_stream(s, 0);
  260. if(!st) {
  261. vfw_read_close(s);
  262. return AVERROR(ENOMEM);
  263. }
  264. /* Set video format */
  265. bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
  266. if(!bisize)
  267. goto fail_io;
  268. bi = av_malloc(bisize);
  269. if(!bi) {
  270. vfw_read_close(s);
  271. return AVERROR(ENOMEM);
  272. }
  273. ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
  274. if(!ret)
  275. goto fail_bi;
  276. dump_bih(s, &bi->bmiHeader);
  277. width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
  278. height = ap->height ? ap->height : bi->bmiHeader.biHeight;
  279. bi->bmiHeader.biWidth = width ;
  280. bi->bmiHeader.biHeight = height;
  281. if (0) {
  282. /* For testing yet unsupported compressions
  283. * Copy these values from user-supplied verbose information */
  284. bi->bmiHeader.biWidth = 320;
  285. bi->bmiHeader.biHeight = 240;
  286. bi->bmiHeader.biPlanes = 1;
  287. bi->bmiHeader.biBitCount = 12;
  288. bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
  289. bi->bmiHeader.biSizeImage = 115200;
  290. dump_bih(s, &bi->bmiHeader);
  291. }
  292. ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
  293. if(!ret) {
  294. av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
  295. goto fail_bi;
  296. }
  297. biCompression = bi->bmiHeader.biCompression;
  298. biBitCount = bi->bmiHeader.biBitCount;
  299. av_free(bi);
  300. /* Set sequence setup */
  301. ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
  302. (LPARAM) &cparms);
  303. if(!ret)
  304. goto fail_io;
  305. dump_captureparms(s, &cparms);
  306. cparms.fYield = 1; // Spawn a background thread
  307. cparms.dwRequestMicroSecPerFrame =
  308. (ap->time_base.num*1000000) / ap->time_base.den;
  309. cparms.fAbortLeftMouse = 0;
  310. cparms.fAbortRightMouse = 0;
  311. cparms.fCaptureAudio = 0;
  312. cparms.vKeyAbort = 0;
  313. ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
  314. (LPARAM) &cparms);
  315. if(!ret)
  316. goto fail_io;
  317. codec = st->codec;
  318. codec->time_base = ap->time_base;
  319. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  320. codec->width = width;
  321. codec->height = height;
  322. codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
  323. if(codec->pix_fmt == PIX_FMT_NONE) {
  324. codec->codec_id = vfw_codecid(biCompression);
  325. if(codec->codec_id == CODEC_ID_NONE) {
  326. av_log(s, AV_LOG_ERROR, "Unknown compression type. "
  327. "Please report verbose (-v 9) debug information.\n");
  328. vfw_read_close(s);
  329. return AVERROR_PATCHWELCOME;
  330. }
  331. codec->bits_per_coded_sample = biBitCount;
  332. } else {
  333. codec->codec_id = CODEC_ID_RAWVIDEO;
  334. if(biCompression == BI_RGB) {
  335. codec->bits_per_coded_sample = biBitCount;
  336. codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  337. if (codec->extradata) {
  338. codec->extradata_size = 9;
  339. memcpy(codec->extradata, "BottomUp", 9);
  340. }
  341. }
  342. }
  343. av_set_pts_info(st, 32, 1, 1000);
  344. ctx->mutex = CreateMutex(NULL, 0, NULL);
  345. if(!ctx->mutex) {
  346. av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
  347. goto fail_io;
  348. }
  349. ctx->event = CreateEvent(NULL, 1, 0, NULL);
  350. if(!ctx->event) {
  351. av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
  352. goto fail_io;
  353. }
  354. ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
  355. if(!ret) {
  356. av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
  357. goto fail_io;
  358. }
  359. return 0;
  360. fail_bi:
  361. av_free(bi);
  362. fail_io:
  363. vfw_read_close(s);
  364. return AVERROR(EIO);
  365. }
  366. static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
  367. {
  368. struct vfw_ctx *ctx = s->priv_data;
  369. AVPacketList *pktl = NULL;
  370. while(!pktl) {
  371. WaitForSingleObject(ctx->mutex, INFINITE);
  372. pktl = ctx->pktl;
  373. if(ctx->pktl) {
  374. *pkt = ctx->pktl->pkt;
  375. ctx->pktl = ctx->pktl->next;
  376. av_free(pktl);
  377. }
  378. ResetEvent(ctx->event);
  379. ReleaseMutex(ctx->mutex);
  380. if(!pktl) {
  381. if(s->flags & AVFMT_FLAG_NONBLOCK) {
  382. return AVERROR(EAGAIN);
  383. } else {
  384. WaitForSingleObject(ctx->event, INFINITE);
  385. }
  386. }
  387. }
  388. ctx->curbufsize -= pkt->size;
  389. return pkt->size;
  390. }
  391. AVInputFormat vfwcap_demuxer = {
  392. "vfwcap",
  393. NULL_IF_CONFIG_SMALL("VFW video capture"),
  394. sizeof(struct vfw_ctx),
  395. NULL,
  396. vfw_read_header,
  397. vfw_read_packet,
  398. vfw_read_close,
  399. .flags = AVFMT_NOFILE,
  400. };