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.

570 lines
18KB

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