vfwcap: Add fallback define for HWND_MESSAGE
[libav.git] / libavdevice / vfwcap.c
CommitLineData
c2345207
RP
1/*
2 * VFW capture interface
406792e7 3 * Copyright (c) 2006-2008 Ramiro Polla
c2345207 4 *
2912e87a 5 * This file is part of Libav.
c2345207 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
c2345207
RP
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
c2345207
RP
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
c2345207
RP
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
245976da 22#include "libavformat/avformat.h"
c3f9ebf7 23#include "libavformat/internal.h"
3e15ea21
AK
24#include "libavutil/log.h"
25#include "libavutil/opt.h"
26#include "libavutil/parseutils.h"
c2345207 27#include <windows.h>
420755dd 28#include <vfw.h>
c2345207 29
ab56fabe
DB
30/* Some obsolete versions of MinGW32 before 4.0.0 lack this. */
31#ifndef HWND_MESSAGE
32#define HWND_MESSAGE ((HWND) -3)
33#endif
34
c2345207 35struct vfw_ctx {
3e15ea21 36 const AVClass *class;
c2345207
RP
37 HWND hwnd;
38 HANDLE mutex;
39 HANDLE event;
40 AVPacketList *pktl;
c2345207
RP
41 unsigned int curbufsize;
42 unsigned int frame_num;
3e15ea21 43 char *video_size; /**< A string describing video size, set by a private option. */
4078ed26 44 char *framerate; /**< Set by a private option. */
c2345207
RP
45};
46
716d413c 47static enum AVPixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
c2345207
RP
48{
49 switch(biCompression) {
e24080f0 50 case MKTAG('U', 'Y', 'V', 'Y'):
716d413c 51 return AV_PIX_FMT_UYVY422;
c2345207 52 case MKTAG('Y', 'U', 'Y', '2'):
716d413c 53 return AV_PIX_FMT_YUYV422;
23ef6da8 54 case MKTAG('I', '4', '2', '0'):
716d413c 55 return AV_PIX_FMT_YUV420P;
c2345207
RP
56 case BI_RGB:
57 switch(biBitCount) { /* 1-8 are untested */
58 case 1:
716d413c 59 return AV_PIX_FMT_MONOWHITE;
c2345207 60 case 4:
716d413c 61 return AV_PIX_FMT_RGB4;
c2345207 62 case 8:
716d413c 63 return AV_PIX_FMT_RGB8;
c2345207 64 case 16:
716d413c 65 return AV_PIX_FMT_RGB555;
c2345207 66 case 24:
716d413c 67 return AV_PIX_FMT_BGR24;
c2345207 68 case 32:
716d413c 69 return AV_PIX_FMT_RGB32;
c2345207
RP
70 }
71 }
716d413c 72 return AV_PIX_FMT_NONE;
c2345207
RP
73}
74
36ef5369 75static enum AVCodecID vfw_codecid(DWORD biCompression)
1c0b9215
RP
76{
77 switch(biCompression) {
78 case MKTAG('d', 'v', 's', 'd'):
36ef5369 79 return AV_CODEC_ID_DVVIDEO;
d7a4961e
NT
80 case MKTAG('M', 'J', 'P', 'G'):
81 case MKTAG('m', 'j', 'p', 'g'):
36ef5369 82 return AV_CODEC_ID_MJPEG;
1c0b9215 83 }
36ef5369 84 return AV_CODEC_ID_NONE;
1c0b9215
RP
85}
86
c2345207
RP
87#define dstruct(pctx, sname, var, type) \
88 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
89
90static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
91{
92 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
93 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
94 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
95 dstruct(s, cparms, wPercentDropForError, "u");
96 dstruct(s, cparms, fYield, "d");
97 dstruct(s, cparms, dwIndexSize, "lu");
98 dstruct(s, cparms, wChunkGranularity, "u");
99 dstruct(s, cparms, fUsingDOSMemory, "d");
100 dstruct(s, cparms, wNumVideoRequested, "u");
101 dstruct(s, cparms, fCaptureAudio, "d");
102 dstruct(s, cparms, wNumAudioRequested, "u");
103 dstruct(s, cparms, vKeyAbort, "u");
104 dstruct(s, cparms, fAbortLeftMouse, "d");
105 dstruct(s, cparms, fAbortRightMouse, "d");
106 dstruct(s, cparms, fLimitEnabled, "d");
107 dstruct(s, cparms, wTimeLimit, "u");
108 dstruct(s, cparms, fMCIControl, "d");
109 dstruct(s, cparms, fStepMCIDevice, "d");
110 dstruct(s, cparms, dwMCIStartTime, "lu");
111 dstruct(s, cparms, dwMCIStopTime, "lu");
112 dstruct(s, cparms, fStepCaptureAt2x, "d");
113 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
114 dstruct(s, cparms, dwAudioBufferSize, "lu");
115 dstruct(s, cparms, fDisableWriteCache, "d");
116 dstruct(s, cparms, AVStreamMaster, "u");
117}
118
119static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
120{
f190f676 121#ifdef DEBUG
c2345207
RP
122 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
123 dstruct(s, vhdr, lpData, "p");
124 dstruct(s, vhdr, dwBufferLength, "lu");
125 dstruct(s, vhdr, dwBytesUsed, "lu");
126 dstruct(s, vhdr, dwTimeCaptured, "lu");
127 dstruct(s, vhdr, dwUser, "lu");
128 dstruct(s, vhdr, dwFlags, "lu");
129 dstruct(s, vhdr, dwReserved[0], "lu");
130 dstruct(s, vhdr, dwReserved[1], "lu");
131 dstruct(s, vhdr, dwReserved[2], "lu");
132 dstruct(s, vhdr, dwReserved[3], "lu");
133#endif
134}
135
136static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
137{
138 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
139 dstruct(s, bih, biSize, "lu");
140 dstruct(s, bih, biWidth, "ld");
141 dstruct(s, bih, biHeight, "ld");
142 dstruct(s, bih, biPlanes, "d");
143 dstruct(s, bih, biBitCount, "d");
144 dstruct(s, bih, biCompression, "lu");
145 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
146 (char*) &bih->biCompression);
147 dstruct(s, bih, biSizeImage, "lu");
148 dstruct(s, bih, biXPelsPerMeter, "lu");
149 dstruct(s, bih, biYPelsPerMeter, "lu");
150 dstruct(s, bih, biClrUsed, "lu");
151 dstruct(s, bih, biClrImportant, "lu");
152}
153
ce03ed4d 154static int shall_we_drop(AVFormatContext *s)
c2345207 155{
ce03ed4d 156 struct vfw_ctx *ctx = s->priv_data;
c2345207 157 const uint8_t dropscore[] = {62, 75, 87, 100};
37d3e066 158 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
c2345207
RP
159 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
160
d76c3e07 161 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
ce03ed4d 162 av_log(s, AV_LOG_ERROR,
c2345207
RP
163 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
164 return 1;
165 }
166
167 return 0;
168}
169
170static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
171{
ce03ed4d 172 AVFormatContext *s;
c2345207
RP
173 struct vfw_ctx *ctx;
174 AVPacketList **ppktl, *pktl_next;
175
ce03ed4d
RP
176 s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
177 ctx = s->priv_data;
c2345207 178
ce03ed4d 179 dump_videohdr(s, vdhdr);
c2345207 180
ce03ed4d 181 if(shall_we_drop(s))
c2345207
RP
182 return FALSE;
183
184 WaitForSingleObject(ctx->mutex, INFINITE);
185
186 pktl_next = av_mallocz(sizeof(AVPacketList));
187 if(!pktl_next)
188 goto fail;
189
190 if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
191 av_free(pktl_next);
192 goto fail;
193 }
194
195 pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
196 memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
197
198 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
199 *ppktl = pktl_next;
200
201 ctx->curbufsize += vdhdr->dwBytesUsed;
202
203 SetEvent(ctx->event);
204 ReleaseMutex(ctx->mutex);
205
206 return TRUE;
207fail:
208 ReleaseMutex(ctx->mutex);
209 return FALSE;
210}
211
c959c6ee
DB
212static int vfw_read_close(AVFormatContext *s)
213{
214 struct vfw_ctx *ctx = s->priv_data;
ad8eede7 215 AVPacketList *pktl;
c959c6ee
DB
216
217 if(ctx->hwnd) {
218 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
219 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
220 DestroyWindow(ctx->hwnd);
221 }
222 if(ctx->mutex)
223 CloseHandle(ctx->mutex);
224 if(ctx->event)
225 CloseHandle(ctx->event);
226
ad8eede7
RP
227 pktl = ctx->pktl;
228 while (pktl) {
229 AVPacketList *next = pktl->next;
a6a27fed 230 av_free_packet(&pktl->pkt);
ad8eede7
RP
231 av_free(pktl);
232 pktl = next;
233 }
234
c959c6ee
DB
235 return 0;
236}
c2345207 237
6e9651d1 238static int vfw_read_header(AVFormatContext *s)
c2345207
RP
239{
240 struct vfw_ctx *ctx = s->priv_data;
241 AVCodecContext *codec;
242 AVStream *st;
243 int devnum;
244 int bisize;
245 BITMAPINFO *bi;
246 CAPTUREPARMS cparms;
247 DWORD biCompression;
248 WORD biBitCount;
c2345207 249 int ret;
a4bda405 250 AVRational framerate_q;
c2345207 251
1973e101
RP
252 if (!strcmp(s->filename, "list")) {
253 for (devnum = 0; devnum <= 9; devnum++) {
254 char driver_name[256];
255 char driver_ver[256];
256 ret = capGetDriverDescription(devnum,
257 driver_name, sizeof(driver_name),
258 driver_ver, sizeof(driver_ver));
259 if (ret) {
260 av_log(s, AV_LOG_INFO, "Driver %d\n", devnum);
261 av_log(s, AV_LOG_INFO, " %s\n", driver_name);
262 av_log(s, AV_LOG_INFO, " %s\n", driver_ver);
263 }
264 }
265 return AVERROR(EIO);
266 }
267
c2345207
RP
268 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
269 if(!ctx->hwnd) {
270 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
5ae092ee 271 return AVERROR(EIO);
c2345207
RP
272 }
273
274 /* If atoi fails, devnum==0 and the default device is used */
275 devnum = atoi(s->filename);
276
277 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
278 if(!ret) {
279 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
280 DestroyWindow(ctx->hwnd);
281 return AVERROR(ENODEV);
282 }
283
284 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
285 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
286
287 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
288 (LPARAM) videostream_cb);
289 if(!ret) {
290 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
76c2662b 291 goto fail_io;
c2345207
RP
292 }
293
ce03ed4d 294 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
c2345207 295
3b3bbdd3 296 st = avformat_new_stream(s, NULL);
c2345207
RP
297 if(!st) {
298 vfw_read_close(s);
2874c81c 299 return AVERROR(ENOMEM);
c2345207
RP
300 }
301
302 /* Set video format */
303 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
e4d286f1 304 if(!bisize)
76c2662b 305 goto fail_io;
c2345207
RP
306 bi = av_malloc(bisize);
307 if(!bi) {
308 vfw_read_close(s);
2874c81c 309 return AVERROR(ENOMEM);
c2345207
RP
310 }
311 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
e4d286f1 312 if(!ret)
e110f486 313 goto fail_bi;
c2345207
RP
314
315 dump_bih(s, &bi->bmiHeader);
316
3e15ea21
AK
317
318 if (ctx->video_size) {
319 ret = av_parse_video_size(&bi->bmiHeader.biWidth, &bi->bmiHeader.biHeight, ctx->video_size);
320 if (ret < 0) {
321 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
322 goto fail_bi;
323 }
324 }
c2345207 325
a9decf00 326 if (0) {
315fa33f
RP
327 /* For testing yet unsupported compressions
328 * Copy these values from user-supplied verbose information */
329 bi->bmiHeader.biWidth = 320;
330 bi->bmiHeader.biHeight = 240;
331 bi->bmiHeader.biPlanes = 1;
332 bi->bmiHeader.biBitCount = 12;
333 bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
334 bi->bmiHeader.biSizeImage = 115200;
335 dump_bih(s, &bi->bmiHeader);
a9decf00 336 }
cada0327 337
c2345207
RP
338 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
339 if(!ret) {
340 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
e110f486 341 goto fail_bi;
c2345207
RP
342 }
343
344 biCompression = bi->bmiHeader.biCompression;
345 biBitCount = bi->bmiHeader.biBitCount;
346
347 av_free(bi);
348
349 /* Set sequence setup */
350 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
351 (LPARAM) &cparms);
e4d286f1 352 if(!ret)
76c2662b 353 goto fail_io;
c2345207
RP
354
355 dump_captureparms(s, &cparms);
356
357 cparms.fYield = 1; // Spawn a background thread
358 cparms.dwRequestMicroSecPerFrame =
a4bda405 359 (framerate_q.den*1000000) / framerate_q.num;
c2345207
RP
360 cparms.fAbortLeftMouse = 0;
361 cparms.fAbortRightMouse = 0;
362 cparms.fCaptureAudio = 0;
363 cparms.vKeyAbort = 0;
364
365 ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
366 (LPARAM) &cparms);
e4d286f1 367 if(!ret)
76c2662b 368 goto fail_io;
c2345207
RP
369
370 codec = st->codec;
a4bda405 371 codec->time_base = (AVRational){framerate_q.den, framerate_q.num};
72415b2a 372 codec->codec_type = AVMEDIA_TYPE_VIDEO;
3e15ea21
AK
373 codec->width = bi->bmiHeader.biWidth;
374 codec->height = bi->bmiHeader.biHeight;
c2345207 375 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
716d413c 376 if(codec->pix_fmt == AV_PIX_FMT_NONE) {
1c0b9215 377 codec->codec_id = vfw_codecid(biCompression);
36ef5369 378 if(codec->codec_id == AV_CODEC_ID_NONE) {
155b0f00
RP
379 av_log(s, AV_LOG_ERROR, "Unknown compression type. "
380 "Please report verbose (-v 9) debug information.\n");
381 vfw_read_close(s);
382 return AVERROR_PATCHWELCOME;
1c0b9215
RP
383 }
384 codec->bits_per_coded_sample = biBitCount;
155b0f00 385 } else {
36ef5369 386 codec->codec_id = AV_CODEC_ID_RAWVIDEO;
faa2989e 387 if(biCompression == BI_RGB) {
155b0f00 388 codec->bits_per_coded_sample = biBitCount;
faa2989e
RP
389 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
390 if (codec->extradata) {
391 codec->extradata_size = 9;
392 memcpy(codec->extradata, "BottomUp", 9);
393 }
394 }
1c0b9215 395 }
42477de5 396
c3f9ebf7 397 avpriv_set_pts_info(st, 32, 1, 1000);
c2345207
RP
398
399 ctx->mutex = CreateMutex(NULL, 0, NULL);
400 if(!ctx->mutex) {
401 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
76c2662b 402 goto fail_io;
c2345207
RP
403 }
404 ctx->event = CreateEvent(NULL, 1, 0, NULL);
405 if(!ctx->event) {
406 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
76c2662b 407 goto fail_io;
c2345207
RP
408 }
409
410 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
411 if(!ret) {
412 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
76c2662b 413 goto fail_io;
c2345207
RP
414 }
415
416 return 0;
76c2662b 417
e110f486
RP
418fail_bi:
419 av_free(bi);
420
76c2662b
RP
421fail_io:
422 vfw_read_close(s);
5ae092ee 423 return AVERROR(EIO);
c2345207
RP
424}
425
426static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
427{
428 struct vfw_ctx *ctx = s->priv_data;
429 AVPacketList *pktl = NULL;
430
431 while(!pktl) {
432 WaitForSingleObject(ctx->mutex, INFINITE);
433 pktl = ctx->pktl;
434 if(ctx->pktl) {
435 *pkt = ctx->pktl->pkt;
436 ctx->pktl = ctx->pktl->next;
437 av_free(pktl);
438 }
439 ResetEvent(ctx->event);
440 ReleaseMutex(ctx->mutex);
441 if(!pktl) {
442 if(s->flags & AVFMT_FLAG_NONBLOCK) {
443 return AVERROR(EAGAIN);
444 } else {
445 WaitForSingleObject(ctx->event, INFINITE);
446 }
447 }
448 }
449
450 ctx->curbufsize -= pkt->size;
451
452 return pkt->size;
453}
454
3e15ea21
AK
455#define OFFSET(x) offsetof(struct vfw_ctx, x)
456#define DEC AV_OPT_FLAG_DECODING_PARAM
457static const AVOption options[] = {
145f741e
AK
458 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
459 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
3e15ea21
AK
460 { NULL },
461};
462
463static const AVClass vfw_class = {
464 .class_name = "VFW indev",
465 .item_name = av_default_item_name,
466 .option = options,
467 .version = LIBAVUTIL_VERSION_INT,
468};
469
c6610a21 470AVInputFormat ff_vfwcap_demuxer = {
30b4ee79
DB
471 .name = "vfwcap",
472 .long_name = NULL_IF_CONFIG_SMALL("VfW video capture"),
473 .priv_data_size = sizeof(struct vfw_ctx),
474 .read_header = vfw_read_header,
475 .read_packet = vfw_read_packet,
476 .read_close = vfw_read_close,
477 .flags = AVFMT_NOFILE,
478 .priv_class = &vfw_class,
c2345207 479};