Statistics
| Branch: | Revision:

ffmpeg / libavdevice / vfwcap.c @ e24080f0

History | View | Annotate | Download (12.4 KB)

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 "libavformat/avformat.h"
23
#include <windows.h>
24
#include <vfw.h>
25

    
26
//#define DEBUG_VFW
27

    
28
/* Defines for VFW missing from MinGW.
29
 * Remove this when MinGW incorporates them. */
30
#define HWND_MESSAGE                ((HWND)-3)
31

    
32
#define BI_RGB                      0
33

    
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('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;
55
    case BI_RGB:
56
        switch(biBitCount) { /* 1-8 are untested */
57
            case 1:
58
                return PIX_FMT_MONOWHITE;
59
            case 4:
60
                return PIX_FMT_RGB4;
61
            case 8:
62
                return PIX_FMT_RGB8;
63
            case 16:
64
                return PIX_FMT_RGB555;
65
            case 24:
66
                return PIX_FMT_BGR24;
67
            case 32:
68
                return PIX_FMT_RGB32;
69
        }
70
    }
71
    return PIX_FMT_NONE;
72
}
73

    
74
static enum CodecID vfw_codecid(DWORD biCompression)
75
{
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;
82
    }
83
    return CODEC_ID_NONE;
84
}
85

    
86
#define dstruct(pctx, sname, var, type) \
87
    av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
88

    
89
static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
90
{
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");
116
}
117

    
118
static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
119
{
120
#ifdef DEBUG_VFW
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");
132
#endif
133
}
134

    
135
static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
136
{
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");
151
}
152

    
153
static int shall_we_drop(struct vfw_ctx *ctx)
154
{
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;
159

    
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);
163
        return 1;
164
    }
165

    
166
    return 0;
167
}
168

    
169
static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
170
{
171
    struct vfw_ctx *ctx;
172
    AVPacketList **ppktl, *pktl_next;
173

    
174
    ctx = (struct vfw_ctx *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
175

    
176
    dump_videohdr(ctx->s, vdhdr);
177

    
178
    if(shall_we_drop(ctx))
179
        return FALSE;
180

    
181
    WaitForSingleObject(ctx->mutex, INFINITE);
182

    
183
    pktl_next = av_mallocz(sizeof(AVPacketList));
184
    if(!pktl_next)
185
        goto fail;
186

    
187
    if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
188
        av_free(pktl_next);
189
        goto fail;
190
    }
191

    
192
    pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
193
    memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
194

    
195
    for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
196
    *ppktl = pktl_next;
197

    
198
    ctx->curbufsize += vdhdr->dwBytesUsed;
199

    
200
    SetEvent(ctx->event);
201
    ReleaseMutex(ctx->mutex);
202

    
203
    return TRUE;
204
fail:
205
    ReleaseMutex(ctx->mutex);
206
    return FALSE;
207
}
208

    
209
static int vfw_read_close(AVFormatContext *s)
210
{
211
    struct vfw_ctx *ctx = s->priv_data;
212

    
213
    if(ctx->hwnd) {
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);
217
    }
218
    if(ctx->mutex)
219
        CloseHandle(ctx->mutex);
220
    if(ctx->event)
221
        CloseHandle(ctx->event);
222

    
223
    return 0;
224
}
225

    
226
static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
227
{
228
    struct vfw_ctx *ctx = s->priv_data;
229
    AVCodecContext *codec;
230
    AVStream *st;
231
    int devnum;
232
    int bisize;
233
    BITMAPINFO *bi;
234
    CAPTUREPARMS cparms;
235
    DWORD biCompression;
236
    WORD biBitCount;
237
    int width;
238
    int height;
239
    int ret;
240

    
241
    if(!ap->time_base.den) {
242
        av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
243
        return AVERROR_IO;
244
    }
245

    
246
    ctx->s = s;
247

    
248
    ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
249
    if(!ctx->hwnd) {
250
        av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
251
        return AVERROR_IO;
252
    }
253

    
254
    /* If atoi fails, devnum==0 and the default device is used */
255
    devnum = atoi(s->filename);
256

    
257
    ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
258
    if(!ret) {
259
        av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
260
        DestroyWindow(ctx->hwnd);
261
        return AVERROR(ENODEV);
262
    }
263

    
264
    SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
265
    SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
266

    
267
    ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
268
                      (LPARAM) videostream_cb);
269
    if(!ret) {
270
        av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
271
        goto fail_io;
272
    }
273

    
274
    SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) ctx);
275

    
276
    st = av_new_stream(s, 0);
277
    if(!st) {
278
        vfw_read_close(s);
279
        return AVERROR_NOMEM;
280
    }
281

    
282
    /* Set video format */
283
    bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
284
    if(!bisize)
285
        goto fail_io;
286
    bi = av_malloc(bisize);
287
    if(!bi) {
288
        vfw_read_close(s);
289
        return AVERROR_NOMEM;
290
    }
291
    ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
292
    if(!ret)
293
        goto fail_bi;
294

    
295
    dump_bih(s, &bi->bmiHeader);
296

    
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;
301

    
302
    if (0) {
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);
312
    }
313

    
314
    ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
315
    if(!ret) {
316
        av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
317
        goto fail_bi;
318
    }
319

    
320
    biCompression = bi->bmiHeader.biCompression;
321
    biBitCount = bi->bmiHeader.biBitCount;
322

    
323
    av_free(bi);
324

    
325
    /* Set sequence setup */
326
    ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
327
                      (LPARAM) &cparms);
328
    if(!ret)
329
        goto fail_io;
330

    
331
    dump_captureparms(s, &cparms);
332

    
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;
340

    
341
    ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
342
                      (LPARAM) &cparms);
343
    if(!ret)
344
        goto fail_io;
345

    
346
    codec = st->codec;
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");
357
            vfw_read_close(s);
358
            return AVERROR_PATCHWELCOME;
359
        }
360
        codec->bits_per_coded_sample = biBitCount;
361
    } else {
362
        codec->codec_id = CODEC_ID_RAWVIDEO;
363
        if(biCompression == BI_RGB)
364
            codec->bits_per_coded_sample = biBitCount;
365
    }
366

    
367
    av_set_pts_info(st, 32, 1, 1000);
368

    
369
    ctx->mutex = CreateMutex(NULL, 0, NULL);
370
    if(!ctx->mutex) {
371
        av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
372
        goto fail_io;
373
    }
374
    ctx->event = CreateEvent(NULL, 1, 0, NULL);
375
    if(!ctx->event) {
376
        av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
377
        goto fail_io;
378
    }
379

    
380
    ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
381
    if(!ret) {
382
        av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
383
        goto fail_io;
384
    }
385

    
386
    return 0;
387

    
388
fail_bi:
389
    av_free(bi);
390

    
391
fail_io:
392
    vfw_read_close(s);
393
    return AVERROR_IO;
394
}
395

    
396
static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
397
{
398
    struct vfw_ctx *ctx = s->priv_data;
399
    AVPacketList *pktl = NULL;
400

    
401
    while(!pktl) {
402
        WaitForSingleObject(ctx->mutex, INFINITE);
403
        pktl = ctx->pktl;
404
        if(ctx->pktl) {
405
            *pkt = ctx->pktl->pkt;
406
            ctx->pktl = ctx->pktl->next;
407
            av_free(pktl);
408
        }
409
        ResetEvent(ctx->event);
410
        ReleaseMutex(ctx->mutex);
411
        if(!pktl) {
412
            if(s->flags & AVFMT_FLAG_NONBLOCK) {
413
                return AVERROR(EAGAIN);
414
            } else {
415
                WaitForSingleObject(ctx->event, INFINITE);
416
            }
417
        }
418
    }
419

    
420
    ctx->curbufsize -= pkt->size;
421

    
422
    return pkt->size;
423
}
424

    
425
AVInputFormat vfwcap_demuxer = {
426
    "vfwcap",
427
    NULL_IF_CONFIG_SMALL("VFW video capture"),
428
    sizeof(struct vfw_ctx),
429
    NULL,
430
    vfw_read_header,
431
    vfw_read_packet,
432
    vfw_read_close,
433
    .flags = AVFMT_NOFILE,
434
};