2 * VFW capture interface
3 * Copyright (c) 2006-2008 Ramiro Polla
5 * This file is part of FFmpeg.
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.
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.
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
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 */
42 unsigned int curbufsize;
43 unsigned int frame_num;
46 static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
48 switch(biCompression) {
49 case MKTAG('Y', 'U', 'Y', '2'):
50 return PIX_FMT_YUYV422;
51 case MKTAG('I', '4', '2', '0'):
52 return PIX_FMT_YUV420P;
54 switch(biBitCount) { /* 1-8 are untested */
56 return PIX_FMT_MONOWHITE;
62 return PIX_FMT_RGB555;
72 static enum CodecID vfw_codecid(DWORD biCompression)
74 switch(biCompression) {
75 case MKTAG('d', 'v', 's', 'd'):
76 return CODEC_ID_DVVIDEO;
77 case MKTAG('M', 'J', 'P', 'G'):
78 case MKTAG('m', 'j', 'p', 'g'):
79 return CODEC_ID_MJPEG;
84 #define dstruct(pctx, sname, var, type) \
85 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
87 static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
89 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
90 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
91 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
92 dstruct(s, cparms, wPercentDropForError, "u");
93 dstruct(s, cparms, fYield, "d");
94 dstruct(s, cparms, dwIndexSize, "lu");
95 dstruct(s, cparms, wChunkGranularity, "u");
96 dstruct(s, cparms, fUsingDOSMemory, "d");
97 dstruct(s, cparms, wNumVideoRequested, "u");
98 dstruct(s, cparms, fCaptureAudio, "d");
99 dstruct(s, cparms, wNumAudioRequested, "u");
100 dstruct(s, cparms, vKeyAbort, "u");
101 dstruct(s, cparms, fAbortLeftMouse, "d");
102 dstruct(s, cparms, fAbortRightMouse, "d");
103 dstruct(s, cparms, fLimitEnabled, "d");
104 dstruct(s, cparms, wTimeLimit, "u");
105 dstruct(s, cparms, fMCIControl, "d");
106 dstruct(s, cparms, fStepMCIDevice, "d");
107 dstruct(s, cparms, dwMCIStartTime, "lu");
108 dstruct(s, cparms, dwMCIStopTime, "lu");
109 dstruct(s, cparms, fStepCaptureAt2x, "d");
110 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
111 dstruct(s, cparms, dwAudioBufferSize, "lu");
112 dstruct(s, cparms, fDisableWriteCache, "d");
113 dstruct(s, cparms, AVStreamMaster, "u");
116 static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
119 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
120 dstruct(s, vhdr, lpData, "p");
121 dstruct(s, vhdr, dwBufferLength, "lu");
122 dstruct(s, vhdr, dwBytesUsed, "lu");
123 dstruct(s, vhdr, dwTimeCaptured, "lu");
124 dstruct(s, vhdr, dwUser, "lu");
125 dstruct(s, vhdr, dwFlags, "lu");
126 dstruct(s, vhdr, dwReserved[0], "lu");
127 dstruct(s, vhdr, dwReserved[1], "lu");
128 dstruct(s, vhdr, dwReserved[2], "lu");
129 dstruct(s, vhdr, dwReserved[3], "lu");
133 static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
135 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
136 dstruct(s, bih, biSize, "lu");
137 dstruct(s, bih, biWidth, "ld");
138 dstruct(s, bih, biHeight, "ld");
139 dstruct(s, bih, biPlanes, "d");
140 dstruct(s, bih, biBitCount, "d");
141 dstruct(s, bih, biCompression, "lu");
142 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
143 (char*) &bih->biCompression);
144 dstruct(s, bih, biSizeImage, "lu");
145 dstruct(s, bih, biXPelsPerMeter, "lu");
146 dstruct(s, bih, biYPelsPerMeter, "lu");
147 dstruct(s, bih, biClrUsed, "lu");
148 dstruct(s, bih, biClrImportant, "lu");
151 static int shall_we_drop(struct vfw_ctx *ctx)
153 AVFormatContext *s = ctx->s;
154 const uint8_t dropscore[] = {62, 75, 87, 100};
155 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
156 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
158 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
159 av_log(ctx->s, AV_LOG_ERROR,
160 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
167 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
170 AVPacketList **ppktl, *pktl_next;
172 ctx = (struct vfw_ctx *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
174 dump_videohdr(ctx->s, vdhdr);
176 if(shall_we_drop(ctx))
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;
212 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
213 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
214 DestroyWindow(ctx->hwnd);
217 CloseHandle(ctx->mutex);
219 CloseHandle(ctx->event);
224 static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
226 struct vfw_ctx *ctx = s->priv_data;
227 AVCodecContext *codec;
239 if(!ap->time_base.den) {
240 av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
246 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
248 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
252 /* If atoi fails, devnum==0 and the default device is used */
253 devnum = atoi(s->filename);
255 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
257 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
258 DestroyWindow(ctx->hwnd);
259 return AVERROR(ENODEV);
262 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
263 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
265 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
266 (LPARAM) videostream_cb);
268 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
272 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) ctx);
274 st = av_new_stream(s, 0);
277 return AVERROR_NOMEM;
280 /* Set video format */
281 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
284 bi = av_malloc(bisize);
287 return AVERROR_NOMEM;
289 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
293 dump_bih(s, &bi->bmiHeader);
295 width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
296 height = ap->height ? ap->height : bi->bmiHeader.biHeight;
297 bi->bmiHeader.biWidth = width ;
298 bi->bmiHeader.biHeight = height;
301 /* For testing yet unsupported compressions
302 * Copy these values from user-supplied verbose information */
303 bi->bmiHeader.biWidth = 320;
304 bi->bmiHeader.biHeight = 240;
305 bi->bmiHeader.biPlanes = 1;
306 bi->bmiHeader.biBitCount = 12;
307 bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
308 bi->bmiHeader.biSizeImage = 115200;
309 dump_bih(s, &bi->bmiHeader);
312 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
314 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
318 biCompression = bi->bmiHeader.biCompression;
319 biBitCount = bi->bmiHeader.biBitCount;
323 /* Set sequence setup */
324 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
329 dump_captureparms(s, &cparms);
331 cparms.fYield = 1; // Spawn a background thread
332 cparms.dwRequestMicroSecPerFrame =
333 (ap->time_base.num*1000000) / ap->time_base.den;
334 cparms.fAbortLeftMouse = 0;
335 cparms.fAbortRightMouse = 0;
336 cparms.fCaptureAudio = 0;
337 cparms.vKeyAbort = 0;
339 ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
345 codec->time_base = ap->time_base;
346 codec->codec_type = CODEC_TYPE_VIDEO;
347 codec->width = width;
348 codec->height = height;
349 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
350 if(codec->pix_fmt == PIX_FMT_NONE) {
351 codec->codec_id = vfw_codecid(biCompression);
352 if(codec->codec_id == CODEC_ID_NONE) {
353 av_log(s, AV_LOG_ERROR, "Unknown compression type. "
354 "Please report verbose (-v 9) debug information.\n");
356 return AVERROR_PATCHWELCOME;
358 codec->bits_per_coded_sample = biBitCount;
360 codec->codec_id = CODEC_ID_RAWVIDEO;
361 if(biCompression == BI_RGB)
362 codec->bits_per_coded_sample = biBitCount;
365 av_set_pts_info(st, 32, 1, 1000);
367 ctx->mutex = CreateMutex(NULL, 0, NULL);
369 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
372 ctx->event = CreateEvent(NULL, 1, 0, NULL);
374 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
378 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
380 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
394 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
396 struct vfw_ctx *ctx = s->priv_data;
397 AVPacketList *pktl = NULL;
400 WaitForSingleObject(ctx->mutex, INFINITE);
403 *pkt = ctx->pktl->pkt;
404 ctx->pktl = ctx->pktl->next;
407 ResetEvent(ctx->event);
408 ReleaseMutex(ctx->mutex);
410 if(s->flags & AVFMT_FLAG_NONBLOCK) {
411 return AVERROR(EAGAIN);
413 WaitForSingleObject(ctx->event, INFINITE);
418 ctx->curbufsize -= pkt->size;
423 AVInputFormat vfwcap_demuxer = {
425 NULL_IF_CONFIG_SMALL("VFW video capture"),
426 sizeof(struct vfw_ctx),
431 .flags = AVFMT_NOFILE,