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