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.

652 lines
18KB

  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 "avdevice.h"
  22. #include "dshow.h"
  23. struct dshow_ctx {
  24. IGraphBuilder *graph;
  25. char *device_name[2];
  26. IBaseFilter *device_filter[2];
  27. IPin *device_pin[2];
  28. libAVFilter *capture_filter[2];
  29. libAVPin *capture_pin[2];
  30. HANDLE mutex;
  31. HANDLE event;
  32. AVPacketList *pktl;
  33. unsigned int curbufsize;
  34. unsigned int video_frame_num;
  35. IMediaControl *control;
  36. };
  37. static enum PixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
  38. {
  39. switch(biCompression) {
  40. case MKTAG('U', 'Y', 'V', 'Y'):
  41. return PIX_FMT_UYVY422;
  42. case MKTAG('Y', 'U', 'Y', '2'):
  43. return PIX_FMT_YUYV422;
  44. case MKTAG('I', '4', '2', '0'):
  45. return PIX_FMT_YUV420P;
  46. case BI_RGB:
  47. switch(biBitCount) { /* 1-8 are untested */
  48. case 1:
  49. return PIX_FMT_MONOWHITE;
  50. case 4:
  51. return PIX_FMT_RGB4;
  52. case 8:
  53. return PIX_FMT_RGB8;
  54. case 16:
  55. return PIX_FMT_RGB555;
  56. case 24:
  57. return PIX_FMT_BGR24;
  58. case 32:
  59. return PIX_FMT_RGB32;
  60. }
  61. }
  62. return PIX_FMT_NONE;
  63. }
  64. static enum CodecID dshow_codecid(DWORD biCompression)
  65. {
  66. switch(biCompression) {
  67. case MKTAG('d', 'v', 's', 'd'):
  68. return CODEC_ID_DVVIDEO;
  69. case MKTAG('M', 'J', 'P', 'G'):
  70. case MKTAG('m', 'j', 'p', 'g'):
  71. return CODEC_ID_MJPEG;
  72. }
  73. return CODEC_ID_NONE;
  74. }
  75. static int
  76. dshow_read_close(AVFormatContext *s)
  77. {
  78. struct dshow_ctx *ctx = s->priv_data;
  79. AVPacketList *pktl;
  80. if (ctx->control) {
  81. IMediaControl_Stop(ctx->control);
  82. IMediaControl_Release(ctx->control);
  83. }
  84. if (ctx->capture_pin[VideoDevice])
  85. libAVPin_Release(ctx->capture_pin[VideoDevice]);
  86. if (ctx->capture_pin[AudioDevice])
  87. libAVPin_Release(ctx->capture_pin[AudioDevice]);
  88. if (ctx->capture_filter[VideoDevice])
  89. libAVFilter_Release(ctx->capture_filter[VideoDevice]);
  90. if (ctx->capture_filter[AudioDevice])
  91. libAVFilter_Release(ctx->capture_filter[AudioDevice]);
  92. if (ctx->device_pin[VideoDevice])
  93. IPin_Release(ctx->device_pin[VideoDevice]);
  94. if (ctx->device_pin[AudioDevice])
  95. IPin_Release(ctx->device_pin[AudioDevice]);
  96. if (ctx->device_filter[VideoDevice])
  97. IBaseFilter_Release(ctx->device_filter[VideoDevice]);
  98. if (ctx->device_filter[AudioDevice])
  99. IBaseFilter_Release(ctx->device_filter[AudioDevice]);
  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. IGraphBuilder_RemoveFilter(ctx->graph, f);
  109. IEnumFilters_Release(fenum);
  110. }
  111. IGraphBuilder_Release(ctx->graph);
  112. }
  113. if (ctx->device_name[0])
  114. av_free(ctx->device_name[0]);
  115. if (ctx->device_name[1])
  116. av_free(ctx->device_name[1]);
  117. if(ctx->mutex)
  118. CloseHandle(ctx->mutex);
  119. if(ctx->event)
  120. CloseHandle(ctx->event);
  121. pktl = ctx->pktl;
  122. while (pktl) {
  123. AVPacketList *next = pktl->next;
  124. av_destruct_packet(&pktl->pkt);
  125. av_free(pktl);
  126. pktl = next;
  127. }
  128. return 0;
  129. }
  130. static char *dup_wchar_to_utf8(wchar_t *w)
  131. {
  132. char *s = NULL;
  133. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  134. s = av_malloc(l);
  135. if (s)
  136. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  137. return s;
  138. }
  139. static int shall_we_drop(AVFormatContext *s)
  140. {
  141. struct dshow_ctx *ctx = s->priv_data;
  142. const uint8_t dropscore[] = {62, 75, 87, 100};
  143. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  144. unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
  145. if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
  146. av_log(s, AV_LOG_ERROR,
  147. "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. static void
  153. callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time)
  154. {
  155. AVFormatContext *s = priv_data;
  156. struct dshow_ctx *ctx = s->priv_data;
  157. AVPacketList **ppktl, *pktl_next;
  158. // dump_videohdr(s, vdhdr);
  159. if(shall_we_drop(s))
  160. return;
  161. WaitForSingleObject(ctx->mutex, INFINITE);
  162. pktl_next = av_mallocz(sizeof(AVPacketList));
  163. if(!pktl_next)
  164. goto fail;
  165. if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
  166. av_free(pktl_next);
  167. goto fail;
  168. }
  169. pktl_next->pkt.stream_index = index;
  170. pktl_next->pkt.pts = time;
  171. memcpy(pktl_next->pkt.data, buf, buf_size);
  172. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  173. *ppktl = pktl_next;
  174. ctx->curbufsize += buf_size;
  175. SetEvent(ctx->event);
  176. ReleaseMutex(ctx->mutex);
  177. return;
  178. fail:
  179. ReleaseMutex(ctx->mutex);
  180. return;
  181. }
  182. static int
  183. dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
  184. enum dshowDeviceType devtype)
  185. {
  186. struct dshow_ctx *ctx = avctx->priv_data;
  187. IBaseFilter *device_filter = NULL;
  188. IEnumMoniker *classenum = NULL;
  189. IGraphBuilder *graph = ctx->graph;
  190. IEnumPins *pins = 0;
  191. IMoniker *m = NULL;
  192. IPin *device_pin = NULL;
  193. libAVPin *capture_pin = NULL;
  194. libAVFilter *capture_filter = NULL;
  195. const char *device_name = ctx->device_name[devtype];
  196. int ret = AVERROR(EIO);
  197. IPin *pin;
  198. int r;
  199. const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
  200. &CLSID_AudioInputDeviceCategory };
  201. const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
  202. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  203. const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
  204. r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
  205. (IEnumMoniker **) &classenum, 0);
  206. if (r != S_OK) {
  207. av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
  208. devtypename);
  209. goto error;
  210. }
  211. while (IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK && !device_filter) {
  212. IPropertyBag *bag = NULL;
  213. char *buf = NULL;
  214. VARIANT var;
  215. r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
  216. if (r != S_OK)
  217. goto fail1;
  218. var.vt = VT_BSTR;
  219. r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
  220. if (r != S_OK)
  221. goto fail1;
  222. buf = dup_wchar_to_utf8(var.bstrVal);
  223. if (strcmp(device_name, buf))
  224. goto fail1;
  225. IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
  226. fail1:
  227. if (buf)
  228. av_free(buf);
  229. if (bag)
  230. IPropertyBag_Release(bag);
  231. IMoniker_Release(m);
  232. }
  233. if (!device_filter) {
  234. av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
  235. devtypename);
  236. goto error;
  237. }
  238. ctx->device_filter [devtype] = device_filter;
  239. r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
  240. if (r != S_OK) {
  241. av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
  242. goto error;
  243. }
  244. r = IBaseFilter_EnumPins(device_filter, &pins);
  245. if (r != S_OK) {
  246. av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
  247. goto error;
  248. }
  249. while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !device_pin) {
  250. IKsPropertySet *p = NULL;
  251. IEnumMediaTypes *types;
  252. PIN_INFO info = {0};
  253. AM_MEDIA_TYPE *type;
  254. GUID category;
  255. DWORD r2;
  256. IPin_QueryPinInfo(pin, &info);
  257. IBaseFilter_Release(info.pFilter);
  258. if (info.dir != PINDIR_OUTPUT)
  259. goto next;
  260. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  261. goto next;
  262. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  263. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  264. goto next;
  265. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  266. goto next;
  267. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  268. goto next;
  269. IEnumMediaTypes_Reset(types);
  270. while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK && !device_pin) {
  271. if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
  272. device_pin = pin;
  273. goto next;
  274. }
  275. CoTaskMemFree(type);
  276. }
  277. next:
  278. if (types)
  279. IEnumMediaTypes_Release(types);
  280. if (p)
  281. IKsPropertySet_Release(p);
  282. if (device_pin != pin)
  283. IPin_Release(pin);
  284. }
  285. if (!device_pin) {
  286. av_log(avctx, AV_LOG_ERROR,
  287. "Could not find output pin from %s capture device.\n", devtypename);
  288. goto error;
  289. }
  290. ctx->device_pin[devtype] = device_pin;
  291. capture_filter = libAVFilter_Create(avctx, callback, devtype);
  292. if (!capture_filter) {
  293. av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
  294. goto error;
  295. }
  296. ctx->capture_filter[devtype] = capture_filter;
  297. r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
  298. filter_name[devtype]);
  299. if (r != S_OK) {
  300. av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
  301. goto error;
  302. }
  303. libAVPin_AddRef(capture_filter->pin);
  304. capture_pin = capture_filter->pin;
  305. ctx->capture_pin[devtype] = capture_pin;
  306. r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
  307. if (r != S_OK) {
  308. av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
  309. goto error;
  310. }
  311. ret = 0;
  312. error:
  313. if (pins)
  314. IEnumPins_Release(pins);
  315. if (classenum)
  316. IEnumMoniker_Release(classenum);
  317. return ret;
  318. }
  319. static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
  320. {
  321. switch (sample_fmt) {
  322. case AV_SAMPLE_FMT_U8: return CODEC_ID_PCM_U8;
  323. case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE;
  324. case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE;
  325. default: return CODEC_ID_NONE; /* Should never happen. */
  326. }
  327. }
  328. static enum SampleFormat sample_fmt_bits_per_sample(int bits)
  329. {
  330. switch (bits) {
  331. case 8: return AV_SAMPLE_FMT_U8;
  332. case 16: return AV_SAMPLE_FMT_S16;
  333. case 32: return AV_SAMPLE_FMT_S32;
  334. default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
  335. }
  336. }
  337. static int
  338. dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap,
  339. enum dshowDeviceType devtype)
  340. {
  341. struct dshow_ctx *ctx = avctx->priv_data;
  342. AM_MEDIA_TYPE type;
  343. AVCodecContext *codec;
  344. AVStream *st;
  345. int ret = AVERROR(EIO);
  346. st = av_new_stream(avctx, devtype);
  347. if (!st) {
  348. ret = AVERROR(ENOMEM);
  349. goto error;
  350. }
  351. ctx->capture_filter[devtype]->stream_index = st->index;
  352. libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
  353. codec = st->codec;
  354. if (devtype == VideoDevice) {
  355. BITMAPINFOHEADER *bih = NULL;
  356. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  357. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  358. bih = &v->bmiHeader;
  359. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  360. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  361. bih = &v->bmiHeader;
  362. }
  363. if (!bih) {
  364. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  365. goto error;
  366. }
  367. codec->time_base = ap->time_base;
  368. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  369. codec->width = bih->biWidth;
  370. codec->height = bih->biHeight;
  371. codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  372. if (codec->pix_fmt == PIX_FMT_NONE) {
  373. codec->codec_id = dshow_codecid(bih->biCompression);
  374. if (codec->codec_id == CODEC_ID_NONE) {
  375. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  376. "Please report verbose (-v 9) debug information.\n");
  377. dshow_read_close(avctx);
  378. return AVERROR_PATCHWELCOME;
  379. }
  380. codec->bits_per_coded_sample = bih->biBitCount;
  381. } else {
  382. codec->codec_id = CODEC_ID_RAWVIDEO;
  383. if (bih->biCompression == BI_RGB) {
  384. codec->bits_per_coded_sample = bih->biBitCount;
  385. codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  386. if (codec->extradata) {
  387. codec->extradata_size = 9;
  388. memcpy(codec->extradata, "BottomUp", 9);
  389. }
  390. }
  391. }
  392. } else {
  393. WAVEFORMATEX *fx = NULL;
  394. if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  395. fx = (void *) type.pbFormat;
  396. }
  397. if (!fx) {
  398. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  399. goto error;
  400. }
  401. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  402. codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
  403. codec->codec_id = waveform_codec_id(codec->sample_fmt);
  404. codec->sample_rate = fx->nSamplesPerSec;
  405. codec->channels = fx->nChannels;
  406. }
  407. av_set_pts_info(st, 64, 1, 10000000);
  408. ret = 0;
  409. error:
  410. return ret;
  411. }
  412. static int parse_device_name(AVFormatContext *avctx)
  413. {
  414. struct dshow_ctx *ctx = avctx->priv_data;
  415. char **device_name = ctx->device_name;
  416. char *name = av_strdup(avctx->filename);
  417. char *tmp = name;
  418. int ret = 1;
  419. char *type;
  420. while ((type = strtok(tmp, "="))) {
  421. char *token = strtok(NULL, ":");
  422. tmp = NULL;
  423. if (!strcmp(type, "video")) {
  424. device_name[0] = token;
  425. } else if (!strcmp(type, "audio")) {
  426. device_name[1] = token;
  427. } else {
  428. device_name[0] = NULL;
  429. device_name[1] = NULL;
  430. break;
  431. }
  432. }
  433. if (!device_name[0] && !device_name[1]) {
  434. ret = 0;
  435. } else {
  436. if (device_name[0])
  437. device_name[0] = av_strdup(device_name[0]);
  438. if (device_name[1])
  439. device_name[1] = av_strdup(device_name[1]);
  440. }
  441. av_free(name);
  442. return ret;
  443. }
  444. static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap)
  445. {
  446. struct dshow_ctx *ctx = avctx->priv_data;
  447. IGraphBuilder *graph = NULL;
  448. ICreateDevEnum *devenum = NULL;
  449. IMediaControl *control = NULL;
  450. int ret = AVERROR(EIO);
  451. int r;
  452. if (!parse_device_name(avctx)) {
  453. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  454. goto error;
  455. }
  456. CoInitialize(0);
  457. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  458. &IID_IGraphBuilder, (void **) &graph);
  459. if (r != S_OK) {
  460. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  461. goto error;
  462. }
  463. ctx->graph = graph;
  464. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  465. &IID_ICreateDevEnum, (void **) &devenum);
  466. if (r != S_OK) {
  467. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  468. goto error;
  469. }
  470. if (ctx->device_name[VideoDevice]) {
  471. ret = dshow_open_device(avctx, devenum, VideoDevice);
  472. if (ret < 0)
  473. goto error;
  474. ret = dshow_add_device(avctx, ap, VideoDevice);
  475. if (ret < 0)
  476. goto error;
  477. }
  478. if (ctx->device_name[AudioDevice]) {
  479. ret = dshow_open_device(avctx, devenum, AudioDevice);
  480. if (ret < 0)
  481. goto error;
  482. ret = dshow_add_device(avctx, ap, AudioDevice);
  483. if (ret < 0)
  484. goto error;
  485. }
  486. ctx->mutex = CreateMutex(NULL, 0, NULL);
  487. if (!ctx->mutex) {
  488. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  489. goto error;
  490. }
  491. ctx->event = CreateEvent(NULL, 1, 0, NULL);
  492. if (!ctx->event) {
  493. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  494. goto error;
  495. }
  496. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  497. if (r != S_OK) {
  498. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  499. goto error;
  500. }
  501. ctx->control = control;
  502. r = IMediaControl_Run(control);
  503. if (r == S_FALSE) {
  504. OAFilterState pfs;
  505. r = IMediaControl_GetState(control, 0, &pfs);
  506. }
  507. if (r != S_OK) {
  508. av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
  509. goto error;
  510. }
  511. ret = 0;
  512. error:
  513. if (ret < 0)
  514. dshow_read_close(avctx);
  515. if (devenum)
  516. ICreateDevEnum_Release(devenum);
  517. return ret;
  518. }
  519. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  520. {
  521. struct dshow_ctx *ctx = s->priv_data;
  522. AVPacketList *pktl = NULL;
  523. while (!pktl) {
  524. WaitForSingleObject(ctx->mutex, INFINITE);
  525. pktl = ctx->pktl;
  526. if (ctx->pktl) {
  527. *pkt = ctx->pktl->pkt;
  528. ctx->pktl = ctx->pktl->next;
  529. av_free(pktl);
  530. }
  531. ResetEvent(ctx->event);
  532. ReleaseMutex(ctx->mutex);
  533. if (!pktl) {
  534. if (s->flags & AVFMT_FLAG_NONBLOCK) {
  535. return AVERROR(EAGAIN);
  536. } else {
  537. WaitForSingleObject(ctx->event, INFINITE);
  538. }
  539. }
  540. }
  541. ctx->curbufsize -= pkt->size;
  542. return pkt->size;
  543. }
  544. AVInputFormat ff_dshow_demuxer = {
  545. "dshow",
  546. NULL_IF_CONFIG_SMALL("DirectShow capture"),
  547. sizeof(struct dshow_ctx),
  548. NULL,
  549. dshow_read_header,
  550. dshow_read_packet,
  551. dshow_read_close,
  552. .flags = AVFMT_NOFILE,
  553. };