2 * VFW capture interface
3 * Copyright (c) 2006-2008 Ramiro Polla
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavformat/avformat.h"
28 /* Defines for VFW missing from MinGW.
29 * Remove this when MinGW incorporates them. */
30 #define HWND_MESSAGE ((HWND)-3)
34 /* End of missing MinGW defines */
41 unsigned int curbufsize;
42 unsigned int frame_num;
45 static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
47 switch(biCompression) {
48 case MKTAG('U', 'Y', 'V', 'Y'):
49 return PIX_FMT_UYVY422;
50 case MKTAG('Y', 'U', 'Y', '2'):
51 return PIX_FMT_YUYV422;
52 case MKTAG('I', '4', '2', '0'):
53 return PIX_FMT_YUV420P;
55 switch(biBitCount) { /* 1-8 are untested */
57 return PIX_FMT_MONOWHITE;
63 return PIX_FMT_RGB555;
73 static enum CodecID vfw_codecid(DWORD biCompression)
75 switch(biCompression) {
76 case MKTAG('d', 'v', 's', 'd'):
77 return CODEC_ID_DVVIDEO;
78 case MKTAG('M', 'J', 'P', 'G'):
79 case MKTAG('m', 'j', 'p', 'g'):
80 return CODEC_ID_MJPEG;
85 #define dstruct(pctx, sname, var, type) \
86 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
88 static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
90 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
91 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
92 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
93 dstruct(s, cparms, wPercentDropForError, "u");
94 dstruct(s, cparms, fYield, "d");
95 dstruct(s, cparms, dwIndexSize, "lu");
96 dstruct(s, cparms, wChunkGranularity, "u");
97 dstruct(s, cparms, fUsingDOSMemory, "d");
98 dstruct(s, cparms, wNumVideoRequested, "u");
99 dstruct(s, cparms, fCaptureAudio, "d");
100 dstruct(s, cparms, wNumAudioRequested, "u");
101 dstruct(s, cparms, vKeyAbort, "u");
102 dstruct(s, cparms, fAbortLeftMouse, "d");
103 dstruct(s, cparms, fAbortRightMouse, "d");
104 dstruct(s, cparms, fLimitEnabled, "d");
105 dstruct(s, cparms, wTimeLimit, "u");
106 dstruct(s, cparms, fMCIControl, "d");
107 dstruct(s, cparms, fStepMCIDevice, "d");
108 dstruct(s, cparms, dwMCIStartTime, "lu");
109 dstruct(s, cparms, dwMCIStopTime, "lu");
110 dstruct(s, cparms, fStepCaptureAt2x, "d");
111 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
112 dstruct(s, cparms, dwAudioBufferSize, "lu");
113 dstruct(s, cparms, fDisableWriteCache, "d");
114 dstruct(s, cparms, AVStreamMaster, "u");
117 static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
120 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
121 dstruct(s, vhdr, lpData, "p");
122 dstruct(s, vhdr, dwBufferLength, "lu");
123 dstruct(s, vhdr, dwBytesUsed, "lu");
124 dstruct(s, vhdr, dwTimeCaptured, "lu");
125 dstruct(s, vhdr, dwUser, "lu");
126 dstruct(s, vhdr, dwFlags, "lu");
127 dstruct(s, vhdr, dwReserved[0], "lu");
128 dstruct(s, vhdr, dwReserved[1], "lu");
129 dstruct(s, vhdr, dwReserved[2], "lu");
130 dstruct(s, vhdr, dwReserved[3], "lu");
134 static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
136 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
137 dstruct(s, bih, biSize, "lu");
138 dstruct(s, bih, biWidth, "ld");
139 dstruct(s, bih, biHeight, "ld");
140 dstruct(s, bih, biPlanes, "d");
141 dstruct(s, bih, biBitCount, "d");
142 dstruct(s, bih, biCompression, "lu");
143 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
144 (char*) &bih->biCompression);
145 dstruct(s, bih, biSizeImage, "lu");
146 dstruct(s, bih, biXPelsPerMeter, "lu");
147 dstruct(s, bih, biYPelsPerMeter, "lu");
148 dstruct(s, bih, biClrUsed, "lu");
149 dstruct(s, bih, biClrImportant, "lu");
152 static int shall_we_drop(AVFormatContext *s)
154 struct vfw_ctx *ctx = s->priv_data;
155 const uint8_t dropscore[] = {62, 75, 87, 100};
156 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
157 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
159 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
160 av_log(s, AV_LOG_ERROR,
161 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
168 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
172 AVPacketList **ppktl, *pktl_next;
174 s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
177 dump_videohdr(s, vdhdr);
182 WaitForSingleObject(ctx->mutex, INFINITE);
184 pktl_next = av_mallocz(sizeof(AVPacketList));
188 if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
193 pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
194 memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
196 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
199 ctx->curbufsize += vdhdr->dwBytesUsed;
201 SetEvent(ctx->event);
202 ReleaseMutex(ctx->mutex);
206 ReleaseMutex(ctx->mutex);
210 static int vfw_read_close(AVFormatContext *s)
212 struct vfw_ctx *ctx = s->priv_data;
216 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
217 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
218 DestroyWindow(ctx->hwnd);
221 CloseHandle(ctx->mutex);
223 CloseHandle(ctx->event);
227 AVPacketList *next = pktl->next;
228 av_destruct_packet(&pktl->pkt);
236 static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
238 struct vfw_ctx *ctx = s->priv_data;
239 AVCodecContext *codec;
251 if (!strcmp(s->filename, "list")) {
252 for (devnum = 0; devnum <= 9; devnum++) {
253 char driver_name[256];
254 char driver_ver[256];
255 ret = capGetDriverDescription(devnum,
256 driver_name, sizeof(driver_name),
257 driver_ver, sizeof(driver_ver));
259 av_log(s, AV_LOG_INFO, "Driver %d\n", devnum);
260 av_log(s, AV_LOG_INFO, " %s\n", driver_name);
261 av_log(s, AV_LOG_INFO, " %s\n", driver_ver);
267 if(!ap->time_base.den) {
268 av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
272 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
274 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
278 /* If atoi fails, devnum==0 and the default device is used */
279 devnum = atoi(s->filename);
281 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
283 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
284 DestroyWindow(ctx->hwnd);
285 return AVERROR(ENODEV);
288 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
289 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
291 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
292 (LPARAM) videostream_cb);
294 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
298 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
300 st = av_new_stream(s, 0);
303 return AVERROR(ENOMEM);
306 /* Set video format */
307 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
310 bi = av_malloc(bisize);
313 return AVERROR(ENOMEM);
315 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
319 dump_bih(s, &bi->bmiHeader);
321 width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
322 height = ap->height ? ap->height : bi->bmiHeader.biHeight;
323 bi->bmiHeader.biWidth = width ;
324 bi->bmiHeader.biHeight = height;
327 /* For testing yet unsupported compressions
328 * Copy these values from user-supplied verbose information */
329 bi->bmiHeader.biWidth = 320;
330 bi->bmiHeader.biHeight = 240;
331 bi->bmiHeader.biPlanes = 1;
332 bi->bmiHeader.biBitCount = 12;
333 bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
334 bi->bmiHeader.biSizeImage = 115200;
335 dump_bih(s, &bi->bmiHeader);
338 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
340 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
344 biCompression = bi->bmiHeader.biCompression;
345 biBitCount = bi->bmiHeader.biBitCount;
349 /* Set sequence setup */
350 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
355 dump_captureparms(s, &cparms);
357 cparms.fYield = 1; // Spawn a background thread
358 cparms.dwRequestMicroSecPerFrame =
359 (ap->time_base.num*1000000) / ap->time_base.den;
360 cparms.fAbortLeftMouse = 0;
361 cparms.fAbortRightMouse = 0;
362 cparms.fCaptureAudio = 0;
363 cparms.vKeyAbort = 0;
365 ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
371 codec->time_base = ap->time_base;
372 codec->codec_type = AVMEDIA_TYPE_VIDEO;
373 codec->width = width;
374 codec->height = height;
375 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
376 if(codec->pix_fmt == PIX_FMT_NONE) {
377 codec->codec_id = vfw_codecid(biCompression);
378 if(codec->codec_id == CODEC_ID_NONE) {
379 av_log(s, AV_LOG_ERROR, "Unknown compression type. "
380 "Please report verbose (-v 9) debug information.\n");
382 return AVERROR_PATCHWELCOME;
384 codec->bits_per_coded_sample = biBitCount;
386 codec->codec_id = CODEC_ID_RAWVIDEO;
387 if(biCompression == BI_RGB) {
388 codec->bits_per_coded_sample = biBitCount;
389 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
390 if (codec->extradata) {
391 codec->extradata_size = 9;
392 memcpy(codec->extradata, "BottomUp", 9);
397 av_set_pts_info(st, 32, 1, 1000);
399 ctx->mutex = CreateMutex(NULL, 0, NULL);
401 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
404 ctx->event = CreateEvent(NULL, 1, 0, NULL);
406 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
410 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
412 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
426 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
428 struct vfw_ctx *ctx = s->priv_data;
429 AVPacketList *pktl = NULL;
432 WaitForSingleObject(ctx->mutex, INFINITE);
435 *pkt = ctx->pktl->pkt;
436 ctx->pktl = ctx->pktl->next;
439 ResetEvent(ctx->event);
440 ReleaseMutex(ctx->mutex);
442 if(s->flags & AVFMT_FLAG_NONBLOCK) {
443 return AVERROR(EAGAIN);
445 WaitForSingleObject(ctx->event, INFINITE);
450 ctx->curbufsize -= pkt->size;
455 AVInputFormat ff_vfwcap_demuxer = {
457 NULL_IF_CONFIG_SMALL("VFW video capture"),
458 sizeof(struct vfw_ctx),
463 .flags = AVFMT_NOFILE,