Cosmetics: remove {}s over one-lined ifs.
[libav.git] / libavdevice / vfwcap.c
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
22 #include "avformat.h"
23 #include <vfw.h>
24 #include <windows.h>
25
26 //#define DEBUG_VFW
27
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)
41
42 #define HWND_MESSAGE ((HWND)-3)
43
44 #define BI_RGB 0
45
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;
55
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 */
83
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;
92 };
93
94 static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
95 {
96 switch(biCompression) {
97 case MKTAG('Y', 'U', 'Y', '2'):
98 return PIX_FMT_YUYV422;
99 case BI_RGB:
100 switch(biBitCount) { /* 1-8 are untested */
101 case 1:
102 return PIX_FMT_MONOWHITE;
103 case 4:
104 return PIX_FMT_RGB4;
105 case 8:
106 return PIX_FMT_RGB8;
107 case 16:
108 return PIX_FMT_RGB555;
109 case 24:
110 return PIX_FMT_BGR24;
111 case 32:
112 return PIX_FMT_RGB32;
113 }
114 }
115 return -1;
116 }
117
118 #define dstruct(pctx, sname, var, type) \
119 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
120
121 static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
122 {
123 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
124 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
125 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
126 dstruct(s, cparms, wPercentDropForError, "u");
127 dstruct(s, cparms, fYield, "d");
128 dstruct(s, cparms, dwIndexSize, "lu");
129 dstruct(s, cparms, wChunkGranularity, "u");
130 dstruct(s, cparms, fUsingDOSMemory, "d");
131 dstruct(s, cparms, wNumVideoRequested, "u");
132 dstruct(s, cparms, fCaptureAudio, "d");
133 dstruct(s, cparms, wNumAudioRequested, "u");
134 dstruct(s, cparms, vKeyAbort, "u");
135 dstruct(s, cparms, fAbortLeftMouse, "d");
136 dstruct(s, cparms, fAbortRightMouse, "d");
137 dstruct(s, cparms, fLimitEnabled, "d");
138 dstruct(s, cparms, wTimeLimit, "u");
139 dstruct(s, cparms, fMCIControl, "d");
140 dstruct(s, cparms, fStepMCIDevice, "d");
141 dstruct(s, cparms, dwMCIStartTime, "lu");
142 dstruct(s, cparms, dwMCIStopTime, "lu");
143 dstruct(s, cparms, fStepCaptureAt2x, "d");
144 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
145 dstruct(s, cparms, dwAudioBufferSize, "lu");
146 dstruct(s, cparms, fDisableWriteCache, "d");
147 dstruct(s, cparms, AVStreamMaster, "u");
148 }
149
150 static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
151 {
152 #ifdef DEBUG_VFW
153 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
154 dstruct(s, vhdr, lpData, "p");
155 dstruct(s, vhdr, dwBufferLength, "lu");
156 dstruct(s, vhdr, dwBytesUsed, "lu");
157 dstruct(s, vhdr, dwTimeCaptured, "lu");
158 dstruct(s, vhdr, dwUser, "lu");
159 dstruct(s, vhdr, dwFlags, "lu");
160 dstruct(s, vhdr, dwReserved[0], "lu");
161 dstruct(s, vhdr, dwReserved[1], "lu");
162 dstruct(s, vhdr, dwReserved[2], "lu");
163 dstruct(s, vhdr, dwReserved[3], "lu");
164 #endif
165 }
166
167 static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
168 {
169 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
170 dstruct(s, bih, biSize, "lu");
171 dstruct(s, bih, biWidth, "ld");
172 dstruct(s, bih, biHeight, "ld");
173 dstruct(s, bih, biPlanes, "d");
174 dstruct(s, bih, biBitCount, "d");
175 dstruct(s, bih, biCompression, "lu");
176 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
177 (char*) &bih->biCompression);
178 dstruct(s, bih, biSizeImage, "lu");
179 dstruct(s, bih, biXPelsPerMeter, "lu");
180 dstruct(s, bih, biYPelsPerMeter, "lu");
181 dstruct(s, bih, biClrUsed, "lu");
182 dstruct(s, bih, biClrImportant, "lu");
183 }
184
185 static int shall_we_drop(struct vfw_ctx *ctx)
186 {
187 AVFormatContext *s = ctx->s;
188 const uint8_t dropscore[] = {62, 75, 87, 100};
189 const int ndropscores = sizeof(dropscore)/sizeof(dropscore[0]);
190 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
191
192 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
193 av_log(ctx->s, AV_LOG_ERROR,
194 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
195 return 1;
196 }
197
198 return 0;
199 }
200
201 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
202 {
203 struct vfw_ctx *ctx;
204 AVPacketList **ppktl, *pktl_next;
205
206 ctx = (struct vfw_ctx *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
207
208 dump_videohdr(ctx->s, vdhdr);
209
210 if(shall_we_drop(ctx))
211 return FALSE;
212
213 WaitForSingleObject(ctx->mutex, INFINITE);
214
215 pktl_next = av_mallocz(sizeof(AVPacketList));
216 if(!pktl_next)
217 goto fail;
218
219 if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
220 av_free(pktl_next);
221 goto fail;
222 }
223
224 pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
225 memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
226
227 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
228 *ppktl = pktl_next;
229
230 ctx->curbufsize += vdhdr->dwBytesUsed;
231
232 SetEvent(ctx->event);
233 ReleaseMutex(ctx->mutex);
234
235 return TRUE;
236 fail:
237 ReleaseMutex(ctx->mutex);
238 return FALSE;
239 }
240
241 static int vfw_read_close(AVFormatContext *s);
242
243 static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
244 {
245 struct vfw_ctx *ctx = s->priv_data;
246 AVCodecContext *codec;
247 AVStream *st;
248 int devnum;
249 int bisize;
250 BITMAPINFO *bi;
251 CAPTUREPARMS cparms;
252 DWORD biCompression;
253 WORD biBitCount;
254 int width;
255 int height;
256 int ret;
257
258 if(!ap->time_base.den) {
259 av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
260 return AVERROR_IO;
261 }
262
263 ctx->s = s;
264
265 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
266 if(!ctx->hwnd) {
267 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
268 return AVERROR_IO;
269 }
270
271 /* If atoi fails, devnum==0 and the default device is used */
272 devnum = atoi(s->filename);
273
274 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
275 if(!ret) {
276 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
277 DestroyWindow(ctx->hwnd);
278 return AVERROR(ENODEV);
279 }
280
281 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
282 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
283
284 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
285 (LPARAM) videostream_cb);
286 if(!ret) {
287 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
288 goto fail_io;
289 }
290
291 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) ctx);
292
293 st = av_new_stream(s, 0);
294 if(!st) {
295 vfw_read_close(s);
296 return AVERROR_NOMEM;
297 }
298
299 /* Set video format */
300 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
301 if(!bisize)
302 goto fail_io;
303 bi = av_malloc(bisize);
304 if(!bi) {
305 vfw_read_close(s);
306 return AVERROR_NOMEM;
307 }
308 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
309 if(!ret)
310 goto fail_bi;
311
312 dump_bih(s, &bi->bmiHeader);
313
314 width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
315 height = ap->height ? ap->height : bi->bmiHeader.biHeight;
316 bi->bmiHeader.biWidth = width ;
317 bi->bmiHeader.biHeight = height;
318
319 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
320 if(!ret) {
321 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
322 goto fail_bi;
323 }
324
325 biCompression = bi->bmiHeader.biCompression;
326 biBitCount = bi->bmiHeader.biBitCount;
327
328 av_free(bi);
329
330 /* Set sequence setup */
331 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
332 (LPARAM) &cparms);
333 if(!ret)
334 goto fail_io;
335
336 dump_captureparms(s, &cparms);
337
338 cparms.fYield = 1; // Spawn a background thread
339 cparms.dwRequestMicroSecPerFrame =
340 (ap->time_base.num*1000000) / ap->time_base.den;
341 cparms.fAbortLeftMouse = 0;
342 cparms.fAbortRightMouse = 0;
343 cparms.fCaptureAudio = 0;
344 cparms.vKeyAbort = 0;
345
346 ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
347 (LPARAM) &cparms);
348 if(!ret)
349 goto fail_io;
350
351 codec = st->codec;
352 codec->time_base = ap->time_base;
353 codec->codec_type = CODEC_TYPE_VIDEO;
354 codec->width = width;
355 codec->height = height;
356 codec->codec_id = CODEC_ID_RAWVIDEO;
357 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
358 if(biCompression == BI_RGB)
359 codec->bits_per_sample = biBitCount;
360
361 av_set_pts_info(st, 32, 1, 1000);
362
363 if(codec->pix_fmt == -1) {
364 av_log(s, AV_LOG_ERROR, "Unknown compression type."
365 "Please report verbose (-v 99) debug information.\n");
366 vfw_read_close(s);
367 return AVERROR_PATCHWELCOME;
368 }
369
370 ctx->mutex = CreateMutex(NULL, 0, NULL);
371 if(!ctx->mutex) {
372 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
373 goto fail_io;
374 }
375 ctx->event = CreateEvent(NULL, 1, 0, NULL);
376 if(!ctx->event) {
377 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
378 goto fail_io;
379 }
380
381 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
382 if(!ret) {
383 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
384 goto fail_io;
385 }
386
387 return 0;
388
389 fail_bi:
390 av_free(bi);
391
392 fail_io:
393 vfw_read_close(s);
394 return AVERROR_IO;
395 }
396
397 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
398 {
399 struct vfw_ctx *ctx = s->priv_data;
400 AVPacketList *pktl = NULL;
401
402 while(!pktl) {
403 WaitForSingleObject(ctx->mutex, INFINITE);
404 pktl = ctx->pktl;
405 if(ctx->pktl) {
406 *pkt = ctx->pktl->pkt;
407 ctx->pktl = ctx->pktl->next;
408 av_free(pktl);
409 }
410 ResetEvent(ctx->event);
411 ReleaseMutex(ctx->mutex);
412 if(!pktl) {
413 if(s->flags & AVFMT_FLAG_NONBLOCK) {
414 return AVERROR(EAGAIN);
415 } else {
416 WaitForSingleObject(ctx->event, INFINITE);
417 }
418 }
419 }
420
421 ctx->curbufsize -= pkt->size;
422
423 return pkt->size;
424 }
425
426 static int vfw_read_close(AVFormatContext *s)
427 {
428 struct vfw_ctx *ctx = s->priv_data;
429
430 if(ctx->hwnd) {
431 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
432 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
433 DestroyWindow(ctx->hwnd);
434 }
435 if(ctx->mutex)
436 CloseHandle(ctx->mutex);
437 if(ctx->event)
438 CloseHandle(ctx->event);
439
440 return 0;
441 }
442
443 AVInputFormat vfwcap_demuxer = {
444 "vfwcap",
445 "VFW video capture",
446 sizeof(struct vfw_ctx),
447 NULL,
448 vfw_read_header,
449 vfw_read_packet,
450 vfw_read_close,
451 .flags = AVFMT_NOFILE,
452 };