FFmpeg  4.3.9
vaapi_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_VAAPI_ENCODE_H
20 #define AVCODEC_VAAPI_ENCODE_H
21 
22 #include <stdint.h>
23 
24 #include <va/va.h>
25 
26 #if VA_CHECK_VERSION(1, 0, 0)
27 #include <va/va_str.h>
28 #endif
29 
30 #include "libavutil/hwcontext.h"
32 
33 #include "avcodec.h"
34 #include "hwconfig.h"
35 
36 struct VAAPIEncodeType;
37 struct VAAPIEncodePicture;
38 
39 enum {
46 };
47 
49 
50 enum {
55 };
56 
57 typedef struct VAAPIEncodeSlice {
58  int index;
59  int row_start;
60  int row_size;
63  void *priv_data;
66 
67 typedef struct VAAPIEncodePicture {
69 
73  int force_idr;
74 
75 #if VA_CHECK_VERSION(1, 0, 0)
76  // ROI regions.
77  VAEncROI *roi;
78 #else
79  void *roi;
80 #endif
81 
82  int type;
83  int b_depth;
86 
88  VASurfaceID input_surface;
89 
91  VASurfaceID recon_surface;
92 
94  VABufferID *param_buffers;
95 
97  VABufferID output_buffer;
98 
99  void *priv_data;
101 
102  // Whether this picture is a reference picture.
104 
105  // The contents of the DPB after this picture has been decoded.
106  // This will contain the picture itself if it is a reference picture,
107  // but not if it isn't.
110  // The reference pictures used in decoding this picture. If they are
111  // used by later pictures they will also appear in the DPB.
112  int nb_refs;
114  // The previous reference picture in encode order. Must be in at least
115  // one of the reference list and DPB list.
117  // Reference count for other pictures referring to this one through
118  // the above pointers, directly from incomplete pictures and indirectly
119  // through completed pictures.
120  int ref_count[2];
121  int ref_removed[2];
122 
126 
127 typedef struct VAAPIEncodeProfile {
128  // lavc profile value (FF_PROFILE_*).
130  // Supported bit depth.
131  int depth;
132  // Number of components.
134  // Chroma subsampling in width dimension.
136  // Chroma subsampling in height dimension.
138  // VAAPI profile value.
139  VAProfile va_profile;
141 
142 enum {
151 };
152 
153 typedef struct VAAPIEncodeRCMode {
154  // Mode from above enum (RC_MODE_*).
155  int mode;
156  // Name.
157  const char *name;
158  // Supported in the compile-time VAAPI version.
160  // VA mode value (VA_RC_*).
161  uint32_t va_mode;
162  // Uses bitrate parameters.
163  int bitrate;
164  // Supports maxrate distinct from bitrate.
165  int maxrate;
166  // Uses quality value.
167  int quality;
168  // Supports HRD/VBV parameters.
169  int hrd;
171 
172 typedef struct VAAPIEncodeContext {
173  const AVClass *class;
174 
175  // Codec-specific hooks.
176  const struct VAAPIEncodeType *codec;
177 
178  // Global options.
179 
180  // Use low power encoding mode.
182 
183  // Number of I frames between IDR frames.
185 
186  // Desired B frame reference depth.
188 
189  // Explicitly set RC mode (otherwise attempt to pick from
190  // available modes).
192 
193  // Explicitly-set QP, for use with the "qp" options.
194  // (Forces CQP mode when set, overriding everything else.)
196 
197  // Desired packed headers.
199 
200  // The required size of surfaces. This is probably the input
201  // size (AVCodecContext.width|height) aligned up to whatever
202  // block size is required by the codec.
205 
206  // The block size for slice calculations.
209 
210  // Everything above this point must be set before calling
211  // ff_vaapi_encode_init().
212 
213  // Chosen encoding profile details.
215 
216  // Chosen rate control mode details.
218  // RC quality level - meaning depends on codec and RC mode.
219  // In CQP mode this sets the fixed quantiser value.
221 
222  // Encoding profile (VAProfile*).
223  VAProfile va_profile;
224  // Encoding entrypoint (VAEntryoint*).
225  VAEntrypoint va_entrypoint;
226  // Rate control mode.
227  unsigned int va_rc_mode;
228  // Bitrate for codec-specific encoder parameters.
229  unsigned int va_bit_rate;
230  // Packed headers which will actually be sent.
231  unsigned int va_packed_headers;
232 
233  // Configuration attributes to use when creating va_config.
234  VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
236 
237  VAConfigID va_config;
238  VAContextID va_context;
239 
243 
244  // The hardware frame context containing the input frames.
247 
248  // The hardware frame context containing the reconstructed frames.
251 
252  // Pool of (reusable) bitstream output buffers.
254 
255  // Global parameters which will be applied at the start of the
256  // sequence (includes rate control parameters below).
257  int global_params_type[MAX_GLOBAL_PARAMS];
258  const void *global_params [MAX_GLOBAL_PARAMS];
259  size_t global_params_size[MAX_GLOBAL_PARAMS];
261 
262  // Rate control parameters.
263  VAEncMiscParameterRateControl rc_params;
264  VAEncMiscParameterHRD hrd_params;
265  VAEncMiscParameterFrameRate fr_params;
266 #if VA_CHECK_VERSION(0, 36, 0)
267  VAEncMiscParameterBufferQualityLevel quality_params;
268 #endif
269 
270  // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
272 
273  // Per-sequence parameters found in the per-picture parameter
274  // structure (VAEncPictureParameterBuffer*).
276 
277  // Current encoding window, in display (input) order.
279  // The next picture to use as the previous reference picture in
280  // encoding order.
282 
283  // Next input order index (display order).
285  // Number of frames that output is behind input.
287  // Next encode order index.
289  // Number of frames decode output will need to be delayed.
291  // Next output order index (in encode order).
293 
294  // Timestamp handling.
298 
299  // Slice structure.
304 
305  // Frame type decision.
306  int gop_size;
309  int p_per_i;
311  int b_per_p;
316 
317  // Whether the driver supports ROI at all.
319  // Maximum number of regions supported by the driver.
321  // Quantisation range for offset calculations. Set by codec-specific
322  // code, as it may change based on parameters.
324 
325  // The encoder does not support cropping information, so warn about
326  // it the first time we encounter any nonzero crop fields.
328  // If the driver does not support ROI then warn the first time we
329  // encounter a frame with ROI side data.
332 
333 enum {
334  // Codec supports controlling the subdivision of pictures into slices.
336  // Codec only supports constant quality (no rate control).
338  // Codec is intra-only.
339  FLAG_INTRA_ONLY = 1 << 2,
340  // Codec supports B-pictures.
341  FLAG_B_PICTURES = 1 << 3,
342  // Codec supports referencing B-pictures.
344  // Codec supports non-IDR key pictures (that is, key pictures do
345  // not necessarily empty the DPB).
347 };
348 
349 typedef struct VAAPIEncodeType {
350  // List of supported profiles and corresponding VAAPI profiles.
351  // (Must end with FF_PROFILE_UNKNOWN.)
353 
354  // Codec feature flags.
355  int flags;
356 
357  // Default quality for this codec - used as quantiser or RC quality
358  // factor depending on RC mode.
360 
361  // Perform any extra codec-specific configuration after the
362  // codec context is initialised (set up the private data and
363  // add any necessary global parameters).
364  int (*configure)(AVCodecContext *avctx);
365 
366  // The size of any private data structure associated with each
367  // picture (can be zero if not required).
369 
370  // The size of the parameter structures:
371  // sizeof(VAEnc{type}ParameterBuffer{codec}).
375 
376  // Fill the parameter structures.
377  int (*init_sequence_params)(AVCodecContext *avctx);
378  int (*init_picture_params)(AVCodecContext *avctx,
379  VAAPIEncodePicture *pic);
380  int (*init_slice_params)(AVCodecContext *avctx,
381  VAAPIEncodePicture *pic,
382  VAAPIEncodeSlice *slice);
383 
384  // The type used by the packed header: this should look like
385  // VAEncPackedHeader{something}.
389 
390  // Write the packed header data to the provided buffer.
391  // The sequence header is also used to fill the codec extradata
392  // when the encoder is starting.
393  int (*write_sequence_header)(AVCodecContext *avctx,
394  char *data, size_t *data_len);
395  int (*write_picture_header)(AVCodecContext *avctx,
396  VAAPIEncodePicture *pic,
397  char *data, size_t *data_len);
398  int (*write_slice_header)(AVCodecContext *avctx,
399  VAAPIEncodePicture *pic,
400  VAAPIEncodeSlice *slice,
401  char *data, size_t *data_len);
402 
403  // Fill an extra parameter structure, which will then be
404  // passed to vaRenderPicture(). Will be called repeatedly
405  // with increasing index argument until AVERROR_EOF is
406  // returned.
407  int (*write_extra_buffer)(AVCodecContext *avctx,
408  VAAPIEncodePicture *pic,
409  int index, int *type,
410  char *data, size_t *data_len);
411 
412  // Write an extra packed header. Will be called repeatedly
413  // with increasing index argument until AVERROR_EOF is
414  // returned.
415  int (*write_extra_header)(AVCodecContext *avctx,
416  VAAPIEncodePicture *pic,
417  int index, int *type,
418  char *data, size_t *data_len);
420 
421 
424 
427 
428 
429 #define VAAPI_ENCODE_COMMON_OPTIONS \
430  { "low_power", \
431  "Use low-power encoding mode (only available on some platforms; " \
432  "may not support all encoding features)", \
433  OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
434  { .i64 = 0 }, 0, 1, FLAGS }, \
435  { "idr_interval", \
436  "Distance (in I-frames) between IDR frames", \
437  OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
438  { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
439  { "b_depth", \
440  "Maximum B-frame reference depth", \
441  OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
442  { .i64 = 1 }, 1, INT_MAX, FLAGS }
443 
444 #define VAAPI_ENCODE_RC_MODE(name, desc) \
445  { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
446  0, 0, FLAGS, "rc_mode" }
447 #define VAAPI_ENCODE_RC_OPTIONS \
448  { "rc_mode",\
449  "Set rate control mode", \
450  OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
451  { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \
452  { "auto", "Choose mode automatically based on other parameters", \
453  0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \
454  VAAPI_ENCODE_RC_MODE(CQP, "Constant-quality"), \
455  VAAPI_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \
456  VAAPI_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \
457  VAAPI_ENCODE_RC_MODE(ICQ, "Intelligent constant-quality"), \
458  VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
459  VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
460 
461 
462 #endif /* AVCODEC_VAAPI_ENCODE_H */
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
VASurfaceID input_surface
Definition: vaapi_encode.h:88
VAProfile va_profile
Definition: vaapi_encode.h:223
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
VAEntrypoint va_entrypoint
Definition: vaapi_encode.h:225
void * codec_sequence_params
Definition: vaapi_encode.h:271
AVBufferRef * input_frames_ref
Definition: vaapi_encode.h:245
static AVPacket pkt
size_t picture_params_size
Definition: vaapi_encode.h:373
AVHWDeviceContext * device
Definition: vaapi_encode.h:241
API-specific header for AV_HWDEVICE_TYPE_VAAPI.
unsigned int va_packed_headers
Definition: vaapi_encode.h:231
const VAAPIEncodeRCMode * rc_mode
Definition: vaapi_encode.h:217
AVBufferRef * output_buffer_ref
Definition: vaapi_encode.h:96
static AVFrame * frame
const char data[16]
Definition: mxf.c:91
VABufferID * param_buffers
Definition: vaapi_encode.h:94
VAContextID va_context
Definition: vaapi_encode.h:238
struct VAAPIEncodePicture * prev
Definition: vaapi_encode.h:116
VASurfaceID recon_surface
Definition: vaapi_encode.h:91
int ff_vaapi_encode_init(AVCodecContext *avctx)
The buffer pool.
unsigned int va_rc_mode
Definition: vaapi_encode.h:227
AVHWFramesContext * input_frames
Definition: vaapi_encode.h:246
const AVCodecHWConfigInternal * ff_vaapi_encode_hw_configs[]
Definition: vaapi_encode.c:30
int ff_vaapi_encode_close(AVCodecContext *avctx)
unsigned int va_bit_rate
Definition: vaapi_encode.h:229
void * codec_picture_params
Definition: vaapi_encode.h:100
VAConfigID va_config
Definition: vaapi_encode.h:237
AVHWFramesContext * recon_frames
Definition: vaapi_encode.h:250
VAAPIEncodeSlice * slices
Definition: vaapi_encode.h:124
const VAAPIEncodeProfile * profile
Definition: vaapi_encode.h:214
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:352
VAEncMiscParameterRateControl rc_params
Definition: vaapi_encode.h:263
VAAPIEncodePicture * next_prev
Definition: vaapi_encode.h:281
struct VAAPIEncodePicture * next
Definition: vaapi_encode.h:68
AVFrame * input_image
Definition: vaapi_encode.h:87
void * codec_picture_params
Definition: vaapi_encode.h:275
AVBufferPool * output_buffer_pool
Definition: vaapi_encode.h:253
int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame)
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:113
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:176
Libavcodec external API header.
VAAPIEncodePicture * pic_start
Definition: vaapi_encode.h:278
main external API structure.
Definition: avcodec.h:526
long long int64_t
Definition: coverity.c:34
size_t slice_params_size
Definition: vaapi_encode.h:374
Describe the class of an AVClass context structure.
Definition: log.h:67
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
cl_device_type type
AVBufferRef * recon_frames_ref
Definition: vaapi_encode.h:249
AVBufferRef * device_ref
Definition: vaapi_encode.h:240
size_t picture_priv_data_size
Definition: vaapi_encode.h:368
const char * name
Definition: vaapi_encode.h:157
A reference to a data buffer.
Definition: buffer.h:81
size_t sequence_params_size
Definition: vaapi_encode.h:372
VAEncMiscParameterHRD hrd_params
Definition: vaapi_encode.h:264
VAProfile va_profile
Definition: vaapi_encode.h:139
int
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
VAEncMiscParameterFrameRate fr_params
Definition: vaapi_encode.h:265
void * codec_slice_params
Definition: vaapi_encode.h:64
AVFrame * recon_image
Definition: vaapi_encode.h:90
VAAPI connection details.
unsigned int desired_packed_headers
Definition: vaapi_encode.h:198
VABufferID output_buffer
Definition: vaapi_encode.h:97
This structure stores compressed data.
Definition: packet.h:332
struct VAAPIEncodePicture * dpb[MAX_DPB_SIZE]
Definition: vaapi_encode.h:109
AVVAAPIDeviceContext * hwctx
Definition: vaapi_encode.h:242