45#define MAX_ARRAY_SIZE 64
60 HANDLE d3dlib, dxgilib;
62 d3dlib = dlopen(
"d3d11.dll", 0);
63 dxgilib = dlopen(
"dxgi.dll", 0);
64 if (!d3dlib || !dxgilib)
67 mD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE) GetProcAddress(d3dlib,
"D3D11CreateDevice");
121 WaitForSingleObjectEx(
ctx, INFINITE, FALSE);
135 ID3D11Texture2D_Release(frames_hwctx->
texture);
138 if (
s->staging_texture)
139 ID3D11Texture2D_Release(
s->staging_texture);
140 s->staging_texture =
NULL;
146 const void *hwconfig,
150 int nb_sw_formats = 0;
160 UINT format_support = 0;
162 if (SUCCEEDED(hr) && (format_support & D3D11_FORMAT_SUPPORT_TEXTURE2D))
179 ID3D11Texture2D_Release((ID3D11Texture2D *)opaque);
190 ID3D11Texture2D_Release(tex);
194 if (
s->nb_surfaces <=
s->nb_surfaces_used) {
196 s->nb_surfaces_used + 1,
199 ID3D11Texture2D_Release(tex);
203 s->nb_surfaces =
s->nb_surfaces_used + 1;
208 s->nb_surfaces_used++;
215 ID3D11Texture2D_Release(tex);
229 ID3D11Texture2D *tex;
230 D3D11_TEXTURE2D_DESC texDesc = {
232 .Height =
ctx->height,
235 .SampleDesc = { .Count = 1 },
237 .Usage = D3D11_USAGE_DEFAULT,
242 hr = ID3D11Device_CreateTexture2D(device_hwctx->
device, &texDesc,
NULL, &tex);
256 D3D11_TEXTURE2D_DESC texDesc;
261 ID3D11Texture2D_GetDesc(hwctx->
texture, &texDesc);
263 if (
s->nb_surfaces_used >= texDesc.ArraySize) {
268 ID3D11Texture2D_AddRef(hwctx->
texture);
280 D3D11_TEXTURE2D_DESC texDesc;
299 texDesc = (D3D11_TEXTURE2D_DESC){
301 .Height =
ctx->height,
304 .SampleDesc = { .Count = 1 },
305 .ArraySize =
ctx->initial_pool_size,
306 .Usage = D3D11_USAGE_DEFAULT,
312 D3D11_TEXTURE2D_DESC texDesc2;
313 ID3D11Texture2D_GetDesc(hwctx->
texture, &texDesc2);
315 if (texDesc.Width != texDesc2.Width ||
316 texDesc.Height != texDesc2.Height ||
317 texDesc.Format != texDesc2.Format) {
322 ctx->initial_pool_size = texDesc2.ArraySize;
325 }
else if (texDesc.ArraySize > 0) {
326 hr = ID3D11Device_CreateTexture2D(device_hwctx->
device, &texDesc,
NULL, &hwctx->
texture);
336 s->nb_surfaces =
ctx->initial_pool_size;
357 frame->data[0] = (uint8_t *)
desc->texture;
377 fmts[0] =
ctx->sw_format;
381 if (
s->format == DXGI_FORMAT_420_OPAQUE)
394 D3D11_TEXTURE2D_DESC texDesc = {
396 .Height =
ctx->height,
399 .SampleDesc = { .Count = 1 },
401 .Usage = D3D11_USAGE_STAGING,
402 .CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE,
405 hr = ID3D11Device_CreateTexture2D(device_hwctx->
device, &texDesc,
NULL, &
s->staging_texture);
416 D3D11_TEXTURE2D_DESC *
desc,
417 D3D11_MAPPED_SUBRESOURCE *
map)
421 for (
i = 0;
i < 4;
i++)
422 linesize[
i] =
map->RowPitch;
425 (uint8_t*)
map->pData, linesize);
437 ID3D11Resource *texture = (ID3D11Resource *)(ID3D11Texture2D *)
frame->data[0];
439 ID3D11Resource *staging;
442 uint8_t *map_data[4];
444 D3D11_TEXTURE2D_DESC
desc;
445 D3D11_MAPPED_SUBRESOURCE
map;
449 if (
frame->hw_frames_ctx->data != (uint8_t *)
ctx || other->
format !=
ctx->sw_format)
454 if (!
s->staging_texture) {
455 ID3D11Texture2D_GetDesc((ID3D11Texture2D *)texture, &
desc);
461 staging = (ID3D11Resource *)
s->staging_texture;
463 ID3D11Texture2D_GetDesc(
s->staging_texture, &
desc);
466 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->
device_context,
471 staging, 0, D3D11_MAP_READ, 0, &
map);
478 ctx->sw_format,
w,
h);
480 ID3D11DeviceContext_Unmap(device_hwctx->
device_context, staging, 0);
483 staging, 0, D3D11_MAP_WRITE, 0, &
map);
490 ctx->sw_format,
w,
h);
492 ID3D11DeviceContext_Unmap(device_hwctx->
device_context, staging, 0);
494 ID3D11DeviceContext_CopySubresourceRegion(device_hwctx->
device_context,
495 texture,
index, 0, 0, 0,
513 if (!device_hwctx->
lock) {
515 if (device_hwctx->
lock_ctx == INVALID_HANDLE_VALUE) {
530 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->
device, &IID_ID3D11VideoDevice,
537 hr = ID3D11DeviceContext_QueryInterface(device_hwctx->
device_context, &IID_ID3D11VideoContext,
550 if (device_hwctx->
device) {
551 ID3D11Device_Release(device_hwctx->
device);
571 CloseHandle(device_hwctx->
lock_ctx);
572 device_hwctx->
lock_ctx = INVALID_HANDLE_VALUE;
580 IDXGIAdapter *adapter =
NULL;
581 IDXGIFactory2 *factory;
583 long int id = strtol(vendor_id,
NULL, 0);
591 while (IDXGIFactory2_EnumAdapters(factory, adapter_id++, &adapter) != DXGI_ERROR_NOT_FOUND) {
593 DXGI_ADAPTER_DESC adapter_desc;
598 IDXGIAdapter_Release(adapter);
602 hr = IDXGIAdapter2_GetDesc(adapter, &adapter_desc);
603 ID3D11Device_Release(device);
604 IDXGIAdapter_Release(adapter);
608 }
else if (adapter_desc.VendorId ==
id) {
609 IDXGIFactory2_Release(factory);
610 return adapter_id - 1;
614 IDXGIFactory2_Release(factory);
624 IDXGIAdapter *pAdapter =
NULL;
625 ID3D10Multithread *pMultithread;
626 UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
632 creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
644 adapter = atoi(device);
651 "vendor id %s\n", e->
value);
658 IDXGIFactory2 *pDXGIFactory;
663 if (FAILED(IDXGIFactory2_EnumAdapters(pDXGIFactory, adapter, &pAdapter)))
665 IDXGIFactory2_Release(pDXGIFactory);
670 DXGI_ADAPTER_DESC
desc;
671 hr = IDXGIAdapter2_GetDesc(pAdapter, &
desc);
678 hr =
mD3D11CreateDevice(pAdapter, pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
NULL, creationFlags,
NULL, 0,
681 IDXGIAdapter_Release(pAdapter);
687 hr = ID3D11Device_QueryInterface(device_hwctx->
device, &IID_ID3D10Multithread, (
void **)&pMultithread);
689 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
690 ID3D10Multithread_Release(pMultithread);
693#if !HAVE_UWP && HAVE_DXGIDEBUG_H
695 HANDLE dxgidebug_dll = LoadLibrary(
"dxgidebug.dll");
697 HRESULT (WINAPI * pf_DXGIGetDebugInterface)(
const GUID *riid,
void **ppDebug)
698 = (
void *)GetProcAddress(dxgidebug_dll,
"DXGIGetDebugInterface");
699 if (pf_DXGIGetDebugInterface) {
700 IDXGIDebug *dxgi_debug =
NULL;
701 hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (
void**)&dxgi_debug);
702 if (SUCCEEDED(hr) && dxgi_debug) {
703 IDXGIDebug_ReportLiveObjects(dxgi_debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
718 device_hwctx->
BindFlags |= D3D11_BIND_SHADER_RESOURCE;
721 device_hwctx->
BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
724 device_hwctx->
BindFlags |= D3D11_BIND_RENDER_TARGET;
727 device_hwctx->
MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static const char *const format[]
simple assert() macros that are a bit more flexible than ISO C assert().
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
common internal and external API header
@ DXGI_FORMAT_B8G8R8A8_UNORM
@ DXGI_FORMAT_R16G16B16A16_FLOAT
static enum AVPixelFormat pix_fmt
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.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
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.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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.
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 ...
AVHWFrameTransferDirection
@ AV_HWDEVICE_TYPE_D3D11VA
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
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)
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
static int ff_thread_once(char *control, void(*routine)(void))
Memory handling functions.
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.
#define AV_PIX_FMT_RGBAF16
AVPixelFormat
Pixel format.
@ 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...
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_VUYX
packed VUYX 4:4:4:4, 32bpp, Variant of VUYA where alpha channel is left undefined
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
#define AV_PIX_FMT_X2BGR10
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
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.
This structure describes decoded (raw) audio or video data.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
This struct describes a set or pool of "hardware" frames (i.e.
ID3D11Texture2D * staging_texture
AVD3D11VAFramesContext p
The public AVD3D11VAFramesContext.
AVBufferPool * pool_internal
#define av_malloc_array(a, b)
#define av_realloc_f(p, o, n)
static AVFormatContext * ctx
static AVDictionary * opts
static enum AVPixelFormat supported_formats[]
static av_cold int load_functions(AVFilterContext *avctx)