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"
23 #include "libavformat/internal.h"
24 #include "libavutil/log.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
36 unsigned int curbufsize;
37 unsigned int frame_num;
38 char *video_size; /**< A string describing video size, set by a private option. */
39 char *framerate; /**< Set by a private option. */
42 static enum AVPixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
44 switch(biCompression) {
45 case MKTAG('U', 'Y', 'V', 'Y'):
46 return AV_PIX_FMT_UYVY422;
47 case MKTAG('Y', 'U', 'Y', '2'):
48 return AV_PIX_FMT_YUYV422;
49 case MKTAG('I', '4', '2', '0'):
50 return AV_PIX_FMT_YUV420P;
52 switch(biBitCount) { /* 1-8 are untested */
54 return AV_PIX_FMT_MONOWHITE;
56 return AV_PIX_FMT_RGB4;
58 return AV_PIX_FMT_RGB8;
60 return AV_PIX_FMT_RGB555;
62 return AV_PIX_FMT_BGR24;
64 return AV_PIX_FMT_RGB32;
67 return AV_PIX_FMT_NONE;
70 static enum AVCodecID vfw_codecid(DWORD biCompression)
72 switch(biCompression) {
73 case MKTAG('d', 'v', 's', 'd'):
74 return AV_CODEC_ID_DVVIDEO;
75 case MKTAG('M', 'J', 'P', 'G'):
76 case MKTAG('m', 'j', 'p', 'g'):
77 return AV_CODEC_ID_MJPEG;
79 return AV_CODEC_ID_NONE;
82 #define dstruct(pctx, sname, var, type) \
83 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
85 static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
87 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
88 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
89 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
90 dstruct(s, cparms, wPercentDropForError, "u");
91 dstruct(s, cparms, fYield, "d");
92 dstruct(s, cparms, dwIndexSize, "lu");
93 dstruct(s, cparms, wChunkGranularity, "u");
94 dstruct(s, cparms, fUsingDOSMemory, "d");
95 dstruct(s, cparms, wNumVideoRequested, "u");
96 dstruct(s, cparms, fCaptureAudio, "d");
97 dstruct(s, cparms, wNumAudioRequested, "u");
98 dstruct(s, cparms, vKeyAbort, "u");
99 dstruct(s, cparms, fAbortLeftMouse, "d");
100 dstruct(s, cparms, fAbortRightMouse, "d");
101 dstruct(s, cparms, fLimitEnabled, "d");
102 dstruct(s, cparms, wTimeLimit, "u");
103 dstruct(s, cparms, fMCIControl, "d");
104 dstruct(s, cparms, fStepMCIDevice, "d");
105 dstruct(s, cparms, dwMCIStartTime, "lu");
106 dstruct(s, cparms, dwMCIStopTime, "lu");
107 dstruct(s, cparms, fStepCaptureAt2x, "d");
108 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
109 dstruct(s, cparms, dwAudioBufferSize, "lu");
110 dstruct(s, cparms, fDisableWriteCache, "d");
111 dstruct(s, cparms, AVStreamMaster, "u");
114 static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
117 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
118 dstruct(s, vhdr, lpData, "p");
119 dstruct(s, vhdr, dwBufferLength, "lu");
120 dstruct(s, vhdr, dwBytesUsed, "lu");
121 dstruct(s, vhdr, dwTimeCaptured, "lu");
122 dstruct(s, vhdr, dwUser, "lu");
123 dstruct(s, vhdr, dwFlags, "lu");
124 dstruct(s, vhdr, dwReserved[0], "lu");
125 dstruct(s, vhdr, dwReserved[1], "lu");
126 dstruct(s, vhdr, dwReserved[2], "lu");
127 dstruct(s, vhdr, dwReserved[3], "lu");
131 static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
133 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
134 dstruct(s, bih, biSize, "lu");
135 dstruct(s, bih, biWidth, "ld");
136 dstruct(s, bih, biHeight, "ld");
137 dstruct(s, bih, biPlanes, "d");
138 dstruct(s, bih, biBitCount, "d");
139 dstruct(s, bih, biCompression, "lu");
140 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
141 (char*) &bih->biCompression);
142 dstruct(s, bih, biSizeImage, "lu");
143 dstruct(s, bih, biXPelsPerMeter, "lu");
144 dstruct(s, bih, biYPelsPerMeter, "lu");
145 dstruct(s, bih, biClrUsed, "lu");
146 dstruct(s, bih, biClrImportant, "lu");
149 static int shall_we_drop(AVFormatContext *s)
151 struct vfw_ctx *ctx = s->priv_data;
152 const uint8_t dropscore[] = {62, 75, 87, 100};
153 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
154 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
156 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
157 av_log(s, AV_LOG_ERROR,
158 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
165 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
169 AVPacketList **ppktl, *pktl_next;
171 s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
174 dump_videohdr(s, vdhdr);
179 WaitForSingleObject(ctx->mutex, INFINITE);
181 pktl_next = av_mallocz(sizeof(AVPacketList));
185 if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
190 pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
191 memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
193 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
196 ctx->curbufsize += vdhdr->dwBytesUsed;
198 SetEvent(ctx->event);
199 ReleaseMutex(ctx->mutex);
203 ReleaseMutex(ctx->mutex);
207 static int vfw_read_close(AVFormatContext *s)
209 struct vfw_ctx *ctx = s->priv_data;
213 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
214 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
215 DestroyWindow(ctx->hwnd);
218 CloseHandle(ctx->mutex);
220 CloseHandle(ctx->event);
224 AVPacketList *next = pktl->next;
225 av_free_packet(&pktl->pkt);
233 static int vfw_read_header(AVFormatContext *s)
235 struct vfw_ctx *ctx = s->priv_data;
236 AVCodecContext *codec;
245 AVRational framerate_q;
247 if (!strcmp(s->filename, "list")) {
248 for (devnum = 0; devnum <= 9; devnum++) {
249 char driver_name[256];
250 char driver_ver[256];
251 ret = capGetDriverDescription(devnum,
252 driver_name, sizeof(driver_name),
253 driver_ver, sizeof(driver_ver));
255 av_log(s, AV_LOG_INFO, "Driver %d\n", devnum);
256 av_log(s, AV_LOG_INFO, " %s\n", driver_name);
257 av_log(s, AV_LOG_INFO, " %s\n", driver_ver);
263 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
265 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
269 /* If atoi fails, devnum==0 and the default device is used */
270 devnum = atoi(s->filename);
272 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
274 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
275 DestroyWindow(ctx->hwnd);
276 return AVERROR(ENODEV);
279 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
280 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
282 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
283 (LPARAM) videostream_cb);
285 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
289 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
291 st = avformat_new_stream(s, NULL);
294 return AVERROR(ENOMEM);
297 /* Set video format */
298 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
301 bi = av_malloc(bisize);
304 return AVERROR(ENOMEM);
306 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
310 dump_bih(s, &bi->bmiHeader);
313 if (ctx->video_size) {
314 ret = av_parse_video_size(&bi->bmiHeader.biWidth, &bi->bmiHeader.biHeight, ctx->video_size);
316 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
322 /* For testing yet unsupported compressions
323 * Copy these values from user-supplied verbose information */
324 bi->bmiHeader.biWidth = 320;
325 bi->bmiHeader.biHeight = 240;
326 bi->bmiHeader.biPlanes = 1;
327 bi->bmiHeader.biBitCount = 12;
328 bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
329 bi->bmiHeader.biSizeImage = 115200;
330 dump_bih(s, &bi->bmiHeader);
333 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
335 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
339 biCompression = bi->bmiHeader.biCompression;
340 biBitCount = bi->bmiHeader.biBitCount;
344 /* Set sequence setup */
345 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
350 dump_captureparms(s, &cparms);
352 cparms.fYield = 1; // Spawn a background thread
353 cparms.dwRequestMicroSecPerFrame =
354 (framerate_q.den*1000000) / framerate_q.num;
355 cparms.fAbortLeftMouse = 0;
356 cparms.fAbortRightMouse = 0;
357 cparms.fCaptureAudio = 0;
358 cparms.vKeyAbort = 0;
360 ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
366 codec->time_base = (AVRational){framerate_q.den, framerate_q.num};
367 codec->codec_type = AVMEDIA_TYPE_VIDEO;
368 codec->width = bi->bmiHeader.biWidth;
369 codec->height = bi->bmiHeader.biHeight;
370 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
371 if(codec->pix_fmt == AV_PIX_FMT_NONE) {
372 codec->codec_id = vfw_codecid(biCompression);
373 if(codec->codec_id == AV_CODEC_ID_NONE) {
374 av_log(s, AV_LOG_ERROR, "Unknown compression type. "
375 "Please report verbose (-v 9) debug information.\n");
377 return AVERROR_PATCHWELCOME;
379 codec->bits_per_coded_sample = biBitCount;
381 codec->codec_id = AV_CODEC_ID_RAWVIDEO;
382 if(biCompression == BI_RGB) {
383 codec->bits_per_coded_sample = biBitCount;
384 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
385 if (codec->extradata) {
386 codec->extradata_size = 9;
387 memcpy(codec->extradata, "BottomUp", 9);
392 avpriv_set_pts_info(st, 32, 1, 1000);
394 ctx->mutex = CreateMutex(NULL, 0, NULL);
396 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
399 ctx->event = CreateEvent(NULL, 1, 0, NULL);
401 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
405 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
407 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
421 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
423 struct vfw_ctx *ctx = s->priv_data;
424 AVPacketList *pktl = NULL;
427 WaitForSingleObject(ctx->mutex, INFINITE);
430 *pkt = ctx->pktl->pkt;
431 ctx->pktl = ctx->pktl->next;
434 ResetEvent(ctx->event);
435 ReleaseMutex(ctx->mutex);
437 if(s->flags & AVFMT_FLAG_NONBLOCK) {
438 return AVERROR(EAGAIN);
440 WaitForSingleObject(ctx->event, INFINITE);
445 ctx->curbufsize -= pkt->size;
450 #define OFFSET(x) offsetof(struct vfw_ctx, x)
451 #define DEC AV_OPT_FLAG_DECODING_PARAM
452 static const AVOption options[] = {
453 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
454 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
458 static const AVClass vfw_class = {
459 .class_name = "VFW indev",
460 .item_name = av_default_item_name,
462 .version = LIBAVUTIL_VERSION_INT,
465 AVInputFormat ff_vfwcap_demuxer = {
467 .long_name = NULL_IF_CONFIG_SMALL("VfW video capture"),
468 .priv_data_size = sizeof(struct vfw_ctx),
469 .read_header = vfw_read_header,
470 .read_packet = vfw_read_packet,
471 .read_close = vfw_read_close,
472 .flags = AVFMT_NOFILE,
473 .priv_class = &vfw_class,