• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavdevice/vfwcap.c

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

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1