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;
72 #if VA_CHECK_VERSION(1, 0, 0)
85 VASurfaceID input_surface;
88 VASurfaceID recon_surface;
91 VABufferID *param_buffers;
93 AVBufferRef *output_buffer_ref;
94 VABufferID output_buffer;
97 void *codec_picture_params;
99 // Whether this picture is a reference picture.
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.
106 struct VAAPIEncodePicture *dpb[MAX_DPB_SIZE];
107 // The reference pictures used in decoding this picture. If they are
108 // used by later pictures they will also appear in the DPB.
110 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
111 // The previous reference picture in encode order. Must be in at least
112 // one of the reference list and DPB list.
113 struct VAAPIEncodePicture *prev;
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.
121 VAAPIEncodeSlice *slices;
122 } VAAPIEncodePicture;
124 typedef struct VAAPIEncodeProfile {
125 // lavc profile value (FF_PROFILE_*).
127 // Supported bit 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;
137 } VAAPIEncodeProfile;
147 RC_MODE_MAX = RC_MODE_AVBR,
150 typedef struct VAAPIEncodeRCMode {
151 // Mode from above enum (RC_MODE_*).
155 // Supported in the compile-time VAAPI version.
157 // VA mode value (VA_RC_*).
159 // Uses bitrate parameters.
161 // Supports maxrate distinct from bitrate.
163 // Uses quality value.
165 // Supports HRD/VBV parameters.
169 typedef struct VAAPIEncodeContext {
170 const AVClass *class;
172 // Codec-specific hooks.
173 const struct VAAPIEncodeType *codec;
177 // Use low power encoding mode.
180 // Number of I frames between IDR frames.
183 // Desired B frame reference depth.
186 // Explicitly set RC mode (otherwise attempt to pick from
188 int explicit_rc_mode;
190 // Explicitly-set QP, for use with the "qp" options.
191 // (Forces CQP mode when set, overriding everything else.)
194 // Desired packed headers.
195 unsigned int desired_packed_headers;
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.
203 // The block size for slice calculations.
204 int slice_block_width;
205 int slice_block_height;
207 // Everything above this point must be set before calling
208 // ff_vaapi_encode_init().
210 // Chosen encoding profile details.
211 const VAAPIEncodeProfile *profile;
213 // Chosen rate control mode details.
214 const VAAPIEncodeRCMode *rc_mode;
215 // RC quality level - meaning depends on codec and RC mode.
216 // In CQP mode this sets the fixed quantiser value.
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;
230 // Configuration attributes to use when creating va_config.
231 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
232 int nb_config_attributes;
234 VAConfigID va_config;
235 VAContextID va_context;
237 AVBufferRef *device_ref;
238 AVHWDeviceContext *device;
239 AVVAAPIDeviceContext *hwctx;
241 // The hardware frame context containing the input frames.
242 AVBufferRef *input_frames_ref;
243 AVHWFramesContext *input_frames;
245 // The hardware frame context containing the reconstructed frames.
246 AVBufferRef *recon_frames_ref;
247 AVHWFramesContext *recon_frames;
249 // Pool of (reusable) bitstream output buffers.
250 AVBufferPool *output_buffer_pool;
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];
257 int nb_global_params;
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;
267 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
268 void *codec_sequence_params;
270 // Per-sequence parameters found in the per-picture parameter
271 // structure (VAEncPictureParameterBuffer*).
272 void *codec_picture_params;
274 // Current encoding window, in display (input) order.
275 VAAPIEncodePicture *pic_start, *pic_end;
276 // The next picture to use as the previous reference picture in
278 VAAPIEncodePicture *next_prev;
280 // Next input order index (display 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;
291 // Timestamp handling.
293 int64_t dts_pts_diff;
294 int64_t ts_ring[MAX_REORDER_DELAY * 3];
297 int slice_block_rows;
298 int slice_block_cols;
302 // Frame type decision.
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.
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.
328 } VAAPIEncodeContext;
331 // Codec supports controlling the subdivision of pictures into slices.
332 FLAG_SLICE_CONTROL = 1 << 0,
333 // Codec only supports constant quality (no rate control).
334 FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
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.
340 FLAG_B_PICTURE_REFERENCES = 1 << 4,
341 // Codec supports non-IDR key pictures (that is, key pictures do
342 // not necessarily empty the DPB).
343 FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
346 typedef struct VAAPIEncodeType {
347 // List of supported profiles and corresponding VAAPI profiles.
348 // (Must end with FF_PROFILE_UNKNOWN.)
349 const VAAPIEncodeProfile *profiles;
351 // Codec feature flags.
354 // Default quality for this codec - used as quantiser or RC quality
355 // factor depending on RC mode.
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);
363 // The size of any private data structure associated with each
364 // picture (can be zero if not required).
365 size_t picture_priv_data_size;
367 // The size of the parameter structures:
368 // sizeof(VAEnc{type}ParameterBuffer{codec}).
369 size_t sequence_params_size;
370 size_t picture_params_size;
371 size_t slice_params_size;
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);
381 // The type used by the packed header: this should look like
382 // VAEncPackedHeader{something}.
383 int sequence_header_type;
384 int picture_header_type;
385 int slice_header_type;
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);
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
404 int (*write_extra_buffer)(AVCodecContext *avctx,
405 VAAPIEncodePicture *pic,
406 int index, int *type,
407 char *data, size_t *data_len);
409 // Write an extra packed header. Will be called repeatedly
410 // with increasing index argument until AVERROR_EOF is
412 int (*write_extra_header)(AVCodecContext *avctx,
413 VAAPIEncodePicture *pic,
414 int index, int *type,
415 char *data, size_t *data_len);
419 int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
420 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
422 int ff_vaapi_encode_init(AVCodecContext *avctx);
423 int ff_vaapi_encode_close(AVCodecContext *avctx);
426 #define VAAPI_ENCODE_COMMON_OPTIONS \
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 }, \
433 "Distance (in I-frames) between IDR frames", \
434 OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
435 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
437 "Maximum B-frame reference depth", \
438 OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
439 { .i64 = 1 }, 1, INT_MAX, FLAGS }
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 \
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")
459 #endif /* AVCODEC_VAAPI_ENCODE_H */