Statistics
| Branch: | Revision:

ffmpeg / libavdevice / vfwcap.c @ ce03ed4d

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
    unsigned int curbufsize;
42
    unsigned int frame_num;
43
};
44

    
45
static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
46
{
47
    switch(biCompression) {
48
    case MKTAG('U', 'Y', 'V', 'Y'):
49
        return PIX_FMT_UYVY422;
50
    case MKTAG('Y', 'U', 'Y', '2'):
51
        return PIX_FMT_YUYV422;
52
    case MKTAG('I', '4', '2', '0'):
53
        return PIX_FMT_YUV420P;
54
    case BI_RGB:
55
        switch(biBitCount) { /* 1-8 are untested */
56
            case 1:
57
                return PIX_FMT_MONOWHITE;
58
            case 4:
59
                return PIX_FMT_RGB4;
60
            case 8:
61
                return PIX_FMT_RGB8;
62
            case 16:
63
                return PIX_FMT_RGB555;
64
            case 24:
65
                return PIX_FMT_BGR24;
66
            case 32:
67
                return PIX_FMT_RGB32;
68
        }
69
    }
70
    return PIX_FMT_NONE;
71
}
72

    
73
static enum CodecID vfw_codecid(DWORD biCompression)
74
{
75
    switch(biCompression) {
76
    case MKTAG('d', 'v', 's', 'd'):
77
        return CODEC_ID_DVVIDEO;
78
    case MKTAG('M', 'J', 'P', 'G'):
79
    case MKTAG('m', 'j', 'p', 'g'):
80
        return CODEC_ID_MJPEG;
81
    }
82
    return CODEC_ID_NONE;
83
}
84

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

    
88
static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
89
{
90
    av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
91
    dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
92
    dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
93
    dstruct(s, cparms, wPercentDropForError, "u");
94
    dstruct(s, cparms, fYield, "d");
95
    dstruct(s, cparms, dwIndexSize, "lu");
96
    dstruct(s, cparms, wChunkGranularity, "u");
97
    dstruct(s, cparms, fUsingDOSMemory, "d");
98
    dstruct(s, cparms, wNumVideoRequested, "u");
99
    dstruct(s, cparms, fCaptureAudio, "d");
100
    dstruct(s, cparms, wNumAudioRequested, "u");
101
    dstruct(s, cparms, vKeyAbort, "u");
102
    dstruct(s, cparms, fAbortLeftMouse, "d");
103
    dstruct(s, cparms, fAbortRightMouse, "d");
104
    dstruct(s, cparms, fLimitEnabled, "d");
105
    dstruct(s, cparms, wTimeLimit, "u");
106
    dstruct(s, cparms, fMCIControl, "d");
107
    dstruct(s, cparms, fStepMCIDevice, "d");
108
    dstruct(s, cparms, dwMCIStartTime, "lu");
109
    dstruct(s, cparms, dwMCIStopTime, "lu");
110
    dstruct(s, cparms, fStepCaptureAt2x, "d");
111
    dstruct(s, cparms, wStepCaptureAverageFrames, "u");
112
    dstruct(s, cparms, dwAudioBufferSize, "lu");
113
    dstruct(s, cparms, fDisableWriteCache, "d");
114
    dstruct(s, cparms, AVStreamMaster, "u");
115
}
116

    
117
static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
118
{
119
#ifdef DEBUG_VFW
120
    av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
121
    dstruct(s, vhdr, lpData, "p");
122
    dstruct(s, vhdr, dwBufferLength, "lu");
123
    dstruct(s, vhdr, dwBytesUsed, "lu");
124
    dstruct(s, vhdr, dwTimeCaptured, "lu");
125
    dstruct(s, vhdr, dwUser, "lu");
126
    dstruct(s, vhdr, dwFlags, "lu");
127
    dstruct(s, vhdr, dwReserved[0], "lu");
128
    dstruct(s, vhdr, dwReserved[1], "lu");
129
    dstruct(s, vhdr, dwReserved[2], "lu");
130
    dstruct(s, vhdr, dwReserved[3], "lu");
131
#endif
132
}
133

    
134
static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
135
{
136
    av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
137
    dstruct(s, bih, biSize, "lu");
138
    dstruct(s, bih, biWidth, "ld");
139
    dstruct(s, bih, biHeight, "ld");
140
    dstruct(s, bih, biPlanes, "d");
141
    dstruct(s, bih, biBitCount, "d");
142
    dstruct(s, bih, biCompression, "lu");
143
    av_log(s, AV_LOG_DEBUG, "    biCompression:\t\"%.4s\"\n",
144
                   (char*) &bih->biCompression);
145
    dstruct(s, bih, biSizeImage, "lu");
146
    dstruct(s, bih, biXPelsPerMeter, "lu");
147
    dstruct(s, bih, biYPelsPerMeter, "lu");
148
    dstruct(s, bih, biClrUsed, "lu");
149
    dstruct(s, bih, biClrImportant, "lu");
150
}
151

    
152
static int shall_we_drop(AVFormatContext *s)
153
{
154
    struct vfw_ctx *ctx = s->priv_data;
155
    const uint8_t dropscore[] = {62, 75, 87, 100};
156
    const int ndropscores = FF_ARRAY_ELEMS(dropscore);
157
    unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
158

    
159
    if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
160
        av_log(s, AV_LOG_ERROR,
161
              "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
162
        return 1;
163
    }
164

    
165
    return 0;
166
}
167

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

    
174
    s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
175
    ctx = s->priv_data;
176

    
177
    dump_videohdr(s, vdhdr);
178

    
179
    if(shall_we_drop(s))
180
        return FALSE;
181

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

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

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

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

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

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

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

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

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

    
214
    if(ctx->hwnd) {
215
        SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
216
        SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
217
        DestroyWindow(ctx->hwnd);
218
    }
219
    if(ctx->mutex)
220
        CloseHandle(ctx->mutex);
221
    if(ctx->event)
222
        CloseHandle(ctx->event);
223

    
224
    return 0;
225
}
226

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

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

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

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

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

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

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

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

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

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

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

    
296
    width  = ap->width  ? ap->width  : bi->bmiHeader.biWidth ;
297
    height = ap->height ? ap->height : bi->bmiHeader.biHeight;
298
    bi->bmiHeader.biWidth  = width ;
299
    bi->bmiHeader.biHeight = height;
300

    
301
    if (0) {
302
        /* For testing yet unsupported compressions
303
         * Copy these values from user-supplied verbose information */
304
        bi->bmiHeader.biWidth       = 320;
305
        bi->bmiHeader.biHeight      = 240;
306
        bi->bmiHeader.biPlanes      = 1;
307
        bi->bmiHeader.biBitCount    = 12;
308
        bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
309
        bi->bmiHeader.biSizeImage   = 115200;
310
        dump_bih(s, &bi->bmiHeader);
311
    }
312

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

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

    
322
    av_free(bi);
323

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

    
330
    dump_captureparms(s, &cparms);
331

    
332
    cparms.fYield = 1; // Spawn a background thread
333
    cparms.dwRequestMicroSecPerFrame =
334
                               (ap->time_base.num*1000000) / ap->time_base.den;
335
    cparms.fAbortLeftMouse = 0;
336
    cparms.fAbortRightMouse = 0;
337
    cparms.fCaptureAudio = 0;
338
    cparms.vKeyAbort = 0;
339

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

    
345
    codec = st->codec;
346
    codec->time_base = ap->time_base;
347
    codec->codec_type = CODEC_TYPE_VIDEO;
348
    codec->width = width;
349
    codec->height = height;
350
    codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
351
    if(codec->pix_fmt == PIX_FMT_NONE) {
352
        codec->codec_id = vfw_codecid(biCompression);
353
        if(codec->codec_id == CODEC_ID_NONE) {
354
            av_log(s, AV_LOG_ERROR, "Unknown compression type. "
355
                             "Please report verbose (-v 9) debug information.\n");
356
            vfw_read_close(s);
357
            return AVERROR_PATCHWELCOME;
358
        }
359
        codec->bits_per_coded_sample = biBitCount;
360
    } else {
361
        codec->codec_id = CODEC_ID_RAWVIDEO;
362
        if(biCompression == BI_RGB)
363
            codec->bits_per_coded_sample = biBitCount;
364
    }
365

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

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

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

    
385
    return 0;
386

    
387
fail_bi:
388
    av_free(bi);
389

    
390
fail_io:
391
    vfw_read_close(s);
392
    return AVERROR(EIO);
393
}
394

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

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

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

    
421
    return pkt->size;
422
}
423

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