vfwcap.c 12 KB

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