2
0

hwcontext_d3d11va.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include <windows.h>
  20. #define COBJMACROS
  21. #include <initguid.h>
  22. #include <d3d11.h>
  23. #include <dxgi1_2.h>
  24. #if HAVE_DXGIDEBUG_H
  25. #include <dxgidebug.h>
  26. #endif
  27. #include "avassert.h"
  28. #include "common.h"
  29. #include "hwcontext.h"
  30. #include "hwcontext_d3d11va.h"
  31. #include "hwcontext_internal.h"
  32. #include "imgutils.h"
  33. #include "pixdesc.h"
  34. #include "pixfmt.h"
  35. #include "thread.h"
  36. typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
  37. static AVOnce functions_loaded = AV_ONCE_INIT;
  38. static PFN_CREATE_DXGI_FACTORY mCreateDXGIFactory;
  39. static PFN_D3D11_CREATE_DEVICE mD3D11CreateDevice;
  40. static av_cold void load_functions(void)
  41. {
  42. #if !HAVE_UWP
  43. // We let these "leak" - this is fine, as unloading has no great benefit, and
  44. // Windows will mark a DLL as loaded forever if its internal refcount overflows
  45. // from too many LoadLibrary calls.
  46. HANDLE d3dlib, dxgilib;
  47. d3dlib = LoadLibrary("d3d11.dll");
  48. dxgilib = LoadLibrary("dxgi.dll");
  49. if (!d3dlib || !dxgilib)
  50. return;
  51. mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) GetProcAddress(d3dlib, "D3D11CreateDevice");
  52. mCreateDXGIFactory = (PFN_CREATE_DXGI_FACTORY) GetProcAddress(dxgilib, "CreateDXGIFactory");
  53. #else
  54. // In UWP (which lacks LoadLibrary), CreateDXGIFactory isn't available,
  55. // only CreateDXGIFactory1
  56. mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) D3D11CreateDevice;
  57. mCreateDXGIFactory = (PFN_CREATE_DXGI_FACTORY) CreateDXGIFactory1;
  58. #endif
  59. }
  60. typedef struct D3D11VAFramesContext {
  61. int nb_surfaces_used;
  62. DXGI_FORMAT format;
  63. ID3D11Texture2D *staging_texture;
  64. } D3D11VAFramesContext;
  65. static const struct {
  66. DXGI_FORMAT d3d_format;
  67. enum AVPixelFormat pix_fmt;
  68. } supported_formats[] = {
  69. { DXGI_FORMAT_NV12, AV_PIX_FMT_NV12 },
  70. { DXGI_FORMAT_P010, AV_PIX_FMT_P010 },
  71. // Special opaque formats. The pix_fmt is merely a place holder, as the
  72. // opaque format cannot be accessed directly.
  73. { DXGI_FORMAT_420_OPAQUE, AV_PIX_FMT_YUV420P },
  74. };
  75. static void d3d11va_default_lock(void *ctx)
  76. {
  77. WaitForSingleObjectEx(ctx, INFINITE, FALSE);
  78. }
  79. static void d3d11va_default_unlock(void *ctx)
  80. {
  81. ReleaseMutex(ctx);
  82. }
  83. static void d3d11va_frames_uninit(AVHWFramesContext *ctx)
  84. {
  85. AVD3D11VAFramesContext *frames_hwctx = ctx->hwctx;
  86. D3D11VAFramesContext *s = ctx->internal->priv;
  87. if (frames_hwctx->texture)
  88. ID3D11Texture2D_Release(frames_hwctx->texture);
  89. frames_hwctx->texture = NULL;
  90. if (s->staging_texture)
  91. ID3D11Texture2D_Release(s->staging_texture);
  92. s->staging_texture = NULL;
  93. }
  94. static int d3d11va_frames_get_constraints(AVHWDeviceContext *ctx,
  95. const void *hwconfig,
  96. AVHWFramesConstraints *constraints)
  97. {
  98. AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
  99. int nb_sw_formats = 0;
  100. HRESULT hr;
  101. int i;
  102. constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
  103. sizeof(*constraints->valid_sw_formats));
  104. if (!constraints->valid_sw_formats)
  105. return AVERROR(ENOMEM);
  106. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  107. UINT format_support = 0;
  108. hr = ID3D11Device_CheckFormatSupport(device_hwctx->device, supported_formats[i].d3d_format, &format_support);
  109. if (SUCCEEDED(hr) && (format_support & D3D11_FORMAT_SUPPORT_TEXTURE2D))
  110. constraints->valid_sw_formats[nb_sw_formats++] = supported_formats[i].pix_fmt;
  111. }
  112. constraints->valid_sw_formats[nb_sw_formats] = AV_PIX_FMT_NONE;
  113. constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
  114. if (!constraints->valid_hw_formats)
  115. return AVERROR(ENOMEM);
  116. constraints->valid_hw_formats[0] = AV_PIX_FMT_D3D11;
  117. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  118. return 0;
  119. }
  120. static void free_texture(void *opaque, uint8_t *data)
  121. {
  122. ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
  123. av_free(data);
  124. }
  125. static AVBufferRef *wrap_texture_buf(ID3D11Texture2D *tex, int index)
  126. {
  127. AVBufferRef *buf;
  128. AVD3D11FrameDescriptor *desc = av_mallocz(sizeof(*desc));
  129. if (!desc) {
  130. ID3D11Texture2D_Release(tex);
  131. return NULL;
  132. }
  133. desc->texture = tex;
  134. desc->index = index;
  135. buf = av_buffer_create((uint8_t *)desc, sizeof(desc), free_texture, tex, 0);
  136. if (!buf) {
  137. ID3D11Texture2D_Release(tex);
  138. av_free(desc);
  139. return NULL;
  140. }
  141. return buf;
  142. }
  143. static AVBufferRef *d3d11va_alloc_single(AVHWFramesContext *ctx)
  144. {
  145. D3D11VAFramesContext *s = ctx->internal->priv;
  146. AVD3D11VAFramesContext *hwctx = ctx->hwctx;
  147. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  148. HRESULT hr;
  149. ID3D11Texture2D *tex;
  150. D3D11_TEXTURE2D_DESC texDesc = {
  151. .Width = ctx->width,
  152. .Height = ctx->height,
  153. .MipLevels = 1,
  154. .Format = s->format,
  155. .SampleDesc = { .Count = 1 },
  156. .ArraySize = 1,
  157. .Usage = D3D11_USAGE_DEFAULT,
  158. .BindFlags = hwctx->BindFlags,
  159. .MiscFlags = hwctx->MiscFlags,
  160. };
  161. hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &tex);
  162. if (FAILED(hr)) {
  163. av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
  164. return NULL;
  165. }
  166. return wrap_texture_buf(tex, 0);
  167. }
  168. static AVBufferRef *d3d11va_pool_alloc(void *opaque, int size)
  169. {
  170. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  171. D3D11VAFramesContext *s = ctx->internal->priv;
  172. AVD3D11VAFramesContext *hwctx = ctx->hwctx;
  173. D3D11_TEXTURE2D_DESC texDesc;
  174. if (!hwctx->texture)
  175. return d3d11va_alloc_single(ctx);
  176. ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc);
  177. if (s->nb_surfaces_used >= texDesc.ArraySize) {
  178. av_log(ctx, AV_LOG_ERROR, "Static surface pool size exceeded.\n");
  179. return NULL;
  180. }
  181. ID3D11Texture2D_AddRef(hwctx->texture);
  182. return wrap_texture_buf(hwctx->texture, s->nb_surfaces_used++);
  183. }
  184. static int d3d11va_frames_init(AVHWFramesContext *ctx)
  185. {
  186. AVD3D11VAFramesContext *hwctx = ctx->hwctx;
  187. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  188. D3D11VAFramesContext *s = ctx->internal->priv;
  189. int i;
  190. HRESULT hr;
  191. D3D11_TEXTURE2D_DESC texDesc;
  192. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  193. if (ctx->sw_format == supported_formats[i].pix_fmt) {
  194. s->format = supported_formats[i].d3d_format;
  195. break;
  196. }
  197. }
  198. if (i == FF_ARRAY_ELEMS(supported_formats)) {
  199. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
  200. av_get_pix_fmt_name(ctx->sw_format));
  201. return AVERROR(EINVAL);
  202. }
  203. texDesc = (D3D11_TEXTURE2D_DESC){
  204. .Width = ctx->width,
  205. .Height = ctx->height,
  206. .MipLevels = 1,
  207. .Format = s->format,
  208. .SampleDesc = { .Count = 1 },
  209. .ArraySize = ctx->initial_pool_size,
  210. .Usage = D3D11_USAGE_DEFAULT,
  211. .BindFlags = hwctx->BindFlags,
  212. .MiscFlags = hwctx->MiscFlags,
  213. };
  214. if (hwctx->texture) {
  215. D3D11_TEXTURE2D_DESC texDesc2;
  216. ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc2);
  217. if (texDesc.Width != texDesc2.Width ||
  218. texDesc.Height != texDesc2.Height ||
  219. texDesc.Format != texDesc2.Format) {
  220. av_log(ctx, AV_LOG_ERROR, "User-provided texture has mismatching parameters\n");
  221. return AVERROR(EINVAL);
  222. }
  223. } else if (texDesc.ArraySize > 0) {
  224. hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &hwctx->texture);
  225. if (FAILED(hr)) {
  226. av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
  227. return AVERROR_UNKNOWN;
  228. }
  229. }
  230. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(AVD3D11FrameDescriptor),
  231. ctx, d3d11va_pool_alloc, NULL);
  232. if (!ctx->internal->pool_internal)
  233. return AVERROR(ENOMEM);
  234. return 0;
  235. }
  236. static int d3d11va_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  237. {
  238. AVD3D11FrameDescriptor *desc;
  239. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  240. if (!frame->buf[0])
  241. return AVERROR(ENOMEM);
  242. desc = (AVD3D11FrameDescriptor *)frame->buf[0]->data;
  243. frame->data[0] = (uint8_t *)desc->texture;
  244. frame->data[1] = (uint8_t *)desc->index;
  245. frame->format = AV_PIX_FMT_D3D11;
  246. frame->width = ctx->width;
  247. frame->height = ctx->height;
  248. return 0;
  249. }
  250. static int d3d11va_transfer_get_formats(AVHWFramesContext *ctx,
  251. enum AVHWFrameTransferDirection dir,
  252. enum AVPixelFormat **formats)
  253. {
  254. D3D11VAFramesContext *s = ctx->internal->priv;
  255. enum AVPixelFormat *fmts;
  256. fmts = av_malloc_array(2, sizeof(*fmts));
  257. if (!fmts)
  258. return AVERROR(ENOMEM);
  259. fmts[0] = ctx->sw_format;
  260. fmts[1] = AV_PIX_FMT_NONE;
  261. // Don't signal support for opaque formats. Actual access would fail.
  262. if (s->format == DXGI_FORMAT_420_OPAQUE)
  263. fmts[0] = AV_PIX_FMT_NONE;
  264. *formats = fmts;
  265. return 0;
  266. }
  267. static int d3d11va_create_staging_texture(AVHWFramesContext *ctx)
  268. {
  269. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  270. D3D11VAFramesContext *s = ctx->internal->priv;
  271. HRESULT hr;
  272. D3D11_TEXTURE2D_DESC texDesc = {
  273. .Width = ctx->width,
  274. .Height = ctx->height,
  275. .MipLevels = 1,
  276. .Format = s->format,
  277. .SampleDesc = { .Count = 1 },
  278. .ArraySize = 1,
  279. .Usage = D3D11_USAGE_STAGING,
  280. .CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE,
  281. };
  282. hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &s->staging_texture);
  283. if (FAILED(hr)) {
  284. av_log(ctx, AV_LOG_ERROR, "Could not create the staging texture (%lx)\n", (long)hr);
  285. return AVERROR_UNKNOWN;
  286. }
  287. return 0;
  288. }
  289. static void fill_texture_ptrs(uint8_t *data[4], int linesize[4],
  290. AVHWFramesContext *ctx,
  291. D3D11_TEXTURE2D_DESC *desc,
  292. D3D11_MAPPED_SUBRESOURCE *map)
  293. {
  294. int i;
  295. for (i = 0; i < 4; i++)
  296. linesize[i] = map->RowPitch;
  297. av_image_fill_pointers(data, ctx->sw_format, desc->Height,
  298. (uint8_t*)map->pData, linesize);
  299. }
  300. static int d3d11va_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
  301. const AVFrame *src)
  302. {
  303. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  304. D3D11VAFramesContext *s = ctx->internal->priv;
  305. int download = src->format == AV_PIX_FMT_D3D11;
  306. const AVFrame *frame = download ? src : dst;
  307. const AVFrame *other = download ? dst : src;
  308. // (The interface types are compatible.)
  309. ID3D11Resource *texture = (ID3D11Resource *)(ID3D11Texture2D *)frame->data[0];
  310. int index = (intptr_t)frame->data[1];
  311. ID3D11Resource *staging;
  312. int w = FFMIN(dst->width, src->width);
  313. int h = FFMIN(dst->height, src->height);
  314. uint8_t *map_data[4];
  315. int map_linesize[4];
  316. D3D11_TEXTURE2D_DESC desc;
  317. D3D11_MAPPED_SUBRESOURCE map;
  318. HRESULT hr;
  319. if (frame->hw_frames_ctx->data != (uint8_t *)ctx || other->format != ctx->sw_format)
  320. return AVERROR(EINVAL);
  321. device_hwctx->lock(device_hwctx->lock_ctx);
  322. if (!s->staging_texture) {
  323. int res = d3d11va_create_staging_texture(ctx);
  324. if (res < 0)
  325. return res;
  326. }
  327. staging = (ID3D11Resource *)s->staging_texture;
  328. ID3D11Texture2D_GetDesc(s->staging_texture, &desc);
  329. if (download) {
  330. ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
  331. staging, 0, 0, 0, 0,
  332. texture, index, NULL);
  333. hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
  334. staging, 0, D3D11_MAP_READ, 0, &map);
  335. if (FAILED(hr))
  336. goto map_failed;
  337. fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
  338. av_image_copy(dst->data, dst->linesize, map_data, map_linesize,
  339. ctx->sw_format, w, h);
  340. ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
  341. } else {
  342. hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
  343. staging, 0, D3D11_MAP_WRITE, 0, &map);
  344. if (FAILED(hr))
  345. goto map_failed;
  346. fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
  347. av_image_copy(map_data, map_linesize, src->data, src->linesize,
  348. ctx->sw_format, w, h);
  349. ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
  350. ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
  351. texture, index, 0, 0, 0,
  352. staging, 0, NULL);
  353. }
  354. device_hwctx->unlock(device_hwctx->lock_ctx);
  355. return 0;
  356. map_failed:
  357. av_log(ctx, AV_LOG_ERROR, "Unable to lock D3D11VA surface (%lx)\n", (long)hr);
  358. device_hwctx->unlock(device_hwctx->lock_ctx);
  359. return AVERROR_UNKNOWN;
  360. }
  361. static int d3d11va_device_init(AVHWDeviceContext *hwdev)
  362. {
  363. AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
  364. HRESULT hr;
  365. if (!device_hwctx->lock) {
  366. device_hwctx->lock_ctx = CreateMutex(NULL, 0, NULL);
  367. if (device_hwctx->lock_ctx == INVALID_HANDLE_VALUE) {
  368. av_log(NULL, AV_LOG_ERROR, "Failed to create a mutex\n");
  369. return AVERROR(EINVAL);
  370. }
  371. device_hwctx->lock = d3d11va_default_lock;
  372. device_hwctx->unlock = d3d11va_default_unlock;
  373. }
  374. if (!device_hwctx->device_context) {
  375. ID3D11Device_GetImmediateContext(device_hwctx->device, &device_hwctx->device_context);
  376. if (!device_hwctx->device_context)
  377. return AVERROR_UNKNOWN;
  378. }
  379. if (!device_hwctx->video_device) {
  380. hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device, &IID_ID3D11VideoDevice,
  381. (void **)&device_hwctx->video_device);
  382. if (FAILED(hr))
  383. return AVERROR_UNKNOWN;
  384. }
  385. if (!device_hwctx->video_context) {
  386. hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device_context, &IID_ID3D11VideoContext,
  387. (void **)&device_hwctx->video_context);
  388. if (FAILED(hr))
  389. return AVERROR_UNKNOWN;
  390. }
  391. return 0;
  392. }
  393. static void d3d11va_device_uninit(AVHWDeviceContext *hwdev)
  394. {
  395. AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
  396. if (device_hwctx->device) {
  397. ID3D11Device_Release(device_hwctx->device);
  398. device_hwctx->device = NULL;
  399. }
  400. if (device_hwctx->device_context) {
  401. ID3D11DeviceContext_Release(device_hwctx->device_context);
  402. device_hwctx->device_context = NULL;
  403. }
  404. if (device_hwctx->video_device) {
  405. ID3D11VideoDevice_Release(device_hwctx->video_device);
  406. device_hwctx->video_device = NULL;
  407. }
  408. if (device_hwctx->video_context) {
  409. ID3D11VideoContext_Release(device_hwctx->video_context);
  410. device_hwctx->video_context = NULL;
  411. }
  412. if (device_hwctx->lock == d3d11va_default_lock) {
  413. CloseHandle(device_hwctx->lock_ctx);
  414. device_hwctx->lock_ctx = INVALID_HANDLE_VALUE;
  415. device_hwctx->lock = NULL;
  416. }
  417. }
  418. static int d3d11va_device_create(AVHWDeviceContext *ctx, const char *device,
  419. AVDictionary *opts, int flags)
  420. {
  421. AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
  422. HRESULT hr;
  423. IDXGIAdapter *pAdapter = NULL;
  424. ID3D10Multithread *pMultithread;
  425. UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
  426. int is_debug = !!av_dict_get(opts, "debug", NULL, 0);
  427. int ret;
  428. // (On UWP we can't check this.)
  429. #if !HAVE_UWP
  430. if (!LoadLibrary("d3d11_1sdklayers.dll"))
  431. is_debug = 0;
  432. #endif
  433. if (is_debug)
  434. creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  435. if ((ret = ff_thread_once(&functions_loaded, load_functions)) != 0)
  436. return AVERROR_UNKNOWN;
  437. if (!mD3D11CreateDevice || !mCreateDXGIFactory) {
  438. av_log(ctx, AV_LOG_ERROR, "Failed to load D3D11 library or its functions\n");
  439. return AVERROR_UNKNOWN;
  440. }
  441. if (device) {
  442. IDXGIFactory2 *pDXGIFactory;
  443. hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&pDXGIFactory);
  444. if (SUCCEEDED(hr)) {
  445. int adapter = atoi(device);
  446. if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
  447. pAdapter = NULL;
  448. IDXGIFactory2_Release(pDXGIFactory);
  449. }
  450. }
  451. if (pAdapter) {
  452. DXGI_ADAPTER_DESC desc;
  453. hr = IDXGIAdapter2_GetDesc(pAdapter, &desc);
  454. if (!FAILED(hr)) {
  455. av_log(ctx, AV_LOG_INFO, "Using device %04x:%04x (%ls).\n",
  456. desc.VendorId, desc.DeviceId, desc.Description);
  457. }
  458. }
  459. hr = mD3D11CreateDevice(pAdapter, pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, NULL, 0,
  460. D3D11_SDK_VERSION, &device_hwctx->device, NULL, NULL);
  461. if (pAdapter)
  462. IDXGIAdapter_Release(pAdapter);
  463. if (FAILED(hr)) {
  464. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device (%lx)\n", (long)hr);
  465. return AVERROR_UNKNOWN;
  466. }
  467. hr = ID3D11Device_QueryInterface(device_hwctx->device, &IID_ID3D10Multithread, (void **)&pMultithread);
  468. if (SUCCEEDED(hr)) {
  469. ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
  470. ID3D10Multithread_Release(pMultithread);
  471. }
  472. #if !HAVE_UWP && HAVE_DXGIDEBUG_H
  473. if (is_debug) {
  474. HANDLE dxgidebug_dll = LoadLibrary("dxgidebug.dll");
  475. if (dxgidebug_dll) {
  476. HRESULT (WINAPI * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug)
  477. = (void *)GetProcAddress(dxgidebug_dll, "DXGIGetDebugInterface");
  478. if (pf_DXGIGetDebugInterface) {
  479. IDXGIDebug *dxgi_debug = NULL;
  480. hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&dxgi_debug);
  481. if (SUCCEEDED(hr) && dxgi_debug)
  482. IDXGIDebug_ReportLiveObjects(dxgi_debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
  483. }
  484. }
  485. }
  486. #endif
  487. return 0;
  488. }
  489. const HWContextType ff_hwcontext_type_d3d11va = {
  490. .type = AV_HWDEVICE_TYPE_D3D11VA,
  491. .name = "D3D11VA",
  492. .device_hwctx_size = sizeof(AVD3D11VADeviceContext),
  493. .frames_hwctx_size = sizeof(AVD3D11VAFramesContext),
  494. .frames_priv_size = sizeof(D3D11VAFramesContext),
  495. .device_create = d3d11va_device_create,
  496. .device_init = d3d11va_device_init,
  497. .device_uninit = d3d11va_device_uninit,
  498. .frames_get_constraints = d3d11va_frames_get_constraints,
  499. .frames_init = d3d11va_frames_init,
  500. .frames_uninit = d3d11va_frames_uninit,
  501. .frames_get_buffer = d3d11va_get_buffer,
  502. .transfer_get_formats = d3d11va_transfer_get_formats,
  503. .transfer_data_to = d3d11va_transfer_data,
  504. .transfer_data_from = d3d11va_transfer_data,
  505. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_D3D11, AV_PIX_FMT_NONE },
  506. };