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