You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1108 lines
36KB

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