]> git.sesse.net Git - ffmpeg/blob - libavdevice/dshow.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavdevice / dshow.c
1 /*
2  * Directshow capture interface
3  * Copyright (c) 2010 Ramiro Polla
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/opt.h"
23
24 #include "avdevice.h"
25 #include "dshow.h"
26
27 struct dshow_ctx {
28     const AVClass *class;
29
30     IGraphBuilder *graph;
31
32     char *device_name[2];
33
34     int   list_devices;
35
36     IBaseFilter *device_filter[2];
37     IPin        *device_pin[2];
38     libAVFilter *capture_filter[2];
39     libAVPin    *capture_pin[2];
40
41     HANDLE mutex;
42     HANDLE event;
43     AVPacketList *pktl;
44
45     unsigned int curbufsize;
46     unsigned int video_frame_num;
47
48     IMediaControl *control;
49 };
50
51 static enum PixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
52 {
53     switch(biCompression) {
54     case MKTAG('U', 'Y', 'V', 'Y'):
55         return PIX_FMT_UYVY422;
56     case MKTAG('Y', 'U', 'Y', '2'):
57         return PIX_FMT_YUYV422;
58     case MKTAG('I', '4', '2', '0'):
59         return PIX_FMT_YUV420P;
60     case BI_RGB:
61         switch(biBitCount) { /* 1-8 are untested */
62             case 1:
63                 return PIX_FMT_MONOWHITE;
64             case 4:
65                 return PIX_FMT_RGB4;
66             case 8:
67                 return PIX_FMT_RGB8;
68             case 16:
69                 return PIX_FMT_RGB555;
70             case 24:
71                 return PIX_FMT_BGR24;
72             case 32:
73                 return PIX_FMT_RGB32;
74         }
75     }
76     return PIX_FMT_NONE;
77 }
78
79 static enum CodecID dshow_codecid(DWORD biCompression)
80 {
81     switch(biCompression) {
82     case MKTAG('d', 'v', 's', 'd'):
83         return CODEC_ID_DVVIDEO;
84     case MKTAG('M', 'J', 'P', 'G'):
85     case MKTAG('m', 'j', 'p', 'g'):
86         return CODEC_ID_MJPEG;
87     }
88     return CODEC_ID_NONE;
89 }
90
91 static int
92 dshow_read_close(AVFormatContext *s)
93 {
94     struct dshow_ctx *ctx = s->priv_data;
95     AVPacketList *pktl;
96
97     if (ctx->control) {
98         IMediaControl_Stop(ctx->control);
99         IMediaControl_Release(ctx->control);
100     }
101
102     if (ctx->capture_pin[VideoDevice])
103         libAVPin_Release(ctx->capture_pin[VideoDevice]);
104     if (ctx->capture_pin[AudioDevice])
105         libAVPin_Release(ctx->capture_pin[AudioDevice]);
106     if (ctx->capture_filter[VideoDevice])
107         libAVFilter_Release(ctx->capture_filter[VideoDevice]);
108     if (ctx->capture_filter[AudioDevice])
109         libAVFilter_Release(ctx->capture_filter[AudioDevice]);
110
111     if (ctx->device_pin[VideoDevice])
112         IPin_Release(ctx->device_pin[VideoDevice]);
113     if (ctx->device_pin[AudioDevice])
114         IPin_Release(ctx->device_pin[AudioDevice]);
115     if (ctx->device_filter[VideoDevice])
116         IBaseFilter_Release(ctx->device_filter[VideoDevice]);
117     if (ctx->device_filter[AudioDevice])
118         IBaseFilter_Release(ctx->device_filter[AudioDevice]);
119
120     if (ctx->graph) {
121         IEnumFilters *fenum;
122         int r;
123         r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
124         if (r == S_OK) {
125             IBaseFilter *f;
126             IEnumFilters_Reset(fenum);
127             while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK)
128                 IGraphBuilder_RemoveFilter(ctx->graph, f);
129             IEnumFilters_Release(fenum);
130         }
131         IGraphBuilder_Release(ctx->graph);
132     }
133
134     if (ctx->device_name[0])
135         av_free(ctx->device_name[0]);
136     if (ctx->device_name[1])
137         av_free(ctx->device_name[1]);
138
139     if(ctx->mutex)
140         CloseHandle(ctx->mutex);
141     if(ctx->event)
142         CloseHandle(ctx->event);
143
144     pktl = ctx->pktl;
145     while (pktl) {
146         AVPacketList *next = pktl->next;
147         av_destruct_packet(&pktl->pkt);
148         av_free(pktl);
149         pktl = next;
150     }
151
152     return 0;
153 }
154
155 static char *dup_wchar_to_utf8(wchar_t *w)
156 {
157     char *s = NULL;
158     int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
159     s = av_malloc(l);
160     if (s)
161         WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
162     return s;
163 }
164
165 static int shall_we_drop(AVFormatContext *s)
166 {
167     struct dshow_ctx *ctx = s->priv_data;
168     const uint8_t dropscore[] = {62, 75, 87, 100};
169     const int ndropscores = FF_ARRAY_ELEMS(dropscore);
170     unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
171
172     if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
173         av_log(s, AV_LOG_ERROR,
174               "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
175         return 1;
176     }
177
178     return 0;
179 }
180
181 static void
182 callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time)
183 {
184     AVFormatContext *s = priv_data;
185     struct dshow_ctx *ctx = s->priv_data;
186     AVPacketList **ppktl, *pktl_next;
187
188 //    dump_videohdr(s, vdhdr);
189
190     if(shall_we_drop(s))
191         return;
192
193     WaitForSingleObject(ctx->mutex, INFINITE);
194
195     pktl_next = av_mallocz(sizeof(AVPacketList));
196     if(!pktl_next)
197         goto fail;
198
199     if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
200         av_free(pktl_next);
201         goto fail;
202     }
203
204     pktl_next->pkt.stream_index = index;
205     pktl_next->pkt.pts = time;
206     memcpy(pktl_next->pkt.data, buf, buf_size);
207
208     for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
209     *ppktl = pktl_next;
210
211     ctx->curbufsize += buf_size;
212
213     SetEvent(ctx->event);
214     ReleaseMutex(ctx->mutex);
215
216     return;
217 fail:
218     ReleaseMutex(ctx->mutex);
219     return;
220 }
221
222 /**
223  * Cycle through available devices using the device enumerator devenum,
224  * retrieve the device with type specified by devtype and return the
225  * pointer to the object found in *pfilter.
226  * If pfilter is NULL, list all device names.
227  */
228 static int
229 dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
230                     enum dshowDeviceType devtype, IBaseFilter **pfilter)
231 {
232     struct dshow_ctx *ctx = avctx->priv_data;
233     IBaseFilter *device_filter = NULL;
234     IEnumMoniker *classenum = NULL;
235     IMoniker *m = NULL;
236     const char *device_name = ctx->device_name[devtype];
237     int r;
238
239     const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
240                                    &CLSID_AudioInputDeviceCategory };
241     const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
242
243     r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
244                                              (IEnumMoniker **) &classenum, 0);
245     if (r != S_OK) {
246         av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
247                devtypename);
248         return AVERROR(EIO);
249     }
250
251     while (IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK && !device_filter) {
252         IPropertyBag *bag = NULL;
253         char *buf = NULL;
254         VARIANT var;
255
256         r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
257         if (r != S_OK)
258             goto fail1;
259
260         var.vt = VT_BSTR;
261         r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
262         if (r != S_OK)
263             goto fail1;
264
265         buf = dup_wchar_to_utf8(var.bstrVal);
266
267         if (pfilter) {
268             if (strcmp(device_name, buf))
269                 goto fail1;
270
271             IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
272         } else {
273             av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf);
274         }
275
276 fail1:
277         if (buf)
278             av_free(buf);
279         if (bag)
280             IPropertyBag_Release(bag);
281         IMoniker_Release(m);
282     }
283
284     IEnumMoniker_Release(classenum);
285
286     if (pfilter) {
287         if (!device_filter) {
288             av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
289                    devtypename);
290             return AVERROR(EIO);
291         }
292         *pfilter = device_filter;
293     }
294
295     return 0;
296 }
297
298 static int
299 dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
300                   enum dshowDeviceType devtype)
301 {
302     struct dshow_ctx *ctx = avctx->priv_data;
303     IBaseFilter *device_filter = NULL;
304     IGraphBuilder *graph = ctx->graph;
305     IEnumPins *pins = 0;
306     IPin *device_pin = NULL;
307     libAVPin *capture_pin = NULL;
308     libAVFilter *capture_filter = NULL;
309     int ret = AVERROR(EIO);
310     IPin *pin;
311     int r;
312
313     const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
314     const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
315     const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
316
317     if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
318         ret = r;
319         goto error;
320     }
321
322     ctx->device_filter [devtype] = device_filter;
323
324     r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
325     if (r != S_OK) {
326         av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
327         goto error;
328     }
329
330     r = IBaseFilter_EnumPins(device_filter, &pins);
331     if (r != S_OK) {
332         av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
333         goto error;
334     }
335
336     while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !device_pin) {
337         IKsPropertySet *p = NULL;
338         IEnumMediaTypes *types;
339         PIN_INFO info = {0};
340         AM_MEDIA_TYPE *type;
341         GUID category;
342         DWORD r2;
343
344         IPin_QueryPinInfo(pin, &info);
345         IBaseFilter_Release(info.pFilter);
346
347         if (info.dir != PINDIR_OUTPUT)
348             goto next;
349         if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
350             goto next;
351         if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
352                                NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
353             goto next;
354         if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
355             goto next;
356
357         if (IPin_EnumMediaTypes(pin, &types) != S_OK)
358             goto next;
359
360         IEnumMediaTypes_Reset(types);
361         while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK && !device_pin) {
362             if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
363                 device_pin = pin;
364                 goto next;
365             }
366             CoTaskMemFree(type);
367         }
368
369 next:
370         if (types)
371             IEnumMediaTypes_Release(types);
372         if (p)
373             IKsPropertySet_Release(p);
374         if (device_pin != pin)
375             IPin_Release(pin);
376     }
377
378     if (!device_pin) {
379         av_log(avctx, AV_LOG_ERROR,
380                "Could not find output pin from %s capture device.\n", devtypename);
381         goto error;
382     }
383     ctx->device_pin[devtype] = device_pin;
384
385     capture_filter = libAVFilter_Create(avctx, callback, devtype);
386     if (!capture_filter) {
387         av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
388         goto error;
389     }
390     ctx->capture_filter[devtype] = capture_filter;
391
392     r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
393                                 filter_name[devtype]);
394     if (r != S_OK) {
395         av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
396         goto error;
397     }
398
399     libAVPin_AddRef(capture_filter->pin);
400     capture_pin = capture_filter->pin;
401     ctx->capture_pin[devtype] = capture_pin;
402
403     r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
404     if (r != S_OK) {
405         av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
406         goto error;
407     }
408
409     ret = 0;
410
411 error:
412     if (pins)
413         IEnumPins_Release(pins);
414
415     return ret;
416 }
417
418 static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
419 {
420     switch (sample_fmt) {
421     case AV_SAMPLE_FMT_U8:  return CODEC_ID_PCM_U8;
422     case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE;
423     case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE;
424     default:                return CODEC_ID_NONE; /* Should never happen. */
425     }
426 }
427
428 static enum SampleFormat sample_fmt_bits_per_sample(int bits)
429 {
430     switch (bits) {
431     case 8:  return AV_SAMPLE_FMT_U8;
432     case 16: return AV_SAMPLE_FMT_S16;
433     case 32: return AV_SAMPLE_FMT_S32;
434     default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
435     }
436 }
437
438 static int
439 dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap,
440                  enum dshowDeviceType devtype)
441 {
442     struct dshow_ctx *ctx = avctx->priv_data;
443     AM_MEDIA_TYPE type;
444     AVCodecContext *codec;
445     AVStream *st;
446     int ret = AVERROR(EIO);
447
448     st = av_new_stream(avctx, devtype);
449     if (!st) {
450         ret = AVERROR(ENOMEM);
451         goto error;
452     }
453
454     ctx->capture_filter[devtype]->stream_index = st->index;
455
456     libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
457
458     codec = st->codec;
459     if (devtype == VideoDevice) {
460         BITMAPINFOHEADER *bih = NULL;
461
462         if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
463             VIDEOINFOHEADER *v = (void *) type.pbFormat;
464             bih = &v->bmiHeader;
465         } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
466             VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
467             bih = &v->bmiHeader;
468         }
469         if (!bih) {
470             av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
471             goto error;
472         }
473
474         codec->time_base  = ap->time_base;
475         codec->codec_type = AVMEDIA_TYPE_VIDEO;
476         codec->width      = bih->biWidth;
477         codec->height     = bih->biHeight;
478         codec->pix_fmt    = dshow_pixfmt(bih->biCompression, bih->biBitCount);
479         if (codec->pix_fmt == PIX_FMT_NONE) {
480             codec->codec_id = dshow_codecid(bih->biCompression);
481             if (codec->codec_id == CODEC_ID_NONE) {
482                 av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
483                                  "Please report verbose (-v 9) debug information.\n");
484                 dshow_read_close(avctx);
485                 return AVERROR_PATCHWELCOME;
486             }
487             codec->bits_per_coded_sample = bih->biBitCount;
488         } else {
489             codec->codec_id = CODEC_ID_RAWVIDEO;
490             if (bih->biCompression == BI_RGB) {
491                 codec->bits_per_coded_sample = bih->biBitCount;
492                 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
493                 if (codec->extradata) {
494                     codec->extradata_size = 9;
495                     memcpy(codec->extradata, "BottomUp", 9);
496                 }
497             }
498         }
499     } else {
500         WAVEFORMATEX *fx = NULL;
501
502         if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
503             fx = (void *) type.pbFormat;
504         }
505         if (!fx) {
506             av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
507             goto error;
508         }
509
510         codec->codec_type  = AVMEDIA_TYPE_AUDIO;
511         codec->sample_fmt  = sample_fmt_bits_per_sample(fx->wBitsPerSample);
512         codec->codec_id    = waveform_codec_id(codec->sample_fmt);
513         codec->sample_rate = fx->nSamplesPerSec;
514         codec->channels    = fx->nChannels;
515     }
516
517     av_set_pts_info(st, 64, 1, 10000000);
518
519     ret = 0;
520
521 error:
522     return ret;
523 }
524
525 static int parse_device_name(AVFormatContext *avctx)
526 {
527     struct dshow_ctx *ctx = avctx->priv_data;
528     char **device_name = ctx->device_name;
529     char *name = av_strdup(avctx->filename);
530     char *tmp = name;
531     int ret = 1;
532     char *type;
533
534     while ((type = strtok(tmp, "="))) {
535         char *token = strtok(NULL, ":");
536         tmp = NULL;
537
538         if        (!strcmp(type, "video")) {
539             device_name[0] = token;
540         } else if (!strcmp(type, "audio")) {
541             device_name[1] = token;
542         } else {
543             device_name[0] = NULL;
544             device_name[1] = NULL;
545             break;
546         }
547     }
548
549     if (!device_name[0] && !device_name[1]) {
550         ret = 0;
551     } else {
552         if (device_name[0])
553             device_name[0] = av_strdup(device_name[0]);
554         if (device_name[1])
555             device_name[1] = av_strdup(device_name[1]);
556     }
557
558     av_free(name);
559     return ret;
560 }
561
562 static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap)
563 {
564     struct dshow_ctx *ctx = avctx->priv_data;
565     IGraphBuilder *graph = NULL;
566     ICreateDevEnum *devenum = NULL;
567     IMediaControl *control = NULL;
568     int ret = AVERROR(EIO);
569     int r;
570
571     if (!ctx->list_devices && !parse_device_name(avctx)) {
572         av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
573         goto error;
574     }
575
576     CoInitialize(0);
577
578     r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
579                          &IID_IGraphBuilder, (void **) &graph);
580     if (r != S_OK) {
581         av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
582         goto error;
583     }
584     ctx->graph = graph;
585
586     r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
587                          &IID_ICreateDevEnum, (void **) &devenum);
588     if (r != S_OK) {
589         av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
590         goto error;
591     }
592
593     if (ctx->list_devices) {
594         av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
595         dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
596         av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
597         dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
598         ret = AVERROR_EXIT;
599         goto error;
600     }
601
602     if (ctx->device_name[VideoDevice]) {
603         ret = dshow_open_device(avctx, devenum, VideoDevice);
604         if (ret < 0)
605             goto error;
606         ret = dshow_add_device(avctx, ap, VideoDevice);
607         if (ret < 0)
608             goto error;
609     }
610     if (ctx->device_name[AudioDevice]) {
611         ret = dshow_open_device(avctx, devenum, AudioDevice);
612         if (ret < 0)
613             goto error;
614         ret = dshow_add_device(avctx, ap, AudioDevice);
615         if (ret < 0)
616             goto error;
617     }
618
619     ctx->mutex = CreateMutex(NULL, 0, NULL);
620     if (!ctx->mutex) {
621         av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
622         goto error;
623     }
624     ctx->event = CreateEvent(NULL, 1, 0, NULL);
625     if (!ctx->event) {
626         av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
627         goto error;
628     }
629
630     r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
631     if (r != S_OK) {
632         av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
633         goto error;
634     }
635     ctx->control = control;
636
637     r = IMediaControl_Run(control);
638     if (r == S_FALSE) {
639         OAFilterState pfs;
640         r = IMediaControl_GetState(control, 0, &pfs);
641     }
642     if (r != S_OK) {
643         av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
644         goto error;
645     }
646
647     ret = 0;
648
649 error:
650
651     if (ret < 0)
652         dshow_read_close(avctx);
653
654     if (devenum)
655         ICreateDevEnum_Release(devenum);
656
657     return ret;
658 }
659
660 static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
661 {
662     struct dshow_ctx *ctx = s->priv_data;
663     AVPacketList *pktl = NULL;
664
665     while (!pktl) {
666         WaitForSingleObject(ctx->mutex, INFINITE);
667         pktl = ctx->pktl;
668         if (ctx->pktl) {
669             *pkt = ctx->pktl->pkt;
670             ctx->pktl = ctx->pktl->next;
671             av_free(pktl);
672         }
673         ResetEvent(ctx->event);
674         ReleaseMutex(ctx->mutex);
675         if (!pktl) {
676             if (s->flags & AVFMT_FLAG_NONBLOCK) {
677                 return AVERROR(EAGAIN);
678             } else {
679                 WaitForSingleObject(ctx->event, INFINITE);
680             }
681         }
682     }
683
684     ctx->curbufsize -= pkt->size;
685
686     return pkt->size;
687 }
688
689 #define OFFSET(x) offsetof(struct dshow_ctx, x)
690 #define DEC AV_OPT_FLAG_DECODING_PARAM
691 static const AVOption options[] = {
692     { "list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_devices" },
693     { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_devices" },
694     { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_devices" },
695     { NULL },
696 };
697
698 static const AVClass dshow_class = {
699     .class_name = "DirectShow indev",
700     .item_name  = av_default_item_name,
701     .option     = options,
702     .version    = LIBAVUTIL_VERSION_INT,
703 };
704
705 AVInputFormat ff_dshow_demuxer = {
706     "dshow",
707     NULL_IF_CONFIG_SMALL("DirectShow capture"),
708     sizeof(struct dshow_ctx),
709     NULL,
710     dshow_read_header,
711     dshow_read_packet,
712     dshow_read_close,
713     .flags = AVFMT_NOFILE,
714     .priv_class = &dshow_class,
715 };