43 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
44 if (completion < psync_ctx->fence_value) {
45 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
48 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
58 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
77 uint64_t completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
85 hr = ID3D12Device_CreateCommandAllocator(
ctx->hwctx->device, D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
86 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
101 .fence_value = fence_value,
123 completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
124 if (completion < pic->fence_value) {
125 if (FAILED(ID3D12Fence_SetEventOnCompletion(
ctx->sync_ctx.fence, pic->
fence_value,
126 ctx->sync_ctx.event)))
129 WaitForSingleObjectEx(
ctx->sync_ctx.event, INFINITE, FALSE);
146 int width =
sizeof(D3D12_VIDEO_ENCODER_OUTPUT_METADATA) +
sizeof(D3D12_VIDEO_ENCODER_FRAME_SUBREGION_METADATA);
147 D3D12_HEAP_PROPERTIES encoded_meta_props = { .Type = D3D12_HEAP_TYPE_DEFAULT }, resolved_meta_props;
148 D3D12_HEAP_TYPE resolved_heap_type = D3D12_HEAP_TYPE_READBACK;
151 D3D12_RESOURCE_DESC meta_desc = {
152 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
154 .Width =
ctx->req.MaxEncoderOutputMetadataBufferSize,
156 .DepthOrArraySize = 1,
158 .Format = DXGI_FORMAT_UNKNOWN,
159 .SampleDesc = { .Count = 1, .Quality = 0 },
160 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
161 .Flags = D3D12_RESOURCE_FLAG_NONE,
164 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &encoded_meta_props, D3D12_HEAP_FLAG_NONE,
165 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
172 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &resolved_meta_props, 0, resolved_heap_type);
174 meta_desc.Width =
width;
176 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &resolved_meta_props, D3D12_HEAP_FLAG_NONE,
177 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
200 ID3D12CommandAllocator *command_allocator =
NULL;
201 ID3D12VideoEncodeCommandList2 *cmd_list =
ctx->command_list;
202 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
203 D3D12_VIDEO_ENCODE_REFERENCE_FRAMES d3d12_refs = { 0 };
205 D3D12_VIDEO_ENCODER_ENCODEFRAME_INPUT_ARGUMENTS input_args = {
206 .SequenceControlDesc = {
207 .Flags = D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_NONE,
208 .IntraRefreshConfig = { 0 },
209 .RateControl =
ctx->rc,
210 .PictureTargetResolution =
ctx->resolution,
211 .SelectedLayoutMode = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
212 .FrameSubregionsLayoutData = { 0 },
213 .CodecGopSequence =
ctx->gop,
216 .InputFrameSubresource = 0,
219 D3D12_VIDEO_ENCODER_ENCODEFRAME_OUTPUT_ARGUMENTS output_args = { 0 };
221 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_INPUT_ARGUMENTS input_metadata = {
222 .EncoderCodec =
ctx->codec->d3d12_codec,
223 .EncoderProfile =
ctx->profile->d3d12_profile,
224 .EncoderInputFormat = frames_hwctx->
format,
225 .EncodedPictureEffectiveResolution =
ctx->resolution,
228 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_OUTPUT_ARGUMENTS output_metadata = { 0 };
284 if (
ctx->codec->init_picture_params) {
285 err =
ctx->codec->init_picture_params(avctx, base_pic);
288 "parameters: %d.\n", err);
294 if (
ctx->codec->write_sequence_header) {
295 bit_len = 8 *
sizeof(
data);
296 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
299 "header: %d.\n", err);
319 d3d12_refs.NumTexture2Ds = base_pic->
nb_refs[0] + base_pic->
nb_refs[1];
320 if (d3d12_refs.NumTexture2Ds) {
321 d3d12_refs.ppTexture2Ds =
av_calloc(d3d12_refs.NumTexture2Ds,
322 sizeof(*d3d12_refs.ppTexture2Ds));
323 if (!d3d12_refs.ppTexture2Ds) {
329 for (j = 0; j < base_pic->
nb_refs[0]; j++)
331 for (j = 0; j < base_pic->
nb_refs[1]; j++)
335 input_args.PictureControlDesc.IntraRefreshFrameIndex = 0;
337 input_args.PictureControlDesc.Flags |= D3D12_VIDEO_ENCODER_PICTURE_CONTROL_FLAG_USED_AS_REFERENCE_PICTURE;
339 input_args.PictureControlDesc.PictureControlCodecData = pic->
pic_ctl;
340 input_args.PictureControlDesc.ReferenceFrames = d3d12_refs;
346 output_args.ReconstructedPicture.ReconstructedPictureSubresource = 0;
348 output_args.EncoderOutputMetadata.Offset = 0;
351 input_metadata.HWLayoutMetadata.Offset = 0;
354 output_metadata.ResolvedLayoutMetadata.Offset = 0;
360 hr = ID3D12CommandAllocator_Reset(command_allocator);
366 hr = ID3D12VideoEncodeCommandList2_Reset(cmd_list, command_allocator);
372 #define TRANSITION_BARRIER(res, before, after) \
373 (D3D12_RESOURCE_BARRIER) { \
374 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, \
375 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE, \
378 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, \
379 .StateBefore = before, \
380 .StateAfter = after, \
385 D3D12_RESOURCE_STATE_COMMON,
386 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
388 D3D12_RESOURCE_STATE_COMMON,
389 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
391 D3D12_RESOURCE_STATE_COMMON,
392 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
394 D3D12_RESOURCE_STATE_COMMON,
395 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
397 D3D12_RESOURCE_STATE_COMMON,
398 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
400 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 5, barriers);
402 if (d3d12_refs.NumTexture2Ds) {
403 D3D12_RESOURCE_BARRIER refs_barriers[3];
405 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
407 D3D12_RESOURCE_STATE_COMMON,
408 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
410 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, d3d12_refs.NumTexture2Ds,
414 ID3D12VideoEncodeCommandList2_EncodeFrame(cmd_list,
ctx->encoder,
ctx->encoder_heap,
415 &input_args, &output_args);
418 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
419 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
421 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 1, &barriers[3]);
423 ID3D12VideoEncodeCommandList2_ResolveEncoderOutputMetadata(cmd_list, &input_metadata, &output_metadata);
425 if (d3d12_refs.NumTexture2Ds) {
426 D3D12_RESOURCE_BARRIER refs_barriers[3];
428 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
430 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
431 D3D12_RESOURCE_STATE_COMMON);
433 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, d3d12_refs.NumTexture2Ds,
438 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
439 D3D12_RESOURCE_STATE_COMMON);
441 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
442 D3D12_RESOURCE_STATE_COMMON);
444 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
445 D3D12_RESOURCE_STATE_COMMON);
447 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
448 D3D12_RESOURCE_STATE_COMMON);
450 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
451 D3D12_RESOURCE_STATE_COMMON);
453 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 5, barriers);
455 hr = ID3D12VideoEncodeCommandList2_Close(cmd_list);
468 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
477 hr = ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value);
489 if (d3d12_refs.ppTexture2Ds)
495 if (command_allocator)
498 if (d3d12_refs.ppTexture2Ds)
501 if (
ctx->codec->free_picture_params)
502 ctx->codec->free_picture_params(pic);
520 "%"PRId64
"/%"PRId64
".\n",
537 switch (
ctx->rc.Mode)
539 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP:
540 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CQP);
542 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR:
543 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CBR);
545 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR:
546 av_freep(&
ctx->rc.ConfigParams.pConfiguration_VBR);
548 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR:
549 av_freep(&
ctx->rc.ConfigParams.pConfiguration_QVBR);
564 if (
ctx->codec->picture_priv_data_size > 0) {
583 if (
ctx->codec->free_picture_params)
584 ctx->codec->free_picture_params(priv);
592 D3D12_VIDEO_ENCODER_OUTPUT_METADATA *meta =
NULL;
603 meta = (D3D12_VIDEO_ENCODER_OUTPUT_METADATA *)
data;
605 if (meta->EncodeErrorFlags != D3D12_VIDEO_ENCODER_ENCODE_ERROR_FLAG_NO_ERROR) {
611 if (meta->EncodedBitstreamWrittenBytesCount == 0) {
617 *
size = meta->EncodedBitstreamWrittenBytesCount;
628 uint8_t *ptr, *mapped_data;
629 size_t total_size = 0;
656 memcpy(ptr, mapped_data, total_size);
706 depth =
desc->comp[0].depth;
707 for (
i = 1;
i <
desc->nb_components;
i++) {
708 if (
desc->comp[
i].depth != depth) {
718 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
724 if (
desc->nb_components > 1 &&
748 {
RC_MODE_CQP,
"CQP", 0, 0, 1, 0, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP },
749 {
RC_MODE_CBR,
"CBR", 1, 0, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR },
750 {
RC_MODE_VBR,
"VBR", 1, 1, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR },
751 {
RC_MODE_QVBR,
"QVBR", 1, 1, 1, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR },
758 D3D12_FEATURE_DATA_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_rc_mode = {
759 .Codec =
ctx->codec->d3d12_codec,
765 d3d12_rc_mode.IsSupported = 0;
766 d3d12_rc_mode.RateControlMode =
rc_mode->d3d12_mode;
768 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
769 D3D12_FEATURE_VIDEO_ENCODER_RATE_CONTROL_MODE,
770 &d3d12_rc_mode,
sizeof(d3d12_rc_mode));
776 return d3d12_rc_mode.IsSupported;
786 int64_t hrd_initial_buffer_fullness;
802 #define TRY_RC_MODE(mode, fail) do { \
803 rc_mode = &d3d12va_encode_rc_modes[mode]; \
804 if (!(rc_mode->d3d12_mode && check_rate_control_support(avctx, rc_mode))) { \
806 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
807 "RC mode.\n", rc_mode->name); \
808 return AVERROR(EINVAL); \
810 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
811 "RC mode.\n", rc_mode->name); \
814 goto rc_mode_found; \
818 if (
ctx->explicit_rc_mode)
821 if (
ctx->explicit_qp)
848 "RC mode compatible with selected options.\n");
863 "bitrate (%"PRId64
") must not be greater than "
864 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
868 rc_target_bitrate = avctx->
bit_rate;
875 rc_target_bitrate = avctx->
bit_rate;
876 rc_peak_bitrate = 2 * avctx->
bit_rate;
881 "in %s RC mode.\n",
rc_mode->name);
883 rc_target_bitrate = avctx->
bit_rate;
887 rc_target_bitrate = 0;
892 if (
ctx->explicit_qp) {
893 rc_quality =
ctx->explicit_qp;
900 rc_quality =
ctx->codec->default_quality;
902 "using default (%d).\n", rc_quality);
918 "must have initial buffer size (%d) <= "
919 "buffer size (%"PRId64
").\n",
925 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
930 "in %s RC mode.\n",
rc_mode->name);
934 hrd_initial_buffer_fullness = 0;
937 if (rc_target_bitrate > UINT32_MAX ||
938 hrd_buffer_size > UINT32_MAX ||
939 hrd_initial_buffer_fullness > UINT32_MAX) {
941 "greater are not supported by D3D12.\n");
945 ctx->rc_quality = rc_quality;
954 "initial fullness %"PRId64
" bits.\n",
955 hrd_buffer_size, hrd_initial_buffer_fullness);
966 fr_num, fr_den, (
double)fr_num / fr_den);
968 ctx->rc.Flags = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_NONE;
969 ctx->rc.TargetFrameRate.Numerator = fr_num;
970 ctx->rc.TargetFrameRate.Denominator = fr_den;
979 D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR *cbr_ctl;
981 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR);
986 cbr_ctl->TargetBitRate = rc_target_bitrate;
987 cbr_ctl->VBVCapacity = hrd_buffer_size;
988 cbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
989 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
991 if (avctx->
qmin > 0 || avctx->
qmax > 0) {
992 cbr_ctl->MinQP = avctx->
qmin;
993 cbr_ctl->MaxQP = avctx->
qmax;
994 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
997 ctx->rc.ConfigParams.pConfiguration_CBR = cbr_ctl;
1001 D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR *vbr_ctl;
1003 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR);
1008 vbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1009 vbr_ctl->PeakBitRate = rc_peak_bitrate;
1010 vbr_ctl->VBVCapacity = hrd_buffer_size;
1011 vbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1012 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1014 if (avctx->
qmin > 0 || avctx->
qmax > 0) {
1015 vbr_ctl->MinQP = avctx->
qmin;
1016 vbr_ctl->MaxQP = avctx->
qmax;
1017 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
1020 ctx->rc.ConfigParams.pConfiguration_VBR = vbr_ctl;
1024 D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR *qvbr_ctl;
1026 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR);
1031 qvbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1032 qvbr_ctl->PeakBitRate = rc_peak_bitrate;
1033 qvbr_ctl->ConstantQualityTarget = rc_quality;
1035 if (avctx->
qmin > 0 || avctx->
qmax > 0) {
1036 qvbr_ctl->MinQP = avctx->
qmin;
1037 qvbr_ctl->MaxQP = avctx->
qmax;
1038 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
1041 ctx->rc.ConfigParams.pConfiguration_QVBR = qvbr_ctl;
1054 uint32_t ref_l0, ref_l1;
1057 D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT support;
1059 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_H264 h264;
1060 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_HEVC hevc;
1063 support.NodeIndex = 0;
1064 support.Codec =
ctx->codec->d3d12_codec;
1065 support.Profile =
ctx->profile->d3d12_profile;
1067 switch (
ctx->codec->d3d12_codec) {
1068 case D3D12_VIDEO_ENCODER_CODEC_H264:
1069 support.PictureSupport.DataSize =
sizeof(codec_support.h264);
1070 support.PictureSupport.pH264Support = &codec_support.h264;
1073 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1074 support.PictureSupport.DataSize =
sizeof(codec_support.hevc);
1075 support.PictureSupport.pHEVCSupport = &codec_support.hevc;
1082 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT,
1083 &support,
sizeof(support));
1087 if (support.IsSupported) {
1088 switch (
ctx->codec->d3d12_codec) {
1089 case D3D12_VIDEO_ENCODER_CODEC_H264:
1090 ref_l0 =
FFMIN(support.PictureSupport.pH264Support->MaxL0ReferencesForP,
1091 support.PictureSupport.pH264Support->MaxL1ReferencesForB);
1092 ref_l1 = support.PictureSupport.pH264Support->MaxL1ReferencesForB;
1095 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1096 ref_l0 =
FFMIN(support.PictureSupport.pHEVCSupport->MaxL0ReferencesForP,
1097 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB);
1098 ref_l1 = support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB;
1105 ref_l0 = ref_l1 = 0;
1108 if (ref_l0 > 0 && ref_l1 > 0 &&
ctx->bi_not_empty) {
1111 "replacing them with B-frames.\n");
1128 D3D12_VIDEO_ENCODER_DESC
desc = {
1130 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1131 .EncodeCodec =
ctx->codec->d3d12_codec,
1132 .EncodeProfile =
ctx->profile->d3d12_profile,
1133 .InputFormat = frames_hwctx->
format,
1134 .CodecConfiguration =
ctx->codec_conf,
1135 .MaxMotionEstimationPrecision = D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_MAXIMUM,
1138 hr = ID3D12VideoDevice3_CreateVideoEncoder(
ctx->video_device3, &
desc, &IID_ID3D12VideoEncoder,
1139 (
void **)&
ctx->encoder);
1153 D3D12_VIDEO_ENCODER_HEAP_DESC
desc = {
1155 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1156 .EncodeCodec =
ctx->codec->d3d12_codec,
1157 .EncodeProfile =
ctx->profile->d3d12_profile,
1158 .EncodeLevel =
ctx->level,
1159 .ResolutionsListCount = 1,
1160 .pResolutionList = &
ctx->resolution,
1163 hr = ID3D12VideoDevice3_CreateVideoEncoderHeap(
ctx->video_device3, &
desc,
1164 &IID_ID3D12VideoEncoderHeap, (
void **)&
ctx->encoder_heap);
1175 ID3D12Resource *pResource;
1177 pResource = (ID3D12Resource *)
data;
1186 ID3D12Resource *pResource =
NULL;
1189 D3D12_HEAP_PROPERTIES heap_props;
1190 D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_READBACK;
1192 D3D12_RESOURCE_DESC
desc = {
1193 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
1196 D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT),
1198 .DepthOrArraySize = 1,
1200 .Format = DXGI_FORMAT_UNKNOWN,
1201 .SampleDesc = { .Count = 1, .Quality = 0 },
1202 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
1203 .Flags = D3D12_RESOURCE_FLAG_NONE,
1206 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &heap_props, 0, heap_type);
1208 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
1209 &
desc, D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource,
1210 (
void **)&pResource);
1236 ctx->req.NodeIndex = 0;
1237 ctx->req.Codec =
ctx->codec->d3d12_codec;
1238 ctx->req.Profile =
ctx->profile->d3d12_profile;
1239 ctx->req.InputFormat = frames_ctx->
format;
1240 ctx->req.PictureTargetResolution =
ctx->resolution;
1242 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1243 D3D12_FEATURE_VIDEO_ENCODER_RESOURCE_REQUIREMENTS,
1244 &
ctx->req,
sizeof(
ctx->req));
1246 av_log(avctx,
AV_LOG_ERROR,
"Failed to check encoder resource requirements support.\n");
1250 if (!
ctx->req.IsSupported) {
1257 if (!
ctx->output_buffer_pool)
1266 ID3D12CommandAllocator *command_allocator =
NULL;
1270 D3D12_COMMAND_QUEUE_DESC queue_desc = {
1271 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
1273 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
1279 if (!
ctx->allocator_queue)
1282 hr = ID3D12Device_CreateFence(
ctx->hwctx->device, 0, D3D12_FENCE_FLAG_NONE,
1283 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence);
1290 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
1291 if (!
ctx->sync_ctx.event)
1298 hr = ID3D12Device_CreateCommandQueue(
ctx->hwctx->device, &queue_desc,
1299 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue);
1306 hr = ID3D12Device_CreateCommandList(
ctx->hwctx->device, 0, queue_desc.Type,
1307 command_allocator,
NULL, &IID_ID3D12CommandList,
1308 (
void **)&
ctx->command_list);
1315 hr = ID3D12VideoEncodeCommandList2_Close(
ctx->command_list);
1322 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
1362 hwctx->
flags = D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY |
1363 D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
1368 "frame context: %d.\n", err);
1396 D3D12_FEATURE_DATA_VIDEO_FEATURE_AREA_SUPPORT support = { 0 };
1411 hr = ID3D12Device_QueryInterface(
ctx->hwctx->device, &IID_ID3D12Device3, (
void **)&
ctx->device3);
1418 hr = ID3D12Device3_QueryInterface(
ctx->device3, &IID_ID3D12VideoDevice3, (
void **)&
ctx->video_device3);
1425 if (FAILED(ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_FEATURE_AREA_SUPPORT,
1426 &support,
sizeof(support))) && !support.VideoEncodeSupport) {
1440 if (
ctx->codec->get_encoder_caps) {
1441 err =
ctx->codec->get_encoder_caps(avctx);
1452 "but this codec does not support controlling slices.\n");
1467 if (
ctx->codec->configure) {
1468 err =
ctx->codec->configure(avctx);
1473 if (
ctx->codec->init_sequence_params) {
1474 err =
ctx->codec->init_sequence_params(avctx);
1477 "failed: %d.\n", err);
1482 if (
ctx->codec->set_level) {
1483 err =
ctx->codec->set_level(avctx);
1513 int num_allocator = 0;
1519 if (!base_ctx->
frame)
1522 for (pic = base_ctx->
pic_start; pic; pic = next) {
1534 if (
ctx->allocator_queue) {
1540 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
1546 if (
ctx->sync_ctx.event)
1547 CloseHandle(
ctx->sync_ctx.event);