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.

463 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 "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. /* End of missing MinGW defines */
  29. struct vfw_ctx {
  30. HWND hwnd;
  31. HANDLE mutex;
  32. HANDLE event;
  33. AVPacketList *pktl;
  34. unsigned int curbufsize;
  35. unsigned int frame_num;
  36. };
  37. static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
  38. {
  39. switch(biCompression) {
  40. case MKTAG('U', 'Y', 'V', 'Y'):
  41. return PIX_FMT_UYVY422;
  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. case MKTAG('M', 'J', 'P', 'G'):
  70. case MKTAG('m', 'j', 'p', 'g'):
  71. return CODEC_ID_MJPEG;
  72. }
  73. return CODEC_ID_NONE;
  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(AVFormatContext *s)
  139. {
  140. struct vfw_ctx *ctx = s->priv_data;
  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(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. AVFormatContext *s;
  154. struct vfw_ctx *ctx;
  155. AVPacketList **ppktl, *pktl_next;
  156. s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  157. ctx = s->priv_data;
  158. dump_videohdr(s, vdhdr);
  159. if(shall_we_drop(s))
  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. AVPacketList *pktl;
  185. if(ctx->hwnd) {
  186. SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
  187. SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
  188. DestroyWindow(ctx->hwnd);
  189. }
  190. if(ctx->mutex)
  191. CloseHandle(ctx->mutex);
  192. if(ctx->event)
  193. CloseHandle(ctx->event);
  194. pktl = ctx->pktl;
  195. while (pktl) {
  196. AVPacketList *next = pktl->next;
  197. av_destruct_packet(&pktl->pkt);
  198. av_free(pktl);
  199. pktl = next;
  200. }
  201. return 0;
  202. }
  203. static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  204. {
  205. struct vfw_ctx *ctx = s->priv_data;
  206. AVCodecContext *codec;
  207. AVStream *st;
  208. int devnum;
  209. int bisize;
  210. BITMAPINFO *bi;
  211. CAPTUREPARMS cparms;
  212. DWORD biCompression;
  213. WORD biBitCount;
  214. int width;
  215. int height;
  216. int ret;
  217. if (!strcmp(s->filename, "list")) {
  218. for (devnum = 0; devnum <= 9; devnum++) {
  219. char driver_name[256];
  220. char driver_ver[256];
  221. ret = capGetDriverDescription(devnum,
  222. driver_name, sizeof(driver_name),
  223. driver_ver, sizeof(driver_ver));
  224. if (ret) {
  225. av_log(s, AV_LOG_INFO, "Driver %d\n", devnum);
  226. av_log(s, AV_LOG_INFO, " %s\n", driver_name);
  227. av_log(s, AV_LOG_INFO, " %s\n", driver_ver);
  228. }
  229. }
  230. return AVERROR(EIO);
  231. }
  232. if(!ap->time_base.den) {
  233. av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
  234. return AVERROR(EIO);
  235. }
  236. ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
  237. if(!ctx->hwnd) {
  238. av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
  239. return AVERROR(EIO);
  240. }
  241. /* If atoi fails, devnum==0 and the default device is used */
  242. devnum = atoi(s->filename);
  243. ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
  244. if(!ret) {
  245. av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
  246. DestroyWindow(ctx->hwnd);
  247. return AVERROR(ENODEV);
  248. }
  249. SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
  250. SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
  251. ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
  252. (LPARAM) videostream_cb);
  253. if(!ret) {
  254. av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
  255. goto fail_io;
  256. }
  257. SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
  258. st = av_new_stream(s, 0);
  259. if(!st) {
  260. vfw_read_close(s);
  261. return AVERROR(ENOMEM);
  262. }
  263. /* Set video format */
  264. bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
  265. if(!bisize)
  266. goto fail_io;
  267. bi = av_malloc(bisize);
  268. if(!bi) {
  269. vfw_read_close(s);
  270. return AVERROR(ENOMEM);
  271. }
  272. ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
  273. if(!ret)
  274. goto fail_bi;
  275. dump_bih(s, &bi->bmiHeader);
  276. width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
  277. height = ap->height ? ap->height : bi->bmiHeader.biHeight;
  278. bi->bmiHeader.biWidth = width ;
  279. bi->bmiHeader.biHeight = height;
  280. if (0) {
  281. /* For testing yet unsupported compressions
  282. * Copy these values from user-supplied verbose information */
  283. bi->bmiHeader.biWidth = 320;
  284. bi->bmiHeader.biHeight = 240;
  285. bi->bmiHeader.biPlanes = 1;
  286. bi->bmiHeader.biBitCount = 12;
  287. bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
  288. bi->bmiHeader.biSizeImage = 115200;
  289. dump_bih(s, &bi->bmiHeader);
  290. }
  291. ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
  292. if(!ret) {
  293. av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
  294. goto fail_bi;
  295. }
  296. biCompression = bi->bmiHeader.biCompression;
  297. biBitCount = bi->bmiHeader.biBitCount;
  298. av_free(bi);
  299. /* Set sequence setup */
  300. ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
  301. (LPARAM) &cparms);
  302. if(!ret)
  303. goto fail_io;
  304. dump_captureparms(s, &cparms);
  305. cparms.fYield = 1; // Spawn a background thread
  306. cparms.dwRequestMicroSecPerFrame =
  307. (ap->time_base.num*1000000) / ap->time_base.den;
  308. cparms.fAbortLeftMouse = 0;
  309. cparms.fAbortRightMouse = 0;
  310. cparms.fCaptureAudio = 0;
  311. cparms.vKeyAbort = 0;
  312. ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
  313. (LPARAM) &cparms);
  314. if(!ret)
  315. goto fail_io;
  316. codec = st->codec;
  317. codec->time_base = ap->time_base;
  318. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  319. codec->width = width;
  320. codec->height = height;
  321. codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
  322. if(codec->pix_fmt == PIX_FMT_NONE) {
  323. codec->codec_id = vfw_codecid(biCompression);
  324. if(codec->codec_id == CODEC_ID_NONE) {
  325. av_log(s, AV_LOG_ERROR, "Unknown compression type. "
  326. "Please report verbose (-v 9) debug information.\n");
  327. vfw_read_close(s);
  328. return AVERROR_PATCHWELCOME;
  329. }
  330. codec->bits_per_coded_sample = biBitCount;
  331. } else {
  332. codec->codec_id = CODEC_ID_RAWVIDEO;
  333. if(biCompression == BI_RGB) {
  334. codec->bits_per_coded_sample = biBitCount;
  335. codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  336. if (codec->extradata) {
  337. codec->extradata_size = 9;
  338. memcpy(codec->extradata, "BottomUp", 9);
  339. }
  340. }
  341. }
  342. av_set_pts_info(st, 32, 1, 1000);
  343. ctx->mutex = CreateMutex(NULL, 0, NULL);
  344. if(!ctx->mutex) {
  345. av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
  346. goto fail_io;
  347. }
  348. ctx->event = CreateEvent(NULL, 1, 0, NULL);
  349. if(!ctx->event) {
  350. av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
  351. goto fail_io;
  352. }
  353. ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
  354. if(!ret) {
  355. av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
  356. goto fail_io;
  357. }
  358. return 0;
  359. fail_bi:
  360. av_free(bi);
  361. fail_io:
  362. vfw_read_close(s);
  363. return AVERROR(EIO);
  364. }
  365. static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
  366. {
  367. struct vfw_ctx *ctx = s->priv_data;
  368. AVPacketList *pktl = NULL;
  369. while(!pktl) {
  370. WaitForSingleObject(ctx->mutex, INFINITE);
  371. pktl = ctx->pktl;
  372. if(ctx->pktl) {
  373. *pkt = ctx->pktl->pkt;
  374. ctx->pktl = ctx->pktl->next;
  375. av_free(pktl);
  376. }
  377. ResetEvent(ctx->event);
  378. ReleaseMutex(ctx->mutex);
  379. if(!pktl) {
  380. if(s->flags & AVFMT_FLAG_NONBLOCK) {
  381. return AVERROR(EAGAIN);
  382. } else {
  383. WaitForSingleObject(ctx->event, INFINITE);
  384. }
  385. }
  386. }
  387. ctx->curbufsize -= pkt->size;
  388. return pkt->size;
  389. }
  390. AVInputFormat ff_vfwcap_demuxer = {
  391. "vfwcap",
  392. NULL_IF_CONFIG_SMALL("VFW video capture"),
  393. sizeof(struct vfw_ctx),
  394. NULL,
  395. vfw_read_header,
  396. vfw_read_packet,
  397. vfw_read_close,
  398. .flags = AVFMT_NOFILE,
  399. };