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);
108 if (s->staging_texture)
109 ID3D11Texture2D_Release(s->staging_texture);
112 static void free_texture(void *opaque, uint8_t *data)
114 ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
117 static AVBufferRef *wrap_texture_buf(ID3D11Texture2D *tex, int index)
120 AVD3D11FrameDescriptor *desc = av_mallocz(sizeof(*desc));
122 ID3D11Texture2D_Release(tex);
129 buf = av_buffer_create((uint8_t *)desc, sizeof(desc), free_texture, tex, 0);
131 ID3D11Texture2D_Release(tex);
139 static AVBufferRef *d3d11va_alloc_single(AVHWFramesContext *ctx)
141 D3D11VAFramesContext *s = ctx->internal->priv;
142 AVD3D11VAFramesContext *hwctx = ctx->hwctx;
143 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
145 ID3D11Texture2D *tex;
146 D3D11_TEXTURE2D_DESC texDesc = {
148 .Height = ctx->height,
151 .SampleDesc = { .Count = 1 },
153 .Usage = D3D11_USAGE_DEFAULT,
154 .BindFlags = hwctx->BindFlags,
155 .MiscFlags = hwctx->MiscFlags,
158 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &tex);
160 av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
164 return wrap_texture_buf(tex, 0);
167 static AVBufferRef *d3d11va_pool_alloc(void *opaque, int size)
169 AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
170 D3D11VAFramesContext *s = ctx->internal->priv;
171 AVD3D11VAFramesContext *hwctx = ctx->hwctx;
172 D3D11_TEXTURE2D_DESC texDesc;
175 return d3d11va_alloc_single(ctx);
177 ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc);
179 if (s->nb_surfaces_used >= texDesc.ArraySize) {
180 av_log(ctx, AV_LOG_ERROR, "Static surface pool size exceeded.\n");
184 ID3D11Texture2D_AddRef(hwctx->texture);
185 return wrap_texture_buf(hwctx->texture, s->nb_surfaces_used++);
188 static int d3d11va_frames_init(AVHWFramesContext *ctx)
190 AVD3D11VAFramesContext *hwctx = ctx->hwctx;
191 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
192 D3D11VAFramesContext *s = ctx->internal->priv;
196 D3D11_TEXTURE2D_DESC texDesc;
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;
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);
210 texDesc = (D3D11_TEXTURE2D_DESC){
212 .Height = ctx->height,
215 .SampleDesc = { .Count = 1 },
216 .ArraySize = ctx->initial_pool_size,
217 .Usage = D3D11_USAGE_DEFAULT,
218 .BindFlags = hwctx->BindFlags,
219 .MiscFlags = hwctx->MiscFlags,
222 if (hwctx->texture) {
223 D3D11_TEXTURE2D_DESC texDesc2;
224 ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc2);
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);
232 } else if (texDesc.ArraySize > 0) {
233 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &hwctx->texture);
235 av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
236 return AVERROR_UNKNOWN;
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);
247 av_log(ctx, AV_LOG_ERROR, "Could not create the staging texture (%lx)\n", (long)hr);
248 return AVERROR_UNKNOWN;
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);
259 static int d3d11va_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
261 AVD3D11FrameDescriptor *desc;
263 frame->buf[0] = av_buffer_pool_get(ctx->pool);
265 return AVERROR(ENOMEM);
267 desc = (AVD3D11FrameDescriptor *)frame->buf[0]->data;
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;
278 static int d3d11va_transfer_get_formats(AVHWFramesContext *ctx,
279 enum AVHWFrameTransferDirection dir,
280 enum AVPixelFormat **formats)
282 enum AVPixelFormat *fmts;
284 fmts = av_malloc_array(2, sizeof(*fmts));
286 return AVERROR(ENOMEM);
288 fmts[0] = ctx->sw_format;
289 fmts[1] = AV_PIX_FMT_NONE;
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)
303 for (i = 0; i < 4; i++)
304 linesize[i] = map->RowPitch;
306 av_image_fill_pointers(data, ctx->sw_format, desc->Height,
307 (uint8_t*)map->pData, linesize);
310 static int d3d11va_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
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];
326 D3D11_TEXTURE2D_DESC desc;
327 D3D11_MAPPED_SUBRESOURCE map;
330 if (frame->hw_frames_ctx->data != (uint8_t *)ctx || other->format != ctx->sw_format)
331 return AVERROR(EINVAL);
333 device_hwctx->lock(device_hwctx->lock_ctx);
335 ID3D11Texture2D_GetDesc(s->staging_texture, &desc);
338 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
340 texture, index, NULL);
342 hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
343 staging, 0, D3D11_MAP_READ, 0, &map);
347 fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
349 av_image_copy(dst->data, dst->linesize, map_data, map_linesize,
350 ctx->sw_format, w, h);
352 ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
354 hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
355 staging, 0, D3D11_MAP_WRITE, 0, &map);
359 fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
361 av_image_copy(map_data, map_linesize, src->data, src->linesize,
362 ctx->sw_format, w, h);
364 ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
366 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
367 texture, index, 0, 0, 0,
371 device_hwctx->unlock(device_hwctx->lock_ctx);
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;
380 static int d3d11va_device_init(AVHWDeviceContext *hwdev)
382 AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
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);
391 device_hwctx->lock = d3d11va_default_lock;
392 device_hwctx->unlock = d3d11va_default_unlock;
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;
401 if (!device_hwctx->video_device) {
402 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device, &IID_ID3D11VideoDevice,
403 (void **)&device_hwctx->video_device);
405 return AVERROR_UNKNOWN;
408 if (!device_hwctx->video_context) {
409 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device_context, &IID_ID3D11VideoContext,
410 (void **)&device_hwctx->video_context);
412 return AVERROR_UNKNOWN;
418 static void d3d11va_device_uninit(AVHWDeviceContext *hwdev)
420 AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
422 if (device_hwctx->device)
423 ID3D11Device_Release(device_hwctx->device);
425 if (device_hwctx->device_context)
426 ID3D11DeviceContext_Release(device_hwctx->device_context);
428 if (device_hwctx->video_device)
429 ID3D11VideoDevice_Release(device_hwctx->video_device);
431 if (device_hwctx->video_context)
432 ID3D11VideoContext_Release(device_hwctx->video_context);
434 if (device_hwctx->lock == d3d11va_default_lock)
435 CloseHandle(device_hwctx->lock_ctx);
438 static int d3d11va_device_create(AVHWDeviceContext *ctx, const char *device,
439 AVDictionary *opts, int flags)
441 AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
444 IDXGIAdapter *pAdapter = NULL;
445 ID3D10Multithread *pMultithread;
446 UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
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;
457 IDXGIFactory2 *pDXGIFactory;
458 hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&pDXGIFactory);
460 int adapter = atoi(device);
461 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
463 IDXGIFactory2_Release(pDXGIFactory);
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);
470 IDXGIAdapter_Release(pAdapter);
472 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device (%lx)\n", (long)hr);
473 return AVERROR_UNKNOWN;
476 hr = ID3D11Device_QueryInterface(device_hwctx->device, &IID_ID3D10Multithread, (void **)&pMultithread);
478 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
479 ID3D10Multithread_Release(pMultithread);
485 const HWContextType ff_hwcontext_type_d3d11va = {
486 .type = AV_HWDEVICE_TYPE_D3D11VA,
489 .device_hwctx_size = sizeof(AVD3D11VADeviceContext),
490 .frames_hwctx_size = sizeof(AVD3D11VAFramesContext),
491 .frames_priv_size = sizeof(D3D11VAFramesContext),
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,
503 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_D3D11, AV_PIX_FMT_NONE },