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.

959 lines
29KB

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