FFmpeg
d3d12va_encode.h
Go to the documentation of this file.
1 /*
2  * Direct3D 12 HW acceleration video encoder
3  *
4  * Copyright (c) 2024 Intel Corporation
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 AVCODEC_D3D12VA_ENCODE_H
24 #define AVCODEC_D3D12VA_ENCODE_H
25 
26 #include "libavutil/fifo.h"
27 #include "libavutil/hwcontext.h"
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "hwconfig.h"
33 #include "hw_base_encode.h"
34 
35 struct D3D12VAEncodeType;
36 
38 
39 #define MAX_PARAM_BUFFER_SIZE 4096
40 #define D3D12VA_VIDEO_ENC_ASYNC_DEPTH 8
41 
42 typedef struct D3D12VAEncodePicture {
45 
48 
50  ID3D12Resource *output_buffer;
51 
52  ID3D12Resource *encoded_metadata;
53  ID3D12Resource *resolved_metadata;
54 
55  D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA pic_ctl;
56 
59 
60 typedef struct D3D12VAEncodeProfile {
61  /**
62  * lavc profile value (AV_PROFILE_*).
63  */
65 
66  /**
67  * Supported bit depth.
68  */
69  int depth;
70 
71  /**
72  * Number of components.
73  */
75 
76  /**
77  * Chroma subsampling in width dimension.
78  */
80 
81  /**
82  * Chroma subsampling in height dimension.
83  */
85 
86  /**
87  * D3D12 profile value.
88  */
89  D3D12_VIDEO_ENCODER_PROFILE_DESC d3d12_profile;
91 
92 enum {
99 };
100 
101 
102 typedef struct D3D12VAEncodeRCMode {
103  /**
104  * Mode from above enum (RC_MODE_*).
105  */
106  int mode;
107 
108  /**
109  * Name.
110  *
111  */
112  const char *name;
113 
114  /**
115  * Uses bitrate parameters.
116  *
117  */
118  int bitrate;
119 
120  /**
121  * Supports maxrate distinct from bitrate.
122  *
123  */
124  int maxrate;
125 
126  /**
127  * Uses quality value.
128  *
129  */
130  int quality;
131 
132  /**
133  * Supports HRD/VBV parameters.
134  *
135  */
136  int hrd;
137 
138  /**
139  * D3D12 mode value.
140  */
141  D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_mode;
143 
144 typedef struct D3D12VAEncodeContext {
146 
147  /**
148  * Codec-specific hooks.
149  */
150  const struct D3D12VAEncodeType *codec;
151 
152  /**
153  * Explicitly set RC mode (otherwise attempt to pick from
154  * available modes).
155  */
157 
158  /**
159  * Explicitly-set QP, for use with the "qp" options.
160  * (Forces CQP mode when set, overriding everything else.)
161  */
163 
164  /**
165  * RC quality level - meaning depends on codec and RC mode.
166  * In CQP mode this sets the fixed quantiser value.
167  */
169 
170  /**
171  * Chosen encoding profile details.
172  */
174 
176 
177  /**
178  * ID3D12Device3 interface.
179  */
180  ID3D12Device3 *device3;
181 
182  /**
183  * ID3D12VideoDevice3 interface.
184  */
185  ID3D12VideoDevice3 *video_device3;
186 
187  /**
188  * Pool of (reusable) bitstream output buffers.
189  */
191 
192  /**
193  * D3D12 video encoder.
194  */
196 
197  ID3D12VideoEncoder *encoder;
198 
199  /**
200  * D3D12 video encoder heap.
201  */
202  ID3D12VideoEncoderHeap *encoder_heap;
203 
204  /**
205  * A cached queue for reusing the D3D12 command allocators.
206  *
207  * @see https://learn.microsoft.com/en-us/windows/win32/direct3d12/recording-command-lists-and-bundles#id3d12commandallocator
208  */
210 
211  /**
212  * D3D12 command queue.
213  */
214  ID3D12CommandQueue *command_queue;
215 
216  /**
217  * D3D12 video encode command list.
218  */
219  ID3D12VideoEncodeCommandList2 *command_list;
220 
221  /**
222  * The sync context used to sync command queue.
223  */
225 
226  /**
227  * The bi_not_empty feature.
228  */
230 
231  /**
232  * D3D12_FEATURE structures.
233  */
234  D3D12_FEATURE_DATA_VIDEO_ENCODER_RESOURCE_REQUIREMENTS req;
235 
236  D3D12_FEATURE_DATA_VIDEO_ENCODER_RESOLUTION_SUPPORT_LIMITS res_limits;
237 
238  /**
239  * D3D12_VIDEO_ENCODER structures.
240  */
241  D3D12_VIDEO_ENCODER_PICTURE_RESOLUTION_DESC resolution;
242 
243  D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION codec_conf;
244 
245  D3D12_VIDEO_ENCODER_RATE_CONTROL rc;
246 
247  D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE gop;
248 
249  D3D12_VIDEO_ENCODER_LEVEL_SETTING level;
251 
252 typedef struct D3D12VAEncodeType {
253  /**
254  * List of supported profiles.
255  */
257 
258  /**
259  * D3D12 codec name.
260  */
261  D3D12_VIDEO_ENCODER_CODEC d3d12_codec;
262 
263  /**
264  * Codec feature flags.
265  */
266  int flags;
267 
268  /**
269  * Default quality for this codec - used as quantiser or RC quality
270  * factor depending on RC mode.
271  */
273 
274  /**
275  * Query codec configuration and determine encode parameters like
276  * block sizes for surface alignment and slices. If not set, assume
277  * that all blocks are 16x16 and that surfaces should be aligned to match
278  * this.
279  */
281 
282  /**
283  * Perform any extra codec-specific configuration.
284  */
285  int (*configure)(AVCodecContext *avctx);
286 
287  /**
288  * Set codec-specific level setting.
289  */
290  int (*set_level)(AVCodecContext *avctx);
291 
292  /**
293  * The size of any private data structure associated with each
294  * picture (can be zero if not required).
295  */
297 
298  /**
299  * Fill the corresponding parameters.
300  */
302 
304  FFHWBaseEncodePicture *base_pic);
305 
307 
308  /**
309  * Write the packed header data to the provided buffer.
310  */
312  char *data, size_t *data_len);
314 
316 
319 
320 #define D3D12VA_ENCODE_RC_MODE(name, desc) \
321  { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
322  0, 0, FLAGS, .unit = "rc_mode" }
323 #define D3D12VA_ENCODE_RC_OPTIONS \
324  { "rc_mode",\
325  "Set rate control mode", \
326  OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
327  { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, .unit = "rc_mode" }, \
328  { "auto", "Choose mode automatically based on other parameters", \
329  0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, .unit = "rc_mode" }, \
330  D3D12VA_ENCODE_RC_MODE(CQP, "Constant-quality"), \
331  D3D12VA_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \
332  D3D12VA_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \
333  D3D12VA_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate")
334 
335 #endif /* AVCODEC_D3D12VA_ENCODE_H */
hwconfig.h
D3D12VAEncodeRCMode::mode
int mode
Mode from above enum (RC_MODE_*).
Definition: d3d12va_encode.h:106
RC_MODE_AUTO
@ RC_MODE_AUTO
Definition: d3d12va_encode.h:93
D3D12VAEncodeType::init_sequence_params
int(* init_sequence_params)(AVCodecContext *avctx)
Fill the corresponding parameters.
Definition: d3d12va_encode.h:301
D3D12VAEncodeProfile::d3d12_profile
D3D12_VIDEO_ENCODER_PROFILE_DESC d3d12_profile
D3D12 profile value.
Definition: d3d12va_encode.h:89
RC_MODE_MAX
@ RC_MODE_MAX
Definition: d3d12va_encode.h:98
RC_MODE_CBR
@ RC_MODE_CBR
Definition: d3d12va_encode.h:95
D3D12VAEncodeType::d3d12_codec
D3D12_VIDEO_ENCODER_CODEC d3d12_codec
D3D12 codec name.
Definition: d3d12va_encode.h:261
AVBufferPool
The buffer pool.
Definition: buffer_internal.h:88
RC_MODE_CQP
@ RC_MODE_CQP
Definition: d3d12va_encode.h:94
D3D12VAEncodeType::init_picture_params
int(* init_picture_params)(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic)
Definition: d3d12va_encode.h:303
D3D12VAEncodeContext::encoder_heap
ID3D12VideoEncoderHeap * encoder_heap
D3D12 video encoder heap.
Definition: d3d12va_encode.h:202
D3D12VAEncodePicture::input_surface
AVD3D12VAFrame * input_surface
Definition: d3d12va_encode.h:46
D3D12VAEncodeContext::req
D3D12_FEATURE_DATA_VIDEO_ENCODER_RESOURCE_REQUIREMENTS req
D3D12_FEATURE structures.
Definition: d3d12va_encode.h:234
internal.h
D3D12VAEncodeContext::explicit_rc_mode
int explicit_rc_mode
Explicitly set RC mode (otherwise attempt to pick from available modes).
Definition: d3d12va_encode.h:156
data
const char data[16]
Definition: mxf.c:148
D3D12VAEncodeRCMode::d3d12_mode
D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE d3d12_mode
D3D12 mode value.
Definition: d3d12va_encode.h:141
D3D12VAEncodeContext::encoder_ref
AVBufferRef * encoder_ref
D3D12 video encoder.
Definition: d3d12va_encode.h:195
D3D12VAEncodeRCMode::maxrate
int maxrate
Supports maxrate distinct from bitrate.
Definition: d3d12va_encode.h:124
D3D12VAEncodeContext::encoder
ID3D12VideoEncoder * encoder
Definition: d3d12va_encode.h:197
D3D12VAEncodeType::configure
int(* configure)(AVCodecContext *avctx)
Perform any extra codec-specific configuration.
Definition: d3d12va_encode.h:285
D3D12VAEncodeContext::sync_ctx
AVD3D12VASyncContext sync_ctx
The sync context used to sync command queue.
Definition: d3d12va_encode.h:224
D3D12VAEncodeContext::resolution
D3D12_VIDEO_ENCODER_PICTURE_RESOLUTION_DESC resolution
D3D12_VIDEO_ENCODER structures.
Definition: d3d12va_encode.h:241
D3D12VAEncodeRCMode::quality
int quality
Uses quality value.
Definition: d3d12va_encode.h:130
D3D12VAEncodePicture::resolved_metadata
ID3D12Resource * resolved_metadata
Definition: d3d12va_encode.h:53
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
fifo.h
D3D12VAEncodePicture::output_buffer_ref
AVBufferRef * output_buffer_ref
Definition: d3d12va_encode.h:49
D3D12VAEncodeContext::rc_quality
int rc_quality
RC quality level - meaning depends on codec and RC mode.
Definition: d3d12va_encode.h:168
D3D12VAEncodePicture::output_buffer
ID3D12Resource * output_buffer
Definition: d3d12va_encode.h:50
D3D12VAEncodeContext::hwctx
AVD3D12VADeviceContext * hwctx
Definition: d3d12va_encode.h:175
D3D12VAEncodePicture::recon_surface
AVD3D12VAFrame * recon_surface
Definition: d3d12va_encode.h:47
ff_d3d12va_encode_close
int ff_d3d12va_encode_close(AVCodecContext *avctx)
Definition: d3d12va_encode.c:1511
D3D12VAEncodeContext::bi_not_empty
int bi_not_empty
The bi_not_empty feature.
Definition: d3d12va_encode.h:229
D3D12VAEncodeType::default_quality
int default_quality
Default quality for this codec - used as quantiser or RC quality factor depending on RC mode.
Definition: d3d12va_encode.h:272
D3D12VAEncodeRCMode::bitrate
int bitrate
Uses bitrate parameters.
Definition: d3d12va_encode.h:118
D3D12VAEncodeType::free_picture_params
void(* free_picture_params)(D3D12VAEncodePicture *pic)
Definition: d3d12va_encode.h:306
D3D12VAEncodeProfile::log2_chroma_w
int log2_chroma_w
Chroma subsampling in width dimension.
Definition: d3d12va_encode.h:79
pkt
AVPacket * pkt
Definition: movenc.c:60
ff_d3d12va_encode_init
int ff_d3d12va_encode_init(AVCodecContext *avctx)
Definition: d3d12va_encode.c:1392
D3D12VAEncodePicture::header_size
int header_size
Definition: d3d12va_encode.h:43
D3D12VAEncodeType::picture_priv_data_size
size_t picture_priv_data_size
The size of any private data structure associated with each picture (can be zero if not required).
Definition: d3d12va_encode.h:296
D3D12VAEncodeProfile::nb_components
int nb_components
Number of components.
Definition: d3d12va_encode.h:74
hw_base_encode.h
D3D12VAEncodePicture::pic_ctl
D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA pic_ctl
Definition: d3d12va_encode.h:55
AVD3D12VASyncContext
This struct is used to sync d3d12 execution.
Definition: hwcontext_d3d12va.h:84
D3D12VAEncodeRCMode
Definition: d3d12va_encode.h:102
D3D12VAEncodeContext::device3
ID3D12Device3 * device3
ID3D12Device3 interface.
Definition: d3d12va_encode.h:180
D3D12VAEncodePicture::fence_value
int fence_value
Definition: d3d12va_encode.h:57
D3D12VAEncodeContext::base
FFHWBaseEncodeContext base
Definition: d3d12va_encode.h:145
D3D12VAEncodeContext::profile
const D3D12VAEncodeProfile * profile
Chosen encoding profile details.
Definition: d3d12va_encode.h:173
RC_MODE_VBR
@ RC_MODE_VBR
Definition: d3d12va_encode.h:96
hwcontext_d3d12va.h
D3D12VAEncodeType::flags
int flags
Codec feature flags.
Definition: d3d12va_encode.h:266
RC_MODE_QVBR
@ RC_MODE_QVBR
Definition: d3d12va_encode.h:97
D3D12VAEncodeContext::rc
D3D12_VIDEO_ENCODER_RATE_CONTROL rc
Definition: d3d12va_encode.h:245
D3D12VAEncodeContext::allocator_queue
AVFifo * allocator_queue
A cached queue for reusing the D3D12 command allocators.
Definition: d3d12va_encode.h:209
D3D12VAEncodeContext::codec_conf
D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION codec_conf
Definition: d3d12va_encode.h:243
AVFifo
Definition: fifo.c:35
D3D12VAEncodeContext::res_limits
D3D12_FEATURE_DATA_VIDEO_ENCODER_RESOLUTION_SUPPORT_LIMITS res_limits
Definition: d3d12va_encode.h:236
D3D12VAEncodeProfile
Definition: d3d12va_encode.h:60
D3D12VAEncodeContext::codec
const struct D3D12VAEncodeType * codec
Codec-specific hooks.
Definition: d3d12va_encode.h:150
AVCodecHWConfigInternal
Definition: hwconfig.h:25
ff_d3d12va_encode_hw_configs
const AVCodecHWConfigInternal *const ff_d3d12va_encode_hw_configs[]
Definition: d3d12va_encode.c:36
AVD3D12VAFrame
D3D12VA frame descriptor for pool allocation.
Definition: hwcontext_d3d12va.h:106
D3D12VAEncodePicture::encoded_metadata
ID3D12Resource * encoded_metadata
Definition: d3d12va_encode.h:52
D3D12VAEncodePicture
Definition: d3d12va_encode.h:42
D3D12VAEncodeContext::level
D3D12_VIDEO_ENCODER_LEVEL_SETTING level
Definition: d3d12va_encode.h:249
ff_d3d12va_encode_receive_packet
int ff_d3d12va_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: d3d12va_encode.c:1387
AVD3D12VADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_d3d12va.h:43
D3D12VAEncodeProfile::log2_chroma_h
int log2_chroma_h
Chroma subsampling in height dimension.
Definition: d3d12va_encode.h:84
D3D12VAEncodeRCMode::hrd
int hrd
Supports HRD/VBV parameters.
Definition: d3d12va_encode.h:136
D3D12VAEncodeType::write_sequence_header
int(* write_sequence_header)(AVCodecContext *avctx, char *data, size_t *data_len)
Write the packed header data to the provided buffer.
Definition: d3d12va_encode.h:311
avcodec.h
D3D12VAEncodeProfile::av_profile
int av_profile
lavc profile value (AV_PROFILE_*).
Definition: d3d12va_encode.h:64
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
D3D12VAEncodeContext
Definition: d3d12va_encode.h:144
D3D12VAEncodeContext::command_list
ID3D12VideoEncodeCommandList2 * command_list
D3D12 video encode command list.
Definition: d3d12va_encode.h:219
D3D12VAEncodeType::get_encoder_caps
int(* get_encoder_caps)(AVCodecContext *avctx)
Query codec configuration and determine encode parameters like block sizes for surface alignment and ...
Definition: d3d12va_encode.h:280
D3D12VAEncodeContext::command_queue
ID3D12CommandQueue * command_queue
D3D12 command queue.
Definition: d3d12va_encode.h:214
D3D12VAEncodeType::set_level
int(* set_level)(AVCodecContext *avctx)
Set codec-specific level setting.
Definition: d3d12va_encode.h:290
AVCodecContext
main external API structure.
Definition: avcodec.h:451
D3D12VAEncodeRCMode::name
const char * name
Name.
Definition: d3d12va_encode.h:112
D3D12VAEncodeContext::video_device3
ID3D12VideoDevice3 * video_device3
ID3D12VideoDevice3 interface.
Definition: d3d12va_encode.h:185
D3D12VAEncodeContext::output_buffer_pool
AVBufferPool * output_buffer_pool
Pool of (reusable) bitstream output buffers.
Definition: d3d12va_encode.h:190
D3D12VAEncodePicture::aligned_header_size
int aligned_header_size
Definition: d3d12va_encode.h:44
D3D12VAEncodeProfile::depth
int depth
Supported bit depth.
Definition: d3d12va_encode.h:69
D3D12VAEncodeContext::gop
D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE gop
Definition: d3d12va_encode.h:247
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPacket
This structure stores compressed data.
Definition: packet.h:516
D3D12VAEncodeType::profiles
const D3D12VAEncodeProfile * profiles
List of supported profiles.
Definition: d3d12va_encode.h:256
D3D12VAEncodeContext::explicit_qp
int explicit_qp
Explicitly-set QP, for use with the "qp" options.
Definition: d3d12va_encode.h:162
hwcontext.h
D3D12VAEncodeType
Definition: d3d12va_encode.h:252
hwcontext_d3d12va_internal.h