144#define STRING_OPTION(var_name, func_name, opt_name, default_value) do { \
145 if (s->var_name ## _string) { \
146 int var = av_ ## func_name ## _from_name(s->var_name ## _string); \
148 av_log(ctx, AV_LOG_ERROR, "Invalid %s.\n", opt_name); \
149 return AVERROR(EINVAL); \
153 s->var_name = default_value; \
167 HANDLE fence_event, DWORD timeout_ms)
169 int has_deadline = (timeout_ms != INFINITE);
170 ULONGLONG deadline = 0;
172 if (ID3D12Fence_GetCompletedValue(fence) >= target)
175 if (FAILED(ID3D12Fence_SetEventOnCompletion(fence, target, fence_event)))
179 deadline = GetTickCount64() + timeout_ms;
184 if (wait_result == WAIT_OBJECT_0) {
185 if (ID3D12Fence_GetCompletedValue(fence) >= target)
188 ULONGLONG now = GetTickCount64();
191 timeout_ms = (DWORD)(deadline - now);
201 ID3D12CommandAllocator **ppAllocator)
206 if (
av_fifo_peek(
s->allocator_queue, &allocator, 1, 0) >= 0) {
207 UINT64 completed = ID3D12Fence_GetCompletedValue(
s->fence);
216 hr = ID3D12Device_CreateCommandAllocator(
s->device, D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS,
217 &IID_ID3D12CommandAllocator, (
void **)ppAllocator);
227 ID3D12CommandAllocator *pAllocator,
228 UINT64 fence_value,
AVFrame *input_ref)
231 .command_allocator = pAllocator,
232 .fence_value = fence_value,
233 .input_ref = input_ref,
238 int confirmed_done =
s->fence &&
s->fence_event &&
241 if (confirmed_done) {
242 ID3D12CommandAllocator_Release(pAllocator);
246 "Failed to queue a command allocator for later reuse\n");
254 int gpu_confirmed_done = 1;
258 if (
s->gpu_work_submitted &&
s->command_queue &&
s->fence &&
s->fence_event) {
259 UINT64 drain_value =
s->fence_value + 1;
261 gpu_confirmed_done = 0;
263 if (SUCCEEDED(ID3D12CommandQueue_Signal(
s->command_queue,
s->fence, drain_value))) {
266 s->fence_value = drain_value + 1;
267 completed = ID3D12Fence_GetCompletedValue(
s->fence);
268 if (completed >= drain_value)
269 gpu_confirmed_done = 1;
272 s->fence_event, INFINITE);
275 if (!gpu_confirmed_done) {
276 HRESULT removed_reason = ID3D12Device_GetDeviceRemovedReason(
s->device);
277 gpu_confirmed_done = FAILED(removed_reason);
278 if (gpu_confirmed_done)
280 "D3D12 device removed (HRESULT 0x%lX) while tearing "
281 "down scale_d3d12\n", removed_reason);
285 if (!gpu_confirmed_done) {
287 "Could not confirm the GPU finished all scale_d3d12 work "
288 "during teardown\n");
293 if (
s->fence_event) {
294 CloseHandle(
s->fence_event);
295 s->fence_event =
NULL;
299 ID3D12Fence_Release(
s->fence);
303 if (
s->command_list) {
304 ID3D12VideoProcessCommandList_Release(
s->command_list);
305 s->command_list =
NULL;
308 if (
s->allocator_queue) {
310 while (
av_fifo_read(
s->allocator_queue, &allocator, 1) >= 0) {
317 if (
s->video_processor) {
318 ID3D12VideoProcessor_Release(
s->video_processor);
319 s->video_processor =
NULL;
322 if (
s->video_device) {
323 ID3D12VideoDevice_Release(
s->video_device);
324 s->video_device =
NULL;
327 if (
s->command_queue) {
328 ID3D12CommandQueue_Release(
s->command_queue);
329 s->command_queue =
NULL;
332 s->gpu_work_submitted = 0;
399 DXGI_COLOR_SPACE_TYPE input_space,
400 DXGI_COLOR_SPACE_TYPE output_space,
401 int explicit_request,
406 (output_space == DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709) ?
412 (input_space == DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709) ?
415 if (canonical_input != canonical) {
416 if (*trc != canonical) {
420 "No exact DXGI color space; retagging transfer "
421 "characteristic from %s to %s\n",
422 old_name ? old_name :
"unknown", new_name ? new_name :
"unknown");
429 if (!explicit_request)
439 "Cannot verify requested transfer characteristic (%s); "
440 "input's curve is unspecified/unknown\n",
441 req_name ? req_name :
"requested value");
445 "Requested transfer characteristic (%s) differs from "
446 "input's (%s); same DXGI color space means no curve "
448 req_name ? req_name :
"requested value",
449 old_name ? old_name :
"transfer characteristic");
458 const char *space_description)
465 "%s forces fixed primaries; retagging output from "
468 old_name ? old_name :
"unknown", new_name ? new_name :
"unknown");
471 "%s forces fixed primaries, which disagrees with "
472 "this input's %s primaries tag\n",
473 space_description, old_name ? old_name :
"unknown");
491 "This DXGI color space has no chroma entry other than "
492 "left-sited; the output will actually be left-sited\n");
498 "This DXGI color space has no chroma entry other than "
499 "left-sited, but this input is tagged with a different siting\n");
512 "HLG has no DXGI entry with any chroma siting other than "
513 "top-left; the output will actually be top-left-sited\n");
519 "HLG has no DXGI entry with any chroma siting other than "
520 "top-left, but this input is tagged with a different siting\n");
535 "DXGI BT.2020 only supports top-left/left chroma "
536 "siting; output will use one of those instead\n");
542 "DXGI BT.2020 only supports top-left/left chroma siting, "
543 "but this input is tagged with a different siting\n");
555 DXGI_COLOR_SPACE_TYPE *out_space)
563 "BT.2020 constant-luminance has no DXGI color space; "
564 "retagging output as bt2020nc\n");
568 "BT.2020 constant-luminance matrix has no DXGI color "
569 "space; cannot process this input without misinterpreting\n");
578 "%s matrix has no DXGI YCbCr color space; only BT.601, "
579 "BT.709 or BT.2020 are supported\n",
name ?
name :
"this");
588 "%s transfer has no DXGI YCbCr color space; only SDR "
589 "gamma, smpte2084 or HLG (arib-std-b67) are "
608 *out_space =
full_range ? DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020
609 : DXGI_COLOR_SPACE_RGB_STUDIO_G2084_NONE_P2020;
616 "HLG has no RGB DXGI color space; use a YUV "
617 "output format or a supported out_color_transfer\n");
620 "HLG transfer characteristic has no RGB DXGI color "
621 "space; this RGB input cannot be processed\n");
628 "Studio range linear RGB has no DXGI color "
629 "space; use out_range=full, or a non-linear "
630 "out_color_transfer (e.g. bt709)\n");
633 "Studio range linear RGB has no DXGI color "
634 "space; this input cannot be processed\n");
638 *out_space = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
646 "%s transfer has no RGB DXGI color space; only "
647 "SDR gamma, linear or smpte2084 are "
648 "supported\n",
name ?
name :
"this");
652 if (primaries_is_2020) {
653 *out_space =
full_range ? DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P2020
654 : DXGI_COLOR_SPACE_RGB_STUDIO_G22_NONE_P2020;
657 *out_space =
full_range ? DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
658 : DXGI_COLOR_SPACE_RGB_STUDIO_G22_NONE_P709;
665 "this RGB transfer characteristic");
678 "No full-range smpte2084 YCbCr DXGI color space "
679 "exists; cannot process this %s\n",
680 is_output ?
"output request" :
"input");
684 *out_space = want_topleft ? DXGI_COLOR_SPACE_YCBCR_STUDIO_G2084_TOPLEFT_P2020
685 : DXGI_COLOR_SPACE_YCBCR_STUDIO_G2084_LEFT_P2020;
690 *out_space =
full_range ? DXGI_COLOR_SPACE_YCBCR_FULL_GHLG_TOPLEFT_P2020
691 : DXGI_COLOR_SPACE_YCBCR_STUDIO_GHLG_TOPLEFT_P2020;
696 "Full-range BT.2020 SDR has no top-left-sited "
697 "DXGI YCbCr color space\n");
702 "Full-range BT.2020 SDR has no top-left-sited DXGI "
703 "YCbCr color space; falling back to left-sited\n");
707 *out_space = DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_TOPLEFT_P2020;
709 *out_space =
full_range ? DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P2020
710 : DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P2020;
721 "%s transfer has no BT.601 DXGI color space; use "
722 "out_color_matrix=bt2020nc or an SDR transfer\n",
726 "%s transfer has no BT.601 DXGI color space; this "
727 "input cannot be processed.\n",
735 *out_space =
full_range ? DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P601
736 : DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601;
748 "DXGI's BT.601 color spaces cannot distinguish "
749 "PAL/NTSC primaries (%s input, %s output)\n",
750 in_name ? in_name :
"unknown", out_name ? out_name :
"unknown");
757 "BT.601 cannot change primaries; tagging output "
759 new_name ? new_name :
"unknown");
762 }
else if (!is_output) {
767 "BT.601 only represents PAL/NTSC primaries, "
768 "which disagrees with this input's %s tag\n",
769 old_name ? old_name :
"unknown");
777 "this BT.601 matrix");
787 "%s has no BT.709 DXGI space; use "
788 "out_color_matrix=bt2020nc or SDR\n",
792 "%s has no BT.709 DXGI space; retag/convert to "
793 "BT.2020 or SDR before scale_d3d12\n",
801 *out_space =
full_range ? DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P709
802 : DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709;
805 "the default BT.709 matrix");
816 case DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_TOPLEFT_P2020:
817 case DXGI_COLOR_SPACE_YCBCR_STUDIO_G2084_TOPLEFT_P2020:
818 case DXGI_COLOR_SPACE_YCBCR_STUDIO_GHLG_TOPLEFT_P2020:
819 case DXGI_COLOR_SPACE_YCBCR_FULL_GHLG_TOPLEFT_P2020:
820 case DXGI_COLOR_SPACE_YCBCR_STUDIO_G24_TOPLEFT_P2020:
830 D3D12_FEATURE_DATA_VIDEO_PROCESS_SUPPORT
probe;
837 DXGI_COLOR_SPACE_TYPE candidate_space;
845 &candidate_range, &candidate_primaries,
848 &candidate_space) < 0)
851 probe =
s->process_support;
852 probe.OutputFormat.Format =
map->dxgi_format;
853 probe.OutputFormat.ColorSpace = candidate_space;
855 hr = ID3D12VideoDevice_CheckFeatureSupport(
s->video_device, D3D12_FEATURE_VIDEO_PROCESS_SUPPORT,
860 (SUCCEEDED(hr) && (
probe.SupportFlags & D3D12_VIDEO_PROCESS_SUPPORT_FLAG_SUPPORTED)) ?
861 "supported" :
"unsupported");
888 ID3D12CommandAllocator *command_allocator =
NULL;
890 if (
s->video_device ||
s->command_queue ||
s->fence ||
s->fence_event ||
891 s->command_list ||
s->video_processor ||
s->allocator_queue) {
894 "Cannot configure a new D3D12 video processor\n");
899 if (
s->output_format == DXGI_FORMAT_UNKNOWN) {
906 s->device = d3d12_hwctx->
device;
909 s->input_width,
s->input_height,
s->width,
s->height);
911 hr = ID3D12Device_QueryInterface(
s->device, &IID_ID3D12VideoDevice, (
void **)&
s->video_device);
918 D3D12_COMMAND_QUEUE_DESC queue_desc = {
919 .Type = D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS,
920 .Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
921 .Flags = D3D12_COMMAND_QUEUE_FLAG_NONE,
925 hr = ID3D12Device_CreateCommandQueue(
s->device, &queue_desc, &IID_ID3D12CommandQueue, (
void **)&
s->command_queue);
933 s->process_support.NodeIndex = 0;
935 s->process_support.InputSample.Format.Format =
s->input_format;
936 s->process_support.InputSample.Format.ColorSpace =
s->input_colorspace;
937 s->process_support.InputSample.Width =
s->input_width;
938 s->process_support.InputSample.Height =
s->input_height;
939 s->process_support.InputFrameRate.Numerator =
s->input_framerate.num;
940 s->process_support.InputFrameRate.Denominator =
s->input_framerate.den;
941 s->process_support.InputFieldType = D3D12_VIDEO_FIELD_TYPE_NONE;
942 s->process_support.InputStereoFormat = D3D12_VIDEO_FRAME_STEREO_FORMAT_NONE;
944 s->process_support.OutputFormat.Format =
s->output_format;
945 s->process_support.OutputFormat.ColorSpace =
s->output_colorspace;
946 s->process_support.OutputFrameRate.Numerator =
s->input_framerate.num;
947 s->process_support.OutputFrameRate.Denominator =
s->input_framerate.den;
948 s->process_support.OutputStereoFormat = D3D12_VIDEO_FRAME_STEREO_FORMAT_NONE;
950 hr = ID3D12VideoDevice_CheckFeatureSupport(
952 D3D12_FEATURE_VIDEO_PROCESS_SUPPORT,
954 sizeof(
s->process_support)
964 if (!(
s->process_support.SupportFlags & D3D12_VIDEO_PROCESS_SUPPORT_FLAG_SUPPORTED)) {
966 "D3D12 video processor does not support converting %s (colorspace %d) to %s "
967 "(colorspace %d) on this device\n",
973 s->output_colorspace);
980 int want_alpha_passthrough =
s->input_has_alpha &&
s->output_has_alpha;
981 if (want_alpha_passthrough &&
982 !(
s->process_support.FeatureSupport & D3D12_VIDEO_PROCESS_FEATURE_FLAG_ALPHA_FILL)) {
984 "Device lacks ALPHA_FILL support; output alpha will be "
985 "forced opaque instead of preserved\n");
986 want_alpha_passthrough = 0;
988 s->alpha_passthrough = want_alpha_passthrough;
990 D3D12_VIDEO_PROCESS_OUTPUT_STREAM_DESC processor_output_desc = {
991 .Format =
s->output_format,
992 .ColorSpace =
s->output_colorspace,
993 .AlphaFillMode = want_alpha_passthrough ? D3D12_VIDEO_PROCESS_ALPHA_FILL_MODE_SOURCE_STREAM
994 : D3D12_VIDEO_PROCESS_ALPHA_FILL_MODE_OPAQUE,
995 .AlphaFillModeSourceStreamIndex = 0,
996 .BackgroundColor = { 0.0f, 0.0f, 0.0f, 1.0f },
997 .FrameRate = {
s->input_framerate.num,
s->input_framerate.den },
998 .EnableStereo = FALSE,
1001 D3D12_VIDEO_PROCESS_INPUT_STREAM_DESC processor_input_desc = {
1002 .Format =
s->input_format,
1003 .ColorSpace =
s->input_colorspace,
1004 .SourceAspectRatio = { 1, 1 },
1005 .DestinationAspectRatio = { 1, 1 },
1006 .FrameRate = {
s->input_framerate.num,
s->input_framerate.den },
1007 .StereoFormat = D3D12_VIDEO_FRAME_STEREO_FORMAT_NONE,
1008 .FieldType = D3D12_VIDEO_FIELD_TYPE_NONE,
1009 .DeinterlaceMode = D3D12_VIDEO_PROCESS_DEINTERLACE_FLAG_NONE,
1010 .EnableOrientation = FALSE,
1011 .FilterFlags = D3D12_VIDEO_PROCESS_FILTER_FLAG_NONE,
1012 .SourceSizeRange = {
1013 .MaxWidth =
s->input_width,
1014 .MaxHeight =
s->input_height,
1015 .MinWidth =
s->input_width,
1016 .MinHeight =
s->input_height
1018 .DestinationSizeRange = {
1019 .MaxWidth =
s->width,
1020 .MaxHeight =
s->height,
1021 .MinWidth =
s->width,
1022 .MinHeight =
s->height
1024 .EnableAlphaBlending = FALSE,
1025 .LumaKey = { .Enable = FALSE, .Lower = 0.0f, .Upper = 1.0f },
1027 .NumFutureFrames = 0,
1028 .EnableAutoProcessing = FALSE,
1031 hr = ID3D12VideoDevice_CreateVideoProcessor(
1034 &processor_output_desc,
1036 &processor_input_desc,
1037 &IID_ID3D12VideoProcessor,
1038 (
void **)&
s->video_processor
1047 hr = ID3D12Device_CreateFence(
s->device, 0, D3D12_FENCE_FLAG_NONE, &IID_ID3D12Fence, (
void **)&
s->fence);
1055 s->fence_event = CreateEvent(
NULL, FALSE, FALSE,
NULL);
1056 if (!
s->fence_event) {
1064 if (!
s->allocator_queue) {
1075 hr = ID3D12Device_CreateCommandList(
1078 D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS,
1081 &IID_ID3D12VideoProcessCommandList,
1082 (
void **)&
s->command_list
1087 ID3D12CommandAllocator_Release(command_allocator);
1093 hr = ID3D12VideoProcessCommandList_Close(
s->command_list);
1097 ID3D12CommandAllocator_Release(command_allocator);
1122 ID3D12CommandAllocator *command_allocator =
NULL;
1123 HRESULT removed_reason;
1124 int gpu_confirmed_done;
1140 if (!
s->hw_device_ctx) {
1150 if (input_device_ctx->
type != filter_device_ctx->
type) {
1159 if (input_d3d12_hwctx->
device != filter_d3d12_hwctx->
device) {
1161 "Input frame's hw_frames_ctx belongs to a different "
1162 "ID3D12Device than this filter's own device\n");
1182 if (!
s->configured) {
1190 s->input_width = in->
width;
1194 s->input_format = input_hwctx->
format;
1196 if (
s->input_format == DXGI_FORMAT_UNKNOWN) {
1208 s->input_is_rgb = in_map ? in_map->
is_rgb : 0;
1224 s->input_av_color_primaries = in_primaries;
1225 s->input_av_color_trc = in_trc;
1228 if (
s->input_colorspace == DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601 ||
1229 s->input_colorspace == DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P601)
1230 input_601_hint = in_primaries;
1235 s->output_is_rgb = out_map ? out_map->
is_rgb : 0;
1254 out_range =
s->color_range;
1267 input_601_hint, &
s->output_colorspace);
1280 if (
s->output_is_rgb)
1283 s->output_av_colorspace = out_spc;
1290 s->output_av_color_range = out_range;
1291 s->output_av_color_primaries = out_primaries;
1292 s->output_av_color_trc = out_trc;
1300 "Negotiated output color space (%s) does not match the "
1301 "%s produced from this input; set an explicit "
1302 "out_color_matrix\n",
1311 "Negotiated output color range (%s) does not match the "
1312 "%s produced from this input; set an explicit out_range\n",
1318 if (!
s->output_is_rgb) {
1323 "Negotiated output chroma siting does not match the "
1324 "siting produced from this input\n");
1333 "Input format: %dx%d, DXGI format: %d, colorspace: %d -> "
1334 "Output DXGI format: %d, colorspace: %d, framerate: %d/%d\n",
1335 s->input_width,
s->input_height,
s->input_format,
s->input_colorspace,
1336 s->output_format,
s->output_colorspace,
1337 s->input_framerate.num,
s->input_framerate.den);
1350 (!
s->output_has_alpha || !
s->alpha_passthrough ||
1351 s->input_colorspace !=
s->output_colorspace)) {
1353 "Input has premultiplied alpha, which this conversion "
1354 "cannot preserve; unpremultiply before scale_d3d12\n");
1368 ID3D12Resource *input_resource = input_frame->
texture;
1369 ID3D12Resource *output_resource =
output_frame->texture;
1371 if (!input_resource || !output_resource) {
1381 hr = ID3D12CommandAllocator_Reset(command_allocator);
1388 hr = ID3D12VideoProcessCommandList_Reset(
s->command_list, command_allocator);
1392 goto force_reconfigure;
1395 D3D12_RESOURCE_DESC input_res_desc, output_res_desc;
1396 input_resource->lpVtbl->GetDesc(input_resource, &input_res_desc);
1397 output_resource->lpVtbl->GetDesc(output_resource, &output_res_desc);
1399 int input_nb_planes =
s->input_is_rgb ? 1 : 2;
1400 int output_nb_planes =
s->output_is_rgb ? 1 : 2;
1402 D3D12_RESOURCE_BARRIER barriers[4];
1403 int nb_barriers = 0;
1405 for (
int p = 0; p < input_nb_planes; p++) {
1406 D3D12_RESOURCE_BARRIER
b = {
1407 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
1408 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
1410 .pResource = input_resource,
1411 .Subresource = input_frame->
subresource_index + p * input_res_desc.DepthOrArraySize,
1412 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
1413 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_PROCESS_READ
1416 barriers[nb_barriers++] =
b;
1418 for (
int p = 0; p < output_nb_planes; p++) {
1419 D3D12_RESOURCE_BARRIER
b = {
1420 .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
1421 .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
1423 .pResource = output_resource,
1424 .Subresource =
output_frame->subresource_index + p * output_res_desc.DepthOrArraySize,
1425 .StateBefore = D3D12_RESOURCE_STATE_COMMON,
1426 .StateAfter = D3D12_RESOURCE_STATE_VIDEO_PROCESS_WRITE
1429 barriers[nb_barriers++] =
b;
1432 ID3D12VideoProcessCommandList_ResourceBarrier(
s->command_list, nb_barriers, barriers);
1434 D3D12_VIDEO_PROCESS_INPUT_STREAM_ARGUMENTS input_args = {0};
1439 input_args.InputStream[0].pTexture2D = input_resource;
1441 input_args.Transform.SourceRectangle.right =
s->input_width;
1442 input_args.Transform.SourceRectangle.bottom =
s->input_height;
1443 input_args.Transform.DestinationRectangle.right =
s->width;
1444 input_args.Transform.DestinationRectangle.bottom =
s->height;
1445 input_args.Transform.Orientation = D3D12_VIDEO_PROCESS_ORIENTATION_DEFAULT;
1447 input_args.Flags = D3D12_VIDEO_PROCESS_INPUT_STREAM_FLAG_NONE;
1449 input_args.RateInfo.OutputIndex = 0;
1450 input_args.RateInfo.InputFrameOrField = 0;
1452 memset(input_args.FilterLevels, 0,
sizeof(input_args.FilterLevels));
1454 input_args.AlphaBlending.Enable = FALSE;
1455 input_args.AlphaBlending.Alpha = 1.0f;
1457 D3D12_VIDEO_PROCESS_OUTPUT_STREAM_ARGUMENTS output_args = {0};
1460 output_args.OutputStream[0].pTexture2D = output_resource;
1461 output_args.OutputStream[0].Subresource =
output_frame->subresource_index;
1462 output_args.TargetRectangle.right =
s->width;
1463 output_args.TargetRectangle.bottom =
s->height;
1465 ID3D12VideoProcessCommandList_ProcessFrames(
1473 for (
int i = 0;
i < nb_barriers;
i++) {
1474 FFSWAP(D3D12_RESOURCE_STATES, barriers[
i].Transition.StateBefore, barriers[
i].Transition.StateAfter);
1476 ID3D12VideoProcessCommandList_ResourceBarrier(
s->command_list, nb_barriers, barriers);
1478 hr = ID3D12VideoProcessCommandList_Close(
s->command_list);
1482 goto force_reconfigure;
1485 int wait_enqueued = 0;
1490 UINT64 completed = ID3D12Fence_GetCompletedValue(input_frame->
sync_ctx.
fence);
1491 if (completed < input_frame->sync_ctx.fence_value) {
1499 s->gpu_work_submitted = 1;
1504 UINT64 completed = ID3D12Fence_GetCompletedValue(
output_frame->sync_ctx.fence);
1505 if (completed < output_frame->sync_ctx.fence_value) {
1514 s->gpu_work_submitted = 1;
1518 ID3D12CommandList *cmd_lists[] = { (ID3D12CommandList *)
s->command_list };
1519 ID3D12CommandQueue_ExecuteCommandLists(
s->command_queue, 1, cmd_lists);
1520 s->gpu_work_submitted = 1;
1522 UINT64 next_output_fence_value =
output_frame->sync_ctx.fence_value + 1;
1523 UINT64 next_private_fence_value =
s->fence_value;
1525 hr = ID3D12CommandQueue_Signal(
s->command_queue,
output_frame->sync_ctx.fence,
1526 next_output_fence_value);
1532 output_frame->sync_ctx.fence_value = next_output_fence_value;
1534 hr = ID3D12CommandQueue_Signal(
s->command_queue,
s->fence, next_private_fence_value);
1551 command_allocator =
NULL;
1561 int in_width = in->
width;
1562 int in_height = in->
height;
1564 out->width =
s->width;
1565 out->height =
s->height;
1568 if (
out->width != in_width ||
out->height != in_height)
1569 av_reduce(&
out->sample_aspect_ratio.num, &
out->sample_aspect_ratio.den,
1574 if (
out->width != in_width ||
out->height != in_height) {
1577 out->crop_left =
out->crop_right =
out->crop_top =
out->crop_bottom = 0;
1580 out->colorspace =
s->output_av_colorspace;
1581 out->color_range =
s->output_av_color_range;
1582 out->color_primaries =
s->output_av_color_primaries;
1583 out->color_trc =
s->output_av_color_trc;
1585 if (
out->color_primaries !=
s->input_av_color_primaries ||
out->color_trc !=
s->input_av_color_trc)
1589 if (!
s->output_has_alpha)
1592 if (
s->output_is_rgb) {
1603 removed_reason = ID3D12Device_GetDeviceRemovedReason(
s->device);
1604 gpu_confirmed_done = FAILED(removed_reason);
1606 if (gpu_confirmed_done) {
1608 "D3D12 device removed (HRESULT 0x%lX); frame "
1609 "resources are safe to release\n", removed_reason);
1610 }
else if (
s->command_queue &&
s->fence &&
s->fence_event) {
1611 UINT64 last_resort =
s->fence_value + 1;
1612 if (SUCCEEDED(ID3D12CommandQueue_Signal(
s->command_queue,
s->fence, last_resort))) {
1615 if (last_resort >=
s->fence_value)
1616 s->fence_value = last_resort + 1;
1618 completed = ID3D12Fence_GetCompletedValue(
s->fence);
1619 if (completed >= last_resort) {
1620 gpu_confirmed_done = 1;
1623 s->fence_event, 5000);
1624 if (!gpu_confirmed_done)
1626 "Post-failure GPU wait timed out; frame "
1627 "resources cannot be safely recycled\n");
1631 "Best-effort post-failure GPU sync Signal also "
1632 "failed; frame resources cannot be safely "
1637 "No fence/queue for post-failure GPU sync; frame "
1638 "resources cannot be safely recycled\n");
1641 if (!gpu_confirmed_done) {
1643 "Abandoning this frame's input/output surfaces and "
1644 "command allocator without releasing them\n");
1653 command_allocator =
NULL;
1656 if (command_allocator)
1657 ID3D12CommandAllocator_Release(command_allocator);
1674 "Cannot (re)configure scale_d3d12: a prior configuration's "
1675 "D3D12 resources could not be confirmed released\n");
1693 outlink->
w =
s->width;
1694 outlink->
h =
s->height;
1709 if (!
s->hw_device_ctx) {
1712 if (!
s->hw_device_ctx) {
1721 s->device = d3d12_hwctx->
device;
1729 if (!
s->hw_frames_ctx_out)
1738 resolved_format = in_frames_ctx->
sw_format;
1742 frames_ctx->
sw_format = resolved_format;
1754 s->output_is_rgb = out_map->
is_rgb;
1757 int input_is_rgb = in_map ? in_map->
is_rgb : 0;
1762 input_is_rgb !=
s->output_is_rgb ||
1768 frames_ctx->
width =
s->width;
1769 frames_ctx->
height =
s->height;
1773 if (
ctx->extra_hw_frames > 0)
1782 frames_hwctx->
format =
s->output_format;
1783 frames_hwctx->
resource_flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
1784 frames_hwctx->
heap_flags = D3D12_HEAP_FLAG_NONE;
1797 inlink->
w, inlink->
h, outlink->
w, outlink->
h);
1834 static const int out_chroma_locs_any[] = {
1837 static const int out_chroma_locs_left[] = {
1843 int out_matrix_known;
1862 if (out_matrix_known)
1904#define OFFSET(x) offsetof(ScaleD3D12Context, x)
1905#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1913 {
"out_color_matrix",
"Output color matrix coefficient set",
1915 {
"out_range",
"Output color range",
1924 {
"out_color_primaries",
"Output color primaries",
1926 {
"out_color_transfer",
"Output color transfer characteristics",
1935 .p.name =
"scale_d3d12",
1938 .p.priv_class = &scale_d3d12_class,
static int probe(const AVProbeData *p)
static const char *const format[]
const FFFilter ff_vf_scale_d3d12
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define i(width, name, range_min, range_max)
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
@ DXGI_FORMAT_B8G8R8A8_UNORM
@ DXGI_FORMAT_R16G16B16A16_FLOAT
@ DXGI_FORMAT_R8G8B8A8_UNORM
static enum AVPixelFormat pix_fmt
reference-counted frame API
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AVERROR_EXTERNAL
Generic error in an external library.
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd, int props)
Remove and free all side data instances that match any of the given side data properties.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
@ AV_SIDE_DATA_PROP_SIZE_DEPENDENT
Side data depends on the video dimensions.
@ AV_SIDE_DATA_PROP_COLOR_DEPENDENT
Side data depends on the video color space.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_log_get_level(void)
Get the current log level.
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
An API-specific header for AV_HWDEVICE_TYPE_D3D12VA.
const VDPAUPixFmtMap * map
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
static FilterLink * ff_filter_link(AVFilterLink *link)
#define AVFILTER_DEFINE_CLASS(fname)
#define FILTER_QUERY_FUNC2(func)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
#define FFSWAP(type, a, b)
const char * av_color_space_name(enum AVColorSpace space)
const char * av_color_range_name(enum AVColorRange range)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
AVChromaLocation
Location of chroma samples.
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
@ AVCHROMA_LOC_UNSPECIFIED
#define AV_PIX_FMT_RGBAF16
AVColorRange
Visual content value range.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_JPEG
Full range content.
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
@ AVALPHA_MODE_PREMULTIPLIED
Alpha channel is multiplied into color values.
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
#define AV_PIX_FMT_X2BGR10
AVColorPrimaries
Chromaticity coordinates of the source primaries.
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
@ AVCOL_PRI_BT2020
ITU-R BT2020.
AVColorTransferCharacteristic
Color Transfer Characteristic.
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
@ AVCOL_TRC_BT709
also ITU-R BT1361
AVColorSpace
YUV colorspace type.
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by, double w_adj)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
int ff_scale_eval_dimensions(void *log_ctx, const char *w_expr, const char *h_expr, AVFilterLink *inlink, AVFilterLink *outlink, int *ret_w, int *ret_h)
Parse and evaluate string expressions for width and height.
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
uint8_t * data
The data buffer.
Describe the class of an AVClass context structure.
This struct is allocated as AVHWDeviceContext.hwctx.
ID3D12Device * device
Device used for objects creation and access.
D3D12VA frame descriptor for pool allocation.
ID3D12Resource * texture
The texture in which the frame is located.
AVD3D12VASyncContext sync_ctx
The sync context for the texture.
int subresource_index
Index of the subresource within the texture.
This struct is allocated as AVHWFramesContext.hwctx.
D3D12_RESOURCE_FLAGS resource_flags
Options for working with resources.
D3D12_HEAP_FLAGS heap_flags
Options for working with heaps allocation when creating resources.
DXGI_FORMAT format
DXGI_FORMAT format.
ID3D12Fence * fence
D3D12 fence object.
uint64_t fence_value
The fence value used for sync.
void * priv
private data for use by the filter
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
enum AVChromaLocation chroma_location
Agreed upon chroma sample location, for video with subsampled chroma.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
enum AVColorSpace colorspace
For non-YUV links, these are respectively set to fallback values (as appropriate for that colorspace)...
AVFilterContext * dst
dest filter
enum AVColorRange color_range
agreed upon YUV color range
AVFrameSideData ** side_data
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
enum AVChromaLocation chroma_location
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
enum AVColorPrimaries color_primaries
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
enum AVColorSpace colorspace
YUV colorspace type.
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is to be handled.
enum AVColorTransferCharacteristic color_trc
int64_t duration
Duration of the frame, in the same units as pts.
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
This struct describes a set or pool of "hardware" frames (i.e.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int initial_pool_size
Initial size of the frame pool.
int width
The allocated dimensions of the frames in this pool.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Rational number (pair of numerator and denominator).
ID3D12CommandAllocator * command_allocator
Link properties exposed to filter code, but not external callers.
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
ID3D12VideoProcessor * video_processor
AVBufferRef * hw_device_ctx
ID3D12VideoDevice * video_device
enum AVColorRange output_av_color_range
enum AVColorPrimaries color_primaries
DXGI_FORMAT output_format
enum AVPixelFormat format
AVRational input_framerate
AVBufferRef * hw_frames_ctx_out
enum AVColorSpace output_av_colorspace
ID3D12VideoProcessCommandList * command_list
enum AVColorPrimaries input_av_color_primaries
D3D12_FEATURE_DATA_VIDEO_PROCESS_SUPPORT process_support
char * color_matrix_string
enum AVColorTransferCharacteristic color_transfer
enum AVColorSpace color_matrix
enum AVColorPrimaries output_av_color_primaries
DXGI_COLOR_SPACE_TYPE output_colorspace
enum AVColorTransferCharacteristic input_av_color_trc
char * color_transfer_string
ID3D12CommandQueue * command_queue
DXGI_COLOR_SPACE_TYPE input_colorspace
enum AVColorTransferCharacteristic output_av_color_trc
char * color_primaries_string
static const Range full_range
static const AVFilterPad scale_d3d12_inputs[]
static int scale_d3d12_filter_frame(AVFilterLink *inlink, AVFrame *in)
static int d3d12_scale_known_matrix(enum AVColorSpace spc)
static int release_d3d12_resources(ScaleD3D12Context *s, AVFilterContext *ctx)
static av_cold int scale_d3d12_init(AVFilterContext *ctx)
static AVRational get_input_framerate(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in)
static int scale_d3d12_query_formats(const AVFilterContext *avctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int scale_d3d12_discard_command_allocator(ScaleD3D12Context *s, AVFilterContext *ctx, ID3D12CommandAllocator *pAllocator, UINT64 fence_value, AVFrame *input_ref)
static int d3d12_scale_reconcile_primaries(AVFilterContext *ctx, int is_output, enum AVColorPrimaries *primaries, enum AVColorPrimaries expected_primaries, const char *space_description)
static const D3D12ScaleFormatMap * d3d12_scale_map_from_pixfmt(enum AVPixelFormat fmt)
static int scale_d3d12_wait_fence_exact(ID3D12Fence *fence, UINT64 target, HANDLE fence_event, DWORD timeout_ms)
static int d3d12_scale_trc_group(enum AVColorTransferCharacteristic trc)
static enum AVColorSpace d3d12_scale_matrix_from_primaries(enum AVColorPrimaries primaries)
static av_cold void scale_d3d12_uninit(AVFilterContext *ctx)
static int d3d12_scale_reject_unrepresentable_siting_2020(AVFilterContext *ctx, int is_output, enum AVChromaLocation chroma_loc)
#define STRING_OPTION(var_name, func_name, opt_name, default_value)
static const AVFilterPad scale_d3d12_outputs[]
static const D3D12ScaleFormatMap d3d12_scale_format_map[]
static int scale_d3d12_config_props(AVFilterLink *outlink)
static void scale_d3d12_report_output_support(ScaleD3D12Context *s, AVFilterContext *ctx)
static const D3D12ScaleFormatMap * d3d12_scale_map_from_dxgi(DXGI_FORMAT fmt)
static const AVOption scale_d3d12_options[]
static int d3d12_scale_known_sdr_trc(enum AVColorTransferCharacteristic trc)
static int scale_d3d12_get_valid_command_allocator(ScaleD3D12Context *s, AVFilterContext *ctx, ID3D12CommandAllocator **ppAllocator)
static int d3d12_scale_finalize_output_trc(AVFilterContext *ctx, DXGI_COLOR_SPACE_TYPE input_space, DXGI_COLOR_SPACE_TYPE output_space, int explicit_request, enum AVColorTransferCharacteristic input_trc, enum AVColorTransferCharacteristic *trc)
static int d3d12_scale_reject_unrepresentable_left(AVFilterContext *ctx, int is_output, enum AVChromaLocation chroma_loc)
static int d3d12_scale_reject_unrepresentable_nonleft(AVFilterContext *ctx, int is_output, enum AVChromaLocation chroma_loc)
static int scale_d3d12_configure_processor(ScaleD3D12Context *s, AVFilterContext *ctx)
static enum AVChromaLocation d3d12_scale_dxgi_chroma_location(DXGI_COLOR_SPACE_TYPE space)
static int d3d12_scale_get_colorspace(AVFilterContext *ctx, int is_rgb, int is_output, enum AVColorSpace *spc, enum AVColorRange *range, enum AVColorPrimaries *primaries, enum AVColorTransferCharacteristic *trc, enum AVChromaLocation chroma_loc, enum AVColorPrimaries input_601_hint, DXGI_COLOR_SPACE_TYPE *out_space)
#define WaitForSingleObject(a, b)