You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
18KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 void free_texture(void *opaque, uint8_t *data)
  95. {
  96. ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
  97. }
  98. static AVBufferRef *wrap_texture_buf(ID3D11Texture2D *tex, int index)
  99. {
  100. AVBufferRef *buf;
  101. AVD3D11FrameDescriptor *desc = av_mallocz(sizeof(*desc));
  102. if (!desc) {
  103. ID3D11Texture2D_Release(tex);
  104. return NULL;
  105. }
  106. desc->texture = tex;
  107. desc->index = index;
  108. buf = av_buffer_create((uint8_t *)desc, sizeof(desc), free_texture, tex, 0);
  109. if (!buf) {
  110. ID3D11Texture2D_Release(tex);
  111. av_free(desc);
  112. return NULL;
  113. }
  114. return buf;
  115. }
  116. static AVBufferRef *d3d11va_alloc_single(AVHWFramesContext *ctx)
  117. {
  118. D3D11VAFramesContext *s = ctx->internal->priv;
  119. AVD3D11VAFramesContext *hwctx = ctx->hwctx;
  120. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  121. HRESULT hr;
  122. ID3D11Texture2D *tex;
  123. D3D11_TEXTURE2D_DESC texDesc = {
  124. .Width = ctx->width,
  125. .Height = ctx->height,
  126. .MipLevels = 1,
  127. .Format = s->format,
  128. .SampleDesc = { .Count = 1 },
  129. .ArraySize = 1,
  130. .Usage = D3D11_USAGE_DEFAULT,
  131. .BindFlags = hwctx->BindFlags,
  132. .MiscFlags = hwctx->MiscFlags,
  133. };
  134. hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &tex);
  135. if (FAILED(hr)) {
  136. av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
  137. return NULL;
  138. }
  139. return wrap_texture_buf(tex, 0);
  140. }
  141. static AVBufferRef *d3d11va_pool_alloc(void *opaque, int size)
  142. {
  143. AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
  144. D3D11VAFramesContext *s = ctx->internal->priv;
  145. AVD3D11VAFramesContext *hwctx = ctx->hwctx;
  146. D3D11_TEXTURE2D_DESC texDesc;
  147. if (!hwctx->texture)
  148. return d3d11va_alloc_single(ctx);
  149. ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc);
  150. if (s->nb_surfaces_used >= texDesc.ArraySize) {
  151. av_log(ctx, AV_LOG_ERROR, "Static surface pool size exceeded.\n");
  152. return NULL;
  153. }
  154. ID3D11Texture2D_AddRef(hwctx->texture);
  155. return wrap_texture_buf(hwctx->texture, s->nb_surfaces_used++);
  156. }
  157. static int d3d11va_frames_init(AVHWFramesContext *ctx)
  158. {
  159. AVD3D11VAFramesContext *hwctx = ctx->hwctx;
  160. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  161. D3D11VAFramesContext *s = ctx->internal->priv;
  162. int i;
  163. HRESULT hr;
  164. D3D11_TEXTURE2D_DESC texDesc;
  165. for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
  166. if (ctx->sw_format == supported_formats[i].pix_fmt) {
  167. s->format = supported_formats[i].d3d_format;
  168. break;
  169. }
  170. }
  171. if (i == FF_ARRAY_ELEMS(supported_formats)) {
  172. av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
  173. av_get_pix_fmt_name(ctx->sw_format));
  174. return AVERROR(EINVAL);
  175. }
  176. texDesc = (D3D11_TEXTURE2D_DESC){
  177. .Width = ctx->width,
  178. .Height = ctx->height,
  179. .MipLevels = 1,
  180. .Format = s->format,
  181. .SampleDesc = { .Count = 1 },
  182. .ArraySize = ctx->initial_pool_size,
  183. .Usage = D3D11_USAGE_DEFAULT,
  184. .BindFlags = hwctx->BindFlags,
  185. .MiscFlags = hwctx->MiscFlags,
  186. };
  187. if (hwctx->texture) {
  188. D3D11_TEXTURE2D_DESC texDesc2;
  189. ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc2);
  190. if (texDesc.Width != texDesc2.Width ||
  191. texDesc.Height != texDesc2.Height ||
  192. texDesc.Format != texDesc2.Format) {
  193. av_log(ctx, AV_LOG_ERROR, "User-provided texture has mismatching parameters\n");
  194. return AVERROR(EINVAL);
  195. }
  196. } else if (texDesc.ArraySize > 0) {
  197. hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &hwctx->texture);
  198. if (FAILED(hr)) {
  199. av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
  200. return AVERROR_UNKNOWN;
  201. }
  202. }
  203. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(AVD3D11FrameDescriptor),
  204. ctx, d3d11va_pool_alloc, NULL);
  205. if (!ctx->internal->pool_internal)
  206. return AVERROR(ENOMEM);
  207. return 0;
  208. }
  209. static int d3d11va_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  210. {
  211. AVD3D11FrameDescriptor *desc;
  212. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  213. if (!frame->buf[0])
  214. return AVERROR(ENOMEM);
  215. desc = (AVD3D11FrameDescriptor *)frame->buf[0]->data;
  216. frame->data[0] = (uint8_t *)desc->texture;
  217. frame->data[1] = (uint8_t *)desc->index;
  218. frame->format = AV_PIX_FMT_D3D11;
  219. frame->width = ctx->width;
  220. frame->height = ctx->height;
  221. return 0;
  222. }
  223. static int d3d11va_transfer_get_formats(AVHWFramesContext *ctx,
  224. enum AVHWFrameTransferDirection dir,
  225. enum AVPixelFormat **formats)
  226. {
  227. D3D11VAFramesContext *s = ctx->internal->priv;
  228. enum AVPixelFormat *fmts;
  229. fmts = av_malloc_array(2, sizeof(*fmts));
  230. if (!fmts)
  231. return AVERROR(ENOMEM);
  232. fmts[0] = ctx->sw_format;
  233. fmts[1] = AV_PIX_FMT_NONE;
  234. // Don't signal support for opaque formats. Actual access would fail.
  235. if (s->format == DXGI_FORMAT_420_OPAQUE)
  236. fmts[0] = AV_PIX_FMT_NONE;
  237. *formats = fmts;
  238. return 0;
  239. }
  240. static int d3d11va_create_staging_texture(AVHWFramesContext *ctx)
  241. {
  242. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  243. D3D11VAFramesContext *s = ctx->internal->priv;
  244. HRESULT hr;
  245. D3D11_TEXTURE2D_DESC texDesc = {
  246. .Width = ctx->width,
  247. .Height = ctx->height,
  248. .MipLevels = 1,
  249. .Format = s->format,
  250. .SampleDesc = { .Count = 1 },
  251. .ArraySize = 1,
  252. .Usage = D3D11_USAGE_STAGING,
  253. .CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE,
  254. };
  255. hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &s->staging_texture);
  256. if (FAILED(hr)) {
  257. av_log(ctx, AV_LOG_ERROR, "Could not create the staging texture (%lx)\n", (long)hr);
  258. return AVERROR_UNKNOWN;
  259. }
  260. return 0;
  261. }
  262. static void fill_texture_ptrs(uint8_t *data[4], int linesize[4],
  263. AVHWFramesContext *ctx,
  264. D3D11_TEXTURE2D_DESC *desc,
  265. D3D11_MAPPED_SUBRESOURCE *map)
  266. {
  267. int i;
  268. for (i = 0; i < 4; i++)
  269. linesize[i] = map->RowPitch;
  270. av_image_fill_pointers(data, ctx->sw_format, desc->Height,
  271. (uint8_t*)map->pData, linesize);
  272. }
  273. static int d3d11va_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
  274. const AVFrame *src)
  275. {
  276. AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  277. D3D11VAFramesContext *s = ctx->internal->priv;
  278. int download = src->format == AV_PIX_FMT_D3D11;
  279. const AVFrame *frame = download ? src : dst;
  280. const AVFrame *other = download ? dst : src;
  281. // (The interface types are compatible.)
  282. ID3D11Resource *texture = (ID3D11Resource *)(ID3D11Texture2D *)frame->data[0];
  283. int index = (intptr_t)frame->data[1];
  284. ID3D11Resource *staging;
  285. int w = FFMIN(dst->width, src->width);
  286. int h = FFMIN(dst->height, src->height);
  287. uint8_t *map_data[4];
  288. int map_linesize[4];
  289. D3D11_TEXTURE2D_DESC desc;
  290. D3D11_MAPPED_SUBRESOURCE map;
  291. HRESULT hr;
  292. if (frame->hw_frames_ctx->data != (uint8_t *)ctx || other->format != ctx->sw_format)
  293. return AVERROR(EINVAL);
  294. device_hwctx->lock(device_hwctx->lock_ctx);
  295. if (!s->staging_texture) {
  296. int res = d3d11va_create_staging_texture(ctx);
  297. if (res < 0)
  298. return res;
  299. }
  300. staging = (ID3D11Resource *)s->staging_texture;
  301. ID3D11Texture2D_GetDesc(s->staging_texture, &desc);
  302. if (download) {
  303. ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
  304. staging, 0, 0, 0, 0,
  305. texture, index, NULL);
  306. hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
  307. staging, 0, D3D11_MAP_READ, 0, &map);
  308. if (FAILED(hr))
  309. goto map_failed;
  310. fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
  311. av_image_copy(dst->data, dst->linesize, map_data, map_linesize,
  312. ctx->sw_format, w, h);
  313. ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
  314. } else {
  315. hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
  316. staging, 0, D3D11_MAP_WRITE, 0, &map);
  317. if (FAILED(hr))
  318. goto map_failed;
  319. fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
  320. av_image_copy(map_data, map_linesize, src->data, src->linesize,
  321. ctx->sw_format, w, h);
  322. ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
  323. ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
  324. texture, index, 0, 0, 0,
  325. staging, 0, NULL);
  326. }
  327. device_hwctx->unlock(device_hwctx->lock_ctx);
  328. return 0;
  329. map_failed:
  330. av_log(ctx, AV_LOG_ERROR, "Unable to lock D3D11VA surface (%lx)\n", (long)hr);
  331. device_hwctx->unlock(device_hwctx->lock_ctx);
  332. return AVERROR_UNKNOWN;
  333. }
  334. static int d3d11va_device_init(AVHWDeviceContext *hwdev)
  335. {
  336. AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
  337. HRESULT hr;
  338. if (!device_hwctx->lock) {
  339. device_hwctx->lock_ctx = CreateMutex(NULL, 0, NULL);
  340. if (device_hwctx->lock_ctx == INVALID_HANDLE_VALUE) {
  341. av_log(NULL, AV_LOG_ERROR, "Failed to create a mutex\n");
  342. return AVERROR(EINVAL);
  343. }
  344. device_hwctx->lock = d3d11va_default_lock;
  345. device_hwctx->unlock = d3d11va_default_unlock;
  346. }
  347. if (!device_hwctx->device_context) {
  348. ID3D11Device_GetImmediateContext(device_hwctx->device, &device_hwctx->device_context);
  349. if (!device_hwctx->device_context)
  350. return AVERROR_UNKNOWN;
  351. }
  352. if (!device_hwctx->video_device) {
  353. hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device, &IID_ID3D11VideoDevice,
  354. (void **)&device_hwctx->video_device);
  355. if (FAILED(hr))
  356. return AVERROR_UNKNOWN;
  357. }
  358. if (!device_hwctx->video_context) {
  359. hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device_context, &IID_ID3D11VideoContext,
  360. (void **)&device_hwctx->video_context);
  361. if (FAILED(hr))
  362. return AVERROR_UNKNOWN;
  363. }
  364. return 0;
  365. }
  366. static void d3d11va_device_uninit(AVHWDeviceContext *hwdev)
  367. {
  368. AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
  369. if (device_hwctx->device)
  370. ID3D11Device_Release(device_hwctx->device);
  371. if (device_hwctx->device_context)
  372. ID3D11DeviceContext_Release(device_hwctx->device_context);
  373. if (device_hwctx->video_device)
  374. ID3D11VideoDevice_Release(device_hwctx->video_device);
  375. if (device_hwctx->video_context)
  376. ID3D11VideoContext_Release(device_hwctx->video_context);
  377. if (device_hwctx->lock == d3d11va_default_lock)
  378. CloseHandle(device_hwctx->lock_ctx);
  379. }
  380. static int d3d11va_device_create(AVHWDeviceContext *ctx, const char *device,
  381. AVDictionary *opts, int flags)
  382. {
  383. AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
  384. HRESULT hr;
  385. IDXGIAdapter *pAdapter = NULL;
  386. ID3D10Multithread *pMultithread;
  387. UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
  388. int is_debug = !!av_dict_get(opts, "debug", NULL, 0);
  389. int ret;
  390. // (On UWP we can't check this.)
  391. #if !HAVE_UWP
  392. if (!LoadLibrary("d3d11_1sdklayers.dll"))
  393. is_debug = 0;
  394. #endif
  395. if (is_debug)
  396. creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  397. if ((ret = ff_thread_once(&functions_loaded, load_functions)) != 0)
  398. return AVERROR_UNKNOWN;
  399. if (!mD3D11CreateDevice || !mCreateDXGIFactory) {
  400. av_log(ctx, AV_LOG_ERROR, "Failed to load D3D11 library or its functions\n");
  401. return AVERROR_UNKNOWN;
  402. }
  403. if (device) {
  404. IDXGIFactory2 *pDXGIFactory;
  405. hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&pDXGIFactory);
  406. if (SUCCEEDED(hr)) {
  407. int adapter = atoi(device);
  408. if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
  409. pAdapter = NULL;
  410. IDXGIFactory2_Release(pDXGIFactory);
  411. }
  412. }
  413. hr = mD3D11CreateDevice(pAdapter, pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, NULL, 0,
  414. D3D11_SDK_VERSION, &device_hwctx->device, NULL, NULL);
  415. if (pAdapter)
  416. IDXGIAdapter_Release(pAdapter);
  417. if (FAILED(hr)) {
  418. av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device (%lx)\n", (long)hr);
  419. return AVERROR_UNKNOWN;
  420. }
  421. hr = ID3D11Device_QueryInterface(device_hwctx->device, &IID_ID3D10Multithread, (void **)&pMultithread);
  422. if (SUCCEEDED(hr)) {
  423. ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
  424. ID3D10Multithread_Release(pMultithread);
  425. }
  426. #if !HAVE_UWP && HAVE_DXGIDEBUG_H
  427. if (is_debug) {
  428. HANDLE dxgidebug_dll = LoadLibrary("dxgidebug.dll");
  429. if (dxgidebug_dll) {
  430. HRESULT (WINAPI * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug)
  431. = (void *)GetProcAddress(dxgidebug_dll, "DXGIGetDebugInterface");
  432. if (pf_DXGIGetDebugInterface) {
  433. IDXGIDebug *dxgi_debug = NULL;
  434. hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&dxgi_debug);
  435. if (SUCCEEDED(hr) && dxgi_debug)
  436. IDXGIDebug_ReportLiveObjects(dxgi_debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
  437. }
  438. }
  439. }
  440. #endif
  441. return 0;
  442. }
  443. const HWContextType ff_hwcontext_type_d3d11va = {
  444. .type = AV_HWDEVICE_TYPE_D3D11VA,
  445. .name = "D3D11VA",
  446. .device_hwctx_size = sizeof(AVD3D11VADeviceContext),
  447. .frames_hwctx_size = sizeof(AVD3D11VAFramesContext),
  448. .frames_priv_size = sizeof(D3D11VAFramesContext),
  449. .device_create = d3d11va_device_create,
  450. .device_init = d3d11va_device_init,
  451. .device_uninit = d3d11va_device_uninit,
  452. .frames_init = d3d11va_frames_init,
  453. .frames_uninit = d3d11va_frames_uninit,
  454. .frames_get_buffer = d3d11va_get_buffer,
  455. .transfer_get_formats = d3d11va_transfer_get_formats,
  456. .transfer_data_to = d3d11va_transfer_data,
  457. .transfer_data_from = d3d11va_transfer_data,
  458. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_D3D11, AV_PIX_FMT_NONE },
  459. };