32 #include "config_components.h"
44 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
45 if (completion < psync_ctx->fence_value) {
46 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
49 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
59 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
78 uint64_t completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
86 hr = ID3D12Device_CreateCommandAllocator(
ctx->hwctx->device, D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
87 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
102 .fence_value = fence_value,
124 completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
125 if (completion < pic->fence_value) {
126 if (FAILED(ID3D12Fence_SetEventOnCompletion(
ctx->sync_ctx.fence, pic->
fence_value,
127 ctx->sync_ctx.event)))
130 WaitForSingleObjectEx(
ctx->sync_ctx.event, INFINITE, FALSE);
147 int width =
sizeof(D3D12_VIDEO_ENCODER_OUTPUT_METADATA) +
sizeof(D3D12_VIDEO_ENCODER_FRAME_SUBREGION_METADATA);
148 #if CONFIG_AV1_D3D12VA_ENCODER
149 if (
ctx->codec->d3d12_codec == D3D12_VIDEO_ENCODER_CODEC_AV1) {
150 width +=
sizeof(D3D12_VIDEO_ENCODER_AV1_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_TILES)
151 +
sizeof(D3D12_VIDEO_ENCODER_AV1_POST_ENCODE_VALUES);
154 D3D12_HEAP_PROPERTIES encoded_meta_props = { .Type = D3D12_HEAP_TYPE_DEFAULT }, resolved_meta_props;
155 D3D12_HEAP_TYPE resolved_heap_type = D3D12_HEAP_TYPE_READBACK;
158 D3D12_RESOURCE_DESC meta_desc = {
159 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
161 .Width =
ctx->req.MaxEncoderOutputMetadataBufferSize,
163 .DepthOrArraySize = 1,
165 .Format = DXGI_FORMAT_UNKNOWN,
166 .SampleDesc = { .Count = 1, .Quality = 0 },
167 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
168 .Flags = D3D12_RESOURCE_FLAG_NONE,
171 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &encoded_meta_props, D3D12_HEAP_FLAG_NONE,
172 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
179 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &resolved_meta_props, 0, resolved_heap_type);
181 meta_desc.Width =
width;
183 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &resolved_meta_props, D3D12_HEAP_FLAG_NONE,
184 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
207 ID3D12CommandAllocator *command_allocator =
NULL;
208 ID3D12VideoEncodeCommandList2 *cmd_list =
ctx->command_list;
209 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
210 D3D12_VIDEO_ENCODE_REFERENCE_FRAMES d3d12_refs = { 0 };
211 int barriers_ref_index = 0;
212 D3D12_RESOURCE_BARRIER *barriers_ref =
NULL;
214 D3D12_VIDEO_ENCODER_ENCODEFRAME_INPUT_ARGUMENTS input_args = {
215 .SequenceControlDesc = {
216 .Flags = D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_NONE,
217 .IntraRefreshConfig = { 0 },
218 .RateControl =
ctx->rc,
219 .PictureTargetResolution =
ctx->resolution,
220 .SelectedLayoutMode = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
221 .FrameSubregionsLayoutData =
ctx->subregions_layout,
222 .CodecGopSequence =
ctx->gop,
225 .InputFrameSubresource = 0,
228 D3D12_VIDEO_ENCODER_ENCODEFRAME_OUTPUT_ARGUMENTS output_args = { 0 };
230 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_INPUT_ARGUMENTS input_metadata = {
231 .EncoderCodec =
ctx->codec->d3d12_codec,
232 .EncoderProfile =
ctx->profile->d3d12_profile,
233 .EncoderInputFormat = frames_hwctx->
format,
234 .EncodedPictureEffectiveResolution =
ctx->resolution,
237 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_OUTPUT_ARGUMENTS output_metadata = { 0 };
295 if (
ctx->codec->init_picture_params) {
296 err =
ctx->codec->init_picture_params(avctx, base_pic);
299 "parameters: %d.\n", err);
305 if (
ctx->codec->write_sequence_header) {
306 bit_len = 8 *
sizeof(
data);
307 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
310 "header: %d.\n", err);
329 d3d12_refs.NumTexture2Ds = base_pic->
nb_refs[0] + base_pic->
nb_refs[1];
330 if (d3d12_refs.NumTexture2Ds) {
331 d3d12_refs.ppTexture2Ds =
av_calloc(d3d12_refs.NumTexture2Ds,
332 sizeof(*d3d12_refs.ppTexture2Ds));
333 if (!d3d12_refs.ppTexture2Ds) {
338 if (
ctx->is_texture_array) {
339 d3d12_refs.pSubresources =
av_calloc(d3d12_refs.NumTexture2Ds,
340 sizeof(*d3d12_refs.pSubresources));
341 if (!d3d12_refs.pSubresources) {
348 for (j = 0; j < base_pic->
nb_refs[0]; j++) {
350 if (
ctx->is_texture_array)
354 for (j = 0; j < base_pic->
nb_refs[1]; j++) {
356 if (
ctx->is_texture_array)
362 input_args.PictureControlDesc.IntraRefreshFrameIndex = 0;
364 input_args.PictureControlDesc.Flags |= D3D12_VIDEO_ENCODER_PICTURE_CONTROL_FLAG_USED_AS_REFERENCE_PICTURE;
366 input_args.PictureControlDesc.PictureControlCodecData = pic->
pic_ctl;
367 input_args.PictureControlDesc.ReferenceFrames = d3d12_refs;
373 output_args.ReconstructedPicture.ReconstructedPictureSubresource =
ctx->is_texture_array ? pic->
subresource_index : 0;
375 output_args.EncoderOutputMetadata.Offset = 0;
378 input_metadata.HWLayoutMetadata.Offset = 0;
381 output_metadata.ResolvedLayoutMetadata.Offset = 0;
387 hr = ID3D12CommandAllocator_Reset(command_allocator);
393 hr = ID3D12VideoEncodeCommandList2_Reset(cmd_list, command_allocator);
399 #define TRANSITION_BARRIER(res, subres, before, after) \
400 (D3D12_RESOURCE_BARRIER) { \
401 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, \
402 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE, \
405 .Subresource = subres, \
406 .StateBefore = before, \
407 .StateAfter = after, \
412 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
413 D3D12_RESOURCE_STATE_COMMON,
414 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
416 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
417 D3D12_RESOURCE_STATE_COMMON,
418 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
420 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
421 D3D12_RESOURCE_STATE_COMMON,
422 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
424 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
425 D3D12_RESOURCE_STATE_COMMON,
426 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
428 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 4, barriers);
430 if (
ctx->is_texture_array)
432 sizeof(D3D12_RESOURCE_BARRIER));
436 if (
ctx->is_texture_array) {
437 D3D12_RESOURCE_DESC references_tex_array_desc = { 0 };
440 for (uint32_t reference_subresource = 0; reference_subresource < references_tex_array_desc.DepthOrArraySize;
441 reference_subresource++) {
443 uint32_t array_size = references_tex_array_desc.DepthOrArraySize;
444 uint32_t mip_slice = reference_subresource % references_tex_array_desc.MipLevels;
445 uint32_t array_slice = (reference_subresource / references_tex_array_desc.MipLevels) % array_size;
447 for (uint32_t plane_slice = 0; plane_slice <
ctx->plane_count; plane_slice++) {
448 uint32_t outputSubresource = mip_slice + array_slice * references_tex_array_desc.MipLevels +
449 plane_slice * references_tex_array_desc.MipLevels * array_size;
452 D3D12_RESOURCE_STATE_COMMON,
453 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
456 D3D12_RESOURCE_STATE_COMMON,
457 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
463 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
464 D3D12_RESOURCE_STATE_COMMON,
465 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
467 if (d3d12_refs.NumTexture2Ds) {
468 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
470 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
471 D3D12_RESOURCE_STATE_COMMON,
472 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
475 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, barriers_ref_index, barriers_ref);
477 ID3D12VideoEncodeCommandList2_EncodeFrame(cmd_list,
ctx->encoder,
ctx->encoder_heap,
478 &input_args, &output_args);
481 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
482 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
483 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
485 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 1, &barriers[3]);
487 ID3D12VideoEncodeCommandList2_ResolveEncoderOutputMetadata(cmd_list, &input_metadata, &output_metadata);
489 if (barriers_ref_index > 0) {
490 for (
i = 0;
i < barriers_ref_index;
i++)
491 FFSWAP(D3D12_RESOURCE_STATES, barriers_ref[
i].Transition.StateBefore, barriers_ref[
i].Transition.StateAfter);
493 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, barriers_ref_index,
498 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
499 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
500 D3D12_RESOURCE_STATE_COMMON);
502 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
503 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
504 D3D12_RESOURCE_STATE_COMMON);
506 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
507 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
508 D3D12_RESOURCE_STATE_COMMON);
510 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
511 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
512 D3D12_RESOURCE_STATE_COMMON);
514 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 4, barriers);
516 hr = ID3D12VideoEncodeCommandList2_Close(cmd_list);
529 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
538 hr = ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value);
550 if (d3d12_refs.ppTexture2Ds)
553 if (
ctx->is_texture_array && d3d12_refs.pSubresources)
554 av_freep(&d3d12_refs.pSubresources);
562 if (command_allocator)
565 if (d3d12_refs.ppTexture2Ds)
568 if (
ctx->is_texture_array && d3d12_refs.pSubresources)
569 av_freep(&d3d12_refs.pSubresources);
574 if (
ctx->codec->free_picture_params)
575 ctx->codec->free_picture_params(pic);
593 "%"PRId64
"/%"PRId64
".\n",
610 switch (
ctx->rc.Mode)
612 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP:
613 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CQP);
615 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR:
616 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CBR);
618 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR:
619 av_freep(&
ctx->rc.ConfigParams.pConfiguration_VBR);
621 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR:
622 av_freep(&
ctx->rc.ConfigParams.pConfiguration_QVBR);
637 if (
ctx->codec->picture_priv_data_size > 0) {
656 if (
ctx->codec->free_picture_params)
657 ctx->codec->free_picture_params(priv);
665 D3D12_VIDEO_ENCODER_OUTPUT_METADATA *meta =
NULL;
676 meta = (D3D12_VIDEO_ENCODER_OUTPUT_METADATA *)
data;
678 if (meta->EncodeErrorFlags != D3D12_VIDEO_ENCODER_ENCODE_ERROR_FLAG_NO_ERROR) {
684 if (meta->EncodedBitstreamWrittenBytesCount == 0) {
690 *
size = meta->EncodedBitstreamWrittenBytesCount;
701 uint8_t *ptr, *mapped_data;
702 size_t total_size = 0;
729 memcpy(ptr, mapped_data, total_size);
752 if (
ctx->codec->get_coded_data)
753 err =
ctx->codec->get_coded_data(avctx, pic,
pkt);
784 depth =
desc->comp[0].depth;
785 for (
i = 1;
i <
desc->nb_components;
i++) {
786 if (
desc->comp[
i].depth != depth) {
796 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
802 if (
desc->nb_components > 1 &&
826 {
RC_MODE_CQP,
"CQP", 0, 0, 1, 0, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP },
827 {
RC_MODE_CBR,
"CBR", 1, 0, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR },
828 {
RC_MODE_VBR,
"VBR", 1, 1, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR },
829 {
RC_MODE_QVBR,
"QVBR", 1, 1, 1, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR },
836 D3D12_FEATURE_DATA_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_rc_mode = {
837 .Codec =
ctx->codec->d3d12_codec,
843 d3d12_rc_mode.IsSupported = 0;
844 d3d12_rc_mode.RateControlMode =
rc_mode->d3d12_mode;
846 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
847 D3D12_FEATURE_VIDEO_ENCODER_RATE_CONTROL_MODE,
848 &d3d12_rc_mode,
sizeof(d3d12_rc_mode));
854 return d3d12_rc_mode.IsSupported;
864 int64_t hrd_initial_buffer_fullness;
868 #define SET_QP_RANGE(ctl) do { \
869 if (avctx->qmin > 0 || avctx->qmax > 0) { \
870 ctl->MinQP = avctx->qmin; \
871 ctl->MaxQP = avctx->qmax; \
872 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE; \
876 #define SET_MAX_FRAME_SIZE(ctl) do { \
877 if (ctx->max_frame_size > 0) { \
878 ctl->MaxFrameBitSize = ctx->max_frame_size * 8; \
879 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE; \
895 #define TRY_RC_MODE(mode, fail) do { \
896 rc_mode = &d3d12va_encode_rc_modes[mode]; \
897 if (!(rc_mode->d3d12_mode && check_rate_control_support(avctx, rc_mode))) { \
899 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
900 "RC mode.\n", rc_mode->name); \
901 return AVERROR(EINVAL); \
903 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
904 "RC mode.\n", rc_mode->name); \
907 goto rc_mode_found; \
911 if (
ctx->explicit_rc_mode)
914 if (
ctx->explicit_qp)
941 "RC mode compatible with selected options.\n");
956 "bitrate (%"PRId64
") must not be greater than "
957 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
961 rc_target_bitrate = avctx->
bit_rate;
968 rc_target_bitrate = avctx->
bit_rate;
969 rc_peak_bitrate = 2 * avctx->
bit_rate;
974 "in %s RC mode.\n",
rc_mode->name);
976 rc_target_bitrate = avctx->
bit_rate;
980 rc_target_bitrate = 0;
985 if (
ctx->explicit_qp) {
986 rc_quality =
ctx->explicit_qp;
993 rc_quality =
ctx->codec->default_quality;
995 "using default (%d).\n", rc_quality);
1011 "must have initial buffer size (%d) <= "
1012 "buffer size (%"PRId64
").\n",
1018 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1023 "in %s RC mode.\n",
rc_mode->name);
1026 hrd_buffer_size = 0;
1027 hrd_initial_buffer_fullness = 0;
1030 if (rc_target_bitrate > UINT32_MAX ||
1031 hrd_buffer_size > UINT32_MAX ||
1032 hrd_initial_buffer_fullness > UINT32_MAX) {
1034 "greater are not supported by D3D12.\n");
1038 ctx->rc_quality = rc_quality;
1047 "initial fullness %"PRId64
" bits.\n",
1048 hrd_buffer_size, hrd_initial_buffer_fullness);
1059 fr_num, fr_den, (
double)fr_num / fr_den);
1061 ctx->rc.Flags = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_NONE;
1062 ctx->rc.TargetFrameRate.Numerator = fr_num;
1063 ctx->rc.TargetFrameRate.Denominator = fr_den;
1071 D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR *cbr_ctl;
1073 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR);
1078 cbr_ctl->TargetBitRate = rc_target_bitrate;
1079 cbr_ctl->VBVCapacity = hrd_buffer_size;
1080 cbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1081 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1086 ctx->rc.ConfigParams.pConfiguration_CBR = cbr_ctl;
1090 D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR *vbr_ctl;
1092 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR);
1097 vbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1098 vbr_ctl->PeakBitRate = rc_peak_bitrate;
1099 vbr_ctl->VBVCapacity = hrd_buffer_size;
1100 vbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1101 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1106 ctx->rc.ConfigParams.pConfiguration_VBR = vbr_ctl;
1110 D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR *qvbr_ctl;
1112 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR);
1117 qvbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1118 qvbr_ctl->PeakBitRate = rc_peak_bitrate;
1119 qvbr_ctl->ConstantQualityTarget = rc_quality;
1124 ctx->rc.ConfigParams.pConfiguration_QVBR = qvbr_ctl;
1137 uint32_t ref_l0, ref_l1;
1140 D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT support;
1142 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_H264 h264;
1143 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_HEVC hevc;
1144 #if CONFIG_AV1_D3D12VA_ENCODER
1145 D3D12_VIDEO_ENCODER_CODEC_AV1_PICTURE_CONTROL_SUPPORT av1;
1149 support.NodeIndex = 0;
1150 support.Codec =
ctx->codec->d3d12_codec;
1151 support.Profile =
ctx->profile->d3d12_profile;
1153 switch (
ctx->codec->d3d12_codec) {
1154 case D3D12_VIDEO_ENCODER_CODEC_H264:
1155 support.PictureSupport.DataSize =
sizeof(codec_support.h264);
1156 support.PictureSupport.pH264Support = &codec_support.h264;
1159 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1160 support.PictureSupport.DataSize =
sizeof(codec_support.hevc);
1161 support.PictureSupport.pHEVCSupport = &codec_support.hevc;
1164 #if CONFIG_AV1_D3D12VA_ENCODER
1165 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1166 memset(&codec_support.av1, 0,
sizeof(codec_support.av1));
1167 support.PictureSupport.DataSize =
sizeof(codec_support.av1);
1168 support.PictureSupport.pAV1Support = &codec_support.av1;
1175 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT,
1176 &support,
sizeof(support));
1180 if (support.IsSupported) {
1181 switch (
ctx->codec->d3d12_codec) {
1182 case D3D12_VIDEO_ENCODER_CODEC_H264:
1183 ref_l0 =
FFMIN(support.PictureSupport.pH264Support->MaxL0ReferencesForP,
1184 support.PictureSupport.pH264Support->MaxL1ReferencesForB ?
1185 support.PictureSupport.pH264Support->MaxL1ReferencesForB : UINT_MAX);
1186 ref_l1 = support.PictureSupport.pH264Support->MaxL1ReferencesForB;
1189 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1190 ref_l0 =
FFMIN(support.PictureSupport.pHEVCSupport->MaxL0ReferencesForP,
1191 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB ?
1192 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB : UINT_MAX);
1193 ref_l1 = support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB;
1196 #if CONFIG_AV1_D3D12VA_ENCODER
1197 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1198 ref_l0 = support.PictureSupport.pAV1Support->MaxUniqueReferencesPerFrame;
1207 ref_l0 = ref_l1 = 0;
1210 if (ref_l0 > 0 && ref_l1 > 0 &&
ctx->bi_not_empty) {
1213 "replacing them with B-frames.\n");
1230 D3D12_VIDEO_ENCODER_DESC
desc = {
1232 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1233 .EncodeCodec =
ctx->codec->d3d12_codec,
1234 .EncodeProfile =
ctx->profile->d3d12_profile,
1235 .InputFormat = frames_hwctx->
format,
1236 .CodecConfiguration =
ctx->codec_conf,
1237 .MaxMotionEstimationPrecision = D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_MAXIMUM,
1240 hr = ID3D12VideoDevice3_CreateVideoEncoder(
ctx->video_device3, &
desc, &IID_ID3D12VideoEncoder,
1241 (
void **)&
ctx->encoder);
1255 D3D12_VIDEO_ENCODER_HEAP_DESC
desc = {
1257 .Flags = D3D12_VIDEO_ENCODER_HEAP_FLAG_NONE,
1258 .EncodeCodec =
ctx->codec->d3d12_codec,
1259 .EncodeProfile =
ctx->profile->d3d12_profile,
1260 .EncodeLevel =
ctx->level,
1261 .ResolutionsListCount = 1,
1262 .pResolutionList = &
ctx->resolution,
1265 hr = ID3D12VideoDevice3_CreateVideoEncoderHeap(
ctx->video_device3, &
desc,
1266 &IID_ID3D12VideoEncoderHeap, (
void **)&
ctx->encoder_heap);
1277 ID3D12Resource *pResource;
1279 pResource = (ID3D12Resource *)
data;
1288 ID3D12Resource *pResource =
NULL;
1291 D3D12_HEAP_PROPERTIES heap_props;
1292 D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_READBACK;
1294 D3D12_RESOURCE_DESC
desc = {
1295 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
1298 D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT),
1300 .DepthOrArraySize = 1,
1302 .Format = DXGI_FORMAT_UNKNOWN,
1303 .SampleDesc = { .Count = 1, .Quality = 0 },
1304 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
1305 .Flags = D3D12_RESOURCE_FLAG_NONE,
1308 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &heap_props, 0, heap_type);
1310 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
1311 &
desc, D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource,
1312 (
void **)&pResource);
1338 ctx->req.NodeIndex = 0;
1339 ctx->req.Codec =
ctx->codec->d3d12_codec;
1340 ctx->req.Profile =
ctx->profile->d3d12_profile;
1341 ctx->req.InputFormat = frames_ctx->
format;
1342 ctx->req.PictureTargetResolution =
ctx->resolution;
1344 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1345 D3D12_FEATURE_VIDEO_ENCODER_RESOURCE_REQUIREMENTS,
1346 &
ctx->req,
sizeof(
ctx->req));
1348 av_log(avctx,
AV_LOG_ERROR,
"Failed to check encoder resource requirements support.\n");
1352 if (!
ctx->req.IsSupported) {
1359 if (!
ctx->output_buffer_pool)
1368 ID3D12CommandAllocator *command_allocator =
NULL;
1372 D3D12_COMMAND_QUEUE_DESC queue_desc = {
1373 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
1375 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
1381 if (!
ctx->allocator_queue)
1384 hr = ID3D12Device_CreateFence(
ctx->hwctx->device, 0, D3D12_FENCE_FLAG_NONE,
1385 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence);
1392 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
1393 if (!
ctx->sync_ctx.event)
1400 hr = ID3D12Device_CreateCommandQueue(
ctx->hwctx->device, &queue_desc,
1401 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue);
1408 hr = ID3D12Device_CreateCommandList(
ctx->hwctx->device, 0, queue_desc.Type,
1409 command_allocator,
NULL, &IID_ID3D12CommandList,
1410 (
void **)&
ctx->command_list);
1417 hr = ID3D12VideoEncodeCommandList2_Close(
ctx->command_list);
1424 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
1465 hwctx->
resource_flags = D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY |
1466 D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
1467 if (
ctx->is_texture_array) {
1475 "frame context: %d.\n", err);
1503 D3D12_FEATURE_DATA_VIDEO_FEATURE_AREA_SUPPORT support = { 0 };
1504 D3D12_FEATURE_DATA_FORMAT_INFO format_info = { 0 };
1519 hr = ID3D12Device_QueryInterface(
ctx->hwctx->device, &IID_ID3D12Device3, (
void **)&
ctx->device3);
1526 hr = ID3D12Device3_QueryInterface(
ctx->device3, &IID_ID3D12VideoDevice3, (
void **)&
ctx->video_device3);
1533 if (FAILED(ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_FEATURE_AREA_SUPPORT,
1534 &support,
sizeof(support))) && !support.VideoEncodeSupport) {
1541 if (FAILED(ID3D12VideoDevice_CheckFeatureSupport(
ctx->hwctx->device, D3D12_FEATURE_FORMAT_INFO,
1542 &format_info,
sizeof(format_info)))) {
1547 ctx->plane_count = format_info.PlaneCount;
1553 if (
ctx->codec->set_tile) {
1554 err =
ctx->codec->set_tile(avctx);
1563 if (
ctx->codec->get_encoder_caps) {
1564 err =
ctx->codec->get_encoder_caps(avctx);
1575 "but this codec does not support controlling slices.\n");
1586 if (
ctx->codec->configure) {
1587 err =
ctx->codec->configure(avctx);
1592 if (
ctx->codec->init_sequence_params) {
1593 err =
ctx->codec->init_sequence_params(avctx);
1596 "failed: %d.\n", err);
1601 if (
ctx->codec->set_level) {
1602 err =
ctx->codec->set_level(avctx);
1636 int num_allocator = 0;
1642 if (!base_ctx->
frame)
1645 for (pic = base_ctx->
pic_start; pic; pic = next) {
1657 if (
ctx->allocator_queue) {
1663 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
1669 if (
ctx->sync_ctx.event)
1670 CloseHandle(
ctx->sync_ctx.event);