65 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
66 if (
ctx->ref_resources[
i] && res ==
ctx->ref_resources[
i]) {
67 ctx->used_mask |= 1 <<
i;
73 for (
i = 0;
i <
ctx->max_num_ref;
i++) {
74 if (!((
ctx->used_mask >>
i) & 0x1)) {
75 ctx->ref_resources[
i] = res;
87 ID3D12Resource **ppBuffer)
92 D3D12_HEAP_PROPERTIES heap_props = { .Type = D3D12_HEAP_TYPE_UPLOAD };
94 D3D12_RESOURCE_DESC
desc = {
95 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
96 .Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT,
97 .Width =
ctx->bitstream_size,
99 .DepthOrArraySize = 1,
101 .Format = DXGI_FORMAT_UNKNOWN,
102 .SampleDesc = { .Count = 1, .Quality = 0 },
103 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
104 .Flags = D3D12_RESOURCE_FLAG_NONE,
108 uint64_t completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
117 hr = ID3D12Device_CreateCommandAllocator(
ctx->device_ctx->device, D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
118 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
124 hr = ID3D12Device_CreateCommittedResource(
ctx->device_ctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
125 &
desc, D3D12_RESOURCE_STATE_GENERIC_READ,
NULL,
126 &IID_ID3D12Resource, (
void **)ppBuffer);
137 ID3D12Resource *pBuffer, uint64_t fence_value)
144 .fence_value = fence_value,
158 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
159 if (completion < psync_ctx->fence_value) {
160 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
163 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
183 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
197 D3D12_VIDEO_DECODER_HEAP_DESC
desc = {
199 .Configuration =
ctx->cfg,
200 .DecodeWidth = frames_ctx->
width,
201 .DecodeHeight = frames_ctx->
height,
202 .Format = frames_hwctx->
format,
205 .MaxDecodePictureBufferCount =
ctx->max_num_ref,
209 &IID_ID3D12VideoDecoderHeap, (
void **)&
ctx->decoder_heap));
216 "[width(%d), height(%d)], on your device!\n", frames_ctx->
width, frames_ctx->
height);
224 D3D12_VIDEO_DECODER_DESC
desc;
230 D3D12_FEATURE_DATA_VIDEO_DECODE_SUPPORT feature = {
232 .Configuration =
ctx->cfg,
233 .Width = frames_ctx->
width,
234 .Height = frames_ctx->
height,
235 .DecodeFormat = frames_hwctx->
format,
240 DX_CHECK(ID3D12VideoDevice_CheckFeatureSupport(device_hwctx->
video_device, D3D12_FEATURE_VIDEO_DECODE_SUPPORT,
241 &feature,
sizeof(feature)));
242 if (!(feature.SupportFlags & D3D12_VIDEO_DECODE_SUPPORT_FLAG_SUPPORTED)) {
246 if (!(feature.DecodeTier >= D3D12_VIDEO_DECODE_TIER_2)) {
247 av_log(avctx,
AV_LOG_ERROR,
"D3D12 video decode on this device requires tier %d support, "
248 "but it is not implemented.\n", feature.DecodeTier);
252 desc = (D3D12_VIDEO_DECODER_DESC) {
254 .Configuration =
ctx->cfg,
258 (
void **)&
ctx->decoder));
261 if (!
ctx->decoder_ref)
288 ID3D12CommandAllocator *command_allocator =
NULL;
289 D3D12_COMMAND_QUEUE_DESC queue_desc = {
290 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE,
292 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
321 if (!
ctx->ref_resources)
325 if (!
ctx->ref_subresources)
330 if (!
ctx->objects_queue)
333 DX_CHECK(ID3D12Device_CreateFence(
ctx->device_ctx->device, 0, D3D12_FENCE_FLAG_NONE,
334 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence));
336 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
337 if (!
ctx->sync_ctx.event)
344 DX_CHECK(ID3D12Device_CreateCommandQueue(
ctx->device_ctx->device, &queue_desc,
345 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue));
347 DX_CHECK(ID3D12Device_CreateCommandList(
ctx->device_ctx->device, 0, queue_desc.Type,
348 command_allocator,
NULL, &IID_ID3D12CommandList, (
void **)&
ctx->command_list));
350 DX_CHECK(ID3D12VideoDecodeCommandList_Close(
ctx->command_list));
352 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
374 int num_allocator = 0;
378 if (
ctx->sync_ctx.fence)
387 if (
ctx->objects_queue) {
394 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
400 if (
ctx->sync_ctx.event)
401 CloseHandle(
ctx->sync_ctx.event);
411 ID3D12Resource *current_resource,
int state_before,
int state_end)
416 for (
int i = 0;
i <
ctx->max_num_ref;
i++) {
417 if (((
ctx->used_mask >>
i) & 0x1) &&
ctx->ref_resources[
i] &&
ctx->ref_resources[
i] != current_resource) {
418 barriers[num_barrier].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
419 barriers[num_barrier].Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
420 barriers[num_barrier].Transition = (D3D12_RESOURCE_TRANSITION_BARRIER){
421 .pResource =
ctx->ref_resources[
i],
422 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
423 .StateBefore = state_before,
424 .StateAfter = state_end,
434 const void *pp,
unsigned pp_size,
435 const void *qm,
unsigned qm_size,
441 ID3D12CommandAllocator *command_allocator =
NULL;
443 ID3D12Resource *resource = (ID3D12Resource *)
f->texture;
445 ID3D12VideoDecodeCommandList *cmd_list =
ctx->command_list;
446 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
448 D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS input_args = {
449 .NumFrameArguments = 2,
452 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_PICTURE_PARAMETERS,
457 .Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_INVERSE_QUANTIZATION_MATRIX,
462 .pHeap =
ctx->decoder_heap,
465 D3D12_VIDEO_DECODE_OUTPUT_STREAM_ARGUMENTS output_args = {
466 .ConversionArguments = { 0 },
467 .OutputSubresource = 0,
468 .pOutputTexture2D = resource,
471 UINT num_barrier = 1;
472 barriers[0] = (D3D12_RESOURCE_BARRIER) {
473 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
474 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
476 .pResource = resource,
477 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
478 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
479 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
483 memset(
ctx->ref_subresources, 0,
sizeof(UINT) *
ctx->max_num_ref);
484 input_args.ReferenceFrames.NumTexture2Ds =
ctx->max_num_ref;
485 input_args.ReferenceFrames.ppTexture2Ds =
ctx->ref_resources;
486 input_args.ReferenceFrames.pSubresources =
ctx->ref_subresources;
493 input_args.NumFrameArguments = 1;
503 DX_CHECK(ID3D12CommandAllocator_Reset(command_allocator));
505 DX_CHECK(ID3D12VideoDecodeCommandList_Reset(cmd_list, command_allocator));
509 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
511 ID3D12VideoDecodeCommandList_DecodeFrame(cmd_list,
ctx->decoder, &output_args, &input_args);
513 for (
int i = 0;
i < num_barrier;
i++)
514 FFSWAP(D3D12_RESOURCE_STATES, barriers[
i].Transition.StateBefore, barriers[
i].Transition.StateAfter);
516 ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier, barriers);
518 DX_CHECK(ID3D12VideoDecodeCommandList_Close(cmd_list));
520 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
522 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
f->sync_ctx.fence, ++
f->sync_ctx.fence_value));
524 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
533 if (command_allocator)