55 ID3D12Resource *resource =
NULL;
56 D3D12_HEAP_PROPERTIES props = { .Type = D3D12_HEAP_TYPE_DEFAULT };
57 D3D12_RESOURCE_DESC
desc;
59 if (reference_only_map ==
NULL) {
68 for (
i = 0;
i <
ctx->max_num_ref + 1;
i++) {
69 if (reference_only_map[
i].resource !=
NULL &&
70 reference_only_map[
i].output_resource == output_resource) {
71 reference_only_map[
i].
used = 1;
77 for (
i = 0;
i <
ctx->max_num_ref + 1;
i++) {
78 if (!reference_only_map[
i].used && reference_only_map[
i].resource !=
NULL) {
79 reference_only_map[
i].
used = 1;
80 resource = reference_only_map[
i].
resource;
87 for (
i = 0;
i <
ctx->max_num_ref + 1;
i++) {
88 if (reference_only_map[
i].resource ==
NULL)
92 if (
i ==
ctx->max_num_ref + 1) {
98 output_resource->lpVtbl->GetDesc(output_resource, &
desc);
99 desc.Flags = D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
101 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, D3D12_HEAP_FLAG_NONE, &
desc,
102 D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource, (
void **)&reference_only_map[
i].
resource))) {
107 reference_only_map[
i].
used = 1;
108 resource = reference_only_map[
i].
resource;
119 if (reference_only_map !=
NULL) {
120 for (
i = 0;
i <
ctx->max_num_ref + 1;
i++) {
121 if (reference_only_map[
i].resource !=
NULL) {
135 if (reference_only_map ==
NULL)
137 memset(
ctx->ref_only_resources, 0,
ctx->max_num_ref *
sizeof(*(
ctx->ref_only_resources)));
138 for (j = 0; j <
ctx->max_num_ref + 1; j++) {
139 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
140 if (reference_only_map[j].used && reference_only_map[j].output_resource ==
ctx->ref_resources[
i]) {
141 ctx->ref_only_resources[
i] = reference_only_map[j].
resource;
145 if (
i ==
ctx->max_num_ref)
146 reference_only_map[j].
used = 0;
172 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
173 if (
ctx->ref_resources[
i] && res ==
ctx->ref_resources[
i]) {
174 ctx->used_mask |= 1 <<
i;
180 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
181 if (!((
ctx->used_mask >>
i) & 0x1)) {
182 ctx->ref_resources[
i] = res;
194 ID3D12Resource **ppBuffer)
199 D3D12_HEAP_PROPERTIES heap_props = { .Type = D3D12_HEAP_TYPE_UPLOAD };
201 D3D12_RESOURCE_DESC
desc = {
202 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
203 .Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT,
204 .Width =
ctx->bitstream_size,
206 .DepthOrArraySize = 1,
208 .Format = DXGI_FORMAT_UNKNOWN,
209 .SampleDesc = { .Count = 1, .Quality = 0 },
210 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
211 .Flags = D3D12_RESOURCE_FLAG_NONE,
215 uint64_t completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
224 hr = ID3D12Device_CreateCommandAllocator(
ctx->device_ctx->device, D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
225 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
231 hr = ID3D12Device_CreateCommittedResource(
ctx->device_ctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
232 &
desc, D3D12_RESOURCE_STATE_GENERIC_READ,
NULL,
233 &IID_ID3D12Resource, (
void **)ppBuffer);
244 ID3D12Resource *pBuffer, uint64_t fence_value)
249 .command_allocator = pAllocator,
251 .fence_value = fence_value,
265 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
266 if (completion < psync_ctx->fence_value) {
267 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
270 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
290 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
304 D3D12_VIDEO_DECODER_HEAP_DESC
desc = {
306 .Configuration =
ctx->cfg,
307 .DecodeWidth = frames_ctx->
width,
308 .DecodeHeight = frames_ctx->
height,
309 .Format = frames_hwctx->
format,
312 .MaxDecodePictureBufferCount =
ctx->max_num_ref,
316 &IID_ID3D12VideoDecoderHeap, (
void **)&
ctx->decoder_heap));
323 "[width(%d), height(%d)], on your device!\n", frames_ctx->
width, frames_ctx->
height);
331 D3D12_VIDEO_DECODER_DESC
desc;
337 D3D12_FEATURE_DATA_VIDEO_DECODE_SUPPORT feature = {
339 .Configuration =
ctx->cfg,
340 .Width = frames_ctx->
width,
341 .Height = frames_ctx->
height,
342 .DecodeFormat = frames_hwctx->
format,
347 DX_CHECK(ID3D12VideoDevice_CheckFeatureSupport(device_hwctx->
video_device, D3D12_FEATURE_VIDEO_DECODE_SUPPORT,
348 &feature,
sizeof(feature)));
349 if (!(feature.SupportFlags & D3D12_VIDEO_DECODE_SUPPORT_FLAG_SUPPORTED)) {
353 if (!(feature.DecodeTier >= D3D12_VIDEO_DECODE_TIER_2)) {
354 av_log(avctx,
AV_LOG_ERROR,
"D3D12 video decode on this device requires tier %d support, "
355 "but it is not implemented.\n", feature.DecodeTier);
359 ctx->reference_only_map =
NULL;
360 ctx->ref_only_resources =
NULL;
361 if (feature.ConfigurationFlags & D3D12_VIDEO_DECODE_CONFIGURATION_FLAG_REFERENCE_ONLY_ALLOCATIONS_REQUIRED) {
362 av_log(avctx,
AV_LOG_VERBOSE,
"Reference-Only Allocations are required for this D3D12 decoder configuration.\n");
364 if (!
ctx->reference_only_map)
366 ctx->ref_only_resources =
av_calloc(
ctx->max_num_ref,
sizeof(*
ctx->ref_only_resources));
367 if (!
ctx->ref_only_resources)
371 desc = (D3D12_VIDEO_DECODER_DESC) {
373 .Configuration =
ctx->cfg,
377 (
void **)&
ctx->decoder));
380 if (!
ctx->decoder_ref)
411 ID3D12CommandAllocator *command_allocator =
NULL;
412 D3D12_COMMAND_QUEUE_DESC queue_desc = {
413 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
415 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
428 if (frames_ctx->
format !=
ctx->pix_fmt) {
444 if (!
ctx->ref_resources)
448 if (!
ctx->ref_subresources)
453 if (!
ctx->objects_queue)
456 DX_CHECK(ID3D12Device_CreateFence(
ctx->device_ctx->device, 0, D3D12_FENCE_FLAG_NONE,
457 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence));
459 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
460 if (!
ctx->sync_ctx.event)
467 DX_CHECK(ID3D12Device_CreateCommandQueue(
ctx->device_ctx->device, &queue_desc,
468 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue));
470 DX_CHECK(ID3D12Device_CreateCommandList(
ctx->device_ctx->device, 0, queue_desc.Type,
471 command_allocator,
NULL, &IID_ID3D12CommandList, (
void **)&
ctx->command_list));
473 DX_CHECK(ID3D12VideoDecodeCommandList_Close(
ctx->command_list));
475 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
497 int num_allocator = 0;
501 if (
ctx->sync_ctx.fence)
510 if (
ctx->objects_queue) {
517 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
524 if (
ctx->sync_ctx.event)
525 CloseHandle(
ctx->sync_ctx.event);
535 ID3D12Resource *current_resource,
int state_before,
int state_end)
538 ID3D12Resource **ref_resources =
ctx->ref_only_resources ?
ctx->ref_only_resources :
ctx->ref_resources;
541 for (
int i = 0;
i <
ctx->max_num_ref;
i++) {
542 if (((
ctx->used_mask >>
i) & 0x1) && ref_resources[
i] && ref_resources[
i] != current_resource) {
543 barriers[num_barrier].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
544 barriers[num_barrier].Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
545 barriers[num_barrier].Transition = (D3D12_RESOURCE_TRANSITION_BARRIER) {
546 .pResource = ref_resources[
i],
547 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
548 .StateBefore = state_before,
549 .StateAfter = state_end,
559 const void *pp,
unsigned pp_size,
560 const void *qm,
unsigned qm_size,
566 ID3D12CommandAllocator *command_allocator =
NULL;
568 ID3D12Resource *output_resource = (ID3D12Resource*)
f->texture;
569 ID3D12Resource *ref_resource =
NULL;
571 ID3D12VideoDecodeCommandList *cmd_list =
ctx->command_list;
572 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
574 D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS input_args = {
575 .NumFrameArguments = 2,
578 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_PICTURE_PARAMETERS,
583 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_INVERSE_QUANTIZATION_MATRIX,
588 .pHeap =
ctx->decoder_heap,
591 D3D12_VIDEO_DECODE_OUTPUT_STREAM_ARGUMENTS output_args = {
592 .ConversionArguments = { 0 },
593 .OutputSubresource = 0,
594 .pOutputTexture2D = output_resource,
597 memset(
ctx->ref_subresources, 0,
sizeof(UINT) *
ctx->max_num_ref);
598 input_args.ReferenceFrames.NumTexture2Ds =
ctx->max_num_ref;
599 input_args.ReferenceFrames.pSubresources =
ctx->ref_subresources;
601 if (
ctx->reference_only_map) {
603 if (ref_resource ==
NULL) {
609 output_args.ConversionArguments.Enable = 1;
610 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_only_resources;
611 output_args.ConversionArguments.pReferenceTexture2D = ref_resource;
612 output_args.ConversionArguments.ReferenceSubresource = 0;
614 ref_resource = output_resource;
615 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_resources;
618 UINT num_barrier = 1;
619 barriers[0] = (D3D12_RESOURCE_BARRIER) {
620 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
621 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
623 .pResource = output_resource,
624 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
625 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
626 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
630 if (
ctx->reference_only_map) {
631 barriers[1] = (D3D12_RESOURCE_BARRIER) {
632 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
633 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
635 .pResource = ref_resource,
636 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
637 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
638 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
649 input_args.NumFrameArguments = 1;
659 DX_CHECK(ID3D12CommandAllocator_Reset(command_allocator));
661 DX_CHECK(ID3D12VideoDecodeCommandList_Reset(cmd_list, command_allocator));
665 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
667 ID3D12VideoDecodeCommandList_DecodeFrame(cmd_list,
ctx->decoder, &output_args, &input_args);
669 for (
int i = 0;
i < num_barrier;
i++)
670 FFSWAP(D3D12_RESOURCE_STATES, barriers[
i].Transition.StateBefore, barriers[
i].Transition.StateAfter);
672 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
674 DX_CHECK(ID3D12VideoDecodeCommandList_Close(cmd_list));
676 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
678 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
f->sync_ctx.fence, ++
f->sync_ctx.fence_value));
680 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
689 if (command_allocator)
static AVFormatContext * ctx
Libavcodec external API header.
#define i(width, name, range_min, range_max)
common internal and external API header
static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
static void free_reference_only_resources(AVCodecContext *avctx)
av_cold int ff_d3d12va_decode_init(AVCodecContext *avctx)
init D3D12VADecodeContext
static int d3d12va_fence_completion(AVD3D12VASyncContext *psync_ctx)
static void prepare_reference_only_resources(AVCodecContext *avctx)
static int d3d12va_sync_with_gpu(AVCodecContext *avctx)
int ff_d3d12va_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
d3d12va common frame params
static int d3d12va_create_decoder_heap(AVCodecContext *avctx)
static AVBufferRef * bufref_wrap_interface(IUnknown *iface)
static ID3D12Resource * get_reference_only_resource(AVCodecContext *avctx, ID3D12Resource *output_resource)
av_cold int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
uninit D3D12VADecodeContext
static int d3d12va_discard_helper_objects(AVCodecContext *avctx, ID3D12CommandAllocator *pAllocator, ID3D12Resource *pBuffer, uint64_t fence_value)
static int d3d12va_get_valid_helper_objects(AVCodecContext *avctx, ID3D12CommandAllocator **ppAllocator, ID3D12Resource **ppBuffer)
int ff_d3d12va_get_suitable_max_bitstream_size(AVCodecContext *avctx)
Get a suitable maximum bitstream size.
static int d3d12va_create_decoder(AVCodecContext *avctx)
unsigned ff_d3d12va_get_surface_index(const AVCodecContext *avctx, D3D12VADecodeContext *ctx, const AVFrame *frame, int curr)
int ff_d3d12va_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*update_input_arguments)(AVCodecContext *, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *, ID3D12Resource *))
d3d12va common end frame
static int d3d12va_update_reference_frames_state(AVCodecContext *avctx, D3D12_RESOURCE_BARRIER *barriers, ID3D12Resource *current_resource, int state_before, int state_end)
static void bufref_free_interface(void *opaque, uint8_t *data)
#define D3D12VA_VIDEO_DEC_ASYNC_DEPTH
#define D3D12VA_DECODE_CONTEXT(avctx)
#define D3D12VA_FRAMES_CONTEXT(avctx)
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, enum AVHWDeviceType dev_type)
Make sure avctx.hw_frames_ctx is set.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
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.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
@ AV_HWDEVICE_TYPE_D3D12VA
An API-specific header for AV_HWDEVICE_TYPE_D3D12VA.
#define D3D12_OBJECT_RELEASE(pInterface)
A release macro used by D3D12 objects highly frequently.
#define DX_CHECK(hr)
A check macro used by D3D12 functions highly frequently.
#define FFSWAP(type, a, b)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
#define AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P12
@ 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_D3D12
Hardware surfaces for Direct3D 12.
A reference to a data buffer.
uint8_t * data
The data buffer.
main external API structure.
int width
picture width / height.
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
int64_t bit_rate
the average bitrate
int coded_width
Bitstream width / height, may be different from width/height e.g.
This struct is allocated as AVHWDeviceContext.hwctx.
ID3D12Device * device
Device used for objects creation and access.
ID3D12VideoDevice * video_device
If unset, this will be set from the device field on init.
D3D12VA frame descriptor for pool allocation.
This struct is allocated as AVHWFramesContext.hwctx.
DXGI_FORMAT format
DXGI_FORMAT format.
This struct is used to sync d3d12 execution.
HANDLE event
A handle to the event object that's raised when the fence reaches a certain value.
ID3D12Fence * fence
D3D12 fence object.
uint64_t fence_value
The fence value used for sync.
This structure describes decoded (raw) audio or video data.
enum AVPixelFormat pix_fmt
Supported pixel format.
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
This struct describes a set or pool of "hardware" frames (i.e.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int width
The allocated dimensions of the frames in this pool.
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
This structure is used to provide the necessary configurations and data to the FFmpeg Direct3D 12 HWA...
ID3D12CommandAllocator * command_allocator
ID3D12Resource * output_resource
ID3D12Resource * resource