dshow.c 36 KB

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