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