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('U', 'Y', 'V', 'Y'):
50 return PIX_FMT_UYVY422
;
51 case MKTAG('Y', 'U', 'Y', '2'):
52 return PIX_FMT_YUYV422
;
53 case MKTAG('I', '4', '2', '0'):
54 return PIX_FMT_YUV420P
;
56 switch(biBitCount
) { /* 1-8 are untested */
58 return PIX_FMT_MONOWHITE
;
64 return PIX_FMT_RGB555
;
74 static enum CodecID
vfw_codecid(DWORD biCompression
)
76 switch(biCompression
) {
77 case MKTAG('d', 'v', 's', 'd'):
78 return CODEC_ID_DVVIDEO
;
79 case MKTAG('M', 'J', 'P', 'G'):
80 case MKTAG('m', 'j', 'p', 'g'):
81 return CODEC_ID_MJPEG
;
86 #define dstruct(pctx, sname, var, type) \
87 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
89 static void dump_captureparms(AVFormatContext
*s
, CAPTUREPARMS
*cparms
)
91 av_log(s
, AV_LOG_DEBUG
, "CAPTUREPARMS\n");
92 dstruct(s
, cparms
, dwRequestMicroSecPerFrame
, "lu");
93 dstruct(s
, cparms
, fMakeUserHitOKToCapture
, "d");
94 dstruct(s
, cparms
, wPercentDropForError
, "u");
95 dstruct(s
, cparms
, fYield
, "d");
96 dstruct(s
, cparms
, dwIndexSize
, "lu");
97 dstruct(s
, cparms
, wChunkGranularity
, "u");
98 dstruct(s
, cparms
, fUsingDOSMemory
, "d");
99 dstruct(s
, cparms
, wNumVideoRequested
, "u");
100 dstruct(s
, cparms
, fCaptureAudio
, "d");
101 dstruct(s
, cparms
, wNumAudioRequested
, "u");
102 dstruct(s
, cparms
, vKeyAbort
, "u");
103 dstruct(s
, cparms
, fAbortLeftMouse
, "d");
104 dstruct(s
, cparms
, fAbortRightMouse
, "d");
105 dstruct(s
, cparms
, fLimitEnabled
, "d");
106 dstruct(s
, cparms
, wTimeLimit
, "u");
107 dstruct(s
, cparms
, fMCIControl
, "d");
108 dstruct(s
, cparms
, fStepMCIDevice
, "d");
109 dstruct(s
, cparms
, dwMCIStartTime
, "lu");
110 dstruct(s
, cparms
, dwMCIStopTime
, "lu");
111 dstruct(s
, cparms
, fStepCaptureAt2x
, "d");
112 dstruct(s
, cparms
, wStepCaptureAverageFrames
, "u");
113 dstruct(s
, cparms
, dwAudioBufferSize
, "lu");
114 dstruct(s
, cparms
, fDisableWriteCache
, "d");
115 dstruct(s
, cparms
, AVStreamMaster
, "u");
118 static void dump_videohdr(AVFormatContext
*s
, VIDEOHDR
*vhdr
)
121 av_log(s
, AV_LOG_DEBUG
, "VIDEOHDR\n");
122 dstruct(s
, vhdr
, lpData
, "p");
123 dstruct(s
, vhdr
, dwBufferLength
, "lu");
124 dstruct(s
, vhdr
, dwBytesUsed
, "lu");
125 dstruct(s
, vhdr
, dwTimeCaptured
, "lu");
126 dstruct(s
, vhdr
, dwUser
, "lu");
127 dstruct(s
, vhdr
, dwFlags
, "lu");
128 dstruct(s
, vhdr
, dwReserved
[0], "lu");
129 dstruct(s
, vhdr
, dwReserved
[1], "lu");
130 dstruct(s
, vhdr
, dwReserved
[2], "lu");
131 dstruct(s
, vhdr
, dwReserved
[3], "lu");
135 static void dump_bih(AVFormatContext
*s
, BITMAPINFOHEADER
*bih
)
137 av_log(s
, AV_LOG_DEBUG
, "BITMAPINFOHEADER\n");
138 dstruct(s
, bih
, biSize
, "lu");
139 dstruct(s
, bih
, biWidth
, "ld");
140 dstruct(s
, bih
, biHeight
, "ld");
141 dstruct(s
, bih
, biPlanes
, "d");
142 dstruct(s
, bih
, biBitCount
, "d");
143 dstruct(s
, bih
, biCompression
, "lu");
144 av_log(s
, AV_LOG_DEBUG
, " biCompression:\t\"%.4s\"\n",
145 (char*) &bih
->biCompression
);
146 dstruct(s
, bih
, biSizeImage
, "lu");
147 dstruct(s
, bih
, biXPelsPerMeter
, "lu");
148 dstruct(s
, bih
, biYPelsPerMeter
, "lu");
149 dstruct(s
, bih
, biClrUsed
, "lu");
150 dstruct(s
, bih
, biClrImportant
, "lu");
153 static int shall_we_drop(struct vfw_ctx
*ctx
)
155 AVFormatContext
*s
= ctx
->s
;
156 const uint8_t dropscore
[] = {62, 75, 87, 100};
157 const int ndropscores
= FF_ARRAY_ELEMS(dropscore
);
158 unsigned int buffer_fullness
= (ctx
->curbufsize
*100)/s
->max_picture_buffer
;
160 if(dropscore
[++ctx
->frame_num
%ndropscores
] <= buffer_fullness
) {
161 av_log(ctx
->s
, AV_LOG_ERROR
,
162 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness
);
169 static LRESULT CALLBACK
videostream_cb(HWND hwnd
, LPVIDEOHDR vdhdr
)
172 AVPacketList
**ppktl
, *pktl_next
;
174 ctx
= (struct vfw_ctx
*) GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
176 dump_videohdr(ctx
->s
, vdhdr
);
178 if(shall_we_drop(ctx
))
181 WaitForSingleObject(ctx
->mutex
, INFINITE
);
183 pktl_next
= av_mallocz(sizeof(AVPacketList
));
187 if(av_new_packet(&pktl_next
->pkt
, vdhdr
->dwBytesUsed
) < 0) {
192 pktl_next
->pkt
.pts
= vdhdr
->dwTimeCaptured
;
193 memcpy(pktl_next
->pkt
.data
, vdhdr
->lpData
, vdhdr
->dwBytesUsed
);
195 for(ppktl
= &ctx
->pktl
; *ppktl
; ppktl
= &(*ppktl
)->next
);
198 ctx
->curbufsize
+= vdhdr
->dwBytesUsed
;
200 SetEvent(ctx
->event
);
201 ReleaseMutex(ctx
->mutex
);
205 ReleaseMutex(ctx
->mutex
);
209 static int vfw_read_close(AVFormatContext
*s
)
211 struct vfw_ctx
*ctx
= s
->priv_data
;
214 SendMessage(ctx
->hwnd
, WM_CAP_SET_CALLBACK_VIDEOSTREAM
, 0, 0);
215 SendMessage(ctx
->hwnd
, WM_CAP_DRIVER_DISCONNECT
, 0, 0);
216 DestroyWindow(ctx
->hwnd
);
219 CloseHandle(ctx
->mutex
);
221 CloseHandle(ctx
->event
);
226 static int vfw_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
228 struct vfw_ctx
*ctx
= s
->priv_data
;
229 AVCodecContext
*codec
;
241 if(!ap
->time_base
.den
) {
242 av_log(s
, AV_LOG_ERROR
, "A time base must be specified.\n");
248 ctx
->hwnd
= capCreateCaptureWindow(NULL
, 0, 0, 0, 0, 0, HWND_MESSAGE
, 0);
250 av_log(s
, AV_LOG_ERROR
, "Could not create capture window.\n");
254 /* If atoi fails, devnum==0 and the default device is used */
255 devnum
= atoi(s
->filename
);
257 ret
= SendMessage(ctx
->hwnd
, WM_CAP_DRIVER_CONNECT
, devnum
, 0);
259 av_log(s
, AV_LOG_ERROR
, "Could not connect to device.\n");
260 DestroyWindow(ctx
->hwnd
);
261 return AVERROR(ENODEV
);
264 SendMessage(ctx
->hwnd
, WM_CAP_SET_OVERLAY
, 0, 0);
265 SendMessage(ctx
->hwnd
, WM_CAP_SET_PREVIEW
, 0, 0);
267 ret
= SendMessage(ctx
->hwnd
, WM_CAP_SET_CALLBACK_VIDEOSTREAM
, 0,
268 (LPARAM
) videostream_cb
);
270 av_log(s
, AV_LOG_ERROR
, "Could not set video stream callback.\n");
274 SetWindowLongPtr(ctx
->hwnd
, GWLP_USERDATA
, (LONG_PTR
) ctx
);
276 st
= av_new_stream(s
, 0);
279 return AVERROR_NOMEM
;
282 /* Set video format */
283 bisize
= SendMessage(ctx
->hwnd
, WM_CAP_GET_VIDEOFORMAT
, 0, 0);
286 bi
= av_malloc(bisize
);
289 return AVERROR_NOMEM
;
291 ret
= SendMessage(ctx
->hwnd
, WM_CAP_GET_VIDEOFORMAT
, bisize
, (LPARAM
) bi
);
295 dump_bih(s
, &bi
->bmiHeader
);
297 width
= ap
->width ? ap
->width
: bi
->bmiHeader
.biWidth
;
298 height
= ap
->height ? ap
->height
: bi
->bmiHeader
.biHeight
;
299 bi
->bmiHeader
.biWidth
= width
;
300 bi
->bmiHeader
.biHeight
= height
;
303 /* For testing yet unsupported compressions
304 * Copy these values from user-supplied verbose information */
305 bi
->bmiHeader
.biWidth
= 320;
306 bi
->bmiHeader
.biHeight
= 240;
307 bi
->bmiHeader
.biPlanes
= 1;
308 bi
->bmiHeader
.biBitCount
= 12;
309 bi
->bmiHeader
.biCompression
= MKTAG('I','4','2','0');
310 bi
->bmiHeader
.biSizeImage
= 115200;
311 dump_bih(s
, &bi
->bmiHeader
);
314 ret
= SendMessage(ctx
->hwnd
, WM_CAP_SET_VIDEOFORMAT
, bisize
, (LPARAM
) bi
);
316 av_log(s
, AV_LOG_ERROR
, "Could not set Video Format.\n");
320 biCompression
= bi
->bmiHeader
.biCompression
;
321 biBitCount
= bi
->bmiHeader
.biBitCount
;
325 /* Set sequence setup */
326 ret
= SendMessage(ctx
->hwnd
, WM_CAP_GET_SEQUENCE_SETUP
, sizeof(cparms
),
331 dump_captureparms(s
, &cparms
);
333 cparms
.fYield
= 1; // Spawn a background thread
334 cparms
.dwRequestMicroSecPerFrame
=
335 (ap
->time_base
.num
*1000000) / ap
->time_base
.den
;
336 cparms
.fAbortLeftMouse
= 0;
337 cparms
.fAbortRightMouse
= 0;
338 cparms
.fCaptureAudio
= 0;
339 cparms
.vKeyAbort
= 0;
341 ret
= SendMessage(ctx
->hwnd
, WM_CAP_SET_SEQUENCE_SETUP
, sizeof(cparms
),
347 codec
->time_base
= ap
->time_base
;
348 codec
->codec_type
= CODEC_TYPE_VIDEO
;
349 codec
->width
= width
;
350 codec
->height
= height
;
351 codec
->pix_fmt
= vfw_pixfmt(biCompression
, biBitCount
);
352 if(codec
->pix_fmt
== PIX_FMT_NONE
) {
353 codec
->codec_id
= vfw_codecid(biCompression
);
354 if(codec
->codec_id
== CODEC_ID_NONE
) {
355 av_log(s
, AV_LOG_ERROR
, "Unknown compression type. "
356 "Please report verbose (-v 9) debug information.\n");
358 return AVERROR_PATCHWELCOME
;
360 codec
->bits_per_coded_sample
= biBitCount
;
362 codec
->codec_id
= CODEC_ID_RAWVIDEO
;
363 if(biCompression
== BI_RGB
)
364 codec
->bits_per_coded_sample
= biBitCount
;
367 av_set_pts_info(st
, 32, 1, 1000);
369 ctx
->mutex
= CreateMutex(NULL
, 0, NULL
);
371 av_log(s
, AV_LOG_ERROR
, "Could not create Mutex.\n" );
374 ctx
->event
= CreateEvent(NULL
, 1, 0, NULL
);
376 av_log(s
, AV_LOG_ERROR
, "Could not create Event.\n" );
380 ret
= SendMessage(ctx
->hwnd
, WM_CAP_SEQUENCE_NOFILE
, 0, 0);
382 av_log(s
, AV_LOG_ERROR
, "Could not start capture sequence.\n" );
396 static int vfw_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
398 struct vfw_ctx
*ctx
= s
->priv_data
;
399 AVPacketList
*pktl
= NULL
;
402 WaitForSingleObject(ctx
->mutex
, INFINITE
);
405 *pkt
= ctx
->pktl
->pkt
;
406 ctx
->pktl
= ctx
->pktl
->next
;
409 ResetEvent(ctx
->event
);
410 ReleaseMutex(ctx
->mutex
);
412 if(s
->flags
& AVFMT_FLAG_NONBLOCK
) {
413 return AVERROR(EAGAIN
);
415 WaitForSingleObject(ctx
->event
, INFINITE
);
420 ctx
->curbufsize
-= pkt
->size
;
425 AVInputFormat vfwcap_demuxer
= {
427 NULL_IF_CONFIG_SMALL("VFW video capture"),
428 sizeof(struct vfw_ctx
),
433 .flags
= AVFMT_NOFILE
,