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)
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)
407 ID3D12CommandAllocator *command_allocator =
NULL;
408 D3D12_COMMAND_QUEUE_DESC queue_desc = {
409 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
411 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
440 if (!
ctx->ref_resources)
444 if (!
ctx->ref_subresources)
449 if (!
ctx->objects_queue)
452 DX_CHECK(ID3D12Device_CreateFence(
ctx->device_ctx->device, 0, D3D12_FENCE_FLAG_NONE,
453 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence));
455 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
456 if (!
ctx->sync_ctx.event)
463 DX_CHECK(ID3D12Device_CreateCommandQueue(
ctx->device_ctx->device, &queue_desc,
464 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue));
466 DX_CHECK(ID3D12Device_CreateCommandList(
ctx->device_ctx->device, 0, queue_desc.Type,
467 command_allocator,
NULL, &IID_ID3D12CommandList, (
void **)&
ctx->command_list));
469 DX_CHECK(ID3D12VideoDecodeCommandList_Close(
ctx->command_list));
471 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
493 int num_allocator = 0;
497 if (
ctx->sync_ctx.fence)
506 if (
ctx->objects_queue) {
513 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
520 if (
ctx->sync_ctx.event)
521 CloseHandle(
ctx->sync_ctx.event);
531 ID3D12Resource *current_resource,
int state_before,
int state_end)
534 ID3D12Resource **ref_resources =
ctx->ref_only_resources ?
ctx->ref_only_resources :
ctx->ref_resources;
537 for (
int i = 0;
i <
ctx->max_num_ref;
i++) {
538 if (((
ctx->used_mask >>
i) & 0x1) && ref_resources[
i] && ref_resources[
i] != current_resource) {
539 barriers[num_barrier].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
540 barriers[num_barrier].Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
541 barriers[num_barrier].Transition = (D3D12_RESOURCE_TRANSITION_BARRIER) {
542 .pResource = ref_resources[
i],
543 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
544 .StateBefore = state_before,
545 .StateAfter = state_end,
555 const void *pp,
unsigned pp_size,
556 const void *qm,
unsigned qm_size,
562 ID3D12CommandAllocator *command_allocator =
NULL;
564 ID3D12Resource *output_resource = (ID3D12Resource*)
f->texture;
565 ID3D12Resource *ref_resource =
NULL;
567 ID3D12VideoDecodeCommandList *cmd_list =
ctx->command_list;
568 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
570 D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS input_args = {
571 .NumFrameArguments = 2,
574 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_PICTURE_PARAMETERS,
579 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_INVERSE_QUANTIZATION_MATRIX,
584 .pHeap =
ctx->decoder_heap,
587 D3D12_VIDEO_DECODE_OUTPUT_STREAM_ARGUMENTS output_args = {
588 .ConversionArguments = { 0 },
589 .OutputSubresource = 0,
590 .pOutputTexture2D = output_resource,
593 memset(
ctx->ref_subresources, 0,
sizeof(UINT) *
ctx->max_num_ref);
594 input_args.ReferenceFrames.NumTexture2Ds =
ctx->max_num_ref;
595 input_args.ReferenceFrames.pSubresources =
ctx->ref_subresources;
597 if (
ctx->reference_only_map) {
599 if (ref_resource ==
NULL) {
605 output_args.ConversionArguments.Enable = 1;
606 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_only_resources;
607 output_args.ConversionArguments.pReferenceTexture2D = ref_resource;
608 output_args.ConversionArguments.ReferenceSubresource = 0;
610 ref_resource = output_resource;
611 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_resources;
614 UINT num_barrier = 1;
615 barriers[0] = (D3D12_RESOURCE_BARRIER) {
616 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
617 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
619 .pResource = output_resource,
620 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
621 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
622 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
626 if (
ctx->reference_only_map) {
627 barriers[1] = (D3D12_RESOURCE_BARRIER) {
628 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
629 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
631 .pResource = ref_resource,
632 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
633 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
634 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
645 input_args.NumFrameArguments = 1;
655 DX_CHECK(ID3D12CommandAllocator_Reset(command_allocator));
657 DX_CHECK(ID3D12VideoDecodeCommandList_Reset(cmd_list, command_allocator));
661 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
663 ID3D12VideoDecodeCommandList_DecodeFrame(cmd_list,
ctx->decoder, &output_args, &input_args);
665 for (
int i = 0;
i < num_barrier;
i++)
666 FFSWAP(D3D12_RESOURCE_STATES, barriers[
i].Transition.StateBefore, barriers[
i].Transition.StateAfter);
668 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
670 DX_CHECK(ID3D12VideoDecodeCommandList_Close(cmd_list));
672 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
674 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
f->sync_ctx.fence, ++
f->sync_ctx.fence_value));
676 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
685 if (command_allocator)