dshow.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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/demux.h"
  27. #include "libavformat/internal.h"
  28. #include "libavformat/riff.h"
  29. #include "avdevice.h"
  30. #include "libavcodec/raw.h"
  31. #include "objidl.h"
  32. #include "shlwapi.h"
  33. // NB: technically, we should include dxva.h and use
  34. // DXVA_ExtendedFormat, but that type is not defined in
  35. // the MinGW headers. The DXVA2_ExtendedFormat and the
  36. // contents of its fields is identical to
  37. // DXVA_ExtendedFormat (see https://docs.microsoft.com/en-us/windows/win32/medfound/extended-color-information#color-space-in-media-types)
  38. // and is provided by MinGW as well, so we use that
  39. // instead. NB also that per the Microsoft docs, the
  40. // lowest 8 bits of the structure, i.e. the SampleFormat
  41. // field, contain AMCONTROL_xxx flags instead of sample
  42. // format information, and should thus not be used.
  43. // NB further that various values in the structure's
  44. // fields (e.g. BT.2020 color space) are not provided
  45. // for either of the DXVA structs, but are provided in
  46. // the flags of the corresponding fields of Media Foundation.
  47. // These may be provided by DirectShow devices (e.g. LAVFilters
  48. // does so). So we use those values here too (the equivalence is
  49. // indicated by Microsoft example code: https://docs.microsoft.com/en-us/windows/win32/api/dxva2api/ns-dxva2api-dxva2_videodesc)
  50. #include "d3d9types.h"
  51. #include "dxva2api.h"
  52. #ifndef AMCONTROL_COLORINFO_PRESENT
  53. // not defined in some versions of MinGW's dvdmedia.h
  54. # define AMCONTROL_COLORINFO_PRESENT 0x00000080 // if set, indicates DXVA color info is present in the upper (24) bits of the dwControlFlags
  55. #endif
  56. static enum AVPixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
  57. {
  58. switch(biCompression) {
  59. case BI_BITFIELDS:
  60. case BI_RGB:
  61. switch(biBitCount) { /* 1-8 are untested */
  62. case 1:
  63. return AV_PIX_FMT_MONOWHITE;
  64. case 4:
  65. return AV_PIX_FMT_RGB4;
  66. case 8:
  67. return AV_PIX_FMT_RGB8;
  68. case 16:
  69. return AV_PIX_FMT_RGB555;
  70. case 24:
  71. return AV_PIX_FMT_BGR24;
  72. case 32:
  73. return AV_PIX_FMT_0RGB32;
  74. }
  75. }
  76. return avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, biCompression); // all others
  77. }
  78. static enum AVColorRange dshow_color_range(DXVA2_ExtendedFormat *fmt_info)
  79. {
  80. switch (fmt_info->NominalRange)
  81. {
  82. case DXVA2_NominalRange_Unknown:
  83. return AVCOL_RANGE_UNSPECIFIED;
  84. case DXVA2_NominalRange_Normal: // equal to DXVA2_NominalRange_0_255
  85. return AVCOL_RANGE_JPEG;
  86. case DXVA2_NominalRange_Wide: // equal to DXVA2_NominalRange_16_235
  87. return AVCOL_RANGE_MPEG;
  88. case DXVA2_NominalRange_48_208:
  89. // not an ffmpeg color range
  90. return AVCOL_RANGE_UNSPECIFIED;
  91. // values from MediaFoundation SDK (mfobjects.h)
  92. case 4: // MFNominalRange_64_127
  93. // not an ffmpeg color range
  94. return AVCOL_RANGE_UNSPECIFIED;
  95. default:
  96. return AVCOL_RANGE_UNSPECIFIED;
  97. }
  98. }
  99. static enum AVColorSpace dshow_color_space(DXVA2_ExtendedFormat *fmt_info)
  100. {
  101. switch (fmt_info->VideoTransferMatrix)
  102. {
  103. case DXVA2_VideoTransferMatrix_BT709:
  104. return AVCOL_SPC_BT709;
  105. case DXVA2_VideoTransferMatrix_BT601:
  106. return AVCOL_SPC_BT470BG;
  107. case DXVA2_VideoTransferMatrix_SMPTE240M:
  108. return AVCOL_SPC_SMPTE240M;
  109. // values from MediaFoundation SDK (mfobjects.h)
  110. case 4: // MFVideoTransferMatrix_BT2020_10
  111. case 5: // MFVideoTransferMatrix_BT2020_12
  112. if (fmt_info->VideoTransferFunction == 12) // MFVideoTransFunc_2020_const
  113. return AVCOL_SPC_BT2020_CL;
  114. else
  115. return AVCOL_SPC_BT2020_NCL;
  116. default:
  117. return AVCOL_SPC_UNSPECIFIED;
  118. }
  119. }
  120. static enum AVColorPrimaries dshow_color_primaries(DXVA2_ExtendedFormat *fmt_info)
  121. {
  122. switch (fmt_info->VideoPrimaries)
  123. {
  124. case DXVA2_VideoPrimaries_Unknown:
  125. return AVCOL_PRI_UNSPECIFIED;
  126. case DXVA2_VideoPrimaries_reserved:
  127. return AVCOL_PRI_RESERVED;
  128. case DXVA2_VideoPrimaries_BT709:
  129. return AVCOL_PRI_BT709;
  130. case DXVA2_VideoPrimaries_BT470_2_SysM:
  131. return AVCOL_PRI_BT470M;
  132. case DXVA2_VideoPrimaries_BT470_2_SysBG:
  133. case DXVA2_VideoPrimaries_EBU3213: // this is PAL
  134. return AVCOL_PRI_BT470BG;
  135. case DXVA2_VideoPrimaries_SMPTE170M:
  136. case DXVA2_VideoPrimaries_SMPTE_C:
  137. return AVCOL_PRI_SMPTE170M;
  138. case DXVA2_VideoPrimaries_SMPTE240M:
  139. return AVCOL_PRI_SMPTE240M;
  140. // values from MediaFoundation SDK (mfobjects.h)
  141. case 9: // MFVideoPrimaries_BT2020
  142. return AVCOL_PRI_BT2020;
  143. case 10: // MFVideoPrimaries_XYZ
  144. return AVCOL_PRI_SMPTE428;
  145. case 11: // MFVideoPrimaries_DCI_P3
  146. return AVCOL_PRI_SMPTE431;
  147. case 12: // MFVideoPrimaries_ACES (Academy Color Encoding System)
  148. // not an FFmpeg color primary
  149. return AVCOL_PRI_UNSPECIFIED;
  150. default:
  151. return AVCOL_PRI_UNSPECIFIED;
  152. }
  153. }
  154. static enum AVColorTransferCharacteristic dshow_color_trc(DXVA2_ExtendedFormat *fmt_info)
  155. {
  156. switch (fmt_info->VideoTransferFunction)
  157. {
  158. case DXVA2_VideoTransFunc_Unknown:
  159. return AVCOL_TRC_UNSPECIFIED;
  160. case DXVA2_VideoTransFunc_10:
  161. return AVCOL_TRC_LINEAR;
  162. case DXVA2_VideoTransFunc_18:
  163. // not an FFmpeg transfer characteristic
  164. return AVCOL_TRC_UNSPECIFIED;
  165. case DXVA2_VideoTransFunc_20:
  166. // not an FFmpeg transfer characteristic
  167. return AVCOL_TRC_UNSPECIFIED;
  168. case DXVA2_VideoTransFunc_22:
  169. return AVCOL_TRC_GAMMA22;
  170. case DXVA2_VideoTransFunc_709:
  171. return AVCOL_TRC_BT709;
  172. case DXVA2_VideoTransFunc_240M:
  173. return AVCOL_TRC_SMPTE240M;
  174. case DXVA2_VideoTransFunc_sRGB:
  175. return AVCOL_TRC_IEC61966_2_1;
  176. case DXVA2_VideoTransFunc_28:
  177. return AVCOL_TRC_GAMMA28;
  178. // values from MediaFoundation SDK (mfobjects.h)
  179. case 9: // MFVideoTransFunc_Log_100
  180. return AVCOL_TRC_LOG;
  181. case 10: // MFVideoTransFunc_Log_316
  182. return AVCOL_TRC_LOG_SQRT;
  183. case 11: // MFVideoTransFunc_709_sym
  184. // not an FFmpeg transfer characteristic
  185. return AVCOL_TRC_UNSPECIFIED;
  186. case 12: // MFVideoTransFunc_2020_const
  187. case 13: // MFVideoTransFunc_2020
  188. if (fmt_info->VideoTransferMatrix == 5) // MFVideoTransferMatrix_BT2020_12
  189. return AVCOL_TRC_BT2020_12;
  190. else
  191. return AVCOL_TRC_BT2020_10;
  192. case 14: // MFVideoTransFunc_26
  193. // not an FFmpeg transfer characteristic
  194. return AVCOL_TRC_UNSPECIFIED;
  195. case 15: // MFVideoTransFunc_2084
  196. return AVCOL_TRC_SMPTEST2084;
  197. case 16: // MFVideoTransFunc_HLG
  198. return AVCOL_TRC_ARIB_STD_B67;
  199. case 17: // MFVideoTransFunc_10_rel
  200. // not an FFmpeg transfer characteristic? Undocumented also by MS
  201. return AVCOL_TRC_UNSPECIFIED;
  202. default:
  203. return AVCOL_TRC_UNSPECIFIED;
  204. }
  205. }
  206. static enum AVChromaLocation dshow_chroma_loc(DXVA2_ExtendedFormat *fmt_info)
  207. {
  208. if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_Cosited) // that is: (DXVA2_VideoChromaSubsampling_Horizontally_Cosited | DXVA2_VideoChromaSubsampling_Vertically_Cosited | DXVA2_VideoChromaSubsampling_Vertically_AlignedChromaPlanes)
  209. return AVCHROMA_LOC_TOPLEFT;
  210. else if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_MPEG1) // that is: DXVA2_VideoChromaSubsampling_Vertically_AlignedChromaPlanes
  211. return AVCHROMA_LOC_CENTER;
  212. else if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_MPEG2) // that is: (DXVA2_VideoChromaSubsampling_Horizontally_Cosited | DXVA2_VideoChromaSubsampling_Vertically_AlignedChromaPlanes)
  213. return AVCHROMA_LOC_LEFT;
  214. else if (fmt_info->VideoChromaSubsampling == DXVA2_VideoChromaSubsampling_DV_PAL) // that is: (DXVA2_VideoChromaSubsampling_Horizontally_Cosited | DXVA2_VideoChromaSubsampling_Vertically_Cosited)
  215. return AVCHROMA_LOC_TOPLEFT;
  216. else
  217. // unknown
  218. return AVCHROMA_LOC_UNSPECIFIED;
  219. }
  220. static int
  221. dshow_read_close(AVFormatContext *s)
  222. {
  223. struct dshow_ctx *ctx = s->priv_data;
  224. PacketListEntry *pktl;
  225. if (ctx->control) {
  226. IMediaControl_Stop(ctx->control);
  227. IMediaControl_Release(ctx->control);
  228. }
  229. if (ctx->media_event)
  230. IMediaEvent_Release(ctx->media_event);
  231. if (ctx->graph) {
  232. IEnumFilters *fenum;
  233. int r;
  234. r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
  235. if (r == S_OK) {
  236. IBaseFilter *f;
  237. IEnumFilters_Reset(fenum);
  238. while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK) {
  239. if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK)
  240. IEnumFilters_Reset(fenum); /* When a filter is removed,
  241. * the list must be reset. */
  242. IBaseFilter_Release(f);
  243. }
  244. IEnumFilters_Release(fenum);
  245. }
  246. IGraphBuilder_Release(ctx->graph);
  247. }
  248. if (ctx->capture_pin[VideoDevice])
  249. ff_dshow_pin_Release(ctx->capture_pin[VideoDevice]);
  250. if (ctx->capture_pin[AudioDevice])
  251. ff_dshow_pin_Release(ctx->capture_pin[AudioDevice]);
  252. if (ctx->capture_filter[VideoDevice])
  253. ff_dshow_filter_Release(ctx->capture_filter[VideoDevice]);
  254. if (ctx->capture_filter[AudioDevice])
  255. ff_dshow_filter_Release(ctx->capture_filter[AudioDevice]);
  256. if (ctx->device_pin[VideoDevice])
  257. IPin_Release(ctx->device_pin[VideoDevice]);
  258. if (ctx->device_pin[AudioDevice])
  259. IPin_Release(ctx->device_pin[AudioDevice]);
  260. if (ctx->device_filter[VideoDevice])
  261. IBaseFilter_Release(ctx->device_filter[VideoDevice]);
  262. if (ctx->device_filter[AudioDevice])
  263. IBaseFilter_Release(ctx->device_filter[AudioDevice]);
  264. av_freep(&ctx->device_name[0]);
  265. av_freep(&ctx->device_name[1]);
  266. av_freep(&ctx->device_unique_name[0]);
  267. av_freep(&ctx->device_unique_name[1]);
  268. if(ctx->mutex)
  269. CloseHandle(ctx->mutex);
  270. if(ctx->event[0])
  271. CloseHandle(ctx->event[0]);
  272. if(ctx->event[1])
  273. CloseHandle(ctx->event[1]);
  274. pktl = ctx->pktl;
  275. while (pktl) {
  276. PacketListEntry *next = pktl->next;
  277. av_packet_unref(&pktl->pkt);
  278. av_free(pktl);
  279. pktl = next;
  280. }
  281. CoUninitialize();
  282. return 0;
  283. }
  284. static char *dup_wchar_to_utf8(wchar_t *w)
  285. {
  286. char *s = NULL;
  287. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  288. s = av_malloc(l);
  289. if (s)
  290. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  291. return s;
  292. }
  293. static int shall_we_drop(AVFormatContext *s, int index, enum dshowDeviceType devtype)
  294. {
  295. struct dshow_ctx *ctx = s->priv_data;
  296. static const uint8_t dropscore[] = {62, 75, 87, 100};
  297. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  298. unsigned int buffer_fullness = (ctx->curbufsize[index]*100)/s->max_picture_buffer;
  299. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  300. if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
  301. av_log(s, AV_LOG_ERROR,
  302. "real-time buffer [%s] [%s input] too full or near too full (%d%% of size: %d [rtbufsize parameter])! frame dropped!\n",
  303. ctx->device_name[devtype], devtypename, buffer_fullness, s->max_picture_buffer);
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. static void
  309. callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
  310. {
  311. AVFormatContext *s = priv_data;
  312. struct dshow_ctx *ctx = s->priv_data;
  313. PacketListEntry **ppktl, *pktl_next;
  314. // dump_videohdr(s, vdhdr);
  315. WaitForSingleObject(ctx->mutex, INFINITE);
  316. if(shall_we_drop(s, index, devtype))
  317. goto fail;
  318. pktl_next = av_mallocz(sizeof(*pktl_next));
  319. if(!pktl_next)
  320. goto fail;
  321. if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
  322. av_free(pktl_next);
  323. goto fail;
  324. }
  325. pktl_next->pkt.stream_index = index;
  326. pktl_next->pkt.pts = time;
  327. memcpy(pktl_next->pkt.data, buf, buf_size);
  328. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  329. *ppktl = pktl_next;
  330. ctx->curbufsize[index] += buf_size;
  331. SetEvent(ctx->event[1]);
  332. ReleaseMutex(ctx->mutex);
  333. return;
  334. fail:
  335. ReleaseMutex(ctx->mutex);
  336. return;
  337. }
  338. static void
  339. dshow_get_device_media_types(AVFormatContext *avctx, enum dshowDeviceType devtype,
  340. enum dshowSourceFilterType sourcetype, IBaseFilter *device_filter,
  341. enum AVMediaType **media_types, int *nb_media_types)
  342. {
  343. IEnumPins *pins = 0;
  344. IPin *pin;
  345. int has_audio = 0, has_video = 0;
  346. if (IBaseFilter_EnumPins(device_filter, &pins) != S_OK)
  347. return;
  348. while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
  349. IKsPropertySet *p = NULL;
  350. PIN_INFO info = { 0 };
  351. GUID category;
  352. DWORD r2;
  353. IEnumMediaTypes *types = NULL;
  354. AM_MEDIA_TYPE *type;
  355. if (IPin_QueryPinInfo(pin, &info) != S_OK)
  356. goto next;
  357. IBaseFilter_Release(info.pFilter);
  358. if (info.dir != PINDIR_OUTPUT)
  359. goto next;
  360. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  361. goto next;
  362. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  363. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  364. goto next;
  365. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  366. goto next;
  367. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  368. goto next;
  369. // enumerate media types exposed by pin
  370. // NB: don't know if a pin can expose both audio and video, check 'm all to be safe
  371. IEnumMediaTypes_Reset(types);
  372. while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) {
  373. if (IsEqualGUID(&type->majortype, &MEDIATYPE_Video)) {
  374. has_video = 1;
  375. } else if (IsEqualGUID(&type->majortype, &MEDIATYPE_Audio)) {
  376. has_audio = 1;
  377. }
  378. CoTaskMemFree(type);
  379. }
  380. next:
  381. if (types)
  382. IEnumMediaTypes_Release(types);
  383. if (p)
  384. IKsPropertySet_Release(p);
  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 && ret != AVERROR(EIO))
  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. goto next;
  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 (!device_filter_unique_name || 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. r = 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. if (ff_dshow_pin_ConnectionMediaType(ctx->capture_pin[devtype], &type) != S_OK) {
  1371. ret = AVERROR(EIO);
  1372. goto error;
  1373. }
  1374. fmt_info = dshow_get_format_info(&type);
  1375. if (!fmt_info) {
  1376. ret = AVERROR(EIO);
  1377. goto error;
  1378. }
  1379. par = st->codecpar;
  1380. if (devtype == VideoDevice) {
  1381. BITMAPINFOHEADER *bih = NULL;
  1382. AVRational time_base;
  1383. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  1384. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  1385. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  1386. bih = &v->bmiHeader;
  1387. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  1388. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  1389. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  1390. bih = &v->bmiHeader;
  1391. }
  1392. if (!bih) {
  1393. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  1394. goto error;
  1395. }
  1396. st->avg_frame_rate = av_inv_q(time_base);
  1397. st->r_frame_rate = av_inv_q(time_base);
  1398. par->codec_type = AVMEDIA_TYPE_VIDEO;
  1399. par->width = fmt_info->width;
  1400. par->height = fmt_info->height;
  1401. par->codec_tag = bih->biCompression;
  1402. par->format = fmt_info->pix_fmt;
  1403. if (bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
  1404. av_log(avctx, AV_LOG_DEBUG, "attempt to use full range for HDYC...\n");
  1405. par->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
  1406. }
  1407. par->color_range = fmt_info->col_range;
  1408. par->color_space = fmt_info->col_space;
  1409. par->color_primaries = fmt_info->col_prim;
  1410. par->color_trc = fmt_info->col_trc;
  1411. par->chroma_location = fmt_info->chroma_loc;
  1412. par->codec_id = fmt_info->codec_id;
  1413. if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
  1414. if (bih->biCompression == BI_RGB || bih->biCompression == BI_BITFIELDS) {
  1415. par->bits_per_coded_sample = bih->biBitCount;
  1416. if (par->height < 0) {
  1417. par->height *= -1;
  1418. } else {
  1419. par->extradata = av_malloc(9 + AV_INPUT_BUFFER_PADDING_SIZE);
  1420. if (par->extradata) {
  1421. par->extradata_size = 9;
  1422. memcpy(par->extradata, "BottomUp", 9);
  1423. }
  1424. }
  1425. }
  1426. } else {
  1427. if (par->codec_id == AV_CODEC_ID_NONE) {
  1428. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  1429. "Please report type 0x%X.\n", (int) bih->biCompression);
  1430. ret = AVERROR_PATCHWELCOME;
  1431. goto error;
  1432. }
  1433. par->bits_per_coded_sample = bih->biBitCount;
  1434. }
  1435. } else {
  1436. if (!IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  1437. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  1438. goto error;
  1439. }
  1440. par->codec_type = AVMEDIA_TYPE_AUDIO;
  1441. par->format = sample_fmt_bits_per_sample(fmt_info->sample_size);
  1442. par->codec_id = waveform_codec_id(par->format);
  1443. par->sample_rate = fmt_info->sample_rate;
  1444. par->ch_layout.nb_channels = fmt_info->channels;
  1445. }
  1446. avpriv_set_pts_info(st, 64, 1, 10000000);
  1447. ret = 0;
  1448. error:
  1449. av_freep(&fmt_info);
  1450. if (type.pbFormat)
  1451. CoTaskMemFree(type.pbFormat);
  1452. return ret;
  1453. }
  1454. static int parse_device_name(AVFormatContext *avctx)
  1455. {
  1456. struct dshow_ctx *ctx = avctx->priv_data;
  1457. char **device_name = ctx->device_name;
  1458. char *name = av_strdup(avctx->url);
  1459. char *tmp = name;
  1460. int ret = 1;
  1461. char *type;
  1462. while ((type = strtok(tmp, "="))) {
  1463. char *token = strtok(NULL, ":");
  1464. tmp = NULL;
  1465. if (!strcmp(type, "video")) {
  1466. device_name[0] = token;
  1467. } else if (!strcmp(type, "audio")) {
  1468. device_name[1] = token;
  1469. } else {
  1470. device_name[0] = NULL;
  1471. device_name[1] = NULL;
  1472. break;
  1473. }
  1474. }
  1475. if (!device_name[0] && !device_name[1]) {
  1476. ret = 0;
  1477. } else {
  1478. if (device_name[0])
  1479. device_name[0] = av_strdup(device_name[0]);
  1480. if (device_name[1])
  1481. device_name[1] = av_strdup(device_name[1]);
  1482. }
  1483. av_free(name);
  1484. return ret;
  1485. }
  1486. static int dshow_read_header(AVFormatContext *avctx)
  1487. {
  1488. struct dshow_ctx *ctx = avctx->priv_data;
  1489. IGraphBuilder *graph = NULL;
  1490. ICreateDevEnum *devenum = NULL;
  1491. IMediaControl *control = NULL;
  1492. IMediaEvent *media_event = NULL;
  1493. HANDLE media_event_handle;
  1494. HANDLE proc;
  1495. int ret = AVERROR(EIO);
  1496. int r;
  1497. CoInitialize(0);
  1498. if (!ctx->list_devices && !parse_device_name(avctx)) {
  1499. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  1500. goto error;
  1501. }
  1502. ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id
  1503. : AV_CODEC_ID_RAWVIDEO;
  1504. if (ctx->pixel_format != AV_PIX_FMT_NONE) {
  1505. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  1506. av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when "
  1507. "video codec is not set or set to rawvideo\n");
  1508. ret = AVERROR(EINVAL);
  1509. goto error;
  1510. }
  1511. }
  1512. if (ctx->framerate) {
  1513. r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
  1514. if (r < 0) {
  1515. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
  1516. goto error;
  1517. }
  1518. }
  1519. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  1520. &IID_IGraphBuilder, (void **) &graph);
  1521. if (r != S_OK) {
  1522. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  1523. goto error;
  1524. }
  1525. ctx->graph = graph;
  1526. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  1527. &IID_ICreateDevEnum, (void **) &devenum);
  1528. if (r != S_OK) {
  1529. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  1530. goto error;
  1531. }
  1532. if (ctx->list_devices) {
  1533. dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, NULL);
  1534. dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, NULL);
  1535. ret = AVERROR_EXIT;
  1536. goto error;
  1537. }
  1538. if (ctx->list_options) {
  1539. if (ctx->device_name[VideoDevice])
  1540. if ((r = dshow_list_device_options(avctx, devenum, VideoDevice, VideoSourceDevice))) {
  1541. ret = r;
  1542. goto error;
  1543. }
  1544. if (ctx->device_name[AudioDevice]) {
  1545. if (dshow_list_device_options(avctx, devenum, AudioDevice, AudioSourceDevice)) {
  1546. /* show audio options from combined video+audio sources as fallback */
  1547. if ((r = dshow_list_device_options(avctx, devenum, AudioDevice, VideoSourceDevice))) {
  1548. ret = r;
  1549. goto error;
  1550. }
  1551. }
  1552. }
  1553. // don't exit yet, allow it to list crossbar options in dshow_open_device
  1554. }
  1555. if (ctx->device_name[VideoDevice]) {
  1556. if ((r = dshow_open_device(avctx, devenum, VideoDevice, VideoSourceDevice)) < 0 ||
  1557. (r = dshow_add_device(avctx, VideoDevice)) < 0) {
  1558. ret = r;
  1559. goto error;
  1560. }
  1561. }
  1562. if (ctx->device_name[AudioDevice]) {
  1563. if ((r = dshow_open_device(avctx, devenum, AudioDevice, AudioSourceDevice)) < 0 ||
  1564. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  1565. av_log(avctx, AV_LOG_INFO, "Searching for audio device within video devices for %s\n", ctx->device_name[AudioDevice]);
  1566. /* see if there's a video source with an audio pin with the given audio name */
  1567. if ((r = dshow_open_device(avctx, devenum, AudioDevice, VideoSourceDevice)) < 0 ||
  1568. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  1569. ret = r;
  1570. goto error;
  1571. }
  1572. }
  1573. }
  1574. if (ctx->list_options) {
  1575. /* allow it to list crossbar options in dshow_open_device */
  1576. ret = AVERROR_EXIT;
  1577. goto error;
  1578. }
  1579. ctx->curbufsize[0] = 0;
  1580. ctx->curbufsize[1] = 0;
  1581. ctx->mutex = CreateMutex(NULL, 0, NULL);
  1582. if (!ctx->mutex) {
  1583. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  1584. goto error;
  1585. }
  1586. ctx->event[1] = CreateEvent(NULL, 1, 0, NULL);
  1587. if (!ctx->event[1]) {
  1588. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  1589. goto error;
  1590. }
  1591. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  1592. if (r != S_OK) {
  1593. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  1594. goto error;
  1595. }
  1596. ctx->control = control;
  1597. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event);
  1598. if (r != S_OK) {
  1599. av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n");
  1600. goto error;
  1601. }
  1602. ctx->media_event = media_event;
  1603. r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle);
  1604. if (r != S_OK) {
  1605. av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n");
  1606. goto error;
  1607. }
  1608. proc = GetCurrentProcess();
  1609. r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0],
  1610. 0, 0, DUPLICATE_SAME_ACCESS);
  1611. if (!r) {
  1612. av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n");
  1613. goto error;
  1614. }
  1615. r = IMediaControl_Run(control);
  1616. if (r == S_FALSE) {
  1617. OAFilterState pfs;
  1618. r = IMediaControl_GetState(control, 0, &pfs);
  1619. }
  1620. if (r != S_OK) {
  1621. av_log(avctx, AV_LOG_ERROR, "Could not run graph (sometimes caused by a device already in use by other application)\n");
  1622. goto error;
  1623. }
  1624. ret = 0;
  1625. error:
  1626. if (devenum)
  1627. ICreateDevEnum_Release(devenum);
  1628. if (ret < 0)
  1629. dshow_read_close(avctx);
  1630. return ret;
  1631. }
  1632. /**
  1633. * Checks media events from DirectShow and returns -1 on error or EOF. Also
  1634. * purges all events that might be in the event queue to stop the trigger
  1635. * of event notification.
  1636. */
  1637. static int dshow_check_event_queue(IMediaEvent *media_event)
  1638. {
  1639. LONG_PTR p1, p2;
  1640. long code;
  1641. int ret = 0;
  1642. while (IMediaEvent_GetEvent(media_event, &code, &p1, &p2, 0) != E_ABORT) {
  1643. if (code == EC_COMPLETE || code == EC_DEVICE_LOST || code == EC_ERRORABORT)
  1644. ret = -1;
  1645. IMediaEvent_FreeEventParams(media_event, code, p1, p2);
  1646. }
  1647. return ret;
  1648. }
  1649. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  1650. {
  1651. struct dshow_ctx *ctx = s->priv_data;
  1652. PacketListEntry *pktl = NULL;
  1653. while (!ctx->eof && !pktl) {
  1654. WaitForSingleObject(ctx->mutex, INFINITE);
  1655. pktl = ctx->pktl;
  1656. if (pktl) {
  1657. *pkt = pktl->pkt;
  1658. ctx->pktl = ctx->pktl->next;
  1659. av_free(pktl);
  1660. ctx->curbufsize[pkt->stream_index] -= pkt->size;
  1661. }
  1662. ResetEvent(ctx->event[1]);
  1663. ReleaseMutex(ctx->mutex);
  1664. if (!pktl) {
  1665. if (dshow_check_event_queue(ctx->media_event) < 0) {
  1666. ctx->eof = 1;
  1667. } else if (s->flags & AVFMT_FLAG_NONBLOCK) {
  1668. return AVERROR(EAGAIN);
  1669. } else {
  1670. WaitForMultipleObjects(2, ctx->event, 0, INFINITE);
  1671. }
  1672. }
  1673. }
  1674. return ctx->eof ? AVERROR(EIO) : pkt->size;
  1675. }
  1676. #define OFFSET(x) offsetof(struct dshow_ctx, x)
  1677. #define DEC AV_OPT_FLAG_DECODING_PARAM
  1678. static const AVOption options[] = {
  1679. { "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 },
  1680. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX, DEC },
  1681. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1682. { "sample_rate", "set audio sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1683. { "sample_size", "set audio sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 16, DEC },
  1684. { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1685. { "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 },
  1686. { "list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, DEC },
  1687. { "list_options", "list available options for specified device", OFFSET(list_options), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, DEC },
  1688. { "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 },
  1689. { "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 },
  1690. { "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 },
  1691. { "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 },
  1692. { "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 },
  1693. { "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 },
  1694. { "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 },
  1695. { "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 },
  1696. { "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 },
  1697. { "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 },
  1698. { "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 },
  1699. { "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 },
  1700. { "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 },
  1701. { "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 },
  1702. { "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 },
  1703. { "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 },
  1704. { "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 },
  1705. { NULL },
  1706. };
  1707. static const AVClass dshow_class = {
  1708. .class_name = "dshow indev",
  1709. .item_name = av_default_item_name,
  1710. .option = options,
  1711. .version = LIBAVUTIL_VERSION_INT,
  1712. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  1713. };
  1714. const FFInputFormat ff_dshow_demuxer = {
  1715. .p.name = "dshow",
  1716. .p.long_name = NULL_IF_CONFIG_SMALL("DirectShow capture"),
  1717. .p.flags = AVFMT_NOFILE | AVFMT_NOBINSEARCH |
  1718. AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  1719. .p.priv_class = &dshow_class,
  1720. .priv_data_size = sizeof(struct dshow_ctx),
  1721. .read_header = dshow_read_header,
  1722. .read_packet = dshow_read_packet,
  1723. .read_close = dshow_read_close,
  1724. .get_device_list= dshow_get_device_list,
  1725. };