dshow.c 73 KB

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