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.

974 lines
30KB

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