FFmpeg
vulkan_encode.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVCODEC_VULKAN_ENCODE_H
20 #define AVCODEC_VULKAN_ENCODE_H
21 
22 #include "codec_id.h"
23 #include "internal.h"
24 
25 #include "encode.h"
26 #include "hwconfig.h"
27 
28 #include "vulkan_video.h"
29 #include "hw_base_encode.h"
30 
31 typedef struct FFVulkanEncodeDescriptor {
34  VkVideoCodecOperationFlagBitsKHR encode_op;
35 
36  VkExtensionProperties ext_props;
38 
39 typedef struct FFVulkanEncodePicture {
41  VkVideoPictureResourceInfoKHR dpb_res;
42  VkVideoReferenceSlotInfoKHR dpb_slot;
43 
44  struct {
45  VkImageView view;
46  VkImageAspectFlags aspect;
47  } in;
48 
49  struct {
50  VkImageView view;
51  VkImageAspectFlags aspect;
52  } dpb;
53 
54  void *codec_layer;
56 
61 
62 /**
63  * Callback for writing stream-level headers.
64  */
66  uint8_t *data, size_t *data_len);
67 
68 /**
69  * Callback for initializing codec-specific picture headers.
70  */
73 
74 /**
75  * Callback for writing alignment data.
76  * Align is the value to align offset to.
77  */
78 typedef int (*vkenc_cb_write_filler)(AVCodecContext *avctx, uint32_t filler,
79  uint8_t *data, size_t *data_len);
80 
81 /**
82  * Callback for writing any extra units requested. data_len must be set
83  * to the available size, and its value will be overwritten by the #bytes written
84  * to the output buffer.
85  */
88  uint8_t *data, size_t *data_len);
89 
90 typedef struct FFVulkanCodec {
91  /**
92  * Codec feature flags.
93  */
94  int flags;
95 /* Codec output packet without timestamp delay, which means the
96  * output packet has same PTS and DTS. For AV1. */
97 #define VK_ENC_FLAG_NO_DELAY 1 << 6
98 
99  /**
100  * Size of the codec-specific picture struct.
101  */
103 
104  /**
105  * Size of the filler header.
106  */
108 
109  /**
110  * Initialize codec-specific structs in a Vulkan profile.
111  */
112  int (*init_profile)(AVCodecContext *avctx, VkVideoProfileInfoKHR *profile,
113  void *pnext);
114 
115  /**
116  * Initialize codec-specific rate control structures for a picture.
117  */
119  VkVideoEncodeRateControlInfoKHR *rc_info,
120  VkVideoEncodeRateControlLayerInfoKHR *rc_layer);
121 
122  /**
123  * Initialize codec-specific picture parameters.
124  */
126  VkVideoEncodeInfoKHR *encode_info);
127 
128  /**
129  * Callback for writing stream headers.
130  */
132  FFHWBaseEncodePicture *base_pic,
133  uint8_t *data, size_t *data_len);
134 
135  /**
136  * Callback for writing alignment data.
137  */
138  int (*write_filler)(AVCodecContext *avctx, uint32_t filler,
139  uint8_t *data, size_t *data_len);
140 
141  /**
142  * Callback for writing any extra units requested. data_len must be set
143  * to the available size, and its value will be overwritten by the #bytes written
144  * to the output buffer.
145  */
147  uint8_t *data, size_t *data_len);
148 } FFVulkanCodec;
149 
150 typedef struct FFVkEncodeCommonOptions {
151  int qp;
152  int quality;
153  int profile;
154  int level;
155  int tier;
157  VkVideoEncodeUsageFlagBitsKHR usage;
158  VkVideoEncodeContentFlagBitsKHR content;
159  VkVideoEncodeTuningModeKHR tune;
160 
161  VkVideoEncodeRateControlModeFlagBitsKHR rc_mode;
162 #define FF_VK_RC_MODE_AUTO 0xFFFFFFFF
164 
165 typedef struct FFVulkanEncodeContext {
167  FFVkVideoCommon common;
170 
173 
174  /* Session parameters object, initialized by each codec independently
175  * and set here. */
176  VkVideoSessionParametersKHR session_params;
177 
179 
181 
183 
184  VkVideoProfileInfoKHR profile;
185  VkVideoProfileListInfoKHR profile_list;
186  VkVideoCapabilitiesKHR caps;
187  VkVideoEncodeQualityLevelPropertiesKHR quality_props;
188  VkVideoEncodeCapabilitiesKHR enc_caps;
189  VkVideoEncodeUsageInfoKHR usage_info;
190 
193 
196 
197 #define VULKAN_ENCODE_COMMON_OPTIONS \
198  { "qp", "Use an explicit constant quantizer for the whole stream", OFFSET(common.opts.qp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, FLAGS }, \
199  { "quality", "Set encode quality (trades off against speed, higher is faster)", OFFSET(common.opts.quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
200  { "rc_mode", "Select rate control type", OFFSET(common.opts.rc_mode), AV_OPT_TYPE_INT, { .i64 = FF_VK_RC_MODE_AUTO }, 0, FF_VK_RC_MODE_AUTO, FLAGS, "rc_mode" }, \
201  { "auto", "Choose mode automatically based on parameters", 0, AV_OPT_TYPE_CONST, { .i64 = FF_VK_RC_MODE_AUTO }, INT_MIN, INT_MAX, FLAGS, "rc_mode" }, \
202  { "driver", "Driver-specific rate control", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "rc_mode" }, \
203  { "cqp", "Constant quantizer mode", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_RATE_CONTROL_MODE_DISABLED_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "rc_mode" }, \
204  { "cbr", "Constant bitrate mode", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_RATE_CONTROL_MODE_CBR_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "rc_mode" }, \
205  { "vbr", "Variable bitrate mode", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_RATE_CONTROL_MODE_VBR_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "rc_mode" }, \
206  { "tune", "Select tuning type", OFFSET(common.opts.tune), AV_OPT_TYPE_INT, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_DEFAULT_KHR }, 0, INT_MAX, FLAGS, "tune" }, \
207  { "default", "Default tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
208  { "hq", "High quality tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_HIGH_QUALITY_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
209  { "ll", "Low-latency tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_LOW_LATENCY_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
210  { "ull", "Ultra low-latency tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_ULTRA_LOW_LATENCY_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
211  { "lossless", "Lossless mode tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_LOSSLESS_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
212  { "usage", "Select usage type", OFFSET(common.opts.usage), AV_OPT_TYPE_FLAGS, { .i64 = VK_VIDEO_ENCODE_USAGE_DEFAULT_KHR }, 0, INT_MAX, FLAGS, "usage" }, \
213  { "default", "Default optimizations", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
214  { "transcode", "Optimize for transcoding", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_TRANSCODING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
215  { "stream", "Optimize for streaming", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_STREAMING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
216  { "record", "Optimize for offline recording", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_RECORDING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
217  { "conference", "Optimize for teleconferencing", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_CONFERENCING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
218  { "content", "Select content type", OFFSET(common.opts.content), AV_OPT_TYPE_FLAGS, { .i64 = VK_VIDEO_ENCODE_CONTENT_DEFAULT_KHR }, 0, INT_MAX, FLAGS, "content" }, \
219  { "default", "Default content", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }, \
220  { "camera", "Camera footage", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_CAMERA_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }, \
221  { "desktop", "Screen recording", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_DESKTOP_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }, \
222  { "rendered", "Game or 3D content", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_RENDERED_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }
223 
224 /**
225  * Initialize encoder.
226  */
228  const FFVulkanEncodeDescriptor *vk_desc,
229  const FFVulkanCodec *codec,
230  void *codec_caps, void *quality_pnext);
231 
232 /**
233  * Write out the extradata in case its needed.
234  */
237 
238 /**
239  * Encode.
240  */
242 
243 /**
244  * Uninitialize encoder.
245  */
247 
248 /**
249  * Create session parameters.
250  */
252  void *codec_params_pnext);
253 
254 /**
255  * Paperwork.
256  */
258 
259 #endif /* AVCODEC_VULKAN_ENCODE_H */
hwconfig.h
FFVulkanEncodeDescriptor::encode_op
VkVideoCodecOperationFlagBitsKHR encode_op
Definition: vulkan_encode.h:34
FFVulkanEncodeDescriptor::encode_extension
FFVulkanExtensions encode_extension
Definition: vulkan_encode.h:33
FFVulkanEncodePicture::view
VkImageView view
Definition: vulkan_encode.h:45
FFVulkanEncodeContext::pic_format
VkFormat pic_format
Definition: vulkan_encode.h:180
FFVulkanExtensions
uint64_t FFVulkanExtensions
Definition: vulkan_functions.h:29
AVBufferPool
The buffer pool.
Definition: buffer_internal.h:88
FFVulkanEncodePicture::codec_layer
void * codec_layer
Definition: vulkan_encode.h:54
ff_vulkan_encode_hw_configs
const AVCodecHWConfigInternal *const ff_vulkan_encode_hw_configs[]
Paperwork.
Definition: vulkan_encode.c:26
FFVulkanEncodeContext::profile
VkVideoProfileInfoKHR profile
Definition: vulkan_encode.h:184
internal.h
encode.h
data
const char data[16]
Definition: mxf.c:149
FFVulkanEncodeContext::explicit_qp
int explicit_qp
Definition: vulkan_encode.h:171
FFVulkanEncodeContext::usage_info
VkVideoEncodeUsageInfoKHR usage_info
Definition: vulkan_encode.h:189
FFVulkanEncodePicture::codec_rc_layer
void * codec_rc_layer
Definition: vulkan_encode.h:55
FFVulkanCodec::filler_header_size
size_t filler_header_size
Size of the filler header.
Definition: vulkan_encode.h:107
FFVulkanEncodeDescriptor
Definition: vulkan_encode.h:31
FFVkEncodeCommonOptions
Definition: vulkan_encode.h:150
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
FFVulkanEncodeContext::caps
VkVideoCapabilitiesKHR caps
Definition: vulkan_encode.h:186
FFVkEncodeCommonOptions::tier
int tier
Definition: vulkan_encode.h:155
pkt
AVPacket * pkt
Definition: movenc.c:60
av_cold
#define av_cold
Definition: attributes.h:90
FFVulkanCodec::write_extra_headers
int(* write_extra_headers)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, uint8_t *data, size_t *data_len)
Callback for writing any extra units requested.
Definition: vulkan_encode.h:146
codec_id.h
ff_vulkan_encode_uninit
void ff_vulkan_encode_uninit(FFVulkanEncodeContext *ctx)
Uninitialize encoder.
Definition: vulkan_encode.c:31
FFVulkanEncodeContext::session_reset
int session_reset
Definition: vulkan_encode.h:172
FFVulkanEncodeDescriptor::ext_props
VkExtensionProperties ext_props
Definition: vulkan_encode.h:36
vkenc_cb_write_extra_headers
int(* vkenc_cb_write_extra_headers)(AVCodecContext *avctx, FFVulkanEncodePicture *pic, uint8_t *data, size_t *data_len)
Callback for writing any extra units requested.
Definition: vulkan_encode.h:86
FFVulkanCodec::write_sequence_headers
int(* write_sequence_headers)(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, uint8_t *data, size_t *data_len)
Callback for writing stream headers.
Definition: vulkan_encode.h:131
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FFVulkanEncodePicture::slices_offset
int slices_offset
Definition: vulkan_encode.h:59
hw_base_encode.h
FFVulkanEncodeContext::quality_props
VkVideoEncodeQualityLevelPropertiesKHR quality_props
Definition: vulkan_encode.h:187
FFVulkanEncodePicture::aspect
VkImageAspectFlags aspect
Definition: vulkan_encode.h:46
FFVulkanEncodeContext::profile_list
VkVideoProfileListInfoKHR profile_list
Definition: vulkan_encode.h:185
FFVkEncodeCommonOptions::async_depth
int async_depth
Definition: vulkan_encode.h:156
FFVkEncodeCommonOptions::qp
int qp
Definition: vulkan_encode.h:151
FFVulkanEncodeContext::qf_enc
FFVkQueueFamilyCtx qf_enc
Definition: vulkan_encode.h:191
FFVulkanEncodeContext::session_params
VkVideoSessionParametersKHR session_params
Definition: vulkan_encode.h:176
FFVulkanEncodePicture::dpb
struct FFVulkanEncodePicture::@283 dpb
FFVulkanCodec::init_pic_rc
int(* init_pic_rc)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VkVideoEncodeRateControlInfoKHR *rc_info, VkVideoEncodeRateControlLayerInfoKHR *rc_layer)
Initialize codec-specific rate control structures for a picture.
Definition: vulkan_encode.h:118
FFVkEncodeCommonOptions::level
int level
Definition: vulkan_encode.h:154
FFVulkanEncodePicture::in
struct FFVulkanEncodePicture::@282 in
vkenc_cb_write_filler
int(* vkenc_cb_write_filler)(AVCodecContext *avctx, uint32_t filler, uint8_t *data, size_t *data_len)
Callback for writing alignment data.
Definition: vulkan_encode.h:78
FFVulkanContext
Definition: vulkan.h:263
FFVulkanEncodeContext::base
FFHWBaseEncodeContext base
Definition: vulkan_encode.h:168
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
FFVulkanEncodeContext::opts
FFVkEncodeCommonOptions opts
Definition: vulkan_encode.h:182
FFVulkanEncodeContext::s
FFVulkanContext s
Definition: vulkan_encode.h:166
FFVulkanEncodeContext::enc_pool
FFVkExecPool enc_pool
Definition: vulkan_encode.h:192
FFVkEncodeCommonOptions::quality
int quality
Definition: vulkan_encode.h:152
ff_vulkan_write_global_header
av_cold int ff_vulkan_write_global_header(AVCodecContext *avctx, FFVulkanEncodeContext *ctx)
Write out the extradata in case its needed.
Definition: vulkan_encode.c:671
FFVulkanEncodeContext
Definition: vulkan_encode.h:165
FFVkEncodeCommonOptions::rc_mode
VkVideoEncodeRateControlModeFlagBitsKHR rc_mode
Definition: vulkan_encode.h:161
FFVkQueueFamilyCtx
Definition: vulkan.h:102
FFVulkanEncodeContext::codec
const FFVulkanCodec * codec
Definition: vulkan_encode.h:169
AVCodecHWConfigInternal
Definition: hwconfig.h:25
vkenc_cb_write_stream_headers
int(* vkenc_cb_write_stream_headers)(AVCodecContext *avctx, uint8_t *data, size_t *data_len)
Callback for writing stream-level headers.
Definition: vulkan_encode.h:65
FFVkExecContext
Definition: vulkan.h:107
FFVulkanCodec::init_pic_params
int(* init_pic_params)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VkVideoEncodeInfoKHR *encode_info)
Initialize codec-specific picture parameters.
Definition: vulkan_encode.h:125
FFVulkanEncodeContext::common
FFVkVideoCommon common
Definition: vulkan_encode.h:167
FFVkEncodeCommonOptions::profile
int profile
Definition: vulkan_encode.h:153
FFVkEncodeCommonOptions::tune
VkVideoEncodeTuningModeKHR tune
Definition: vulkan_encode.h:159
FFVkEncodeCommonOptions::usage
VkVideoEncodeUsageFlagBitsKHR usage
Definition: vulkan_encode.h:157
VkFormat
enum VkFormat VkFormat
Definition: hwcontext_stub.c:25
vkenc_cb_init_pic_headers
int(* vkenc_cb_init_pic_headers)(AVCodecContext *avctx, FFVulkanEncodePicture *pic)
Callback for initializing codec-specific picture headers.
Definition: vulkan_encode.h:71
FFVulkanEncodePicture::exec
FFVkExecContext * exec
Definition: vulkan_encode.h:57
profile
int profile
Definition: mxfenc.c:2233
FFVulkanEncodeDescriptor::codec_id
enum AVCodecID codec_id
Definition: vulkan_encode.h:32
ff_vulkan_encode_receive_packet
int ff_vulkan_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Encode.
Definition: vulkan_encode.c:541
FFVulkanEncodeContext::enc_caps
VkVideoEncodeCapabilitiesKHR enc_caps
Definition: vulkan_encode.h:188
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
FFVkExecPool
Definition: vulkan.h:241
AVCodecContext
main external API structure.
Definition: avcodec.h:451
FFVulkanEncodePicture::base
FFHWBaseEncodePicture base
Definition: vulkan_encode.h:40
filler
int(* filler)(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:421
ff_vulkan_encode_create_session_params
int ff_vulkan_encode_create_session_params(AVCodecContext *avctx, FFVulkanEncodeContext *ctx, void *codec_params_pnext)
Create session parameters.
Definition: vulkan_encode.c:1026
FFVulkanEncodePicture::pkt_buf
AVBufferRef * pkt_buf
Definition: vulkan_encode.h:58
vulkan_video.h
FFVulkanCodec::init_profile
int(* init_profile)(AVCodecContext *avctx, VkVideoProfileInfoKHR *profile, void *pnext)
Initialize codec-specific structs in a Vulkan profile.
Definition: vulkan_encode.h:112
ff_vulkan_encode_init
av_cold int ff_vulkan_encode_init(AVCodecContext *avctx, FFVulkanEncodeContext *ctx, const FFVulkanEncodeDescriptor *vk_desc, const FFVulkanCodec *codec, void *codec_caps, void *quality_pnext)
Initialize encoder.
Definition: vulkan_encode.c:701
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
FFVulkanCodec::flags
int flags
Codec feature flags.
Definition: vulkan_encode.h:94
FFVulkanCodec::write_filler
int(* write_filler)(AVCodecContext *avctx, uint32_t filler, uint8_t *data, size_t *data_len)
Callback for writing alignment data.
Definition: vulkan_encode.h:138
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFVulkanCodec
Definition: vulkan_encode.h:90
FFVulkanCodec::picture_priv_data_size
size_t picture_priv_data_size
Size of the codec-specific picture struct.
Definition: vulkan_encode.h:102
FFVkEncodeCommonOptions::content
VkVideoEncodeContentFlagBitsKHR content
Definition: vulkan_encode.h:158
FFVulkanEncodeContext::buf_pool
AVBufferPool * buf_pool
Definition: vulkan_encode.h:178
FFVulkanEncodeContext::slots
FFHWBaseEncodePicture * slots[32]
Definition: vulkan_encode.h:194
FFVulkanEncodePicture::dpb_slot
VkVideoReferenceSlotInfoKHR dpb_slot
Definition: vulkan_encode.h:42
FFVulkanEncodePicture
Definition: vulkan_encode.h:39
FFVulkanEncodePicture::dpb_res
VkVideoPictureResourceInfoKHR dpb_res
Definition: vulkan_encode.h:41