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 };
290 if (
ctx->codec->init_picture_params) {
291 err =
ctx->codec->init_picture_params(avctx, pic);
294 "parameters: %d.\n", err);
300 if (
ctx->codec->write_sequence_header) {
301 bit_len = 8 *
sizeof(
data);
302 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
305 "header: %d.\n", err);
325 d3d12_refs.NumTexture2Ds = base_pic->
nb_refs[0] + base_pic->
nb_refs[1];
326 if (d3d12_refs.NumTexture2Ds) {
327 d3d12_refs.ppTexture2Ds =
av_calloc(d3d12_refs.NumTexture2Ds,
328 sizeof(*d3d12_refs.ppTexture2Ds));
329 if (!d3d12_refs.ppTexture2Ds) {
335 for (j = 0; j < base_pic->
nb_refs[0]; j++)
337 for (j = 0; j < base_pic->
nb_refs[1]; j++)
341 input_args.PictureControlDesc.IntraRefreshFrameIndex = 0;
343 input_args.PictureControlDesc.Flags |= D3D12_VIDEO_ENCODER_PICTURE_CONTROL_FLAG_USED_AS_REFERENCE_PICTURE;
345 input_args.PictureControlDesc.PictureControlCodecData = pic->
pic_ctl;
346 input_args.PictureControlDesc.ReferenceFrames = d3d12_refs;
352 output_args.ReconstructedPicture.ReconstructedPictureSubresource = 0;
354 output_args.EncoderOutputMetadata.Offset = 0;
357 input_metadata.HWLayoutMetadata.Offset = 0;
360 output_metadata.ResolvedLayoutMetadata.Offset = 0;
366 hr = ID3D12CommandAllocator_Reset(command_allocator);
372 hr = ID3D12VideoEncodeCommandList2_Reset(cmd_list, command_allocator);
378 #define TRANSITION_BARRIER(res, before, after) \
379 (D3D12_RESOURCE_BARRIER) { \
380 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, \
381 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE, \
384 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, \
385 .StateBefore = before, \
386 .StateAfter = after, \
391 D3D12_RESOURCE_STATE_COMMON,
392 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
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 D3D12_RESOURCE_STATE_COMMON,
401 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
403 D3D12_RESOURCE_STATE_COMMON,
404 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
406 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 5, barriers);
408 if (d3d12_refs.NumTexture2Ds) {
409 D3D12_RESOURCE_BARRIER refs_barriers[3];
411 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
413 D3D12_RESOURCE_STATE_COMMON,
414 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
416 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, d3d12_refs.NumTexture2Ds,
420 ID3D12VideoEncodeCommandList2_EncodeFrame(cmd_list,
ctx->encoder,
ctx->encoder_heap,
421 &input_args, &output_args);
424 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
425 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
427 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 1, &barriers[3]);
429 ID3D12VideoEncodeCommandList2_ResolveEncoderOutputMetadata(cmd_list, &input_metadata, &output_metadata);
431 if (d3d12_refs.NumTexture2Ds) {
432 D3D12_RESOURCE_BARRIER refs_barriers[3];
434 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
436 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
437 D3D12_RESOURCE_STATE_COMMON);
439 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, d3d12_refs.NumTexture2Ds,
444 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
445 D3D12_RESOURCE_STATE_COMMON);
447 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
448 D3D12_RESOURCE_STATE_COMMON);
450 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
451 D3D12_RESOURCE_STATE_COMMON);
453 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
454 D3D12_RESOURCE_STATE_COMMON);
456 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
457 D3D12_RESOURCE_STATE_COMMON);
459 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 5, barriers);
461 hr = ID3D12VideoEncodeCommandList2_Close(cmd_list);
474 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
483 hr = ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value);
495 if (d3d12_refs.ppTexture2Ds)
501 if (command_allocator)
504 if (d3d12_refs.ppTexture2Ds)
507 if (
ctx->codec->free_picture_params)
508 ctx->codec->free_picture_params(pic);
525 "%"PRId64
"/%"PRId64
".\n",
542 switch (
ctx->rc.Mode)
544 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP:
545 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CQP);
547 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR:
548 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CBR);
550 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR:
551 av_freep(&
ctx->rc.ConfigParams.pConfiguration_VBR);
553 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR:
554 av_freep(&
ctx->rc.ConfigParams.pConfiguration_QVBR);
573 if (
ctx->codec->picture_priv_data_size > 0) {
595 if (
ctx->codec->free_picture_params)
596 ctx->codec->free_picture_params(pic);
608 D3D12_VIDEO_ENCODER_OUTPUT_METADATA *meta =
NULL;
619 meta = (D3D12_VIDEO_ENCODER_OUTPUT_METADATA *)
data;
621 if (meta->EncodeErrorFlags != D3D12_VIDEO_ENCODER_ENCODE_ERROR_FLAG_NO_ERROR) {
627 if (meta->EncodedBitstreamWrittenBytesCount == 0) {
633 *
size = meta->EncodedBitstreamWrittenBytesCount;
644 uint8_t *ptr, *mapped_data;
645 size_t total_size = 0;
672 memcpy(ptr, mapped_data, total_size);
722 depth =
desc->comp[0].depth;
723 for (
i = 1;
i <
desc->nb_components;
i++) {
724 if (
desc->comp[
i].depth != depth) {
734 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
740 if (
desc->nb_components > 1 &&
764 {
RC_MODE_CQP,
"CQP", 0, 0, 1, 0, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP },
765 {
RC_MODE_CBR,
"CBR", 1, 0, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR },
766 {
RC_MODE_VBR,
"VBR", 1, 1, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR },
767 {
RC_MODE_QVBR,
"QVBR", 1, 1, 1, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR },
774 D3D12_FEATURE_DATA_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_rc_mode = {
775 .Codec =
ctx->codec->d3d12_codec,
781 d3d12_rc_mode.IsSupported = 0;
782 d3d12_rc_mode.RateControlMode =
rc_mode->d3d12_mode;
784 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
785 D3D12_FEATURE_VIDEO_ENCODER_RATE_CONTROL_MODE,
786 &d3d12_rc_mode,
sizeof(d3d12_rc_mode));
792 return d3d12_rc_mode.IsSupported;
802 int64_t hrd_initial_buffer_fullness;
818 #define TRY_RC_MODE(mode, fail) do { \
819 rc_mode = &d3d12va_encode_rc_modes[mode]; \
820 if (!(rc_mode->d3d12_mode && check_rate_control_support(avctx, rc_mode))) { \
822 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
823 "RC mode.\n", rc_mode->name); \
824 return AVERROR(EINVAL); \
826 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
827 "RC mode.\n", rc_mode->name); \
830 goto rc_mode_found; \
834 if (
ctx->explicit_rc_mode)
837 if (
ctx->explicit_qp)
864 "RC mode compatible with selected options.\n");
879 "bitrate (%"PRId64
") must not be greater than "
880 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
884 rc_target_bitrate = avctx->
bit_rate;
891 rc_target_bitrate = avctx->
bit_rate;
892 rc_peak_bitrate = 2 * avctx->
bit_rate;
897 "in %s RC mode.\n",
rc_mode->name);
899 rc_target_bitrate = avctx->
bit_rate;
903 rc_target_bitrate = 0;
908 if (
ctx->explicit_qp) {
909 rc_quality =
ctx->explicit_qp;
916 rc_quality =
ctx->codec->default_quality;
918 "using default (%d).\n", rc_quality);
934 "must have initial buffer size (%d) <= "
935 "buffer size (%"PRId64
").\n",
941 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
946 "in %s RC mode.\n",
rc_mode->name);
950 hrd_initial_buffer_fullness = 0;
953 if (rc_target_bitrate > UINT32_MAX ||
954 hrd_buffer_size > UINT32_MAX ||
955 hrd_initial_buffer_fullness > UINT32_MAX) {
957 "greater are not supported by D3D12.\n");
961 ctx->rc_quality = rc_quality;
970 "initial fullness %"PRId64
" bits.\n",
971 hrd_buffer_size, hrd_initial_buffer_fullness);
982 fr_num, fr_den, (
double)fr_num / fr_den);
984 ctx->rc.Flags = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_NONE;
985 ctx->rc.TargetFrameRate.Numerator = fr_num;
986 ctx->rc.TargetFrameRate.Denominator = fr_den;
995 D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR *cbr_ctl;
997 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR);
1002 cbr_ctl->TargetBitRate = rc_target_bitrate;
1003 cbr_ctl->VBVCapacity = hrd_buffer_size;
1004 cbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1005 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1007 if (avctx->
qmin > 0 || avctx->
qmax > 0) {
1008 cbr_ctl->MinQP = avctx->
qmin;
1009 cbr_ctl->MaxQP = avctx->
qmax;
1010 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
1013 ctx->rc.ConfigParams.pConfiguration_CBR = cbr_ctl;
1017 D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR *vbr_ctl;
1019 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR);
1024 vbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1025 vbr_ctl->PeakBitRate = rc_peak_bitrate;
1026 vbr_ctl->VBVCapacity = hrd_buffer_size;
1027 vbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1028 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1030 if (avctx->
qmin > 0 || avctx->
qmax > 0) {
1031 vbr_ctl->MinQP = avctx->
qmin;
1032 vbr_ctl->MaxQP = avctx->
qmax;
1033 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
1036 ctx->rc.ConfigParams.pConfiguration_VBR = vbr_ctl;
1040 D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR *qvbr_ctl;
1042 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR);
1047 qvbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1048 qvbr_ctl->PeakBitRate = rc_peak_bitrate;
1049 qvbr_ctl->ConstantQualityTarget = rc_quality;
1051 if (avctx->
qmin > 0 || avctx->
qmax > 0) {
1052 qvbr_ctl->MinQP = avctx->
qmin;
1053 qvbr_ctl->MaxQP = avctx->
qmax;
1054 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
1057 ctx->rc.ConfigParams.pConfiguration_QVBR = qvbr_ctl;
1070 uint32_t ref_l0, ref_l1;
1073 D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT support;
1075 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_H264 h264;
1076 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_HEVC hevc;
1079 support.NodeIndex = 0;
1080 support.Codec =
ctx->codec->d3d12_codec;
1081 support.Profile =
ctx->profile->d3d12_profile;
1083 switch (
ctx->codec->d3d12_codec) {
1084 case D3D12_VIDEO_ENCODER_CODEC_H264:
1085 support.PictureSupport.DataSize =
sizeof(codec_support.h264);
1086 support.PictureSupport.pH264Support = &codec_support.h264;
1089 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1090 support.PictureSupport.DataSize =
sizeof(codec_support.hevc);
1091 support.PictureSupport.pHEVCSupport = &codec_support.hevc;
1098 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT,
1099 &support,
sizeof(support));
1103 if (support.IsSupported) {
1104 switch (
ctx->codec->d3d12_codec) {
1105 case D3D12_VIDEO_ENCODER_CODEC_H264:
1106 ref_l0 =
FFMIN(support.PictureSupport.pH264Support->MaxL0ReferencesForP,
1107 support.PictureSupport.pH264Support->MaxL1ReferencesForB);
1108 ref_l1 = support.PictureSupport.pH264Support->MaxL1ReferencesForB;
1111 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1112 ref_l0 =
FFMIN(support.PictureSupport.pHEVCSupport->MaxL0ReferencesForP,
1113 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB);
1114 ref_l1 = support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB;
1121 ref_l0 = ref_l1 = 0;
1124 if (ref_l0 > 0 && ref_l1 > 0 &&
ctx->bi_not_empty) {
1127 "replacing them with B-frames.\n");
1144 D3D12_VIDEO_ENCODER_DESC
desc = {
1146 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1147 .EncodeCodec =
ctx->codec->d3d12_codec,
1148 .EncodeProfile =
ctx->profile->d3d12_profile,
1149 .InputFormat = frames_hwctx->
format,
1150 .CodecConfiguration =
ctx->codec_conf,
1151 .MaxMotionEstimationPrecision = D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_MAXIMUM,
1154 hr = ID3D12VideoDevice3_CreateVideoEncoder(
ctx->video_device3, &
desc, &IID_ID3D12VideoEncoder,
1155 (
void **)&
ctx->encoder);
1169 D3D12_VIDEO_ENCODER_HEAP_DESC
desc = {
1171 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1172 .EncodeCodec =
ctx->codec->d3d12_codec,
1173 .EncodeProfile =
ctx->profile->d3d12_profile,
1174 .EncodeLevel =
ctx->level,
1175 .ResolutionsListCount = 1,
1176 .pResolutionList = &
ctx->resolution,
1179 hr = ID3D12VideoDevice3_CreateVideoEncoderHeap(
ctx->video_device3, &
desc,
1180 &IID_ID3D12VideoEncoderHeap, (
void **)&
ctx->encoder_heap);
1191 ID3D12Resource *pResource;
1193 pResource = (ID3D12Resource *)
data;
1202 ID3D12Resource *pResource =
NULL;
1205 D3D12_HEAP_PROPERTIES heap_props;
1206 D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_READBACK;
1208 D3D12_RESOURCE_DESC
desc = {
1209 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
1212 D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT),
1214 .DepthOrArraySize = 1,
1216 .Format = DXGI_FORMAT_UNKNOWN,
1217 .SampleDesc = { .Count = 1, .Quality = 0 },
1218 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
1219 .Flags = D3D12_RESOURCE_FLAG_NONE,
1222 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &heap_props, 0, heap_type);
1224 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
1225 &
desc, D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource,
1226 (
void **)&pResource);
1252 ctx->req.NodeIndex = 0;
1253 ctx->req.Codec =
ctx->codec->d3d12_codec;
1254 ctx->req.Profile =
ctx->profile->d3d12_profile;
1255 ctx->req.InputFormat = frames_ctx->
format;
1256 ctx->req.PictureTargetResolution =
ctx->resolution;
1258 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1259 D3D12_FEATURE_VIDEO_ENCODER_RESOURCE_REQUIREMENTS,
1260 &
ctx->req,
sizeof(
ctx->req));
1262 av_log(avctx,
AV_LOG_ERROR,
"Failed to check encoder resource requirements support.\n");
1266 if (!
ctx->req.IsSupported) {
1273 if (!
ctx->output_buffer_pool)
1282 ID3D12CommandAllocator *command_allocator =
NULL;
1286 D3D12_COMMAND_QUEUE_DESC queue_desc = {
1287 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
1289 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
1295 if (!
ctx->allocator_queue)
1298 hr = ID3D12Device_CreateFence(
ctx->hwctx->device, 0, D3D12_FENCE_FLAG_NONE,
1299 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence);
1306 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
1307 if (!
ctx->sync_ctx.event)
1314 hr = ID3D12Device_CreateCommandQueue(
ctx->hwctx->device, &queue_desc,
1315 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue);
1322 hr = ID3D12Device_CreateCommandList(
ctx->hwctx->device, 0, queue_desc.Type,
1323 command_allocator,
NULL, &IID_ID3D12CommandList,
1324 (
void **)&
ctx->command_list);
1331 hr = ID3D12VideoEncodeCommandList2_Close(
ctx->command_list);
1338 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
1378 hwctx->
flags = D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY |
1379 D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
1384 "frame context: %d.\n", err);
1410 D3D12_FEATURE_DATA_VIDEO_FEATURE_AREA_SUPPORT support = { 0 };
1425 hr = ID3D12Device_QueryInterface(
ctx->hwctx->device, &IID_ID3D12Device3, (
void **)&
ctx->device3);
1432 hr = ID3D12Device3_QueryInterface(
ctx->device3, &IID_ID3D12VideoDevice3, (
void **)&
ctx->video_device3);
1439 if (FAILED(ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_FEATURE_AREA_SUPPORT,
1440 &support,
sizeof(support))) && !support.VideoEncodeSupport) {
1454 if (
ctx->codec->get_encoder_caps) {
1455 err =
ctx->codec->get_encoder_caps(avctx);
1466 "but this codec does not support controlling slices.\n");
1481 if (
ctx->codec->configure) {
1482 err =
ctx->codec->configure(avctx);
1487 if (
ctx->codec->init_sequence_params) {
1488 err =
ctx->codec->init_sequence_params(avctx);
1491 "failed: %d.\n", err);
1496 if (
ctx->codec->set_level) {
1497 err =
ctx->codec->set_level(avctx);
1527 int num_allocator = 0;
1533 if (!base_ctx->
frame)
1536 for (pic = base_ctx->
pic_start; pic; pic = next) {
1548 if (
ctx->allocator_queue) {
1554 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
1560 if (
ctx->sync_ctx.event)
1561 CloseHandle(
ctx->sync_ctx.event);