libavdevice/vfwcap.c
c2345207
 /*
  * VFW capture interface
406792e7
  * Copyright (c) 2006-2008 Ramiro Polla
c2345207
  *
  * 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
  */
 
c3f9ebf7
 #include "libavformat/internal.h"
3e15ea21
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
c2345207
 #include <windows.h>
420755dd
 #include <vfw.h>
6b899e16
 #include "avdevice.h"
c2345207
 
 /* Defines for VFW missing from MinGW.
  * Remove this when MinGW incorporates them. */
 #define HWND_MESSAGE                ((HWND)-3)
 
 /* End of missing MinGW defines */
 
 struct vfw_ctx {
3e15ea21
     const AVClass *class;
c2345207
     HWND hwnd;
     HANDLE mutex;
     HANDLE event;
     AVPacketList *pktl;
     unsigned int curbufsize;
     unsigned int frame_num;
3e15ea21
     char *video_size;       /**< A string describing video size, set by a private option. */
4078ed26
     char *framerate;        /**< Set by a private option. */
c2345207
 };
 
716d413c
 static enum AVPixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
c2345207
 {
     switch(biCompression) {
e24080f0
     case MKTAG('U', 'Y', 'V', 'Y'):
716d413c
         return AV_PIX_FMT_UYVY422;
c2345207
     case MKTAG('Y', 'U', 'Y', '2'):
716d413c
         return AV_PIX_FMT_YUYV422;
23ef6da8
     case MKTAG('I', '4', '2', '0'):
716d413c
         return AV_PIX_FMT_YUV420P;
c2345207
     case BI_RGB:
         switch(biBitCount) { /* 1-8 are untested */
             case 1:
716d413c
                 return AV_PIX_FMT_MONOWHITE;
c2345207
             case 4:
716d413c
                 return AV_PIX_FMT_RGB4;
c2345207
             case 8:
716d413c
                 return AV_PIX_FMT_RGB8;
c2345207
             case 16:
716d413c
                 return AV_PIX_FMT_RGB555;
c2345207
             case 24:
716d413c
                 return AV_PIX_FMT_BGR24;
c2345207
             case 32:
716d413c
                 return AV_PIX_FMT_RGB32;
c2345207
         }
     }
716d413c
     return AV_PIX_FMT_NONE;
c2345207
 }
 
36ef5369
 static enum AVCodecID vfw_codecid(DWORD biCompression)
1c0b9215
 {
     switch(biCompression) {
     case MKTAG('d', 'v', 's', 'd'):
36ef5369
         return AV_CODEC_ID_DVVIDEO;
d7a4961e
     case MKTAG('M', 'J', 'P', 'G'):
     case MKTAG('m', 'j', 'p', 'g'):
36ef5369
         return AV_CODEC_ID_MJPEG;
1c0b9215
     }
36ef5369
     return AV_CODEC_ID_NONE;
1c0b9215
 }
 
c2345207
 #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)
 {
f190f676
 #ifdef DEBUG
c2345207
     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");
 }
 
ce03ed4d
 static int shall_we_drop(AVFormatContext *s)
c2345207
 {
ce03ed4d
     struct vfw_ctx *ctx = s->priv_data;
c2345207
     const uint8_t dropscore[] = {62, 75, 87, 100};
37d3e066
     const int ndropscores = FF_ARRAY_ELEMS(dropscore);
c2345207
     unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
 
d76c3e07
     if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
ce03ed4d
         av_log(s, AV_LOG_ERROR,
c2345207
               "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
         return 1;
     }
 
     return 0;
 }
 
 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
 {
ce03ed4d
     AVFormatContext *s;
c2345207
     struct vfw_ctx *ctx;
     AVPacketList **ppktl, *pktl_next;
 
ce03ed4d
     s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
     ctx = s->priv_data;
c2345207
 
ce03ed4d
     dump_videohdr(s, vdhdr);
c2345207
 
ce03ed4d
     if(shall_we_drop(s))
c2345207
         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;
 }
 
c959c6ee
 static int vfw_read_close(AVFormatContext *s)
 {
     struct vfw_ctx *ctx = s->priv_data;
ad8eede7
     AVPacketList *pktl;
c959c6ee
 
     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);
 
ad8eede7
     pktl = ctx->pktl;
     while (pktl) {
         AVPacketList *next = pktl->next;
         av_destruct_packet(&pktl->pkt);
         av_free(pktl);
         pktl = next;
     }
 
c959c6ee
     return 0;
 }
c2345207
 
6e9651d1
 static int vfw_read_header(AVFormatContext *s)
c2345207
 {
     struct vfw_ctx *ctx = s->priv_data;
     AVCodecContext *codec;
     AVStream *st;
     int devnum;
     int bisize;
177aec12
     BITMAPINFO *bi = NULL;
c2345207
     CAPTUREPARMS cparms;
     DWORD biCompression;
     WORD biBitCount;
     int ret;
a4bda405
     AVRational framerate_q;
c2345207
 
1973e101
     if (!strcmp(s->filename, "list")) {
         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);
     }
 
c2345207
     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");
5ae092ee
         return AVERROR(EIO);
c2345207
     }
 
     /* If atoi fails, devnum==0 and the default device is used */
     devnum = atoi(s->filename);
 
     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");
177aec12
         goto fail;
c2345207
     }
 
ce03ed4d
     SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
c2345207
 
3b3bbdd3
     st = avformat_new_stream(s, NULL);
c2345207
     if(!st) {
         vfw_read_close(s);
2874c81c
         return AVERROR(ENOMEM);
c2345207
     }
 
     /* Set video format */
     bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
e4d286f1
     if(!bisize)
177aec12
         goto fail;
c2345207
     bi = av_malloc(bisize);
     if(!bi) {
         vfw_read_close(s);
2874c81c
         return AVERROR(ENOMEM);
c2345207
     }
     ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
e4d286f1
     if(!ret)
177aec12
         goto fail;
c2345207
 
     dump_bih(s, &bi->bmiHeader);
 
0a333001
     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;
     }
3e15ea21
 
     if (ctx->video_size) {
         ret = av_parse_video_size(&bi->bmiHeader.biWidth, &bi->bmiHeader.biHeight, ctx->video_size);
         if (ret < 0) {
             av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
177aec12
             goto fail;
3e15ea21
         }
     }
c2345207
 
a9decf00
     if (0) {
315fa33f
         /* 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);
a9decf00
     }
cada0327
 
c2345207
     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");
177aec12
         goto fail;
c2345207
     }
 
     biCompression = bi->bmiHeader.biCompression;
     biBitCount = bi->bmiHeader.biBitCount;
 
     /* Set sequence setup */
     ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
                       (LPARAM) &cparms);
e4d286f1
     if(!ret)
177aec12
         goto fail;
c2345207
 
     dump_captureparms(s, &cparms);
 
     cparms.fYield = 1; // Spawn a background thread
     cparms.dwRequestMicroSecPerFrame =
a4bda405
                                (framerate_q.den*1000000) / framerate_q.num;
c2345207
     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);
e4d286f1
     if(!ret)
177aec12
         goto fail;
c2345207
 
     codec = st->codec;
16dc5f20
     codec->time_base = av_inv_q(framerate_q);
72415b2a
     codec->codec_type = AVMEDIA_TYPE_VIDEO;
3e15ea21
     codec->width  = bi->bmiHeader.biWidth;
     codec->height = bi->bmiHeader.biHeight;
c2345207
     codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
716d413c
     if(codec->pix_fmt == AV_PIX_FMT_NONE) {
1c0b9215
         codec->codec_id = vfw_codecid(biCompression);
36ef5369
         if(codec->codec_id == AV_CODEC_ID_NONE) {
155b0f00
             av_log(s, AV_LOG_ERROR, "Unknown compression type. "
                              "Please report verbose (-v 9) debug information.\n");
             vfw_read_close(s);
             return AVERROR_PATCHWELCOME;
1c0b9215
         }
         codec->bits_per_coded_sample = biBitCount;
155b0f00
     } else {
36ef5369
         codec->codec_id = AV_CODEC_ID_RAWVIDEO;
faa2989e
         if(biCompression == BI_RGB) {
155b0f00
             codec->bits_per_coded_sample = biBitCount;
faa2989e
             codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
             if (codec->extradata) {
                 codec->extradata_size = 9;
                 memcpy(codec->extradata, "BottomUp", 9);
             }
         }
1c0b9215
     }
42477de5
 
177aec12
     av_freep(&bi);
 
c3f9ebf7
     avpriv_set_pts_info(st, 32, 1, 1000);
c2345207
 
     ctx->mutex = CreateMutex(NULL, 0, NULL);
     if(!ctx->mutex) {
         av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
177aec12
         goto fail;
c2345207
     }
     ctx->event = CreateEvent(NULL, 1, 0, NULL);
     if(!ctx->event) {
         av_log(s, AV_LOG_ERROR, "Could not create Event.\n" );
177aec12
         goto fail;
c2345207
     }
 
     ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0);
     if(!ret) {
         av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" );
177aec12
         goto fail;
c2345207
     }
 
     return 0;
76c2662b
 
177aec12
 fail:
     av_freep(&bi);
76c2662b
     vfw_read_close(s);
5ae092ee
     return AVERROR(EIO);
c2345207
 }
 
 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;
 }
 
3e15ea21
 #define OFFSET(x) offsetof(struct vfw_ctx, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
145f741e
     { "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 },
3e15ea21
     { NULL },
 };
 
 static const AVClass vfw_class = {
     .class_name = "VFW indev",
     .item_name  = av_default_item_name,
     .option     = options,
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
66355be3
 AVInputFormat ff_vfwcap_demuxer = {
30b4ee79
     .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,
c2345207
 };