2 * This file is part of Libav.
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.
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.
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
21 // Include thread.h before redefining _WIN32_WINNT, to get
22 // the right implementation for AVOnce
25 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
27 #define _WIN32_WINNT 0x0600
37 #include "hwcontext.h"
38 #include "hwcontext_d3d11va.h"
39 #include "hwcontext_internal.h"
44 typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
46 static AVOnce functions_loaded = AV_ONCE_INIT;
48 static PFN_CREATE_DXGI_FACTORY mCreateDXGIFactory;
49 static PFN_D3D11_CREATE_DEVICE mD3D11CreateDevice;
51 static av_cold void load_functions(void)
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;
59 d3dlib = LoadLibrary("d3d11.dll");
60 dxgilib = LoadLibrary("dxgi.dll");
61 if (!d3dlib || !dxgilib)
64 mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) GetProcAddress(d3dlib, "D3D11CreateDevice");
65 mCreateDXGIFactory = (PFN_CREATE_DXGI_FACTORY) GetProcAddress(dxgilib, "CreateDXGIFactory");
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;
74 typedef struct D3D11VAFramesContext {
79 ID3D11Texture2D *staging_texture;
80 } D3D11VAFramesContext;
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 },
90 static void d3d11va_default_lock(void *ctx)
92 WaitForSingleObjectEx(ctx, INFINITE, FALSE);
95 static void d3d11va_default_unlock(void *ctx)
100 static void d3d11va_frames_uninit(AVHWFramesContext *ctx)
102 AVD3D11VAFramesContext *frames_hwctx = ctx->hwctx;
103 D3D11VAFramesContext *s = ctx->internal->priv;
105 if (frames_hwctx->texture)
106 ID3D11Texture2D_Release(frames_hwctx->texture);
107 frames_hwctx->texture = NULL;
109 if (s->staging_texture)
110 ID3D11Texture2D_Release(s->staging_texture);
111 s->staging_texture = NULL;
114 static void free_texture(void *opaque, uint8_t *data)
116 ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
119 static AVBufferRef *wrap_texture_buf(ID3D11Texture2D *tex, int index)
122 AVD3D11FrameDescriptor *desc = av_mallocz(sizeof(*desc));
124 ID3D11Texture2D_Release(tex);
131 buf = av_buffer_create((uint8_t *)desc, sizeof(desc), free_texture, tex, 0);
133 ID3D11Texture2D_Release(tex);
141 static AVBufferRef *d3d11va_alloc_single(AVHWFramesContext *ctx)
143 D3D11VAFramesContext *s = ctx->internal->priv;
144 AVD3D11VAFramesContext *hwctx = ctx->hwctx;
145 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
147 ID3D11Texture2D *tex;
148 D3D11_TEXTURE2D_DESC texDesc = {
150 .Height = ctx->height,
153 .SampleDesc = { .Count = 1 },
155 .Usage = D3D11_USAGE_DEFAULT,
156 .BindFlags = hwctx->BindFlags,
157 .MiscFlags = hwctx->MiscFlags,
160 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &tex);
162 av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
166 return wrap_texture_buf(tex, 0);
169 static AVBufferRef *d3d11va_pool_alloc(void *opaque, int size)
171 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
172 D3D11VAFramesContext *s = ctx->internal->priv;
173 AVD3D11VAFramesContext *hwctx = ctx->hwctx;
174 D3D11_TEXTURE2D_DESC texDesc;
177 return d3d11va_alloc_single(ctx);
179 ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc);
181 if (s->nb_surfaces_used >= texDesc.ArraySize) {
182 av_log(ctx, AV_LOG_ERROR, "Static surface pool size exceeded.\n");
186 ID3D11Texture2D_AddRef(hwctx->texture);
187 return wrap_texture_buf(hwctx->texture, s->nb_surfaces_used++);
190 static int d3d11va_frames_init(AVHWFramesContext *ctx)
192 AVD3D11VAFramesContext *hwctx = ctx->hwctx;
193 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
194 D3D11VAFramesContext *s = ctx->internal->priv;
198 D3D11_TEXTURE2D_DESC texDesc;
200 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
201 if (ctx->sw_format == supported_formats[i].pix_fmt) {
202 s->format = supported_formats[i].d3d_format;
206 if (i == FF_ARRAY_ELEMS(supported_formats)) {
207 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
208 av_get_pix_fmt_name(ctx->sw_format));
209 return AVERROR(EINVAL);
212 texDesc = (D3D11_TEXTURE2D_DESC){
214 .Height = ctx->height,
217 .SampleDesc = { .Count = 1 },
218 .ArraySize = ctx->initial_pool_size,
219 .Usage = D3D11_USAGE_DEFAULT,
220 .BindFlags = hwctx->BindFlags,
221 .MiscFlags = hwctx->MiscFlags,
224 if (hwctx->texture) {
225 D3D11_TEXTURE2D_DESC texDesc2;
226 ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc2);
228 if (texDesc.Width != texDesc2.Width ||
229 texDesc.Height != texDesc2.Height ||
230 texDesc.Format != texDesc2.Format) {
231 av_log(ctx, AV_LOG_ERROR, "User-provided texture has mismatching parameters\n");
232 return AVERROR(EINVAL);
234 } else if (texDesc.ArraySize > 0) {
235 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &hwctx->texture);
237 av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
238 return AVERROR_UNKNOWN;
242 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(AVD3D11FrameDescriptor),
243 ctx, d3d11va_pool_alloc, NULL);
244 if (!ctx->internal->pool_internal)
245 return AVERROR(ENOMEM);
250 static int d3d11va_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
252 AVD3D11FrameDescriptor *desc;
254 frame->buf[0] = av_buffer_pool_get(ctx->pool);
256 return AVERROR(ENOMEM);
258 desc = (AVD3D11FrameDescriptor *)frame->buf[0]->data;
260 frame->data[0] = (uint8_t *)desc->texture;
261 frame->data[1] = (uint8_t *)desc->index;
262 frame->format = AV_PIX_FMT_D3D11;
263 frame->width = ctx->width;
264 frame->height = ctx->height;
269 static int d3d11va_transfer_get_formats(AVHWFramesContext *ctx,
270 enum AVHWFrameTransferDirection dir,
271 enum AVPixelFormat **formats)
273 enum AVPixelFormat *fmts;
275 fmts = av_malloc_array(2, sizeof(*fmts));
277 return AVERROR(ENOMEM);
279 fmts[0] = ctx->sw_format;
280 fmts[1] = AV_PIX_FMT_NONE;
287 static int d3d11va_create_staging_texture(AVHWFramesContext *ctx)
289 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
290 D3D11VAFramesContext *s = ctx->internal->priv;
292 D3D11_TEXTURE2D_DESC texDesc = {
294 .Height = ctx->height,
297 .SampleDesc = { .Count = 1 },
299 .Usage = D3D11_USAGE_STAGING,
300 .CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE,
303 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &s->staging_texture);
305 av_log(ctx, AV_LOG_ERROR, "Could not create the staging texture (%lx)\n", (long)hr);
306 return AVERROR_UNKNOWN;
312 static void fill_texture_ptrs(uint8_t *data[4], int linesize[4],
313 AVHWFramesContext *ctx,
314 D3D11_TEXTURE2D_DESC *desc,
315 D3D11_MAPPED_SUBRESOURCE *map)
319 for (i = 0; i < 4; i++)
320 linesize[i] = map->RowPitch;
322 av_image_fill_pointers(data, ctx->sw_format, desc->Height,
323 (uint8_t*)map->pData, linesize);
326 static int d3d11va_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
329 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
330 D3D11VAFramesContext *s = ctx->internal->priv;
331 int download = src->format == AV_PIX_FMT_D3D11;
332 const AVFrame *frame = download ? src : dst;
333 const AVFrame *other = download ? dst : src;
334 // (The interface types are compatible.)
335 ID3D11Resource *texture = (ID3D11Resource *)(ID3D11Texture2D *)frame->data[0];
336 int index = (intptr_t)frame->data[1];
337 ID3D11Resource *staging;
338 int w = FFMIN(dst->width, src->width);
339 int h = FFMIN(dst->height, src->height);
340 uint8_t *map_data[4];
342 D3D11_TEXTURE2D_DESC desc;
343 D3D11_MAPPED_SUBRESOURCE map;
346 if (frame->hw_frames_ctx->data != (uint8_t *)ctx || other->format != ctx->sw_format)
347 return AVERROR(EINVAL);
349 device_hwctx->lock(device_hwctx->lock_ctx);
351 if (!s->staging_texture) {
352 int res = d3d11va_create_staging_texture(ctx);
357 staging = (ID3D11Resource *)s->staging_texture;
359 ID3D11Texture2D_GetDesc(s->staging_texture, &desc);
362 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
364 texture, index, NULL);
366 hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
367 staging, 0, D3D11_MAP_READ, 0, &map);
371 fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
373 av_image_copy(dst->data, dst->linesize, map_data, map_linesize,
374 ctx->sw_format, w, h);
376 ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
378 hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
379 staging, 0, D3D11_MAP_WRITE, 0, &map);
383 fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
385 av_image_copy(map_data, map_linesize, src->data, src->linesize,
386 ctx->sw_format, w, h);
388 ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
390 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
391 texture, index, 0, 0, 0,
395 device_hwctx->unlock(device_hwctx->lock_ctx);
399 av_log(ctx, AV_LOG_ERROR, "Unable to lock D3D11VA surface (%lx)\n", (long)hr);
400 device_hwctx->unlock(device_hwctx->lock_ctx);
401 return AVERROR_UNKNOWN;
404 static int d3d11va_device_init(AVHWDeviceContext *hwdev)
406 AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
409 if (!device_hwctx->lock) {
410 device_hwctx->lock_ctx = CreateMutex(NULL, 0, NULL);
411 if (device_hwctx->lock_ctx == INVALID_HANDLE_VALUE) {
412 av_log(NULL, AV_LOG_ERROR, "Failed to create a mutex\n");
413 return AVERROR(EINVAL);
415 device_hwctx->lock = d3d11va_default_lock;
416 device_hwctx->unlock = d3d11va_default_unlock;
419 if (!device_hwctx->device_context) {
420 ID3D11Device_GetImmediateContext(device_hwctx->device, &device_hwctx->device_context);
421 if (!device_hwctx->device_context)
422 return AVERROR_UNKNOWN;
425 if (!device_hwctx->video_device) {
426 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device, &IID_ID3D11VideoDevice,
427 (void **)&device_hwctx->video_device);
429 return AVERROR_UNKNOWN;
432 if (!device_hwctx->video_context) {
433 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device_context, &IID_ID3D11VideoContext,
434 (void **)&device_hwctx->video_context);
436 return AVERROR_UNKNOWN;
442 static void d3d11va_device_uninit(AVHWDeviceContext *hwdev)
444 AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
446 if (device_hwctx->device)
447 ID3D11Device_Release(device_hwctx->device);
449 if (device_hwctx->device_context)
450 ID3D11DeviceContext_Release(device_hwctx->device_context);
452 if (device_hwctx->video_device)
453 ID3D11VideoDevice_Release(device_hwctx->video_device);
455 if (device_hwctx->video_context)
456 ID3D11VideoContext_Release(device_hwctx->video_context);
458 if (device_hwctx->lock == d3d11va_default_lock)
459 CloseHandle(device_hwctx->lock_ctx);
462 static int d3d11va_device_create(AVHWDeviceContext *ctx, const char *device,
463 AVDictionary *opts, int flags)
465 AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
468 IDXGIAdapter *pAdapter = NULL;
469 ID3D10Multithread *pMultithread;
470 UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
473 if ((ret = ff_thread_once(&functions_loaded, load_functions)) != 0)
474 return AVERROR_UNKNOWN;
475 if (!mD3D11CreateDevice || !mCreateDXGIFactory) {
476 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D11 library or its functions\n");
477 return AVERROR_UNKNOWN;
481 IDXGIFactory2 *pDXGIFactory;
482 hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&pDXGIFactory);
484 int adapter = atoi(device);
485 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
487 IDXGIFactory2_Release(pDXGIFactory);
491 hr = mD3D11CreateDevice(pAdapter, pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, NULL, 0,
492 D3D11_SDK_VERSION, &device_hwctx->device, NULL, NULL);
494 IDXGIAdapter_Release(pAdapter);
496 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device (%lx)\n", (long)hr);
497 return AVERROR_UNKNOWN;
500 hr = ID3D11Device_QueryInterface(device_hwctx->device, &IID_ID3D10Multithread, (void **)&pMultithread);
502 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
503 ID3D10Multithread_Release(pMultithread);
509 const HWContextType ff_hwcontext_type_d3d11va = {
510 .type = AV_HWDEVICE_TYPE_D3D11VA,
513 .device_hwctx_size = sizeof(AVD3D11VADeviceContext),
514 .frames_hwctx_size = sizeof(AVD3D11VAFramesContext),
515 .frames_priv_size = sizeof(D3D11VAFramesContext),
517 .device_create = d3d11va_device_create,
518 .device_init = d3d11va_device_init,
519 .device_uninit = d3d11va_device_uninit,
520 .frames_init = d3d11va_frames_init,
521 .frames_uninit = d3d11va_frames_uninit,
522 .frames_get_buffer = d3d11va_get_buffer,
523 .transfer_get_formats = d3d11va_transfer_get_formats,
524 .transfer_data_to = d3d11va_transfer_data,
525 .transfer_data_from = d3d11va_transfer_data,
527 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_D3D11, AV_PIX_FMT_NONE },