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.

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