]> git.sesse.net Git - ffmpeg/blob - libavdevice/dshow.c
dshow: initialize variable to prevent releasing random data
[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 /**
299  * Cycle through available pins using the device_filter device, of type
300  * devtype, retrieve the first output pin and return the pointer to the
301  * object found in *ppin.
302  */
303 static int
304 dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
305                  IBaseFilter *device_filter, IPin **ppin)
306 {
307     IEnumPins *pins = 0;
308     IPin *device_pin = NULL;
309     IPin *pin;
310     int r;
311
312     const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
313     const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
314
315     r = IBaseFilter_EnumPins(device_filter, &pins);
316     if (r != S_OK) {
317         av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
318         return AVERROR(EIO);
319     }
320
321     while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !device_pin) {
322         IKsPropertySet *p = NULL;
323         IEnumMediaTypes *types = NULL;
324         PIN_INFO info = {0};
325         AM_MEDIA_TYPE *type;
326         GUID category;
327         DWORD r2;
328
329         IPin_QueryPinInfo(pin, &info);
330         IBaseFilter_Release(info.pFilter);
331
332         if (info.dir != PINDIR_OUTPUT)
333             goto next;
334         if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
335             goto next;
336         if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
337                                NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
338             goto next;
339         if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
340             goto next;
341
342         if (IPin_EnumMediaTypes(pin, &types) != S_OK)
343             goto next;
344
345         IEnumMediaTypes_Reset(types);
346         while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK && !device_pin) {
347             if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
348                 device_pin = pin;
349                 goto next;
350             }
351             CoTaskMemFree(type);
352         }
353
354 next:
355         if (types)
356             IEnumMediaTypes_Release(types);
357         if (p)
358             IKsPropertySet_Release(p);
359         if (device_pin != pin)
360             IPin_Release(pin);
361     }
362
363     IEnumPins_Release(pins);
364
365     if (!device_pin) {
366         av_log(avctx, AV_LOG_ERROR,
367                "Could not find output pin from %s capture device.\n", devtypename);
368         return AVERROR(EIO);
369     }
370     *ppin = device_pin;
371
372     return 0;
373 }
374
375 static int
376 dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
377                   enum dshowDeviceType devtype)
378 {
379     struct dshow_ctx *ctx = avctx->priv_data;
380     IBaseFilter *device_filter = NULL;
381     IGraphBuilder *graph = ctx->graph;
382     IPin *device_pin = NULL;
383     libAVPin *capture_pin = NULL;
384     libAVFilter *capture_filter = NULL;
385     int ret = AVERROR(EIO);
386     int r;
387
388     const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
389
390     if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
391         ret = r;
392         goto error;
393     }
394
395     ctx->device_filter [devtype] = device_filter;
396
397     r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
398     if (r != S_OK) {
399         av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
400         goto error;
401     }
402
403     if ((r = dshow_cycle_pins(avctx, devtype, device_filter, &device_pin)) < 0) {
404         ret = r;
405         goto error;
406     }
407     ctx->device_pin[devtype] = device_pin;
408
409     capture_filter = libAVFilter_Create(avctx, callback, devtype);
410     if (!capture_filter) {
411         av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
412         goto error;
413     }
414     ctx->capture_filter[devtype] = capture_filter;
415
416     r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
417                                 filter_name[devtype]);
418     if (r != S_OK) {
419         av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
420         goto error;
421     }
422
423     libAVPin_AddRef(capture_filter->pin);
424     capture_pin = capture_filter->pin;
425     ctx->capture_pin[devtype] = capture_pin;
426
427     r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
428     if (r != S_OK) {
429         av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
430         goto error;
431     }
432
433     ret = 0;
434
435 error:
436     return ret;
437 }
438
439 static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
440 {
441     switch (sample_fmt) {
442     case AV_SAMPLE_FMT_U8:  return CODEC_ID_PCM_U8;
443     case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE;
444     case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE;
445     default:                return CODEC_ID_NONE; /* Should never happen. */
446     }
447 }
448
449 static enum SampleFormat sample_fmt_bits_per_sample(int bits)
450 {
451     switch (bits) {
452     case 8:  return AV_SAMPLE_FMT_U8;
453     case 16: return AV_SAMPLE_FMT_S16;
454     case 32: return AV_SAMPLE_FMT_S32;
455     default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
456     }
457 }
458
459 static int
460 dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap,
461                  enum dshowDeviceType devtype)
462 {
463     struct dshow_ctx *ctx = avctx->priv_data;
464     AM_MEDIA_TYPE type;
465     AVCodecContext *codec;
466     AVStream *st;
467     int ret = AVERROR(EIO);
468
469     st = av_new_stream(avctx, devtype);
470     if (!st) {
471         ret = AVERROR(ENOMEM);
472         goto error;
473     }
474
475     ctx->capture_filter[devtype]->stream_index = st->index;
476
477     libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
478
479     codec = st->codec;
480     if (devtype == VideoDevice) {
481         BITMAPINFOHEADER *bih = NULL;
482
483         if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
484             VIDEOINFOHEADER *v = (void *) type.pbFormat;
485             bih = &v->bmiHeader;
486         } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
487             VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
488             bih = &v->bmiHeader;
489         }
490         if (!bih) {
491             av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
492             goto error;
493         }
494
495         codec->time_base  = ap->time_base;
496         codec->codec_type = AVMEDIA_TYPE_VIDEO;
497         codec->width      = bih->biWidth;
498         codec->height     = bih->biHeight;
499         codec->pix_fmt    = dshow_pixfmt(bih->biCompression, bih->biBitCount);
500         if (codec->pix_fmt == PIX_FMT_NONE) {
501             codec->codec_id = dshow_codecid(bih->biCompression);
502             if (codec->codec_id == CODEC_ID_NONE) {
503                 av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
504                                  "Please report verbose (-v 9) debug information.\n");
505                 dshow_read_close(avctx);
506                 return AVERROR_PATCHWELCOME;
507             }
508             codec->bits_per_coded_sample = bih->biBitCount;
509         } else {
510             codec->codec_id = CODEC_ID_RAWVIDEO;
511             if (bih->biCompression == BI_RGB) {
512                 codec->bits_per_coded_sample = bih->biBitCount;
513                 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
514                 if (codec->extradata) {
515                     codec->extradata_size = 9;
516                     memcpy(codec->extradata, "BottomUp", 9);
517                 }
518             }
519         }
520     } else {
521         WAVEFORMATEX *fx = NULL;
522
523         if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
524             fx = (void *) type.pbFormat;
525         }
526         if (!fx) {
527             av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
528             goto error;
529         }
530
531         codec->codec_type  = AVMEDIA_TYPE_AUDIO;
532         codec->sample_fmt  = sample_fmt_bits_per_sample(fx->wBitsPerSample);
533         codec->codec_id    = waveform_codec_id(codec->sample_fmt);
534         codec->sample_rate = fx->nSamplesPerSec;
535         codec->channels    = fx->nChannels;
536     }
537
538     av_set_pts_info(st, 64, 1, 10000000);
539
540     ret = 0;
541
542 error:
543     return ret;
544 }
545
546 static int parse_device_name(AVFormatContext *avctx)
547 {
548     struct dshow_ctx *ctx = avctx->priv_data;
549     char **device_name = ctx->device_name;
550     char *name = av_strdup(avctx->filename);
551     char *tmp = name;
552     int ret = 1;
553     char *type;
554
555     while ((type = strtok(tmp, "="))) {
556         char *token = strtok(NULL, ":");
557         tmp = NULL;
558
559         if        (!strcmp(type, "video")) {
560             device_name[0] = token;
561         } else if (!strcmp(type, "audio")) {
562             device_name[1] = token;
563         } else {
564             device_name[0] = NULL;
565             device_name[1] = NULL;
566             break;
567         }
568     }
569
570     if (!device_name[0] && !device_name[1]) {
571         ret = 0;
572     } else {
573         if (device_name[0])
574             device_name[0] = av_strdup(device_name[0]);
575         if (device_name[1])
576             device_name[1] = av_strdup(device_name[1]);
577     }
578
579     av_free(name);
580     return ret;
581 }
582
583 static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap)
584 {
585     struct dshow_ctx *ctx = avctx->priv_data;
586     IGraphBuilder *graph = NULL;
587     ICreateDevEnum *devenum = NULL;
588     IMediaControl *control = NULL;
589     int ret = AVERROR(EIO);
590     int r;
591
592     if (!ctx->list_devices && !parse_device_name(avctx)) {
593         av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
594         goto error;
595     }
596
597     CoInitialize(0);
598
599     r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
600                          &IID_IGraphBuilder, (void **) &graph);
601     if (r != S_OK) {
602         av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
603         goto error;
604     }
605     ctx->graph = graph;
606
607     r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
608                          &IID_ICreateDevEnum, (void **) &devenum);
609     if (r != S_OK) {
610         av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
611         goto error;
612     }
613
614     if (ctx->list_devices) {
615         av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
616         dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
617         av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
618         dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
619         ret = AVERROR_EXIT;
620         goto error;
621     }
622
623     if (ctx->device_name[VideoDevice]) {
624         ret = dshow_open_device(avctx, devenum, VideoDevice);
625         if (ret < 0)
626             goto error;
627         ret = dshow_add_device(avctx, ap, VideoDevice);
628         if (ret < 0)
629             goto error;
630     }
631     if (ctx->device_name[AudioDevice]) {
632         ret = dshow_open_device(avctx, devenum, AudioDevice);
633         if (ret < 0)
634             goto error;
635         ret = dshow_add_device(avctx, ap, AudioDevice);
636         if (ret < 0)
637             goto error;
638     }
639
640     ctx->mutex = CreateMutex(NULL, 0, NULL);
641     if (!ctx->mutex) {
642         av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
643         goto error;
644     }
645     ctx->event = CreateEvent(NULL, 1, 0, NULL);
646     if (!ctx->event) {
647         av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
648         goto error;
649     }
650
651     r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
652     if (r != S_OK) {
653         av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
654         goto error;
655     }
656     ctx->control = control;
657
658     r = IMediaControl_Run(control);
659     if (r == S_FALSE) {
660         OAFilterState pfs;
661         r = IMediaControl_GetState(control, 0, &pfs);
662     }
663     if (r != S_OK) {
664         av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
665         goto error;
666     }
667
668     ret = 0;
669
670 error:
671
672     if (ret < 0)
673         dshow_read_close(avctx);
674
675     if (devenum)
676         ICreateDevEnum_Release(devenum);
677
678     return ret;
679 }
680
681 static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
682 {
683     struct dshow_ctx *ctx = s->priv_data;
684     AVPacketList *pktl = NULL;
685
686     while (!pktl) {
687         WaitForSingleObject(ctx->mutex, INFINITE);
688         pktl = ctx->pktl;
689         if (ctx->pktl) {
690             *pkt = ctx->pktl->pkt;
691             ctx->pktl = ctx->pktl->next;
692             av_free(pktl);
693         }
694         ResetEvent(ctx->event);
695         ReleaseMutex(ctx->mutex);
696         if (!pktl) {
697             if (s->flags & AVFMT_FLAG_NONBLOCK) {
698                 return AVERROR(EAGAIN);
699             } else {
700                 WaitForSingleObject(ctx->event, INFINITE);
701             }
702         }
703     }
704
705     ctx->curbufsize -= pkt->size;
706
707     return pkt->size;
708 }
709
710 #define OFFSET(x) offsetof(struct dshow_ctx, x)
711 #define DEC AV_OPT_FLAG_DECODING_PARAM
712 static const AVOption options[] = {
713     { "list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_devices" },
714     { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_devices" },
715     { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_devices" },
716     { NULL },
717 };
718
719 static const AVClass dshow_class = {
720     .class_name = "DirectShow indev",
721     .item_name  = av_default_item_name,
722     .option     = options,
723     .version    = LIBAVUTIL_VERSION_INT,
724 };
725
726 AVInputFormat ff_dshow_demuxer = {
727     "dshow",
728     NULL_IF_CONFIG_SMALL("DirectShow capture"),
729     sizeof(struct dshow_ctx),
730     NULL,
731     dshow_read_header,
732     dshow_read_packet,
733     dshow_read_close,
734     .flags = AVFMT_NOFILE,
735     .priv_class = &dshow_class,
736 };