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 width =
sizeof(D3D12_VIDEO_ENCODER_OUTPUT_METADATA) +
sizeof(D3D12_VIDEO_ENCODER_FRAME_SUBREGION_METADATA);
270 #if CONFIG_AV1_D3D12VA_ENCODER
271 if (
ctx->codec->d3d12_codec == D3D12_VIDEO_ENCODER_CODEC_AV1) {
272 width +=
sizeof(D3D12_VIDEO_ENCODER_AV1_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_TILES)
273 +
sizeof(D3D12_VIDEO_ENCODER_AV1_POST_ENCODE_VALUES);
276 D3D12_HEAP_PROPERTIES encoded_meta_props = { .Type = D3D12_HEAP_TYPE_DEFAULT }, resolved_meta_props;
277 D3D12_HEAP_TYPE resolved_heap_type = D3D12_HEAP_TYPE_READBACK;
280 D3D12_RESOURCE_DESC meta_desc = {
281 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
283 .Width =
ctx->req.MaxEncoderOutputMetadataBufferSize,
285 .DepthOrArraySize = 1,
287 .Format = DXGI_FORMAT_UNKNOWN,
288 .SampleDesc = { .Count = 1, .Quality = 0 },
289 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
290 .Flags = D3D12_RESOURCE_FLAG_NONE,
293 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &encoded_meta_props, D3D12_HEAP_FLAG_NONE,
294 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
301 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &resolved_meta_props, 0, resolved_heap_type);
303 meta_desc.Width =
width;
305 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &resolved_meta_props, D3D12_HEAP_FLAG_NONE,
306 &meta_desc, D3D12_RESOURCE_STATE_COMMON,
NULL,
329 ID3D12CommandAllocator *command_allocator =
NULL;
330 ID3D12VideoEncodeCommandList2 *cmd_list =
ctx->command_list;
331 D3D12_RESOURCE_BARRIER barriers[32] = { 0 };
332 D3D12_VIDEO_ENCODE_REFERENCE_FRAMES d3d12_refs = { 0 };
333 int barriers_ref_index = 0;
334 D3D12_RESOURCE_BARRIER *barriers_ref =
NULL;
336 D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAGS seq_flags = D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_NONE;
339 if (
ctx->intra_refresh.Mode != D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE) {
340 seq_flags |= D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_REQUEST_INTRA_REFRESH;
343 D3D12_VIDEO_ENCODER_ENCODEFRAME_INPUT_ARGUMENTS input_args = {
344 .SequenceControlDesc = {
346 .IntraRefreshConfig =
ctx->intra_refresh,
347 .RateControl =
ctx->rc,
348 .PictureTargetResolution =
ctx->resolution,
349 .SelectedLayoutMode = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
350 .FrameSubregionsLayoutData =
ctx->subregions_layout,
351 .CodecGopSequence =
ctx->gop,
354 .InputFrameSubresource = 0,
357 D3D12_VIDEO_ENCODER_ENCODEFRAME_OUTPUT_ARGUMENTS output_args = { 0 };
359 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_INPUT_ARGUMENTS input_metadata = {
360 .EncoderCodec =
ctx->codec->d3d12_codec,
361 .EncoderProfile =
ctx->profile->d3d12_profile,
362 .EncoderInputFormat = frames_hwctx->
format,
363 .EncodedPictureEffectiveResolution =
ctx->resolution,
366 D3D12_VIDEO_ENCODER_RESOLVE_METADATA_OUTPUT_ARGUMENTS output_metadata = { 0 };
433 input_args.SequenceControlDesc.RateControl.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_DELTA_QP;
434 av_log(avctx,
AV_LOG_DEBUG,
"ROI delta QP map created with %d blocks (region size: %d pixels).\n",
438 if (
ctx->codec->init_picture_params) {
439 err =
ctx->codec->init_picture_params(avctx, base_pic);
442 "parameters: %d.\n", err);
448 if (
ctx->codec->write_sequence_header) {
449 bit_len = 8 *
sizeof(
data);
450 err =
ctx->codec->write_sequence_header(avctx,
data, &bit_len);
453 "header: %d.\n", err);
472 d3d12_refs.NumTexture2Ds = base_pic->
nb_refs[0] + base_pic->
nb_refs[1];
473 if (d3d12_refs.NumTexture2Ds) {
474 d3d12_refs.ppTexture2Ds =
av_calloc(d3d12_refs.NumTexture2Ds,
475 sizeof(*d3d12_refs.ppTexture2Ds));
476 if (!d3d12_refs.ppTexture2Ds) {
481 if (
ctx->is_texture_array) {
482 d3d12_refs.pSubresources =
av_calloc(d3d12_refs.NumTexture2Ds,
483 sizeof(*d3d12_refs.pSubresources));
484 if (!d3d12_refs.pSubresources) {
491 for (j = 0; j < base_pic->
nb_refs[0]; j++) {
493 if (
ctx->is_texture_array)
497 for (j = 0; j < base_pic->
nb_refs[1]; j++) {
499 if (
ctx->is_texture_array)
505 input_args.PictureControlDesc.IntraRefreshFrameIndex =
ctx->intra_refresh_frame_index;
507 input_args.PictureControlDesc.Flags |= D3D12_VIDEO_ENCODER_PICTURE_CONTROL_FLAG_USED_AS_REFERENCE_PICTURE;
509 input_args.PictureControlDesc.PictureControlCodecData = pic->
pic_ctl;
510 input_args.PictureControlDesc.ReferenceFrames = d3d12_refs;
516 output_args.ReconstructedPicture.ReconstructedPictureSubresource =
ctx->is_texture_array ? pic->
subresource_index : 0;
518 output_args.EncoderOutputMetadata.Offset = 0;
521 input_metadata.HWLayoutMetadata.Offset = 0;
524 output_metadata.ResolvedLayoutMetadata.Offset = 0;
530 hr = ID3D12CommandAllocator_Reset(command_allocator);
536 hr = ID3D12VideoEncodeCommandList2_Reset(cmd_list, command_allocator);
542 #define TRANSITION_BARRIER(res, subres, before, after) \
543 (D3D12_RESOURCE_BARRIER) { \
544 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, \
545 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE, \
548 .Subresource = subres, \
549 .StateBefore = before, \
550 .StateAfter = after, \
555 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
556 D3D12_RESOURCE_STATE_COMMON,
557 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
559 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
560 D3D12_RESOURCE_STATE_COMMON,
561 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
563 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
564 D3D12_RESOURCE_STATE_COMMON,
565 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
567 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
568 D3D12_RESOURCE_STATE_COMMON,
569 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
571 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 4, barriers);
573 if (
ctx->is_texture_array)
575 sizeof(D3D12_RESOURCE_BARRIER));
579 if (
ctx->is_texture_array) {
580 D3D12_RESOURCE_DESC references_tex_array_desc = { 0 };
583 for (uint32_t reference_subresource = 0; reference_subresource < references_tex_array_desc.DepthOrArraySize;
584 reference_subresource++) {
586 uint32_t array_size = references_tex_array_desc.DepthOrArraySize;
587 uint32_t mip_slice = reference_subresource % references_tex_array_desc.MipLevels;
588 uint32_t array_slice = (reference_subresource / references_tex_array_desc.MipLevels) % array_size;
590 for (uint32_t plane_slice = 0; plane_slice <
ctx->plane_count; plane_slice++) {
591 uint32_t outputSubresource = mip_slice + array_slice * references_tex_array_desc.MipLevels +
592 plane_slice * references_tex_array_desc.MipLevels * array_size;
595 D3D12_RESOURCE_STATE_COMMON,
596 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
599 D3D12_RESOURCE_STATE_COMMON,
600 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
606 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
607 D3D12_RESOURCE_STATE_COMMON,
608 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE);
610 if (d3d12_refs.NumTexture2Ds) {
611 for (
i = 0;
i < d3d12_refs.NumTexture2Ds;
i++)
613 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
614 D3D12_RESOURCE_STATE_COMMON,
615 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
618 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, barriers_ref_index, barriers_ref);
620 ID3D12VideoEncodeCommandList2_EncodeFrame(cmd_list,
ctx->encoder,
ctx->encoder_heap,
621 &input_args, &output_args);
624 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
625 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
626 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ);
628 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 1, &barriers[3]);
630 ID3D12VideoEncodeCommandList2_ResolveEncoderOutputMetadata(cmd_list, &input_metadata, &output_metadata);
632 if (barriers_ref_index > 0) {
633 for (
i = 0;
i < barriers_ref_index;
i++)
634 FFSWAP(D3D12_RESOURCE_STATES, barriers_ref[
i].Transition.StateBefore, barriers_ref[
i].Transition.StateAfter);
636 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, barriers_ref_index,
641 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
642 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
643 D3D12_RESOURCE_STATE_COMMON);
645 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
646 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
647 D3D12_RESOURCE_STATE_COMMON);
649 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
650 D3D12_RESOURCE_STATE_VIDEO_ENCODE_READ,
651 D3D12_RESOURCE_STATE_COMMON);
653 D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
654 D3D12_RESOURCE_STATE_VIDEO_ENCODE_WRITE,
655 D3D12_RESOURCE_STATE_COMMON);
657 ID3D12VideoEncodeCommandList2_ResourceBarrier(cmd_list, 4, barriers);
659 hr = ID3D12VideoEncodeCommandList2_Close(cmd_list);
672 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
681 hr = ID3D12CommandQueue_Signal(
ctx->command_queue,
ctx->sync_ctx.fence, ++
ctx->sync_ctx.fence_value);
694 if (
ctx->intra_refresh.Mode != D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE) {
695 ctx->intra_refresh_frame_index =
696 (
ctx->intra_refresh_frame_index + 1) %
ctx->intra_refresh.IntraRefreshDuration;
699 if (d3d12_refs.ppTexture2Ds)
702 if (
ctx->is_texture_array && d3d12_refs.pSubresources)
703 av_freep(&d3d12_refs.pSubresources);
711 if (command_allocator)
714 if (d3d12_refs.ppTexture2Ds)
717 if (
ctx->is_texture_array && d3d12_refs.pSubresources)
718 av_freep(&d3d12_refs.pSubresources);
723 if (
ctx->codec->free_picture_params)
724 ctx->codec->free_picture_params(pic);
742 "%"PRId64
"/%"PRId64
".\n",
759 switch (
ctx->rc.Mode)
761 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP:
762 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CQP);
764 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR:
765 av_freep(&
ctx->rc.ConfigParams.pConfiguration_CBR);
767 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR:
768 av_freep(&
ctx->rc.ConfigParams.pConfiguration_VBR);
770 case D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR:
771 av_freep(&
ctx->rc.ConfigParams.pConfiguration_QVBR);
786 if (
ctx->codec->picture_priv_data_size > 0) {
805 if (
ctx->codec->free_picture_params)
806 ctx->codec->free_picture_params(priv);
817 D3D12_VIDEO_ENCODER_OUTPUT_METADATA *meta =
NULL;
828 meta = (D3D12_VIDEO_ENCODER_OUTPUT_METADATA *)
data;
830 if (meta->EncodeErrorFlags != D3D12_VIDEO_ENCODER_ENCODE_ERROR_FLAG_NO_ERROR) {
836 if (meta->EncodedBitstreamWrittenBytesCount == 0) {
842 *
size = meta->EncodedBitstreamWrittenBytesCount;
853 uint8_t *ptr, *mapped_data;
854 size_t total_size = 0;
881 memcpy(ptr, mapped_data, total_size);
904 if (
ctx->codec->get_coded_data)
905 err =
ctx->codec->get_coded_data(avctx, pic,
pkt);
936 depth =
desc->comp[0].depth;
937 for (
i = 1;
i <
desc->nb_components;
i++) {
938 if (
desc->comp[
i].depth != depth) {
948 for (
i = 0; (
ctx->codec->profiles[
i].av_profile !=
954 if (
desc->nb_components > 1 &&
978 {
RC_MODE_CQP,
"CQP", 0, 0, 1, 0, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP },
979 {
RC_MODE_CBR,
"CBR", 1, 0, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR },
980 {
RC_MODE_VBR,
"VBR", 1, 1, 0, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR },
981 {
RC_MODE_QVBR,
"QVBR", 1, 1, 1, 1, D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR },
988 D3D12_FEATURE_DATA_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_rc_mode = {
989 .Codec =
ctx->codec->d3d12_codec,
995 d3d12_rc_mode.IsSupported = 0;
996 d3d12_rc_mode.RateControlMode =
rc_mode->d3d12_mode;
998 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
999 D3D12_FEATURE_VIDEO_ENCODER_RATE_CONTROL_MODE,
1000 &d3d12_rc_mode,
sizeof(d3d12_rc_mode));
1006 return d3d12_rc_mode.IsSupported;
1016 int64_t hrd_initial_buffer_fullness;
1020 #define SET_QP_RANGE(ctl) do { \
1021 if (avctx->qmin > 0 || avctx->qmax > 0) { \
1022 ctl->MinQP = avctx->qmin; \
1023 ctl->MaxQP = avctx->qmax; \
1024 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE; \
1028 #define SET_MAX_FRAME_SIZE(ctl) do { \
1029 if (ctx->max_frame_size > 0) { \
1030 ctl->MaxFrameBitSize = ctx->max_frame_size * 8; \
1031 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE; \
1047 #define TRY_RC_MODE(mode, fail) do { \
1048 rc_mode = &d3d12va_encode_rc_modes[mode]; \
1049 if (!(rc_mode->d3d12_mode && check_rate_control_support(avctx, rc_mode))) { \
1051 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1052 "RC mode.\n", rc_mode->name); \
1053 return AVERROR(EINVAL); \
1055 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1056 "RC mode.\n", rc_mode->name); \
1059 goto rc_mode_found; \
1063 if (
ctx->explicit_rc_mode)
1066 if (
ctx->explicit_qp)
1093 "RC mode compatible with selected options.\n");
1108 "bitrate (%"PRId64
") must not be greater than "
1109 "maxrate (%"PRId64
").\n", avctx->
bit_rate,
1113 rc_target_bitrate = avctx->
bit_rate;
1120 rc_target_bitrate = avctx->
bit_rate;
1121 rc_peak_bitrate = 2 * avctx->
bit_rate;
1126 "in %s RC mode.\n",
rc_mode->name);
1128 rc_target_bitrate = avctx->
bit_rate;
1129 rc_peak_bitrate = 0;
1132 rc_target_bitrate = 0;
1133 rc_peak_bitrate = 0;
1137 if (
ctx->explicit_qp) {
1138 rc_quality =
ctx->explicit_qp;
1145 rc_quality =
ctx->codec->default_quality;
1147 "using default (%d).\n", rc_quality);
1163 "must have initial buffer size (%d) <= "
1164 "buffer size (%"PRId64
").\n",
1170 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1175 "in %s RC mode.\n",
rc_mode->name);
1178 hrd_buffer_size = 0;
1179 hrd_initial_buffer_fullness = 0;
1182 if (rc_target_bitrate > UINT32_MAX ||
1183 hrd_buffer_size > UINT32_MAX ||
1184 hrd_initial_buffer_fullness > UINT32_MAX) {
1186 "greater are not supported by D3D12.\n");
1190 ctx->rc_quality = rc_quality;
1199 "initial fullness %"PRId64
" bits.\n",
1200 hrd_buffer_size, hrd_initial_buffer_fullness);
1211 fr_num, fr_den, (
double)fr_num / fr_den);
1213 ctx->rc.Flags = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_NONE;
1214 ctx->rc.TargetFrameRate.Numerator = fr_num;
1215 ctx->rc.TargetFrameRate.Denominator = fr_den;
1223 D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR *cbr_ctl;
1225 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CBR);
1230 cbr_ctl->TargetBitRate = rc_target_bitrate;
1231 cbr_ctl->VBVCapacity = hrd_buffer_size;
1232 cbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1233 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1238 ctx->rc.ConfigParams.pConfiguration_CBR = cbr_ctl;
1242 D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR *vbr_ctl;
1244 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_VBR);
1249 vbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1250 vbr_ctl->PeakBitRate = rc_peak_bitrate;
1251 vbr_ctl->VBVCapacity = hrd_buffer_size;
1252 vbr_ctl->InitialVBVFullness = hrd_initial_buffer_fullness;
1253 ctx->rc.Flags |= D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
1258 ctx->rc.ConfigParams.pConfiguration_VBR = vbr_ctl;
1262 D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR *qvbr_ctl;
1264 ctx->rc.ConfigParams.DataSize =
sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_QVBR);
1269 qvbr_ctl->TargetAvgBitRate = rc_target_bitrate;
1270 qvbr_ctl->PeakBitRate = rc_peak_bitrate;
1271 qvbr_ctl->ConstantQualityTarget = rc_quality;
1276 ctx->rc.ConfigParams.pConfiguration_QVBR = qvbr_ctl;
1289 uint32_t ref_l0, ref_l1;
1292 D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT support;
1294 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_H264 h264;
1295 D3D12_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT_HEVC hevc;
1296 #if CONFIG_AV1_D3D12VA_ENCODER
1297 D3D12_VIDEO_ENCODER_CODEC_AV1_PICTURE_CONTROL_SUPPORT av1;
1301 support.NodeIndex = 0;
1302 support.Codec =
ctx->codec->d3d12_codec;
1303 support.Profile =
ctx->profile->d3d12_profile;
1305 switch (
ctx->codec->d3d12_codec) {
1306 case D3D12_VIDEO_ENCODER_CODEC_H264:
1307 support.PictureSupport.DataSize =
sizeof(codec_support.h264);
1308 support.PictureSupport.pH264Support = &codec_support.h264;
1311 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1312 support.PictureSupport.DataSize =
sizeof(codec_support.hevc);
1313 support.PictureSupport.pHEVCSupport = &codec_support.hevc;
1316 #if CONFIG_AV1_D3D12VA_ENCODER
1317 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1318 memset(&codec_support.av1, 0,
sizeof(codec_support.av1));
1319 support.PictureSupport.DataSize =
sizeof(codec_support.av1);
1320 support.PictureSupport.pAV1Support = &codec_support.av1;
1327 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_PICTURE_CONTROL_SUPPORT,
1328 &support,
sizeof(support));
1332 if (support.IsSupported) {
1333 switch (
ctx->codec->d3d12_codec) {
1334 case D3D12_VIDEO_ENCODER_CODEC_H264:
1335 ref_l0 =
FFMIN(support.PictureSupport.pH264Support->MaxL0ReferencesForP,
1336 support.PictureSupport.pH264Support->MaxL1ReferencesForB ?
1337 support.PictureSupport.pH264Support->MaxL1ReferencesForB : UINT_MAX);
1338 ref_l1 = support.PictureSupport.pH264Support->MaxL1ReferencesForB;
1341 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1342 ref_l0 =
FFMIN(support.PictureSupport.pHEVCSupport->MaxL0ReferencesForP,
1343 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB ?
1344 support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB : UINT_MAX);
1345 ref_l1 = support.PictureSupport.pHEVCSupport->MaxL1ReferencesForB;
1348 #if CONFIG_AV1_D3D12VA_ENCODER
1349 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1350 ref_l0 = support.PictureSupport.pAV1Support->MaxUniqueReferencesPerFrame;
1359 ref_l0 = ref_l1 = 0;
1362 if (ref_l0 > 0 && ref_l1 > 0 &&
ctx->bi_not_empty) {
1365 "replacing them with B-frames.\n");
1380 if (
ctx->intra_refresh.Mode == D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE)
1384 #if CONFIG_D3D12_INTRA_REFRESH
1386 D3D12_VIDEO_ENCODER_LEVEL_SETTING
level = { 0 };
1387 D3D12_VIDEO_ENCODER_LEVELS_H264 h264_level = { 0 };
1388 D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC hevc_level = { 0 };
1389 #if CONFIG_AV1_D3D12VA_ENCODER
1390 D3D12_VIDEO_ENCODER_AV1_LEVEL_TIER_CONSTRAINTS av1_level = { 0 };
1393 switch (
ctx->codec->d3d12_codec) {
1394 case D3D12_VIDEO_ENCODER_CODEC_H264:
1395 level.DataSize =
sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264);
1396 level.pH264LevelSetting = &h264_level;
1398 case D3D12_VIDEO_ENCODER_CODEC_HEVC:
1399 level.DataSize =
sizeof(D3D12_VIDEO_ENCODER_LEVEL_TIER_CONSTRAINTS_HEVC);
1400 level.pHEVCLevelSetting = &hevc_level;
1402 #if CONFIG_AV1_D3D12VA_ENCODER
1403 case D3D12_VIDEO_ENCODER_CODEC_AV1:
1404 level.DataSize =
sizeof(D3D12_VIDEO_ENCODER_AV1_LEVEL_TIER_CONSTRAINTS);
1405 level.pAV1LevelSetting = &av1_level;
1412 D3D12_FEATURE_DATA_VIDEO_ENCODER_INTRA_REFRESH_MODE intra_refresh_support = {
1414 .Codec =
ctx->codec->d3d12_codec,
1415 .Profile =
ctx->profile->d3d12_profile,
1417 .IntraRefreshMode =
ctx->intra_refresh.Mode,
1420 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1421 D3D12_FEATURE_VIDEO_ENCODER_INTRA_REFRESH_MODE,
1422 &intra_refresh_support,
sizeof(intra_refresh_support));
1424 if (FAILED(hr) || !intra_refresh_support.IsSupported) {
1425 av_log(avctx,
AV_LOG_ERROR,
"Requested intra refresh mode not supported by driver.\n");
1431 "Support will be validated during encoder initialization.\n");
1435 if (
ctx->intra_refresh.IntraRefreshDuration == 0) {
1436 ctx->intra_refresh.IntraRefreshDuration = base_ctx->
gop_size;
1438 ctx->intra_refresh.IntraRefreshDuration);
1442 ctx->intra_refresh_frame_index = 0;
1445 ctx->intra_refresh.Mode,
ctx->intra_refresh.IntraRefreshDuration);
1457 D3D12_VIDEO_ENCODER_DESC
desc = {
1459 .Flags = D3D12_VIDEO_ENCODER_FLAG_NONE,
1460 .EncodeCodec =
ctx->codec->d3d12_codec,
1461 .EncodeProfile =
ctx->profile->d3d12_profile,
1462 .InputFormat = frames_hwctx->
format,
1463 .CodecConfiguration =
ctx->codec_conf,
1464 .MaxMotionEstimationPrecision =
ctx->me_precision,
1467 hr = ID3D12VideoDevice3_CreateVideoEncoder(
ctx->video_device3, &
desc, &IID_ID3D12VideoEncoder,
1468 (
void **)&
ctx->encoder);
1482 D3D12_VIDEO_ENCODER_HEAP_DESC
desc = {
1484 .Flags = D3D12_VIDEO_ENCODER_HEAP_FLAG_NONE,
1485 .EncodeCodec =
ctx->codec->d3d12_codec,
1486 .EncodeProfile =
ctx->profile->d3d12_profile,
1487 .EncodeLevel =
ctx->level,
1488 .ResolutionsListCount = 1,
1489 .pResolutionList = &
ctx->resolution,
1492 hr = ID3D12VideoDevice3_CreateVideoEncoderHeap(
ctx->video_device3, &
desc,
1493 &IID_ID3D12VideoEncoderHeap, (
void **)&
ctx->encoder_heap);
1504 ID3D12Resource *pResource;
1506 pResource = (ID3D12Resource *)
data;
1515 ID3D12Resource *pResource =
NULL;
1518 D3D12_HEAP_PROPERTIES heap_props;
1519 D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_READBACK;
1521 D3D12_RESOURCE_DESC
desc = {
1522 .Dimension = D3D12_RESOURCE_DIMENSION_BUFFER,
1525 D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT),
1527 .DepthOrArraySize = 1,
1529 .Format = DXGI_FORMAT_UNKNOWN,
1530 .SampleDesc = { .Count = 1, .Quality = 0 },
1531 .Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
1532 .Flags = D3D12_RESOURCE_FLAG_NONE,
1535 ctx->hwctx->device->lpVtbl->GetCustomHeapProperties(
ctx->hwctx->device, &heap_props, 0, heap_type);
1537 hr = ID3D12Device_CreateCommittedResource(
ctx->hwctx->device, &heap_props, D3D12_HEAP_FLAG_NONE,
1538 &
desc, D3D12_RESOURCE_STATE_COMMON,
NULL, &IID_ID3D12Resource,
1539 (
void **)&pResource);
1565 ctx->req.NodeIndex = 0;
1566 ctx->req.Codec =
ctx->codec->d3d12_codec;
1567 ctx->req.Profile =
ctx->profile->d3d12_profile;
1568 ctx->req.InputFormat = frames_ctx->
format;
1569 ctx->req.PictureTargetResolution =
ctx->resolution;
1571 hr = ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3,
1572 D3D12_FEATURE_VIDEO_ENCODER_RESOURCE_REQUIREMENTS,
1573 &
ctx->req,
sizeof(
ctx->req));
1575 av_log(avctx,
AV_LOG_ERROR,
"Failed to check encoder resource requirements support.\n");
1579 if (!
ctx->req.IsSupported) {
1586 if (!
ctx->output_buffer_pool)
1595 ID3D12CommandAllocator *command_allocator =
NULL;
1599 D3D12_COMMAND_QUEUE_DESC queue_desc = {
1600 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE,
1602 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
1608 if (!
ctx->allocator_queue)
1611 hr = ID3D12Device_CreateFence(
ctx->hwctx->device, 0, D3D12_FENCE_FLAG_NONE,
1612 &IID_ID3D12Fence, (
void **)&
ctx->sync_ctx.fence);
1619 ctx->sync_ctx.event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
1620 if (!
ctx->sync_ctx.event)
1627 hr = ID3D12Device_CreateCommandQueue(
ctx->hwctx->device, &queue_desc,
1628 &IID_ID3D12CommandQueue, (
void **)&
ctx->command_queue);
1635 hr = ID3D12Device_CreateCommandList(
ctx->hwctx->device, 0, queue_desc.Type,
1636 command_allocator,
NULL, &IID_ID3D12CommandList,
1637 (
void **)&
ctx->command_list);
1644 hr = ID3D12VideoEncodeCommandList2_Close(
ctx->command_list);
1651 ID3D12CommandQueue_ExecuteCommandLists(
ctx->command_queue, 1, (ID3D12CommandList **)&
ctx->command_list);
1692 hwctx->
resource_flags = D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY |
1693 D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
1694 if (
ctx->is_texture_array) {
1702 "frame context: %d.\n", err);
1730 D3D12_FEATURE_DATA_VIDEO_FEATURE_AREA_SUPPORT support = { 0 };
1731 D3D12_FEATURE_DATA_FORMAT_INFO format_info = { 0 };
1746 hr = ID3D12Device_QueryInterface(
ctx->hwctx->device, &IID_ID3D12Device3, (
void **)&
ctx->device3);
1753 hr = ID3D12Device3_QueryInterface(
ctx->device3, &IID_ID3D12VideoDevice3, (
void **)&
ctx->video_device3);
1760 if (FAILED(ID3D12VideoDevice3_CheckFeatureSupport(
ctx->video_device3, D3D12_FEATURE_VIDEO_FEATURE_AREA_SUPPORT,
1761 &support,
sizeof(support))) && !support.VideoEncodeSupport) {
1768 if (FAILED(ID3D12VideoDevice_CheckFeatureSupport(
ctx->hwctx->device, D3D12_FEATURE_FORMAT_INFO,
1769 &format_info,
sizeof(format_info)))) {
1774 ctx->plane_count = format_info.PlaneCount;
1780 if (
ctx->codec->set_tile) {
1781 err =
ctx->codec->set_tile(avctx);
1790 if (
ctx->codec->get_encoder_caps) {
1791 err =
ctx->codec->get_encoder_caps(avctx);
1806 "but this codec does not support controlling slices.\n");
1817 if (
ctx->codec->configure) {
1818 err =
ctx->codec->configure(avctx);
1823 if (
ctx->codec->init_sequence_params) {
1824 err =
ctx->codec->init_sequence_params(avctx);
1827 "failed: %d.\n", err);
1832 if (
ctx->codec->set_level) {
1833 err =
ctx->codec->set_level(avctx);
1867 int num_allocator = 0;
1873 if (!base_ctx->
frame)
1876 for (pic = base_ctx->
pic_start; pic; pic = next) {
1888 if (
ctx->allocator_queue) {
1894 av_log(avctx,
AV_LOG_VERBOSE,
"Total number of command allocators reused: %d\n", num_allocator);
1900 if (
ctx->sync_ctx.event)
1901 CloseHandle(
ctx->sync_ctx.event);