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.

431 lines
12KB

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