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.

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