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.

1026 lines
32KB

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