2 * Directshow capture interface
3 * Copyright (c) 2010 Ramiro Polla
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/parseutils.h"
23 #include "libavutil/opt.h"
38 IBaseFilter *device_filter[2];
40 libAVFilter *capture_filter[2];
41 libAVPin *capture_pin[2];
47 unsigned int curbufsize;
48 unsigned int video_frame_num;
50 IMediaControl *control;
57 AVRational requested_framerate;
64 static enum PixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
66 switch(biCompression) {
67 case MKTAG('U', 'Y', 'V', 'Y'):
68 return PIX_FMT_UYVY422;
69 case MKTAG('Y', 'U', 'Y', '2'):
70 return PIX_FMT_YUYV422;
71 case MKTAG('I', '4', '2', '0'):
72 return PIX_FMT_YUV420P;
74 switch(biBitCount) { /* 1-8 are untested */
76 return PIX_FMT_MONOWHITE;
82 return PIX_FMT_RGB555;
92 static enum CodecID dshow_codecid(DWORD biCompression)
94 switch(biCompression) {
95 case MKTAG('d', 'v', 's', 'd'):
96 return CODEC_ID_DVVIDEO;
97 case MKTAG('M', 'J', 'P', 'G'):
98 case MKTAG('m', 'j', 'p', 'g'):
99 return CODEC_ID_MJPEG;
101 return CODEC_ID_NONE;
105 dshow_read_close(AVFormatContext *s)
107 struct dshow_ctx *ctx = s->priv_data;
111 IMediaControl_Stop(ctx->control);
112 IMediaControl_Release(ctx->control);
118 r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
121 IEnumFilters_Reset(fenum);
122 while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK)
123 if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK)
124 IEnumFilters_Reset(fenum); /* When a filter is removed,
125 * the list must be reset. */
126 IEnumFilters_Release(fenum);
128 IGraphBuilder_Release(ctx->graph);
131 if (ctx->capture_pin[VideoDevice])
132 libAVPin_Release(ctx->capture_pin[VideoDevice]);
133 if (ctx->capture_pin[AudioDevice])
134 libAVPin_Release(ctx->capture_pin[AudioDevice]);
135 if (ctx->capture_filter[VideoDevice])
136 libAVFilter_Release(ctx->capture_filter[VideoDevice]);
137 if (ctx->capture_filter[AudioDevice])
138 libAVFilter_Release(ctx->capture_filter[AudioDevice]);
140 if (ctx->device_pin[VideoDevice])
141 IPin_Release(ctx->device_pin[VideoDevice]);
142 if (ctx->device_pin[AudioDevice])
143 IPin_Release(ctx->device_pin[AudioDevice]);
144 if (ctx->device_filter[VideoDevice])
145 IBaseFilter_Release(ctx->device_filter[VideoDevice]);
146 if (ctx->device_filter[AudioDevice])
147 IBaseFilter_Release(ctx->device_filter[AudioDevice]);
149 if (ctx->device_name[0])
150 av_free(ctx->device_name[0]);
151 if (ctx->device_name[1])
152 av_free(ctx->device_name[1]);
155 CloseHandle(ctx->mutex);
157 CloseHandle(ctx->event);
161 AVPacketList *next = pktl->next;
162 av_destruct_packet(&pktl->pkt);
170 static char *dup_wchar_to_utf8(wchar_t *w)
173 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
176 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
180 static int shall_we_drop(AVFormatContext *s)
182 struct dshow_ctx *ctx = s->priv_data;
183 const uint8_t dropscore[] = {62, 75, 87, 100};
184 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
185 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
187 if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
188 av_log(s, AV_LOG_ERROR,
189 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
197 callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time)
199 AVFormatContext *s = priv_data;
200 struct dshow_ctx *ctx = s->priv_data;
201 AVPacketList **ppktl, *pktl_next;
203 // dump_videohdr(s, vdhdr);
208 WaitForSingleObject(ctx->mutex, INFINITE);
210 pktl_next = av_mallocz(sizeof(AVPacketList));
214 if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
219 pktl_next->pkt.stream_index = index;
220 pktl_next->pkt.pts = time;
221 memcpy(pktl_next->pkt.data, buf, buf_size);
223 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
226 ctx->curbufsize += buf_size;
228 SetEvent(ctx->event);
229 ReleaseMutex(ctx->mutex);
233 ReleaseMutex(ctx->mutex);
238 * Cycle through available devices using the device enumerator devenum,
239 * retrieve the device with type specified by devtype and return the
240 * pointer to the object found in *pfilter.
241 * If pfilter is NULL, list all device names.
244 dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
245 enum dshowDeviceType devtype, IBaseFilter **pfilter)
247 struct dshow_ctx *ctx = avctx->priv_data;
248 IBaseFilter *device_filter = NULL;
249 IEnumMoniker *classenum = NULL;
251 const char *device_name = ctx->device_name[devtype];
254 const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
255 &CLSID_AudioInputDeviceCategory };
256 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
258 r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
259 (IEnumMoniker **) &classenum, 0);
261 av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
266 while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) {
267 IPropertyBag *bag = NULL;
271 r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
276 r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
280 buf = dup_wchar_to_utf8(var.bstrVal);
283 if (strcmp(device_name, buf))
286 IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
288 av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf);
295 IPropertyBag_Release(bag);
299 IEnumMoniker_Release(classenum);
302 if (!device_filter) {
303 av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
307 *pfilter = device_filter;
314 * Cycle through available formats using the specified pin,
315 * try to set parameters specified through AVOptions and if successful
316 * return 1 in *pformat_set.
317 * If pformat_set is NULL, list all pin capabilities.
320 dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
321 IPin *pin, int *pformat_set)
323 struct dshow_ctx *ctx = avctx->priv_data;
324 IAMStreamConfig *config = NULL;
325 AM_MEDIA_TYPE *type = NULL;
330 if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
332 if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
335 caps = av_malloc(size);
339 for (i = 0; i < n && !format_set; i++) {
340 IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
343 ff_print_AM_MEDIA_TYPE(type);
346 if (devtype == VideoDevice) {
347 VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
348 BITMAPINFOHEADER *bih;
351 ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
353 if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
354 VIDEOINFOHEADER *v = (void *) type->pbFormat;
355 fr = &v->AvgTimePerFrame;
357 } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
358 VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
359 fr = &v->AvgTimePerFrame;
365 av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g\n",
366 vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
367 1e7 / vcaps->MinFrameInterval,
368 vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
369 1e7 / vcaps->MaxFrameInterval);
372 if (ctx->framerate) {
373 int64_t framerate = ((int64_t) ctx->requested_framerate.den*10000000)
374 / ctx->requested_framerate.num;
375 if (framerate > vcaps->MaxFrameInterval ||
376 framerate < vcaps->MinFrameInterval)
380 if (ctx->video_size) {
381 if (ctx->requested_width > vcaps->MaxOutputSize.cx ||
382 ctx->requested_width < vcaps->MinOutputSize.cx ||
383 ctx->requested_height > vcaps->MaxOutputSize.cy ||
384 ctx->requested_height < vcaps->MinOutputSize.cy)
386 bih->biWidth = ctx->requested_width;
387 bih->biHeight = ctx->requested_height;
390 AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
393 ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
395 if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
396 fx = (void *) type->pbFormat;
401 av_log(avctx, AV_LOG_INFO, " min ch=%lu bits=%lu rate=%6lu max ch=%lu bits=%lu rate=%6lu\n",
402 acaps->MinimumChannels, acaps->MinimumBitsPerSample, acaps->MinimumSampleFrequency,
403 acaps->MaximumChannels, acaps->MaximumBitsPerSample, acaps->MaximumSampleFrequency);
406 if (ctx->sample_rate) {
407 if (ctx->sample_rate > acaps->MaximumSampleFrequency ||
408 ctx->sample_rate < acaps->MinimumSampleFrequency)
410 fx->nSamplesPerSec = ctx->sample_rate;
412 if (ctx->sample_size) {
413 if (ctx->sample_size > acaps->MaximumBitsPerSample ||
414 ctx->sample_size < acaps->MinimumBitsPerSample)
416 fx->wBitsPerSample = ctx->sample_size;
419 if (ctx->channels > acaps->MaximumChannels ||
420 ctx->channels < acaps->MinimumChannels)
422 fx->nChannels = ctx->channels;
425 if (IAMStreamConfig_SetFormat(config, type) != S_OK)
430 CoTaskMemFree(type->pbFormat);
434 IAMStreamConfig_Release(config);
438 *pformat_set = format_set;
442 * Cycle through available pins using the device_filter device, of type
443 * devtype, retrieve the first output pin and return the pointer to the
444 * object found in *ppin.
445 * If ppin is NULL, cycle through all pins listing audio/video capabilities.
448 dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
449 IBaseFilter *device_filter, IPin **ppin)
451 struct dshow_ctx *ctx = avctx->priv_data;
453 IPin *device_pin = NULL;
457 const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
458 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
460 int set_format = (devtype == VideoDevice && (ctx->video_size || ctx->framerate))
461 || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate));
464 r = IBaseFilter_EnumPins(device_filter, &pins);
466 av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
471 av_log(avctx, AV_LOG_INFO, "DirectShow %s device options\n",
474 while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
475 IKsPropertySet *p = NULL;
476 IEnumMediaTypes *types = NULL;
482 IPin_QueryPinInfo(pin, &info);
483 IBaseFilter_Release(info.pFilter);
485 if (info.dir != PINDIR_OUTPUT)
487 if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
489 if (IKsPropertySet_Get(p, &ROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
490 NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
492 if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
496 char *buf = dup_wchar_to_utf8(info.achName);
497 av_log(avctx, AV_LOG_INFO, " Pin \"%s\"\n", buf);
499 dshow_cycle_formats(avctx, devtype, pin, NULL);
503 dshow_cycle_formats(avctx, devtype, pin, &format_set);
509 if (IPin_EnumMediaTypes(pin, &types) != S_OK)
512 IEnumMediaTypes_Reset(types);
513 while (!device_pin && IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) {
514 if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
523 IEnumMediaTypes_Release(types);
525 IKsPropertySet_Release(p);
526 if (device_pin != pin)
530 IEnumPins_Release(pins);
533 if (set_format && !format_set) {
534 av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
538 av_log(avctx, AV_LOG_ERROR,
539 "Could not find output pin from %s capture device.\n", devtypename);
549 * List options for device with type devtype.
551 * @param devenum device enumerator used for accessing the device
554 dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
555 enum dshowDeviceType devtype)
557 IBaseFilter *device_filter = NULL;
560 if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0)
562 if ((r = dshow_cycle_pins(avctx, devtype, device_filter, NULL)) < 0)
569 dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
570 enum dshowDeviceType devtype)
572 struct dshow_ctx *ctx = avctx->priv_data;
573 IBaseFilter *device_filter = NULL;
574 IGraphBuilder *graph = ctx->graph;
575 IPin *device_pin = NULL;
576 libAVPin *capture_pin = NULL;
577 libAVFilter *capture_filter = NULL;
578 int ret = AVERROR(EIO);
581 const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
583 if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
588 ctx->device_filter [devtype] = device_filter;
590 r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
592 av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
596 if ((r = dshow_cycle_pins(avctx, devtype, device_filter, &device_pin)) < 0) {
600 ctx->device_pin[devtype] = device_pin;
602 capture_filter = libAVFilter_Create(avctx, callback, devtype);
603 if (!capture_filter) {
604 av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
607 ctx->capture_filter[devtype] = capture_filter;
609 r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
610 filter_name[devtype]);
612 av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
616 libAVPin_AddRef(capture_filter->pin);
617 capture_pin = capture_filter->pin;
618 ctx->capture_pin[devtype] = capture_pin;
620 r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
622 av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
632 static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
634 switch (sample_fmt) {
635 case AV_SAMPLE_FMT_U8: return CODEC_ID_PCM_U8;
636 case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE;
637 case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE;
638 default: return CODEC_ID_NONE; /* Should never happen. */
642 static enum SampleFormat sample_fmt_bits_per_sample(int bits)
645 case 8: return AV_SAMPLE_FMT_U8;
646 case 16: return AV_SAMPLE_FMT_S16;
647 case 32: return AV_SAMPLE_FMT_S32;
648 default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
653 dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap,
654 enum dshowDeviceType devtype)
656 struct dshow_ctx *ctx = avctx->priv_data;
658 AVCodecContext *codec;
660 int ret = AVERROR(EIO);
662 st = av_new_stream(avctx, devtype);
664 ret = AVERROR(ENOMEM);
668 ctx->capture_filter[devtype]->stream_index = st->index;
670 libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
673 if (devtype == VideoDevice) {
674 BITMAPINFOHEADER *bih = NULL;
676 if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
677 VIDEOINFOHEADER *v = (void *) type.pbFormat;
679 } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
680 VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
684 av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
688 codec->time_base = ap->time_base;
689 codec->codec_type = AVMEDIA_TYPE_VIDEO;
690 codec->width = bih->biWidth;
691 codec->height = bih->biHeight;
692 codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
693 if (codec->pix_fmt == PIX_FMT_NONE) {
694 codec->codec_id = dshow_codecid(bih->biCompression);
695 if (codec->codec_id == CODEC_ID_NONE) {
696 av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
697 "Please report verbose (-v 9) debug information.\n");
698 dshow_read_close(avctx);
699 return AVERROR_PATCHWELCOME;
701 codec->bits_per_coded_sample = bih->biBitCount;
703 codec->codec_id = CODEC_ID_RAWVIDEO;
704 if (bih->biCompression == BI_RGB) {
705 codec->bits_per_coded_sample = bih->biBitCount;
706 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
707 if (codec->extradata) {
708 codec->extradata_size = 9;
709 memcpy(codec->extradata, "BottomUp", 9);
714 WAVEFORMATEX *fx = NULL;
716 if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
717 fx = (void *) type.pbFormat;
720 av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
724 codec->codec_type = AVMEDIA_TYPE_AUDIO;
725 codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
726 codec->codec_id = waveform_codec_id(codec->sample_fmt);
727 codec->sample_rate = fx->nSamplesPerSec;
728 codec->channels = fx->nChannels;
731 av_set_pts_info(st, 64, 1, 10000000);
739 static int parse_device_name(AVFormatContext *avctx)
741 struct dshow_ctx *ctx = avctx->priv_data;
742 char **device_name = ctx->device_name;
743 char *name = av_strdup(avctx->filename);
748 while ((type = strtok(tmp, "="))) {
749 char *token = strtok(NULL, ":");
752 if (!strcmp(type, "video")) {
753 device_name[0] = token;
754 } else if (!strcmp(type, "audio")) {
755 device_name[1] = token;
757 device_name[0] = NULL;
758 device_name[1] = NULL;
763 if (!device_name[0] && !device_name[1]) {
767 device_name[0] = av_strdup(device_name[0]);
769 device_name[1] = av_strdup(device_name[1]);
776 static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap)
778 struct dshow_ctx *ctx = avctx->priv_data;
779 IGraphBuilder *graph = NULL;
780 ICreateDevEnum *devenum = NULL;
781 IMediaControl *control = NULL;
782 int ret = AVERROR(EIO);
785 if (!ctx->list_devices && !parse_device_name(avctx)) {
786 av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
790 if (ctx->video_size) {
791 r = av_parse_video_size(&ctx->requested_width, &ctx->requested_height, ctx->video_size);
793 av_log(avctx, AV_LOG_ERROR, "Could not parse video size '%s'.\n", ctx->video_size);
797 if (ctx->framerate) {
798 r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
800 av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
807 r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
808 &IID_IGraphBuilder, (void **) &graph);
810 av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
815 r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
816 &IID_ICreateDevEnum, (void **) &devenum);
818 av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
822 if (ctx->list_devices) {
823 av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
824 dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
825 av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
826 dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
830 if (ctx->list_options) {
831 if (ctx->device_name[VideoDevice])
832 dshow_list_device_options(avctx, devenum, VideoDevice);
833 if (ctx->device_name[AudioDevice])
834 dshow_list_device_options(avctx, devenum, AudioDevice);
839 if (ctx->device_name[VideoDevice]) {
840 ret = dshow_open_device(avctx, devenum, VideoDevice);
843 ret = dshow_add_device(avctx, ap, VideoDevice);
847 if (ctx->device_name[AudioDevice]) {
848 ret = dshow_open_device(avctx, devenum, AudioDevice);
851 ret = dshow_add_device(avctx, ap, AudioDevice);
856 ctx->mutex = CreateMutex(NULL, 0, NULL);
858 av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
861 ctx->event = CreateEvent(NULL, 1, 0, NULL);
863 av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
867 r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
869 av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
872 ctx->control = control;
874 r = IMediaControl_Run(control);
877 r = IMediaControl_GetState(control, 0, &pfs);
880 av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
889 dshow_read_close(avctx);
892 ICreateDevEnum_Release(devenum);
897 static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
899 struct dshow_ctx *ctx = s->priv_data;
900 AVPacketList *pktl = NULL;
903 WaitForSingleObject(ctx->mutex, INFINITE);
906 *pkt = ctx->pktl->pkt;
907 ctx->pktl = ctx->pktl->next;
910 ResetEvent(ctx->event);
911 ReleaseMutex(ctx->mutex);
913 if (s->flags & AVFMT_FLAG_NONBLOCK) {
914 return AVERROR(EAGAIN);
916 WaitForSingleObject(ctx->event, INFINITE);
921 ctx->curbufsize -= pkt->size;
926 #define OFFSET(x) offsetof(struct dshow_ctx, x)
927 #define DEC AV_OPT_FLAG_DECODING_PARAM
928 static const AVOption options[] = {
929 { "video_size", "set video size given a string such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
930 { "framerate", "set video frame rate", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
931 { "sample_rate", "set audio sample rate", OFFSET(sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, DEC },
932 { "sample_size", "set audio sample size", OFFSET(sample_size), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 16, DEC },
933 { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, DEC },
934 { "list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_devices" },
935 { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_devices" },
936 { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_devices" },
937 { "list_options", "list available options for specified device", OFFSET(list_options), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_options" },
938 { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_options" },
939 { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_options" },
943 static const AVClass dshow_class = {
944 .class_name = "DirectShow indev",
945 .item_name = av_default_item_name,
947 .version = LIBAVUTIL_VERSION_INT,
950 AVInputFormat ff_dshow_demuxer = {
952 NULL_IF_CONFIG_SMALL("DirectShow capture"),
953 sizeof(struct dshow_ctx),
958 .flags = AVFMT_NOFILE,
959 .priv_class = &dshow_class,