dshow.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  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. if (pin)
  385. IPin_Release(pin);
  386. }
  387. IEnumPins_Release(pins);
  388. if (has_audio || has_video) {
  389. int nb_types = has_audio + has_video;
  390. *media_types = av_malloc_array(nb_types, sizeof(enum AVMediaType));
  391. if (*media_types) {
  392. if (has_audio)
  393. (*media_types)[0] = AVMEDIA_TYPE_AUDIO;
  394. if (has_video)
  395. (*media_types)[0 + has_audio] = AVMEDIA_TYPE_VIDEO;
  396. *nb_media_types = nb_types;
  397. }
  398. }
  399. }
  400. /**
  401. * Cycle through available devices using the device enumerator devenum,
  402. * retrieve the device with type specified by devtype and return the
  403. * pointer to the object found in *pfilter.
  404. * If pfilter is NULL, list all device names.
  405. * If device_list is not NULL, populate it with found devices instead of
  406. * outputting device names to log
  407. */
  408. static int
  409. dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
  410. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype,
  411. IBaseFilter **pfilter, char **device_unique_name,
  412. AVDeviceInfoList **device_list)
  413. {
  414. struct dshow_ctx *ctx = avctx->priv_data;
  415. IBaseFilter *device_filter = NULL;
  416. IEnumMoniker *classenum = NULL;
  417. IMoniker *m = NULL;
  418. const char *device_name = ctx->device_name[devtype];
  419. int skip = (devtype == VideoDevice) ? ctx->video_device_number
  420. : ctx->audio_device_number;
  421. int r;
  422. const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
  423. &CLSID_AudioInputDeviceCategory };
  424. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio only";
  425. const char *sourcetypename = (sourcetype == VideoSourceDevice) ? "video" : "audio";
  426. r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[sourcetype],
  427. (IEnumMoniker **) &classenum, 0);
  428. if (r != S_OK) {
  429. av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices (or none found).\n",
  430. devtypename);
  431. return AVERROR(EIO);
  432. }
  433. while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) {
  434. IPropertyBag *bag = NULL;
  435. char *friendly_name = NULL;
  436. char *unique_name = NULL;
  437. VARIANT var;
  438. IBindCtx *bind_ctx = NULL;
  439. LPOLESTR olestr = NULL;
  440. LPMALLOC co_malloc = NULL;
  441. AVDeviceInfo *device = NULL;
  442. enum AVMediaType *media_types = NULL;
  443. int nb_media_types = 0;
  444. int i;
  445. r = CoGetMalloc(1, &co_malloc);
  446. if (r != S_OK)
  447. goto fail;
  448. r = CreateBindCtx(0, &bind_ctx);
  449. if (r != S_OK)
  450. goto fail;
  451. /* GetDisplayname works for both video and audio, DevicePath doesn't */
  452. r = IMoniker_GetDisplayName(m, bind_ctx, NULL, &olestr);
  453. if (r != S_OK)
  454. goto fail;
  455. unique_name = dup_wchar_to_utf8(olestr);
  456. /* replace ':' with '_' since we use : to delineate between sources */
  457. for (i = 0; i < strlen(unique_name); i++) {
  458. if (unique_name[i] == ':')
  459. unique_name[i] = '_';
  460. }
  461. r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
  462. if (r != S_OK)
  463. goto fail;
  464. var.vt = VT_BSTR;
  465. r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
  466. if (r != S_OK)
  467. goto fail;
  468. friendly_name = dup_wchar_to_utf8(var.bstrVal);
  469. if (pfilter) {
  470. if (strcmp(device_name, friendly_name) && strcmp(device_name, unique_name))
  471. goto fail;
  472. if (!skip--) {
  473. r = IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
  474. if (r != S_OK) {
  475. av_log(avctx, AV_LOG_ERROR, "Unable to BindToObject for %s\n", device_name);
  476. goto fail;
  477. }
  478. *device_unique_name = unique_name;
  479. unique_name = NULL;
  480. // success, loop will end now
  481. }
  482. } else {
  483. // get media types exposed by pins of device
  484. if (IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void* ) &device_filter) == S_OK) {
  485. dshow_get_device_media_types(avctx, devtype, sourcetype, device_filter, &media_types, &nb_media_types);
  486. IBaseFilter_Release(device_filter);
  487. device_filter = NULL;
  488. }
  489. if (device_list) {
  490. device = av_mallocz(sizeof(AVDeviceInfo));
  491. if (!device)
  492. goto fail;
  493. device->device_name = av_strdup(unique_name);
  494. device->device_description = av_strdup(friendly_name);
  495. if (!device->device_name || !device->device_description)
  496. goto fail;
  497. // make space in device_list for this new device
  498. if (av_reallocp_array(&(*device_list)->devices,
  499. (*device_list)->nb_devices + 1,
  500. sizeof(*(*device_list)->devices)) < 0)
  501. goto fail;
  502. // attach media_types to device
  503. device->nb_media_types = nb_media_types;
  504. device->media_types = media_types;
  505. nb_media_types = 0;
  506. media_types = NULL;
  507. // store device in list
  508. (*device_list)->devices[(*device_list)->nb_devices] = device;
  509. (*device_list)->nb_devices++;
  510. device = NULL; // copied into array, make sure not freed below
  511. }
  512. else {
  513. av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
  514. if (nb_media_types > 0) {
  515. const char* media_type = av_get_media_type_string(media_types[0]);
  516. av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
  517. for (int i = 1; i < nb_media_types; ++i) {
  518. media_type = av_get_media_type_string(media_types[i]);
  519. av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
  520. }
  521. av_log(avctx, AV_LOG_INFO, ")");
  522. } else {
  523. av_log(avctx, AV_LOG_INFO, " (none)");
  524. }
  525. av_log(avctx, AV_LOG_INFO, "\n");
  526. av_log(avctx, AV_LOG_INFO, " Alternative name \"%s\"\n", unique_name);
  527. }
  528. }
  529. fail:
  530. av_freep(&media_types);
  531. if (device) {
  532. av_freep(&device->device_name);
  533. av_freep(&device->device_description);
  534. // NB: no need to av_freep(&device->media_types), its only moved to device once nothing can fail anymore
  535. av_free(device);
  536. }
  537. if (olestr && co_malloc)
  538. IMalloc_Free(co_malloc, olestr);
  539. if (bind_ctx)
  540. IBindCtx_Release(bind_ctx);
  541. av_freep(&friendly_name);
  542. av_freep(&unique_name);
  543. if (bag)
  544. IPropertyBag_Release(bag);
  545. IMoniker_Release(m);
  546. }
  547. IEnumMoniker_Release(classenum);
  548. if (pfilter) {
  549. if (!device_filter) {
  550. av_log(avctx, AV_LOG_ERROR, "Could not find %s device with name [%s] among source devices of type %s.\n",
  551. devtypename, device_name, sourcetypename);
  552. return AVERROR(EIO);
  553. }
  554. *pfilter = device_filter;
  555. }
  556. return 0;
  557. }
  558. static int dshow_get_device_list(AVFormatContext *avctx, AVDeviceInfoList *device_list)
  559. {
  560. ICreateDevEnum *devenum = NULL;
  561. int r;
  562. int ret = AVERROR(EIO);
  563. if (!device_list)
  564. return AVERROR(EINVAL);
  565. CoInitialize(0);
  566. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  567. &IID_ICreateDevEnum, (void**)&devenum);
  568. if (r != S_OK) {
  569. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  570. goto error;
  571. }
  572. ret = dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, &device_list);
  573. if (ret < S_OK)
  574. goto error;
  575. ret = dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, &device_list);
  576. error:
  577. if (devenum)
  578. ICreateDevEnum_Release(devenum);
  579. CoUninitialize();
  580. return ret;
  581. }
  582. static int dshow_should_set_format(AVFormatContext *avctx, enum dshowDeviceType devtype)
  583. {
  584. struct dshow_ctx *ctx = avctx->priv_data;
  585. return (devtype == VideoDevice && (ctx->framerate ||
  586. (ctx->requested_width && ctx->requested_height) ||
  587. ctx->pixel_format != AV_PIX_FMT_NONE ||
  588. ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO))
  589. || (devtype == AudioDevice && (ctx->channels || ctx->sample_size || ctx->sample_rate));
  590. }
  591. struct dshow_format_info {
  592. enum dshowDeviceType devtype;
  593. // video
  594. int64_t framerate;
  595. enum AVPixelFormat pix_fmt;
  596. enum AVCodecID codec_id;
  597. enum AVColorRange col_range;
  598. enum AVColorSpace col_space;
  599. enum AVColorPrimaries col_prim;
  600. enum AVColorTransferCharacteristic col_trc;
  601. enum AVChromaLocation chroma_loc;
  602. int width;
  603. int height;
  604. // audio
  605. int sample_rate;
  606. int sample_size;
  607. int channels;
  608. };
  609. // user must av_free the returned pointer
  610. static struct dshow_format_info *dshow_get_format_info(AM_MEDIA_TYPE *type)
  611. {
  612. struct dshow_format_info *fmt_info = NULL;
  613. BITMAPINFOHEADER *bih;
  614. DXVA2_ExtendedFormat *extended_format_info = NULL;
  615. WAVEFORMATEX *fx;
  616. enum dshowDeviceType devtype;
  617. int64_t framerate;
  618. if (!type)
  619. return NULL;
  620. if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
  621. VIDEOINFOHEADER *v = (void *) type->pbFormat;
  622. framerate = v->AvgTimePerFrame;
  623. bih = &v->bmiHeader;
  624. devtype = VideoDevice;
  625. } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
  626. VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
  627. devtype = VideoDevice;
  628. framerate = v->AvgTimePerFrame;
  629. bih = &v->bmiHeader;
  630. if (v->dwControlFlags & AMCONTROL_COLORINFO_PRESENT)
  631. extended_format_info = (DXVA2_ExtendedFormat *) &v->dwControlFlags;
  632. } else if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
  633. fx = (void *) type->pbFormat;
  634. devtype = AudioDevice;
  635. } else {
  636. return NULL;
  637. }
  638. fmt_info = av_mallocz(sizeof(struct dshow_format_info));
  639. if (!fmt_info)
  640. return NULL;
  641. // initialize fields where unset is not zero
  642. fmt_info->pix_fmt = AV_PIX_FMT_NONE;
  643. fmt_info->col_space = AVCOL_SPC_UNSPECIFIED;
  644. fmt_info->col_prim = AVCOL_PRI_UNSPECIFIED;
  645. fmt_info->col_trc = AVCOL_TRC_UNSPECIFIED;
  646. // now get info about format
  647. fmt_info->devtype = devtype;
  648. if (devtype == VideoDevice) {
  649. fmt_info->width = bih->biWidth;
  650. fmt_info->height = bih->biHeight;
  651. fmt_info->framerate = framerate;
  652. fmt_info->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  653. if (fmt_info->pix_fmt == AV_PIX_FMT_NONE) {
  654. const AVCodecTag *const tags[] = { avformat_get_riff_video_tags(), NULL };
  655. fmt_info->codec_id = av_codec_get_id(tags, bih->biCompression);
  656. }
  657. else
  658. fmt_info->codec_id = AV_CODEC_ID_RAWVIDEO;
  659. if (extended_format_info) {
  660. fmt_info->col_range = dshow_color_range(extended_format_info);
  661. fmt_info->col_space = dshow_color_space(extended_format_info);
  662. fmt_info->col_prim = dshow_color_primaries(extended_format_info);
  663. fmt_info->col_trc = dshow_color_trc(extended_format_info);
  664. fmt_info->chroma_loc = dshow_chroma_loc(extended_format_info);
  665. }
  666. } else {
  667. fmt_info->sample_rate = fx->nSamplesPerSec;
  668. fmt_info->sample_size = fx->wBitsPerSample;
  669. fmt_info->channels = fx->nChannels;
  670. }
  671. return fmt_info;
  672. }
  673. static void dshow_get_default_format(IPin *pin, IAMStreamConfig *config, enum dshowDeviceType devtype, AM_MEDIA_TYPE **type)
  674. {
  675. HRESULT hr;
  676. if ((hr = IAMStreamConfig_GetFormat(config, type)) != S_OK) {
  677. if (hr == E_NOTIMPL || !IsEqualGUID(&(*type)->majortype, devtype == VideoDevice ? &MEDIATYPE_Video : &MEDIATYPE_Audio)) {
  678. // default not available or of wrong type,
  679. // fall back to iterating exposed formats
  680. // until one of the right type is found
  681. IEnumMediaTypes* types = NULL;
  682. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  683. return;
  684. IEnumMediaTypes_Reset(types);
  685. while (IEnumMediaTypes_Next(types, 1, type, NULL) == S_OK) {
  686. if (IsEqualGUID(&(*type)->majortype, devtype == VideoDevice ? &MEDIATYPE_Video : &MEDIATYPE_Audio)) {
  687. break;
  688. }
  689. CoTaskMemFree(*type);
  690. *type = NULL;
  691. }
  692. IEnumMediaTypes_Release(types);
  693. }
  694. }
  695. }
  696. /**
  697. * Cycle through available formats available from the specified pin,
  698. * try to set parameters specified through AVOptions, or the pin's
  699. * default format if no such parameters were set. If successful,
  700. * return 1 in *pformat_set.
  701. * If pformat_set is NULL, list all pin capabilities.
  702. */
  703. static void
  704. dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
  705. IPin *pin, int *pformat_set)
  706. {
  707. struct dshow_ctx *ctx = avctx->priv_data;
  708. IAMStreamConfig *config = NULL;
  709. AM_MEDIA_TYPE *type = NULL;
  710. AM_MEDIA_TYPE *previous_match_type = NULL;
  711. int format_set = 0;
  712. void *caps = NULL;
  713. int i, n, size, r;
  714. int wait_for_better = 0;
  715. int use_default;
  716. // format parameters requested by user
  717. // if none are requested by user, the values will below be set to
  718. // those of the default format
  719. // video
  720. enum AVCodecID requested_video_codec_id = ctx->video_codec_id;
  721. enum AVPixelFormat requested_pixel_format = ctx->pixel_format;
  722. int64_t requested_framerate = ctx->framerate ? ((int64_t)ctx->requested_framerate.den * 10000000)
  723. / ctx->requested_framerate.num : 0;
  724. int requested_width = ctx->requested_width;
  725. int requested_height = ctx->requested_height;
  726. // audio
  727. int requested_sample_rate = ctx->sample_rate;
  728. int requested_sample_size = ctx->sample_size;
  729. int requested_channels = ctx->channels;
  730. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  731. return;
  732. if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
  733. goto end;
  734. caps = av_malloc(size);
  735. if (!caps)
  736. goto end;
  737. /**
  738. * If we should open the device with the default format,
  739. * then:
  740. * 1. check what the format of the default device is, and
  741. * 2. below we iterate all formats till we find a matching
  742. * one, with most info exposed (see comment below).
  743. */
  744. use_default = !dshow_should_set_format(avctx, devtype);
  745. if (use_default && pformat_set)
  746. {
  747. // get default
  748. dshow_get_default_format(pin, config, devtype, &type);
  749. if (!type)
  750. // this pin does not expose any formats of the expected type
  751. goto end;
  752. if (type) {
  753. // interrogate default format, so we know what to search for below
  754. struct dshow_format_info *fmt_info = dshow_get_format_info(type);
  755. if (fmt_info) {
  756. if (fmt_info->devtype == VideoDevice) {
  757. requested_video_codec_id = fmt_info->codec_id;
  758. requested_pixel_format = fmt_info->pix_fmt;
  759. requested_framerate = fmt_info->framerate;
  760. requested_width = fmt_info->width;
  761. requested_height = fmt_info->height;
  762. } else {
  763. requested_sample_rate = fmt_info->sample_rate;
  764. requested_sample_size = fmt_info->sample_size;
  765. requested_channels = fmt_info->channels;
  766. }
  767. av_free(fmt_info); // free but don't set to NULL to enable below check
  768. }
  769. if (type && type->pbFormat)
  770. CoTaskMemFree(type->pbFormat);
  771. CoTaskMemFree(type);
  772. type = NULL;
  773. if (!fmt_info)
  774. // default format somehow invalid, can't continue with this pin
  775. goto end;
  776. fmt_info = NULL;
  777. }
  778. }
  779. // NB: some devices (e.g. Logitech C920) expose each video format twice:
  780. // both a format containing a VIDEOINFOHEADER and a format containing
  781. // a VIDEOINFOHEADER2. We want, if possible, to select a format with a
  782. // VIDEOINFOHEADER2, as this potentially provides more info about the
  783. // format. So, if in the iteration below we have found a matching format,
  784. // but it is a VIDEOINFOHEADER, keep looking for a matching format that
  785. // exposes contains a VIDEOINFOHEADER2. Fall back to the VIDEOINFOHEADER
  786. // format if no corresponding VIDEOINFOHEADER2 is found when we finish
  787. // iterating.
  788. for (i = 0; i < n && !format_set; i++) {
  789. struct dshow_format_info *fmt_info = NULL;
  790. r = IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
  791. if (r != S_OK)
  792. goto next;
  793. #if DSHOWDEBUG
  794. ff_print_AM_MEDIA_TYPE(type);
  795. #endif
  796. fmt_info = dshow_get_format_info(type);
  797. if (!fmt_info)
  798. goto next;
  799. if (devtype == VideoDevice) {
  800. VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
  801. BITMAPINFOHEADER *bih;
  802. int64_t *fr;
  803. #if DSHOWDEBUG
  804. ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
  805. #endif
  806. if (fmt_info->devtype != VideoDevice)
  807. goto next;
  808. if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
  809. VIDEOINFOHEADER *v = (void *) type->pbFormat;
  810. fr = &v->AvgTimePerFrame;
  811. bih = &v->bmiHeader;
  812. wait_for_better = 1;
  813. } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
  814. VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
  815. fr = &v->AvgTimePerFrame;
  816. bih = &v->bmiHeader;
  817. wait_for_better = 0;
  818. }
  819. if (!pformat_set) {
  820. const char *chroma = av_chroma_location_name(fmt_info->chroma_loc);
  821. if (fmt_info->pix_fmt == AV_PIX_FMT_NONE) {
  822. const AVCodec *codec = avcodec_find_decoder(fmt_info->codec_id);
  823. if (fmt_info->codec_id == AV_CODEC_ID_NONE || !codec) {
  824. av_log(avctx, AV_LOG_INFO, " unknown compression type 0x%X", (int) bih->biCompression);
  825. } else {
  826. av_log(avctx, AV_LOG_INFO, " vcodec=%s", codec->name);
  827. }
  828. } else {
  829. av_log(avctx, AV_LOG_INFO, " pixel_format=%s", av_get_pix_fmt_name(fmt_info->pix_fmt));
  830. }
  831. av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g",
  832. vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
  833. 1e7 / vcaps->MaxFrameInterval,
  834. vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
  835. 1e7 / vcaps->MinFrameInterval);
  836. if (fmt_info->col_range != AVCOL_RANGE_UNSPECIFIED ||
  837. fmt_info->col_space != AVCOL_SPC_UNSPECIFIED ||
  838. fmt_info->col_prim != AVCOL_PRI_UNSPECIFIED ||
  839. fmt_info->col_trc != AVCOL_TRC_UNSPECIFIED) {
  840. const char *range = av_color_range_name(fmt_info->col_range);
  841. const char *space = av_color_space_name(fmt_info->col_space);
  842. const char *prim = av_color_primaries_name(fmt_info->col_prim);
  843. const char *trc = av_color_transfer_name(fmt_info->col_trc);
  844. av_log(avctx, AV_LOG_INFO, " (%s, %s/%s/%s",
  845. range ? range : "unknown",
  846. space ? space : "unknown",
  847. prim ? prim : "unknown",
  848. trc ? trc : "unknown");
  849. if (fmt_info->chroma_loc != AVCHROMA_LOC_UNSPECIFIED)
  850. av_log(avctx, AV_LOG_INFO, ", %s", chroma ? chroma : "unknown");
  851. av_log(avctx, AV_LOG_INFO, ")");
  852. }
  853. else if (fmt_info->chroma_loc != AVCHROMA_LOC_UNSPECIFIED)
  854. av_log(avctx, AV_LOG_INFO, "(%s)", chroma ? chroma : "unknown");
  855. av_log(avctx, AV_LOG_INFO, "\n");
  856. goto next;
  857. }
  858. if (requested_video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  859. if (requested_video_codec_id != fmt_info->codec_id)
  860. goto next;
  861. }
  862. if (requested_pixel_format != AV_PIX_FMT_NONE &&
  863. requested_pixel_format != fmt_info->pix_fmt) {
  864. goto next;
  865. }
  866. if (requested_framerate) {
  867. if (requested_framerate > vcaps->MaxFrameInterval ||
  868. requested_framerate < vcaps->MinFrameInterval)
  869. goto next;
  870. *fr = requested_framerate;
  871. }
  872. if (requested_width && requested_height) {
  873. if (requested_width > vcaps->MaxOutputSize.cx ||
  874. requested_width < vcaps->MinOutputSize.cx ||
  875. requested_height > vcaps->MaxOutputSize.cy ||
  876. requested_height < vcaps->MinOutputSize.cy)
  877. goto next;
  878. bih->biWidth = requested_width;
  879. bih->biHeight = requested_height;
  880. }
  881. } else {
  882. WAVEFORMATEX *fx;
  883. AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
  884. #if DSHOWDEBUG
  885. ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
  886. #endif
  887. if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
  888. fx = (void *) type->pbFormat;
  889. } else {
  890. goto next;
  891. }
  892. if (!pformat_set) {
  893. av_log(
  894. avctx,
  895. AV_LOG_INFO,
  896. " ch=%2u, bits=%2u, rate=%6lu\n",
  897. fx->nChannels, fx->wBitsPerSample, fx->nSamplesPerSec
  898. );
  899. continue;
  900. }
  901. if (
  902. (requested_sample_rate && requested_sample_rate != fx->nSamplesPerSec) ||
  903. (requested_sample_size && requested_sample_size != fx->wBitsPerSample) ||
  904. (requested_channels && requested_channels != fx->nChannels )
  905. ) {
  906. goto next;
  907. }
  908. }
  909. // found a matching format. Either apply or store
  910. // for safekeeping if we might maybe find a better
  911. // format with more info attached to it (see comment
  912. // above loop)
  913. if (!wait_for_better) {
  914. if (IAMStreamConfig_SetFormat(config, type) != S_OK)
  915. goto next;
  916. format_set = 1;
  917. }
  918. else if (!previous_match_type) {
  919. // store this matching format for possible later use.
  920. // If we have already found a matching format, ignore it
  921. previous_match_type = type;
  922. type = NULL;
  923. }
  924. next:
  925. av_freep(&fmt_info);
  926. if (type && type->pbFormat)
  927. CoTaskMemFree(type->pbFormat);
  928. CoTaskMemFree(type);
  929. type = NULL;
  930. }
  931. // set the pin's format, if wanted
  932. if (pformat_set && !format_set) {
  933. if (previous_match_type) {
  934. // previously found a matching VIDEOINFOHEADER format and stored
  935. // it for safe keeping. Searching further for a matching
  936. // VIDEOINFOHEADER2 format yielded nothing. So set the pin's
  937. // format based on the VIDEOINFOHEADER format.
  938. // NB: this never applies to an audio format because
  939. // previous_match_type always NULL in that case
  940. if (IAMStreamConfig_SetFormat(config, previous_match_type) == S_OK)
  941. format_set = 1;
  942. }
  943. else if (use_default) {
  944. // default format returned by device apparently was not contained
  945. // in the capabilities of any of the formats returned by the device
  946. // (sic?). Fall back to directly setting the default format
  947. dshow_get_default_format(pin, config, devtype, &type);
  948. if (IAMStreamConfig_SetFormat(config, type) == S_OK)
  949. format_set = 1;
  950. if (type && type->pbFormat)
  951. CoTaskMemFree(type->pbFormat);
  952. CoTaskMemFree(type);
  953. type = NULL;
  954. }
  955. }
  956. end:
  957. if (previous_match_type && previous_match_type->pbFormat)
  958. CoTaskMemFree(previous_match_type->pbFormat);
  959. CoTaskMemFree(previous_match_type);
  960. IAMStreamConfig_Release(config);
  961. av_free(caps);
  962. if (pformat_set)
  963. *pformat_set = format_set;
  964. }
  965. /**
  966. * Set audio device buffer size in milliseconds (which can directly impact
  967. * latency, depending on the device).
  968. */
  969. static int
  970. dshow_set_audio_buffer_size(AVFormatContext *avctx, IPin *pin)
  971. {
  972. struct dshow_ctx *ctx = avctx->priv_data;
  973. IAMBufferNegotiation *buffer_negotiation = NULL;
  974. ALLOCATOR_PROPERTIES props = { -1, -1, -1, -1 };
  975. IAMStreamConfig *config = NULL;
  976. AM_MEDIA_TYPE *type = NULL;
  977. int ret = AVERROR(EIO);
  978. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  979. goto end;
  980. if (IAMStreamConfig_GetFormat(config, &type) != S_OK)
  981. goto end;
  982. if (!IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx))
  983. goto end;
  984. props.cbBuffer = (((WAVEFORMATEX *) type->pbFormat)->nAvgBytesPerSec)
  985. * ctx->audio_buffer_size / 1000;
  986. if (IPin_QueryInterface(pin, &IID_IAMBufferNegotiation, (void **) &buffer_negotiation) != S_OK)
  987. goto end;
  988. if (IAMBufferNegotiation_SuggestAllocatorProperties(buffer_negotiation, &props) != S_OK)
  989. goto end;
  990. ret = 0;
  991. end:
  992. if (buffer_negotiation)
  993. IAMBufferNegotiation_Release(buffer_negotiation);
  994. if (type) {
  995. if (type->pbFormat)
  996. CoTaskMemFree(type->pbFormat);
  997. CoTaskMemFree(type);
  998. }
  999. if (config)
  1000. IAMStreamConfig_Release(config);
  1001. return ret;
  1002. }
  1003. /**
  1004. * Pops up a user dialog allowing them to adjust properties for the given filter, if possible.
  1005. */
  1006. void
  1007. ff_dshow_show_filter_properties(IBaseFilter *device_filter, AVFormatContext *avctx) {
  1008. ISpecifyPropertyPages *property_pages = NULL;
  1009. IUnknown *device_filter_iunknown = NULL;
  1010. HRESULT hr;
  1011. FILTER_INFO filter_info = {0}; /* a warning on this line is false positive GCC bug 53119 AFAICT */
  1012. CAUUID ca_guid = {0};
  1013. hr = IBaseFilter_QueryInterface(device_filter, &IID_ISpecifyPropertyPages, (void **)&property_pages);
  1014. if (hr != S_OK) {
  1015. av_log(avctx, AV_LOG_WARNING, "requested filter does not have a property page to show");
  1016. goto end;
  1017. }
  1018. hr = IBaseFilter_QueryFilterInfo(device_filter, &filter_info);
  1019. if (hr != S_OK) {
  1020. goto fail;
  1021. }
  1022. hr = IBaseFilter_QueryInterface(device_filter, &IID_IUnknown, (void **)&device_filter_iunknown);
  1023. if (hr != S_OK) {
  1024. goto fail;
  1025. }
  1026. hr = ISpecifyPropertyPages_GetPages(property_pages, &ca_guid);
  1027. if (hr != S_OK) {
  1028. goto fail;
  1029. }
  1030. hr = OleCreatePropertyFrame(NULL, 0, 0, filter_info.achName, 1, &device_filter_iunknown, ca_guid.cElems,
  1031. ca_guid.pElems, 0, 0, NULL);
  1032. if (hr != S_OK) {
  1033. goto fail;
  1034. }
  1035. goto end;
  1036. fail:
  1037. av_log(avctx, AV_LOG_ERROR, "Failure showing property pages for filter");
  1038. end:
  1039. if (property_pages)
  1040. ISpecifyPropertyPages_Release(property_pages);
  1041. if (device_filter_iunknown)
  1042. IUnknown_Release(device_filter_iunknown);
  1043. if (filter_info.pGraph)
  1044. IFilterGraph_Release(filter_info.pGraph);
  1045. if (ca_guid.pElems)
  1046. CoTaskMemFree(ca_guid.pElems);
  1047. }
  1048. /**
  1049. * Cycle through available pins using the device_filter device, of type
  1050. * devtype, retrieve the first output pin and return the pointer to the
  1051. * object found in *ppin.
  1052. * If ppin is NULL, cycle through all pins listing audio/video capabilities.
  1053. */
  1054. static int
  1055. dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
  1056. enum dshowSourceFilterType sourcetype, IBaseFilter *device_filter, IPin **ppin)
  1057. {
  1058. struct dshow_ctx *ctx = avctx->priv_data;
  1059. IEnumPins *pins = 0;
  1060. IPin *device_pin = NULL;
  1061. IPin *pin;
  1062. int r;
  1063. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio only";
  1064. const char *sourcetypename = (sourcetype == VideoSourceDevice) ? "video" : "audio";
  1065. int set_format = dshow_should_set_format(avctx, devtype);
  1066. int format_set = 0;
  1067. int should_show_properties = (devtype == VideoDevice) ? ctx->show_video_device_dialog : ctx->show_audio_device_dialog;
  1068. if (should_show_properties)
  1069. ff_dshow_show_filter_properties(device_filter, avctx);
  1070. r = IBaseFilter_EnumPins(device_filter, &pins);
  1071. if (r != S_OK) {
  1072. av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
  1073. return AVERROR(EIO);
  1074. }
  1075. if (!ppin) {
  1076. av_log(avctx, AV_LOG_INFO, "DirectShow %s device options (from %s devices)\n",
  1077. devtypename, sourcetypename);
  1078. }
  1079. while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
  1080. IKsPropertySet *p = NULL;
  1081. PIN_INFO info = {0};
  1082. GUID category;
  1083. DWORD r2;
  1084. char *name_buf = NULL;
  1085. wchar_t *pin_id = NULL;
  1086. char *pin_buf = NULL;
  1087. char *desired_pin_name = devtype == VideoDevice ? ctx->video_pin_name : ctx->audio_pin_name;
  1088. IPin_QueryPinInfo(pin, &info);
  1089. IBaseFilter_Release(info.pFilter);
  1090. if (info.dir != PINDIR_OUTPUT)
  1091. goto next;
  1092. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  1093. goto next;
  1094. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  1095. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  1096. goto next;
  1097. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  1098. goto next;
  1099. name_buf = dup_wchar_to_utf8(info.achName);
  1100. r = IPin_QueryId(pin, &pin_id);
  1101. if (r != S_OK) {
  1102. av_log(avctx, AV_LOG_ERROR, "Could not query pin id\n");
  1103. return AVERROR(EIO);
  1104. }
  1105. pin_buf = dup_wchar_to_utf8(pin_id);
  1106. if (!ppin) {
  1107. av_log(avctx, AV_LOG_INFO, " Pin \"%s\" (alternative pin name \"%s\")\n", name_buf, pin_buf);
  1108. dshow_cycle_formats(avctx, devtype, pin, NULL);
  1109. goto next;
  1110. }
  1111. if (desired_pin_name) {
  1112. if(strcmp(name_buf, desired_pin_name) && strcmp(pin_buf, desired_pin_name)) {
  1113. av_log(avctx, AV_LOG_DEBUG, "skipping pin \"%s\" (\"%s\") != requested \"%s\"\n",
  1114. name_buf, pin_buf, desired_pin_name);
  1115. goto next;
  1116. }
  1117. }
  1118. // will either try to find format matching options supplied by user
  1119. // or try to open default format. Successful if returns with format_set==1
  1120. dshow_cycle_formats(avctx, devtype, pin, &format_set);
  1121. if (!format_set) {
  1122. goto next;
  1123. }
  1124. if (devtype == AudioDevice && ctx->audio_buffer_size) {
  1125. if (dshow_set_audio_buffer_size(avctx, pin) < 0) {
  1126. av_log(avctx, AV_LOG_ERROR, "unable to set audio buffer size %d to pin, using pin anyway...", ctx->audio_buffer_size);
  1127. }
  1128. }
  1129. if (format_set) {
  1130. device_pin = pin;
  1131. av_log(avctx, AV_LOG_DEBUG, "Selecting pin %s on %s\n", name_buf, devtypename);
  1132. }
  1133. next:
  1134. if (p)
  1135. IKsPropertySet_Release(p);
  1136. if (device_pin != pin)
  1137. IPin_Release(pin);
  1138. av_free(name_buf);
  1139. av_free(pin_buf);
  1140. if (pin_id)
  1141. CoTaskMemFree(pin_id);
  1142. }
  1143. IEnumPins_Release(pins);
  1144. if (ppin) {
  1145. if (set_format && !format_set) {
  1146. av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
  1147. return AVERROR(EIO);
  1148. }
  1149. if (!device_pin) {
  1150. av_log(avctx, AV_LOG_ERROR,
  1151. "Could not find output pin from %s capture device.\n", devtypename);
  1152. return AVERROR(EIO);
  1153. }
  1154. *ppin = device_pin;
  1155. }
  1156. return 0;
  1157. }
  1158. /**
  1159. * List options for device with type devtype, source filter type sourcetype
  1160. *
  1161. * @param devenum device enumerator used for accessing the device
  1162. */
  1163. static int
  1164. dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
  1165. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype)
  1166. {
  1167. struct dshow_ctx *ctx = avctx->priv_data;
  1168. IBaseFilter *device_filter = NULL;
  1169. char *device_unique_name = NULL;
  1170. int r;
  1171. if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_unique_name, NULL)) < 0)
  1172. return r;
  1173. ctx->device_filter[devtype] = device_filter;
  1174. ctx->device_unique_name[devtype] = device_unique_name;
  1175. if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, NULL)) < 0)
  1176. return r;
  1177. return 0;
  1178. }
  1179. static int
  1180. dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
  1181. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype)
  1182. {
  1183. struct dshow_ctx *ctx = avctx->priv_data;
  1184. IBaseFilter *device_filter = NULL;
  1185. char *device_filter_unique_name = NULL;
  1186. IGraphBuilder *graph = ctx->graph;
  1187. IPin *device_pin = NULL;
  1188. DShowPin *capture_pin = NULL;
  1189. DShowFilter *capture_filter = NULL;
  1190. ICaptureGraphBuilder2 *graph_builder2 = NULL;
  1191. int ret = AVERROR(EIO);
  1192. int r;
  1193. IStream *ifile_stream = NULL;
  1194. IStream *ofile_stream = NULL;
  1195. IPersistStream *pers_stream = NULL;
  1196. enum dshowDeviceType otherDevType = (devtype == VideoDevice) ? AudioDevice : VideoDevice;
  1197. const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
  1198. if ( ((ctx->audio_filter_load_file) && (strlen(ctx->audio_filter_load_file)>0) && (sourcetype == AudioSourceDevice)) ||
  1199. ((ctx->video_filter_load_file) && (strlen(ctx->video_filter_load_file)>0) && (sourcetype == VideoSourceDevice)) ) {
  1200. HRESULT hr;
  1201. char *filename = NULL;
  1202. if (sourcetype == AudioSourceDevice)
  1203. filename = ctx->audio_filter_load_file;
  1204. else
  1205. filename = ctx->video_filter_load_file;
  1206. hr = SHCreateStreamOnFile ((LPCSTR) filename, STGM_READ, &ifile_stream);
  1207. if (S_OK != hr) {
  1208. av_log(avctx, AV_LOG_ERROR, "Could not open capture filter description file.\n");
  1209. goto error;
  1210. }
  1211. hr = OleLoadFromStream(ifile_stream, &IID_IBaseFilter, (void **) &device_filter);
  1212. if (hr != S_OK) {
  1213. av_log(avctx, AV_LOG_ERROR, "Could not load capture filter from file.\n");
  1214. goto error;
  1215. }
  1216. if (sourcetype == AudioSourceDevice)
  1217. av_log(avctx, AV_LOG_INFO, "Audio-");
  1218. else
  1219. av_log(avctx, AV_LOG_INFO, "Video-");
  1220. av_log(avctx, AV_LOG_INFO, "Capture filter loaded successfully from file \"%s\".\n", filename);
  1221. } else {
  1222. if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_filter_unique_name, NULL)) < 0) {
  1223. ret = r;
  1224. goto error;
  1225. }
  1226. }
  1227. if (ctx->device_filter[otherDevType]) {
  1228. // avoid adding add two instances of the same device to the graph, one for video, one for audio
  1229. // a few devices don't support this (could also do this check earlier to avoid double crossbars, etc. but they seem OK)
  1230. if (strcmp(device_filter_unique_name, ctx->device_unique_name[otherDevType]) == 0) {
  1231. av_log(avctx, AV_LOG_DEBUG, "reusing previous graph capture filter... %s\n", device_filter_unique_name);
  1232. IBaseFilter_Release(device_filter);
  1233. device_filter = ctx->device_filter[otherDevType];
  1234. IBaseFilter_AddRef(ctx->device_filter[otherDevType]);
  1235. } else {
  1236. av_log(avctx, AV_LOG_DEBUG, "not reusing previous graph capture filter %s != %s\n", device_filter_unique_name, ctx->device_unique_name[otherDevType]);
  1237. }
  1238. }
  1239. ctx->device_filter [devtype] = device_filter;
  1240. ctx->device_unique_name [devtype] = device_filter_unique_name;
  1241. r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
  1242. if (r != S_OK) {
  1243. av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
  1244. goto error;
  1245. }
  1246. if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, &device_pin)) < 0) {
  1247. ret = r;
  1248. goto error;
  1249. }
  1250. ctx->device_pin[devtype] = device_pin;
  1251. capture_filter = ff_dshow_filter_Create(avctx, callback, devtype);
  1252. if (!capture_filter) {
  1253. av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
  1254. goto error;
  1255. }
  1256. ctx->capture_filter[devtype] = capture_filter;
  1257. if ( ((ctx->audio_filter_save_file) && (strlen(ctx->audio_filter_save_file)>0) && (sourcetype == AudioSourceDevice)) ||
  1258. ((ctx->video_filter_save_file) && (strlen(ctx->video_filter_save_file)>0) && (sourcetype == VideoSourceDevice)) ) {
  1259. HRESULT hr;
  1260. char *filename = NULL;
  1261. if (sourcetype == AudioSourceDevice)
  1262. filename = ctx->audio_filter_save_file;
  1263. else
  1264. filename = ctx->video_filter_save_file;
  1265. hr = SHCreateStreamOnFile ((LPCSTR) filename, STGM_CREATE | STGM_READWRITE, &ofile_stream);
  1266. if (S_OK != hr) {
  1267. av_log(avctx, AV_LOG_ERROR, "Could not create capture filter description file.\n");
  1268. goto error;
  1269. }
  1270. hr = IBaseFilter_QueryInterface(device_filter, &IID_IPersistStream, (void **) &pers_stream);
  1271. if (hr != S_OK) {
  1272. av_log(avctx, AV_LOG_ERROR, "Query for IPersistStream failed.\n");
  1273. goto error;
  1274. }
  1275. hr = OleSaveToStream(pers_stream, ofile_stream);
  1276. if (hr != S_OK) {
  1277. av_log(avctx, AV_LOG_ERROR, "Could not save capture filter \n");
  1278. goto error;
  1279. }
  1280. hr = IStream_Commit(ofile_stream, STGC_DEFAULT);
  1281. if (S_OK != hr) {
  1282. av_log(avctx, AV_LOG_ERROR, "Could not commit capture filter data to file.\n");
  1283. goto error;
  1284. }
  1285. if (sourcetype == AudioSourceDevice)
  1286. av_log(avctx, AV_LOG_INFO, "Audio-");
  1287. else
  1288. av_log(avctx, AV_LOG_INFO, "Video-");
  1289. av_log(avctx, AV_LOG_INFO, "Capture filter saved successfully to file \"%s\".\n", filename);
  1290. }
  1291. r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
  1292. filter_name[devtype]);
  1293. if (r != S_OK) {
  1294. av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
  1295. goto error;
  1296. }
  1297. ff_dshow_pin_AddRef(capture_filter->pin);
  1298. capture_pin = capture_filter->pin;
  1299. ctx->capture_pin[devtype] = capture_pin;
  1300. r = CoCreateInstance(&CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
  1301. &IID_ICaptureGraphBuilder2, (void **) &graph_builder2);
  1302. if (r != S_OK) {
  1303. av_log(avctx, AV_LOG_ERROR, "Could not create CaptureGraphBuilder2\n");
  1304. goto error;
  1305. }
  1306. ICaptureGraphBuilder2_SetFiltergraph(graph_builder2, graph);
  1307. if (r != S_OK) {
  1308. av_log(avctx, AV_LOG_ERROR, "Could not set graph for CaptureGraphBuilder2\n");
  1309. goto error;
  1310. }
  1311. r = ICaptureGraphBuilder2_RenderStream(graph_builder2, NULL, NULL, (IUnknown *) device_pin, NULL /* no intermediate filter */,
  1312. (IBaseFilter *) capture_filter); /* connect pins, optionally insert intermediate filters like crossbar if necessary */
  1313. if (r != S_OK) {
  1314. av_log(avctx, AV_LOG_ERROR, "Could not RenderStream to connect pins\n");
  1315. goto error;
  1316. }
  1317. r = ff_dshow_try_setup_crossbar_options(graph_builder2, device_filter, devtype, avctx);
  1318. if (r != S_OK) {
  1319. av_log(avctx, AV_LOG_ERROR, "Could not setup CrossBar\n");
  1320. goto error;
  1321. }
  1322. ret = 0;
  1323. error:
  1324. if (graph_builder2 != NULL)
  1325. ICaptureGraphBuilder2_Release(graph_builder2);
  1326. if (pers_stream)
  1327. IPersistStream_Release(pers_stream);
  1328. if (ifile_stream)
  1329. IStream_Release(ifile_stream);
  1330. if (ofile_stream)
  1331. IStream_Release(ofile_stream);
  1332. return ret;
  1333. }
  1334. static enum AVCodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
  1335. {
  1336. switch (sample_fmt) {
  1337. case AV_SAMPLE_FMT_U8: return AV_CODEC_ID_PCM_U8;
  1338. case AV_SAMPLE_FMT_S16: return AV_CODEC_ID_PCM_S16LE;
  1339. case AV_SAMPLE_FMT_S32: return AV_CODEC_ID_PCM_S32LE;
  1340. default: return AV_CODEC_ID_NONE; /* Should never happen. */
  1341. }
  1342. }
  1343. static enum AVSampleFormat sample_fmt_bits_per_sample(int bits)
  1344. {
  1345. switch (bits) {
  1346. case 8: return AV_SAMPLE_FMT_U8;
  1347. case 16: return AV_SAMPLE_FMT_S16;
  1348. case 32: return AV_SAMPLE_FMT_S32;
  1349. default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
  1350. }
  1351. }
  1352. static int
  1353. dshow_add_device(AVFormatContext *avctx,
  1354. enum dshowDeviceType devtype)
  1355. {
  1356. struct dshow_ctx *ctx = avctx->priv_data;
  1357. AM_MEDIA_TYPE type;
  1358. AVCodecParameters *par;
  1359. AVStream *st;
  1360. struct dshow_format_info *fmt_info = NULL;
  1361. int ret = AVERROR(EIO);
  1362. type.pbFormat = NULL;
  1363. st = avformat_new_stream(avctx, NULL);
  1364. if (!st) {
  1365. ret = AVERROR(ENOMEM);
  1366. goto error;
  1367. }
  1368. st->id = devtype;
  1369. ctx->capture_filter[devtype]->stream_index = st->index;
  1370. ff_dshow_pin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
  1371. fmt_info = dshow_get_format_info(&type);
  1372. if (!fmt_info) {
  1373. ret = AVERROR(EIO);
  1374. goto error;
  1375. }
  1376. par = st->codecpar;
  1377. if (devtype == VideoDevice) {
  1378. BITMAPINFOHEADER *bih = NULL;
  1379. AVRational time_base;
  1380. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  1381. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  1382. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  1383. bih = &v->bmiHeader;
  1384. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  1385. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  1386. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  1387. bih = &v->bmiHeader;
  1388. }
  1389. if (!bih) {
  1390. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  1391. goto error;
  1392. }
  1393. st->avg_frame_rate = av_inv_q(time_base);
  1394. st->r_frame_rate = av_inv_q(time_base);
  1395. par->codec_type = AVMEDIA_TYPE_VIDEO;
  1396. par->width = fmt_info->width;
  1397. par->height = fmt_info->height;
  1398. par->codec_tag = bih->biCompression;
  1399. par->format = fmt_info->pix_fmt;
  1400. if (bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
  1401. av_log(avctx, AV_LOG_DEBUG, "attempt to use full range for HDYC...\n");
  1402. par->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
  1403. }
  1404. par->color_range = fmt_info->col_range;
  1405. par->color_space = fmt_info->col_space;
  1406. par->color_primaries = fmt_info->col_prim;
  1407. par->color_trc = fmt_info->col_trc;
  1408. par->chroma_location = fmt_info->chroma_loc;
  1409. par->codec_id = fmt_info->codec_id;
  1410. if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
  1411. if (bih->biCompression == BI_RGB || bih->biCompression == BI_BITFIELDS) {
  1412. par->bits_per_coded_sample = bih->biBitCount;
  1413. if (par->height < 0) {
  1414. par->height *= -1;
  1415. } else {
  1416. par->extradata = av_malloc(9 + AV_INPUT_BUFFER_PADDING_SIZE);
  1417. if (par->extradata) {
  1418. par->extradata_size = 9;
  1419. memcpy(par->extradata, "BottomUp", 9);
  1420. }
  1421. }
  1422. }
  1423. } else {
  1424. if (par->codec_id == AV_CODEC_ID_NONE) {
  1425. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  1426. "Please report type 0x%X.\n", (int) bih->biCompression);
  1427. ret = AVERROR_PATCHWELCOME;
  1428. goto error;
  1429. }
  1430. par->bits_per_coded_sample = bih->biBitCount;
  1431. }
  1432. } else {
  1433. if (!IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  1434. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  1435. goto error;
  1436. }
  1437. par->codec_type = AVMEDIA_TYPE_AUDIO;
  1438. par->format = sample_fmt_bits_per_sample(fmt_info->sample_size);
  1439. par->codec_id = waveform_codec_id(par->format);
  1440. par->sample_rate = fmt_info->sample_rate;
  1441. par->ch_layout.nb_channels = fmt_info->channels;
  1442. }
  1443. avpriv_set_pts_info(st, 64, 1, 10000000);
  1444. ret = 0;
  1445. error:
  1446. av_freep(&fmt_info);
  1447. if (type.pbFormat)
  1448. CoTaskMemFree(type.pbFormat);
  1449. return ret;
  1450. }
  1451. static int parse_device_name(AVFormatContext *avctx)
  1452. {
  1453. struct dshow_ctx *ctx = avctx->priv_data;
  1454. char **device_name = ctx->device_name;
  1455. char *name = av_strdup(avctx->url);
  1456. char *tmp = name;
  1457. int ret = 1;
  1458. char *type;
  1459. while ((type = strtok(tmp, "="))) {
  1460. char *token = strtok(NULL, ":");
  1461. tmp = NULL;
  1462. if (!strcmp(type, "video")) {
  1463. device_name[0] = token;
  1464. } else if (!strcmp(type, "audio")) {
  1465. device_name[1] = token;
  1466. } else {
  1467. device_name[0] = NULL;
  1468. device_name[1] = NULL;
  1469. break;
  1470. }
  1471. }
  1472. if (!device_name[0] && !device_name[1]) {
  1473. ret = 0;
  1474. } else {
  1475. if (device_name[0])
  1476. device_name[0] = av_strdup(device_name[0]);
  1477. if (device_name[1])
  1478. device_name[1] = av_strdup(device_name[1]);
  1479. }
  1480. av_free(name);
  1481. return ret;
  1482. }
  1483. static int dshow_read_header(AVFormatContext *avctx)
  1484. {
  1485. struct dshow_ctx *ctx = avctx->priv_data;
  1486. IGraphBuilder *graph = NULL;
  1487. ICreateDevEnum *devenum = NULL;
  1488. IMediaControl *control = NULL;
  1489. IMediaEvent *media_event = NULL;
  1490. HANDLE media_event_handle;
  1491. HANDLE proc;
  1492. int ret = AVERROR(EIO);
  1493. int r;
  1494. CoInitialize(0);
  1495. if (!ctx->list_devices && !parse_device_name(avctx)) {
  1496. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  1497. goto error;
  1498. }
  1499. ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id
  1500. : AV_CODEC_ID_RAWVIDEO;
  1501. if (ctx->pixel_format != AV_PIX_FMT_NONE) {
  1502. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  1503. av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when "
  1504. "video codec is not set or set to rawvideo\n");
  1505. ret = AVERROR(EINVAL);
  1506. goto error;
  1507. }
  1508. }
  1509. if (ctx->framerate) {
  1510. r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
  1511. if (r < 0) {
  1512. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
  1513. goto error;
  1514. }
  1515. }
  1516. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  1517. &IID_IGraphBuilder, (void **) &graph);
  1518. if (r != S_OK) {
  1519. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  1520. goto error;
  1521. }
  1522. ctx->graph = graph;
  1523. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  1524. &IID_ICreateDevEnum, (void **) &devenum);
  1525. if (r != S_OK) {
  1526. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  1527. goto error;
  1528. }
  1529. if (ctx->list_devices) {
  1530. dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, NULL);
  1531. dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, NULL);
  1532. ret = AVERROR_EXIT;
  1533. goto error;
  1534. }
  1535. if (ctx->list_options) {
  1536. if (ctx->device_name[VideoDevice])
  1537. if ((r = dshow_list_device_options(avctx, devenum, VideoDevice, VideoSourceDevice))) {
  1538. ret = r;
  1539. goto error;
  1540. }
  1541. if (ctx->device_name[AudioDevice]) {
  1542. if (dshow_list_device_options(avctx, devenum, AudioDevice, AudioSourceDevice)) {
  1543. /* show audio options from combined video+audio sources as fallback */
  1544. if ((r = dshow_list_device_options(avctx, devenum, AudioDevice, VideoSourceDevice))) {
  1545. ret = r;
  1546. goto error;
  1547. }
  1548. }
  1549. }
  1550. // don't exit yet, allow it to list crossbar options in dshow_open_device
  1551. }
  1552. if (ctx->device_name[VideoDevice]) {
  1553. if ((r = dshow_open_device(avctx, devenum, VideoDevice, VideoSourceDevice)) < 0 ||
  1554. (r = dshow_add_device(avctx, VideoDevice)) < 0) {
  1555. ret = r;
  1556. goto error;
  1557. }
  1558. }
  1559. if (ctx->device_name[AudioDevice]) {
  1560. if ((r = dshow_open_device(avctx, devenum, AudioDevice, AudioSourceDevice)) < 0 ||
  1561. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  1562. av_log(avctx, AV_LOG_INFO, "Searching for audio device within video devices for %s\n", ctx->device_name[AudioDevice]);
  1563. /* see if there's a video source with an audio pin with the given audio name */
  1564. if ((r = dshow_open_device(avctx, devenum, AudioDevice, VideoSourceDevice)) < 0 ||
  1565. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  1566. ret = r;
  1567. goto error;
  1568. }
  1569. }
  1570. }
  1571. if (ctx->list_options) {
  1572. /* allow it to list crossbar options in dshow_open_device */
  1573. ret = AVERROR_EXIT;
  1574. goto error;
  1575. }
  1576. ctx->curbufsize[0] = 0;
  1577. ctx->curbufsize[1] = 0;
  1578. ctx->mutex = CreateMutex(NULL, 0, NULL);
  1579. if (!ctx->mutex) {
  1580. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  1581. goto error;
  1582. }
  1583. ctx->event[1] = CreateEvent(NULL, 1, 0, NULL);
  1584. if (!ctx->event[1]) {
  1585. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  1586. goto error;
  1587. }
  1588. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  1589. if (r != S_OK) {
  1590. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  1591. goto error;
  1592. }
  1593. ctx->control = control;
  1594. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event);
  1595. if (r != S_OK) {
  1596. av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n");
  1597. goto error;
  1598. }
  1599. ctx->media_event = media_event;
  1600. r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle);
  1601. if (r != S_OK) {
  1602. av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n");
  1603. goto error;
  1604. }
  1605. proc = GetCurrentProcess();
  1606. r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0],
  1607. 0, 0, DUPLICATE_SAME_ACCESS);
  1608. if (!r) {
  1609. av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n");
  1610. goto error;
  1611. }
  1612. r = IMediaControl_Run(control);
  1613. if (r == S_FALSE) {
  1614. OAFilterState pfs;
  1615. r = IMediaControl_GetState(control, 0, &pfs);
  1616. }
  1617. if (r != S_OK) {
  1618. av_log(avctx, AV_LOG_ERROR, "Could not run graph (sometimes caused by a device already in use by other application)\n");
  1619. goto error;
  1620. }
  1621. ret = 0;
  1622. error:
  1623. if (devenum)
  1624. ICreateDevEnum_Release(devenum);
  1625. if (ret < 0)
  1626. dshow_read_close(avctx);
  1627. return ret;
  1628. }
  1629. /**
  1630. * Checks media events from DirectShow and returns -1 on error or EOF. Also
  1631. * purges all events that might be in the event queue to stop the trigger
  1632. * of event notification.
  1633. */
  1634. static int dshow_check_event_queue(IMediaEvent *media_event)
  1635. {
  1636. LONG_PTR p1, p2;
  1637. long code;
  1638. int ret = 0;
  1639. while (IMediaEvent_GetEvent(media_event, &code, &p1, &p2, 0) != E_ABORT) {
  1640. if (code == EC_COMPLETE || code == EC_DEVICE_LOST || code == EC_ERRORABORT)
  1641. ret = -1;
  1642. IMediaEvent_FreeEventParams(media_event, code, p1, p2);
  1643. }
  1644. return ret;
  1645. }
  1646. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  1647. {
  1648. struct dshow_ctx *ctx = s->priv_data;
  1649. PacketListEntry *pktl = NULL;
  1650. while (!ctx->eof && !pktl) {
  1651. WaitForSingleObject(ctx->mutex, INFINITE);
  1652. pktl = ctx->pktl;
  1653. if (pktl) {
  1654. *pkt = pktl->pkt;
  1655. ctx->pktl = ctx->pktl->next;
  1656. av_free(pktl);
  1657. ctx->curbufsize[pkt->stream_index] -= pkt->size;
  1658. }
  1659. ResetEvent(ctx->event[1]);
  1660. ReleaseMutex(ctx->mutex);
  1661. if (!pktl) {
  1662. if (dshow_check_event_queue(ctx->media_event) < 0) {
  1663. ctx->eof = 1;
  1664. } else if (s->flags & AVFMT_FLAG_NONBLOCK) {
  1665. return AVERROR(EAGAIN);
  1666. } else {
  1667. WaitForMultipleObjects(2, ctx->event, 0, INFINITE);
  1668. }
  1669. }
  1670. }
  1671. return ctx->eof ? AVERROR(EIO) : pkt->size;
  1672. }
  1673. #define OFFSET(x) offsetof(struct dshow_ctx, x)
  1674. #define DEC AV_OPT_FLAG_DECODING_PARAM
  1675. static const AVOption options[] = {
  1676. { "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 },
  1677. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX, DEC },
  1678. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1679. { "sample_rate", "set audio sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1680. { "sample_size", "set audio sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 16, DEC },
  1681. { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1682. { "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 },
  1683. { "list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, DEC },
  1684. { "list_options", "list available options for specified device", OFFSET(list_options), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, DEC },
  1685. { "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 },
  1686. { "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 },
  1687. { "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 },
  1688. { "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 },
  1689. { "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 },
  1690. { "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 },
  1691. { "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 },
  1692. { "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 },
  1693. { "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 },
  1694. { "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 },
  1695. { "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 },
  1696. { "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 },
  1697. { "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 },
  1698. { "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 },
  1699. { "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 },
  1700. { "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 },
  1701. { "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 },
  1702. { NULL },
  1703. };
  1704. static const AVClass dshow_class = {
  1705. .class_name = "dshow indev",
  1706. .item_name = av_default_item_name,
  1707. .option = options,
  1708. .version = LIBAVUTIL_VERSION_INT,
  1709. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  1710. };
  1711. const AVInputFormat ff_dshow_demuxer = {
  1712. .name = "dshow",
  1713. .long_name = NULL_IF_CONFIG_SMALL("DirectShow capture"),
  1714. .priv_data_size = sizeof(struct dshow_ctx),
  1715. .read_header = dshow_read_header,
  1716. .read_packet = dshow_read_packet,
  1717. .read_close = dshow_read_close,
  1718. .get_device_list= dshow_get_device_list,
  1719. .flags = AVFMT_NOFILE | AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  1720. .priv_class = &dshow_class,
  1721. };