dshow.c 36 KB

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