dshow.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. #include "libavutil/parseutils.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/opt.h"
  24. #include "libavformat/internal.h"
  25. #include "libavformat/riff.h"
  26. #include "avdevice.h"
  27. #include "dshow_capture.h"
  28. #include "libavcodec/raw.h"
  29. struct dshow_ctx {
  30. const AVClass *class;
  31. IGraphBuilder *graph;
  32. char *device_name[2];
  33. int video_device_number;
  34. int audio_device_number;
  35. int list_options;
  36. int list_devices;
  37. int audio_buffer_size;
  38. IBaseFilter *device_filter[2];
  39. IPin *device_pin[2];
  40. libAVFilter *capture_filter[2];
  41. libAVPin *capture_pin[2];
  42. HANDLE mutex;
  43. HANDLE event[2]; /* event[0] is set by DirectShow
  44. * event[1] is set by callback() */
  45. AVPacketList *pktl;
  46. int eof;
  47. int64_t curbufsize[2];
  48. unsigned int video_frame_num;
  49. IMediaControl *control;
  50. IMediaEvent *media_event;
  51. enum AVPixelFormat pixel_format;
  52. enum AVCodecID video_codec_id;
  53. char *framerate;
  54. int requested_width;
  55. int requested_height;
  56. AVRational requested_framerate;
  57. int sample_rate;
  58. int sample_size;
  59. int channels;
  60. };
  61. static enum AVPixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
  62. {
  63. switch(biCompression) {
  64. case BI_BITFIELDS:
  65. case BI_RGB:
  66. switch(biBitCount) { /* 1-8 are untested */
  67. case 1:
  68. return AV_PIX_FMT_MONOWHITE;
  69. case 4:
  70. return AV_PIX_FMT_RGB4;
  71. case 8:
  72. return AV_PIX_FMT_RGB8;
  73. case 16:
  74. return AV_PIX_FMT_RGB555;
  75. case 24:
  76. return AV_PIX_FMT_BGR24;
  77. case 32:
  78. return AV_PIX_FMT_0RGB32;
  79. }
  80. }
  81. return avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), biCompression); // all others
  82. }
  83. static int
  84. dshow_read_close(AVFormatContext *s)
  85. {
  86. struct dshow_ctx *ctx = s->priv_data;
  87. AVPacketList *pktl;
  88. if (ctx->control) {
  89. IMediaControl_Stop(ctx->control);
  90. IMediaControl_Release(ctx->control);
  91. }
  92. if (ctx->media_event)
  93. IMediaEvent_Release(ctx->media_event);
  94. if (ctx->graph) {
  95. IEnumFilters *fenum;
  96. int r;
  97. r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
  98. if (r == S_OK) {
  99. IBaseFilter *f;
  100. IEnumFilters_Reset(fenum);
  101. while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK) {
  102. if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK)
  103. IEnumFilters_Reset(fenum); /* When a filter is removed,
  104. * the list must be reset. */
  105. IBaseFilter_Release(f);
  106. }
  107. IEnumFilters_Release(fenum);
  108. }
  109. IGraphBuilder_Release(ctx->graph);
  110. }
  111. if (ctx->capture_pin[VideoDevice])
  112. libAVPin_Release(ctx->capture_pin[VideoDevice]);
  113. if (ctx->capture_pin[AudioDevice])
  114. libAVPin_Release(ctx->capture_pin[AudioDevice]);
  115. if (ctx->capture_filter[VideoDevice])
  116. libAVFilter_Release(ctx->capture_filter[VideoDevice]);
  117. if (ctx->capture_filter[AudioDevice])
  118. libAVFilter_Release(ctx->capture_filter[AudioDevice]);
  119. if (ctx->device_pin[VideoDevice])
  120. IPin_Release(ctx->device_pin[VideoDevice]);
  121. if (ctx->device_pin[AudioDevice])
  122. IPin_Release(ctx->device_pin[AudioDevice]);
  123. if (ctx->device_filter[VideoDevice])
  124. IBaseFilter_Release(ctx->device_filter[VideoDevice]);
  125. if (ctx->device_filter[AudioDevice])
  126. IBaseFilter_Release(ctx->device_filter[AudioDevice]);
  127. if (ctx->device_name[0])
  128. av_free(ctx->device_name[0]);
  129. if (ctx->device_name[1])
  130. av_free(ctx->device_name[1]);
  131. if(ctx->mutex)
  132. CloseHandle(ctx->mutex);
  133. if(ctx->event[0])
  134. CloseHandle(ctx->event[0]);
  135. if(ctx->event[1])
  136. CloseHandle(ctx->event[1]);
  137. pktl = ctx->pktl;
  138. while (pktl) {
  139. AVPacketList *next = pktl->next;
  140. av_destruct_packet(&pktl->pkt);
  141. av_free(pktl);
  142. pktl = next;
  143. }
  144. CoUninitialize();
  145. return 0;
  146. }
  147. static char *dup_wchar_to_utf8(wchar_t *w)
  148. {
  149. char *s = NULL;
  150. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  151. s = av_malloc(l);
  152. if (s)
  153. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  154. return s;
  155. }
  156. static int shall_we_drop(AVFormatContext *s, int index, enum dshowDeviceType devtype)
  157. {
  158. struct dshow_ctx *ctx = s->priv_data;
  159. static const uint8_t dropscore[] = {62, 75, 87, 100};
  160. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  161. unsigned int buffer_fullness = (ctx->curbufsize[index]*100)/s->max_picture_buffer;
  162. if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
  163. av_log(s, AV_LOG_ERROR,
  164. "real-time buffer[%s] too full (%d%% of size: %d)! frame dropped!\n", ctx->device_name[devtype], buffer_fullness, s->max_picture_buffer);
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. static void
  170. callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
  171. {
  172. AVFormatContext *s = priv_data;
  173. struct dshow_ctx *ctx = s->priv_data;
  174. AVPacketList **ppktl, *pktl_next;
  175. // dump_videohdr(s, vdhdr);
  176. WaitForSingleObject(ctx->mutex, INFINITE);
  177. if(shall_we_drop(s, index, devtype))
  178. goto fail;
  179. pktl_next = av_mallocz(sizeof(AVPacketList));
  180. if(!pktl_next)
  181. goto fail;
  182. if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
  183. av_free(pktl_next);
  184. goto fail;
  185. }
  186. pktl_next->pkt.stream_index = index;
  187. pktl_next->pkt.pts = time;
  188. memcpy(pktl_next->pkt.data, buf, buf_size);
  189. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  190. *ppktl = pktl_next;
  191. ctx->curbufsize[index] += buf_size;
  192. SetEvent(ctx->event[1]);
  193. ReleaseMutex(ctx->mutex);
  194. return;
  195. fail:
  196. ReleaseMutex(ctx->mutex);
  197. return;
  198. }
  199. /**
  200. * Cycle through available devices using the device enumerator devenum,
  201. * retrieve the device with type specified by devtype and return the
  202. * pointer to the object found in *pfilter.
  203. * If pfilter is NULL, list all device names.
  204. */
  205. static int
  206. dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
  207. enum dshowDeviceType devtype, IBaseFilter **pfilter)
  208. {
  209. struct dshow_ctx *ctx = avctx->priv_data;
  210. IBaseFilter *device_filter = NULL;
  211. IEnumMoniker *classenum = NULL;
  212. IMoniker *m = NULL;
  213. const char *device_name = ctx->device_name[devtype];
  214. int skip = (devtype == VideoDevice) ? ctx->video_device_number
  215. : ctx->audio_device_number;
  216. int r;
  217. const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
  218. &CLSID_AudioInputDeviceCategory };
  219. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  220. r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
  221. (IEnumMoniker **) &classenum, 0);
  222. if (r != S_OK) {
  223. av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
  224. devtypename);
  225. return AVERROR(EIO);
  226. }
  227. while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) {
  228. IPropertyBag *bag = NULL;
  229. char *buf = NULL;
  230. VARIANT var;
  231. r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
  232. if (r != S_OK)
  233. goto fail1;
  234. var.vt = VT_BSTR;
  235. r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
  236. if (r != S_OK)
  237. goto fail1;
  238. buf = dup_wchar_to_utf8(var.bstrVal);
  239. if (pfilter) {
  240. if (strcmp(device_name, buf))
  241. goto fail1;
  242. if (!skip--)
  243. IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
  244. } else {
  245. av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf);
  246. }
  247. fail1:
  248. if (buf)
  249. av_free(buf);
  250. if (bag)
  251. IPropertyBag_Release(bag);
  252. IMoniker_Release(m);
  253. }
  254. IEnumMoniker_Release(classenum);
  255. if (pfilter) {
  256. if (!device_filter) {
  257. av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
  258. devtypename);
  259. return AVERROR(EIO);
  260. }
  261. *pfilter = device_filter;
  262. }
  263. return 0;
  264. }
  265. /**
  266. * Cycle through available formats using the specified pin,
  267. * try to set parameters specified through AVOptions and if successful
  268. * return 1 in *pformat_set.
  269. * If pformat_set is NULL, list all pin capabilities.
  270. */
  271. static void
  272. dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
  273. IPin *pin, int *pformat_set)
  274. {
  275. struct dshow_ctx *ctx = avctx->priv_data;
  276. IAMStreamConfig *config = NULL;
  277. AM_MEDIA_TYPE *type = NULL;
  278. int format_set = 0;
  279. void *caps = NULL;
  280. int i, n, size;
  281. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  282. return;
  283. if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
  284. goto end;
  285. caps = av_malloc(size);
  286. if (!caps)
  287. goto end;
  288. for (i = 0; i < n && !format_set; i++) {
  289. IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
  290. #if DSHOWDEBUG
  291. ff_print_AM_MEDIA_TYPE(type);
  292. #endif
  293. if (devtype == VideoDevice) {
  294. VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
  295. BITMAPINFOHEADER *bih;
  296. int64_t *fr;
  297. const AVCodecTag *const tags[] = { avformat_get_riff_video_tags(), NULL };
  298. #if DSHOWDEBUG
  299. ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
  300. #endif
  301. if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
  302. VIDEOINFOHEADER *v = (void *) type->pbFormat;
  303. fr = &v->AvgTimePerFrame;
  304. bih = &v->bmiHeader;
  305. } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
  306. VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
  307. fr = &v->AvgTimePerFrame;
  308. bih = &v->bmiHeader;
  309. } else {
  310. goto next;
  311. }
  312. if (!pformat_set) {
  313. enum AVPixelFormat pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  314. if (pix_fmt == AV_PIX_FMT_NONE) {
  315. enum AVCodecID codec_id = av_codec_get_id(tags, bih->biCompression);
  316. AVCodec *codec = avcodec_find_decoder(codec_id);
  317. if (codec_id == AV_CODEC_ID_NONE || !codec) {
  318. av_log(avctx, AV_LOG_INFO, " unknown compression type 0x%X", (int) bih->biCompression);
  319. } else {
  320. av_log(avctx, AV_LOG_INFO, " vcodec=%s", codec->name);
  321. }
  322. } else {
  323. av_log(avctx, AV_LOG_INFO, " pixel_format=%s", av_get_pix_fmt_name(pix_fmt));
  324. }
  325. av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g\n",
  326. vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
  327. 1e7 / vcaps->MaxFrameInterval,
  328. vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
  329. 1e7 / vcaps->MinFrameInterval);
  330. continue;
  331. }
  332. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  333. if (ctx->video_codec_id != av_codec_get_id(tags, bih->biCompression))
  334. goto next;
  335. }
  336. if (ctx->pixel_format != AV_PIX_FMT_NONE &&
  337. ctx->pixel_format != dshow_pixfmt(bih->biCompression, bih->biBitCount)) {
  338. goto next;
  339. }
  340. if (ctx->framerate) {
  341. int64_t framerate = ((int64_t) ctx->requested_framerate.den*10000000)
  342. / ctx->requested_framerate.num;
  343. if (framerate > vcaps->MaxFrameInterval ||
  344. framerate < vcaps->MinFrameInterval)
  345. goto next;
  346. *fr = framerate;
  347. }
  348. if (ctx->requested_width && ctx->requested_height) {
  349. if (ctx->requested_width > vcaps->MaxOutputSize.cx ||
  350. ctx->requested_width < vcaps->MinOutputSize.cx ||
  351. ctx->requested_height > vcaps->MaxOutputSize.cy ||
  352. ctx->requested_height < vcaps->MinOutputSize.cy)
  353. goto next;
  354. bih->biWidth = ctx->requested_width;
  355. bih->biHeight = ctx->requested_height;
  356. }
  357. } else {
  358. AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
  359. WAVEFORMATEX *fx;
  360. #if DSHOWDEBUG
  361. ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
  362. #endif
  363. if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
  364. fx = (void *) type->pbFormat;
  365. } else {
  366. goto next;
  367. }
  368. if (!pformat_set) {
  369. av_log(avctx, AV_LOG_INFO, " min ch=%lu bits=%lu rate=%6lu max ch=%lu bits=%lu rate=%6lu\n",
  370. acaps->MinimumChannels, acaps->MinimumBitsPerSample, acaps->MinimumSampleFrequency,
  371. acaps->MaximumChannels, acaps->MaximumBitsPerSample, acaps->MaximumSampleFrequency);
  372. continue;
  373. }
  374. if (ctx->sample_rate) {
  375. if (ctx->sample_rate > acaps->MaximumSampleFrequency ||
  376. ctx->sample_rate < acaps->MinimumSampleFrequency)
  377. goto next;
  378. fx->nSamplesPerSec = ctx->sample_rate;
  379. }
  380. if (ctx->sample_size) {
  381. if (ctx->sample_size > acaps->MaximumBitsPerSample ||
  382. ctx->sample_size < acaps->MinimumBitsPerSample)
  383. goto next;
  384. fx->wBitsPerSample = ctx->sample_size;
  385. }
  386. if (ctx->channels) {
  387. if (ctx->channels > acaps->MaximumChannels ||
  388. ctx->channels < acaps->MinimumChannels)
  389. goto next;
  390. fx->nChannels = ctx->channels;
  391. }
  392. }
  393. if (IAMStreamConfig_SetFormat(config, type) != S_OK)
  394. goto next;
  395. format_set = 1;
  396. next:
  397. if (type->pbFormat)
  398. CoTaskMemFree(type->pbFormat);
  399. CoTaskMemFree(type);
  400. }
  401. end:
  402. IAMStreamConfig_Release(config);
  403. if (caps)
  404. av_free(caps);
  405. if (pformat_set)
  406. *pformat_set = format_set;
  407. }
  408. /**
  409. * Set audio device buffer size in milliseconds (which can directly impact
  410. * latency, depending on the device).
  411. */
  412. static int
  413. dshow_set_audio_buffer_size(AVFormatContext *avctx, IPin *pin)
  414. {
  415. struct dshow_ctx *ctx = avctx->priv_data;
  416. IAMBufferNegotiation *buffer_negotiation = NULL;
  417. ALLOCATOR_PROPERTIES props = { -1, -1, -1, -1 };
  418. IAMStreamConfig *config = NULL;
  419. AM_MEDIA_TYPE *type = NULL;
  420. int ret = AVERROR(EIO);
  421. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  422. goto end;
  423. if (IAMStreamConfig_GetFormat(config, &type) != S_OK)
  424. goto end;
  425. if (!IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx))
  426. goto end;
  427. props.cbBuffer = (((WAVEFORMATEX *) type->pbFormat)->nAvgBytesPerSec)
  428. * ctx->audio_buffer_size / 1000;
  429. if (IPin_QueryInterface(pin, &IID_IAMBufferNegotiation, (void **) &buffer_negotiation) != S_OK)
  430. goto end;
  431. if (IAMBufferNegotiation_SuggestAllocatorProperties(buffer_negotiation, &props) != S_OK)
  432. goto end;
  433. ret = 0;
  434. end:
  435. if (buffer_negotiation)
  436. IAMBufferNegotiation_Release(buffer_negotiation);
  437. if (type) {
  438. if (type->pbFormat)
  439. CoTaskMemFree(type->pbFormat);
  440. CoTaskMemFree(type);
  441. }
  442. if (config)
  443. IAMStreamConfig_Release(config);
  444. return ret;
  445. }
  446. /**
  447. * Cycle through available pins using the device_filter device, of type
  448. * devtype, retrieve the first output pin and return the pointer to the
  449. * object found in *ppin.
  450. * If ppin is NULL, cycle through all pins listing audio/video capabilities.
  451. */
  452. static int
  453. dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
  454. IBaseFilter *device_filter, IPin **ppin)
  455. {
  456. struct dshow_ctx *ctx = avctx->priv_data;
  457. IEnumPins *pins = 0;
  458. IPin *device_pin = NULL;
  459. IPin *pin;
  460. int r;
  461. const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
  462. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  463. int set_format = (devtype == VideoDevice && (ctx->framerate ||
  464. (ctx->requested_width && ctx->requested_height) ||
  465. ctx->pixel_format != AV_PIX_FMT_NONE ||
  466. ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO))
  467. || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate));
  468. int format_set = 0;
  469. r = IBaseFilter_EnumPins(device_filter, &pins);
  470. if (r != S_OK) {
  471. av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
  472. return AVERROR(EIO);
  473. }
  474. if (!ppin) {
  475. av_log(avctx, AV_LOG_INFO, "DirectShow %s device options\n",
  476. devtypename);
  477. }
  478. while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
  479. IKsPropertySet *p = NULL;
  480. IEnumMediaTypes *types = NULL;
  481. PIN_INFO info = {0};
  482. AM_MEDIA_TYPE *type;
  483. GUID category;
  484. DWORD r2;
  485. IPin_QueryPinInfo(pin, &info);
  486. IBaseFilter_Release(info.pFilter);
  487. if (info.dir != PINDIR_OUTPUT)
  488. goto next;
  489. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  490. goto next;
  491. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  492. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  493. goto next;
  494. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  495. goto next;
  496. if (!ppin) {
  497. char *buf = dup_wchar_to_utf8(info.achName);
  498. av_log(avctx, AV_LOG_INFO, " Pin \"%s\"\n", buf);
  499. av_free(buf);
  500. dshow_cycle_formats(avctx, devtype, pin, NULL);
  501. goto next;
  502. }
  503. if (set_format) {
  504. dshow_cycle_formats(avctx, devtype, pin, &format_set);
  505. if (!format_set) {
  506. goto next;
  507. }
  508. }
  509. if (devtype == AudioDevice && ctx->audio_buffer_size) {
  510. if (dshow_set_audio_buffer_size(avctx, pin) < 0) {
  511. av_log(avctx, AV_LOG_ERROR, "unable to set audio buffer size %d to pin, using pin anyway...", ctx->audio_buffer_size);
  512. }
  513. }
  514. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  515. goto next;
  516. IEnumMediaTypes_Reset(types);
  517. while (!device_pin && IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) {
  518. if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
  519. device_pin = pin;
  520. goto next;
  521. }
  522. CoTaskMemFree(type);
  523. }
  524. next:
  525. if (types)
  526. IEnumMediaTypes_Release(types);
  527. if (p)
  528. IKsPropertySet_Release(p);
  529. if (device_pin != pin)
  530. IPin_Release(pin);
  531. }
  532. IEnumPins_Release(pins);
  533. if (ppin) {
  534. if (set_format && !format_set) {
  535. av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
  536. return AVERROR(EIO);
  537. }
  538. if (!device_pin) {
  539. av_log(avctx, AV_LOG_ERROR,
  540. "Could not find output pin from %s capture device.\n", devtypename);
  541. return AVERROR(EIO);
  542. }
  543. *ppin = device_pin;
  544. }
  545. return 0;
  546. }
  547. /**
  548. * List options for device with type devtype.
  549. *
  550. * @param devenum device enumerator used for accessing the device
  551. */
  552. static int
  553. dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
  554. enum dshowDeviceType devtype)
  555. {
  556. struct dshow_ctx *ctx = avctx->priv_data;
  557. IBaseFilter *device_filter = NULL;
  558. int r;
  559. if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0)
  560. return r;
  561. ctx->device_filter[devtype] = device_filter;
  562. if ((r = dshow_cycle_pins(avctx, devtype, device_filter, NULL)) < 0)
  563. return r;
  564. return 0;
  565. }
  566. static int
  567. dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
  568. enum dshowDeviceType devtype)
  569. {
  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);
  577. int r;
  578. const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
  579. if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
  580. ret = r;
  581. goto error;
  582. }
  583. ctx->device_filter [devtype] = device_filter;
  584. r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
  585. if (r != S_OK) {
  586. av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
  587. goto error;
  588. }
  589. if ((r = dshow_cycle_pins(avctx, devtype, device_filter, &device_pin)) < 0) {
  590. ret = r;
  591. goto error;
  592. }
  593. ctx->device_pin[devtype] = device_pin;
  594. capture_filter = libAVFilter_Create(avctx, callback, devtype);
  595. if (!capture_filter) {
  596. av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
  597. goto error;
  598. }
  599. ctx->capture_filter[devtype] = capture_filter;
  600. r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
  601. filter_name[devtype]);
  602. if (r != S_OK) {
  603. av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
  604. goto error;
  605. }
  606. libAVPin_AddRef(capture_filter->pin);
  607. capture_pin = capture_filter->pin;
  608. ctx->capture_pin[devtype] = capture_pin;
  609. r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
  610. if (r != S_OK) {
  611. av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
  612. goto error;
  613. }
  614. ret = 0;
  615. error:
  616. return ret;
  617. }
  618. static enum AVCodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
  619. {
  620. switch (sample_fmt) {
  621. case AV_SAMPLE_FMT_U8: return AV_CODEC_ID_PCM_U8;
  622. case AV_SAMPLE_FMT_S16: return AV_CODEC_ID_PCM_S16LE;
  623. case AV_SAMPLE_FMT_S32: return AV_CODEC_ID_PCM_S32LE;
  624. default: return AV_CODEC_ID_NONE; /* Should never happen. */
  625. }
  626. }
  627. static enum AVSampleFormat sample_fmt_bits_per_sample(int bits)
  628. {
  629. switch (bits) {
  630. case 8: return AV_SAMPLE_FMT_U8;
  631. case 16: return AV_SAMPLE_FMT_S16;
  632. case 32: return AV_SAMPLE_FMT_S32;
  633. default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
  634. }
  635. }
  636. static int
  637. dshow_add_device(AVFormatContext *avctx,
  638. enum dshowDeviceType devtype)
  639. {
  640. struct dshow_ctx *ctx = avctx->priv_data;
  641. AM_MEDIA_TYPE type;
  642. AVCodecContext *codec;
  643. AVStream *st;
  644. int ret = AVERROR(EIO);
  645. st = avformat_new_stream(avctx, NULL);
  646. if (!st) {
  647. ret = AVERROR(ENOMEM);
  648. goto error;
  649. }
  650. st->id = devtype;
  651. ctx->capture_filter[devtype]->stream_index = st->index;
  652. libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
  653. codec = st->codec;
  654. if (devtype == VideoDevice) {
  655. BITMAPINFOHEADER *bih = NULL;
  656. AVRational time_base;
  657. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  658. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  659. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  660. bih = &v->bmiHeader;
  661. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  662. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  663. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  664. bih = &v->bmiHeader;
  665. }
  666. if (!bih) {
  667. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  668. goto error;
  669. }
  670. codec->time_base = time_base;
  671. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  672. codec->width = bih->biWidth;
  673. codec->height = bih->biHeight;
  674. codec->codec_tag = bih->biCompression;
  675. codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  676. if (bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
  677. av_log(avctx, AV_LOG_DEBUG, "attempt to use full range for HDYC...\n");
  678. codec->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
  679. }
  680. if (codec->pix_fmt == AV_PIX_FMT_NONE) {
  681. const AVCodecTag *const tags[] = { avformat_get_riff_video_tags(), NULL };
  682. codec->codec_id = av_codec_get_id(tags, bih->biCompression);
  683. if (codec->codec_id == AV_CODEC_ID_NONE) {
  684. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  685. "Please report type 0x%X.\n", (int) bih->biCompression);
  686. return AVERROR_PATCHWELCOME;
  687. }
  688. codec->bits_per_coded_sample = bih->biBitCount;
  689. } else {
  690. codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  691. if (bih->biCompression == BI_RGB || bih->biCompression == BI_BITFIELDS) {
  692. codec->bits_per_coded_sample = bih->biBitCount;
  693. codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  694. if (codec->extradata) {
  695. codec->extradata_size = 9;
  696. memcpy(codec->extradata, "BottomUp", 9);
  697. }
  698. }
  699. }
  700. } else {
  701. WAVEFORMATEX *fx = NULL;
  702. if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  703. fx = (void *) type.pbFormat;
  704. }
  705. if (!fx) {
  706. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  707. goto error;
  708. }
  709. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  710. codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
  711. codec->codec_id = waveform_codec_id(codec->sample_fmt);
  712. codec->sample_rate = fx->nSamplesPerSec;
  713. codec->channels = fx->nChannels;
  714. }
  715. avpriv_set_pts_info(st, 64, 1, 10000000);
  716. ret = 0;
  717. error:
  718. return ret;
  719. }
  720. static int parse_device_name(AVFormatContext *avctx)
  721. {
  722. struct dshow_ctx *ctx = avctx->priv_data;
  723. char **device_name = ctx->device_name;
  724. char *name = av_strdup(avctx->filename);
  725. char *tmp = name;
  726. int ret = 1;
  727. char *type;
  728. while ((type = strtok(tmp, "="))) {
  729. char *token = strtok(NULL, ":");
  730. tmp = NULL;
  731. if (!strcmp(type, "video")) {
  732. device_name[0] = token;
  733. } else if (!strcmp(type, "audio")) {
  734. device_name[1] = token;
  735. } else {
  736. device_name[0] = NULL;
  737. device_name[1] = NULL;
  738. break;
  739. }
  740. }
  741. if (!device_name[0] && !device_name[1]) {
  742. ret = 0;
  743. } else {
  744. if (device_name[0])
  745. device_name[0] = av_strdup(device_name[0]);
  746. if (device_name[1])
  747. device_name[1] = av_strdup(device_name[1]);
  748. }
  749. av_free(name);
  750. return ret;
  751. }
  752. static int dshow_read_header(AVFormatContext *avctx)
  753. {
  754. struct dshow_ctx *ctx = avctx->priv_data;
  755. IGraphBuilder *graph = NULL;
  756. ICreateDevEnum *devenum = NULL;
  757. IMediaControl *control = NULL;
  758. IMediaEvent *media_event = NULL;
  759. HANDLE media_event_handle;
  760. HANDLE proc;
  761. int ret = AVERROR(EIO);
  762. int r;
  763. CoInitialize(0);
  764. if (!ctx->list_devices && !parse_device_name(avctx)) {
  765. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  766. goto error;
  767. }
  768. ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id
  769. : AV_CODEC_ID_RAWVIDEO;
  770. if (ctx->pixel_format != AV_PIX_FMT_NONE) {
  771. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  772. av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when "
  773. "video codec is not set or set to rawvideo\n");
  774. ret = AVERROR(EINVAL);
  775. goto error;
  776. }
  777. }
  778. if (ctx->framerate) {
  779. r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
  780. if (r < 0) {
  781. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
  782. goto error;
  783. }
  784. }
  785. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  786. &IID_IGraphBuilder, (void **) &graph);
  787. if (r != S_OK) {
  788. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  789. goto error;
  790. }
  791. ctx->graph = graph;
  792. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  793. &IID_ICreateDevEnum, (void **) &devenum);
  794. if (r != S_OK) {
  795. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  796. goto error;
  797. }
  798. if (ctx->list_devices) {
  799. av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
  800. dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
  801. av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
  802. dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
  803. ret = AVERROR_EXIT;
  804. goto error;
  805. }
  806. if (ctx->list_options) {
  807. if (ctx->device_name[VideoDevice])
  808. dshow_list_device_options(avctx, devenum, VideoDevice);
  809. if (ctx->device_name[AudioDevice])
  810. dshow_list_device_options(avctx, devenum, AudioDevice);
  811. ret = AVERROR_EXIT;
  812. goto error;
  813. }
  814. if (ctx->device_name[VideoDevice]) {
  815. if ((r = dshow_open_device(avctx, devenum, VideoDevice)) < 0 ||
  816. (r = dshow_add_device(avctx, VideoDevice)) < 0) {
  817. ret = r;
  818. goto error;
  819. }
  820. }
  821. if (ctx->device_name[AudioDevice]) {
  822. if ((r = dshow_open_device(avctx, devenum, AudioDevice)) < 0 ||
  823. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  824. ret = r;
  825. goto error;
  826. }
  827. }
  828. ctx->curbufsize[0] = 0;
  829. ctx->curbufsize[1] = 0;
  830. ctx->mutex = CreateMutex(NULL, 0, NULL);
  831. if (!ctx->mutex) {
  832. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  833. goto error;
  834. }
  835. ctx->event[1] = CreateEvent(NULL, 1, 0, NULL);
  836. if (!ctx->event[1]) {
  837. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  838. goto error;
  839. }
  840. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  841. if (r != S_OK) {
  842. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  843. goto error;
  844. }
  845. ctx->control = control;
  846. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event);
  847. if (r != S_OK) {
  848. av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n");
  849. goto error;
  850. }
  851. ctx->media_event = media_event;
  852. r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle);
  853. if (r != S_OK) {
  854. av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n");
  855. goto error;
  856. }
  857. proc = GetCurrentProcess();
  858. r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0],
  859. 0, 0, DUPLICATE_SAME_ACCESS);
  860. if (!r) {
  861. av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n");
  862. goto error;
  863. }
  864. r = IMediaControl_Run(control);
  865. if (r == S_FALSE) {
  866. OAFilterState pfs;
  867. r = IMediaControl_GetState(control, 0, &pfs);
  868. }
  869. if (r != S_OK) {
  870. av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
  871. goto error;
  872. }
  873. ret = 0;
  874. error:
  875. if (devenum)
  876. ICreateDevEnum_Release(devenum);
  877. if (ret < 0)
  878. dshow_read_close(avctx);
  879. return ret;
  880. }
  881. /**
  882. * Checks media events from DirectShow and returns -1 on error or EOF. Also
  883. * purges all events that might be in the event queue to stop the trigger
  884. * of event notification.
  885. */
  886. static int dshow_check_event_queue(IMediaEvent *media_event)
  887. {
  888. LONG_PTR p1, p2;
  889. long code;
  890. int ret = 0;
  891. while (IMediaEvent_GetEvent(media_event, &code, &p1, &p2, 0) != E_ABORT) {
  892. if (code == EC_COMPLETE || code == EC_DEVICE_LOST || code == EC_ERRORABORT)
  893. ret = -1;
  894. IMediaEvent_FreeEventParams(media_event, code, p1, p2);
  895. }
  896. return ret;
  897. }
  898. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  899. {
  900. struct dshow_ctx *ctx = s->priv_data;
  901. AVPacketList *pktl = NULL;
  902. while (!ctx->eof && !pktl) {
  903. WaitForSingleObject(ctx->mutex, INFINITE);
  904. pktl = ctx->pktl;
  905. if (pktl) {
  906. *pkt = pktl->pkt;
  907. ctx->pktl = ctx->pktl->next;
  908. av_free(pktl);
  909. ctx->curbufsize[pkt->stream_index] -= pkt->size;
  910. }
  911. ResetEvent(ctx->event[1]);
  912. ReleaseMutex(ctx->mutex);
  913. if (!pktl) {
  914. if (dshow_check_event_queue(ctx->media_event) < 0) {
  915. ctx->eof = 1;
  916. } else if (s->flags & AVFMT_FLAG_NONBLOCK) {
  917. return AVERROR(EAGAIN);
  918. } else {
  919. WaitForMultipleObjects(2, ctx->event, 0, INFINITE);
  920. }
  921. }
  922. }
  923. return ctx->eof ? AVERROR(EIO) : pkt->size;
  924. }
  925. #define OFFSET(x) offsetof(struct dshow_ctx, x)
  926. #define DEC AV_OPT_FLAG_DECODING_PARAM
  927. static const AVOption options[] = {
  928. { "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 },
  929. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX, DEC },
  930. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  931. { "sample_rate", "set audio sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  932. { "sample_size", "set audio sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 16, DEC },
  933. { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  934. { "list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, DEC, "list_devices" },
  935. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "list_devices" },
  936. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "list_devices" },
  937. { "list_options", "list available options for specified device", OFFSET(list_options), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, DEC, "list_options" },
  938. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "list_options" },
  939. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "list_options" },
  940. { "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 },
  941. { "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 },
  942. { "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 },
  943. { NULL },
  944. };
  945. static const AVClass dshow_class = {
  946. .class_name = "dshow indev",
  947. .item_name = av_default_item_name,
  948. .option = options,
  949. .version = LIBAVUTIL_VERSION_INT,
  950. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  951. };
  952. AVInputFormat ff_dshow_demuxer = {
  953. .name = "dshow",
  954. .long_name = NULL_IF_CONFIG_SMALL("DirectShow capture"),
  955. .priv_data_size = sizeof(struct dshow_ctx),
  956. .read_header = dshow_read_header,
  957. .read_packet = dshow_read_packet,
  958. .read_close = dshow_read_close,
  959. .flags = AVFMT_NOFILE,
  960. .priv_class = &dshow_class,
  961. };