dshow.c 29 KB

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