00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavformat/avformat.h"
00023 #include <windows.h>
00024 #include <vfw.h>
00025
00026
00027
00028
00029
00030 #define HWND_MESSAGE ((HWND)-3)
00031
00032 #define BI_RGB 0
00033
00034
00035
00036 struct vfw_ctx {
00037 HWND hwnd;
00038 HANDLE mutex;
00039 HANDLE event;
00040 AVPacketList *pktl;
00041 AVFormatContext *s;
00042 unsigned int curbufsize;
00043 unsigned int frame_num;
00044 };
00045
00046 static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
00047 {
00048 switch(biCompression) {
00049 case MKTAG('Y', 'U', 'Y', '2'):
00050 return PIX_FMT_YUYV422;
00051 case MKTAG('I', '4', '2', '0'):
00052 return PIX_FMT_YUV420P;
00053 case BI_RGB:
00054 switch(biBitCount) {
00055 case 1:
00056 return PIX_FMT_MONOWHITE;
00057 case 4:
00058 return PIX_FMT_RGB4;
00059 case 8:
00060 return PIX_FMT_RGB8;
00061 case 16:
00062 return PIX_FMT_RGB555;
00063 case 24:
00064 return PIX_FMT_BGR24;
00065 case 32:
00066 return PIX_FMT_RGB32;
00067 }
00068 }
00069 return -1;
00070 }
00071
00072 #define dstruct(pctx, sname, var, type) \
00073 av_log(pctx, AV_LOG_DEBUG, #var":\t%"type"\n", sname->var)
00074
00075 static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms)
00076 {
00077 av_log(s, AV_LOG_DEBUG, "CAPTUREPARMS\n");
00078 dstruct(s, cparms, dwRequestMicroSecPerFrame, "lu");
00079 dstruct(s, cparms, fMakeUserHitOKToCapture, "d");
00080 dstruct(s, cparms, wPercentDropForError, "u");
00081 dstruct(s, cparms, fYield, "d");
00082 dstruct(s, cparms, dwIndexSize, "lu");
00083 dstruct(s, cparms, wChunkGranularity, "u");
00084 dstruct(s, cparms, fUsingDOSMemory, "d");
00085 dstruct(s, cparms, wNumVideoRequested, "u");
00086 dstruct(s, cparms, fCaptureAudio, "d");
00087 dstruct(s, cparms, wNumAudioRequested, "u");
00088 dstruct(s, cparms, vKeyAbort, "u");
00089 dstruct(s, cparms, fAbortLeftMouse, "d");
00090 dstruct(s, cparms, fAbortRightMouse, "d");
00091 dstruct(s, cparms, fLimitEnabled, "d");
00092 dstruct(s, cparms, wTimeLimit, "u");
00093 dstruct(s, cparms, fMCIControl, "d");
00094 dstruct(s, cparms, fStepMCIDevice, "d");
00095 dstruct(s, cparms, dwMCIStartTime, "lu");
00096 dstruct(s, cparms, dwMCIStopTime, "lu");
00097 dstruct(s, cparms, fStepCaptureAt2x, "d");
00098 dstruct(s, cparms, wStepCaptureAverageFrames, "u");
00099 dstruct(s, cparms, dwAudioBufferSize, "lu");
00100 dstruct(s, cparms, fDisableWriteCache, "d");
00101 dstruct(s, cparms, AVStreamMaster, "u");
00102 }
00103
00104 static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr)
00105 {
00106 #ifdef DEBUG_VFW
00107 av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n");
00108 dstruct(s, vhdr, lpData, "p");
00109 dstruct(s, vhdr, dwBufferLength, "lu");
00110 dstruct(s, vhdr, dwBytesUsed, "lu");
00111 dstruct(s, vhdr, dwTimeCaptured, "lu");
00112 dstruct(s, vhdr, dwUser, "lu");
00113 dstruct(s, vhdr, dwFlags, "lu");
00114 dstruct(s, vhdr, dwReserved[0], "lu");
00115 dstruct(s, vhdr, dwReserved[1], "lu");
00116 dstruct(s, vhdr, dwReserved[2], "lu");
00117 dstruct(s, vhdr, dwReserved[3], "lu");
00118 #endif
00119 }
00120
00121 static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
00122 {
00123 av_log(s, AV_LOG_DEBUG, "BITMAPINFOHEADER\n");
00124 dstruct(s, bih, biSize, "lu");
00125 dstruct(s, bih, biWidth, "ld");
00126 dstruct(s, bih, biHeight, "ld");
00127 dstruct(s, bih, biPlanes, "d");
00128 dstruct(s, bih, biBitCount, "d");
00129 dstruct(s, bih, biCompression, "lu");
00130 av_log(s, AV_LOG_DEBUG, " biCompression:\t\"%.4s\"\n",
00131 (char*) &bih->biCompression);
00132 dstruct(s, bih, biSizeImage, "lu");
00133 dstruct(s, bih, biXPelsPerMeter, "lu");
00134 dstruct(s, bih, biYPelsPerMeter, "lu");
00135 dstruct(s, bih, biClrUsed, "lu");
00136 dstruct(s, bih, biClrImportant, "lu");
00137 }
00138
00139 static int shall_we_drop(struct vfw_ctx *ctx)
00140 {
00141 AVFormatContext *s = ctx->s;
00142 const uint8_t dropscore[] = {62, 75, 87, 100};
00143 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
00144 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
00145
00146 if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
00147 av_log(ctx->s, AV_LOG_ERROR,
00148 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
00149 return 1;
00150 }
00151
00152 return 0;
00153 }
00154
00155 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
00156 {
00157 struct vfw_ctx *ctx;
00158 AVPacketList **ppktl, *pktl_next;
00159
00160 ctx = (struct vfw_ctx *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
00161
00162 dump_videohdr(ctx->s, vdhdr);
00163
00164 if(shall_we_drop(ctx))
00165 return FALSE;
00166
00167 WaitForSingleObject(ctx->mutex, INFINITE);
00168
00169 pktl_next = av_mallocz(sizeof(AVPacketList));
00170 if(!pktl_next)
00171 goto fail;
00172
00173 if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
00174 av_free(pktl_next);
00175 goto fail;
00176 }
00177
00178 pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
00179 memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
00180
00181 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
00182 *ppktl = pktl_next;
00183
00184 ctx->curbufsize += vdhdr->dwBytesUsed;
00185
00186 SetEvent(ctx->event);
00187 ReleaseMutex(ctx->mutex);
00188
00189 return TRUE;
00190 fail:
00191 ReleaseMutex(ctx->mutex);
00192 return FALSE;
00193 }
00194
00195 static int vfw_read_close(AVFormatContext *s);
00196
00197 static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
00198 {
00199 struct vfw_ctx *ctx = s->priv_data;
00200 AVCodecContext *codec;
00201 AVStream *st;
00202 int devnum;
00203 int bisize;
00204 BITMAPINFO *bi;
00205 CAPTUREPARMS cparms;
00206 DWORD biCompression;
00207 WORD biBitCount;
00208 int width;
00209 int height;
00210 int ret;
00211
00212 if(!ap->time_base.den) {
00213 av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
00214 return AVERROR_IO;
00215 }
00216
00217 ctx->s = s;
00218
00219 ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
00220 if(!ctx->hwnd) {
00221 av_log(s, AV_LOG_ERROR, "Could not create capture window.\n");
00222 return AVERROR_IO;
00223 }
00224
00225
00226 devnum = atoi(s->filename);
00227
00228 ret = SendMessage(ctx->hwnd, WM_CAP_DRIVER_CONNECT, devnum, 0);
00229 if(!ret) {
00230 av_log(s, AV_LOG_ERROR, "Could not connect to device.\n");
00231 DestroyWindow(ctx->hwnd);
00232 return AVERROR(ENODEV);
00233 }
00234
00235 SendMessage(ctx->hwnd, WM_CAP_SET_OVERLAY, 0, 0);
00236 SendMessage(ctx->hwnd, WM_CAP_SET_PREVIEW, 0, 0);
00237
00238 ret = SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0,
00239 (LPARAM) videostream_cb);
00240 if(!ret) {
00241 av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n");
00242 goto fail_io;
00243 }
00244
00245 SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) ctx);
00246
00247 st = av_new_stream(s, 0);
00248 if(!st) {
00249 vfw_read_close(s);
00250 return AVERROR_NOMEM;
00251 }
00252
00253
00254 bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
00255 if(!bisize)
00256 goto fail_io;
00257 bi = av_malloc(bisize);
00258 if(!bi) {
00259 vfw_read_close(s);
00260 return AVERROR_NOMEM;
00261 }
00262 ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
00263 if(!ret)
00264 goto fail_bi;
00265
00266 dump_bih(s, &bi->bmiHeader);
00267
00268 width = ap->width ? ap->width : bi->bmiHeader.biWidth ;
00269 height = ap->height ? ap->height : bi->bmiHeader.biHeight;
00270 bi->bmiHeader.biWidth = width ;
00271 bi->bmiHeader.biHeight = height;
00272
00273 #if 0
00274
00275
00276 bi->bmiHeader.biWidth = 320;
00277 bi->bmiHeader.biHeight = 240;
00278 bi->bmiHeader.biPlanes = 1;
00279 bi->bmiHeader.biBitCount = 12;
00280 bi->bmiHeader.biCompression = MKTAG('I','4','2','0');
00281 bi->bmiHeader.biSizeImage = 115200;
00282 dump_bih(s, &bi->bmiHeader);
00283 #endif
00284
00285 ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi);
00286 if(!ret) {
00287 av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n");
00288 goto fail_bi;
00289 }
00290
00291 biCompression = bi->bmiHeader.biCompression;
00292 biBitCount = bi->bmiHeader.biBitCount;
00293
00294 av_free(bi);
00295
00296
00297 ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
00298 (LPARAM) &cparms);
00299 if(!ret)
00300 goto fail_io;
00301
00302 dump_captureparms(s, &cparms);
00303
00304 cparms.fYield = 1;
00305 cparms.dwRequestMicroSecPerFrame =
00306 (ap->time_base.num*1000000) / ap->time_base.den;
00307 cparms.fAbortLeftMouse = 0;
00308 cparms.fAbortRightMouse = 0;
00309 cparms.fCaptureAudio = 0;
00310 cparms.vKeyAbort = 0;
00311
00312 ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
00313 (LPARAM) &cparms);
00314 if(!ret)
00315 goto fail_io;
00316
00317 codec = st->codec;
00318 codec->time_base = ap->time_base;
00319 codec->codec_type = CODEC_TYPE_VIDEO;
00320 codec->width = width;
00321 codec->height = height;
00322 codec->codec_id = CODEC_ID_RAWVIDEO;
00323 codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
00324 if(biCompression == BI_RGB)
00325 codec->bits_per_coded_sample = biBitCount;
00326
00327 av_set_pts_info(st, 32, 1, 1000);
00328
00329 if(codec->pix_fmt == -1) {
00330 av_log(s, AV_LOG_ERROR, "Unknown compression type."
00331 "Please report verbose (-v 99) debug information.\n");
00332 vfw_read_close(s);
00333 return AVERROR_PATCHWELCOME;
00334 }
00335
00336 ctx->mutex = CreateMutex(NULL, 0, NULL);
00337 if(!ctx->mutex) {
00338 av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
00339 goto fail_io;
00340 }
00341 ctx->event = CreateEvent(NULL, 1, 0, NULL);
00342 if(!ctx->event) {
00343 av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
00344 goto fail_io;
00345 }
00346
00347 ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
00348 if(!ret) {
00349 av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
00350 goto fail_io;
00351 }
00352
00353 return 0;
00354
00355 fail_bi:
00356 av_free(bi);
00357
00358 fail_io:
00359 vfw_read_close(s);
00360 return AVERROR_IO;
00361 }
00362
00363 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
00364 {
00365 struct vfw_ctx *ctx = s->priv_data;
00366 AVPacketList *pktl = NULL;
00367
00368 while(!pktl) {
00369 WaitForSingleObject(ctx->mutex, INFINITE);
00370 pktl = ctx->pktl;
00371 if(ctx->pktl) {
00372 *pkt = ctx->pktl->pkt;
00373 ctx->pktl = ctx->pktl->next;
00374 av_free(pktl);
00375 }
00376 ResetEvent(ctx->event);
00377 ReleaseMutex(ctx->mutex);
00378 if(!pktl) {
00379 if(s->flags & AVFMT_FLAG_NONBLOCK) {
00380 return AVERROR(EAGAIN);
00381 } else {
00382 WaitForSingleObject(ctx->event, INFINITE);
00383 }
00384 }
00385 }
00386
00387 ctx->curbufsize -= pkt->size;
00388
00389 return pkt->size;
00390 }
00391
00392 static int vfw_read_close(AVFormatContext *s)
00393 {
00394 struct vfw_ctx *ctx = s->priv_data;
00395
00396 if(ctx->hwnd) {
00397 SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
00398 SendMessage(ctx->hwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
00399 DestroyWindow(ctx->hwnd);
00400 }
00401 if(ctx->mutex)
00402 CloseHandle(ctx->mutex);
00403 if(ctx->event)
00404 CloseHandle(ctx->event);
00405
00406 return 0;
00407 }
00408
00409 AVInputFormat vfwcap_demuxer = {
00410 "vfwcap",
00411 NULL_IF_CONFIG_SMALL("VFW video capture"),
00412 sizeof(struct vfw_ctx),
00413 NULL,
00414 vfw_read_header,
00415 vfw_read_packet,
00416 vfw_read_close,
00417 .flags = AVFMT_NOFILE,
00418 };