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