2008-03-09 00:10:28 +02:00
|
|
|
/*
|
|
|
|
* VFW capture interface
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2006-2008 Ramiro Polla
|
2008-03-09 00:10:28 +02:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2015-04-08 17:27:42 +02:00
|
|
|
#include "libavutil/internal.h"
|
2011-05-24 08:43:01 +03:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
2015-04-08 17:27:42 +02:00
|
|
|
|
|
|
|
#include "libavformat/internal.h"
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2015-04-10 11:22:33 +02:00
|
|
|
// windows.h must no be included before winsock2.h, and libavformat internal
|
|
|
|
// headers may include winsock2.h
|
2015-04-10 18:08:52 +02:00
|
|
|
#include <windows.h>
|
2015-04-10 11:22:33 +02:00
|
|
|
// windows.h needs to be included before vfw.h
|
2015-04-10 18:08:52 +02:00
|
|
|
#include <vfw.h>
|
|
|
|
|
2011-05-26 20:11:25 +03:00
|
|
|
#include "avdevice.h"
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2014-08-25 18:26:15 +03:00
|
|
|
/* Some obsolete versions of MinGW32 before 4.0.0 lack this. */
|
|
|
|
#ifndef HWND_MESSAGE
|
|
|
|
#define HWND_MESSAGE ((HWND) -3)
|
|
|
|
#endif
|
|
|
|
|
2008-03-09 00:10:28 +02:00
|
|
|
struct vfw_ctx {
|
2011-05-24 08:43:01 +03:00
|
|
|
const AVClass *class;
|
2008-03-09 00:10:28 +02:00
|
|
|
HWND hwnd;
|
|
|
|
HANDLE mutex;
|
|
|
|
HANDLE event;
|
|
|
|
AVPacketList *pktl;
|
|
|
|
unsigned int curbufsize;
|
|
|
|
unsigned int frame_num;
|
2011-05-24 08:43:01 +03:00
|
|
|
char *video_size; /**< A string describing video size, set by a private option. */
|
2011-05-24 22:16:47 +03:00
|
|
|
char *framerate; /**< Set by a private option. */
|
2008-03-09 00:10:28 +02:00
|
|
|
};
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
static enum AVPixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
|
2008-03-09 00:10:28 +02:00
|
|
|
{
|
|
|
|
switch(biCompression) {
|
2010-03-10 07:25:06 +02:00
|
|
|
case MKTAG('U', 'Y', 'V', 'Y'):
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_UYVY422;
|
2008-03-09 00:10:28 +02:00
|
|
|
case MKTAG('Y', 'U', 'Y', '2'):
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_YUYV422;
|
2008-04-14 18:41:10 +03:00
|
|
|
case MKTAG('I', '4', '2', '0'):
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_YUV420P;
|
2008-03-09 00:10:28 +02:00
|
|
|
case BI_RGB:
|
|
|
|
switch(biBitCount) { /* 1-8 are untested */
|
|
|
|
case 1:
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_MONOWHITE;
|
2008-03-09 00:10:28 +02:00
|
|
|
case 4:
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_RGB4;
|
2008-03-09 00:10:28 +02:00
|
|
|
case 8:
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_RGB8;
|
2008-03-09 00:10:28 +02:00
|
|
|
case 16:
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_RGB555;
|
2008-03-09 00:10:28 +02:00
|
|
|
case 24:
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_BGR24;
|
2008-03-09 00:10:28 +02:00
|
|
|
case 32:
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_RGB32;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-06 13:10:34 +03:00
|
|
|
return AV_PIX_FMT_NONE;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
2012-08-05 12:11:04 +03:00
|
|
|
static enum AVCodecID vfw_codecid(DWORD biCompression)
|
2009-07-30 20:29:24 +03:00
|
|
|
{
|
|
|
|
switch(biCompression) {
|
|
|
|
case MKTAG('d', 'v', 's', 'd'):
|
2012-08-05 12:11:04 +03:00
|
|
|
return AV_CODEC_ID_DVVIDEO;
|
2010-02-23 18:49:16 +02:00
|
|
|
case MKTAG('M', 'J', 'P', 'G'):
|
|
|
|
case MKTAG('m', 'j', 'p', 'g'):
|
2012-08-05 12:11:04 +03:00
|
|
|
return AV_CODEC_ID_MJPEG;
|
2009-07-30 20:29:24 +03:00
|
|
|
}
|
2012-08-05 12:11:04 +03:00
|
|
|
return AV_CODEC_ID_NONE;
|
2009-07-30 20:29:24 +03:00
|
|
|
}
|
|
|
|
|
2008-03-09 00:10:28 +02:00
|
|
|
#define dstruct(pctx, sname, var, type) \
|
|
|
|
av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
|
|
|
|
|
|
|
|
static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
|
|
|
|
{
|
|
|
|
av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
|
|
|
|
dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
|
|
|
|
dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
|
|
|
|
dstruct(s, cparms, wPercentDropForError, "u");
|
|
|
|
dstruct(s, cparms, fYield, "d");
|
|
|
|
dstruct(s, cparms, dwIndexSize, "lu");
|
|
|
|
dstruct(s, cparms, wChunkGranularity, "u");
|
|
|
|
dstruct(s, cparms, fUsingDOSMemory, "d");
|
|
|
|
dstruct(s, cparms, wNumVideoRequested, "u");
|
|
|
|
dstruct(s, cparms, fCaptureAudio, "d");
|
|
|
|
dstruct(s, cparms, wNumAudioRequested, "u");
|
|
|
|
dstruct(s, cparms, vKeyAbort, "u");
|
|
|
|
dstruct(s, cparms, fAbortLeftMouse, "d");
|
|
|
|
dstruct(s, cparms, fAbortRightMouse, "d");
|
|
|
|
dstruct(s, cparms, fLimitEnabled, "d");
|
|
|
|
dstruct(s, cparms, wTimeLimit, "u");
|
|
|
|
dstruct(s, cparms, fMCIControl, "d");
|
|
|
|
dstruct(s, cparms, fStepMCIDevice, "d");
|
|
|
|
dstruct(s, cparms, dwMCIStartTime, "lu");
|
|
|
|
dstruct(s, cparms, dwMCIStopTime, "lu");
|
|
|
|
dstruct(s, cparms, fStepCaptureAt2x, "d");
|
|
|
|
dstruct(s, cparms, wStepCaptureAverageFrames, "u");
|
|
|
|
dstruct(s, cparms, dwAudioBufferSize, "lu");
|
|
|
|
dstruct(s, cparms, fDisableWriteCache, "d");
|
|
|
|
dstruct(s, cparms, AVStreamMaster, "u");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
|
|
|
|
{
|
2011-06-02 23:45:15 +03:00
|
|
|
#ifdef DEBUG
|
2008-03-09 00:10:28 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
|
|
|
|
dstruct(s, vhdr, lpData, "p");
|
|
|
|
dstruct(s, vhdr, dwBufferLength, "lu");
|
|
|
|
dstruct(s, vhdr, dwBytesUsed, "lu");
|
|
|
|
dstruct(s, vhdr, dwTimeCaptured, "lu");
|
|
|
|
dstruct(s, vhdr, dwUser, "lu");
|
|
|
|
dstruct(s, vhdr, dwFlags, "lu");
|
|
|
|
dstruct(s, vhdr, dwReserved[0], "lu");
|
|
|
|
dstruct(s, vhdr, dwReserved[1], "lu");
|
|
|
|
dstruct(s, vhdr, dwReserved[2], "lu");
|
|
|
|
dstruct(s, vhdr, dwReserved[3], "lu");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
|
|
|
|
{
|
|
|
|
av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
|
|
|
|
dstruct(s, bih, biSize, "lu");
|
|
|
|
dstruct(s, bih, biWidth, "ld");
|
|
|
|
dstruct(s, bih, biHeight, "ld");
|
|
|
|
dstruct(s, bih, biPlanes, "d");
|
|
|
|
dstruct(s, bih, biBitCount, "d");
|
|
|
|
dstruct(s, bih, biCompression, "lu");
|
|
|
|
av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
|
|
|
|
(char*) &bih->biCompression);
|
|
|
|
dstruct(s, bih, biSizeImage, "lu");
|
|
|
|
dstruct(s, bih, biXPelsPerMeter, "lu");
|
|
|
|
dstruct(s, bih, biYPelsPerMeter, "lu");
|
|
|
|
dstruct(s, bih, biClrUsed, "lu");
|
|
|
|
dstruct(s, bih, biClrImportant, "lu");
|
|
|
|
}
|
|
|
|
|
2010-03-17 06:24:43 +02:00
|
|
|
static int shall_we_drop(AVFormatContext *s)
|
2008-03-09 00:10:28 +02:00
|
|
|
{
|
2010-03-17 06:24:43 +02:00
|
|
|
struct vfw_ctx *ctx = s->priv_data;
|
2015-11-11 18:38:43 +02:00
|
|
|
static const uint8_t dropscore[4] = { 62, 75, 87, 100 };
|
2008-10-22 00:40:24 +03:00
|
|
|
const int ndropscores = FF_ARRAY_ELEMS(dropscore);
|
2008-03-09 00:10:28 +02:00
|
|
|
unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
|
|
|
|
|
2008-03-09 01:06:33 +02:00
|
|
|
if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
|
2010-03-17 06:24:43 +02:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2008-03-09 00:10:28 +02:00
|
|
|
"real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
|
|
|
|
{
|
2010-03-17 06:24:43 +02:00
|
|
|
AVFormatContext *s;
|
2008-03-09 00:10:28 +02:00
|
|
|
struct vfw_ctx *ctx;
|
|
|
|
AVPacketList **ppktl, *pktl_next;
|
|
|
|
|
2010-03-17 06:24:43 +02:00
|
|
|
s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
|
|
|
ctx = s->priv_data;
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2010-03-17 06:24:43 +02:00
|
|
|
dump_videohdr(s, vdhdr);
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2010-03-17 06:24:43 +02:00
|
|
|
if(shall_we_drop(s))
|
2008-03-09 00:10:28 +02:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
WaitForSingleObject(ctx->mutex, INFINITE);
|
|
|
|
|
|
|
|
pktl_next = av_mallocz(sizeof(AVPacketList));
|
|
|
|
if(!pktl_next)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
|
|
|
|
av_free(pktl_next);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
|
|
|
|
memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
|
|
|
|
|
|
|
|
for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
|
|
|
|
*ppktl = pktl_next;
|
|
|
|
|
|
|
|
ctx->curbufsize += vdhdr->dwBytesUsed;
|
|
|
|
|
|
|
|
SetEvent(ctx->event);
|
|
|
|
ReleaseMutex(ctx->mutex);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
fail:
|
|
|
|
ReleaseMutex(ctx->mutex);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-08-02 20:39:10 +03:00
|
|
|
static int vfw_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
struct vfw_ctx *ctx = s->priv_data;
|
2010-03-17 06:36:06 +02:00
|
|
|
AVPacketList *pktl;
|
2009-08-02 20:39:10 +03:00
|
|
|
|
|
|
|
if(ctx->hwnd) {
|
|
|
|
SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
|
|
|
|
SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
|
|
|
|
DestroyWindow(ctx->hwnd);
|
|
|
|
}
|
|
|
|
if(ctx->mutex)
|
|
|
|
CloseHandle(ctx->mutex);
|
|
|
|
if(ctx->event)
|
|
|
|
CloseHandle(ctx->event);
|
|
|
|
|
2010-03-17 06:36:06 +02:00
|
|
|
pktl = ctx->pktl;
|
|
|
|
while (pktl) {
|
|
|
|
AVPacketList *next = pktl->next;
|
2015-10-23 11:11:31 +02:00
|
|
|
av_packet_unref(&pktl->pkt);
|
2010-03-17 06:36:06 +02:00
|
|
|
av_free(pktl);
|
|
|
|
pktl = next;
|
|
|
|
}
|
|
|
|
|
2009-08-02 20:39:10 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int vfw_read_header(AVFormatContext *s)
|
2008-03-09 00:10:28 +02:00
|
|
|
{
|
|
|
|
struct vfw_ctx *ctx = s->priv_data;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
AVCodecParameters *par;
|
2008-03-09 00:10:28 +02:00
|
|
|
AVStream *st;
|
|
|
|
int devnum;
|
|
|
|
int bisize;
|
2011-08-27 16:06:37 +03:00
|
|
|
BITMAPINFO *bi = NULL;
|
2008-03-09 00:10:28 +02:00
|
|
|
CAPTUREPARMS cparms;
|
|
|
|
DWORD biCompression;
|
|
|
|
WORD biBitCount;
|
|
|
|
int ret;
|
2011-06-03 11:41:47 +03:00
|
|
|
AVRational framerate_q;
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2017-12-30 00:29:52 +02:00
|
|
|
if (!strcmp(s->url, "list")) {
|
2010-10-23 05:11:30 +03:00
|
|
|
for (devnum = 0; devnum <= 9; devnum++) {
|
|
|
|
char driver_name[256];
|
|
|
|
char driver_ver[256];
|
|
|
|
ret = capGetDriverDescription(devnum,
|
|
|
|
driver_name, sizeof(driver_name),
|
|
|
|
driver_ver, sizeof(driver_ver));
|
|
|
|
if (ret) {
|
|
|
|
av_log(s, AV_LOG_INFO, "Driver %d\n", devnum);
|
|
|
|
av_log(s, AV_LOG_INFO, " %s\n", driver_name);
|
|
|
|
av_log(s, AV_LOG_INFO, " %s\n", driver_ver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
2008-03-09 00:10:28 +02:00
|
|
|
ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
|
|
|
|
if(!ctx->hwnd) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
|
2010-03-13 21:19:44 +02:00
|
|
|
return AVERROR(EIO);
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If atoi fails, devnum==0 and the default device is used */
|
2017-12-30 00:29:52 +02:00
|
|
|
devnum = atoi(s->url);
|
2008-03-09 00:10:28 +02:00
|
|
|
|
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
|
|
|
|
if(!ret) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
|
|
|
|
DestroyWindow(ctx->hwnd);
|
|
|
|
return AVERROR(ENODEV);
|
|
|
|
}
|
|
|
|
|
|
|
|
SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
|
|
|
|
SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
|
|
|
|
|
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
|
|
|
|
(LPARAM) videostream_cb);
|
|
|
|
if(!ret) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
2010-03-17 06:24:43 +02:00
|
|
|
SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2008-03-09 00:10:28 +02:00
|
|
|
if(!st) {
|
|
|
|
vfw_read_close(s);
|
2010-04-03 17:15:00 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set video format */
|
|
|
|
bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
|
2008-04-14 18:13:06 +03:00
|
|
|
if(!bisize)
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
bi = av_malloc(bisize);
|
|
|
|
if(!bi) {
|
|
|
|
vfw_read_close(s);
|
2010-04-03 17:15:00 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
|
2008-04-14 18:13:06 +03:00
|
|
|
if(!ret)
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
|
|
|
|
dump_bih(s, &bi->bmiHeader);
|
|
|
|
|
2011-09-02 07:33:07 +03:00
|
|
|
ret = av_parse_video_rate(&framerate_q, ctx->framerate);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-05-24 08:43:01 +03:00
|
|
|
|
|
|
|
if (ctx->video_size) {
|
2018-04-15 23:40:19 +02:00
|
|
|
int w, h;
|
|
|
|
ret = av_parse_video_size(&w, &h, ctx->video_size);
|
2011-05-24 08:43:01 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2011-05-24 08:43:01 +03:00
|
|
|
}
|
2018-04-15 23:40:19 +02:00
|
|
|
bi->bmiHeader.biWidth = w;
|
|
|
|
bi->bmiHeader.biHeight = h;
|
2011-05-24 08:43:01 +03:00
|
|
|
}
|
2008-03-09 00:10:28 +02:00
|
|
|
|
2009-09-04 19:31:21 +03:00
|
|
|
if (0) {
|
2009-09-04 19:32:06 +03:00
|
|
|
/* For testing yet unsupported compressions
|
|
|
|
* Copy these values from user-supplied verbose information */
|
|
|
|
bi->bmiHeader.biWidth = 320;
|
|
|
|
bi->bmiHeader.biHeight = 240;
|
|
|
|
bi->bmiHeader.biPlanes = 1;
|
|
|
|
bi->bmiHeader.biBitCount = 12;
|
|
|
|
bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
|
|
|
|
bi->bmiHeader.biSizeImage = 115200;
|
|
|
|
dump_bih(s, &bi->bmiHeader);
|
2009-09-04 19:31:21 +03:00
|
|
|
}
|
2008-04-14 18:44:33 +03:00
|
|
|
|
2008-03-09 00:10:28 +02:00
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
|
|
|
|
if(!ret) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
biCompression = bi->bmiHeader.biCompression;
|
|
|
|
biBitCount = bi->bmiHeader.biBitCount;
|
|
|
|
|
|
|
|
/* Set sequence setup */
|
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
|
|
|
|
(LPARAM) &cparms);
|
2008-04-14 18:13:06 +03:00
|
|
|
if(!ret)
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
|
|
|
|
dump_captureparms(s, &cparms);
|
|
|
|
|
|
|
|
cparms.fYield = 1; // Spawn a background thread
|
|
|
|
cparms.dwRequestMicroSecPerFrame =
|
2011-06-03 11:41:47 +03:00
|
|
|
(framerate_q.den*1000000) / framerate_q.num;
|
2008-03-09 00:10:28 +02:00
|
|
|
cparms.fAbortLeftMouse = 0;
|
|
|
|
cparms.fAbortRightMouse = 0;
|
|
|
|
cparms.fCaptureAudio = 0;
|
|
|
|
cparms.vKeyAbort = 0;
|
|
|
|
|
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
|
|
|
|
(LPARAM) &cparms);
|
2008-04-14 18:13:06 +03:00
|
|
|
if(!ret)
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
st->avg_frame_rate = framerate_q;
|
|
|
|
|
|
|
|
par = st->codecpar;
|
|
|
|
par->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
par->width = bi->bmiHeader.biWidth;
|
|
|
|
par->height = bi->bmiHeader.biHeight;
|
|
|
|
par->format = vfw_pixfmt(biCompression, biBitCount);
|
|
|
|
if (par->format == AV_PIX_FMT_NONE) {
|
|
|
|
par->codec_id = vfw_codecid(biCompression);
|
|
|
|
if (par->codec_id == AV_CODEC_ID_NONE) {
|
2015-12-16 19:01:34 +02:00
|
|
|
avpriv_report_missing_feature(s, "This compression type");
|
2009-07-30 20:31:28 +03:00
|
|
|
vfw_read_close(s);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
2009-07-30 20:29:24 +03:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->bits_per_coded_sample = biBitCount;
|
2009-07-30 20:31:28 +03:00
|
|
|
} else {
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->codec_id = AV_CODEC_ID_RAWVIDEO;
|
2010-04-29 16:05:27 +03:00
|
|
|
if(biCompression == BI_RGB) {
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->bits_per_coded_sample = biBitCount;
|
|
|
|
par->extradata = av_malloc(9 + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (par->extradata) {
|
|
|
|
par->extradata_size = 9;
|
|
|
|
memcpy(par->extradata, "BottomUp", 9);
|
2010-04-29 16:05:27 +03:00
|
|
|
}
|
|
|
|
}
|
2009-07-30 20:29:24 +03:00
|
|
|
}
|
2009-07-30 20:25:15 +03:00
|
|
|
|
2011-08-27 16:06:37 +03:00
|
|
|
av_freep(&bi);
|
|
|
|
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 32, 1, 1000);
|
2008-03-09 00:10:28 +02:00
|
|
|
|
|
|
|
ctx->mutex = CreateMutex(NULL, 0, NULL);
|
|
|
|
if(!ctx->mutex) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
ctx->event = CreateEvent(NULL, 1, 0, NULL);
|
|
|
|
if(!ctx->event) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
|
|
|
|
if(!ret) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
|
2011-08-27 16:06:37 +03:00
|
|
|
goto fail;
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-04-14 17:59:51 +03:00
|
|
|
|
2011-08-27 16:06:37 +03:00
|
|
|
fail:
|
|
|
|
av_freep(&bi);
|
2008-04-14 17:59:51 +03:00
|
|
|
vfw_read_close(s);
|
2010-03-13 21:19:44 +02:00
|
|
|
return AVERROR(EIO);
|
2008-03-09 00:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
struct vfw_ctx *ctx = s->priv_data;
|
|
|
|
AVPacketList *pktl = NULL;
|
|
|
|
|
|
|
|
while(!pktl) {
|
|
|
|
WaitForSingleObject(ctx->mutex, INFINITE);
|
|
|
|
pktl = ctx->pktl;
|
|
|
|
if(ctx->pktl) {
|
|
|
|
*pkt = ctx->pktl->pkt;
|
|
|
|
ctx->pktl = ctx->pktl->next;
|
|
|
|
av_free(pktl);
|
|
|
|
}
|
|
|
|
ResetEvent(ctx->event);
|
|
|
|
ReleaseMutex(ctx->mutex);
|
|
|
|
if(!pktl) {
|
|
|
|
if(s->flags & AVFMT_FLAG_NONBLOCK) {
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
} else {
|
|
|
|
WaitForSingleObject(ctx->event, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->curbufsize -= pkt->size;
|
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2011-05-24 08:43:01 +03:00
|
|
|
#define OFFSET(x) offsetof(struct vfw_ctx, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2011-10-04 08:38:01 +03:00
|
|
|
{ "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
|
|
|
|
{ "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
|
2011-05-24 08:43:01 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass vfw_class = {
|
|
|
|
.class_name = "VFW indev",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2014-02-23 01:32:51 +03:00
|
|
|
.category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
|
2011-05-24 08:43:01 +03:00
|
|
|
};
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_vfwcap_demuxer = {
|
2011-09-23 21:50:11 +03:00
|
|
|
.name = "vfwcap",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("VfW video capture"),
|
|
|
|
.priv_data_size = sizeof(struct vfw_ctx),
|
|
|
|
.read_header = vfw_read_header,
|
|
|
|
.read_packet = vfw_read_packet,
|
|
|
|
.read_close = vfw_read_close,
|
|
|
|
.flags = AVFMT_NOFILE,
|
|
|
|
.priv_class = &vfw_class,
|
2008-03-09 00:10:28 +02:00
|
|
|
};
|