]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/vfwcap.c
Port pred8x8l_top_dc_mmxext/ssse3 (H.264 intra prediction) from x264 to FFmpeg.
[ffmpeg] / libavdevice / vfwcap.c
index 1ab5548cef70236184642faf6c171cc2abd1fb8c..7b54490fbb55b7b34390c77b0315a6aa5bbbe25f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * VFW capture interface
- * Copyright (c) 2006-2008 Ramiro Polla.
+ * Copyright (c) 2006-2008 Ramiro Polla
  *
  * This file is part of FFmpeg.
  *
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "avformat.h"
-#include <vfw.h>
+#include "libavformat/avformat.h"
 #include <windows.h>
+#include <vfw.h>
 
 //#define DEBUG_VFW
 
 /* Defines for VFW missing from MinGW.
  * Remove this when MinGW incorporates them. */
-#define WM_CAP_START                (0x0400)
-#define WM_CAP_SET_CALLBACK_VIDEOSTREAM (WM_CAP_START + 6)
-#define WM_CAP_DRIVER_CONNECT       (WM_CAP_START + 10)
-#define WM_CAP_DRIVER_DISCONNECT    (WM_CAP_START + 11)
-#define WM_CAP_GET_VIDEOFORMAT      (WM_CAP_START + 44)
-#define WM_CAP_SET_VIDEOFORMAT      (WM_CAP_START + 45)
-#define WM_CAP_SET_PREVIEW          (WM_CAP_START + 50)
-#define WM_CAP_SET_OVERLAY          (WM_CAP_START + 51)
-#define WM_CAP_SEQUENCE_NOFILE      (WM_CAP_START + 63)
-#define WM_CAP_SET_SEQUENCE_SETUP   (WM_CAP_START + 64)
-#define WM_CAP_GET_SEQUENCE_SETUP   (WM_CAP_START + 65)
-
 #define HWND_MESSAGE                ((HWND)-3)
 
 #define BI_RGB                      0
 
-typedef struct videohdr_tag {
-    LPBYTE      lpData;
-    DWORD       dwBufferLength;
-    DWORD       dwBytesUsed;
-    DWORD       dwTimeCaptured;
-    DWORD       dwUser;
-    DWORD       dwFlags;
-    DWORD_PTR   dwReserved[4];
-} VIDEOHDR, NEAR *PVIDEOHDR, FAR * LPVIDEOHDR;
-
-typedef struct {
-    DWORD dwRequestMicroSecPerFrame;
-    BOOL  fMakeUserHitOKToCapture;
-    UINT  wPercentDropForError;
-    BOOL  fYield;
-    DWORD dwIndexSize;
-    UINT  wChunkGranularity;
-    BOOL  fUsingDOSMemory;
-    UINT  wNumVideoRequested;
-    BOOL  fCaptureAudio;
-    UINT  wNumAudioRequested;
-    UINT  vKeyAbort;
-    BOOL  fAbortLeftMouse;
-    BOOL  fAbortRightMouse;
-    BOOL  fLimitEnabled;
-    UINT  wTimeLimit;
-    BOOL  fMCIControl;
-    BOOL  fStepMCIDevice;
-    DWORD dwMCIStartTime;
-    DWORD dwMCIStopTime;
-    BOOL  fStepCaptureAt2x;
-    UINT  wStepCaptureAverageFrames;
-    DWORD dwAudioBufferSize;
-    BOOL  fDisableWriteCache;
-    UINT  AVStreamMaster;
-} CAPTUREPARMS;
 /* End of missing MinGW defines */
 
 struct vfw_ctx {
@@ -86,7 +38,6 @@ struct vfw_ctx {
     HANDLE mutex;
     HANDLE event;
     AVPacketList *pktl;
-    AVFormatContext *s;
     unsigned int curbufsize;
     unsigned int frame_num;
 };
@@ -94,8 +45,12 @@ struct vfw_ctx {
 static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
 {
     switch(biCompression) {
+    case MKTAG('U', 'Y', 'V', 'Y'):
+        return PIX_FMT_UYVY422;
     case MKTAG('Y', 'U', 'Y', '2'):
         return PIX_FMT_YUYV422;
+    case MKTAG('I', '4', '2', '0'):
+        return PIX_FMT_YUV420P;
     case BI_RGB:
         switch(biBitCount) { /* 1-8 are untested */
             case 1:
@@ -112,7 +67,19 @@ static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount)
                 return PIX_FMT_RGB32;
         }
     }
-    return -1;
+    return PIX_FMT_NONE;
+}
+
+static enum CodecID vfw_codecid(DWORD biCompression)
+{
+    switch(biCompression) {
+    case MKTAG('d', 'v', 's', 'd'):
+        return CODEC_ID_DVVIDEO;
+    case MKTAG('M', 'J', 'P', 'G'):
+    case MKTAG('m', 'j', 'p', 'g'):
+        return CODEC_ID_MJPEG;
+    }
+    return CODEC_ID_NONE;
 }
 
 #define dstruct(pctx, sname, var, type) \
@@ -182,15 +149,15 @@ static void dump_bih(AVFormatContext *s, BITMAPINFOHEADER *bih)
     dstruct(s, bih, biClrImportant, "lu");
 }
 
-static int shall_we_drop(struct vfw_ctx *ctx)
+static int shall_we_drop(AVFormatContext *s)
 {
-    AVFormatContext *s = ctx->s;
+    struct vfw_ctx *ctx = s->priv_data;
     const uint8_t dropscore[] = {62, 75, 87, 100};
-    const int ndropscores = sizeof(dropscore)/sizeof(dropscore[0]);
+    const int ndropscores = FF_ARRAY_ELEMS(dropscore);
     unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
 
     if(dropscore[++ctx->frame_num%ndropscores] <= buffer_fullness) {
-        av_log(ctx->s, AV_LOG_ERROR,
+        av_log(s, AV_LOG_ERROR,
               "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
         return 1;
     }
@@ -200,14 +167,16 @@ static int shall_we_drop(struct vfw_ctx *ctx)
 
 static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
 {
+    AVFormatContext *s;
     struct vfw_ctx *ctx;
     AVPacketList **ppktl, *pktl_next;
 
-    ctx = (struct vfw_ctx *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
+    s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
+    ctx = s->priv_data;
 
-    dump_videohdr(ctx->s, vdhdr);
+    dump_videohdr(s, vdhdr);
 
-    if(shall_we_drop(ctx))
+    if(shall_we_drop(s))
         return FALSE;
 
     WaitForSingleObject(ctx->mutex, INFINITE);
@@ -238,7 +207,31 @@ fail:
     return FALSE;
 }
 
-static int vfw_read_close(AVFormatContext *s);
+static int vfw_read_close(AVFormatContext *s)
+{
+    struct vfw_ctx *ctx = s->priv_data;
+    AVPacketList *pktl;
+
+    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);
+
+    pktl = ctx->pktl;
+    while (pktl) {
+        AVPacketList *next = pktl->next;
+        av_destruct_packet(&pktl->pkt);
+        av_free(pktl);
+        pktl = next;
+    }
+
+    return 0;
+}
 
 static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
 {
@@ -255,17 +248,31 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
     int height;
     int ret;
 
+    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);
+    }
+
     if(!ap->time_base.den) {
         av_log(s, AV_LOG_ERROR, "A time base must be specified.\n");
-        return AVERROR_IO;
+        return AVERROR(EIO);
     }
 
-    ctx->s = s;
-
     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");
-        return AVERROR_IO;
+        return AVERROR(EIO);
     }
 
     /* If atoi fails, devnum==0 and the default device is used */
@@ -288,28 +295,26 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
         goto fail_io;
     }
 
-    SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) ctx);
+    SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
 
     st = av_new_stream(s, 0);
     if(!st) {
         vfw_read_close(s);
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
     }
 
     /* Set video format */
     bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0);
-    if(!bisize) {
+    if(!bisize)
         goto fail_io;
-    }
     bi = av_malloc(bisize);
     if(!bi) {
         vfw_read_close(s);
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
     }
     ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi);
-    if(!ret) {
+    if(!ret)
         goto fail_bi;
-    }
 
     dump_bih(s, &bi->bmiHeader);
 
@@ -318,6 +323,18 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
     bi->bmiHeader.biWidth  = width ;
     bi->bmiHeader.biHeight = height;
 
+    if (0) {
+        /* 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);
+    }
+
     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");
@@ -332,9 +349,8 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
     /* Set sequence setup */
     ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms),
                       (LPARAM) &cparms);
-    if(!ret) {
+    if(!ret)
         goto fail_io;
-    }
 
     dump_captureparms(s, &cparms);
 
@@ -348,29 +364,38 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
     ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms),
                       (LPARAM) &cparms);
-    if(!ret) {
+    if(!ret)
         goto fail_io;
-    }
 
     codec = st->codec;
     codec->time_base = ap->time_base;
-    codec->codec_type = CODEC_TYPE_VIDEO;
+    codec->codec_type = AVMEDIA_TYPE_VIDEO;
     codec->width = width;
     codec->height = height;
-    codec->codec_id = CODEC_ID_RAWVIDEO;
     codec->pix_fmt = vfw_pixfmt(biCompression, biBitCount);
-    if(biCompression == BI_RGB)
-        codec->bits_per_sample = biBitCount;
+    if(codec->pix_fmt == PIX_FMT_NONE) {
+        codec->codec_id = vfw_codecid(biCompression);
+        if(codec->codec_id == CODEC_ID_NONE) {
+            av_log(s, AV_LOG_ERROR, "Unknown compression type. "
+                             "Please report verbose (-v 9) debug information.\n");
+            vfw_read_close(s);
+            return AVERROR_PATCHWELCOME;
+        }
+        codec->bits_per_coded_sample = biBitCount;
+    } else {
+        codec->codec_id = CODEC_ID_RAWVIDEO;
+        if(biCompression == BI_RGB) {
+            codec->bits_per_coded_sample = biBitCount;
+            codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (codec->extradata) {
+                codec->extradata_size = 9;
+                memcpy(codec->extradata, "BottomUp", 9);
+            }
+        }
+    }
 
     av_set_pts_info(st, 32, 1, 1000);
 
-    if(codec->pix_fmt == -1) {
-        av_log(s, AV_LOG_ERROR, "Unknown compression type."
-                         "Please report verbose (-v 99) debug information.\n");
-        vfw_read_close(s);
-        return AVERROR_PATCHWELCOME;
-    }
-
     ctx->mutex = CreateMutex(NULL, 0, NULL);
     if(!ctx->mutex) {
         av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" );
@@ -395,7 +420,7 @@ fail_bi:
 
 fail_io:
     vfw_read_close(s);
-    return AVERROR_IO;
+    return AVERROR(EIO);
 }
 
 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
@@ -427,26 +452,9 @@ static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
     return pkt->size;
 }
 
-static int vfw_read_close(AVFormatContext *s)
-{
-    struct vfw_ctx *ctx = s->priv_data;
-
-    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);
-
-    return 0;
-}
-
 AVInputFormat vfwcap_demuxer = {
     "vfwcap",
-    "VFW video capture",
+    NULL_IF_CONFIG_SMALL("VFW video capture"),
     sizeof(struct vfw_ctx),
     NULL,
     vfw_read_header,