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);
115 if (ctx->capture_pin[VideoDevice])
116 libAVPin_Release(ctx->capture_pin[VideoDevice]);
117 if (ctx->capture_pin[AudioDevice])
118 libAVPin_Release(ctx->capture_pin[AudioDevice]);
119 if (ctx->capture_filter[VideoDevice])
120 libAVFilter_Release(ctx->capture_filter[VideoDevice]);
121 if (ctx->capture_filter[AudioDevice])
122 libAVFilter_Release(ctx->capture_filter[AudioDevice]);
124 if (ctx->device_pin[VideoDevice])
125 IPin_Release(ctx->device_pin[VideoDevice]);
126 if (ctx->device_pin[AudioDevice])
127 IPin_Release(ctx->device_pin[AudioDevice]);
128 if (ctx->device_filter[VideoDevice])
129 IBaseFilter_Release(ctx->device_filter[VideoDevice]);
130 if (ctx->device_filter[AudioDevice])
131 IBaseFilter_Release(ctx->device_filter[AudioDevice]);
136 r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
139 IEnumFilters_Reset(fenum);
140 while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK)
141 IGraphBuilder_RemoveFilter(ctx->graph, f);
142 IEnumFilters_Release(fenum);
144 IGraphBuilder_Release(ctx->graph);
147 if (ctx->device_name[0])
148 av_free(ctx->device_name[0]);
149 if (ctx->device_name[1])
150 av_free(ctx->device_name[1]);
153 CloseHandle(ctx->mutex);
155 CloseHandle(ctx->event);
159 AVPacketList *next = pktl->next;
160 av_destruct_packet(&pktl->pkt);
168 static char *dup_wchar_to_utf8(wchar_t *w)
171 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
174 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
178 static int shall_we_drop(AVFormatContext *s)
180 struct dshow_ctx *ctx = s->priv_data;
181 const uint8_t dropscore[] = {62, 75, 87, 100};
182 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
183 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
185 if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
186 av_log(s, AV_LOG_ERROR,
187 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
195 callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time)
197 AVFormatContext *s = priv_data;
198 struct dshow_ctx *ctx = s->priv_data;
199 AVPacketList **ppktl, *pktl_next;
201 // dump_videohdr(s, vdhdr);
206 WaitForSingleObject(ctx->mutex, INFINITE);
208 pktl_next = av_mallocz(sizeof(AVPacketList));
212 if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
217 pktl_next->pkt.stream_index = index;
218 pktl_next->pkt.pts = time;
219 memcpy(pktl_next->pkt.data, buf, buf_size);
221 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
224 ctx->curbufsize += buf_size;
226 SetEvent(ctx->event);
227 ReleaseMutex(ctx->mutex);
231 ReleaseMutex(ctx->mutex);
236 * Cycle through available devices using the device enumerator devenum,
237 * retrieve the device with type specified by devtype and return the
238 * pointer to the object found in *pfilter.
239 * If pfilter is NULL, list all device names.
242 dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
243 enum dshowDeviceType devtype, IBaseFilter **pfilter)
245 struct dshow_ctx *ctx = avctx->priv_data;
246 IBaseFilter *device_filter = NULL;
247 IEnumMoniker *classenum = NULL;
249 const char *device_name = ctx->device_name[devtype];
252 const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
253 &CLSID_AudioInputDeviceCategory };
254 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
256 r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
257 (IEnumMoniker **) &classenum, 0);
259 av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
264 while (IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK && !device_filter) {
265 IPropertyBag *bag = NULL;
269 r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
274 r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
278 buf = dup_wchar_to_utf8(var.bstrVal);
281 if (strcmp(device_name, buf))
284 IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
286 av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf);
293 IPropertyBag_Release(bag);
297 IEnumMoniker_Release(classenum);
300 if (!device_filter) {
301 av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
305 *pfilter = device_filter;
312 * Cycle through available formats using the specified pin,
313 * try to set parameters specified through AVOptions and if successful
314 * return 1 in *pformat_set.
315 * If pformat_set is NULL, list all pin capabilities.
318 dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
319 IPin *pin, int *pformat_set)
321 struct dshow_ctx *ctx = avctx->priv_data;
322 IAMStreamConfig *config = NULL;
323 AM_MEDIA_TYPE *type = NULL;
328 if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
330 if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
333 caps = av_malloc(size);
337 for (i = 0; i < n && !format_set; i++) {
338 IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
341 ff_print_AM_MEDIA_TYPE(type);
344 if (devtype == VideoDevice) {
345 VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
346 BITMAPINFOHEADER *bih;
349 ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
351 if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
352 VIDEOINFOHEADER *v = (void *) type->pbFormat;
353 fr = &v->AvgTimePerFrame;
355 } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
356 VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
357 fr = &v->AvgTimePerFrame;
363 av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g\n",
364 vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
365 1e7 / vcaps->MinFrameInterval,
366 vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
367 1e7 / vcaps->MaxFrameInterval);
370 if (ctx->framerate) {
371 int64_t framerate = ((int64_t) ctx->requested_framerate.den*10000000)
372 / ctx->requested_framerate.num;
373 if (framerate > vcaps->MaxFrameInterval ||
374 framerate < vcaps->MinFrameInterval)
378 if (ctx->video_size) {
379 if (ctx->requested_width > vcaps->MaxOutputSize.cx ||
380 ctx->requested_width < vcaps->MinOutputSize.cx ||
381 ctx->requested_height > vcaps->MaxOutputSize.cy ||
382 ctx->requested_height < vcaps->MinOutputSize.cy)
384 bih->biWidth = ctx->requested_width;
385 bih->biHeight = ctx->requested_height;
388 AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
391 ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
393 if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
394 fx = (void *) type->pbFormat;
399 av_log(avctx, AV_LOG_INFO, " min ch=%lu bits=%lu rate=%6lu max ch=%lu bits=%lu rate=%6lu\n",
400 acaps->MinimumChannels, acaps->MinimumBitsPerSample, acaps->MinimumSampleFrequency,
401 acaps->MaximumChannels, acaps->MaximumBitsPerSample, acaps->MaximumSampleFrequency);
404 if (ctx->sample_rate) {
405 if (ctx->sample_rate > acaps->MaximumSampleFrequency ||
406 ctx->sample_rate < acaps->MinimumSampleFrequency)
408 fx->nSamplesPerSec = ctx->sample_rate;
410 if (ctx->sample_size) {
411 if (ctx->sample_size > acaps->MaximumBitsPerSample ||
412 ctx->sample_size < acaps->MinimumBitsPerSample)
414 fx->wBitsPerSample = ctx->sample_size;
417 if (ctx->channels > acaps->MaximumChannels ||
418 ctx->channels < acaps->MinimumChannels)
420 fx->nChannels = ctx->channels;
423 if (IAMStreamConfig_SetFormat(config, type) != S_OK)
428 CoTaskMemFree(type->pbFormat);
432 IAMStreamConfig_Release(config);
436 *pformat_set = format_set;
440 * Cycle through available pins using the device_filter device, of type
441 * devtype, retrieve the first output pin and return the pointer to the
442 * object found in *ppin.
443 * If ppin is NULL, cycle through all pins listing audio/video capabilities.
446 dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
447 IBaseFilter *device_filter, IPin **ppin)
449 struct dshow_ctx *ctx = avctx->priv_data;
451 IPin *device_pin = NULL;
455 const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
456 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
458 int set_format = (devtype == VideoDevice && (ctx->video_size || ctx->framerate))
459 || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate));
462 r = IBaseFilter_EnumPins(device_filter, &pins);
464 av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
469 av_log(avctx, AV_LOG_INFO, "DirectShow %s device options\n",
472 while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !device_pin) {
473 IKsPropertySet *p = NULL;
474 IEnumMediaTypes *types = NULL;
480 IPin_QueryPinInfo(pin, &info);
481 IBaseFilter_Release(info.pFilter);
483 if (info.dir != PINDIR_OUTPUT)
485 if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
487 if (IKsPropertySet_Get(p, &ROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
488 NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
490 if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
494 char *buf = dup_wchar_to_utf8(info.achName);
495 av_log(avctx, AV_LOG_INFO, " Pin \"%s\"\n", buf);
497 dshow_cycle_formats(avctx, devtype, pin, NULL);
501 dshow_cycle_formats(avctx, devtype, pin, &format_set);
507 if (IPin_EnumMediaTypes(pin, &types) != S_OK)
510 IEnumMediaTypes_Reset(types);
511 while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK && !device_pin) {
512 if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
521 IEnumMediaTypes_Release(types);
523 IKsPropertySet_Release(p);
524 if (device_pin != pin)
528 IEnumPins_Release(pins);
531 if (set_format && !format_set) {
532 av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
536 av_log(avctx, AV_LOG_ERROR,
537 "Could not find output pin from %s capture device.\n", devtypename);
547 * List options for device with type devtype.
549 * @param devenum device enumerator used for accessing the device
552 dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
553 enum dshowDeviceType devtype)
555 IBaseFilter *device_filter = NULL;
558 if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0)
560 if ((r = dshow_cycle_pins(avctx, devtype, device_filter, NULL)) < 0)
567 dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
568 enum dshowDeviceType devtype)
570 struct dshow_ctx *ctx = avctx->priv_data;
571 IBaseFilter *device_filter = NULL;
572 IGraphBuilder *graph = ctx->graph;
573 IPin *device_pin = NULL;
574 libAVPin *capture_pin = NULL;
575 libAVFilter *capture_filter = NULL;
576 int ret = AVERROR(EIO);
579 const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
581 if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
586 ctx->device_filter [devtype] = device_filter;
588 r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
590 av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
594 if ((r = dshow_cycle_pins(avctx, devtype, device_filter, &device_pin)) < 0) {
598 ctx->device_pin[devtype] = device_pin;
600 capture_filter = libAVFilter_Create(avctx, callback, devtype);
601 if (!capture_filter) {
602 av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
605 ctx->capture_filter[devtype] = capture_filter;
607 r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
608 filter_name[devtype]);
610 av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
614 libAVPin_AddRef(capture_filter->pin);
615 capture_pin = capture_filter->pin;
616 ctx->capture_pin[devtype] = capture_pin;
618 r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
620 av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
630 static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
632 switch (sample_fmt) {
633 case AV_SAMPLE_FMT_U8: return CODEC_ID_PCM_U8;
634 case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE;
635 case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE;
636 default: return CODEC_ID_NONE; /* Should never happen. */
640 static enum SampleFormat sample_fmt_bits_per_sample(int bits)
643 case 8: return AV_SAMPLE_FMT_U8;
644 case 16: return AV_SAMPLE_FMT_S16;
645 case 32: return AV_SAMPLE_FMT_S32;
646 default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
651 dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap,
652 enum dshowDeviceType devtype)
654 struct dshow_ctx *ctx = avctx->priv_data;
656 AVCodecContext *codec;
658 int ret = AVERROR(EIO);
660 st = av_new_stream(avctx, devtype);
662 ret = AVERROR(ENOMEM);
666 ctx->capture_filter[devtype]->stream_index = st->index;
668 libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
671 if (devtype == VideoDevice) {
672 BITMAPINFOHEADER *bih = NULL;
674 if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
675 VIDEOINFOHEADER *v = (void *) type.pbFormat;
677 } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
678 VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
682 av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
686 codec->time_base = ap->time_base;
687 codec->codec_type = AVMEDIA_TYPE_VIDEO;
688 codec->width = bih->biWidth;
689 codec->height = bih->biHeight;
690 codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
691 if (codec->pix_fmt == PIX_FMT_NONE) {
692 codec->codec_id = dshow_codecid(bih->biCompression);
693 if (codec->codec_id == CODEC_ID_NONE) {
694 av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
695 "Please report verbose (-v 9) debug information.\n");
696 dshow_read_close(avctx);
697 return AVERROR_PATCHWELCOME;
699 codec->bits_per_coded_sample = bih->biBitCount;
701 codec->codec_id = CODEC_ID_RAWVIDEO;
702 if (bih->biCompression == BI_RGB) {
703 codec->bits_per_coded_sample = bih->biBitCount;
704 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
705 if (codec->extradata) {
706 codec->extradata_size = 9;
707 memcpy(codec->extradata, "BottomUp", 9);
712 WAVEFORMATEX *fx = NULL;
714 if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
715 fx = (void *) type.pbFormat;
718 av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
722 codec->codec_type = AVMEDIA_TYPE_AUDIO;
723 codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
724 codec->codec_id = waveform_codec_id(codec->sample_fmt);
725 codec->sample_rate = fx->nSamplesPerSec;
726 codec->channels = fx->nChannels;
729 av_set_pts_info(st, 64, 1, 10000000);
737 static int parse_device_name(AVFormatContext *avctx)
739 struct dshow_ctx *ctx = avctx->priv_data;
740 char **device_name = ctx->device_name;
741 char *name = av_strdup(avctx->filename);
746 while ((type = strtok(tmp, "="))) {
747 char *token = strtok(NULL, ":");
750 if (!strcmp(type, "video")) {
751 device_name[0] = token;
752 } else if (!strcmp(type, "audio")) {
753 device_name[1] = token;
755 device_name[0] = NULL;
756 device_name[1] = NULL;
761 if (!device_name[0] && !device_name[1]) {
765 device_name[0] = av_strdup(device_name[0]);
767 device_name[1] = av_strdup(device_name[1]);
774 static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap)
776 struct dshow_ctx *ctx = avctx->priv_data;
777 IGraphBuilder *graph = NULL;
778 ICreateDevEnum *devenum = NULL;
779 IMediaControl *control = NULL;
780 int ret = AVERROR(EIO);
783 if (!ctx->list_devices && !parse_device_name(avctx)) {
784 av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
788 if (ctx->video_size) {
789 r = av_parse_video_size(&ctx->requested_width, &ctx->requested_height, ctx->video_size);
791 av_log(avctx, AV_LOG_ERROR, "Could not parse video size '%s'.\n", ctx->video_size);
795 if (ctx->framerate) {
796 r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
798 av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
805 r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
806 &IID_IGraphBuilder, (void **) &graph);
808 av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
813 r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
814 &IID_ICreateDevEnum, (void **) &devenum);
816 av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
820 if (ctx->list_devices) {
821 av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
822 dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
823 av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
824 dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
828 if (ctx->list_options) {
829 if (ctx->device_name[VideoDevice])
830 dshow_list_device_options(avctx, devenum, VideoDevice);
831 if (ctx->device_name[AudioDevice])
832 dshow_list_device_options(avctx, devenum, AudioDevice);
837 if (ctx->device_name[VideoDevice]) {
838 ret = dshow_open_device(avctx, devenum, VideoDevice);
841 ret = dshow_add_device(avctx, ap, VideoDevice);
845 if (ctx->device_name[AudioDevice]) {
846 ret = dshow_open_device(avctx, devenum, AudioDevice);
849 ret = dshow_add_device(avctx, ap, AudioDevice);
854 ctx->mutex = CreateMutex(NULL, 0, NULL);
856 av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
859 ctx->event = CreateEvent(NULL, 1, 0, NULL);
861 av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
865 r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
867 av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
870 ctx->control = control;
872 r = IMediaControl_Run(control);
875 r = IMediaControl_GetState(control, 0, &pfs);
878 av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
887 dshow_read_close(avctx);
890 ICreateDevEnum_Release(devenum);
895 static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
897 struct dshow_ctx *ctx = s->priv_data;
898 AVPacketList *pktl = NULL;
901 WaitForSingleObject(ctx->mutex, INFINITE);
904 *pkt = ctx->pktl->pkt;
905 ctx->pktl = ctx->pktl->next;
908 ResetEvent(ctx->event);
909 ReleaseMutex(ctx->mutex);
911 if (s->flags & AVFMT_FLAG_NONBLOCK) {
912 return AVERROR(EAGAIN);
914 WaitForSingleObject(ctx->event, INFINITE);
919 ctx->curbufsize -= pkt->size;
924 #define OFFSET(x) offsetof(struct dshow_ctx, x)
925 #define DEC AV_OPT_FLAG_DECODING_PARAM
926 static const AVOption options[] = {
927 { "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 },
928 { "framerate", "set video frame rate", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
929 { "sample_rate", "set audio sample rate", OFFSET(sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, DEC },
930 { "sample_size", "set audio sample size", OFFSET(sample_size), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 16, DEC },
931 { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, DEC },
932 { "list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_devices" },
933 { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_devices" },
934 { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_devices" },
935 { "list_options", "list available options for specified device", OFFSET(list_options), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_options" },
936 { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_options" },
937 { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_options" },
941 static const AVClass dshow_class = {
942 .class_name = "DirectShow indev",
943 .item_name = av_default_item_name,
945 .version = LIBAVUTIL_VERSION_INT,
948 AVInputFormat ff_dshow_demuxer = {
950 NULL_IF_CONFIG_SMALL("DirectShow capture"),
951 sizeof(struct dshow_ctx),
956 .flags = AVFMT_NOFILE,
957 .priv_class = &dshow_class,