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