Consistency: make the output buffer the first argument of functions
[FFMpeg-mirror/DVCPRO-HD.git] / libavdevice / vfwcap.c
blobbe6003a4228e9c91739afcc370096d20598a6e60
1 /*
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"
23 #include <vfw.h>
24 #include <windows.h>
26 //#define DEBUG_VFW
28 /* Defines for VFW missing from MinGW.
29 * Remove this when MinGW incorporates them. */
30 #define WM_CAP_START (0x0400)
31 #define WM_CAP_SET_CALLBACK_VIDEOSTREAM (WM_CAP_START + 6)
32 #define WM_CAP_DRIVER_CONNECT (WM_CAP_START + 10)
33 #define WM_CAP_DRIVER_DISCONNECT (WM_CAP_START + 11)
34 #define WM_CAP_GET_VIDEOFORMAT (WM_CAP_START + 44)
35 #define WM_CAP_SET_VIDEOFORMAT (WM_CAP_START + 45)
36 #define WM_CAP_SET_PREVIEW (WM_CAP_START + 50)
37 #define WM_CAP_SET_OVERLAY (WM_CAP_START + 51)
38 #define WM_CAP_SEQUENCE_NOFILE (WM_CAP_START + 63)
39 #define WM_CAP_SET_SEQUENCE_SETUP (WM_CAP_START + 64)
40 #define WM_CAP_GET_SEQUENCE_SETUP (WM_CAP_START + 65)
42 #define HWND_MESSAGE ((HWND)-3)
44 #define BI_RGB 0
46 typedef struct videohdr_tag {
47 LPBYTE lpData;
48 DWORD dwBufferLength;
49 DWORD dwBytesUsed;
50 DWORD dwTimeCaptured;
51 DWORD dwUser;
52 DWORD dwFlags;
53 DWORD_PTR dwReserved[4];
54 } VIDEOHDR, NEAR *PVIDEOHDR, FAR * LPVIDEOHDR;
56 typedef struct {
57 DWORD dwRequestMicroSecPerFrame;
58 BOOL fMakeUserHitOKToCapture;
59 UINT wPercentDropForError;
60 BOOL fYield;
61 DWORD dwIndexSize;
62 UINT wChunkGranularity;
63 BOOL fUsingDOSMemory;
64 UINT wNumVideoRequested;
65 BOOL fCaptureAudio;
66 UINT wNumAudioRequested;
67 UINT vKeyAbort;
68 BOOL fAbortLeftMouse;
69 BOOL fAbortRightMouse;
70 BOOL fLimitEnabled;
71 UINT wTimeLimit;
72 BOOL fMCIControl;
73 BOOL fStepMCIDevice;
74 DWORD dwMCIStartTime;
75 DWORD dwMCIStopTime;
76 BOOL fStepCaptureAt2x;
77 UINT wStepCaptureAverageFrames;
78 DWORD dwAudioBufferSize;
79 BOOL fDisableWriteCache;
80 UINT AVStreamMaster;
81 } CAPTUREPARMS;
82 /* End of missing MinGW defines */
84 struct vfw_ctx {
85 HWND hwnd;
86 HANDLE mutex;
87 HANDLE event;
88 AVPacketList *pktl;
89 AVFormatContext *s;
90 unsigned int curbufsize;
91 unsigned int frame_num;
94 static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
96 switch(biCompression) {
97 case MKTAG('Y', 'U', 'Y', '2'):
98 return PIX_FMT_YUYV422;
99 case MKTAG('I', '4', '2', '0'):
100 return PIX_FMT_YUV420P;
101 case BI_RGB:
102 switch(biBitCount) { /* 1-8 are untested */
103 case 1:
104 return PIX_FMT_MONOWHITE;
105 case 4:
106 return PIX_FMT_RGB4;
107 case 8:
108 return PIX_FMT_RGB8;
109 case 16:
110 return PIX_FMT_RGB555;
111 case 24:
112 return PIX_FMT_BGR24;
113 case 32:
114 return PIX_FMT_RGB32;
117 return -1;
120 #define dstruct(pctx, sname, var, type) \
121 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
123 static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
125 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
126 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
127 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
128 dstruct(s, cparms, wPercentDropForError, "u");
129 dstruct(s, cparms, fYield, "d");
130 dstruct(s, cparms, dwIndexSize, "lu");
131 dstruct(s, cparms, wChunkGranularity, "u");
132 dstruct(s, cparms, fUsingDOSMemory, "d");
133 dstruct(s, cparms, wNumVideoRequested, "u");
134 dstruct(s, cparms, fCaptureAudio, "d");
135 dstruct(s, cparms, wNumAudioRequested, "u");
136 dstruct(s, cparms, vKeyAbort, "u");
137 dstruct(s, cparms, fAbortLeftMouse, "d");
138 dstruct(s, cparms, fAbortRightMouse, "d");
139 dstruct(s, cparms, fLimitEnabled, "d");
140 dstruct(s, cparms, wTimeLimit, "u");
141 dstruct(s, cparms, fMCIControl, "d");
142 dstruct(s, cparms, fStepMCIDevice, "d");
143 dstruct(s, cparms, dwMCIStartTime, "lu");
144 dstruct(s, cparms, dwMCIStopTime, "lu");
145 dstruct(s, cparms, fStepCaptureAt2x, "d");
146 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
147 dstruct(s, cparms, dwAudioBufferSize, "lu");
148 dstruct(s, cparms, fDisableWriteCache, "d");
149 dstruct(s, cparms, AVStreamMaster, "u");
152 static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
154 #ifdef DEBUG_VFW
155 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
156 dstruct(s, vhdr, lpData, "p");
157 dstruct(s, vhdr, dwBufferLength, "lu");
158 dstruct(s, vhdr, dwBytesUsed, "lu");
159 dstruct(s, vhdr, dwTimeCaptured, "lu");
160 dstruct(s, vhdr, dwUser, "lu");
161 dstruct(s, vhdr, dwFlags, "lu");
162 dstruct(s, vhdr, dwReserved[0], "lu");
163 dstruct(s, vhdr, dwReserved[1], "lu");
164 dstruct(s, vhdr, dwReserved[2], "lu");
165 dstruct(s, vhdr, dwReserved[3], "lu");
166 #endif
169 static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
171 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
172 dstruct(s, bih, biSize, "lu");
173 dstruct(s, bih, biWidth, "ld");
174 dstruct(s, bih, biHeight, "ld");
175 dstruct(s, bih, biPlanes, "d");
176 dstruct(s, bih, biBitCount, "d");
177 dstruct(s, bih, biCompression, "lu");
178 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
179 (char*) &bih->biCompression);
180 dstruct(s, bih, biSizeImage, "lu");
181 dstruct(s, bih, biXPelsPerMeter, "lu");
182 dstruct(s, bih, biYPelsPerMeter, "lu");
183 dstruct(s, bih, biClrUsed, "lu");
184 dstruct(s, bih, biClrImportant, "lu");
187 static int shall_we_drop(struct vfw_ctx *ctx)
189 AVFormatContext *s = ctx->s;
190 const uint8_t dropscore[] = {62, 75, 87, 100};
191 const int ndropscores = sizeof(dropscore)/sizeof(dropscore[0]);
192 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
194 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
195 av_log(ctx->s, AV_LOG_ERROR,
196 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
197 return 1;
200 return 0;
203 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
205 struct vfw_ctx *ctx;
206 AVPacketList **ppktl, *pktl_next;
208 ctx = (struct vfw_ctx *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
210 dump_videohdr(ctx->s, vdhdr);
212 if(shall_we_drop(ctx))
213 return FALSE;
215 WaitForSingleObject(ctx->mutex, INFINITE);
217 pktl_next = av_mallocz(sizeof(AVPacketList));
218 if(!pktl_next)
219 goto fail;
221 if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
222 av_free(pktl_next);
223 goto fail;
226 pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
227 memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
229 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
230 *ppktl = pktl_next;
232 ctx->curbufsize += vdhdr->dwBytesUsed;
234 SetEvent(ctx->event);
235 ReleaseMutex(ctx->mutex);
237 return TRUE;
238 fail:
239 ReleaseMutex(ctx->mutex);
240 return FALSE;
243 static int vfw_read_close(AVFormatContext *s);
245 static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
247 struct vfw_ctx *ctx = s->priv_data;
248 AVCodecContext *codec;
249 AVStream *st;
250 int devnum;
251 int bisize;
252 BITMAPINFO *bi;
253 CAPTUREPARMS cparms;
254 DWORD biCompression;
255 WORD biBitCount;
256 int width;
257 int height;
258 int ret;
260 if(!ap->time_base.den) {
261 av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
262 return AVERROR_IO;
265 ctx->s = s;
267 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
268 if(!ctx->hwnd) {
269 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
270 return AVERROR_IO;
273 /* If atoi fails, devnum==0 and the default device is used */
274 devnum = atoi(s->filename);
276 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
277 if(!ret) {
278 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
279 DestroyWindow(ctx->hwnd);
280 return AVERROR(ENODEV);
283 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
284 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
286 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
287 (LPARAM) videostream_cb);
288 if(!ret) {
289 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
290 goto fail_io;
293 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) ctx);
295 st = av_new_stream(s, 0);
296 if(!st) {
297 vfw_read_close(s);
298 return AVERROR_NOMEM;
301 /* Set video format */
302 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
303 if(!bisize)
304 goto fail_io;
305 bi = av_malloc(bisize);
306 if(!bi) {
307 vfw_read_close(s);
308 return AVERROR_NOMEM;
310 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
311 if(!ret)
312 goto fail_bi;
314 dump_bih(s, &bi->bmiHeader);
316 width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
317 height = ap->height ? ap->height : bi->bmiHeader.biHeight;
318 bi->bmiHeader.biWidth = width ;
319 bi->bmiHeader.biHeight = height;
321 #if 0
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);
331 #endif
333 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
334 if(!ret) {
335 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
336 goto fail_bi;
339 biCompression = bi->bmiHeader.biCompression;
340 biBitCount = bi->bmiHeader.biBitCount;
342 av_free(bi);
344 /* Set sequence setup */
345 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
346 (LPARAM) &cparms);
347 if(!ret)
348 goto fail_io;
350 dump_captureparms(s, &cparms);
352 cparms.fYield = 1; // Spawn a background thread
353 cparms.dwRequestMicroSecPerFrame =
354 (ap->time_base.num*1000000) / ap->time_base.den;
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),
361 (LPARAM) &cparms);
362 if(!ret)
363 goto fail_io;
365 codec = st->codec;
366 codec->time_base = ap->time_base;
367 codec->codec_type = CODEC_TYPE_VIDEO;
368 codec->width = width;
369 codec->height = height;
370 codec->codec_id = CODEC_ID_RAWVIDEO;
371 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
372 if(biCompression == BI_RGB)
373 codec->bits_per_sample = biBitCount;
375 av_set_pts_info(st, 32, 1, 1000);
377 if(codec->pix_fmt == -1) {
378 av_log(s, AV_LOG_ERROR, "Unknown compression type."
379 "Please report verbose (-v 99) debug information.\n");
380 vfw_read_close(s);
381 return AVERROR_PATCHWELCOME;
384 ctx->mutex = CreateMutex(NULL, 0, NULL);
385 if(!ctx->mutex) {
386 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
387 goto fail_io;
389 ctx->event = CreateEvent(NULL, 1, 0, NULL);
390 if(!ctx->event) {
391 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
392 goto fail_io;
395 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
396 if(!ret) {
397 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
398 goto fail_io;
401 return 0;
403 fail_bi:
404 av_free(bi);
406 fail_io:
407 vfw_read_close(s);
408 return AVERROR_IO;
411 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
413 struct vfw_ctx *ctx = s->priv_data;
414 AVPacketList *pktl = NULL;
416 while(!pktl) {
417 WaitForSingleObject(ctx->mutex, INFINITE);
418 pktl = ctx->pktl;
419 if(ctx->pktl) {
420 *pkt = ctx->pktl->pkt;
421 ctx->pktl = ctx->pktl->next;
422 av_free(pktl);
424 ResetEvent(ctx->event);
425 ReleaseMutex(ctx->mutex);
426 if(!pktl) {
427 if(s->flags & AVFMT_FLAG_NONBLOCK) {
428 return AVERROR(EAGAIN);
429 } else {
430 WaitForSingleObject(ctx->event, INFINITE);
435 ctx->curbufsize -= pkt->size;
437 return pkt->size;
440 static int vfw_read_close(AVFormatContext *s)
442 struct vfw_ctx *ctx = s->priv_data;
444 if(ctx->hwnd) {
445 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
446 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
447 DestroyWindow(ctx->hwnd);
449 if(ctx->mutex)
450 CloseHandle(ctx->mutex);
451 if(ctx->event)
452 CloseHandle(ctx->event);
454 return 0;
457 AVInputFormat vfwcap_demuxer = {
458 "vfwcap",
459 NULL_IF_CONFIG_SMALL("VFW video capture"),
460 sizeof(struct vfw_ctx),
461 NULL,
462 vfw_read_header,
463 vfw_read_packet,
464 vfw_read_close,
465 .flags = AVFMT_NOFILE,