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