FFmpeg
Loading...
Searching...
No Matches
d3d12_filter.h
Go to the documentation of this file.
1/*
2 * D3D12VA filter 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#ifndef AVFILTER_D3D12_FILTER_H
24#define AVFILTER_D3D12_FILTER_H
25
26#include "config.h"
27
28#include "libavutil/buffer.h"
29#include "libavutil/fifo.h"
30#include "libavutil/frame.h"
32#include "libavutil/pixfmt.h"
33
34#include "avfilter.h"
35
36/**
37 * Maximum number of input frames (including reference frames) that one
38 * submission can read.
39 */
40#define D3D12_FILTER_MAX_INPUTS 8
41
47
49
50const D3D12FilterFormat *ff_d3d12_filter_format_from_dxgi(DXGI_FORMAT dxgi_format);
51
52/**
53 * Iterate over all supported formats.
54 *
55 * @param opaque a pointer where state is stored, must point to NULL on the
56 * first call
57 * @return the next format, or NULL when the iteration is finished
58 */
60
61/**
62 * Map AVFrame color properties to a DXGI color space.
63 *
64 * For an input (is_output = 0), properties that no DXGI color space can
65 * represent are rejected. For an output request (is_output = 1), they are
66 * retagged to the nearest representable value where possible. spc and
67 * primaries may be adjusted; range and trc are only inspected. The caller
68 * must derive the final AVFrame color metadata, including any transfer
69 * characteristic reconciliation required by a conversion.
70 *
71 * @param input_601_hint primaries of a BT.601 input, used to keep the
72 * output's PAL/NTSC primaries consistent with it
73 */
74int ff_d3d12_filter_get_colorspace(AVFilterContext *ctx, int is_rgb, int is_output,
75 enum AVColorSpace *spc,
76 enum AVColorRange *range,
79 enum AVChromaLocation chroma_loc,
80 enum AVColorPrimaries input_601_hint,
81 DXGI_COLOR_SPACE_TYPE *out_space);
82
84
86 const AVFrame *in);
87
88/**
89 * Wait until fence reaches target, or until timeout_ms expires.
90 *
91 * @return 1 if the fence reached target, 0 otherwise
92 */
93int ff_d3d12_filter_wait_fence(ID3D12Fence *fence, UINT64 target,
94 HANDLE fence_event, DWORD timeout_ms);
95
96/**
97 * Append one transition barrier per plane of a D3D12 frame.
98 */
99void ff_d3d12_filter_add_frame_barriers(D3D12_RESOURCE_BARRIER *barriers, int *nb_barriers,
100 const AVD3D12VAFrame *frame, int nb_planes,
101 D3D12_RESOURCE_STATES state_before,
102 D3D12_RESOURCE_STATES state_after);
103
104/**
105 * Allocate and initialize a pool of D3D12 frames that can be used as
106 * render targets. ctx->extra_hw_frames is added to initial_pool_size.
107 */
109 enum AVPixelFormat sw_format, int width, int height,
110 int initial_pool_size, AVBufferRef **frames_ref);
111
112/**
113 * One command queue with its command list, a pool of command allocators
114 * and a private fence. Each submission signals the next private fence
115 * value; its command allocator and input references are recycled once the
116 * fence has passed that value.
117 */
118typedef struct D3D12CommandContext {
119 ID3D12Device *device;
120 D3D12_COMMAND_LIST_TYPE type;
121 ID3D12CommandQueue *queue;
122
123 /* The member matching type is valid. */
124 union {
125 ID3D12GraphicsCommandList *graphics;
126 ID3D12VideoProcessCommandList *video_process;
127#if HAVE_ID3D12VIDEOENCODECOMMANDLIST
128 ID3D12VideoEncodeCommandList *video_encode;
129#endif
131
132 /* Owned between ff_d3d12_cmd_begin() and ff_d3d12_cmd_submit(). */
133 ID3D12CommandAllocator *command_allocator;
135
136 ID3D12Fence *fence;
137 /* Value signaled by the next submission. */
140
141 /* Set by ff_d3d12_cmd_init(), cleared when the context is torn down,
142 * including when a command list error tears it down. */
143 int ready;
146
147/**
148 * Create the command queue, the fence and a closed command list of the
149 * given type, after releasing any previous configuration. type must be
150 * DIRECT, VIDEO_PROCESS or, when available in the headers, VIDEO_ENCODE.
151 * Releases everything on failure.
152 */
154 ID3D12Device *device, D3D12_COMMAND_LIST_TYPE type);
155
156/**
157 * Get a command allocator the GPU is done with, and reset it together with
158 * the command list for recording. If the command list cannot be reset, the
159 * context is torn down and cmd->ready is cleared.
160 */
162
163/**
164 * Make the queue of cmd wait, on the GPU, until the latest submission of
165 * other has completed. Must be called before the submission of cmd that
166 * depends on it. Does nothing if other has no pending work.
167 */
169 const D3D12CommandContext *other);
170
171/**
172 * Close and execute the command list. The queue first waits for the fences
173 * of all inputs and of the output, then signals the output frame's fence.
174 * output may be NULL. References to the inputs are kept until the GPU has
175 * finished with them. If the command list cannot be closed, the context is
176 * torn down and cmd->ready is cleared.
177 *
178 * @param[out] frames_in_use set to 1 on failure if the GPU may still access
179 * the frames, in which case the caller must not
180 * free them
181 */
183 AVFrame *const *inputs, int nb_inputs, AVFrame *output,
184 int *frames_in_use);
185
186/**
187 * Wait on the CPU until the latest submission has completed.
188 *
189 * @return 0 on success, AVERROR_EXTERNAL on timeout or failure
190 */
191int ff_d3d12_cmd_sync(AVFilterContext *ctx, D3D12CommandContext *cmd, DWORD timeout_ms);
192
193/**
194 * Wait for the GPU to finish all submitted work, then release all D3D12
195 * objects and clear cmd->ready.
196 *
197 * @return 0 on success, AVERROR_EXTERNAL if the GPU could not be confirmed
198 * idle, in which case nothing is released
199 */
201
202#endif /* AVFILTER_D3D12_FILTER_H */
static const AVFilterPad inputs[]
Definition af_aap.c:299
static AVFormatContext * ctx
Main libavfilter public API header.
refcounted data buffer API
const D3D12FilterFormat * ff_d3d12_filter_format_from_pix_fmt(enum AVPixelFormat pix_fmt)
const D3D12FilterFormat * ff_d3d12_filter_format_from_dxgi(DXGI_FORMAT dxgi_format)
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.
AVRational ff_d3d12_filter_get_input_framerate(AVFilterContext *ctx, AVFilterLink *inlink, const AVFrame *in)
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_sync(AVFilterContext *ctx, D3D12CommandContext *cmd, DWORD timeout_ms)
Wait on the CPU until the latest submission has completed.
int ff_d3d12_cmd_wait_context(AVFilterContext *ctx, D3D12CommandContext *cmd, const D3D12CommandContext *other)
Make the queue of cmd wait, on the GPU, until the latest submission of other has completed.
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...
int ff_d3d12_filter_wait_fence(ID3D12Fence *fence, UINT64 target, HANDLE fence_event, DWORD timeout_ms)
Wait until fence reaches target, or until timeout_ms expires.
int ff_d3d12_filter_init_output_frames(AVFilterContext *ctx, AVBufferRef *device_ref, enum AVPixelFormat sw_format, int width, int height, int initial_pool_size, AVBufferRef **frames_ref)
Allocate and initialize a pool of D3D12 frames that can be used as render targets.
const D3D12FilterFormat * ff_d3d12_filter_format_iterate(void **opaque)
Iterate over all supported formats.
enum AVChromaLocation ff_d3d12_filter_chroma_location(DXGI_COLOR_SPACE_TYPE space)
void ff_d3d12_filter_add_frame_barriers(D3D12_RESOURCE_BARRIER *barriers, int *nb_barriers, const AVD3D12VAFrame *frame, int nb_planes, D3D12_RESOURCE_STATES state_before, D3D12_RESOURCE_STATES state_after)
Append one transition barrier per plane of a D3D12 frame.
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 enum AVPixelFormat pix_fmt
static AVFrame * frame
static ListView * list
Definition ffplay.c:377
A generic FIFO API.
reference-counted frame API
An API-specific header for AV_HWDEVICE_TYPE_D3D12VA.
enum MPFType type
Definition jpegmpfenc.c:55
enum AVColorPrimaries primaries
enum AVColorSpace space
enum AVColorRange range
pixel format definitions
AVChromaLocation
Location of chroma samples.
Definition pixfmt.h:811
AVColorRange
Visual content value range.
Definition pixfmt.h:757
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:647
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:677
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
A reference to a data buffer.
Definition buffer.h:82
D3D12VA frame descriptor for pool allocation.
Definition fifo.c:35
An instance of a filter.
Definition avfilter.h:279
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
Rational number (pair of numerator and denominator).
Definition rational.h:58
One command queue with its command list, a pool of command allocators and a private fence.
ID3D12VideoProcessCommandList * video_process
ID3D12GraphicsCommandList * graphics
ID3D12Fence * fence
ID3D12Device * device
ID3D12CommandQueue * queue
ID3D12CommandAllocator * command_allocator
D3D12_COMMAND_LIST_TYPE type
enum AVPixelFormat pix_fmt
DXGI_FORMAT dxgi_format
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99