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 
60 
62  char tail_data[16];
63  size_t tail_size;
65 
66 /**
67  * Callback for writing stream-level headers.
68  */
70  uint8_t *data, size_t *data_len);
71 
72 /**
73  * Callback for initializing codec-specific picture headers.
74  */
77 
78 /**
79  * Callback for writing alignment data.
80  * Align is the value to align offset to.
81  */
82 typedef int (*vkenc_cb_write_filler)(AVCodecContext *avctx, uint32_t filler,
83  uint8_t *data, size_t *data_len);
84 
85 /**
86  * Callback for writing any extra units requested. data_len must be set
87  * to the available size, and its value will be overwritten by the #bytes written
88  * to the output buffer.
89  */
92  uint8_t *data, size_t *data_len);
93 
94 typedef struct FFVulkanCodec {
95  /**
96  * Codec feature flags.
97  */
98  int flags;
99 /* Codec output packet without timestamp delay, which means the
100  * output packet has same PTS and DTS. For AV1. */
101 #define VK_ENC_FLAG_NO_DELAY 1 << 6
102 
103  /**
104  * Size of the codec-specific picture struct.
105  */
107 
108  /**
109  * Size of the filler header.
110  */
112 
113  /**
114  * Initialize codec-specific structs in a Vulkan profile.
115  */
116  int (*init_profile)(AVCodecContext *avctx, VkVideoProfileInfoKHR *profile,
117  void *pnext);
118 
119  /**
120  * Initialize codec-specific rate control structures for a picture.
121  */
123  VkVideoEncodeRateControlInfoKHR *rc_info,
124  VkVideoEncodeRateControlLayerInfoKHR *rc_layer);
125 
126  /**
127  * Initialize codec-specific picture parameters.
128  */
130  VkVideoEncodeInfoKHR *encode_info);
131 
132  /**
133  * Callback for writing stream headers.
134  */
136  FFHWBaseEncodePicture *base_pic,
137  uint8_t *data, size_t *data_len);
138 
139  /**
140  * Callback for writing alignment data.
141  */
142  int (*write_filler)(AVCodecContext *avctx, uint32_t filler,
143  uint8_t *data, size_t *data_len);
144 
145  /**
146  * Callback for writing any extra units requested. data_len must be set
147  * to the available size, and its value will be overwritten by the #bytes written
148  * to the output buffer.
149  */
151  uint8_t *data, size_t *data_len);
152 } FFVulkanCodec;
153 
154 typedef struct FFVkEncodeCommonOptions {
155  int qp;
156  int quality;
157  int profile;
158  int level;
159  int tier;
161  VkVideoEncodeUsageFlagBitsKHR usage;
162  VkVideoEncodeContentFlagBitsKHR content;
163  VkVideoEncodeTuningModeKHR tune;
164 
165  VkVideoEncodeRateControlModeFlagBitsKHR rc_mode;
166 #define FF_VK_RC_MODE_AUTO 0xFFFFFFFF
168 
169 typedef struct FFVulkanEncodeContext {
171  FFVkVideoCommon common;
174 
177 
178  /* Session parameters object, initialized by each codec independently
179  * and set here. */
180  VkVideoSessionParametersKHR session_params;
181 
183 
185 
187 
188  VkVideoProfileInfoKHR profile;
189  VkVideoProfileListInfoKHR profile_list;
190  VkVideoCapabilitiesKHR caps;
191  VkVideoEncodeQualityLevelPropertiesKHR quality_props;
192  VkVideoEncodeCapabilitiesKHR enc_caps;
193  VkVideoEncodeUsageInfoKHR usage_info;
194 
197 
199 
203 
204 #define VULKAN_ENCODE_COMMON_OPTIONS \
205  { "qp", "Use an explicit constant quantizer for the whole stream", OFFSET(common.opts.qp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, FLAGS }, \
206  { "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 }, \
207  { "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" }, \
208  { "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" }, \
209  { "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" }, \
210  { "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" }, \
211  { "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" }, \
212  { "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" }, \
213  { "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" }, \
214  { "default", "Default tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
215  { "hq", "High quality tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_HIGH_QUALITY_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
216  { "ll", "Low-latency tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_LOW_LATENCY_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
217  { "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" }, \
218  { "lossless", "Lossless mode tuning", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_TUNING_MODE_LOSSLESS_KHR }, INT_MIN, INT_MAX, FLAGS, "tune" }, \
219  { "usage", "Select usage type", OFFSET(common.opts.usage), AV_OPT_TYPE_FLAGS, { .i64 = VK_VIDEO_ENCODE_USAGE_DEFAULT_KHR }, 0, INT_MAX, FLAGS, "usage" }, \
220  { "default", "Default optimizations", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
221  { "transcode", "Optimize for transcoding", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_TRANSCODING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
222  { "stream", "Optimize for streaming", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_STREAMING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
223  { "record", "Optimize for offline recording", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_RECORDING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
224  { "conference", "Optimize for teleconferencing", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_USAGE_CONFERENCING_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "usage" }, \
225  { "content", "Select content type", OFFSET(common.opts.content), AV_OPT_TYPE_FLAGS, { .i64 = VK_VIDEO_ENCODE_CONTENT_DEFAULT_KHR }, 0, INT_MAX, FLAGS, "content" }, \
226  { "default", "Default content", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_DEFAULT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }, \
227  { "camera", "Camera footage", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_CAMERA_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }, \
228  { "desktop", "Screen recording", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_DESKTOP_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }, \
229  { "rendered", "Game or 3D content", 0, AV_OPT_TYPE_CONST, { .i64 = VK_VIDEO_ENCODE_CONTENT_RENDERED_BIT_KHR }, INT_MIN, INT_MAX, FLAGS, "content" }
230 
231 /**
232  * Initialize encoder.
233  */
235  const FFVulkanEncodeDescriptor *vk_desc,
236  const FFVulkanCodec *codec,
237  void *codec_caps, void *quality_pnext);
238 
239 /**
240  * Write out the extradata in case its needed.
241  */
244 
245 /**
246  * Encode.
247  */
249 
250 /**
251  * Uninitialize encoder.
252  */
254 
255 /**
256  * Create session parameters.
257  */
259  void *codec_params_pnext);
260 
261 /**
262  * Paperwork.
263  */
265 
266 #endif /* AVCODEC_VULKAN_ENCODE_H */
hwconfig.h
FFVulkanEncodeContext::qf_enc
AVVulkanDeviceQueueFamily * qf_enc
Definition: vulkan_encode.h:195
FFVulkanEncodePicture::dpb
struct FFVulkanEncodePicture::@328 dpb
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:184
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:188
internal.h
FFVulkanEncodePicture::tail_data
char tail_data[16]
Definition: vulkan_encode.h:62
encode.h
data
const char data[16]
Definition: mxf.c:149
FFVulkanEncodeContext::explicit_qp
int explicit_qp
Definition: vulkan_encode.h:175
FFVulkanEncodeContext::usage_info
VkVideoEncodeUsageInfoKHR usage_info
Definition: vulkan_encode.h:193
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:111
FFVulkanEncodeDescriptor
Definition: vulkan_encode.h:31
FFVkEncodeCommonOptions
Definition: vulkan_encode.h:154
FFVulkanEncodeContext::prev_buf_ref
AVBufferRef * prev_buf_ref
Definition: vulkan_encode.h:200
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
FFVulkanEncodeContext::caps
VkVideoCapabilitiesKHR caps
Definition: vulkan_encode.h:190
FFVkEncodeCommonOptions::tier
int tier
Definition: vulkan_encode.h:159
pkt
AVPacket * pkt
Definition: movenc.c:60
av_cold
#define av_cold
Definition: attributes.h:106
FFVulkanEncodePicture::non_independent_frame
int non_independent_frame
Definition: vulkan_encode.h:61
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:150
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:176
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:90
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:135
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:191
FFVulkanEncodePicture::aspect
VkImageAspectFlags aspect
Definition: vulkan_encode.h:46
FFVulkanEncodeContext::profile_list
VkVideoProfileListInfoKHR profile_list
Definition: vulkan_encode.h:189
FFVkEncodeCommonOptions::async_depth
int async_depth
Definition: vulkan_encode.h:160
FFVkEncodeCommonOptions::qp
int qp
Definition: vulkan_encode.h:155
FFVulkanEncodeContext::session_params
VkVideoSessionParametersKHR session_params
Definition: vulkan_encode.h:180
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:122
FFVkEncodeCommonOptions::level
int level
Definition: vulkan_encode.h:158
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:82
FFVulkanContext
Definition: vulkan.h:274
FFVulkanEncodeContext::base
FFHWBaseEncodeContext base
Definition: vulkan_encode.h:172
FFVulkanEncodePicture::tail_size
size_t tail_size
Definition: vulkan_encode.h:63
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
FFVulkanEncodeContext::opts
FFVkEncodeCommonOptions opts
Definition: vulkan_encode.h:186
FFVulkanEncodeContext::s
FFVulkanContext s
Definition: vulkan_encode.h:170
FFVulkanEncodeContext::enc_pool
FFVkExecPool enc_pool
Definition: vulkan_encode.h:196
FFVkEncodeCommonOptions::quality
int quality
Definition: vulkan_encode.h:156
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:709
FFVulkanEncodeContext
Definition: vulkan_encode.h:169
FFVkEncodeCommonOptions::rc_mode
VkVideoEncodeRateControlModeFlagBitsKHR rc_mode
Definition: vulkan_encode.h:165
FFVulkanEncodeContext::codec
const FFVulkanCodec * codec
Definition: vulkan_encode.h:173
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:69
FFVkExecContext
Definition: vulkan.h:111
FFVulkanCodec::init_pic_params
int(* init_pic_params)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, VkVideoEncodeInfoKHR *encode_info)
Initialize codec-specific picture parameters.
Definition: vulkan_encode.h:129
FFVulkanEncodePicture::in
struct FFVulkanEncodePicture::@327 in
FFVulkanEncodeContext::common
FFVkVideoCommon common
Definition: vulkan_encode.h:171
FFVkEncodeCommonOptions::profile
int profile
Definition: vulkan_encode.h:157
FFVkEncodeCommonOptions::tune
VkVideoEncodeTuningModeKHR tune
Definition: vulkan_encode.h:163
FFVkEncodeCommonOptions::usage
VkVideoEncodeUsageFlagBitsKHR usage
Definition: vulkan_encode.h:161
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:75
FFVulkanEncodeContext::prev_buf_size
size_t prev_buf_size
Definition: vulkan_encode.h:201
FFVulkanEncodePicture::exec
FFVkExecContext * exec
Definition: vulkan_encode.h:57
profile
int profile
Definition: mxfenc.c:2297
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:580
FFVulkanEncodeContext::enc_caps
VkVideoEncodeCapabilitiesKHR enc_caps
Definition: vulkan_encode.h:192
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
FFVkExecPool
Definition: vulkan.h:252
AVCodecContext
main external API structure.
Definition: avcodec.h:431
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:1061
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:116
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:739
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
FFVulkanCodec::flags
int flags
Codec feature flags.
Definition: vulkan_encode.h:98
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:142
AVPacket
This structure stores compressed data.
Definition: packet.h:565
FFVulkanCodec
Definition: vulkan_encode.h:94
FFVulkanCodec::picture_priv_data_size
size_t picture_priv_data_size
Size of the codec-specific picture struct.
Definition: vulkan_encode.h:106
FFVkEncodeCommonOptions::content
VkVideoEncodeContentFlagBitsKHR content
Definition: vulkan_encode.h:162
AVVulkanDeviceQueueFamily
Definition: hwcontext_vulkan.h:33
FFVulkanEncodeContext::buf_pool
AVBufferPool * buf_pool
Definition: vulkan_encode.h:182
FFVulkanEncodeContext::slots
FFHWBaseEncodePicture * slots[32]
Definition: vulkan_encode.h:198
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