FFmpeg
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 #if VA_CHECK_VERSION(1, 0, 0)
73  // ROI regions.
74  VAEncROI *roi;
75 #else
76  void *roi;
77 #endif
78 
79  int type;
80  int b_depth;
83 
85  VASurfaceID input_surface;
86 
88  VASurfaceID recon_surface;
89 
91  VABufferID *param_buffers;
92 
94  VABufferID output_buffer;
95 
96  void *priv_data;
98 
99  // Whether this picture is a reference picture.
101 
102  // The contents of the DPB after this picture has been decoded.
103  // This will contain the picture itself if it is a reference picture,
104  // but not if it isn't.
107  // The reference pictures used in decoding this picture. If they are
108  // used by later pictures they will also appear in the DPB.
109  int nb_refs;
111  // The previous reference picture in encode order. Must be in at least
112  // one of the reference list and DPB list.
114  // Reference count for other pictures referring to this one through
115  // the above pointers, directly from incomplete pictures and indirectly
116  // through completed pictures.
117  int ref_count[2];
118  int ref_removed[2];
119 
123 
124 typedef struct VAAPIEncodeProfile {
125  // lavc profile value (FF_PROFILE_*).
127  // Supported bit depth.
128  int depth;
129  // Number of components.
131  // Chroma subsampling in width dimension.
133  // Chroma subsampling in height dimension.
135  // VAAPI profile value.
136  VAProfile va_profile;
138 
139 enum {
148 };
149 
150 typedef struct VAAPIEncodeRCMode {
151  // Mode from above enum (RC_MODE_*).
152  int mode;
153  // Name.
154  const char *name;
155  // Supported in the compile-time VAAPI version.
157  // VA mode value (VA_RC_*).
158  uint32_t va_mode;
159  // Uses bitrate parameters.
160  int bitrate;
161  // Supports maxrate distinct from bitrate.
162  int maxrate;
163  // Uses quality value.
164  int quality;
165  // Supports HRD/VBV parameters.
166  int hrd;
168 
169 typedef struct VAAPIEncodeContext {
170  const AVClass *class;
171 
172  // Codec-specific hooks.
173  const struct VAAPIEncodeType *codec;
174 
175  // Global options.
176 
177  // Use low power encoding mode.
179 
180  // Number of I frames between IDR frames.
182 
183  // Desired B frame reference depth.
185 
186  // Explicitly set RC mode (otherwise attempt to pick from
187  // available modes).
189 
190  // Explicitly-set QP, for use with the "qp" options.
191  // (Forces CQP mode when set, overriding everything else.)
193 
194  // Desired packed headers.
196 
197  // The required size of surfaces. This is probably the input
198  // size (AVCodecContext.width|height) aligned up to whatever
199  // block size is required by the codec.
202 
203  // The block size for slice calculations.
206 
207  // Everything above this point must be set before calling
208  // ff_vaapi_encode_init().
209 
210  // Chosen encoding profile details.
212 
213  // Chosen rate control mode details.
215  // RC quality level - meaning depends on codec and RC mode.
216  // In CQP mode this sets the fixed quantiser value.
218 
219  // Encoding profile (VAProfile*).
220  VAProfile va_profile;
221  // Encoding entrypoint (VAEntryoint*).
222  VAEntrypoint va_entrypoint;
223  // Rate control mode.
224  unsigned int va_rc_mode;
225  // Bitrate for codec-specific encoder parameters.
226  unsigned int va_bit_rate;
227  // Packed headers which will actually be sent.
228  unsigned int va_packed_headers;
229 
230  // Configuration attributes to use when creating va_config.
231  VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
233 
234  VAConfigID va_config;
235  VAContextID va_context;
236 
240 
241  // The hardware frame context containing the input frames.
244 
245  // The hardware frame context containing the reconstructed frames.
248 
249  // Pool of (reusable) bitstream output buffers.
251 
252  // Global parameters which will be applied at the start of the
253  // sequence (includes rate control parameters below).
254  int global_params_type[MAX_GLOBAL_PARAMS];
255  const void *global_params [MAX_GLOBAL_PARAMS];
256  size_t global_params_size[MAX_GLOBAL_PARAMS];
258 
259  // Rate control parameters.
260  VAEncMiscParameterRateControl rc_params;
261  VAEncMiscParameterHRD hrd_params;
262  VAEncMiscParameterFrameRate fr_params;
263 #if VA_CHECK_VERSION(0, 36, 0)
264  VAEncMiscParameterBufferQualityLevel quality_params;
265 #endif
266 
267  // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
269 
270  // Per-sequence parameters found in the per-picture parameter
271  // structure (VAEncPictureParameterBuffer*).
273 
274  // Current encoding window, in display (input) order.
276  // The next picture to use as the previous reference picture in
277  // encoding order.
279 
280  // Next input order index (display order).
281  int64_t input_order;
282  // Number of frames that output is behind input.
283  int64_t output_delay;
284  // Next encode order index.
285  int64_t encode_order;
286  // Number of frames decode output will need to be delayed.
287  int64_t decode_delay;
288  // Next output order index (in encode order).
289  int64_t output_order;
290 
291  // Timestamp handling.
292  int64_t first_pts;
293  int64_t dts_pts_diff;
294  int64_t ts_ring[MAX_REORDER_DELAY * 3];
295 
296  // Slice structure.
301 
302  // Frame type decision.
303  int gop_size;
306  int p_per_i;
308  int b_per_p;
313 
314  // Whether the driver supports ROI at all.
316  // Maximum number of regions supported by the driver.
318  // Quantisation range for offset calculations. Set by codec-specific
319  // code, as it may change based on parameters.
321 
322  // The encoder does not support cropping information, so warn about
323  // it the first time we encounter any nonzero crop fields.
325  // If the driver does not support ROI then warn the first time we
326  // encounter a frame with ROI side data.
329 
330 enum {
331  // Codec supports controlling the subdivision of pictures into slices.
333  // Codec only supports constant quality (no rate control).
335  // Codec is intra-only.
336  FLAG_INTRA_ONLY = 1 << 2,
337  // Codec supports B-pictures.
338  FLAG_B_PICTURES = 1 << 3,
339  // Codec supports referencing B-pictures.
341  // Codec supports non-IDR key pictures (that is, key pictures do
342  // not necessarily empty the DPB).
344 };
345 
346 typedef struct VAAPIEncodeType {
347  // List of supported profiles and corresponding VAAPI profiles.
348  // (Must end with FF_PROFILE_UNKNOWN.)
350 
351  // Codec feature flags.
352  int flags;
353 
354  // Default quality for this codec - used as quantiser or RC quality
355  // factor depending on RC mode.
357 
358  // Perform any extra codec-specific configuration after the
359  // codec context is initialised (set up the private data and
360  // add any necessary global parameters).
361  int (*configure)(AVCodecContext *avctx);
362 
363  // The size of any private data structure associated with each
364  // picture (can be zero if not required).
366 
367  // The size of the parameter structures:
368  // sizeof(VAEnc{type}ParameterBuffer{codec}).
372 
373  // Fill the parameter structures.
374  int (*init_sequence_params)(AVCodecContext *avctx);
375  int (*init_picture_params)(AVCodecContext *avctx,
376  VAAPIEncodePicture *pic);
377  int (*init_slice_params)(AVCodecContext *avctx,
378  VAAPIEncodePicture *pic,
379  VAAPIEncodeSlice *slice);
380 
381  // The type used by the packed header: this should look like
382  // VAEncPackedHeader{something}.
386 
387  // Write the packed header data to the provided buffer.
388  // The sequence header is also used to fill the codec extradata
389  // when the encoder is starting.
390  int (*write_sequence_header)(AVCodecContext *avctx,
391  char *data, size_t *data_len);
392  int (*write_picture_header)(AVCodecContext *avctx,
393  VAAPIEncodePicture *pic,
394  char *data, size_t *data_len);
395  int (*write_slice_header)(AVCodecContext *avctx,
396  VAAPIEncodePicture *pic,
397  VAAPIEncodeSlice *slice,
398  char *data, size_t *data_len);
399 
400  // Fill an extra parameter structure, which will then be
401  // passed to vaRenderPicture(). Will be called repeatedly
402  // with increasing index argument until AVERROR_EOF is
403  // returned.
404  int (*write_extra_buffer)(AVCodecContext *avctx,
405  VAAPIEncodePicture *pic,
406  int index, int *type,
407  char *data, size_t *data_len);
408 
409  // Write an extra packed header. Will be called repeatedly
410  // with increasing index argument until AVERROR_EOF is
411  // returned.
412  int (*write_extra_header)(AVCodecContext *avctx,
413  VAAPIEncodePicture *pic,
414  int index, int *type,
415  char *data, size_t *data_len);
417 
418 
421 
424 
425 
426 #define VAAPI_ENCODE_COMMON_OPTIONS \
427  { "low_power", \
428  "Use low-power encoding mode (only available on some platforms; " \
429  "may not support all encoding features)", \
430  OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
431  { .i64 = 0 }, 0, 1, FLAGS }, \
432  { "idr_interval", \
433  "Distance (in I-frames) between IDR frames", \
434  OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
435  { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
436  { "b_depth", \
437  "Maximum B-frame reference depth", \
438  OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
439  { .i64 = 1 }, 1, INT_MAX, FLAGS }
440 
441 #define VAAPI_ENCODE_RC_MODE(name, desc) \
442  { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
443  0, 0, FLAGS, "rc_mode" }
444 #define VAAPI_ENCODE_RC_OPTIONS \
445  { "rc_mode",\
446  "Set rate control mode", \
447  OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
448  { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \
449  { "auto", "Choose mode automatically based on other parameters", \
450  0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \
451  VAAPI_ENCODE_RC_MODE(CQP, "Constant-quality"), \
452  VAAPI_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \
453  VAAPI_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \
454  VAAPI_ENCODE_RC_MODE(ICQ, "Intelligent constant-quality"), \
455  VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
456  VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
457 
458 
459 #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:85
VAProfile va_profile
Definition: vaapi_encode.h:220
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
VAEntrypoint va_entrypoint
Definition: vaapi_encode.h:222
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
GLint GLenum type
Definition: opengl_enc.c:104
void * codec_sequence_params
Definition: vaapi_encode.h:268
AVBufferRef * input_frames_ref
Definition: vaapi_encode.h:242
static AVPacket pkt
size_t picture_params_size
Definition: vaapi_encode.h:370
AVHWDeviceContext * device
Definition: vaapi_encode.h:238
API-specific header for AV_HWDEVICE_TYPE_VAAPI.
unsigned int va_packed_headers
Definition: vaapi_encode.h:228
const VAAPIEncodeRCMode * rc_mode
Definition: vaapi_encode.h:214
AVBufferRef * output_buffer_ref
Definition: vaapi_encode.h:93
VABufferID * param_buffers
Definition: vaapi_encode.h:91
VAContextID va_context
Definition: vaapi_encode.h:235
struct VAAPIEncodePicture * prev
Definition: vaapi_encode.h:113
VASurfaceID recon_surface
Definition: vaapi_encode.h:88
int ff_vaapi_encode_init(AVCodecContext *avctx)
The buffer pool.
unsigned int va_rc_mode
Definition: vaapi_encode.h:224
AVHWFramesContext * input_frames
Definition: vaapi_encode.h:243
int ff_vaapi_encode_close(AVCodecContext *avctx)
unsigned int va_bit_rate
Definition: vaapi_encode.h:226
void * codec_picture_params
Definition: vaapi_encode.h:97
VAConfigID va_config
Definition: vaapi_encode.h:234
AVHWFramesContext * recon_frames
Definition: vaapi_encode.h:247
VAAPIEncodeSlice * slices
Definition: vaapi_encode.h:121
const VAAPIEncodeProfile * profile
Definition: vaapi_encode.h:211
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:349
VAEncMiscParameterRateControl rc_params
Definition: vaapi_encode.h:260
VAAPIEncodePicture * next_prev
Definition: vaapi_encode.h:278
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
struct VAAPIEncodePicture * next
Definition: vaapi_encode.h:65
AVFrame * input_image
Definition: vaapi_encode.h:84
void * codec_picture_params
Definition: vaapi_encode.h:272
AVBufferPool * output_buffer_pool
Definition: vaapi_encode.h:250
int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame)
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:110
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:173
Libavcodec external API header.
VAAPIEncodePicture * pic_start
Definition: vaapi_encode.h:275
main external API structure.
Definition: avcodec.h:1568
size_t slice_params_size
Definition: vaapi_encode.h:371
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
AVBufferRef * recon_frames_ref
Definition: vaapi_encode.h:246
AVBufferRef * device_ref
Definition: vaapi_encode.h:237
size_t picture_priv_data_size
Definition: vaapi_encode.h:365
const char * name
Definition: vaapi_encode.h:154
A reference to a data buffer.
Definition: buffer.h:81
size_t sequence_params_size
Definition: vaapi_encode.h:369
VAEncMiscParameterHRD hrd_params
Definition: vaapi_encode.h:261
VAProfile va_profile
Definition: vaapi_encode.h:136
int
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
VAEncMiscParameterFrameRate fr_params
Definition: vaapi_encode.h:262
void * codec_slice_params
Definition: vaapi_encode.h:61
AVFrame * recon_image
Definition: vaapi_encode.h:87
VAAPI connection details.
unsigned int desired_packed_headers
Definition: vaapi_encode.h:195
VABufferID output_buffer
Definition: vaapi_encode.h:94
This structure stores compressed data.
Definition: avcodec.h:1457
struct VAAPIEncodePicture * dpb[MAX_DPB_SIZE]
Definition: vaapi_encode.h:106
AVVAAPIDeviceContext * hwctx
Definition: vaapi_encode.h:239