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) {
65 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
66 if (!reference_only_map[
i].used && reference_only_map[
i].resource !=
NULL) {
67 reference_only_map[
i].
used = 1;
68 resource = reference_only_map[
i].
resource;
75 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
76 if (reference_only_map[
i].resource ==
NULL)
80 if (
i ==
ctx->max_num_ref) {
86 output_resource->lpVtbl->GetDesc(output_resource, &
desc);
87 desc.Flags = D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
89 if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->
device, &props, D3D12_HEAP_FLAG_NONE, &
desc,
90 D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource, (
void **)&reference_only_map[
i].
resource))) {
95 reference_only_map[
i].
used = 1;
96 resource = reference_only_map[
i].
resource;
107 if (reference_only_map !=
NULL) {
108 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
109 if (reference_only_map[
i].resource !=
NULL) {
123 if (reference_only_map ==
NULL)
125 memset(
ctx->ref_only_resources, 0,
ctx->max_num_ref *
sizeof(*(
ctx->ref_only_resources)));
126 for (j = 0; j <
ctx->max_num_ref; j++) {
127 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
128 if (reference_only_map[j].used && reference_only_map[j].output_resource ==
ctx->ref_resources[
i]) {
129 ctx->ref_only_resources[
i] = reference_only_map[j].
resource;
133 if (
i ==
ctx->max_num_ref)
134 reference_only_map[j].
used = 0;
160 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
161 if (
ctx->ref_resources[
i] && res ==
ctx->ref_resources[
i]) {
162 ctx->used_mask |= 1 <<
i;
168 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
169 if (!((
ctx->used_mask >>
i) & 0x1)) {
170 ctx->ref_resources[
i] = res;
182 ID3D12Resource **ppBuffer)
187 D3D12_HEAP_PROPERTIES heap_props = { .Type = D3D12_HEAP_TYPE_UPLOAD };
189 D3D12_RESOURCE_DESC
desc = {
190 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
191 .Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT,
192 .Width =
ctx->bitstream_size,
194 .DepthOrArraySize = 1,
196 .Format = DXGI_FORMAT_UNKNOWN,
197 .SampleDesc = { .Count = 1, .Quality = 0 },
198 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
199 .Flags = D3D12_RESOURCE_FLAG_NONE,
203 uint64_t completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
212 hr = ID3D12Device_CreateCommandAllocator(
ctx->device_ctx->device, D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
213 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
219 hr = ID3D12Device_CreateCommittedResource(
ctx->device_ctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
220 &
desc, D3D12_RESOURCE_STATE_GENERIC_READ,
NULL,
221 &IID_ID3D12Resource, (
void **)ppBuffer);
232 ID3D12Resource *pBuffer, uint64_t fence_value)
239 .fence_value = fence_value,
253 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
254 if (completion < psync_ctx->fence_value) {
255 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
258 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
278 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
292 D3D12_VIDEO_DECODER_HEAP_DESC
desc = {
294 .Configuration =
ctx->cfg,
295 .DecodeWidth = frames_ctx->
width,
296 .DecodeHeight = frames_ctx->
height,
297 .Format = frames_hwctx->
format,
300 .MaxDecodePictureBufferCount =
ctx->max_num_ref,
304 &IID_ID3D12VideoDecoderHeap, (
void **)&
ctx->decoder_heap));
311 "[width(%d), height(%d)], on your device!\n", frames_ctx->
width, frames_ctx->
height);
319 D3D12_VIDEO_DECODER_DESC
desc;
325 D3D12_FEATURE_DATA_VIDEO_DECODE_SUPPORT feature = {
327 .Configuration =
ctx->cfg,
328 .Width = frames_ctx->
width,
329 .Height = frames_ctx->
height,
330 .DecodeFormat = frames_hwctx->
format,
335 DX_CHECK(ID3D12VideoDevice_CheckFeatureSupport(device_hwctx->
video_device, D3D12_FEATURE_VIDEO_DECODE_SUPPORT,
336 &feature,
sizeof(feature)));
337 if (!(feature.SupportFlags & D3D12_VIDEO_DECODE_SUPPORT_FLAG_SUPPORTED)) {
341 if (!(feature.DecodeTier >= D3D12_VIDEO_DECODE_TIER_2)) {
342 av_log(avctx,
AV_LOG_ERROR,
"D3D12 video decode on this device requires tier %d support, "
343 "but it is not implemented.\n", feature.DecodeTier);
347 ctx->reference_only_map =
NULL;
348 ctx->ref_only_resources =
NULL;
349 if (feature.ConfigurationFlags & D3D12_VIDEO_DECODE_CONFIGURATION_FLAG_REFERENCE_ONLY_ALLOCATIONS_REQUIRED) {
350 av_log(avctx,
AV_LOG_VERBOSE,
"Reference-Only Allocations are required for this D3D12 decoder configuration.\n");
352 if (!
ctx->reference_only_map)
354 ctx->ref_only_resources =
av_calloc(
ctx->max_num_ref,
sizeof(*
ctx->ref_only_resources));
355 if (!
ctx->ref_only_resources)
359 desc = (D3D12_VIDEO_DECODER_DESC) {
361 .Configuration =
ctx->cfg,
365 (
void **)&
ctx->decoder));
368 if (!
ctx->decoder_ref)
395 ID3D12CommandAllocator *command_allocator =
NULL;
396 D3D12_COMMAND_QUEUE_DESC queue_desc = {
397 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
399 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
428 if (!
ctx->ref_resources)
432 if (!
ctx->ref_subresources)
437 if (!
ctx->objects_queue)
440 DX_CHECK(ID3D12Device_CreateFence(
ctx->device_ctx->device, 0, D3D12_FENCE_FLAG_NONE,
441 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence));
443 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
444 if (!
ctx->sync_ctx.event)
451 DX_CHECK(ID3D12Device_CreateCommandQueue(
ctx->device_ctx->device, &queue_desc,
452 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue));
454 DX_CHECK(ID3D12Device_CreateCommandList(
ctx->device_ctx->device, 0, queue_desc.Type,
455 command_allocator,
NULL, &IID_ID3D12CommandList, (
void **)&
ctx->command_list));
457 DX_CHECK(ID3D12VideoDecodeCommandList_Close(
ctx->command_list));
459 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
481 int num_allocator = 0;
485 if (
ctx->sync_ctx.fence)
494 if (
ctx->objects_queue) {
501 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
508 if (
ctx->sync_ctx.event)
509 CloseHandle(
ctx->sync_ctx.event);
519 ID3D12Resource *current_resource,
int state_before,
int state_end)
522 ID3D12Resource **ref_resources =
ctx->ref_only_resources ?
ctx->ref_only_resources :
ctx->ref_resources;
525 for (
int i = 0;
i <
ctx->max_num_ref;
i++) {
526 if (((
ctx->used_mask >>
i) & 0x1) && ref_resources[
i] && ref_resources[
i] != current_resource) {
527 barriers[num_barrier].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
528 barriers[num_barrier].Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
529 barriers[num_barrier].Transition = (D3D12_RESOURCE_TRANSITION_BARRIER) {
530 .pResource = ref_resources[
i],
531 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
532 .StateBefore = state_before,
533 .StateAfter = state_end,
543 const void *pp,
unsigned pp_size,
544 const void *qm,
unsigned qm_size,
550 ID3D12CommandAllocator *command_allocator =
NULL;
552 ID3D12Resource *output_resource = (ID3D12Resource*)
f->texture;
553 ID3D12Resource *ref_resource =
NULL;
555 ID3D12VideoDecodeCommandList *cmd_list =
ctx->command_list;
556 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
558 D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS input_args = {
559 .NumFrameArguments = 2,
562 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_PICTURE_PARAMETERS,
567 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_INVERSE_QUANTIZATION_MATRIX,
572 .pHeap =
ctx->decoder_heap,
575 D3D12_VIDEO_DECODE_OUTPUT_STREAM_ARGUMENTS output_args = {
576 .ConversionArguments = { 0 },
577 .OutputSubresource = 0,
578 .pOutputTexture2D = output_resource,
581 memset(
ctx->ref_subresources, 0,
sizeof(UINT) *
ctx->max_num_ref);
582 input_args.ReferenceFrames.NumTexture2Ds =
ctx->max_num_ref;
583 input_args.ReferenceFrames.pSubresources =
ctx->ref_subresources;
585 if (
ctx->reference_only_map) {
587 if (ref_resource ==
NULL) {
593 output_args.ConversionArguments.Enable = 1;
594 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_only_resources;
595 output_args.ConversionArguments.pReferenceTexture2D = ref_resource;
596 output_args.ConversionArguments.ReferenceSubresource = 0;
598 ref_resource = output_resource;
599 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_resources;
602 UINT num_barrier = 1;
603 barriers[0] = (D3D12_RESOURCE_BARRIER) {
604 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
605 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
607 .pResource = output_resource,
608 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
609 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
610 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
614 if (
ctx->reference_only_map) {
615 barriers[1] = (D3D12_RESOURCE_BARRIER) {
616 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
617 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
619 .pResource = ref_resource,
620 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
621 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
622 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
633 input_args.NumFrameArguments = 1;
643 DX_CHECK(ID3D12CommandAllocator_Reset(command_allocator));
645 DX_CHECK(ID3D12VideoDecodeCommandList_Reset(cmd_list, command_allocator));
649 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
651 ID3D12VideoDecodeCommandList_DecodeFrame(cmd_list,
ctx->decoder, &output_args, &input_args);
653 for (
int i = 0;
i < num_barrier;
i++)
654 FFSWAP(D3D12_RESOURCE_STATES, barriers[
i].Transition.StateBefore, barriers[
i].Transition.StateAfter);
656 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
658 DX_CHECK(ID3D12VideoDecodeCommandList_Close(cmd_list));
660 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
662 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
f->sync_ctx.fence, ++
f->sync_ctx.fence_value));
664 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
673 if (command_allocator)