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.

467 lines
13KB

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