FFmpeg
Loading...
Searching...
No Matches
d3d12_vpp.c
Go to the documentation of this file.
1/*
2 * D3D12VA video processor helpers
3 *
4 * Copyright (c) 2026 Advanced Micro Devices, Inc.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#define COBJMACROS
24
25#include "libavutil/log.h"
26#include "libavutil/pixdesc.h"
27
28#include "d3d12_vpp.h"
29
31{
32 if (vpp->video_processor) {
33 ID3D12VideoProcessor_Release(vpp->video_processor);
34 vpp->video_processor = NULL;
35 }
36
37 if (vpp->video_device) {
38 ID3D12VideoDevice_Release(vpp->video_device);
39 vpp->video_device = NULL;
40 }
41}
42
43/* The command context tears itself down after a command list error; the
44 * video processor then has to be configured again. */
46{
47 if (vpp->cmd.ready)
48 return;
49
50 vpp->configured = 0;
51 if (!vpp->cmd.queue)
53}
54
56{
57 int ret;
58
59 vpp->configured = 0;
60
62 if (ret < 0)
63 return ret;
64
66
67 return 0;
68}
69
70int ff_d3d12_vpp_init(AVFilterContext *ctx, D3D12VPPContext *vpp, ID3D12Device *device)
71{
72 HRESULT hr;
73 int ret;
74
75 if (ff_d3d12_vpp_uninit(ctx, vpp) < 0) {
77 "Cannot configure a new D3D12 video processor\n");
78 return AVERROR_EXTERNAL;
79 }
80
81 hr = ID3D12Device_QueryInterface(device, &IID_ID3D12VideoDevice, (void **)&vpp->video_device);
82 if (FAILED(hr)) {
83 av_log(ctx, AV_LOG_ERROR, "Failed to get D3D12 video device interface: HRESULT 0x%lX\n", hr);
84
85 return AVERROR_EXTERNAL;
86 }
87
88 ret = ff_d3d12_cmd_init(ctx, &vpp->cmd, device, D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS);
89 if (ret < 0) {
91 return ret;
92 }
93
94 return 0;
95}
96
98 const D3D12_FEATURE_DATA_VIDEO_PROCESS_SUPPORT *support)
99{
100 D3D12_FEATURE_DATA_VIDEO_PROCESS_SUPPORT probe;
101 const D3D12FilterFormat *map;
102 void *iter = NULL;
103
105 return;
106
107 while ((map = ff_d3d12_filter_format_iterate(&iter))) {
108 DXGI_COLOR_SPACE_TYPE candidate_space;
109 enum AVColorRange candidate_range = map->is_rgb ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
110 enum AVColorSpace candidate_spc = AVCOL_SPC_UNSPECIFIED;
111 enum AVColorPrimaries candidate_primaries = AVCOL_PRI_UNSPECIFIED;
113 HRESULT hr;
114
115 if (ff_d3d12_filter_get_colorspace(ctx, map->is_rgb, 1, &candidate_spc,
116 &candidate_range, &candidate_primaries,
117 &candidate_trc, AVCHROMA_LOC_UNSPECIFIED,
119 &candidate_space) < 0)
120 continue;
121
122 probe = *support;
123 probe.OutputFormat.Format = map->dxgi_format;
124 probe.OutputFormat.ColorSpace = candidate_space;
125
126 hr = ID3D12VideoDevice_CheckFeatureSupport(vpp->video_device, D3D12_FEATURE_VIDEO_PROCESS_SUPPORT,
127 &probe, sizeof(probe));
128
129 av_log(ctx, AV_LOG_DEBUG, "Probe output %s (DXGI %d, colorspace %d): %s\n",
130 av_get_pix_fmt_name(map->pix_fmt), map->dxgi_format, candidate_space,
131 (SUCCEEDED(hr) && (probe.SupportFlags & D3D12_VIDEO_PROCESS_SUPPORT_FLAG_SUPPORTED)) ?
132 "supported" : "unsupported");
133 }
134}
135
137 const D3D12_VIDEO_PROCESS_OUTPUT_STREAM_DESC *output_desc,
138 const D3D12_VIDEO_PROCESS_INPUT_STREAM_DESC *input_desc)
139{
140 HRESULT hr;
141
142 hr = ID3D12VideoDevice_CreateVideoProcessor(
143 vpp->video_device,
144 0,
145 output_desc,
146 1,
147 input_desc,
148 &IID_ID3D12VideoProcessor,
149 (void **)&vpp->video_processor
150 );
151
152 if (FAILED(hr)) {
153 av_log(ctx, AV_LOG_ERROR, "Failed to create video processor: HRESULT 0x%lX\n", hr);
155 return AVERROR_EXTERNAL;
156 }
157
158 vpp->configured = 1;
159
160 return 0;
161}
162
164{
165 int ret = ff_d3d12_cmd_begin(ctx, &vpp->cmd);
166
168
169 return ret;
170}
171
173 AVFrame *const *inputs, int nb_inputs, AVFrame *output,
174 int *frames_in_use)
175{
176 int ret = ff_d3d12_cmd_submit(ctx, &vpp->cmd, inputs, nb_inputs, output,
177 frames_in_use);
178
180
181 return ret;
182}
static int probe(const AVProbeData *p)
Definition act.c:39
static const AVFilterPad inputs[]
Definition af_aap.c:299
static AVFormatContext * ctx
#define NULL
Definition coverity.c:32
int ff_d3d12_cmd_uninit(AVFilterContext *ctx, D3D12CommandContext *cmd)
Wait for the GPU to finish all submitted work, then release all D3D12 objects and clear cmd->ready.
int ff_d3d12_filter_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)
Map AVFrame color properties to a DXGI color space.
int ff_d3d12_cmd_submit(AVFilterContext *ctx, D3D12CommandContext *cmd, AVFrame *const *inputs, int nb_inputs, AVFrame *output, int *frames_in_use)
Close and execute the command list.
int ff_d3d12_cmd_begin(AVFilterContext *ctx, D3D12CommandContext *cmd)
Get a command allocator the GPU is done with, and reset it together with the command list for recordi...
const D3D12FilterFormat * ff_d3d12_filter_format_iterate(void **opaque)
Iterate over all supported formats.
int ff_d3d12_cmd_init(AVFilterContext *ctx, D3D12CommandContext *cmd, ID3D12Device *device, D3D12_COMMAND_LIST_TYPE type)
Create the command queue, the fence and a closed command list of the given type, after releasing any ...
static void d3d12_vpp_release(D3D12VPPContext *vpp)
Definition d3d12_vpp.c:30
int ff_d3d12_vpp_init(AVFilterContext *ctx, D3D12VPPContext *vpp, ID3D12Device *device)
Create the video device and the video process command context on device, after releasing any previous...
Definition d3d12_vpp.c:70
int ff_d3d12_vpp_uninit(AVFilterContext *ctx, D3D12VPPContext *vpp)
Wait for the GPU to finish all submitted work, then release all D3D12 objects and clear vpp->configur...
Definition d3d12_vpp.c:55
int ff_d3d12_vpp_begin(AVFilterContext *ctx, D3D12VPPContext *vpp)
Start recording into vpp->cmd.list.video_process, see ff_d3d12_cmd_begin().
Definition d3d12_vpp.c:163
int ff_d3d12_vpp_submit(AVFilterContext *ctx, D3D12VPPContext *vpp, AVFrame *const *inputs, int nb_inputs, AVFrame *output, int *frames_in_use)
Execute the recorded commands, see ff_d3d12_cmd_submit().
Definition d3d12_vpp.c:172
int ff_d3d12_vpp_create_processor(AVFilterContext *ctx, D3D12VPPContext *vpp, const D3D12_VIDEO_PROCESS_OUTPUT_STREAM_DESC *output_desc, const D3D12_VIDEO_PROCESS_INPUT_STREAM_DESC *input_desc)
Create the video processor and mark vpp as configured.
Definition d3d12_vpp.c:136
static void d3d12_vpp_check_cmd(D3D12VPPContext *vpp)
Definition d3d12_vpp.c:45
void ff_d3d12_vpp_report_output_support(AVFilterContext *ctx, D3D12VPPContext *vpp, const D3D12_FEATURE_DATA_VIDEO_PROCESS_SUPPORT *support)
Log, at debug level, which output formats the video processor supports for the input described in sup...
Definition d3d12_vpp.c:97
int ret
Definition ffplay.c:391
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_log_get_level(void)
Get the current log level.
Definition log.c:477
const VDPAUPixFmtMap * map
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.
Definition pixdesc.c:3404
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:812
AVColorRange
Visual content value range.
Definition pixfmt.h:757
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:647
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:650
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:677
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:680
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:714
An instance of a filter.
Definition avfilter.h:279
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
ID3D12CommandQueue * queue
Video processor state shared by the D3D12 filters.
Definition d3d12_vpp.h:36
D3D12CommandContext cmd
Definition d3d12_vpp.h:37
ID3D12VideoDevice * video_device
Definition d3d12_vpp.h:39
ID3D12VideoProcessor * video_processor
Definition d3d12_vpp.h:40
#define av_log(a,...)