dshow.c 34 KB

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