32 #include "config_components.h"
43 D3D12_VIDEO_ENCODER_VALIDATION_FLAGS
flags)
45 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_CODEC_NOT_SUPPORTED)
47 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_INPUT_FORMAT_NOT_SUPPORTED)
49 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_CODEC_CONFIGURATION_NOT_SUPPORTED)
51 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_RATE_CONTROL_MODE_NOT_SUPPORTED)
53 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_RATE_CONTROL_CONFIGURATION_NOT_SUPPORTED)
55 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_INTRA_REFRESH_MODE_NOT_SUPPORTED)
57 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_SUBREGION_LAYOUT_MODE_NOT_SUPPORTED)
59 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_RESOLUTION_NOT_SUPPORTED_IN_LIST)
61 if (
flags & D3D12_VIDEO_ENCODER_VALIDATION_FLAG_GOP_STRUCTURE_NOT_SUPPORTED)
67 uint64_t completion = ID3D12Fence_GetCompletedValue(psync_ctx->
fence);
68 if (completion < psync_ctx->fence_value) {
69 if (FAILED(ID3D12Fence_SetEventOnCompletion(psync_ctx->
fence, psync_ctx->
fence_value, psync_ctx->
event)))
72 WaitForSingleObjectEx(psync_ctx->
event, INFINITE, FALSE);
82 DX_CHECK(ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value));
101 uint64_t completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
109 hr = ID3D12Device_CreateCommandAllocator(
ctx->hwctx->device, D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
110 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
125 .fence_value = fence_value,
147 completion = ID3D12Fence_GetCompletedValue(
ctx->sync_ctx.fence);
148 if (completion < pic->fence_value) {
149 if (FAILED(ID3D12Fence_SetEventOnCompletion(
ctx->sync_ctx.fence, pic->
fence_value,
150 ctx->sync_ctx.event)))
153 WaitForSingleObjectEx(
ctx->sync_ctx.event, INFINITE, FALSE);
174 int block_width, block_height;
175 int block_size, qp_range;
179 block_size =
ctx->qp_map_region_size;
182 switch (
ctx->codec->d3d12_codec) {
183 case D3D12_VIDEO_ENCODER_CODEC_H264:
184 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
188 #if CONFIG_AV1_D3D12VA_ENCODER
189 case D3D12_VIDEO_ENCODER_CODEC_AV1:
200 block_width = (avctx->
width + block_size - 1) / block_size;
201 block_height = (avctx->
height + block_size - 1) / block_size;
216 nb_roi =
size / roi_size;
219 for (
i = nb_roi - 1;
i >= 0;
i--) {
220 int startx, endx, starty, endy;
227 starty =
FFMIN(block_height, roi->
top / block_size);
228 endy =
FFMIN(block_height, (roi->
bottom + block_size - 1) / block_size);
229 startx =
FFMIN(block_width, roi->
left / block_size);
230 endx =
FFMIN(block_width, (roi->
right + block_size - 1) / block_size);
246 int16_t *qp_map_int16 = (int16_t *)pic->
qp_map;
248 for (y = starty; y < endy; y++)
249 for (x = startx; x < endx; x++)
250 qp_map_int16[x + y * block_width] = delta_qp;
252 int8_t *qp_map_int8 = (int8_t *)pic->
qp_map;
254 for (y = starty; y < endy; y++)
255 for (x = startx; x < endx; x++)
256 qp_map_int8[x + y * block_width] = delta_qp;
269 int num_subregions =
ctx->num_subregions > 0 ?
ctx->num_subregions : 1;
270 int width =
sizeof(D3D12_VIDEO_ENCODER_OUTPUT_METADATA) +
271 sizeof(D3D12_VIDEO_ENCODER_FRAME_SUBREGION_METADATA) * num_subregions;
272 #if CONFIG_AV1_D3D12VA_ENCODER
273 if (
ctx->codec->d3d12_codec == D3D12_VIDEO_ENCODER_CODEC_AV1) {
274 width +=
sizeof(D3D12_VIDEO_ENCODER_AV1_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_TILES)
275 +
sizeof(D3D12_VIDEO_ENCODER_AV1_POST_ENCODE_VALUES);
278 D3D12_HEAP_PROPERTIES encoded_meta_props = { .Type = D3D12_HEAP_TYPE_DEFAULT }, resolved_meta_props;
279 D3D12_HEAP_TYPE resolved_heap_type = D3D12_HEAP_TYPE_READBACK;
282 D3D12_RESOURCE_DESC meta_desc = {
283 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
285 .Width =
ctx->req.MaxEncoderOutputMetadataBufferSize,
287 .DepthOrArraySize = 1,
289 .Format = DXGI_FORMAT_UNKNOWN,
290 .SampleDesc = { .Count = 1, .Quality = 0 },
291 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
292 .Flags = D3D12_RESOURCE_FLAG_NONE,
295 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &encoded_meta_props, D3D12_HEAP_FLAG_NONE,
296 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
303 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &resolved_meta_props, 0, resolved_heap_type);
305 meta_desc.Width =
width;
307 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &resolved_meta_props, D3D12_HEAP_FLAG_NONE,
308 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
331 ID3D12CommandAllocator *command_allocator =
NULL;
332 ID3D12VideoEncodeCommandList2 *cmd_list =
ctx->command_list;
333 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
334 D3D12_VIDEO_ENCODE_REFERENCE_FRAMES d3d12_refs = { 0 };
335 int barriers_ref_index = 0;
336 D3D12_RESOURCE_BARRIER *barriers_ref =
NULL;
338 D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAGS seq_flags = D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_NONE;
341 if (
ctx->intra_refresh.Mode != D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE) {
342 seq_flags |= D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_REQUEST_INTRA_REFRESH;
345 D3D12_VIDEO_ENCODER_ENCODEFRAME_INPUT_ARGUMENTS input_args = {
346 .SequenceControlDesc = {
348 .IntraRefreshConfig =
ctx->intra_refresh,
349 .RateControl =
ctx->rc,
350 .PictureTargetResolution =
ctx->resolution,
351 .SelectedLayoutMode =
ctx->subregion_mode,
352 .FrameSubregionsLayoutData =
ctx->subregions_layout,
353 .CodecGopSequence =
ctx->gop,
356 .InputFrameSubresource = 0,
359 D3D12_VIDEO_ENCODER_ENCODEFRAME_OUTPUT_ARGUMENTS output_args = { 0 };
361 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_INPUT_ARGUMENTS input_metadata = {
362 .EncoderCodec =
ctx->codec->d3d12_codec,
363 .EncoderProfile =
ctx->profile->d3d12_profile,
364 .EncoderInputFormat = frames_hwctx->
format,
365 .EncodedPictureEffectiveResolution =
ctx->resolution,
368 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_OUTPUT_ARGUMENTS output_metadata = { 0 };
435 input_args.SequenceControlDesc.RateControl.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_DELTA_QP;
436 av_log(avctx,
AV_LOG_DEBUG,
"ROI delta QP map created with %d blocks (region size: %d pixels).\n",
440 if (
ctx->codec->init_picture_params) {
441 err =
ctx->codec->init_picture_params(avctx, base_pic);
444 "parameters: %d.\n", err);
450 if (
ctx->codec->write_sequence_header) {
451 bit_len = 8 *
sizeof(
data);
452 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
455 "header: %d.\n", err);
474 d3d12_refs.NumTexture2Ds = base_pic->
nb_refs[0] + base_pic->
nb_refs[1];
475 if (d3d12_refs.NumTexture2Ds) {
476 d3d12_refs.ppTexture2Ds =
av_calloc(d3d12_refs.NumTexture2Ds,
477 sizeof(*d3d12_refs.ppTexture2Ds));
478 if (!d3d12_refs.ppTexture2Ds) {
483 if (
ctx->is_texture_array) {
484 d3d12_refs.pSubresources =
av_calloc(d3d12_refs.NumTexture2Ds,
485 sizeof(*d3d12_refs.pSubresources));
486 if (!d3d12_refs.pSubresources) {
493 for (j = 0; j < base_pic->
nb_refs[0]; j++) {
495 if (
ctx->is_texture_array)
499 for (j = 0; j < base_pic->
nb_refs[1]; j++) {
501 if (
ctx->is_texture_array)
507 input_args.PictureControlDesc.IntraRefreshFrameIndex =
ctx->intra_refresh_frame_index;
509 input_args.PictureControlDesc.Flags |= D3D12_VIDEO_ENCODER_PICTURE_CONTROL_FLAG_USED_AS_REFERENCE_PICTURE;
511 input_args.PictureControlDesc.PictureControlCodecData = pic->
pic_ctl;
512 input_args.PictureControlDesc.ReferenceFrames = d3d12_refs;
518 output_args.ReconstructedPicture.ReconstructedPictureSubresource =
ctx->is_texture_array ? pic->
subresource_index : 0;
520 output_args.EncoderOutputMetadata.Offset = 0;
523 input_metadata.HWLayoutMetadata.Offset = 0;
526 output_metadata.ResolvedLayoutMetadata.Offset = 0;
532 hr = ID3D12CommandAllocator_Reset(command_allocator);
538 hr = ID3D12VideoEncodeCommandList2_Reset(cmd_list, command_allocator);
544 #define TRANSITION_BARRIER(res, subres, before, after) \
545 (D3D12_RESOURCE_BARRIER) { \
546 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, \
547 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE, \
550 .Subresource = subres, \
551 .StateBefore = before, \
552 .StateAfter = after, \
557 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
558 D3D12_RESOURCE_STATE_COMMON,
559 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
561 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
562 D3D12_RESOURCE_STATE_COMMON,
563 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
565 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
566 D3D12_RESOURCE_STATE_COMMON,
567 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
569 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
570 D3D12_RESOURCE_STATE_COMMON,
571 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
573 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 4, barriers);
575 if (
ctx->is_texture_array)
577 sizeof(D3D12_RESOURCE_BARRIER));
581 if (
ctx->is_texture_array) {
582 D3D12_RESOURCE_DESC references_tex_array_desc = { 0 };
585 for (uint32_t reference_subresource = 0; reference_subresource < references_tex_array_desc.DepthOrArraySize;
586 reference_subresource++) {
588 uint32_t array_size = references_tex_array_desc.DepthOrArraySize;
589 uint32_t mip_slice = reference_subresource % references_tex_array_desc.MipLevels;
590 uint32_t array_slice = (reference_subresource / references_tex_array_desc.MipLevels) % array_size;
592 for (uint32_t plane_slice = 0; plane_slice <
ctx->plane_count; plane_slice++) {
593 uint32_t outputSubresource = mip_slice + array_slice * references_tex_array_desc.MipLevels +
594 plane_slice * references_tex_array_desc.MipLevels * array_size;
597 D3D12_RESOURCE_STATE_COMMON,
598 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
601 D3D12_RESOURCE_STATE_COMMON,
602 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
608 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
609 D3D12_RESOURCE_STATE_COMMON,
610 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
612 if (d3d12_refs.NumTexture2Ds) {
613 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
615 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
616 D3D12_RESOURCE_STATE_COMMON,
617 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
620 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, barriers_ref_index, barriers_ref);
622 ID3D12VideoEncodeCommandList2_EncodeFrame(cmd_list,
ctx->encoder,
ctx->encoder_heap,
623 &input_args, &output_args);
626 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
627 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
628 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
630 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 1, &barriers[3]);
632 ID3D12VideoEncodeCommandList2_ResolveEncoderOutputMetadata(cmd_list, &input_metadata, &output_metadata);
634 if (barriers_ref_index > 0) {
635 for (
i = 0;
i < barriers_ref_index;
i++)
636 FFSWAP(D3D12_RESOURCE_STATES, barriers_ref[
i].Transition.StateBefore, barriers_ref[
i].Transition.StateAfter);
638 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, barriers_ref_index,
643 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
644 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
645 D3D12_RESOURCE_STATE_COMMON);
647 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
648 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
649 D3D12_RESOURCE_STATE_COMMON);
651 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
652 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
653 D3D12_RESOURCE_STATE_COMMON);
655 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
656 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
657 D3D12_RESOURCE_STATE_COMMON);
659 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 4, barriers);
661 hr = ID3D12VideoEncodeCommandList2_Close(cmd_list);
674 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
683 hr = ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value);
696 if (
ctx->intra_refresh.Mode != D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE) {
697 ctx->intra_refresh_frame_index =
698 (
ctx->intra_refresh_frame_index + 1) %
ctx->intra_refresh.IntraRefreshDuration;
701 if (d3d12_refs.ppTexture2Ds)
704 if (
ctx->is_texture_array && d3d12_refs.pSubresources)
705 av_freep(&d3d12_refs.pSubresources);
713 if (command_allocator)
716 if (d3d12_refs.ppTexture2Ds)
719 if (
ctx->is_texture_array && d3d12_refs.pSubresources)
720 av_freep(&d3d12_refs.pSubresources);
725 if (
ctx->codec->free_picture_params)
726 ctx->codec->free_picture_params(pic);
744 "%"PRId64
"/%"PRId64
".\n",
761 switch (
ctx->rc.Mode)
763 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP:
764 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CQP);
766 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR:
767 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CBR);
769 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR:
770 av_freep(&
ctx->rc.ConfigParams.pConfiguration_VBR);
772 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR:
773 av_freep(&
ctx->rc.ConfigParams.pConfiguration_QVBR);
788 if (
ctx->codec->picture_priv_data_size > 0) {
807 if (
ctx->codec->free_picture_params)
808 ctx->codec->free_picture_params(priv);
819 D3D12_VIDEO_ENCODER_OUTPUT_METADATA *meta =
NULL;
830 meta = (D3D12_VIDEO_ENCODER_OUTPUT_METADATA *)
data;
832 if (meta->EncodeErrorFlags != D3D12_VIDEO_ENCODER_ENCODE_ERROR_FLAG_NO_ERROR) {
838 if (meta->EncodedBitstreamWrittenBytesCount == 0) {
844 *
size = meta->EncodedBitstreamWrittenBytesCount;
855 uint8_t *ptr, *mapped_data;
856 size_t total_size = 0;
883 memcpy(ptr, mapped_data, total_size);
906 if (
ctx->codec->get_coded_data)
907 err =
ctx->codec->get_coded_data(avctx, pic,
pkt);
938 depth =
desc->comp[0].depth;
939 for (
i = 1;
i <
desc->nb_components;
i++) {
940 if (
desc->comp[
i].depth != depth) {
950 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
956 if (
desc->nb_components > 1 &&
980 {
RC_MODE_CQP,
"CQP", 0, 0, 1, 0, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP },
981 {
RC_MODE_CBR,
"CBR", 1, 0, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR },
982 {
RC_MODE_VBR,
"VBR", 1, 1, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR },
983 {
RC_MODE_QVBR,
"QVBR", 1, 1, 1, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR },
990 D3D12_FEATURE_DATA_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_rc_mode = {
991 .Codec =
ctx->codec->d3d12_codec,
997 d3d12_rc_mode.IsSupported = 0;
998 d3d12_rc_mode.RateControlMode =
rc_mode->d3d12_mode;
1000 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1001 D3D12_FEATURE_VIDEO_ENCODER_RATE_CONTROL_MODE,
1002 &d3d12_rc_mode,
sizeof(d3d12_rc_mode));
1008 return d3d12_rc_mode.IsSupported;
1018 int64_t hrd_initial_buffer_fullness;
1022 #define SET_QP_RANGE(ctl) do { \
1023 if (avctx->qmin > 0 || avctx->qmax > 0) { \
1024 ctl->MinQP = avctx->qmin; \
1025 ctl->MaxQP = avctx->qmax; \
1026 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE; \
1030 #define SET_MAX_FRAME_SIZE(ctl) do { \
1031 if (ctx->max_frame_size > 0) { \
1032 ctl->MaxFrameBitSize = ctx->max_frame_size * 8; \
1033 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE; \
1049 #define TRY_RC_MODE(mode, fail) do { \
1050 rc_mode = &d3d12va_encode_rc_modes[mode]; \
1051 if (!(rc_mode->d3d12_mode && check_rate_control_support(avctx, rc_mode))) { \
1053 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1054 "RC mode.\n", rc_mode->name); \
1055 return AVERROR(EINVAL); \
1057 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1058 "RC mode.\n", rc_mode->name); \
1061 goto rc_mode_found; \
1065 if (
ctx->explicit_rc_mode)
1068 if (
ctx->explicit_qp)
1095 "RC mode compatible with selected options.\n");
1110 "bitrate (%"PRId64
") must not be greater than "
1111 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1115 rc_target_bitrate = avctx->
bit_rate;
1122 rc_target_bitrate = avctx->
bit_rate;
1123 rc_peak_bitrate = 2 * avctx->
bit_rate;
1128 "in %s RC mode.\n",
rc_mode->name);
1130 rc_target_bitrate = avctx->
bit_rate;
1131 rc_peak_bitrate = 0;
1134 rc_target_bitrate = 0;
1135 rc_peak_bitrate = 0;
1139 if (
ctx->explicit_qp) {
1140 rc_quality =
ctx->explicit_qp;
1147 rc_quality =
ctx->codec->default_quality;
1149 "using default (%d).\n", rc_quality);
1165 "must have initial buffer size (%d) <= "
1166 "buffer size (%"PRId64
").\n",
1172 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1177 "in %s RC mode.\n",
rc_mode->name);
1180 hrd_buffer_size = 0;
1181 hrd_initial_buffer_fullness = 0;
1184 if (rc_target_bitrate > UINT32_MAX ||
1185 hrd_buffer_size > UINT32_MAX ||
1186 hrd_initial_buffer_fullness > UINT32_MAX) {
1188 "greater are not supported by D3D12.\n");
1192 ctx->rc_quality = rc_quality;
1201 "initial fullness %"PRId64
" bits.\n",
1202 hrd_buffer_size, hrd_initial_buffer_fullness);
1213 fr_num, fr_den, (
double)fr_num / fr_den);
1215 ctx->rc.Flags = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_NONE;
1216 ctx->rc.TargetFrameRate.Numerator = fr_num;
1217 ctx->rc.TargetFrameRate.Denominator = fr_den;
1225 D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR *cbr_ctl;
1227 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR);
1232 cbr_ctl->TargetBitRate = rc_target_bitrate;
1233 cbr_ctl->VBVCapacity = hrd_buffer_size;
1234 cbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1235 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1240 ctx->rc.ConfigParams.pConfiguration_CBR = cbr_ctl;
1244 D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR *vbr_ctl;
1246 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR);
1251 vbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1252 vbr_ctl->PeakBitRate = rc_peak_bitrate;
1253 vbr_ctl->VBVCapacity = hrd_buffer_size;
1254 vbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1255 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1260 ctx->rc.ConfigParams.pConfiguration_VBR = vbr_ctl;
1264 D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR *qvbr_ctl;
1266 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR);
1271 qvbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1272 qvbr_ctl->PeakBitRate = rc_peak_bitrate;
1273 qvbr_ctl->ConstantQualityTarget = rc_quality;
1278 ctx->rc.ConfigParams.pConfiguration_QVBR = qvbr_ctl;
1291 uint32_t ref_l0, ref_l1;
1294 D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT support;
1296 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_H264 h264;
1297 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_HEVC hevc;
1298 #if CONFIG_AV1_D3D12VA_ENCODER
1299 D3D12_VIDEO_ENCODER_CODEC_AV1_PICTURE_CONTROL_SUPPORT av1;
1303 support.NodeIndex = 0;
1304 support.Codec =
ctx->codec->d3d12_codec;
1305 support.Profile =
ctx->profile->d3d12_profile;
1307 switch (
ctx->codec->d3d12_codec) {
1308 case D3D12_VIDEO_ENCODER_CODEC_H264:
1309 support.PictureSupport.DataSize =
sizeof(codec_support.h264);
1310 support.PictureSupport.pH264Support = &codec_support.h264;
1313 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1314 support.PictureSupport.DataSize =
sizeof(codec_support.hevc);
1315 support.PictureSupport.pHEVCSupport = &codec_support.hevc;
1318 #if CONFIG_AV1_D3D12VA_ENCODER
1319 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1320 memset(&codec_support.av1, 0,
sizeof(codec_support.av1));
1321 support.PictureSupport.DataSize =
sizeof(codec_support.av1);
1322 support.PictureSupport.pAV1Support = &codec_support.av1;
1329 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT,
1330 &support,
sizeof(support));
1334 if (support.IsSupported) {
1335 switch (
ctx->codec->d3d12_codec) {
1336 case D3D12_VIDEO_ENCODER_CODEC_H264:
1337 ref_l0 =
FFMIN(support.PictureSupport.pH264Support->MaxL0ReferencesForP,
1338 support.PictureSupport.pH264Support->MaxL1ReferencesForB ?
1339 support.PictureSupport.pH264Support->MaxL1ReferencesForB : UINT_MAX);
1340 ref_l1 = support.PictureSupport.pH264Support->MaxL1ReferencesForB;
1343 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1344 ref_l0 =
FFMIN(support.PictureSupport.pHEVCSupport->MaxL0ReferencesForP,
1345 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB ?
1346 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB : UINT_MAX);
1347 ref_l1 = support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB;
1350 #if CONFIG_AV1_D3D12VA_ENCODER
1351 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1352 ref_l0 = support.PictureSupport.pAV1Support->MaxUniqueReferencesPerFrame;
1361 ref_l0 = ref_l1 = 0;
1364 if (ref_l0 > 0 && ref_l1 > 0 &&
ctx->bi_not_empty) {
1367 "replacing them with B-frames.\n");
1382 if (
ctx->intra_refresh.Mode == D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE)
1386 #if CONFIG_D3D12_INTRA_REFRESH
1388 D3D12_VIDEO_ENCODER_LEVEL_SETTING
level = { 0 };
1389 D3D12_VIDEO_ENCODER_LEVELS_H264 h264_level = { 0 };
1390 D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC hevc_level = { 0 };
1391 #if CONFIG_AV1_D3D12VA_ENCODER
1392 D3D12_VIDEO_ENCODER_AV1_LEVEL_TIER_CONSTRAINTS av1_level = { 0 };
1395 switch (
ctx->codec->d3d12_codec) {
1396 case D3D12_VIDEO_ENCODER_CODEC_H264:
1397 level.DataSize =
sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264);
1398 level.pH264LevelSetting = &h264_level;
1400 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1401 level.DataSize =
sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC);
1402 level.pHEVCLevelSetting = &hevc_level;
1404 #if CONFIG_AV1_D3D12VA_ENCODER
1405 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1406 level.DataSize =
sizeof(D3D12_VIDEO_ENCODER_AV1_LEVEL_TIER_CONSTRAINTS);
1407 level.pAV1LevelSetting = &av1_level;
1414 D3D12_FEATURE_DATA_VIDEO_ENCODER_INTRA_REFRESH_MODE intra_refresh_support = {
1416 .Codec =
ctx->codec->d3d12_codec,
1417 .Profile =
ctx->profile->d3d12_profile,
1419 .IntraRefreshMode =
ctx->intra_refresh.Mode,
1422 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1423 D3D12_FEATURE_VIDEO_ENCODER_INTRA_REFRESH_MODE,
1424 &intra_refresh_support,
sizeof(intra_refresh_support));
1426 if (FAILED(hr) || !intra_refresh_support.IsSupported) {
1427 av_log(avctx,
AV_LOG_ERROR,
"Requested intra refresh mode not supported by driver.\n");
1433 "Support will be validated during encoder initialization.\n");
1437 if (
ctx->intra_refresh.IntraRefreshDuration == 0) {
1438 ctx->intra_refresh.IntraRefreshDuration = base_ctx->
gop_size;
1440 ctx->intra_refresh.IntraRefreshDuration);
1444 ctx->intra_refresh_frame_index = 0;
1447 ctx->intra_refresh.Mode,
ctx->intra_refresh.IntraRefreshDuration);
1459 D3D12_VIDEO_ENCODER_DESC
desc = {
1461 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1462 .EncodeCodec =
ctx->codec->d3d12_codec,
1463 .EncodeProfile =
ctx->profile->d3d12_profile,
1464 .InputFormat = frames_hwctx->
format,
1465 .CodecConfiguration =
ctx->codec_conf,
1466 .MaxMotionEstimationPrecision =
ctx->me_precision,
1469 hr = ID3D12VideoDevice3_CreateVideoEncoder(
ctx->video_device3, &
desc, &IID_ID3D12VideoEncoder,
1470 (
void **)&
ctx->encoder);
1484 D3D12_VIDEO_ENCODER_HEAP_DESC
desc = {
1486 .Flags = D3D12_VIDEO_ENCODER_HEAP_FLAG_NONE,
1487 .EncodeCodec =
ctx->codec->d3d12_codec,
1488 .EncodeProfile =
ctx->profile->d3d12_profile,
1489 .EncodeLevel =
ctx->level,
1490 .ResolutionsListCount = 1,
1491 .pResolutionList = &
ctx->resolution,
1494 hr = ID3D12VideoDevice3_CreateVideoEncoderHeap(
ctx->video_device3, &
desc,
1495 &IID_ID3D12VideoEncoderHeap, (
void **)&
ctx->encoder_heap);
1506 ID3D12Resource *pResource;
1508 pResource = (ID3D12Resource *)
data;
1517 ID3D12Resource *pResource =
NULL;
1520 D3D12_HEAP_PROPERTIES heap_props;
1521 D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_READBACK;
1523 D3D12_RESOURCE_DESC
desc = {
1524 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
1527 D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT),
1529 .DepthOrArraySize = 1,
1531 .Format = DXGI_FORMAT_UNKNOWN,
1532 .SampleDesc = { .Count = 1, .Quality = 0 },
1533 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
1534 .Flags = D3D12_RESOURCE_FLAG_NONE,
1537 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &heap_props, 0, heap_type);
1539 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
1540 &
desc, D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource,
1541 (
void **)&pResource);
1567 ctx->req.NodeIndex = 0;
1568 ctx->req.Codec =
ctx->codec->d3d12_codec;
1569 ctx->req.Profile =
ctx->profile->d3d12_profile;
1570 ctx->req.InputFormat = frames_ctx->
format;
1571 ctx->req.PictureTargetResolution =
ctx->resolution;
1573 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1574 D3D12_FEATURE_VIDEO_ENCODER_RESOURCE_REQUIREMENTS,
1575 &
ctx->req,
sizeof(
ctx->req));
1577 av_log(avctx,
AV_LOG_ERROR,
"Failed to check encoder resource requirements support.\n");
1581 if (!
ctx->req.IsSupported) {
1588 if (!
ctx->output_buffer_pool)
1597 ID3D12CommandAllocator *command_allocator =
NULL;
1601 D3D12_COMMAND_QUEUE_DESC queue_desc = {
1602 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
1604 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
1610 if (!
ctx->allocator_queue)
1613 hr = ID3D12Device_CreateFence(
ctx->hwctx->device, 0, D3D12_FENCE_FLAG_NONE,
1614 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence);
1621 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
1622 if (!
ctx->sync_ctx.event)
1629 hr = ID3D12Device_CreateCommandQueue(
ctx->hwctx->device, &queue_desc,
1630 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue);
1637 hr = ID3D12Device_CreateCommandList(
ctx->hwctx->device, 0, queue_desc.Type,
1638 command_allocator,
NULL, &IID_ID3D12CommandList,
1639 (
void **)&
ctx->command_list);
1646 hr = ID3D12VideoEncodeCommandList2_Close(
ctx->command_list);
1653 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
1694 hwctx->
resource_flags = D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY |
1695 D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
1696 if (
ctx->is_texture_array) {
1704 "frame context: %d.\n", err);
1732 D3D12_FEATURE_DATA_VIDEO_FEATURE_AREA_SUPPORT support = { 0 };
1733 D3D12_FEATURE_DATA_FORMAT_INFO format_info = { 0 };
1748 hr = ID3D12Device_QueryInterface(
ctx->hwctx->device, &IID_ID3D12Device3, (
void **)&
ctx->device3);
1755 hr = ID3D12Device3_QueryInterface(
ctx->device3, &IID_ID3D12VideoDevice3, (
void **)&
ctx->video_device3);
1762 if (FAILED(ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_FEATURE_AREA_SUPPORT,
1763 &support,
sizeof(support))) && !support.VideoEncodeSupport) {
1770 if (FAILED(ID3D12VideoDevice_CheckFeatureSupport(
ctx->hwctx->device, D3D12_FEATURE_FORMAT_INFO,
1771 &format_info,
sizeof(format_info)))) {
1776 ctx->plane_count = format_info.PlaneCount;
1786 if (
ctx->codec->get_encoder_caps) {
1787 err =
ctx->codec->get_encoder_caps(avctx);
1792 if (
ctx->codec->set_tile) {
1793 err =
ctx->codec->set_tile(avctx);
1808 "but this codec does not support controlling slices.\n");
1819 if (
ctx->codec->configure) {
1820 err =
ctx->codec->configure(avctx);
1825 if (
ctx->codec->init_sequence_params) {
1826 err =
ctx->codec->init_sequence_params(avctx);
1829 "failed: %d.\n", err);
1834 if (
ctx->codec->set_level) {
1835 err =
ctx->codec->set_level(avctx);
1869 int num_allocator = 0;
1875 if (!base_ctx->
frame)
1878 for (pic = base_ctx->
pic_start; pic; pic = next) {
1890 if (
ctx->allocator_queue) {
1896 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
1902 if (
ctx->sync_ctx.event)
1903 CloseHandle(
ctx->sync_ctx.event);