FFmpeg
Loading...
Searching...
No Matches
hwcontext_d3d11va.c
Go to the documentation of this file.
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#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 "mem.h"
40#include "pixdesc.h"
41#include "pixfmt.h"
42#include "thread.h"
43#include "compat/w32dlfcn.h"
44
45#define MAX_ARRAY_SIZE 64 // Driver specification limits ArraySize to 64 for decoder-bound resources
46
47typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
48
50
52static PFN_D3D11_CREATE_DEVICE mD3D11CreateDevice;
53
54static av_cold void load_functions(void)
55{
56#if !HAVE_UWP
57 // We let these "leak" - this is fine, as unloading has no great benefit, and
58 // Windows will mark a DLL as loaded forever if its internal refcount overflows
59 // from too many LoadLibrary calls.
60 HANDLE d3dlib, dxgilib;
61
62 d3dlib = dlopen("d3d11.dll", 0);
63 dxgilib = dlopen("dxgi.dll", 0);
64 if (!d3dlib || !dxgilib)
65 return;
66
67 mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) GetProcAddress(d3dlib, "D3D11CreateDevice");
68 mCreateDXGIFactory = (PFN_CREATE_DXGI_FACTORY) GetProcAddress(dxgilib, "CreateDXGIFactory1");
70 mCreateDXGIFactory = (PFN_CREATE_DXGI_FACTORY) GetProcAddress(dxgilib, "CreateDXGIFactory");
71#else
72 // In UWP (which lacks LoadLibrary), CreateDXGIFactory isn't available,
73 // only CreateDXGIFactory1
74 mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) D3D11CreateDevice;
75 mCreateDXGIFactory = (PFN_CREATE_DXGI_FACTORY) CreateDXGIFactory1;
76#endif
77}
78
79typedef struct D3D11VAFramesContext {
80 /**
81 * The public AVD3D11VAFramesContext. See hwcontext_d3d11va.h for it.
82 */
84
87
88 DXGI_FORMAT format;
89
90 ID3D11Texture2D *staging_texture;
92
93static const struct {
94 DXGI_FORMAT d3d_format;
97 { DXGI_FORMAT_NV12, AV_PIX_FMT_NV12 },
98 { DXGI_FORMAT_P010, AV_PIX_FMT_P010 },
100 { DXGI_FORMAT_R10G10B10A2_UNORM, AV_PIX_FMT_X2BGR10 },
102 { DXGI_FORMAT_AYUV, AV_PIX_FMT_VUYX },
103 { DXGI_FORMAT_YUY2, AV_PIX_FMT_YUYV422 },
104 { DXGI_FORMAT_Y210, AV_PIX_FMT_Y210 },
105 { DXGI_FORMAT_Y410, AV_PIX_FMT_XV30 },
106 { DXGI_FORMAT_P016, AV_PIX_FMT_P016 },
107 { DXGI_FORMAT_Y216, AV_PIX_FMT_Y216 },
108 { DXGI_FORMAT_Y416, AV_PIX_FMT_XV48 },
109 // There is no 12bit pixel format defined in DXGI_FORMAT*, use 16bit to compatible
110 // with 12 bit AV_PIX_FMT* formats.
111 { DXGI_FORMAT_P016, AV_PIX_FMT_P012 },
112 { DXGI_FORMAT_Y216, AV_PIX_FMT_Y212 },
113 { DXGI_FORMAT_Y416, AV_PIX_FMT_XV36 },
114 // Special opaque formats. The pix_fmt is merely a place holder, as the
115 // opaque format cannot be accessed directly.
116 { DXGI_FORMAT_420_OPAQUE, AV_PIX_FMT_YUV420P },
118
119static void d3d11va_default_lock(void *ctx)
120{
121 WaitForSingleObjectEx(ctx, INFINITE, FALSE);
122}
123
124static void d3d11va_default_unlock(void *ctx)
125{
126 ReleaseMutex(ctx);
127}
128
130{
131 D3D11VAFramesContext *s = ctx->hwctx;
132 AVD3D11VAFramesContext *frames_hwctx = &s->p;
133
134 if (frames_hwctx->texture)
135 ID3D11Texture2D_Release(frames_hwctx->texture);
136 frames_hwctx->texture = NULL;
137
138 if (s->staging_texture)
139 ID3D11Texture2D_Release(s->staging_texture);
140 s->staging_texture = NULL;
141
142 av_freep(&frames_hwctx->texture_infos);
143}
144
146 const void *hwconfig,
147 AVHWFramesConstraints *constraints)
148{
149 AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
150 int nb_sw_formats = 0;
151 HRESULT hr;
152 int i;
153
155 sizeof(*constraints->valid_sw_formats));
156 if (!constraints->valid_sw_formats)
157 return AVERROR(ENOMEM);
158
159 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
160 UINT format_support = 0;
161 hr = ID3D11Device_CheckFormatSupport(device_hwctx->device, supported_formats[i].d3d_format, &format_support);
162 if (SUCCEEDED(hr) && (format_support & D3D11_FORMAT_SUPPORT_TEXTURE2D))
163 constraints->valid_sw_formats[nb_sw_formats++] = supported_formats[i].pix_fmt;
164 }
165 constraints->valid_sw_formats[nb_sw_formats] = AV_PIX_FMT_NONE;
166
167 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
168 if (!constraints->valid_hw_formats)
169 return AVERROR(ENOMEM);
170
171 constraints->valid_hw_formats[0] = AV_PIX_FMT_D3D11;
172 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
173
174 return 0;
175}
176
177static void free_texture(void *opaque, uint8_t *data)
178{
179 ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
180 av_free(data);
181}
182
183static AVBufferRef *wrap_texture_buf(AVHWFramesContext *ctx, ID3D11Texture2D *tex, int index)
184{
185 AVBufferRef *buf;
187 D3D11VAFramesContext *s = ctx->hwctx;
188 AVD3D11VAFramesContext *frames_hwctx = &s->p;
189 if (!desc) {
190 ID3D11Texture2D_Release(tex);
191 return NULL;
192 }
193
194 if (s->nb_surfaces <= s->nb_surfaces_used) {
195 frames_hwctx->texture_infos = av_realloc_f(frames_hwctx->texture_infos,
196 s->nb_surfaces_used + 1,
197 sizeof(*frames_hwctx->texture_infos));
198 if (!frames_hwctx->texture_infos) {
199 ID3D11Texture2D_Release(tex);
200 av_free(desc);
201 return NULL;
202 }
203 s->nb_surfaces = s->nb_surfaces_used + 1;
204 }
205
206 frames_hwctx->texture_infos[s->nb_surfaces_used].texture = tex;
207 frames_hwctx->texture_infos[s->nb_surfaces_used].index = index;
208 s->nb_surfaces_used++;
209
210 desc->texture = tex;
211 desc->index = index;
212
213 buf = av_buffer_create((uint8_t *)desc, sizeof(*desc), free_texture, tex, 0);
214 if (!buf) {
215 ID3D11Texture2D_Release(tex);
216 av_free(desc);
217 return NULL;
218 }
219
220 return buf;
221}
222
224{
225 D3D11VAFramesContext *s = ctx->hwctx;
226 AVD3D11VAFramesContext *hwctx = &s->p;
227 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
228 HRESULT hr;
229 ID3D11Texture2D *tex;
230 D3D11_TEXTURE2D_DESC texDesc = {
231 .Width = ctx->width,
232 .Height = ctx->height,
233 .MipLevels = 1,
234 .Format = s->format,
235 .SampleDesc = { .Count = 1 },
236 .ArraySize = 1,
237 .Usage = D3D11_USAGE_DEFAULT,
238 .BindFlags = hwctx->BindFlags,
239 .MiscFlags = hwctx->MiscFlags,
240 };
241
242 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &tex);
243 if (FAILED(hr)) {
244 av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
245 return NULL;
246 }
247
248 return wrap_texture_buf(ctx, tex, 0);
249}
250
251static AVBufferRef *d3d11va_pool_alloc(void *opaque, size_t size)
252{
254 D3D11VAFramesContext *s = ctx->hwctx;
255 AVD3D11VAFramesContext *hwctx = &s->p;
256 D3D11_TEXTURE2D_DESC texDesc;
257
258 if (!hwctx->texture)
260
261 ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc);
262
263 if (s->nb_surfaces_used >= texDesc.ArraySize) {
264 av_log(ctx, AV_LOG_ERROR, "Static surface pool size exceeded.\n");
265 return NULL;
266 }
267
268 ID3D11Texture2D_AddRef(hwctx->texture);
269 return wrap_texture_buf(ctx, hwctx->texture, s->nb_surfaces_used);
270}
271
273{
274 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
275 D3D11VAFramesContext *s = ctx->hwctx;
276 AVD3D11VAFramesContext *hwctx = &s->p;
277
278 int i;
279 HRESULT hr;
280 D3D11_TEXTURE2D_DESC texDesc;
281
282 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
283 if (ctx->sw_format == supported_formats[i].pix_fmt) {
284 s->format = supported_formats[i].d3d_format;
285 break;
286 }
287 }
289 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
290 av_get_pix_fmt_name(ctx->sw_format));
291 return AVERROR(EINVAL);
292 }
293
294 hwctx->BindFlags |= device_hwctx->BindFlags;
295 hwctx->MiscFlags |= device_hwctx->MiscFlags;
296
297 if (s->format == DXGI_FORMAT_420_OPAQUE)
298 hwctx->BindFlags &= ~D3D11_BIND_SHADER_RESOURCE;
299
300 ctx->initial_pool_size = FFMIN(ctx->initial_pool_size, MAX_ARRAY_SIZE);
301
302 texDesc = (D3D11_TEXTURE2D_DESC){
303 .Width = ctx->width,
304 .Height = ctx->height,
305 .MipLevels = 1,
306 .Format = s->format,
307 .SampleDesc = { .Count = 1 },
308 .ArraySize = ctx->initial_pool_size,
309 .Usage = D3D11_USAGE_DEFAULT,
310 .BindFlags = hwctx->BindFlags,
311 .MiscFlags = hwctx->MiscFlags,
312 };
313
314 if (hwctx->texture) {
315 D3D11_TEXTURE2D_DESC texDesc2;
316 ID3D11Texture2D_GetDesc(hwctx->texture, &texDesc2);
317
318 if (texDesc.Width != texDesc2.Width ||
319 texDesc.Height != texDesc2.Height ||
320 texDesc.Format != texDesc2.Format) {
321 av_log(ctx, AV_LOG_ERROR, "User-provided texture has mismatching parameters\n");
322 return AVERROR(EINVAL);
323 }
324
325 ctx->initial_pool_size = texDesc2.ArraySize;
326 hwctx->BindFlags = texDesc2.BindFlags;
327 hwctx->MiscFlags = texDesc2.MiscFlags;
328 } else if (texDesc.ArraySize > 0) {
329 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &hwctx->texture);
330 if (FAILED(hr)) {
331 av_log(ctx, AV_LOG_ERROR, "Could not create the texture (%lx)\n", (long)hr);
332 return AVERROR_UNKNOWN;
333 }
334 }
335
336 hwctx->texture_infos = av_realloc_f(NULL, ctx->initial_pool_size, sizeof(*hwctx->texture_infos));
337 if (!hwctx->texture_infos)
338 return AVERROR(ENOMEM);
339 s->nb_surfaces = ctx->initial_pool_size;
340
344 if (!ffhwframesctx(ctx)->pool_internal)
345 return AVERROR(ENOMEM);
346
347 return 0;
348}
349
351{
353
354 frame->buf[0] = av_buffer_pool_get(ctx->pool);
355 if (!frame->buf[0])
356 return AVERROR(ENOMEM);
357
358 desc = (AVD3D11FrameDescriptor *)frame->buf[0]->data;
359
360 frame->data[0] = (uint8_t *)desc->texture;
361 frame->data[1] = (uint8_t *)desc->index;
362 frame->format = AV_PIX_FMT_D3D11;
363 frame->width = ctx->width;
364 frame->height = ctx->height;
365
366 return 0;
367}
368
371 enum AVPixelFormat **formats)
372{
373 D3D11VAFramesContext *s = ctx->hwctx;
374 enum AVPixelFormat *fmts;
375
376 fmts = av_malloc_array(2, sizeof(*fmts));
377 if (!fmts)
378 return AVERROR(ENOMEM);
379
380 fmts[0] = ctx->sw_format;
381 fmts[1] = AV_PIX_FMT_NONE;
382
383 // Don't signal support for opaque formats. Actual access would fail.
384 if (s->format == DXGI_FORMAT_420_OPAQUE)
385 fmts[0] = AV_PIX_FMT_NONE;
386
387 *formats = fmts;
388
389 return 0;
390}
391
393{
394 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
395 D3D11VAFramesContext *s = ctx->hwctx;
396 HRESULT hr;
397 D3D11_TEXTURE2D_DESC texDesc = {
398 .Width = ctx->width,
399 .Height = ctx->height,
400 .MipLevels = 1,
401 .Format = format,
402 .SampleDesc = { .Count = 1 },
403 .ArraySize = 1,
404 .Usage = D3D11_USAGE_STAGING,
405 .CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE,
406 };
407
408 hr = ID3D11Device_CreateTexture2D(device_hwctx->device, &texDesc, NULL, &s->staging_texture);
409 if (FAILED(hr)) {
410 av_log(ctx, AV_LOG_ERROR, "Could not create the staging texture (%lx)\n", (long)hr);
411 return AVERROR_UNKNOWN;
412 }
413
414 return 0;
415}
416
417static void fill_texture_ptrs(uint8_t *data[4], int linesize[4],
419 D3D11_TEXTURE2D_DESC *desc,
420 D3D11_MAPPED_SUBRESOURCE *map)
421{
422 int i;
423
424 for (i = 0; i < 4; i++)
425 linesize[i] = map->RowPitch;
426
427 av_image_fill_pointers(data, ctx->sw_format, desc->Height,
428 (uint8_t*)map->pData, linesize);
429}
430
432 const AVFrame *src)
433{
434 AVD3D11VADeviceContext *device_hwctx = ctx->device_ctx->hwctx;
435 D3D11VAFramesContext *s = ctx->hwctx;
436 int download = src->format == AV_PIX_FMT_D3D11;
437 const AVFrame *frame = download ? src : dst;
438 const AVFrame *other = download ? dst : src;
439 // (The interface types are compatible.)
440 ID3D11Resource *texture = (ID3D11Resource *)(ID3D11Texture2D *)frame->data[0];
441 int index = (intptr_t)frame->data[1];
442 ID3D11Resource *staging;
443 int w = FFMIN(dst->width, src->width);
444 int h = FFMIN(dst->height, src->height);
445 uint8_t *map_data[4];
446 int map_linesize[4];
447 D3D11_TEXTURE2D_DESC desc;
448 D3D11_MAPPED_SUBRESOURCE map;
449 HRESULT hr;
450 int res;
451
452 if (frame->hw_frames_ctx->data != (uint8_t *)ctx || other->format != ctx->sw_format)
453 return AVERROR(EINVAL);
454
455 device_hwctx->lock(device_hwctx->lock_ctx);
456
457 if (!s->staging_texture) {
458 ID3D11Texture2D_GetDesc((ID3D11Texture2D *)texture, &desc);
460 if (res < 0)
461 return res;
462 }
463
464 staging = (ID3D11Resource *)s->staging_texture;
465
466 ID3D11Texture2D_GetDesc(s->staging_texture, &desc);
467
468 if (download) {
469 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
470 staging, 0, 0, 0, 0,
471 texture, index, NULL);
472
473 hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
474 staging, 0, D3D11_MAP_READ, 0, &map);
475 if (FAILED(hr))
476 goto map_failed;
477
478 fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
479
480 av_image_copy2(dst->data, dst->linesize, map_data, map_linesize,
481 ctx->sw_format, w, h);
482
483 ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
484 } else {
485 hr = ID3D11DeviceContext_Map(device_hwctx->device_context,
486 staging, 0, D3D11_MAP_WRITE, 0, &map);
487 if (FAILED(hr))
488 goto map_failed;
489
490 fill_texture_ptrs(map_data, map_linesize, ctx, &desc, &map);
491
492 av_image_copy2(map_data, map_linesize, src->data, src->linesize,
493 ctx->sw_format, w, h);
494
495 ID3D11DeviceContext_Unmap(device_hwctx->device_context, staging, 0);
496
497 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->device_context,
498 texture, index, 0, 0, 0,
499 staging, 0, NULL);
500 }
501
502 device_hwctx->unlock(device_hwctx->lock_ctx);
503 return 0;
504
505map_failed:
506 av_log(ctx, AV_LOG_ERROR, "Unable to lock D3D11VA surface (%lx)\n", (long)hr);
507 device_hwctx->unlock(device_hwctx->lock_ctx);
508 return AVERROR_UNKNOWN;
509}
510
512{
513 AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
514 HRESULT hr;
515
516 if (!device_hwctx->lock) {
517 device_hwctx->lock_ctx = CreateMutex(NULL, 0, NULL);
518 if (device_hwctx->lock_ctx == INVALID_HANDLE_VALUE) {
519 av_log(NULL, AV_LOG_ERROR, "Failed to create a mutex\n");
520 return AVERROR(EINVAL);
521 }
522 device_hwctx->lock = d3d11va_default_lock;
523 device_hwctx->unlock = d3d11va_default_unlock;
524 }
525
526 if (!device_hwctx->device_context) {
527 ID3D11Device_GetImmediateContext(device_hwctx->device, &device_hwctx->device_context);
528 if (!device_hwctx->device_context)
529 return AVERROR_UNKNOWN;
530 }
531
532 if (!device_hwctx->video_device) {
533 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device, &IID_ID3D11VideoDevice,
534 (void **)&device_hwctx->video_device);
535 if (FAILED(hr))
536 return AVERROR_UNKNOWN;
537 }
538
539 if (!device_hwctx->video_context) {
540 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->device_context, &IID_ID3D11VideoContext,
541 (void **)&device_hwctx->video_context);
542 if (FAILED(hr))
543 return AVERROR_UNKNOWN;
544 }
545
546 return 0;
547}
548
550{
551 AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
552
553 if (device_hwctx->device) {
554 ID3D11Device_Release(device_hwctx->device);
555 device_hwctx->device = NULL;
556 }
557
558 if (device_hwctx->device_context) {
559 ID3D11DeviceContext_Release(device_hwctx->device_context);
560 device_hwctx->device_context = NULL;
561 }
562
563 if (device_hwctx->video_device) {
564 ID3D11VideoDevice_Release(device_hwctx->video_device);
565 device_hwctx->video_device = NULL;
566 }
567
568 if (device_hwctx->video_context) {
569 ID3D11VideoContext_Release(device_hwctx->video_context);
570 device_hwctx->video_context = NULL;
571 }
572
573 if (device_hwctx->lock == d3d11va_default_lock) {
574 CloseHandle(device_hwctx->lock_ctx);
575 device_hwctx->lock_ctx = INVALID_HANDLE_VALUE;
576 device_hwctx->lock = NULL;
577 }
578}
579
580static int d3d11va_device_find_adapter_by_vendor_id(AVHWDeviceContext *ctx, uint32_t flags, const char *vendor_id)
581{
582 HRESULT hr;
583 IDXGIAdapter *adapter = NULL;
584 IDXGIFactory2 *factory;
585 int adapter_id = 0;
586 long int id = strtol(vendor_id, NULL, 0);
587
588 hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&factory);
589 if (FAILED(hr)) {
590 av_log(ctx, AV_LOG_ERROR, "CreateDXGIFactory returned error\n");
591 return -1;
592 }
593
594 while (IDXGIFactory2_EnumAdapters(factory, adapter_id++, &adapter) != DXGI_ERROR_NOT_FOUND) {
595 ID3D11Device* device = NULL;
596 DXGI_ADAPTER_DESC adapter_desc;
597
598 hr = mD3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, flags, NULL, 0, D3D11_SDK_VERSION, &device, NULL, NULL);
599 if (FAILED(hr)) {
600 av_log(ctx, AV_LOG_DEBUG, "D3D11CreateDevice returned error, try next adapter\n");
601 IDXGIAdapter_Release(adapter);
602 continue;
603 }
604
605 hr = IDXGIAdapter2_GetDesc(adapter, &adapter_desc);
606 ID3D11Device_Release(device);
607 IDXGIAdapter_Release(adapter);
608 if (FAILED(hr)) {
609 av_log(ctx, AV_LOG_DEBUG, "IDXGIAdapter2_GetDesc returned error, try next adapter\n");
610 continue;
611 } else if (adapter_desc.VendorId == id) {
612 IDXGIFactory2_Release(factory);
613 return adapter_id - 1;
614 }
615 }
616
617 IDXGIFactory2_Release(factory);
618 return -1;
619}
620
621static int d3d11va_device_create(AVHWDeviceContext *ctx, const char *device,
622 AVDictionary *opts, int flags)
623{
624 AVD3D11VADeviceContext *device_hwctx = ctx->hwctx;
625
626 HRESULT hr;
627 IDXGIAdapter *pAdapter = NULL;
628 ID3D10Multithread *pMultithread;
629 UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
630 int is_debug = !!av_dict_get(opts, "debug", NULL, 0);
631 int ret;
632 int adapter = -1;
633
634 if (is_debug) {
635 creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
636 av_log(ctx, AV_LOG_INFO, "Enabling d3d11 debugging.\n");
637 }
638
640 return AVERROR_UNKNOWN;
642 av_log(ctx, AV_LOG_ERROR, "Failed to load D3D11 library or its functions\n");
643 return AVERROR_UNKNOWN;
644 }
645
646 if (device) {
647 adapter = atoi(device);
648 } else {
649 AVDictionaryEntry *e = av_dict_get(opts, "vendor_id", NULL, 0);
650 if (e && e->value) {
651 adapter = d3d11va_device_find_adapter_by_vendor_id(ctx, creationFlags, e->value);
652 if (adapter < 0) {
653 av_log(ctx, AV_LOG_ERROR, "Failed to find d3d11va adapter by "
654 "vendor id %s\n", e->value);
655 return AVERROR_UNKNOWN;
656 }
657 }
658 }
659
660 if (adapter >= 0) {
661 IDXGIFactory2 *pDXGIFactory;
662
663 av_log(ctx, AV_LOG_VERBOSE, "Selecting d3d11va adapter %d\n", adapter);
664 hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&pDXGIFactory);
665 if (SUCCEEDED(hr)) {
666 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
667 pAdapter = NULL;
668 IDXGIFactory2_Release(pDXGIFactory);
669 }
670 }
671
672 if (pAdapter) {
673 DXGI_ADAPTER_DESC desc;
674 hr = IDXGIAdapter2_GetDesc(pAdapter, &desc);
675 if (!FAILED(hr)) {
676 av_log(ctx, AV_LOG_INFO, "Using device %04x:%04x (%ls).\n",
677 desc.VendorId, desc.DeviceId, desc.Description);
678 }
679 }
680
681 hr = mD3D11CreateDevice(pAdapter, pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, NULL, 0,
682 D3D11_SDK_VERSION, &device_hwctx->device, NULL, NULL);
683 if (pAdapter)
684 IDXGIAdapter_Release(pAdapter);
685 if (FAILED(hr)) {
686 av_log(ctx, AV_LOG_ERROR, "Failed to create Direct3D device (%lx)\n", (long)hr);
687 return AVERROR_UNKNOWN;
688 }
689
690 hr = ID3D11Device_QueryInterface(device_hwctx->device, &IID_ID3D10Multithread, (void **)&pMultithread);
691 if (SUCCEEDED(hr)) {
692 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
693 ID3D10Multithread_Release(pMultithread);
694 }
695
696#if !HAVE_UWP && HAVE_DXGIDEBUG_H
697 if (is_debug) {
698 HANDLE dxgidebug_dll = LoadLibrary("dxgidebug.dll");
699 if (dxgidebug_dll) {
700 HRESULT (WINAPI * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug)
701 = (void *)GetProcAddress(dxgidebug_dll, "DXGIGetDebugInterface");
702 if (pf_DXGIGetDebugInterface) {
703 IDXGIDebug *dxgi_debug = NULL;
704 hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&dxgi_debug);
705 if (SUCCEEDED(hr) && dxgi_debug) {
706 IDXGIDebug_ReportLiveObjects(dxgi_debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
707 av_log(ctx, AV_LOG_INFO, "Enabled dxgi debugging.\n");
708 } else {
709 av_log(ctx, AV_LOG_WARNING, "Failed enabling dxgi debugging.\n");
710 }
711 } else {
712 av_log(ctx, AV_LOG_WARNING, "Failed getting dxgi debug interface.\n");
713 }
714 } else {
715 av_log(ctx, AV_LOG_WARNING, "Failed loading dxgi debug library.\n");
716 }
717 }
718#endif
719
720 if (av_dict_get(opts, "SHADER", NULL, 0))
721 device_hwctx->BindFlags |= D3D11_BIND_SHADER_RESOURCE;
722
723 if (av_dict_get(opts, "UAV", NULL, 0))
724 device_hwctx->BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
725
726 if (av_dict_get(opts, "RTV", NULL, 0))
727 device_hwctx->BindFlags |= D3D11_BIND_RENDER_TARGET;
728
729 if (av_dict_get(opts, "SHARED", NULL, 0))
730 device_hwctx->MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
731
732 return 0;
733}
734
737 .name = "D3D11VA",
738
739 .device_hwctx_size = sizeof(AVD3D11VADeviceContext),
740 .frames_hwctx_size = sizeof(D3D11VAFramesContext),
741
742 .device_create = d3d11va_device_create,
743 .device_init = d3d11va_device_init,
744 .device_uninit = d3d11va_device_uninit,
745 .frames_get_constraints = d3d11va_frames_get_constraints,
746 .frames_init = d3d11va_frames_init,
747 .frames_uninit = d3d11va_frames_uninit,
748 .frames_get_buffer = d3d11va_get_buffer,
749 .transfer_get_formats = d3d11va_transfer_get_formats,
750 .transfer_data_to = d3d11va_transfer_data,
751 .transfer_data_from = d3d11va_transfer_data,
752
753 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_D3D11, AV_PIX_FMT_NONE },
754};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static const char *const format[]
Definition af_aiir.c:445
static AVFormatContext * ctx
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define NULL
Definition coverity.c:32
@ DXGI_FORMAT_B8G8R8A8_UNORM
Definition dds.c:92
@ DXGI_FORMAT_R16G16B16A16_FLOAT
Definition dds.c:63
static enum AVPixelFormat pix_fmt
static AVFrame * frame
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:56
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition buffer.c:396
AVBufferPool * av_buffer_pool_init2(size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition buffer.c:260
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition imgutils.c:145
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition imgutils.h:184
int index
Definition gxfenc.c:90
AVHWFrameTransferDirection
Definition hwcontext.h:406
@ AV_HWDEVICE_TYPE_D3D11VA
Definition hwcontext.h:35
static int d3d11va_create_staging_texture(AVHWFramesContext *ctx, DXGI_FORMAT format)
static void d3d11va_default_lock(void *ctx)
static AVBufferRef * d3d11va_alloc_single(AVHWFramesContext *ctx)
static void free_texture(void *opaque, uint8_t *data)
static int d3d11va_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
static int d3d11va_device_init(AVHWDeviceContext *hwdev)
static PFN_D3D11_CREATE_DEVICE mD3D11CreateDevice
#define MAX_ARRAY_SIZE
static int d3d11va_device_create(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
static av_cold void load_functions(void)
static int d3d11va_device_find_adapter_by_vendor_id(AVHWDeviceContext *ctx, uint32_t flags, const char *vendor_id)
static int d3d11va_transfer_data(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
static int d3d11va_frames_init(AVHWFramesContext *ctx)
HRESULT(WINAPI * PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory)
DXGI_FORMAT d3d_format
static void fill_texture_ptrs(uint8_t *data[4], int linesize[4], AVHWFramesContext *ctx, D3D11_TEXTURE2D_DESC *desc, D3D11_MAPPED_SUBRESOURCE *map)
static AVOnce functions_loaded
static void d3d11va_default_unlock(void *ctx)
static void d3d11va_frames_uninit(AVHWFramesContext *ctx)
static int d3d11va_frames_get_constraints(AVHWDeviceContext *ctx, const void *hwconfig, AVHWFramesConstraints *constraints)
const HWContextType ff_hwcontext_type_d3d11va
static PFN_CREATE_DXGI_FACTORY mCreateDXGIFactory
static AVBufferRef * d3d11va_pool_alloc(void *opaque, size_t size)
static AVBufferRef * wrap_texture_buf(AVHWFramesContext *ctx, ID3D11Texture2D *tex, int index)
static int d3d11va_transfer_get_formats(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
static void d3d11va_device_uninit(AVHWDeviceContext *hwdev)
An API-specific header for AV_HWDEVICE_TYPE_D3D11VA.
static FFHWFramesContext * ffhwframesctx(AVHWFramesContext *ctx)
const VDPAUPixFmtMap * map
misc image utilities
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
const char data[16]
Definition mxf.c:149
void ID3D11Device
Definition nvenc.h:28
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
pixel format definitions
#define AV_PIX_FMT_RGBAF16
Definition pixfmt.h:630
#define AV_PIX_FMT_P012
Definition pixfmt.h:609
#define AV_PIX_FMT_Y210
Definition pixfmt.h:612
#define AV_PIX_FMT_P010
Definition pixfmt.h:608
#define AV_PIX_FMT_P016
Definition pixfmt.h:610
#define AV_PIX_FMT_XV30
Definition pixfmt.h:615
#define AV_PIX_FMT_XV48
Definition pixfmt.h:617
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_VUYX
packed VUYX 4:4:4:4, 32bpp, Variant of VUYA where alpha channel is left undefined
Definition pixfmt.h:406
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition pixfmt.h:336
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition pixfmt.h:74
#define AV_PIX_FMT_X2BGR10
Definition pixfmt.h:620
#define AV_PIX_FMT_Y216
Definition pixfmt.h:614
#define AV_PIX_FMT_Y212
Definition pixfmt.h:613
#define AV_PIX_FMT_XV36
Definition pixfmt.h:616
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
D3D11 frame descriptor for pool allocation.
ID3D11Texture2D * texture
The texture in which the frame is located.
intptr_t index
The index into the array texture element representing the frame, or 0 if the texture is not an array ...
This struct is allocated as AVHWDeviceContext.hwctx.
void(* lock)(void *lock_ctx)
Callbacks for locking.
UINT BindFlags
D3D11_TEXTURE2D_DESC.BindFlags to be applied to D3D11 resources allocated for frames using this devic...
UINT MiscFlags
D3D11_TEXTURE2D_DESC.MiscFlags to be applied to D3D11 resources allocated for frames using this devic...
void(* unlock)(void *lock_ctx)
ID3D11VideoContext * video_context
If unset, this will be set from the device_context field on init.
ID3D11Device * device
Device used for texture creation and access.
ID3D11DeviceContext * device_context
If unset, this will be set from the device field on init.
ID3D11VideoDevice * video_device
If unset, this will be set from the device field on init.
This struct is allocated as AVHWFramesContext.hwctx.
UINT MiscFlags
D3D11_TEXTURE2D_DESC.MiscFlags used for texture creation.
ID3D11Texture2D * texture
The canonical texture used for pool allocation.
AVD3D11FrameDescriptor * texture_infos
In case if texture structure member above is not NULL contains the same texture pointer for all eleme...
UINT BindFlags
D3D11_TEXTURE2D_DESC.BindFlags used for texture creation.
char * value
Definition dict.h:92
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition hwcontext.h:88
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition hwcontext.h:444
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition hwcontext.h:449
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition hwcontext.h:456
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
ID3D11Texture2D * staging_texture
AVD3D11VAFramesContext p
The public AVD3D11VAFramesContext.
AVBufferPool * pool_internal
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_realloc_f(p, o, n)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
int size
static enum AVPixelFormat supported_formats[]
static av_cold int load_functions(AVFilterContext *avctx)