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/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "libavformat/internal.h"
27 #include "dshow_capture.h"
35 int video_device_number;
36 int audio_device_number;
40 int audio_buffer_size;
42 IBaseFilter *device_filter[2];
44 libAVFilter *capture_filter[2];
45 libAVPin *capture_pin[2];
48 HANDLE event[2]; /* event[0] is set by DirectShow
49 * event[1] is set by callback() */
55 unsigned int video_frame_num;
57 IMediaControl *control;
58 IMediaEvent *media_event;
60 enum AVPixelFormat pixel_format;
61 enum AVCodecID video_codec_id;
66 AVRational requested_framerate;
73 static enum AVPixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
75 switch(biCompression) {
76 case MKTAG('U', 'Y', 'V', 'Y'):
77 return AV_PIX_FMT_UYVY422;
78 case MKTAG('Y', 'U', 'Y', '2'):
79 return AV_PIX_FMT_YUYV422;
80 case MKTAG('I', '4', '2', '0'):
81 return AV_PIX_FMT_YUV420P;
84 switch(biBitCount) { /* 1-8 are untested */
86 return AV_PIX_FMT_MONOWHITE;
88 return AV_PIX_FMT_RGB4;
90 return AV_PIX_FMT_RGB8;
92 return AV_PIX_FMT_RGB555;
94 return AV_PIX_FMT_BGR24;
96 return AV_PIX_FMT_RGB32;
99 return AV_PIX_FMT_NONE;
102 static enum AVCodecID dshow_codecid(DWORD biCompression)
104 switch(biCompression) {
105 case MKTAG('d', 'v', 's', 'd'):
106 return AV_CODEC_ID_DVVIDEO;
107 case MKTAG('M', 'J', 'P', 'G'):
108 case MKTAG('m', 'j', 'p', 'g'):
109 return AV_CODEC_ID_MJPEG;
111 return AV_CODEC_ID_NONE;
115 dshow_read_close(AVFormatContext *s)
117 struct dshow_ctx *ctx = s->priv_data;
121 IMediaControl_Stop(ctx->control);
122 IMediaControl_Release(ctx->control);
125 if (ctx->media_event)
126 IMediaEvent_Release(ctx->media_event);
131 r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
134 IEnumFilters_Reset(fenum);
135 while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK) {
136 if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK)
137 IEnumFilters_Reset(fenum); /* When a filter is removed,
138 * the list must be reset. */
139 IBaseFilter_Release(f);
141 IEnumFilters_Release(fenum);
143 IGraphBuilder_Release(ctx->graph);
146 if (ctx->capture_pin[VideoDevice])
147 libAVPin_Release(ctx->capture_pin[VideoDevice]);
148 if (ctx->capture_pin[AudioDevice])
149 libAVPin_Release(ctx->capture_pin[AudioDevice]);
150 if (ctx->capture_filter[VideoDevice])
151 libAVFilter_Release(ctx->capture_filter[VideoDevice]);
152 if (ctx->capture_filter[AudioDevice])
153 libAVFilter_Release(ctx->capture_filter[AudioDevice]);
155 if (ctx->device_pin[VideoDevice])
156 IPin_Release(ctx->device_pin[VideoDevice]);
157 if (ctx->device_pin[AudioDevice])
158 IPin_Release(ctx->device_pin[AudioDevice]);
159 if (ctx->device_filter[VideoDevice])
160 IBaseFilter_Release(ctx->device_filter[VideoDevice]);
161 if (ctx->device_filter[AudioDevice])
162 IBaseFilter_Release(ctx->device_filter[AudioDevice]);
164 if (ctx->device_name[0])
165 av_free(ctx->device_name[0]);
166 if (ctx->device_name[1])
167 av_free(ctx->device_name[1]);
170 CloseHandle(ctx->mutex);
172 CloseHandle(ctx->event[0]);
174 CloseHandle(ctx->event[1]);
178 AVPacketList *next = pktl->next;
179 av_destruct_packet(&pktl->pkt);
187 static char *dup_wchar_to_utf8(wchar_t *w)
190 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
193 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
197 static int shall_we_drop(AVFormatContext *s)
199 struct dshow_ctx *ctx = s->priv_data;
200 const uint8_t dropscore[] = {62, 75, 87, 100};
201 const int ndropscores = FF_ARRAY_ELEMS(dropscore);
202 unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
204 if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
205 av_log(s, AV_LOG_ERROR,
206 "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
214 callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time)
216 AVFormatContext *s = priv_data;
217 struct dshow_ctx *ctx = s->priv_data;
218 AVPacketList **ppktl, *pktl_next;
220 // dump_videohdr(s, vdhdr);
222 WaitForSingleObject(ctx->mutex, INFINITE);
227 pktl_next = av_mallocz(sizeof(AVPacketList));
231 if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
236 pktl_next->pkt.stream_index = index;
237 pktl_next->pkt.pts = time;
238 memcpy(pktl_next->pkt.data, buf, buf_size);
240 for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
243 ctx->curbufsize += buf_size;
245 SetEvent(ctx->event[1]);
246 ReleaseMutex(ctx->mutex);
250 ReleaseMutex(ctx->mutex);
255 * Cycle through available devices using the device enumerator devenum,
256 * retrieve the device with type specified by devtype and return the
257 * pointer to the object found in *pfilter.
258 * If pfilter is NULL, list all device names.
261 dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
262 enum dshowDeviceType devtype, IBaseFilter **pfilter)
264 struct dshow_ctx *ctx = avctx->priv_data;
265 IBaseFilter *device_filter = NULL;
266 IEnumMoniker *classenum = NULL;
268 const char *device_name = ctx->device_name[devtype];
269 int skip = (devtype == VideoDevice) ? ctx->video_device_number
270 : ctx->audio_device_number;
273 const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
274 &CLSID_AudioInputDeviceCategory };
275 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
277 r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
278 (IEnumMoniker **) &classenum, 0);
280 av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
285 while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) {
286 IPropertyBag *bag = NULL;
290 r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
295 r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
299 buf = dup_wchar_to_utf8(var.bstrVal);
302 if (strcmp(device_name, buf))
306 IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
308 av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf);
315 IPropertyBag_Release(bag);
319 IEnumMoniker_Release(classenum);
322 if (!device_filter) {
323 av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
327 *pfilter = device_filter;
334 * Cycle through available formats using the specified pin,
335 * try to set parameters specified through AVOptions and if successful
336 * return 1 in *pformat_set.
337 * If pformat_set is NULL, list all pin capabilities.
340 dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
341 IPin *pin, int *pformat_set)
343 struct dshow_ctx *ctx = avctx->priv_data;
344 IAMStreamConfig *config = NULL;
345 AM_MEDIA_TYPE *type = NULL;
350 if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
352 if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
355 caps = av_malloc(size);
359 for (i = 0; i < n && !format_set; i++) {
360 IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
363 ff_print_AM_MEDIA_TYPE(type);
366 if (devtype == VideoDevice) {
367 VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
368 BITMAPINFOHEADER *bih;
371 ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
373 if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
374 VIDEOINFOHEADER *v = (void *) type->pbFormat;
375 fr = &v->AvgTimePerFrame;
377 } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
378 VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
379 fr = &v->AvgTimePerFrame;
385 enum AVPixelFormat pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
386 if (pix_fmt == AV_PIX_FMT_NONE) {
387 enum AVCodecID codec_id = dshow_codecid(bih->biCompression);
388 AVCodec *codec = avcodec_find_decoder(codec_id);
389 if (codec_id == AV_CODEC_ID_NONE || !codec) {
390 av_log(avctx, AV_LOG_INFO, " unknown compression type 0x%X", (int) bih->biCompression);
392 av_log(avctx, AV_LOG_INFO, " vcodec=%s", codec->name);
395 av_log(avctx, AV_LOG_INFO, " pixel_format=%s", av_get_pix_fmt_name(pix_fmt));
397 av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g\n",
398 vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
399 1e7 / vcaps->MaxFrameInterval,
400 vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
401 1e7 / vcaps->MinFrameInterval);
404 if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
405 if (ctx->video_codec_id != dshow_codecid(bih->biCompression))
408 if (ctx->pixel_format != AV_PIX_FMT_NONE &&
409 ctx->pixel_format != dshow_pixfmt(bih->biCompression, bih->biBitCount)) {
412 if (ctx->framerate) {
413 int64_t framerate = ((int64_t) ctx->requested_framerate.den*10000000)
414 / ctx->requested_framerate.num;
415 if (framerate > vcaps->MaxFrameInterval ||
416 framerate < vcaps->MinFrameInterval)
420 if (ctx->requested_width && ctx->requested_height) {
421 if (ctx->requested_width > vcaps->MaxOutputSize.cx ||
422 ctx->requested_width < vcaps->MinOutputSize.cx ||
423 ctx->requested_height > vcaps->MaxOutputSize.cy ||
424 ctx->requested_height < vcaps->MinOutputSize.cy)
426 bih->biWidth = ctx->requested_width;
427 bih->biHeight = ctx->requested_height;
430 AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
433 ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
435 if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
436 fx = (void *) type->pbFormat;
441 av_log(avctx, AV_LOG_INFO, " min ch=%lu bits=%lu rate=%6lu max ch=%lu bits=%lu rate=%6lu\n",
442 acaps->MinimumChannels, acaps->MinimumBitsPerSample, acaps->MinimumSampleFrequency,
443 acaps->MaximumChannels, acaps->MaximumBitsPerSample, acaps->MaximumSampleFrequency);
446 if (ctx->sample_rate) {
447 if (ctx->sample_rate > acaps->MaximumSampleFrequency ||
448 ctx->sample_rate < acaps->MinimumSampleFrequency)
450 fx->nSamplesPerSec = ctx->sample_rate;
452 if (ctx->sample_size) {
453 if (ctx->sample_size > acaps->MaximumBitsPerSample ||
454 ctx->sample_size < acaps->MinimumBitsPerSample)
456 fx->wBitsPerSample = ctx->sample_size;
459 if (ctx->channels > acaps->MaximumChannels ||
460 ctx->channels < acaps->MinimumChannels)
462 fx->nChannels = ctx->channels;
465 if (IAMStreamConfig_SetFormat(config, type) != S_OK)
470 CoTaskMemFree(type->pbFormat);
474 IAMStreamConfig_Release(config);
478 *pformat_set = format_set;
482 * Set audio device buffer size in milliseconds (which can directly impact
483 * latency, depending on the device).
486 dshow_set_audio_buffer_size(AVFormatContext *avctx, IPin *pin)
488 struct dshow_ctx *ctx = avctx->priv_data;
489 IAMBufferNegotiation *buffer_negotiation = NULL;
490 ALLOCATOR_PROPERTIES props = { -1, -1, -1, -1 };
491 IAMStreamConfig *config = NULL;
492 AM_MEDIA_TYPE *type = NULL;
493 int ret = AVERROR(EIO);
495 if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
497 if (IAMStreamConfig_GetFormat(config, &type) != S_OK)
499 if (!IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx))
502 props.cbBuffer = (((WAVEFORMATEX *) type->pbFormat)->nAvgBytesPerSec)
503 * ctx->audio_buffer_size / 1000;
505 if (IPin_QueryInterface(pin, &IID_IAMBufferNegotiation, (void **) &buffer_negotiation) != S_OK)
507 if (IAMBufferNegotiation_SuggestAllocatorProperties(buffer_negotiation, &props) != S_OK)
513 if (buffer_negotiation)
514 IAMBufferNegotiation_Release(buffer_negotiation);
517 CoTaskMemFree(type->pbFormat);
521 IAMStreamConfig_Release(config);
527 * Cycle through available pins using the device_filter device, of type
528 * devtype, retrieve the first output pin and return the pointer to the
529 * object found in *ppin.
530 * If ppin is NULL, cycle through all pins listing audio/video capabilities.
533 dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
534 IBaseFilter *device_filter, IPin **ppin)
536 struct dshow_ctx *ctx = avctx->priv_data;
538 IPin *device_pin = NULL;
542 const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
543 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
545 int set_format = (devtype == VideoDevice && (ctx->framerate ||
546 (ctx->requested_width && ctx->requested_height) ||
547 ctx->pixel_format != AV_PIX_FMT_NONE ||
548 ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO))
549 || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate));
552 r = IBaseFilter_EnumPins(device_filter, &pins);
554 av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
559 av_log(avctx, AV_LOG_INFO, "DirectShow %s device options\n",
562 while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
563 IKsPropertySet *p = NULL;
564 IEnumMediaTypes *types = NULL;
570 IPin_QueryPinInfo(pin, &info);
571 IBaseFilter_Release(info.pFilter);
573 if (info.dir != PINDIR_OUTPUT)
575 if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
577 if (IKsPropertySet_Get(p, &ROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
578 NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
580 if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
584 char *buf = dup_wchar_to_utf8(info.achName);
585 av_log(avctx, AV_LOG_INFO, " Pin \"%s\"\n", buf);
587 dshow_cycle_formats(avctx, devtype, pin, NULL);
591 dshow_cycle_formats(avctx, devtype, pin, &format_set);
596 if (devtype == AudioDevice && ctx->audio_buffer_size) {
597 if (dshow_set_audio_buffer_size(avctx, pin) < 0)
601 if (IPin_EnumMediaTypes(pin, &types) != S_OK)
604 IEnumMediaTypes_Reset(types);
605 while (!device_pin && IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) {
606 if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
615 IEnumMediaTypes_Release(types);
617 IKsPropertySet_Release(p);
618 if (device_pin != pin)
622 IEnumPins_Release(pins);
625 if (set_format && !format_set) {
626 av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
630 av_log(avctx, AV_LOG_ERROR,
631 "Could not find output pin from %s capture device.\n", devtypename);
641 * List options for device with type devtype.
643 * @param devenum device enumerator used for accessing the device
646 dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
647 enum dshowDeviceType devtype)
649 struct dshow_ctx *ctx = avctx->priv_data;
650 IBaseFilter *device_filter = NULL;
653 if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0)
655 ctx->device_filter[devtype] = device_filter;
656 if ((r = dshow_cycle_pins(avctx, devtype, device_filter, NULL)) < 0)
663 dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
664 enum dshowDeviceType devtype)
666 struct dshow_ctx *ctx = avctx->priv_data;
667 IBaseFilter *device_filter = NULL;
668 IGraphBuilder *graph = ctx->graph;
669 IPin *device_pin = NULL;
670 libAVPin *capture_pin = NULL;
671 libAVFilter *capture_filter = NULL;
672 int ret = AVERROR(EIO);
675 const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
677 if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
682 ctx->device_filter [devtype] = device_filter;
684 r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
686 av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
690 if ((r = dshow_cycle_pins(avctx, devtype, device_filter, &device_pin)) < 0) {
694 ctx->device_pin[devtype] = device_pin;
696 capture_filter = libAVFilter_Create(avctx, callback, devtype);
697 if (!capture_filter) {
698 av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
701 ctx->capture_filter[devtype] = capture_filter;
703 r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
704 filter_name[devtype]);
706 av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
710 libAVPin_AddRef(capture_filter->pin);
711 capture_pin = capture_filter->pin;
712 ctx->capture_pin[devtype] = capture_pin;
714 r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
716 av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
726 static enum AVCodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
728 switch (sample_fmt) {
729 case AV_SAMPLE_FMT_U8: return AV_CODEC_ID_PCM_U8;
730 case AV_SAMPLE_FMT_S16: return AV_CODEC_ID_PCM_S16LE;
731 case AV_SAMPLE_FMT_S32: return AV_CODEC_ID_PCM_S32LE;
732 default: return AV_CODEC_ID_NONE; /* Should never happen. */
736 static enum AVSampleFormat sample_fmt_bits_per_sample(int bits)
739 case 8: return AV_SAMPLE_FMT_U8;
740 case 16: return AV_SAMPLE_FMT_S16;
741 case 32: return AV_SAMPLE_FMT_S32;
742 default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
747 dshow_add_device(AVFormatContext *avctx,
748 enum dshowDeviceType devtype)
750 struct dshow_ctx *ctx = avctx->priv_data;
752 AVCodecContext *codec;
754 int ret = AVERROR(EIO);
756 st = avformat_new_stream(avctx, NULL);
758 ret = AVERROR(ENOMEM);
763 ctx->capture_filter[devtype]->stream_index = st->index;
765 libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
768 if (devtype == VideoDevice) {
769 BITMAPINFOHEADER *bih = NULL;
770 AVRational time_base;
772 if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
773 VIDEOINFOHEADER *v = (void *) type.pbFormat;
774 time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
776 } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
777 VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
778 time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
782 av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
786 codec->time_base = time_base;
787 codec->codec_type = AVMEDIA_TYPE_VIDEO;
788 codec->width = bih->biWidth;
789 codec->height = bih->biHeight;
790 codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
791 if (codec->pix_fmt == AV_PIX_FMT_NONE) {
792 codec->codec_id = dshow_codecid(bih->biCompression);
793 if (codec->codec_id == AV_CODEC_ID_NONE) {
794 av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
795 "Please report verbose (-v 9) debug information.\n");
796 dshow_read_close(avctx);
797 return AVERROR_PATCHWELCOME;
799 codec->bits_per_coded_sample = bih->biBitCount;
801 codec->codec_id = AV_CODEC_ID_RAWVIDEO;
802 if (bih->biCompression == BI_RGB || bih->biCompression == BI_BITFIELDS) {
803 codec->bits_per_coded_sample = bih->biBitCount;
804 codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
805 if (codec->extradata) {
806 codec->extradata_size = 9;
807 memcpy(codec->extradata, "BottomUp", 9);
812 WAVEFORMATEX *fx = NULL;
814 if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
815 fx = (void *) type.pbFormat;
818 av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
822 codec->codec_type = AVMEDIA_TYPE_AUDIO;
823 codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
824 codec->codec_id = waveform_codec_id(codec->sample_fmt);
825 codec->sample_rate = fx->nSamplesPerSec;
826 codec->channels = fx->nChannels;
829 avpriv_set_pts_info(st, 64, 1, 10000000);
837 static int parse_device_name(AVFormatContext *avctx)
839 struct dshow_ctx *ctx = avctx->priv_data;
840 char **device_name = ctx->device_name;
841 char *name = av_strdup(avctx->filename);
846 while ((type = strtok(tmp, "="))) {
847 char *token = strtok(NULL, ":");
850 if (!strcmp(type, "video")) {
851 device_name[0] = token;
852 } else if (!strcmp(type, "audio")) {
853 device_name[1] = token;
855 device_name[0] = NULL;
856 device_name[1] = NULL;
861 if (!device_name[0] && !device_name[1]) {
865 device_name[0] = av_strdup(device_name[0]);
867 device_name[1] = av_strdup(device_name[1]);
874 static int dshow_read_header(AVFormatContext *avctx)
876 struct dshow_ctx *ctx = avctx->priv_data;
877 IGraphBuilder *graph = NULL;
878 ICreateDevEnum *devenum = NULL;
879 IMediaControl *control = NULL;
880 IMediaEvent *media_event = NULL;
881 HANDLE media_event_handle;
883 int ret = AVERROR(EIO);
886 if (!ctx->list_devices && !parse_device_name(avctx)) {
887 av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
891 ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id
892 : AV_CODEC_ID_RAWVIDEO;
893 if (ctx->pixel_format != AV_PIX_FMT_NONE) {
894 if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
895 av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when "
896 "video codec is not set or set to rawvideo\n");
897 ret = AVERROR(EINVAL);
901 if (ctx->framerate) {
902 r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
904 av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
911 r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
912 &IID_IGraphBuilder, (void **) &graph);
914 av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
919 r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
920 &IID_ICreateDevEnum, (void **) &devenum);
922 av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
926 if (ctx->list_devices) {
927 av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
928 dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
929 av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
930 dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
934 if (ctx->list_options) {
935 if (ctx->device_name[VideoDevice])
936 dshow_list_device_options(avctx, devenum, VideoDevice);
937 if (ctx->device_name[AudioDevice])
938 dshow_list_device_options(avctx, devenum, AudioDevice);
943 if (ctx->device_name[VideoDevice]) {
944 if ((r = dshow_open_device(avctx, devenum, VideoDevice)) < 0 ||
945 (r = dshow_add_device(avctx, VideoDevice)) < 0) {
950 if (ctx->device_name[AudioDevice]) {
951 if ((r = dshow_open_device(avctx, devenum, AudioDevice)) < 0 ||
952 (r = dshow_add_device(avctx, AudioDevice)) < 0) {
958 ctx->mutex = CreateMutex(NULL, 0, NULL);
960 av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
963 ctx->event[1] = CreateEvent(NULL, 1, 0, NULL);
964 if (!ctx->event[1]) {
965 av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
969 r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
971 av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
974 ctx->control = control;
976 r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event);
978 av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n");
981 ctx->media_event = media_event;
983 r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle);
985 av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n");
988 proc = GetCurrentProcess();
989 r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0],
990 0, 0, DUPLICATE_SAME_ACCESS);
992 av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n");
996 r = IMediaControl_Run(control);
999 r = IMediaControl_GetState(control, 0, &pfs);
1002 av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
1011 dshow_read_close(avctx);
1014 ICreateDevEnum_Release(devenum);
1020 * Checks media events from DirectShow and returns -1 on error or EOF. Also
1021 * purges all events that might be in the event queue to stop the trigger
1022 * of event notification.
1024 static int dshow_check_event_queue(IMediaEvent *media_event)
1030 while (IMediaEvent_GetEvent(media_event, &code, &p1, &p2, 0) != E_ABORT) {
1031 if (code == EC_COMPLETE || code == EC_DEVICE_LOST || code == EC_ERRORABORT)
1033 IMediaEvent_FreeEventParams(media_event, code, p1, p2);
1039 static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
1041 struct dshow_ctx *ctx = s->priv_data;
1042 AVPacketList *pktl = NULL;
1044 while (!ctx->eof && !pktl) {
1045 WaitForSingleObject(ctx->mutex, INFINITE);
1049 ctx->pktl = ctx->pktl->next;
1051 ctx->curbufsize -= pkt->size;
1053 ResetEvent(ctx->event[1]);
1054 ReleaseMutex(ctx->mutex);
1056 if (dshow_check_event_queue(ctx->media_event) < 0) {
1058 } else if (s->flags & AVFMT_FLAG_NONBLOCK) {
1059 return AVERROR(EAGAIN);
1061 WaitForMultipleObjects(2, ctx->event, 0, INFINITE);
1066 return ctx->eof ? AVERROR(EIO) : pkt->size;
1069 #define OFFSET(x) offsetof(struct dshow_ctx, x)
1070 #define DEC AV_OPT_FLAG_DECODING_PARAM
1071 static const AVOption options[] = {
1072 { "video_size", "set video size given a string such as 640x480 or hd720.", OFFSET(requested_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
1073 { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1, DEC },
1074 { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
1075 { "sample_rate", "set audio sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
1076 { "sample_size", "set audio sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 16, DEC },
1077 { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
1078 { "list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, DEC, "list_devices" },
1079 { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "list_devices" },
1080 { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "list_devices" },
1081 { "list_options", "list available options for specified device", OFFSET(list_options), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, DEC, "list_options" },
1082 { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "list_options" },
1083 { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "list_options" },
1084 { "video_device_number", "set video device number for devices with same name (starts at 0)", OFFSET(video_device_number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
1085 { "audio_device_number", "set audio device number for devices with same name (starts at 0)", OFFSET(audio_device_number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
1086 { "audio_buffer_size", "set audio device buffer latency size in milliseconds (default is the device's default)", OFFSET(audio_buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
1090 static const AVClass dshow_class = {
1091 .class_name = "dshow indev",
1092 .item_name = av_default_item_name,
1094 .version = LIBAVUTIL_VERSION_INT,
1097 AVInputFormat ff_dshow_demuxer = {
1099 .long_name = NULL_IF_CONFIG_SMALL("DirectShow capture"),
1100 .priv_data_size = sizeof(struct dshow_ctx),
1101 .read_header = dshow_read_header,
1102 .read_packet = dshow_read_packet,
1103 .read_close = dshow_read_close,
1104 .flags = AVFMT_NOFILE,
1105 .priv_class = &dshow_class,