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"
35 struct VAAPIEncodeType;
36 struct VAAPIEncodePicture;
39 MAX_CONFIG_ATTRIBUTES = 4,
40 MAX_GLOBAL_PARAMS = 4,
42 MAX_PICTURE_REFERENCES = 2,
43 MAX_REORDER_DELAY = 16,
44 MAX_PARAM_BUFFER_SIZE = 1024,
54 typedef struct VAAPIEncodeSlice {
61 void *codec_slice_params;
64 typedef struct VAAPIEncodePicture {
65 struct VAAPIEncodePicture *next;
67 int64_t display_order;
78 VASurfaceID input_surface;
81 VASurfaceID recon_surface;
84 VABufferID *param_buffers;
86 AVBufferRef *output_buffer_ref;
87 VABufferID output_buffer;
90 void *codec_picture_params;
92 // Whether this picture is a reference picture.
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.
99 struct VAAPIEncodePicture *dpb[MAX_DPB_SIZE];
100 // The reference pictures used in decoding this picture. If they are
101 // used by later pictures they will also appear in the DPB.
103 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
104 // The previous reference picture in encode order. Must be in at least
105 // one of the reference list and DPB list.
106 struct VAAPIEncodePicture *prev;
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.
114 VAAPIEncodeSlice *slices;
115 } VAAPIEncodePicture;
117 typedef struct VAAPIEncodeProfile {
118 // lavc profile value (FF_PROFILE_*).
120 // Supported bit 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;
130 } VAAPIEncodeProfile;
140 RC_MODE_MAX = RC_MODE_AVBR,
143 typedef struct VAAPIEncodeRCMode {
144 // Mode from above enum (RC_MODE_*).
148 // Supported in the compile-time VAAPI version.
150 // VA mode value (VA_RC_*).
152 // Uses bitrate parameters.
154 // Supports maxrate distinct from bitrate.
156 // Uses quality value.
158 // Supports HRD/VBV parameters.
162 typedef struct VAAPIEncodeContext {
163 const AVClass *class;
165 // Codec-specific hooks.
166 const struct VAAPIEncodeType *codec;
170 // Use low power encoding mode.
173 // Number of I frames between IDR frames.
176 // Desired B frame reference depth.
179 // Explicitly set RC mode (otherwise attempt to pick from
181 int explicit_rc_mode;
183 // Explicitly-set QP, for use with the "qp" options.
184 // (Forces CQP mode when set, overriding everything else.)
187 // Desired packed headers.
188 unsigned int desired_packed_headers;
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.
196 // The block size for slice calculations.
197 int slice_block_width;
198 int slice_block_height;
200 // Everything above this point must be set before calling
201 // ff_vaapi_encode_init().
203 // Chosen encoding profile details.
204 const VAAPIEncodeProfile *profile;
206 // Chosen rate control mode details.
207 const VAAPIEncodeRCMode *rc_mode;
208 // RC quality level - meaning depends on codec and RC mode.
209 // In CQP mode this sets the fixed quantiser value.
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;
223 // Configuration attributes to use when creating va_config.
224 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
225 int nb_config_attributes;
227 VAConfigID va_config;
228 VAContextID va_context;
230 AVBufferRef *device_ref;
231 AVHWDeviceContext *device;
232 AVVAAPIDeviceContext *hwctx;
234 // The hardware frame context containing the input frames.
235 AVBufferRef *input_frames_ref;
236 AVHWFramesContext *input_frames;
238 // The hardware frame context containing the reconstructed frames.
239 AVBufferRef *recon_frames_ref;
240 AVHWFramesContext *recon_frames;
242 // Pool of (reusable) bitstream output buffers.
243 AVBufferPool *output_buffer_pool;
245 // Global parameters which will be applied at the start of the
246 // sequence (includes rate control parameters below).
247 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
248 size_t global_params_size[MAX_GLOBAL_PARAMS];
249 int nb_global_params;
251 // Rate control parameters.
253 VAEncMiscParameterBuffer misc;
254 VAEncMiscParameterRateControl rc;
257 VAEncMiscParameterBuffer misc;
258 VAEncMiscParameterHRD hrd;
261 VAEncMiscParameterBuffer misc;
262 VAEncMiscParameterFrameRate fr;
264 #if VA_CHECK_VERSION(0, 36, 0)
266 VAEncMiscParameterBuffer misc;
267 VAEncMiscParameterBufferQualityLevel quality;
271 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
272 void *codec_sequence_params;
274 // Per-sequence parameters found in the per-picture parameter
275 // structure (VAEncPictureParameterBuffer*).
276 void *codec_picture_params;
278 // Current encoding window, in display (input) order.
279 VAAPIEncodePicture *pic_start, *pic_end;
280 // The next picture to use as the previous reference picture in
282 VAAPIEncodePicture *next_prev;
284 // Next input order index (display order).
286 // Number of frames that output is behind input.
287 int64_t output_delay;
288 // Next encode order index.
289 int64_t encode_order;
290 // Number of frames decode output will need to be delayed.
291 int64_t decode_delay;
292 // Next output order index (in encode order).
293 int64_t output_order;
295 // Timestamp handling.
297 int64_t dts_pts_diff;
298 int64_t ts_ring[MAX_REORDER_DELAY * 3];
301 int slice_block_rows;
302 int slice_block_cols;
306 // Frame type decision.
318 // The encoder does not support cropping information, so warn about
319 // it the first time we encounter any nonzero crop fields.
321 } VAAPIEncodeContext;
324 // Codec supports controlling the subdivision of pictures into slices.
325 FLAG_SLICE_CONTROL = 1 << 0,
326 // Codec only supports constant quality (no rate control).
327 FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
328 // Codec is intra-only.
329 FLAG_INTRA_ONLY = 1 << 2,
330 // Codec supports B-pictures.
331 FLAG_B_PICTURES = 1 << 3,
332 // Codec supports referencing B-pictures.
333 FLAG_B_PICTURE_REFERENCES = 1 << 4,
334 // Codec supports non-IDR key pictures (that is, key pictures do
335 // not necessarily empty the DPB).
336 FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
339 typedef struct VAAPIEncodeType {
340 // List of supported profiles and corresponding VAAPI profiles.
341 // (Must end with FF_PROFILE_UNKNOWN.)
342 const VAAPIEncodeProfile *profiles;
344 // Codec feature flags.
347 // Default quality for this codec - used as quantiser or RC quality
348 // factor depending on RC mode.
351 // Perform any extra codec-specific configuration after the
352 // codec context is initialised (set up the private data and
353 // add any necessary global parameters).
354 int (*configure)(AVCodecContext *avctx);
356 // The size of any private data structure associated with each
357 // picture (can be zero if not required).
358 size_t picture_priv_data_size;
360 // The size of the parameter structures:
361 // sizeof(VAEnc{type}ParameterBuffer{codec}).
362 size_t sequence_params_size;
363 size_t picture_params_size;
364 size_t slice_params_size;
366 // Fill the parameter structures.
367 int (*init_sequence_params)(AVCodecContext *avctx);
368 int (*init_picture_params)(AVCodecContext *avctx,
369 VAAPIEncodePicture *pic);
370 int (*init_slice_params)(AVCodecContext *avctx,
371 VAAPIEncodePicture *pic,
372 VAAPIEncodeSlice *slice);
374 // The type used by the packed header: this should look like
375 // VAEncPackedHeader{something}.
376 int sequence_header_type;
377 int picture_header_type;
378 int slice_header_type;
380 // Write the packed header data to the provided buffer.
381 // The sequence header is also used to fill the codec extradata
382 // when the encoder is starting.
383 int (*write_sequence_header)(AVCodecContext *avctx,
384 char *data, size_t *data_len);
385 int (*write_picture_header)(AVCodecContext *avctx,
386 VAAPIEncodePicture *pic,
387 char *data, size_t *data_len);
388 int (*write_slice_header)(AVCodecContext *avctx,
389 VAAPIEncodePicture *pic,
390 VAAPIEncodeSlice *slice,
391 char *data, size_t *data_len);
393 // Fill an extra parameter structure, which will then be
394 // passed to vaRenderPicture(). Will be called repeatedly
395 // with increasing index argument until AVERROR_EOF is
397 int (*write_extra_buffer)(AVCodecContext *avctx,
398 VAAPIEncodePicture *pic,
399 int index, int *type,
400 char *data, size_t *data_len);
402 // Write an extra packed header. Will be called repeatedly
403 // with increasing index argument until AVERROR_EOF is
405 int (*write_extra_header)(AVCodecContext *avctx,
406 VAAPIEncodePicture *pic,
407 int index, int *type,
408 char *data, size_t *data_len);
412 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
413 const AVFrame *input_image, int *got_packet);
415 int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
416 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
418 int ff_vaapi_encode_init(AVCodecContext *avctx);
419 int ff_vaapi_encode_close(AVCodecContext *avctx);
422 #define VAAPI_ENCODE_COMMON_OPTIONS \
424 "Use low-power encoding mode (only available on some platforms; " \
425 "may not support all encoding features)", \
426 OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
427 { .i64 = 0 }, 0, 1, FLAGS }, \
429 "Distance (in I-frames) between IDR frames", \
430 OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
431 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
433 "Maximum B-frame reference depth", \
434 OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
435 { .i64 = 1 }, 1, INT_MAX, FLAGS }
437 #define VAAPI_ENCODE_RC_MODE(name, desc) \
438 { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
439 0, 0, FLAGS, "rc_mode" }
440 #define VAAPI_ENCODE_RC_OPTIONS \
442 "Set rate control mode", \
443 OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
444 { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \
445 { "auto", "Choose mode automatically based on other parameters", \
446 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \
447 VAAPI_ENCODE_RC_MODE(CQP, "Constant-quality"), \
448 VAAPI_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \
449 VAAPI_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \
450 VAAPI_ENCODE_RC_MODE(ICQ, "Intelligent constant-quality"), \
451 VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
452 VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
455 #endif /* AVCODEC_VAAPI_ENCODE_H */