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