2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVCODEC_VAAPI_ENCODE_H
20 #define AVCODEC_VAAPI_ENCODE_H
26 #if VA_CHECK_VERSION(1, 0, 0)
27 #include <va/va_str.h>
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_vaapi.h"
36 struct VAAPIEncodeType;
37 struct VAAPIEncodePicture;
40 MAX_CONFIG_ATTRIBUTES = 4,
41 MAX_GLOBAL_PARAMS = 4,
43 MAX_PICTURE_REFERENCES = 2,
44 MAX_REORDER_DELAY = 16,
45 MAX_PARAM_BUFFER_SIZE = 1024,
48 extern const AVCodecHWConfigInternal *ff_vaapi_encode_hw_configs[];
57 typedef struct VAAPIEncodeSlice {
64 void *codec_slice_params;
67 typedef struct VAAPIEncodePicture {
68 struct VAAPIEncodePicture *next;
70 int64_t display_order;
75 #if VA_CHECK_VERSION(1, 0, 0)
88 VASurfaceID input_surface;
91 VASurfaceID recon_surface;
94 VABufferID *param_buffers;
96 AVBufferRef *output_buffer_ref;
97 VABufferID output_buffer;
100 void *codec_picture_params;
102 // Whether this picture is a reference picture.
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.
109 struct VAAPIEncodePicture *dpb[MAX_DPB_SIZE];
110 // The reference pictures used in decoding this picture. If they are
111 // used by later pictures they will also appear in the DPB.
113 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
114 // The previous reference picture in encode order. Must be in at least
115 // one of the reference list and DPB list.
116 struct VAAPIEncodePicture *prev;
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.
124 VAAPIEncodeSlice *slices;
125 } VAAPIEncodePicture;
127 typedef struct VAAPIEncodeProfile {
128 // lavc profile value (FF_PROFILE_*).
130 // Supported bit 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;
140 } VAAPIEncodeProfile;
150 RC_MODE_MAX = RC_MODE_AVBR,
153 typedef struct VAAPIEncodeRCMode {
154 // Mode from above enum (RC_MODE_*).
158 // Supported in the compile-time VAAPI version.
160 // VA mode value (VA_RC_*).
162 // Uses bitrate parameters.
164 // Supports maxrate distinct from bitrate.
166 // Uses quality value.
168 // Supports HRD/VBV parameters.
172 typedef struct VAAPIEncodeContext {
173 const AVClass *class;
175 // Codec-specific hooks.
176 const struct VAAPIEncodeType *codec;
180 // Use low power encoding mode.
183 // Number of I frames between IDR frames.
186 // Desired B frame reference depth.
189 // Explicitly set RC mode (otherwise attempt to pick from
191 int explicit_rc_mode;
193 // Explicitly-set QP, for use with the "qp" options.
194 // (Forces CQP mode when set, overriding everything else.)
197 // Desired packed headers.
198 unsigned int desired_packed_headers;
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.
206 // The block size for slice calculations.
207 int slice_block_width;
208 int slice_block_height;
210 // Everything above this point must be set before calling
211 // ff_vaapi_encode_init().
213 // Chosen encoding profile details.
214 const VAAPIEncodeProfile *profile;
216 // Chosen rate control mode details.
217 const VAAPIEncodeRCMode *rc_mode;
218 // RC quality level - meaning depends on codec and RC mode.
219 // In CQP mode this sets the fixed quantiser value.
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;
233 // Configuration attributes to use when creating va_config.
234 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
235 int nb_config_attributes;
237 VAConfigID va_config;
238 VAContextID va_context;
240 AVBufferRef *device_ref;
241 AVHWDeviceContext *device;
242 AVVAAPIDeviceContext *hwctx;
244 // The hardware frame context containing the input frames.
245 AVBufferRef *input_frames_ref;
246 AVHWFramesContext *input_frames;
248 // The hardware frame context containing the reconstructed frames.
249 AVBufferRef *recon_frames_ref;
250 AVHWFramesContext *recon_frames;
252 // Pool of (reusable) bitstream output buffers.
253 AVBufferPool *output_buffer_pool;
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];
260 int nb_global_params;
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;
270 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
271 void *codec_sequence_params;
273 // Per-sequence parameters found in the per-picture parameter
274 // structure (VAEncPictureParameterBuffer*).
275 void *codec_picture_params;
277 // Current encoding window, in display (input) order.
278 VAAPIEncodePicture *pic_start, *pic_end;
279 // The next picture to use as the previous reference picture in
281 VAAPIEncodePicture *next_prev;
283 // Next input order index (display order).
285 // Number of frames that output is behind input.
286 int64_t output_delay;
287 // Next encode order index.
288 int64_t encode_order;
289 // Number of frames decode output will need to be delayed.
290 int64_t decode_delay;
291 // Next output order index (in encode order).
292 int64_t output_order;
294 // Timestamp handling.
296 int64_t dts_pts_diff;
297 int64_t ts_ring[MAX_REORDER_DELAY * 3];
300 int slice_block_rows;
301 int slice_block_cols;
305 // Frame type decision.
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.
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.
333 } VAAPIEncodeContext;
336 // Codec supports controlling the subdivision of pictures into slices.
337 FLAG_SLICE_CONTROL = 1 << 0,
338 // Codec only supports constant quality (no rate control).
339 FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
340 // Codec is intra-only.
341 FLAG_INTRA_ONLY = 1 << 2,
342 // Codec supports B-pictures.
343 FLAG_B_PICTURES = 1 << 3,
344 // Codec supports referencing B-pictures.
345 FLAG_B_PICTURE_REFERENCES = 1 << 4,
346 // Codec supports non-IDR key pictures (that is, key pictures do
347 // not necessarily empty the DPB).
348 FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
351 typedef struct VAAPIEncodeType {
352 // List of supported profiles and corresponding VAAPI profiles.
353 // (Must end with FF_PROFILE_UNKNOWN.)
354 const VAAPIEncodeProfile *profiles;
356 // Codec feature flags.
359 // Default quality for this codec - used as quantiser or RC quality
360 // factor depending on RC mode.
363 // Perform any extra codec-specific configuration after the
364 // codec context is initialised (set up the private data and
365 // add any necessary global parameters).
366 int (*configure)(AVCodecContext *avctx);
368 // The size of any private data structure associated with each
369 // picture (can be zero if not required).
370 size_t picture_priv_data_size;
372 // The size of the parameter structures:
373 // sizeof(VAEnc{type}ParameterBuffer{codec}).
374 size_t sequence_params_size;
375 size_t picture_params_size;
376 size_t slice_params_size;
378 // Fill the parameter structures.
379 int (*init_sequence_params)(AVCodecContext *avctx);
380 int (*init_picture_params)(AVCodecContext *avctx,
381 VAAPIEncodePicture *pic);
382 int (*init_slice_params)(AVCodecContext *avctx,
383 VAAPIEncodePicture *pic,
384 VAAPIEncodeSlice *slice);
386 // The type used by the packed header: this should look like
387 // VAEncPackedHeader{something}.
388 int sequence_header_type;
389 int picture_header_type;
390 int slice_header_type;
392 // Write the packed header data to the provided buffer.
393 // The sequence header is also used to fill the codec extradata
394 // when the encoder is starting.
395 int (*write_sequence_header)(AVCodecContext *avctx,
396 char *data, size_t *data_len);
397 int (*write_picture_header)(AVCodecContext *avctx,
398 VAAPIEncodePicture *pic,
399 char *data, size_t *data_len);
400 int (*write_slice_header)(AVCodecContext *avctx,
401 VAAPIEncodePicture *pic,
402 VAAPIEncodeSlice *slice,
403 char *data, size_t *data_len);
405 // Fill an extra parameter structure, which will then be
406 // passed to vaRenderPicture(). Will be called repeatedly
407 // with increasing index argument until AVERROR_EOF is
409 int (*write_extra_buffer)(AVCodecContext *avctx,
410 VAAPIEncodePicture *pic,
411 int index, int *type,
412 char *data, size_t *data_len);
414 // Write an extra packed header. Will be called repeatedly
415 // with increasing index argument until AVERROR_EOF is
417 int (*write_extra_header)(AVCodecContext *avctx,
418 VAAPIEncodePicture *pic,
419 int index, int *type,
420 char *data, size_t *data_len);
424 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
426 int ff_vaapi_encode_init(AVCodecContext *avctx);
427 int ff_vaapi_encode_close(AVCodecContext *avctx);
430 #define VAAPI_ENCODE_COMMON_OPTIONS \
432 "Use low-power encoding mode (only available on some platforms; " \
433 "may not support all encoding features)", \
434 OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
435 { .i64 = 0 }, 0, 1, FLAGS }, \
437 "Distance (in I-frames) between IDR frames", \
438 OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
439 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
441 "Maximum B-frame reference depth", \
442 OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
443 { .i64 = 1 }, 1, INT_MAX, FLAGS }
445 #define VAAPI_ENCODE_RC_MODE(name, desc) \
446 { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
447 0, 0, FLAGS, "rc_mode" }
448 #define VAAPI_ENCODE_RC_OPTIONS \
450 "Set rate control mode", \
451 OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
452 { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \
453 { "auto", "Choose mode automatically based on other parameters", \
454 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \
455 VAAPI_ENCODE_RC_MODE(CQP, "Constant-quality"), \
456 VAAPI_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \
457 VAAPI_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \
458 VAAPI_ENCODE_RC_MODE(ICQ, "Intelligent constant-quality"), \
459 VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
460 VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
463 #endif /* AVCODEC_VAAPI_ENCODE_H */