vfwcap.c 12 KB

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