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