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.
317 } VAAPIEncodeContext;
320 // Codec supports controlling the subdivision of pictures into slices.
321 FLAG_SLICE_CONTROL = 1 << 0,
322 // Codec only supports constant quality (no rate control).
323 FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
324 // Codec is intra-only.
325 FLAG_INTRA_ONLY = 1 << 2,
326 // Codec supports B-pictures.
327 FLAG_B_PICTURES = 1 << 3,
328 // Codec supports referencing B-pictures.
329 FLAG_B_PICTURE_REFERENCES = 1 << 4,
330 // Codec supports non-IDR key pictures (that is, key pictures do
331 // not necessarily empty the DPB).
332 FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
335 typedef struct VAAPIEncodeType {
336 // List of supported profiles and corresponding VAAPI profiles.
337 // (Must end with FF_PROFILE_UNKNOWN.)
338 const VAAPIEncodeProfile *profiles;
340 // Codec feature flags.
343 // Default quality for this codec - used as quantiser or RC quality
344 // factor depending on RC mode.
347 // Perform any extra codec-specific configuration after the
348 // codec context is initialised (set up the private data and
349 // add any necessary global parameters).
350 int (*configure)(AVCodecContext *avctx);
352 // The size of any private data structure associated with each
353 // picture (can be zero if not required).
354 size_t picture_priv_data_size;
356 // The size of the parameter structures:
357 // sizeof(VAEnc{type}ParameterBuffer{codec}).
358 size_t sequence_params_size;
359 size_t picture_params_size;
360 size_t slice_params_size;
362 // Fill the parameter structures.
363 int (*init_sequence_params)(AVCodecContext *avctx);
364 int (*init_picture_params)(AVCodecContext *avctx,
365 VAAPIEncodePicture *pic);
366 int (*init_slice_params)(AVCodecContext *avctx,
367 VAAPIEncodePicture *pic,
368 VAAPIEncodeSlice *slice);
370 // The type used by the packed header: this should look like
371 // VAEncPackedHeader{something}.
372 int sequence_header_type;
373 int picture_header_type;
374 int slice_header_type;
376 // Write the packed header data to the provided buffer.
377 // The sequence header is also used to fill the codec extradata
378 // when the encoder is starting.
379 int (*write_sequence_header)(AVCodecContext *avctx,
380 char *data, size_t *data_len);
381 int (*write_picture_header)(AVCodecContext *avctx,
382 VAAPIEncodePicture *pic,
383 char *data, size_t *data_len);
384 int (*write_slice_header)(AVCodecContext *avctx,
385 VAAPIEncodePicture *pic,
386 VAAPIEncodeSlice *slice,
387 char *data, size_t *data_len);
389 // Fill an extra parameter structure, which will then be
390 // passed to vaRenderPicture(). Will be called repeatedly
391 // with increasing index argument until AVERROR_EOF is
393 int (*write_extra_buffer)(AVCodecContext *avctx,
394 VAAPIEncodePicture *pic,
395 int index, int *type,
396 char *data, size_t *data_len);
398 // Write an extra packed header. Will be called repeatedly
399 // with increasing index argument until AVERROR_EOF is
401 int (*write_extra_header)(AVCodecContext *avctx,
402 VAAPIEncodePicture *pic,
403 int index, int *type,
404 char *data, size_t *data_len);
408 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
409 const AVFrame *input_image, int *got_packet);
411 int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
412 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
414 int ff_vaapi_encode_init(AVCodecContext *avctx);
415 int ff_vaapi_encode_close(AVCodecContext *avctx);
418 #define VAAPI_ENCODE_COMMON_OPTIONS \
420 "Use low-power encoding mode (only available on some platforms; " \
421 "may not support all encoding features)", \
422 OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
423 { .i64 = 0 }, 0, 1, FLAGS }, \
425 "Distance (in I-frames) between IDR frames", \
426 OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
427 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
429 "Maximum B-frame reference depth", \
430 OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
431 { .i64 = 1 }, 1, INT_MAX, FLAGS }
433 #define VAAPI_ENCODE_RC_MODE(name, desc) \
434 { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
435 0, 0, FLAGS, "rc_mode" }
436 #define VAAPI_ENCODE_RC_OPTIONS \
438 "Set rate control mode", \
439 OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
440 { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \
441 { "auto", "Choose mode automatically based on other parameters", \
442 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \
443 VAAPI_ENCODE_RC_MODE(CQP, "Constant-quality"), \
444 VAAPI_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \
445 VAAPI_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \
446 VAAPI_ENCODE_RC_MODE(ICQ, "Intelligent constant-quality"), \
447 VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
448 VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
451 #endif /* AVCODEC_VAAPI_ENCODE_H */