dshow.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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 "dshow_capture.h"
  22. #include "libavutil/parseutils.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/mem.h"
  26. #include "libavformat/internal.h"
  27. #include "libavformat/riff.h"
  28. #include "avdevice.h"
  29. #include "libavcodec/raw.h"
  30. #include "objidl.h"
  31. #include "shlwapi.h"
  32. // NB: technically, we should include dxva.h and use
  33. // DXVA_ExtendedFormat, but that type is not defined in
  34. // the MinGW headers. The DXVA2_ExtendedFormat and the
  35. // contents of its fields is identical to
  36. // DXVA_ExtendedFormat (see https://docs.microsoft.com/en-us/windows/win32/medfound/extended-color-information#color-space-in-media-types)
  37. // and is provided by MinGW as well, so we use that
  38. // instead. NB also that per the Microsoft docs, the
  39. // lowest 8 bits of the structure, i.e. the SampleFormat
  40. // field, contain AMCONTROL_xxx flags instead of sample
  41. // format information, and should thus not be used.
  42. // NB further that various values in the structure's
  43. // fields (e.g. BT.2020 color space) are not provided
  44. // for either of the DXVA structs, but are provided in
  45. // the flags of the corresponding fields of Media Foundation.
  46. // These may be provided by DirectShow devices (e.g. LAVFilters
  47. // does so). So we use those values here too (the equivalence is
  48. // indicated by Microsoft example code: https://docs.microsoft.com/en-us/windows/win32/api/dxva2api/ns-dxva2api-dxva2_videodesc)
  49. #include "d3d9types.h"
  50. #include "dxva2api.h"
  51. #ifndef AMCONTROL_COLORINFO_PRESENT
  52. // not defined in some versions of MinGW's dvdmedia.h
  53. # define AMCONTROL_COLORINFO_PRESENT 0x00000080 // if set, indicates DXVA color info is present in the upper (24) bits of the dwControlFlags
  54. #endif
  55. static enum AVPixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
  56. {
  57. switch(biCompression) {
  58. case BI_BITFIELDS:
  59. case BI_RGB:
  60. switch(biBitCount) { /* 1-8 are untested */
  61. case 1:
  62. return AV_PIX_FMT_MONOWHITE;
  63. case 4:
  64. return AV_PIX_FMT_RGB4;
  65. case 8:
  66. return AV_PIX_FMT_RGB8;
  67. case 16:
  68. return AV_PIX_FMT_RGB555;
  69. case 24:
  70. return AV_PIX_FMT_BGR24;
  71. case 32:
  72. return AV_PIX_FMT_0RGB32;
  73. }
  74. }
  75. return avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, biCompression); // all others
  76. }
  77. static enum AVColorRange dshow_color_range(DXVA2_ExtendedFormat *fmt_info)
  78. {
  79. switch (fmt_info->NominalRange)
  80. {
  81. case DXVA2_NominalRange_Unknown:
  82. return AVCOL_RANGE_UNSPECIFIED;
  83. case DXVA2_NominalRange_Normal: // equal to DXVA2_NominalRange_0_255
  84. return AVCOL_RANGE_JPEG;
  85. case DXVA2_NominalRange_Wide: // equal to DXVA2_NominalRange_16_235
  86. return AVCOL_RANGE_MPEG;
  87. case DXVA2_NominalRange_48_208:
  88. // not an ffmpeg color range
  89. return AVCOL_RANGE_UNSPECIFIED;
  90. // values from MediaFoundation SDK (mfobjects.h)
  91. case 4: // MFNominalRange_64_127
  92. // not an ffmpeg color range
  93. return AVCOL_RANGE_UNSPECIFIED;
  94. default:
  95. return AVCOL_RANGE_UNSPECIFIED;
  96. }
  97. }
  98. static enum AVColorSpace dshow_color_space(DXVA2_ExtendedFormat *fmt_info)
  99. {
  100. switch (fmt_info->VideoTransferMatrix)
  101. {
  102. case DXVA2_VideoTransferMatrix_BT709:
  103. return AVCOL_SPC_BT709;
  104. case DXVA2_VideoTransferMatrix_BT601:
  105. return AVCOL_SPC_BT470BG;
  106. case DXVA2_VideoTransferMatrix_SMPTE240M:
  107. return AVCOL_SPC_SMPTE240M;
  108. // values from MediaFoundation SDK (mfobjects.h)
  109. case 4: // MFVideoTransferMatrix_BT2020_10
  110. case 5: // MFVideoTransferMatrix_BT2020_12
  111. if (fmt_info->VideoTransferFunction == 12) // MFVideoTransFunc_2020_const
  112. return AVCOL_SPC_BT2020_CL;
  113. else
  114. return AVCOL_SPC_BT2020_NCL;
  115. default:
  116. return AVCOL_SPC_UNSPECIFIED;
  117. }
  118. }
  119. static enum AVColorPrimaries dshow_color_primaries(DXVA2_ExtendedFormat *fmt_info)
  120. {
  121. switch (fmt_info->VideoPrimaries)
  122. {
  123. case DXVA2_VideoPrimaries_Unknown:
  124. return AVCOL_PRI_UNSPECIFIED;
  125. case DXVA2_VideoPrimaries_reserved:
  126. return AVCOL_PRI_RESERVED;
  127. case DXVA2_VideoPrimaries_BT709:
  128. return AVCOL_PRI_BT709;
  129. case DXVA2_VideoPrimaries_BT470_2_SysM:
  130. return AVCOL_PRI_BT470M;
  131. case DXVA2_VideoPrimaries_BT470_2_SysBG:
  132. case DXVA2_VideoPrimaries_EBU3213: // this is PAL
  133. return AVCOL_PRI_BT470BG;
  134. case DXVA2_VideoPrimaries_SMPTE170M:
  135. case DXVA2_VideoPrimaries_SMPTE_C:
  136. return AVCOL_PRI_SMPTE170M;
  137. case DXVA2_VideoPrimaries_SMPTE240M:
  138. return AVCOL_PRI_SMPTE240M;
  139. // values from MediaFoundation SDK (mfobjects.h)
  140. case 9: // MFVideoPrimaries_BT2020
  141. return AVCOL_PRI_BT2020;
  142. case 10: // MFVideoPrimaries_XYZ
  143. return AVCOL_PRI_SMPTE428;
  144. case 11: // MFVideoPrimaries_DCI_P3
  145. return AVCOL_PRI_SMPTE431;
  146. case 12: // MFVideoPrimaries_ACES (Academy Color Encoding System)
  147. // not an FFmpeg color primary
  148. return AVCOL_PRI_UNSPECIFIED;
  149. default:
  150. return AVCOL_PRI_UNSPECIFIED;
  151. }
  152. }
  153. static enum AVColorTransferCharacteristic dshow_color_trc(DXVA2_ExtendedFormat *fmt_info)
  154. {
  155. switch (fmt_info->VideoTransferFunction)
  156. {
  157. case DXVA2_VideoTransFunc_Unknown:
  158. return AVCOL_TRC_UNSPECIFIED;
  159. case DXVA2_VideoTransFunc_10:
  160. return AVCOL_TRC_LINEAR;
  161. case DXVA2_VideoTransFunc_18:
  162. // not an FFmpeg transfer characteristic
  163. return AVCOL_TRC_UNSPECIFIED;
  164. case DXVA2_VideoTransFunc_20:
  165. // not an FFmpeg transfer characteristic
  166. return AVCOL_TRC_UNSPECIFIED;
  167. case DXVA2_VideoTransFunc_22:
  168. return AVCOL_TRC_GAMMA22;
  169. case DXVA2_VideoTransFunc_709:
  170. return AVCOL_TRC_BT709;
  171. case DXVA2_VideoTransFunc_240M:
  172. return AVCOL_TRC_SMPTE240M;
  173. case DXVA2_VideoTransFunc_sRGB:
  174. return AVCOL_TRC_IEC61966_2_1;
  175. case DXVA2_VideoTransFunc_28:
  176. return AVCOL_TRC_GAMMA28;
  177. // values from MediaFoundation SDK (mfobjects.h)
  178. case 9: // MFVideoTransFunc_Log_100
  179. return AVCOL_TRC_LOG;
  180. case 10: // MFVideoTransFunc_Log_316
  181. return AVCOL_TRC_LOG_SQRT;
  182. case 11: // MFVideoTransFunc_709_sym
  183. // not an FFmpeg transfer characteristic
  184. return AVCOL_TRC_UNSPECIFIED;
  185. case 12: // MFVideoTransFunc_2020_const
  186. case 13: // MFVideoTransFunc_2020
  187. if (fmt_info->VideoTransferMatrix == 5) // MFVideoTransferMatrix_BT2020_12
  188. return AVCOL_TRC_BT2020_12;
  189. else
  190. return AVCOL_TRC_BT2020_10;
  191. case 14: // MFVideoTransFunc_26
  192. // not an FFmpeg transfer characteristic
  193. return AVCOL_TRC_UNSPECIFIED;
  194. case 15: // MFVideoTransFunc_2084
  195. return AVCOL_TRC_SMPTEST2084;
  196. case 16: // MFVideoTransFunc_HLG
  197. return AVCOL_TRC_ARIB_STD_B67;
  198. case 17: // MFVideoTransFunc_10_rel
  199. // not an FFmpeg transfer characteristic? Undocumented also by MS
  200. return AVCOL_TRC_UNSPECIFIED;
  201. default:
  202. return AVCOL_TRC_UNSPECIFIED;
  203. }
  204. }
  205. static enum AVChromaLocation dshow_chroma_loc(DXVA2_ExtendedFormat *fmt_info)
  206. {
  207. if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_Cosited) // that is: (DXVA2_VideoChromaSubsampling_Horizontally_Cosited | DXVA2_VideoChromaSubsampling_Vertically_Cosited | DXVA2_VideoChromaSubsampling_Vertically_AlignedChromaPlanes)
  208. return AVCHROMA_LOC_TOPLEFT;
  209. else if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_MPEG1) // that is: DXVA2_VideoChromaSubsampling_Vertically_AlignedChromaPlanes
  210. return AVCHROMA_LOC_CENTER;
  211. else if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_MPEG2) // that is: (DXVA2_VideoChromaSubsampling_Horizontally_Cosited | DXVA2_VideoChromaSubsampling_Vertically_AlignedChromaPlanes)
  212. return AVCHROMA_LOC_LEFT;
  213. else if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_DV_PAL) // that is: (DXVA2_VideoChromaSubsampling_Horizontally_Cosited | DXVA2_VideoChromaSubsampling_Vertically_Cosited)
  214. return AVCHROMA_LOC_TOPLEFT;
  215. else
  216. // unknown
  217. return AVCHROMA_LOC_UNSPECIFIED;
  218. }
  219. static int
  220. dshow_read_close(AVFormatContext *s)
  221. {
  222. struct dshow_ctx *ctx = s->priv_data;
  223. PacketListEntry *pktl;
  224. if (ctx->control) {
  225. IMediaControl_Stop(ctx->control);
  226. IMediaControl_Release(ctx->control);
  227. }
  228. if (ctx->media_event)
  229. IMediaEvent_Release(ctx->media_event);
  230. if (ctx->graph) {
  231. IEnumFilters *fenum;
  232. int r;
  233. r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
  234. if (r == S_OK) {
  235. IBaseFilter *f;
  236. IEnumFilters_Reset(fenum);
  237. while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK) {
  238. if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK)
  239. IEnumFilters_Reset(fenum); /* When a filter is removed,
  240. * the list must be reset. */
  241. IBaseFilter_Release(f);
  242. }
  243. IEnumFilters_Release(fenum);
  244. }
  245. IGraphBuilder_Release(ctx->graph);
  246. }
  247. if (ctx->capture_pin[VideoDevice])
  248. ff_dshow_pin_Release(ctx->capture_pin[VideoDevice]);
  249. if (ctx->capture_pin[AudioDevice])
  250. ff_dshow_pin_Release(ctx->capture_pin[AudioDevice]);
  251. if (ctx->capture_filter[VideoDevice])
  252. ff_dshow_filter_Release(ctx->capture_filter[VideoDevice]);
  253. if (ctx->capture_filter[AudioDevice])
  254. ff_dshow_filter_Release(ctx->capture_filter[AudioDevice]);
  255. if (ctx->device_pin[VideoDevice])
  256. IPin_Release(ctx->device_pin[VideoDevice]);
  257. if (ctx->device_pin[AudioDevice])
  258. IPin_Release(ctx->device_pin[AudioDevice]);
  259. if (ctx->device_filter[VideoDevice])
  260. IBaseFilter_Release(ctx->device_filter[VideoDevice]);
  261. if (ctx->device_filter[AudioDevice])
  262. IBaseFilter_Release(ctx->device_filter[AudioDevice]);
  263. av_freep(&ctx->device_name[0]);
  264. av_freep(&ctx->device_name[1]);
  265. av_freep(&ctx->device_unique_name[0]);
  266. av_freep(&ctx->device_unique_name[1]);
  267. if(ctx->mutex)
  268. CloseHandle(ctx->mutex);
  269. if(ctx->event[0])
  270. CloseHandle(ctx->event[0]);
  271. if(ctx->event[1])
  272. CloseHandle(ctx->event[1]);
  273. pktl = ctx->pktl;
  274. while (pktl) {
  275. PacketListEntry *next = pktl->next;
  276. av_packet_unref(&pktl->pkt);
  277. av_free(pktl);
  278. pktl = next;
  279. }
  280. CoUninitialize();
  281. return 0;
  282. }
  283. static char *dup_wchar_to_utf8(wchar_t *w)
  284. {
  285. char *s = NULL;
  286. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  287. s = av_malloc(l);
  288. if (s)
  289. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  290. return s;
  291. }
  292. static int shall_we_drop(AVFormatContext *s, int index, enum dshowDeviceType devtype)
  293. {
  294. struct dshow_ctx *ctx = s->priv_data;
  295. static const uint8_t dropscore[] = {62, 75, 87, 100};
  296. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  297. unsigned int buffer_fullness = (ctx->curbufsize[index]*100)/s->max_picture_buffer;
  298. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  299. if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
  300. av_log(s, AV_LOG_ERROR,
  301. "real-time buffer [%s] [%s input] too full or near too full (%d%% of size: %d [rtbufsize parameter])! frame dropped!\n",
  302. ctx->device_name[devtype], devtypename, buffer_fullness, s->max_picture_buffer);
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. static void
  308. callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
  309. {
  310. AVFormatContext *s = priv_data;
  311. struct dshow_ctx *ctx = s->priv_data;
  312. PacketListEntry **ppktl, *pktl_next;
  313. // dump_videohdr(s, vdhdr);
  314. WaitForSingleObject(ctx->mutex, INFINITE);
  315. if(shall_we_drop(s, index, devtype))
  316. goto fail;
  317. pktl_next = av_mallocz(sizeof(*pktl_next));
  318. if(!pktl_next)
  319. goto fail;
  320. if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
  321. av_free(pktl_next);
  322. goto fail;
  323. }
  324. pktl_next->pkt.stream_index = index;
  325. pktl_next->pkt.pts = time;
  326. memcpy(pktl_next->pkt.data, buf, buf_size);
  327. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  328. *ppktl = pktl_next;
  329. ctx->curbufsize[index] += buf_size;
  330. SetEvent(ctx->event[1]);
  331. ReleaseMutex(ctx->mutex);
  332. return;
  333. fail:
  334. ReleaseMutex(ctx->mutex);
  335. return;
  336. }
  337. static void
  338. dshow_get_device_media_types(AVFormatContext *avctx, enum dshowDeviceType devtype,
  339. enum dshowSourceFilterType sourcetype, IBaseFilter *device_filter,
  340. enum AVMediaType **media_types, int *nb_media_types)
  341. {
  342. IEnumPins *pins = 0;
  343. IPin *pin;
  344. int has_audio = 0, has_video = 0;
  345. if (IBaseFilter_EnumPins(device_filter, &pins) != S_OK)
  346. return;
  347. while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
  348. IKsPropertySet *p = NULL;
  349. PIN_INFO info = { 0 };
  350. GUID category;
  351. DWORD r2;
  352. IEnumMediaTypes *types = NULL;
  353. AM_MEDIA_TYPE *type;
  354. if (IPin_QueryPinInfo(pin, &info) != S_OK)
  355. goto next;
  356. IBaseFilter_Release(info.pFilter);
  357. if (info.dir != PINDIR_OUTPUT)
  358. goto next;
  359. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  360. goto next;
  361. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  362. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  363. goto next;
  364. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  365. goto next;
  366. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  367. goto next;
  368. // enumerate media types exposed by pin
  369. // NB: don't know if a pin can expose both audio and video, check 'm all to be safe
  370. IEnumMediaTypes_Reset(types);
  371. while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) {
  372. if (IsEqualGUID(&type->majortype, &MEDIATYPE_Video)) {
  373. has_video = 1;
  374. } else if (IsEqualGUID(&type->majortype, &MEDIATYPE_Audio)) {
  375. has_audio = 1;
  376. }
  377. CoTaskMemFree(type);
  378. }
  379. next:
  380. if (types)
  381. IEnumMediaTypes_Release(types);
  382. if (p)
  383. IKsPropertySet_Release(p);
  384. IPin_Release(pin);
  385. }
  386. IEnumPins_Release(pins);
  387. if (has_audio || has_video) {
  388. int nb_types = has_audio + has_video;
  389. *media_types = av_malloc_array(nb_types, sizeof(enum AVMediaType));
  390. if (*media_types) {
  391. if (has_audio)
  392. (*media_types)[0] = AVMEDIA_TYPE_AUDIO;
  393. if (has_video)
  394. (*media_types)[0 + has_audio] = AVMEDIA_TYPE_VIDEO;
  395. *nb_media_types = nb_types;
  396. }
  397. }
  398. }
  399. /**
  400. * Cycle through available devices using the device enumerator devenum,
  401. * retrieve the device with type specified by devtype and return the
  402. * pointer to the object found in *pfilter.
  403. * If pfilter is NULL, list all device names.
  404. * If device_list is not NULL, populate it with found devices instead of
  405. * outputting device names to log
  406. */
  407. static int
  408. dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
  409. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype,
  410. IBaseFilter **pfilter, char **device_unique_name,
  411. AVDeviceInfoList **device_list)
  412. {
  413. struct dshow_ctx *ctx = avctx->priv_data;
  414. IBaseFilter *device_filter = NULL;
  415. IEnumMoniker *classenum = NULL;
  416. IMoniker *m = NULL;
  417. const char *device_name = ctx->device_name[devtype];
  418. int skip = (devtype == VideoDevice) ? ctx->video_device_number
  419. : ctx->audio_device_number;
  420. int r;
  421. const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
  422. &CLSID_AudioInputDeviceCategory };
  423. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio only";
  424. const char *sourcetypename = (sourcetype == VideoSourceDevice) ? "video" : "audio";
  425. r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[sourcetype],
  426. (IEnumMoniker **) &classenum, 0);
  427. if (r != S_OK) {
  428. av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices (or none found).\n",
  429. devtypename);
  430. return AVERROR(EIO);
  431. }
  432. while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) {
  433. IPropertyBag *bag = NULL;
  434. char *friendly_name = NULL;
  435. char *unique_name = NULL;
  436. VARIANT var;
  437. IBindCtx *bind_ctx = NULL;
  438. LPOLESTR olestr = NULL;
  439. LPMALLOC co_malloc = NULL;
  440. AVDeviceInfo *device = NULL;
  441. enum AVMediaType *media_types = NULL;
  442. int nb_media_types = 0;
  443. int i;
  444. r = CoGetMalloc(1, &co_malloc);
  445. if (r != S_OK)
  446. goto fail;
  447. r = CreateBindCtx(0, &bind_ctx);
  448. if (r != S_OK)
  449. goto fail;
  450. /* GetDisplayname works for both video and audio, DevicePath doesn't */
  451. r = IMoniker_GetDisplayName(m, bind_ctx, NULL, &olestr);
  452. if (r != S_OK)
  453. goto fail;
  454. unique_name = dup_wchar_to_utf8(olestr);
  455. /* replace ':' with '_' since we use : to delineate between sources */
  456. for (i = 0; i < strlen(unique_name); i++) {
  457. if (unique_name[i] == ':')
  458. unique_name[i] = '_';
  459. }
  460. r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
  461. if (r != S_OK)
  462. goto fail;
  463. var.vt = VT_BSTR;
  464. r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
  465. if (r != S_OK)
  466. goto fail;
  467. friendly_name = dup_wchar_to_utf8(var.bstrVal);
  468. if (pfilter) {
  469. if (strcmp(device_name, friendly_name) && strcmp(device_name, unique_name))
  470. goto fail;
  471. if (!skip--) {
  472. r = IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
  473. if (r != S_OK) {
  474. av_log(avctx, AV_LOG_ERROR, "Unable to BindToObject for %s\n", device_name);
  475. goto fail;
  476. }
  477. *device_unique_name = unique_name;
  478. unique_name = NULL;
  479. // success, loop will end now
  480. }
  481. } else {
  482. // get media types exposed by pins of device
  483. if (IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void* ) &device_filter) == S_OK) {
  484. dshow_get_device_media_types(avctx, devtype, sourcetype, device_filter, &media_types, &nb_media_types);
  485. IBaseFilter_Release(device_filter);
  486. device_filter = NULL;
  487. }
  488. if (device_list) {
  489. device = av_mallocz(sizeof(AVDeviceInfo));
  490. if (!device)
  491. goto fail;
  492. device->device_name = av_strdup(unique_name);
  493. device->device_description = av_strdup(friendly_name);
  494. if (!device->device_name || !device->device_description)
  495. goto fail;
  496. // make space in device_list for this new device
  497. if (av_reallocp_array(&(*device_list)->devices,
  498. (*device_list)->nb_devices + 1,
  499. sizeof(*(*device_list)->devices)) < 0)
  500. goto fail;
  501. // attach media_types to device
  502. device->nb_media_types = nb_media_types;
  503. device->media_types = media_types;
  504. nb_media_types = 0;
  505. media_types = NULL;
  506. // store device in list
  507. (*device_list)->devices[(*device_list)->nb_devices] = device;
  508. (*device_list)->nb_devices++;
  509. device = NULL; // copied into array, make sure not freed below
  510. }
  511. else {
  512. av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
  513. if (nb_media_types > 0) {
  514. const char* media_type = av_get_media_type_string(media_types[0]);
  515. av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
  516. for (int i = 1; i < nb_media_types; ++i) {
  517. media_type = av_get_media_type_string(media_types[i]);
  518. av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
  519. }
  520. av_log(avctx, AV_LOG_INFO, ")");
  521. } else {
  522. av_log(avctx, AV_LOG_INFO, " (none)");
  523. }
  524. av_log(avctx, AV_LOG_INFO, "\n");
  525. av_log(avctx, AV_LOG_INFO, " Alternative name \"%s\"\n", unique_name);
  526. }
  527. }
  528. fail:
  529. av_freep(&media_types);
  530. if (device) {
  531. av_freep(&device->device_name);
  532. av_freep(&device->device_description);
  533. // NB: no need to av_freep(&device->media_types), its only moved to device once nothing can fail anymore
  534. av_free(device);
  535. }
  536. if (olestr && co_malloc)
  537. IMalloc_Free(co_malloc, olestr);
  538. if (bind_ctx)
  539. IBindCtx_Release(bind_ctx);
  540. av_freep(&friendly_name);
  541. av_freep(&unique_name);
  542. if (bag)
  543. IPropertyBag_Release(bag);
  544. IMoniker_Release(m);
  545. }
  546. IEnumMoniker_Release(classenum);
  547. if (pfilter) {
  548. if (!device_filter) {
  549. av_log(avctx, AV_LOG_ERROR, "Could not find %s device with name [%s] among source devices of type %s.\n",
  550. devtypename, device_name, sourcetypename);
  551. return AVERROR(EIO);
  552. }
  553. *pfilter = device_filter;
  554. }
  555. return 0;
  556. }
  557. static int dshow_get_device_list(AVFormatContext *avctx, AVDeviceInfoList *device_list)
  558. {
  559. ICreateDevEnum *devenum = NULL;
  560. int r;
  561. int ret = AVERROR(EIO);
  562. if (!device_list)
  563. return AVERROR(EINVAL);
  564. CoInitialize(0);
  565. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  566. &IID_ICreateDevEnum, (void**)&devenum);
  567. if (r != S_OK) {
  568. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  569. goto error;
  570. }
  571. ret = dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, &device_list);
  572. if (ret < S_OK && ret != AVERROR(EIO))
  573. goto error;
  574. ret = dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, &device_list);
  575. error:
  576. if (devenum)
  577. ICreateDevEnum_Release(devenum);
  578. CoUninitialize();
  579. return ret;
  580. }
  581. static int dshow_should_set_format(AVFormatContext *avctx, enum dshowDeviceType devtype)
  582. {
  583. struct dshow_ctx *ctx = avctx->priv_data;
  584. return (devtype == VideoDevice && (ctx->framerate ||
  585. (ctx->requested_width && ctx->requested_height) ||
  586. ctx->pixel_format != AV_PIX_FMT_NONE ||
  587. ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO))
  588. || (devtype == AudioDevice && (ctx->channels || ctx->sample_size || ctx->sample_rate));
  589. }
  590. struct dshow_format_info {
  591. enum dshowDeviceType devtype;
  592. // video
  593. int64_t framerate;
  594. enum AVPixelFormat pix_fmt;
  595. enum AVCodecID codec_id;
  596. enum AVColorRange col_range;
  597. enum AVColorSpace col_space;
  598. enum AVColorPrimaries col_prim;
  599. enum AVColorTransferCharacteristic col_trc;
  600. enum AVChromaLocation chroma_loc;
  601. int width;
  602. int height;
  603. // audio
  604. int sample_rate;
  605. int sample_size;
  606. int channels;
  607. };
  608. // user must av_free the returned pointer
  609. static struct dshow_format_info *dshow_get_format_info(AM_MEDIA_TYPE *type)
  610. {
  611. struct dshow_format_info *fmt_info = NULL;
  612. BITMAPINFOHEADER *bih;
  613. DXVA2_ExtendedFormat *extended_format_info = NULL;
  614. WAVEFORMATEX *fx;
  615. enum dshowDeviceType devtype;
  616. int64_t framerate;
  617. if (!type)
  618. return NULL;
  619. if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
  620. VIDEOINFOHEADER *v = (void *) type->pbFormat;
  621. framerate = v->AvgTimePerFrame;
  622. bih = &v->bmiHeader;
  623. devtype = VideoDevice;
  624. } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
  625. VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
  626. devtype = VideoDevice;
  627. framerate = v->AvgTimePerFrame;
  628. bih = &v->bmiHeader;
  629. if (v->dwControlFlags & AMCONTROL_COLORINFO_PRESENT)
  630. extended_format_info = (DXVA2_ExtendedFormat *) &v->dwControlFlags;
  631. } else if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
  632. fx = (void *) type->pbFormat;
  633. devtype = AudioDevice;
  634. } else {
  635. return NULL;
  636. }
  637. fmt_info = av_mallocz(sizeof(struct dshow_format_info));
  638. if (!fmt_info)
  639. return NULL;
  640. // initialize fields where unset is not zero
  641. fmt_info->pix_fmt = AV_PIX_FMT_NONE;
  642. fmt_info->col_space = AVCOL_SPC_UNSPECIFIED;
  643. fmt_info->col_prim = AVCOL_PRI_UNSPECIFIED;
  644. fmt_info->col_trc = AVCOL_TRC_UNSPECIFIED;
  645. // now get info about format
  646. fmt_info->devtype = devtype;
  647. if (devtype == VideoDevice) {
  648. fmt_info->width = bih->biWidth;
  649. fmt_info->height = bih->biHeight;
  650. fmt_info->framerate = framerate;
  651. fmt_info->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  652. if (fmt_info->pix_fmt == AV_PIX_FMT_NONE) {
  653. const AVCodecTag *const tags[] = { avformat_get_riff_video_tags(), NULL };
  654. fmt_info->codec_id = av_codec_get_id(tags, bih->biCompression);
  655. }
  656. else
  657. fmt_info->codec_id = AV_CODEC_ID_RAWVIDEO;
  658. if (extended_format_info) {
  659. fmt_info->col_range = dshow_color_range(extended_format_info);
  660. fmt_info->col_space = dshow_color_space(extended_format_info);
  661. fmt_info->col_prim = dshow_color_primaries(extended_format_info);
  662. fmt_info->col_trc = dshow_color_trc(extended_format_info);
  663. fmt_info->chroma_loc = dshow_chroma_loc(extended_format_info);
  664. }
  665. } else {
  666. fmt_info->sample_rate = fx->nSamplesPerSec;
  667. fmt_info->sample_size = fx->wBitsPerSample;
  668. fmt_info->channels = fx->nChannels;
  669. }
  670. return fmt_info;
  671. }
  672. static void dshow_get_default_format(IPin *pin, IAMStreamConfig *config, enum dshowDeviceType devtype, AM_MEDIA_TYPE **type)
  673. {
  674. HRESULT hr;
  675. if ((hr = IAMStreamConfig_GetFormat(config, type)) != S_OK) {
  676. if (hr == E_NOTIMPL || !IsEqualGUID(&(*type)->majortype, devtype == VideoDevice ? &MEDIATYPE_Video : &MEDIATYPE_Audio)) {
  677. // default not available or of wrong type,
  678. // fall back to iterating exposed formats
  679. // until one of the right type is found
  680. IEnumMediaTypes* types = NULL;
  681. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  682. return;
  683. IEnumMediaTypes_Reset(types);
  684. while (IEnumMediaTypes_Next(types, 1, type, NULL) == S_OK) {
  685. if (IsEqualGUID(&(*type)->majortype, devtype == VideoDevice ? &MEDIATYPE_Video : &MEDIATYPE_Audio)) {
  686. break;
  687. }
  688. CoTaskMemFree(*type);
  689. *type = NULL;
  690. }
  691. IEnumMediaTypes_Release(types);
  692. }
  693. }
  694. }
  695. /**
  696. * Cycle through available formats available from the specified pin,
  697. * try to set parameters specified through AVOptions, or the pin's
  698. * default format if no such parameters were set. If successful,
  699. * return 1 in *pformat_set.
  700. * If pformat_set is NULL, list all pin capabilities.
  701. */
  702. static void
  703. dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
  704. IPin *pin, int *pformat_set)
  705. {
  706. struct dshow_ctx *ctx = avctx->priv_data;
  707. IAMStreamConfig *config = NULL;
  708. AM_MEDIA_TYPE *type = NULL;
  709. AM_MEDIA_TYPE *previous_match_type = NULL;
  710. int format_set = 0;
  711. void *caps = NULL;
  712. int i, n, size, r;
  713. int wait_for_better = 0;
  714. int use_default;
  715. // format parameters requested by user
  716. // if none are requested by user, the values will below be set to
  717. // those of the default format
  718. // video
  719. enum AVCodecID requested_video_codec_id = ctx->video_codec_id;
  720. enum AVPixelFormat requested_pixel_format = ctx->pixel_format;
  721. int64_t requested_framerate = ctx->framerate ? ((int64_t)ctx->requested_framerate.den * 10000000)
  722. / ctx->requested_framerate.num : 0;
  723. int requested_width = ctx->requested_width;
  724. int requested_height = ctx->requested_height;
  725. // audio
  726. int requested_sample_rate = ctx->sample_rate;
  727. int requested_sample_size = ctx->sample_size;
  728. int requested_channels = ctx->channels;
  729. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  730. return;
  731. if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
  732. goto end;
  733. caps = av_malloc(size);
  734. if (!caps)
  735. goto end;
  736. /**
  737. * If we should open the device with the default format,
  738. * then:
  739. * 1. check what the format of the default device is, and
  740. * 2. below we iterate all formats till we find a matching
  741. * one, with most info exposed (see comment below).
  742. */
  743. use_default = !dshow_should_set_format(avctx, devtype);
  744. if (use_default && pformat_set)
  745. {
  746. // get default
  747. dshow_get_default_format(pin, config, devtype, &type);
  748. if (!type)
  749. // this pin does not expose any formats of the expected type
  750. goto end;
  751. if (type) {
  752. // interrogate default format, so we know what to search for below
  753. struct dshow_format_info *fmt_info = dshow_get_format_info(type);
  754. if (fmt_info) {
  755. if (fmt_info->devtype == VideoDevice) {
  756. requested_video_codec_id = fmt_info->codec_id;
  757. requested_pixel_format = fmt_info->pix_fmt;
  758. requested_framerate = fmt_info->framerate;
  759. requested_width = fmt_info->width;
  760. requested_height = fmt_info->height;
  761. } else {
  762. requested_sample_rate = fmt_info->sample_rate;
  763. requested_sample_size = fmt_info->sample_size;
  764. requested_channels = fmt_info->channels;
  765. }
  766. av_free(fmt_info); // free but don't set to NULL to enable below check
  767. }
  768. if (type && type->pbFormat)
  769. CoTaskMemFree(type->pbFormat);
  770. CoTaskMemFree(type);
  771. type = NULL;
  772. if (!fmt_info)
  773. // default format somehow invalid, can't continue with this pin
  774. goto end;
  775. fmt_info = NULL;
  776. }
  777. }
  778. // NB: some devices (e.g. Logitech C920) expose each video format twice:
  779. // both a format containing a VIDEOINFOHEADER and a format containing
  780. // a VIDEOINFOHEADER2. We want, if possible, to select a format with a
  781. // VIDEOINFOHEADER2, as this potentially provides more info about the
  782. // format. So, if in the iteration below we have found a matching format,
  783. // but it is a VIDEOINFOHEADER, keep looking for a matching format that
  784. // exposes contains a VIDEOINFOHEADER2. Fall back to the VIDEOINFOHEADER
  785. // format if no corresponding VIDEOINFOHEADER2 is found when we finish
  786. // iterating.
  787. for (i = 0; i < n && !format_set; i++) {
  788. struct dshow_format_info *fmt_info = NULL;
  789. r = IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
  790. if (r != S_OK)
  791. goto next;
  792. #if DSHOWDEBUG
  793. ff_print_AM_MEDIA_TYPE(type);
  794. #endif
  795. fmt_info = dshow_get_format_info(type);
  796. if (!fmt_info)
  797. goto next;
  798. if (devtype == VideoDevice) {
  799. VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
  800. BITMAPINFOHEADER *bih;
  801. int64_t *fr;
  802. #if DSHOWDEBUG
  803. ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
  804. #endif
  805. if (fmt_info->devtype != VideoDevice)
  806. goto next;
  807. if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
  808. VIDEOINFOHEADER *v = (void *) type->pbFormat;
  809. fr = &v->AvgTimePerFrame;
  810. bih = &v->bmiHeader;
  811. wait_for_better = 1;
  812. } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
  813. VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
  814. fr = &v->AvgTimePerFrame;
  815. bih = &v->bmiHeader;
  816. wait_for_better = 0;
  817. }
  818. if (!pformat_set) {
  819. const char *chroma = av_chroma_location_name(fmt_info->chroma_loc);
  820. if (fmt_info->pix_fmt == AV_PIX_FMT_NONE) {
  821. const AVCodec *codec = avcodec_find_decoder(fmt_info->codec_id);
  822. if (fmt_info->codec_id == AV_CODEC_ID_NONE || !codec) {
  823. av_log(avctx, AV_LOG_INFO, " unknown compression type 0x%X", (int) bih->biCompression);
  824. } else {
  825. av_log(avctx, AV_LOG_INFO, " vcodec=%s", codec->name);
  826. }
  827. } else {
  828. av_log(avctx, AV_LOG_INFO, " pixel_format=%s", av_get_pix_fmt_name(fmt_info->pix_fmt));
  829. }
  830. av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g",
  831. vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
  832. 1e7 / vcaps->MaxFrameInterval,
  833. vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
  834. 1e7 / vcaps->MinFrameInterval);
  835. if (fmt_info->col_range != AVCOL_RANGE_UNSPECIFIED ||
  836. fmt_info->col_space != AVCOL_SPC_UNSPECIFIED ||
  837. fmt_info->col_prim != AVCOL_PRI_UNSPECIFIED ||
  838. fmt_info->col_trc != AVCOL_TRC_UNSPECIFIED) {
  839. const char *range = av_color_range_name(fmt_info->col_range);
  840. const char *space = av_color_space_name(fmt_info->col_space);
  841. const char *prim = av_color_primaries_name(fmt_info->col_prim);
  842. const char *trc = av_color_transfer_name(fmt_info->col_trc);
  843. av_log(avctx, AV_LOG_INFO, " (%s, %s/%s/%s",
  844. range ? range : "unknown",
  845. space ? space : "unknown",
  846. prim ? prim : "unknown",
  847. trc ? trc : "unknown");
  848. if (fmt_info->chroma_loc != AVCHROMA_LOC_UNSPECIFIED)
  849. av_log(avctx, AV_LOG_INFO, ", %s", chroma ? chroma : "unknown");
  850. av_log(avctx, AV_LOG_INFO, ")");
  851. }
  852. else if (fmt_info->chroma_loc != AVCHROMA_LOC_UNSPECIFIED)
  853. av_log(avctx, AV_LOG_INFO, "(%s)", chroma ? chroma : "unknown");
  854. av_log(avctx, AV_LOG_INFO, "\n");
  855. goto next;
  856. }
  857. if (requested_video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  858. if (requested_video_codec_id != fmt_info->codec_id)
  859. goto next;
  860. }
  861. if (requested_pixel_format != AV_PIX_FMT_NONE &&
  862. requested_pixel_format != fmt_info->pix_fmt) {
  863. goto next;
  864. }
  865. if (requested_framerate) {
  866. if (requested_framerate > vcaps->MaxFrameInterval ||
  867. requested_framerate < vcaps->MinFrameInterval)
  868. goto next;
  869. *fr = requested_framerate;
  870. }
  871. if (requested_width && requested_height) {
  872. if (requested_width > vcaps->MaxOutputSize.cx ||
  873. requested_width < vcaps->MinOutputSize.cx ||
  874. requested_height > vcaps->MaxOutputSize.cy ||
  875. requested_height < vcaps->MinOutputSize.cy)
  876. goto next;
  877. bih->biWidth = requested_width;
  878. bih->biHeight = requested_height;
  879. }
  880. } else {
  881. WAVEFORMATEX *fx;
  882. AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
  883. #if DSHOWDEBUG
  884. ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
  885. #endif
  886. if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
  887. fx = (void *) type->pbFormat;
  888. } else {
  889. goto next;
  890. }
  891. if (!pformat_set) {
  892. av_log(
  893. avctx,
  894. AV_LOG_INFO,
  895. " ch=%2u, bits=%2u, rate=%6lu\n",
  896. fx->nChannels, fx->wBitsPerSample, fx->nSamplesPerSec
  897. );
  898. goto next;
  899. }
  900. if (
  901. (requested_sample_rate && requested_sample_rate != fx->nSamplesPerSec) ||
  902. (requested_sample_size && requested_sample_size != fx->wBitsPerSample) ||
  903. (requested_channels && requested_channels != fx->nChannels )
  904. ) {
  905. goto next;
  906. }
  907. }
  908. // found a matching format. Either apply or store
  909. // for safekeeping if we might maybe find a better
  910. // format with more info attached to it (see comment
  911. // above loop)
  912. if (!wait_for_better) {
  913. if (IAMStreamConfig_SetFormat(config, type) != S_OK)
  914. goto next;
  915. format_set = 1;
  916. }
  917. else if (!previous_match_type) {
  918. // store this matching format for possible later use.
  919. // If we have already found a matching format, ignore it
  920. previous_match_type = type;
  921. type = NULL;
  922. }
  923. next:
  924. av_freep(&fmt_info);
  925. if (type && type->pbFormat)
  926. CoTaskMemFree(type->pbFormat);
  927. CoTaskMemFree(type);
  928. type = NULL;
  929. }
  930. // set the pin's format, if wanted
  931. if (pformat_set && !format_set) {
  932. if (previous_match_type) {
  933. // previously found a matching VIDEOINFOHEADER format and stored
  934. // it for safe keeping. Searching further for a matching
  935. // VIDEOINFOHEADER2 format yielded nothing. So set the pin's
  936. // format based on the VIDEOINFOHEADER format.
  937. // NB: this never applies to an audio format because
  938. // previous_match_type always NULL in that case
  939. if (IAMStreamConfig_SetFormat(config, previous_match_type) == S_OK)
  940. format_set = 1;
  941. }
  942. else if (use_default) {
  943. // default format returned by device apparently was not contained
  944. // in the capabilities of any of the formats returned by the device
  945. // (sic?). Fall back to directly setting the default format
  946. dshow_get_default_format(pin, config, devtype, &type);
  947. if (IAMStreamConfig_SetFormat(config, type) == S_OK)
  948. format_set = 1;
  949. if (type && type->pbFormat)
  950. CoTaskMemFree(type->pbFormat);
  951. CoTaskMemFree(type);
  952. type = NULL;
  953. }
  954. }
  955. end:
  956. if (previous_match_type && previous_match_type->pbFormat)
  957. CoTaskMemFree(previous_match_type->pbFormat);
  958. CoTaskMemFree(previous_match_type);
  959. IAMStreamConfig_Release(config);
  960. av_free(caps);
  961. if (pformat_set)
  962. *pformat_set = format_set;
  963. }
  964. /**
  965. * Set audio device buffer size in milliseconds (which can directly impact
  966. * latency, depending on the device).
  967. */
  968. static int
  969. dshow_set_audio_buffer_size(AVFormatContext *avctx, IPin *pin)
  970. {
  971. struct dshow_ctx *ctx = avctx->priv_data;
  972. IAMBufferNegotiation *buffer_negotiation = NULL;
  973. ALLOCATOR_PROPERTIES props = { -1, -1, -1, -1 };
  974. IAMStreamConfig *config = NULL;
  975. AM_MEDIA_TYPE *type = NULL;
  976. int ret = AVERROR(EIO);
  977. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  978. goto end;
  979. if (IAMStreamConfig_GetFormat(config, &type) != S_OK)
  980. goto end;
  981. if (!IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx))
  982. goto end;
  983. props.cbBuffer = (((WAVEFORMATEX *) type->pbFormat)->nAvgBytesPerSec)
  984. * ctx->audio_buffer_size / 1000;
  985. if (IPin_QueryInterface(pin, &IID_IAMBufferNegotiation, (void **) &buffer_negotiation) != S_OK)
  986. goto end;
  987. if (IAMBufferNegotiation_SuggestAllocatorProperties(buffer_negotiation, &props) != S_OK)
  988. goto end;
  989. ret = 0;
  990. end:
  991. if (buffer_negotiation)
  992. IAMBufferNegotiation_Release(buffer_negotiation);
  993. if (type) {
  994. if (type->pbFormat)
  995. CoTaskMemFree(type->pbFormat);
  996. CoTaskMemFree(type);
  997. }
  998. if (config)
  999. IAMStreamConfig_Release(config);
  1000. return ret;
  1001. }
  1002. /**
  1003. * Pops up a user dialog allowing them to adjust properties for the given filter, if possible.
  1004. */
  1005. void
  1006. ff_dshow_show_filter_properties(IBaseFilter *device_filter, AVFormatContext *avctx) {
  1007. ISpecifyPropertyPages *property_pages = NULL;
  1008. IUnknown *device_filter_iunknown = NULL;
  1009. HRESULT hr;
  1010. FILTER_INFO filter_info = {0}; /* a warning on this line is false positive GCC bug 53119 AFAICT */
  1011. CAUUID ca_guid = {0};
  1012. hr = IBaseFilter_QueryInterface(device_filter, &IID_ISpecifyPropertyPages, (void **)&property_pages);
  1013. if (hr != S_OK) {
  1014. av_log(avctx, AV_LOG_WARNING, "requested filter does not have a property page to show");
  1015. goto end;
  1016. }
  1017. hr = IBaseFilter_QueryFilterInfo(device_filter, &filter_info);
  1018. if (hr != S_OK) {
  1019. goto fail;
  1020. }
  1021. hr = IBaseFilter_QueryInterface(device_filter, &IID_IUnknown, (void **)&device_filter_iunknown);
  1022. if (hr != S_OK) {
  1023. goto fail;
  1024. }
  1025. hr = ISpecifyPropertyPages_GetPages(property_pages, &ca_guid);
  1026. if (hr != S_OK) {
  1027. goto fail;
  1028. }
  1029. hr = OleCreatePropertyFrame(NULL, 0, 0, filter_info.achName, 1, &device_filter_iunknown, ca_guid.cElems,
  1030. ca_guid.pElems, 0, 0, NULL);
  1031. if (hr != S_OK) {
  1032. goto fail;
  1033. }
  1034. goto end;
  1035. fail:
  1036. av_log(avctx, AV_LOG_ERROR, "Failure showing property pages for filter");
  1037. end:
  1038. if (property_pages)
  1039. ISpecifyPropertyPages_Release(property_pages);
  1040. if (device_filter_iunknown)
  1041. IUnknown_Release(device_filter_iunknown);
  1042. if (filter_info.pGraph)
  1043. IFilterGraph_Release(filter_info.pGraph);
  1044. if (ca_guid.pElems)
  1045. CoTaskMemFree(ca_guid.pElems);
  1046. }
  1047. /**
  1048. * Cycle through available pins using the device_filter device, of type
  1049. * devtype, retrieve the first output pin and return the pointer to the
  1050. * object found in *ppin.
  1051. * If ppin is NULL, cycle through all pins listing audio/video capabilities.
  1052. */
  1053. static int
  1054. dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
  1055. enum dshowSourceFilterType sourcetype, IBaseFilter *device_filter, IPin **ppin)
  1056. {
  1057. struct dshow_ctx *ctx = avctx->priv_data;
  1058. IEnumPins *pins = 0;
  1059. IPin *device_pin = NULL;
  1060. IPin *pin;
  1061. int r;
  1062. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio only";
  1063. const char *sourcetypename = (sourcetype == VideoSourceDevice) ? "video" : "audio";
  1064. int set_format = dshow_should_set_format(avctx, devtype);
  1065. int format_set = 0;
  1066. int should_show_properties = (devtype == VideoDevice) ? ctx->show_video_device_dialog : ctx->show_audio_device_dialog;
  1067. if (should_show_properties)
  1068. ff_dshow_show_filter_properties(device_filter, avctx);
  1069. r = IBaseFilter_EnumPins(device_filter, &pins);
  1070. if (r != S_OK) {
  1071. av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
  1072. return AVERROR(EIO);
  1073. }
  1074. if (!ppin) {
  1075. av_log(avctx, AV_LOG_INFO, "DirectShow %s device options (from %s devices)\n",
  1076. devtypename, sourcetypename);
  1077. }
  1078. while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
  1079. IKsPropertySet *p = NULL;
  1080. PIN_INFO info = {0};
  1081. GUID category;
  1082. DWORD r2;
  1083. char *name_buf = NULL;
  1084. wchar_t *pin_id = NULL;
  1085. char *pin_buf = NULL;
  1086. char *desired_pin_name = devtype == VideoDevice ? ctx->video_pin_name : ctx->audio_pin_name;
  1087. IPin_QueryPinInfo(pin, &info);
  1088. IBaseFilter_Release(info.pFilter);
  1089. if (info.dir != PINDIR_OUTPUT)
  1090. goto next;
  1091. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  1092. goto next;
  1093. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  1094. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  1095. goto next;
  1096. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  1097. goto next;
  1098. name_buf = dup_wchar_to_utf8(info.achName);
  1099. r = IPin_QueryId(pin, &pin_id);
  1100. if (r != S_OK) {
  1101. av_log(avctx, AV_LOG_ERROR, "Could not query pin id\n");
  1102. return AVERROR(EIO);
  1103. }
  1104. pin_buf = dup_wchar_to_utf8(pin_id);
  1105. if (!ppin) {
  1106. av_log(avctx, AV_LOG_INFO, " Pin \"%s\" (alternative pin name \"%s\")\n", name_buf, pin_buf);
  1107. dshow_cycle_formats(avctx, devtype, pin, NULL);
  1108. goto next;
  1109. }
  1110. if (desired_pin_name) {
  1111. if(strcmp(name_buf, desired_pin_name) && strcmp(pin_buf, desired_pin_name)) {
  1112. av_log(avctx, AV_LOG_DEBUG, "skipping pin \"%s\" (\"%s\") != requested \"%s\"\n",
  1113. name_buf, pin_buf, desired_pin_name);
  1114. goto next;
  1115. }
  1116. }
  1117. // will either try to find format matching options supplied by user
  1118. // or try to open default format. Successful if returns with format_set==1
  1119. dshow_cycle_formats(avctx, devtype, pin, &format_set);
  1120. if (!format_set) {
  1121. goto next;
  1122. }
  1123. if (devtype == AudioDevice && ctx->audio_buffer_size) {
  1124. if (dshow_set_audio_buffer_size(avctx, pin) < 0) {
  1125. av_log(avctx, AV_LOG_ERROR, "unable to set audio buffer size %d to pin, using pin anyway...", ctx->audio_buffer_size);
  1126. }
  1127. }
  1128. if (format_set) {
  1129. device_pin = pin;
  1130. av_log(avctx, AV_LOG_DEBUG, "Selecting pin %s on %s\n", name_buf, devtypename);
  1131. }
  1132. next:
  1133. if (p)
  1134. IKsPropertySet_Release(p);
  1135. if (device_pin != pin)
  1136. IPin_Release(pin);
  1137. av_free(name_buf);
  1138. av_free(pin_buf);
  1139. if (pin_id)
  1140. CoTaskMemFree(pin_id);
  1141. }
  1142. IEnumPins_Release(pins);
  1143. if (ppin) {
  1144. if (set_format && !format_set) {
  1145. av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
  1146. return AVERROR(EIO);
  1147. }
  1148. if (!device_pin) {
  1149. av_log(avctx, AV_LOG_ERROR,
  1150. "Could not find output pin from %s capture device.\n", devtypename);
  1151. return AVERROR(EIO);
  1152. }
  1153. *ppin = device_pin;
  1154. }
  1155. return 0;
  1156. }
  1157. /**
  1158. * List options for device with type devtype, source filter type sourcetype
  1159. *
  1160. * @param devenum device enumerator used for accessing the device
  1161. */
  1162. static int
  1163. dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
  1164. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype)
  1165. {
  1166. struct dshow_ctx *ctx = avctx->priv_data;
  1167. IBaseFilter *device_filter = NULL;
  1168. char *device_unique_name = NULL;
  1169. int r;
  1170. if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_unique_name, NULL)) < 0)
  1171. return r;
  1172. ctx->device_filter[devtype] = device_filter;
  1173. ctx->device_unique_name[devtype] = device_unique_name;
  1174. if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, NULL)) < 0)
  1175. return r;
  1176. return 0;
  1177. }
  1178. static int
  1179. dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
  1180. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype)
  1181. {
  1182. struct dshow_ctx *ctx = avctx->priv_data;
  1183. IBaseFilter *device_filter = NULL;
  1184. char *device_filter_unique_name = NULL;
  1185. IGraphBuilder *graph = ctx->graph;
  1186. IPin *device_pin = NULL;
  1187. DShowPin *capture_pin = NULL;
  1188. DShowFilter *capture_filter = NULL;
  1189. ICaptureGraphBuilder2 *graph_builder2 = NULL;
  1190. int ret = AVERROR(EIO);
  1191. int r;
  1192. IStream *ifile_stream = NULL;
  1193. IStream *ofile_stream = NULL;
  1194. IPersistStream *pers_stream = NULL;
  1195. enum dshowDeviceType otherDevType = (devtype == VideoDevice) ? AudioDevice : VideoDevice;
  1196. const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
  1197. if ( ((ctx->audio_filter_load_file) && (strlen(ctx->audio_filter_load_file)>0) && (sourcetype == AudioSourceDevice)) ||
  1198. ((ctx->video_filter_load_file) && (strlen(ctx->video_filter_load_file)>0) && (sourcetype == VideoSourceDevice)) ) {
  1199. HRESULT hr;
  1200. char *filename = NULL;
  1201. if (sourcetype == AudioSourceDevice)
  1202. filename = ctx->audio_filter_load_file;
  1203. else
  1204. filename = ctx->video_filter_load_file;
  1205. hr = SHCreateStreamOnFile ((LPCSTR) filename, STGM_READ, &ifile_stream);
  1206. if (S_OK != hr) {
  1207. av_log(avctx, AV_LOG_ERROR, "Could not open capture filter description file.\n");
  1208. goto error;
  1209. }
  1210. hr = OleLoadFromStream(ifile_stream, &IID_IBaseFilter, (void **) &device_filter);
  1211. if (hr != S_OK) {
  1212. av_log(avctx, AV_LOG_ERROR, "Could not load capture filter from file.\n");
  1213. goto error;
  1214. }
  1215. if (sourcetype == AudioSourceDevice)
  1216. av_log(avctx, AV_LOG_INFO, "Audio-");
  1217. else
  1218. av_log(avctx, AV_LOG_INFO, "Video-");
  1219. av_log(avctx, AV_LOG_INFO, "Capture filter loaded successfully from file \"%s\".\n", filename);
  1220. } else {
  1221. if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_filter_unique_name, NULL)) < 0) {
  1222. ret = r;
  1223. goto error;
  1224. }
  1225. }
  1226. if (ctx->device_filter[otherDevType]) {
  1227. // avoid adding add two instances of the same device to the graph, one for video, one for audio
  1228. // a few devices don't support this (could also do this check earlier to avoid double crossbars, etc. but they seem OK)
  1229. if (!device_filter_unique_name || strcmp(device_filter_unique_name, ctx->device_unique_name[otherDevType]) == 0) {
  1230. av_log(avctx, AV_LOG_DEBUG, "reusing previous graph capture filter... %s\n", device_filter_unique_name);
  1231. IBaseFilter_Release(device_filter);
  1232. device_filter = ctx->device_filter[otherDevType];
  1233. IBaseFilter_AddRef(ctx->device_filter[otherDevType]);
  1234. } else {
  1235. av_log(avctx, AV_LOG_DEBUG, "not reusing previous graph capture filter %s != %s\n", device_filter_unique_name, ctx->device_unique_name[otherDevType]);
  1236. }
  1237. }
  1238. ctx->device_filter [devtype] = device_filter;
  1239. ctx->device_unique_name [devtype] = device_filter_unique_name;
  1240. r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
  1241. if (r != S_OK) {
  1242. av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
  1243. goto error;
  1244. }
  1245. if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, &device_pin)) < 0) {
  1246. ret = r;
  1247. goto error;
  1248. }
  1249. ctx->device_pin[devtype] = device_pin;
  1250. capture_filter = ff_dshow_filter_Create(avctx, callback, devtype);
  1251. if (!capture_filter) {
  1252. av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
  1253. goto error;
  1254. }
  1255. ctx->capture_filter[devtype] = capture_filter;
  1256. if ( ((ctx->audio_filter_save_file) && (strlen(ctx->audio_filter_save_file)>0) && (sourcetype == AudioSourceDevice)) ||
  1257. ((ctx->video_filter_save_file) && (strlen(ctx->video_filter_save_file)>0) && (sourcetype == VideoSourceDevice)) ) {
  1258. HRESULT hr;
  1259. char *filename = NULL;
  1260. if (sourcetype == AudioSourceDevice)
  1261. filename = ctx->audio_filter_save_file;
  1262. else
  1263. filename = ctx->video_filter_save_file;
  1264. hr = SHCreateStreamOnFile ((LPCSTR) filename, STGM_CREATE | STGM_READWRITE, &ofile_stream);
  1265. if (S_OK != hr) {
  1266. av_log(avctx, AV_LOG_ERROR, "Could not create capture filter description file.\n");
  1267. goto error;
  1268. }
  1269. hr = IBaseFilter_QueryInterface(device_filter, &IID_IPersistStream, (void **) &pers_stream);
  1270. if (hr != S_OK) {
  1271. av_log(avctx, AV_LOG_ERROR, "Query for IPersistStream failed.\n");
  1272. goto error;
  1273. }
  1274. hr = OleSaveToStream(pers_stream, ofile_stream);
  1275. if (hr != S_OK) {
  1276. av_log(avctx, AV_LOG_ERROR, "Could not save capture filter \n");
  1277. goto error;
  1278. }
  1279. hr = IStream_Commit(ofile_stream, STGC_DEFAULT);
  1280. if (S_OK != hr) {
  1281. av_log(avctx, AV_LOG_ERROR, "Could not commit capture filter data to file.\n");
  1282. goto error;
  1283. }
  1284. if (sourcetype == AudioSourceDevice)
  1285. av_log(avctx, AV_LOG_INFO, "Audio-");
  1286. else
  1287. av_log(avctx, AV_LOG_INFO, "Video-");
  1288. av_log(avctx, AV_LOG_INFO, "Capture filter saved successfully to file \"%s\".\n", filename);
  1289. }
  1290. r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
  1291. filter_name[devtype]);
  1292. if (r != S_OK) {
  1293. av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
  1294. goto error;
  1295. }
  1296. ff_dshow_pin_AddRef(capture_filter->pin);
  1297. capture_pin = capture_filter->pin;
  1298. ctx->capture_pin[devtype] = capture_pin;
  1299. r = CoCreateInstance(&CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
  1300. &IID_ICaptureGraphBuilder2, (void **) &graph_builder2);
  1301. if (r != S_OK) {
  1302. av_log(avctx, AV_LOG_ERROR, "Could not create CaptureGraphBuilder2\n");
  1303. goto error;
  1304. }
  1305. r = ICaptureGraphBuilder2_SetFiltergraph(graph_builder2, graph);
  1306. if (r != S_OK) {
  1307. av_log(avctx, AV_LOG_ERROR, "Could not set graph for CaptureGraphBuilder2\n");
  1308. goto error;
  1309. }
  1310. r = ICaptureGraphBuilder2_RenderStream(graph_builder2, NULL, NULL, (IUnknown *) device_pin, NULL /* no intermediate filter */,
  1311. (IBaseFilter *) capture_filter); /* connect pins, optionally insert intermediate filters like crossbar if necessary */
  1312. if (r != S_OK) {
  1313. av_log(avctx, AV_LOG_ERROR, "Could not RenderStream to connect pins\n");
  1314. goto error;
  1315. }
  1316. r = ff_dshow_try_setup_crossbar_options(graph_builder2, device_filter, devtype, avctx);
  1317. if (r != S_OK) {
  1318. av_log(avctx, AV_LOG_ERROR, "Could not setup CrossBar\n");
  1319. goto error;
  1320. }
  1321. ret = 0;
  1322. error:
  1323. if (graph_builder2 != NULL)
  1324. ICaptureGraphBuilder2_Release(graph_builder2);
  1325. if (pers_stream)
  1326. IPersistStream_Release(pers_stream);
  1327. if (ifile_stream)
  1328. IStream_Release(ifile_stream);
  1329. if (ofile_stream)
  1330. IStream_Release(ofile_stream);
  1331. return ret;
  1332. }
  1333. static enum AVCodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
  1334. {
  1335. switch (sample_fmt) {
  1336. case AV_SAMPLE_FMT_U8: return AV_CODEC_ID_PCM_U8;
  1337. case AV_SAMPLE_FMT_S16: return AV_CODEC_ID_PCM_S16LE;
  1338. case AV_SAMPLE_FMT_S32: return AV_CODEC_ID_PCM_S32LE;
  1339. default: return AV_CODEC_ID_NONE; /* Should never happen. */
  1340. }
  1341. }
  1342. static enum AVSampleFormat sample_fmt_bits_per_sample(int bits)
  1343. {
  1344. switch (bits) {
  1345. case 8: return AV_SAMPLE_FMT_U8;
  1346. case 16: return AV_SAMPLE_FMT_S16;
  1347. case 32: return AV_SAMPLE_FMT_S32;
  1348. default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
  1349. }
  1350. }
  1351. static int
  1352. dshow_add_device(AVFormatContext *avctx,
  1353. enum dshowDeviceType devtype)
  1354. {
  1355. struct dshow_ctx *ctx = avctx->priv_data;
  1356. AM_MEDIA_TYPE type;
  1357. AVCodecParameters *par;
  1358. AVStream *st;
  1359. struct dshow_format_info *fmt_info = NULL;
  1360. int ret = AVERROR(EIO);
  1361. type.pbFormat = NULL;
  1362. st = avformat_new_stream(avctx, NULL);
  1363. if (!st) {
  1364. ret = AVERROR(ENOMEM);
  1365. goto error;
  1366. }
  1367. st->id = devtype;
  1368. ctx->capture_filter[devtype]->stream_index = st->index;
  1369. if (ff_dshow_pin_ConnectionMediaType(ctx->capture_pin[devtype], &type) != S_OK) {
  1370. ret = AVERROR(EIO);
  1371. goto error;
  1372. }
  1373. fmt_info = dshow_get_format_info(&type);
  1374. if (!fmt_info) {
  1375. ret = AVERROR(EIO);
  1376. goto error;
  1377. }
  1378. par = st->codecpar;
  1379. if (devtype == VideoDevice) {
  1380. BITMAPINFOHEADER *bih = NULL;
  1381. AVRational time_base;
  1382. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  1383. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  1384. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  1385. bih = &v->bmiHeader;
  1386. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  1387. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  1388. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  1389. bih = &v->bmiHeader;
  1390. }
  1391. if (!bih) {
  1392. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  1393. goto error;
  1394. }
  1395. st->avg_frame_rate = av_inv_q(time_base);
  1396. st->r_frame_rate = av_inv_q(time_base);
  1397. par->codec_type = AVMEDIA_TYPE_VIDEO;
  1398. par->width = fmt_info->width;
  1399. par->height = fmt_info->height;
  1400. par->codec_tag = bih->biCompression;
  1401. par->format = fmt_info->pix_fmt;
  1402. if (bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
  1403. av_log(avctx, AV_LOG_DEBUG, "attempt to use full range for HDYC...\n");
  1404. par->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
  1405. }
  1406. par->color_range = fmt_info->col_range;
  1407. par->color_space = fmt_info->col_space;
  1408. par->color_primaries = fmt_info->col_prim;
  1409. par->color_trc = fmt_info->col_trc;
  1410. par->chroma_location = fmt_info->chroma_loc;
  1411. par->codec_id = fmt_info->codec_id;
  1412. if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
  1413. if (bih->biCompression == BI_RGB || bih->biCompression == BI_BITFIELDS) {
  1414. par->bits_per_coded_sample = bih->biBitCount;
  1415. if (par->height < 0) {
  1416. par->height *= -1;
  1417. } else {
  1418. par->extradata = av_malloc(9 + AV_INPUT_BUFFER_PADDING_SIZE);
  1419. if (par->extradata) {
  1420. par->extradata_size = 9;
  1421. memcpy(par->extradata, "BottomUp", 9);
  1422. }
  1423. }
  1424. }
  1425. } else {
  1426. if (par->codec_id == AV_CODEC_ID_NONE) {
  1427. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  1428. "Please report type 0x%X.\n", (int) bih->biCompression);
  1429. ret = AVERROR_PATCHWELCOME;
  1430. goto error;
  1431. }
  1432. par->bits_per_coded_sample = bih->biBitCount;
  1433. }
  1434. } else {
  1435. if (!IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  1436. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  1437. goto error;
  1438. }
  1439. par->codec_type = AVMEDIA_TYPE_AUDIO;
  1440. par->format = sample_fmt_bits_per_sample(fmt_info->sample_size);
  1441. par->codec_id = waveform_codec_id(par->format);
  1442. par->sample_rate = fmt_info->sample_rate;
  1443. par->ch_layout.nb_channels = fmt_info->channels;
  1444. }
  1445. avpriv_set_pts_info(st, 64, 1, 10000000);
  1446. ret = 0;
  1447. error:
  1448. av_freep(&fmt_info);
  1449. if (type.pbFormat)
  1450. CoTaskMemFree(type.pbFormat);
  1451. return ret;
  1452. }
  1453. static int parse_device_name(AVFormatContext *avctx)
  1454. {
  1455. struct dshow_ctx *ctx = avctx->priv_data;
  1456. char **device_name = ctx->device_name;
  1457. char *name = av_strdup(avctx->url);
  1458. char *tmp = name;
  1459. int ret = 1;
  1460. char *type;
  1461. while ((type = strtok(tmp, "="))) {
  1462. char *token = strtok(NULL, ":");
  1463. tmp = NULL;
  1464. if (!strcmp(type, "video")) {
  1465. device_name[0] = token;
  1466. } else if (!strcmp(type, "audio")) {
  1467. device_name[1] = token;
  1468. } else {
  1469. device_name[0] = NULL;
  1470. device_name[1] = NULL;
  1471. break;
  1472. }
  1473. }
  1474. if (!device_name[0] && !device_name[1]) {
  1475. ret = 0;
  1476. } else {
  1477. if (device_name[0])
  1478. device_name[0] = av_strdup(device_name[0]);
  1479. if (device_name[1])
  1480. device_name[1] = av_strdup(device_name[1]);
  1481. }
  1482. av_free(name);
  1483. return ret;
  1484. }
  1485. static int dshow_read_header(AVFormatContext *avctx)
  1486. {
  1487. struct dshow_ctx *ctx = avctx->priv_data;
  1488. IGraphBuilder *graph = NULL;
  1489. ICreateDevEnum *devenum = NULL;
  1490. IMediaControl *control = NULL;
  1491. IMediaEvent *media_event = NULL;
  1492. HANDLE media_event_handle;
  1493. HANDLE proc;
  1494. int ret = AVERROR(EIO);
  1495. int r;
  1496. CoInitialize(0);
  1497. if (!ctx->list_devices && !parse_device_name(avctx)) {
  1498. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  1499. goto error;
  1500. }
  1501. ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id
  1502. : AV_CODEC_ID_RAWVIDEO;
  1503. if (ctx->pixel_format != AV_PIX_FMT_NONE) {
  1504. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  1505. av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when "
  1506. "video codec is not set or set to rawvideo\n");
  1507. ret = AVERROR(EINVAL);
  1508. goto error;
  1509. }
  1510. }
  1511. if (ctx->framerate) {
  1512. r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
  1513. if (r < 0) {
  1514. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
  1515. goto error;
  1516. }
  1517. }
  1518. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  1519. &IID_IGraphBuilder, (void **) &graph);
  1520. if (r != S_OK) {
  1521. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  1522. goto error;
  1523. }
  1524. ctx->graph = graph;
  1525. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  1526. &IID_ICreateDevEnum, (void **) &devenum);
  1527. if (r != S_OK) {
  1528. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  1529. goto error;
  1530. }
  1531. if (ctx->list_devices) {
  1532. dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, NULL);
  1533. dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, NULL);
  1534. ret = AVERROR_EXIT;
  1535. goto error;
  1536. }
  1537. if (ctx->list_options) {
  1538. if (ctx->device_name[VideoDevice])
  1539. if ((r = dshow_list_device_options(avctx, devenum, VideoDevice, VideoSourceDevice))) {
  1540. ret = r;
  1541. goto error;
  1542. }
  1543. if (ctx->device_name[AudioDevice]) {
  1544. if (dshow_list_device_options(avctx, devenum, AudioDevice, AudioSourceDevice)) {
  1545. /* show audio options from combined video+audio sources as fallback */
  1546. if ((r = dshow_list_device_options(avctx, devenum, AudioDevice, VideoSourceDevice))) {
  1547. ret = r;
  1548. goto error;
  1549. }
  1550. }
  1551. }
  1552. // don't exit yet, allow it to list crossbar options in dshow_open_device
  1553. }
  1554. if (ctx->device_name[VideoDevice]) {
  1555. if ((r = dshow_open_device(avctx, devenum, VideoDevice, VideoSourceDevice)) < 0 ||
  1556. (r = dshow_add_device(avctx, VideoDevice)) < 0) {
  1557. ret = r;
  1558. goto error;
  1559. }
  1560. }
  1561. if (ctx->device_name[AudioDevice]) {
  1562. if ((r = dshow_open_device(avctx, devenum, AudioDevice, AudioSourceDevice)) < 0 ||
  1563. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  1564. av_log(avctx, AV_LOG_INFO, "Searching for audio device within video devices for %s\n", ctx->device_name[AudioDevice]);
  1565. /* see if there's a video source with an audio pin with the given audio name */
  1566. if ((r = dshow_open_device(avctx, devenum, AudioDevice, VideoSourceDevice)) < 0 ||
  1567. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  1568. ret = r;
  1569. goto error;
  1570. }
  1571. }
  1572. }
  1573. if (ctx->list_options) {
  1574. /* allow it to list crossbar options in dshow_open_device */
  1575. ret = AVERROR_EXIT;
  1576. goto error;
  1577. }
  1578. ctx->curbufsize[0] = 0;
  1579. ctx->curbufsize[1] = 0;
  1580. ctx->mutex = CreateMutex(NULL, 0, NULL);
  1581. if (!ctx->mutex) {
  1582. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  1583. goto error;
  1584. }
  1585. ctx->event[1] = CreateEvent(NULL, 1, 0, NULL);
  1586. if (!ctx->event[1]) {
  1587. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  1588. goto error;
  1589. }
  1590. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  1591. if (r != S_OK) {
  1592. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  1593. goto error;
  1594. }
  1595. ctx->control = control;
  1596. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event);
  1597. if (r != S_OK) {
  1598. av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n");
  1599. goto error;
  1600. }
  1601. ctx->media_event = media_event;
  1602. r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle);
  1603. if (r != S_OK) {
  1604. av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n");
  1605. goto error;
  1606. }
  1607. proc = GetCurrentProcess();
  1608. r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0],
  1609. 0, 0, DUPLICATE_SAME_ACCESS);
  1610. if (!r) {
  1611. av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n");
  1612. goto error;
  1613. }
  1614. r = IMediaControl_Run(control);
  1615. if (r == S_FALSE) {
  1616. OAFilterState pfs;
  1617. r = IMediaControl_GetState(control, 0, &pfs);
  1618. }
  1619. if (r != S_OK) {
  1620. av_log(avctx, AV_LOG_ERROR, "Could not run graph (sometimes caused by a device already in use by other application)\n");
  1621. goto error;
  1622. }
  1623. ret = 0;
  1624. error:
  1625. if (devenum)
  1626. ICreateDevEnum_Release(devenum);
  1627. if (ret < 0)
  1628. dshow_read_close(avctx);
  1629. return ret;
  1630. }
  1631. /**
  1632. * Checks media events from DirectShow and returns -1 on error or EOF. Also
  1633. * purges all events that might be in the event queue to stop the trigger
  1634. * of event notification.
  1635. */
  1636. static int dshow_check_event_queue(IMediaEvent *media_event)
  1637. {
  1638. LONG_PTR p1, p2;
  1639. long code;
  1640. int ret = 0;
  1641. while (IMediaEvent_GetEvent(media_event, &code, &p1, &p2, 0) != E_ABORT) {
  1642. if (code == EC_COMPLETE || code == EC_DEVICE_LOST || code == EC_ERRORABORT)
  1643. ret = -1;
  1644. IMediaEvent_FreeEventParams(media_event, code, p1, p2);
  1645. }
  1646. return ret;
  1647. }
  1648. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  1649. {
  1650. struct dshow_ctx *ctx = s->priv_data;
  1651. PacketListEntry *pktl = NULL;
  1652. while (!ctx->eof && !pktl) {
  1653. WaitForSingleObject(ctx->mutex, INFINITE);
  1654. pktl = ctx->pktl;
  1655. if (pktl) {
  1656. *pkt = pktl->pkt;
  1657. ctx->pktl = ctx->pktl->next;
  1658. av_free(pktl);
  1659. ctx->curbufsize[pkt->stream_index] -= pkt->size;
  1660. }
  1661. ResetEvent(ctx->event[1]);
  1662. ReleaseMutex(ctx->mutex);
  1663. if (!pktl) {
  1664. if (dshow_check_event_queue(ctx->media_event) < 0) {
  1665. ctx->eof = 1;
  1666. } else if (s->flags & AVFMT_FLAG_NONBLOCK) {
  1667. return AVERROR(EAGAIN);
  1668. } else {
  1669. WaitForMultipleObjects(2, ctx->event, 0, INFINITE);
  1670. }
  1671. }
  1672. }
  1673. return ctx->eof ? AVERROR(EIO) : pkt->size;
  1674. }
  1675. #define OFFSET(x) offsetof(struct dshow_ctx, x)
  1676. #define DEC AV_OPT_FLAG_DECODING_PARAM
  1677. static const AVOption options[] = {
  1678. { "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 },
  1679. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX, DEC },
  1680. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1681. { "sample_rate", "set audio sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1682. { "sample_size", "set audio sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 16, DEC },
  1683. { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1684. { "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 },
  1685. { "list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, DEC },
  1686. { "list_options", "list available options for specified device", OFFSET(list_options), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, DEC },
  1687. { "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 },
  1688. { "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 },
  1689. { "video_pin_name", "select video capture pin by name", OFFSET(video_pin_name),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  1690. { "audio_pin_name", "select audio capture pin by name", OFFSET(audio_pin_name),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  1691. { "crossbar_video_input_pin_number", "set video input pin number for crossbar device", OFFSET(crossbar_video_input_pin_number), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC },
  1692. { "crossbar_audio_input_pin_number", "set audio input pin number for crossbar device", OFFSET(crossbar_audio_input_pin_number), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC },
  1693. { "show_video_device_dialog", "display property dialog for video capture device", OFFSET(show_video_device_dialog), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  1694. { "show_audio_device_dialog", "display property dialog for audio capture device", OFFSET(show_audio_device_dialog), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  1695. { "show_video_crossbar_connection_dialog", "display property dialog for crossbar connecting pins filter on video device", OFFSET(show_video_crossbar_connection_dialog), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  1696. { "show_audio_crossbar_connection_dialog", "display property dialog for crossbar connecting pins filter on audio device", OFFSET(show_audio_crossbar_connection_dialog), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  1697. { "show_analog_tv_tuner_dialog", "display property dialog for analog tuner filter", OFFSET(show_analog_tv_tuner_dialog), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  1698. { "show_analog_tv_tuner_audio_dialog", "display property dialog for analog tuner audio filter", OFFSET(show_analog_tv_tuner_audio_dialog), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
  1699. { "audio_device_load", "load audio capture filter device (and properties) from file", OFFSET(audio_filter_load_file), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1700. { "audio_device_save", "save audio capture filter device (and properties) to file", OFFSET(audio_filter_save_file), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1701. { "video_device_load", "load video capture filter device (and properties) from file", OFFSET(video_filter_load_file), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1702. { "video_device_save", "save video capture filter device (and properties) to file", OFFSET(video_filter_save_file), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1703. { "use_video_device_timestamps", "use device instead of wallclock timestamps for video frames", OFFSET(use_video_device_timestamps), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DEC },
  1704. { NULL },
  1705. };
  1706. static const AVClass dshow_class = {
  1707. .class_name = "dshow indev",
  1708. .item_name = av_default_item_name,
  1709. .option = options,
  1710. .version = LIBAVUTIL_VERSION_INT,
  1711. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  1712. };
  1713. const AVInputFormat ff_dshow_demuxer = {
  1714. .name = "dshow",
  1715. .long_name = NULL_IF_CONFIG_SMALL("DirectShow capture"),
  1716. .priv_data_size = sizeof(struct dshow_ctx),
  1717. .read_header = dshow_read_header,
  1718. .read_packet = dshow_read_packet,
  1719. .read_close = dshow_read_close,
  1720. .get_device_list= dshow_get_device_list,
  1721. .flags = AVFMT_NOFILE | AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  1722. .priv_class = &dshow_class,
  1723. };