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;
132 typedef struct VAAPIEncodeContext {
133 const AVClass *class;
135 // Codec-specific hooks.
136 const struct VAAPIEncodeType *codec;
140 // Use low power encoding mode.
143 // Number of I frames between IDR frames.
146 // Desired B frame reference depth.
149 // Desired packed headers.
150 unsigned int desired_packed_headers;
152 // The required size of surfaces. This is probably the input
153 // size (AVCodecContext.width|height) aligned up to whatever
154 // block size is required by the codec.
158 // The block size for slice calculations.
159 int slice_block_width;
160 int slice_block_height;
162 // Everything above this point must be set before calling
163 // ff_vaapi_encode_init().
165 // Chosen encoding profile details.
166 const VAAPIEncodeProfile *profile;
168 // Encoding profile (VAProfile*).
169 VAProfile va_profile;
170 // Encoding entrypoint (VAEntryoint*).
171 VAEntrypoint va_entrypoint;
172 // Rate control mode.
173 unsigned int va_rc_mode;
174 // Bitrate for codec-specific encoder parameters.
175 unsigned int va_bit_rate;
176 // Packed headers which will actually be sent.
177 unsigned int va_packed_headers;
179 // Configuration attributes to use when creating va_config.
180 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
181 int nb_config_attributes;
183 VAConfigID va_config;
184 VAContextID va_context;
186 AVBufferRef *device_ref;
187 AVHWDeviceContext *device;
188 AVVAAPIDeviceContext *hwctx;
190 // The hardware frame context containing the input frames.
191 AVBufferRef *input_frames_ref;
192 AVHWFramesContext *input_frames;
194 // The hardware frame context containing the reconstructed frames.
195 AVBufferRef *recon_frames_ref;
196 AVHWFramesContext *recon_frames;
198 // Pool of (reusable) bitstream output buffers.
199 AVBufferPool *output_buffer_pool;
201 // Global parameters which will be applied at the start of the
202 // sequence (includes rate control parameters below).
203 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
204 size_t global_params_size[MAX_GLOBAL_PARAMS];
205 int nb_global_params;
207 // Rate control parameters.
209 VAEncMiscParameterBuffer misc;
210 VAEncMiscParameterRateControl rc;
213 VAEncMiscParameterBuffer misc;
214 VAEncMiscParameterHRD hrd;
217 VAEncMiscParameterBuffer misc;
218 VAEncMiscParameterFrameRate fr;
220 #if VA_CHECK_VERSION(0, 36, 0)
222 VAEncMiscParameterBuffer misc;
223 VAEncMiscParameterBufferQualityLevel quality;
227 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
228 void *codec_sequence_params;
230 // Per-sequence parameters found in the per-picture parameter
231 // structure (VAEncPictureParameterBuffer*).
232 void *codec_picture_params;
234 // Current encoding window, in display (input) order.
235 VAAPIEncodePicture *pic_start, *pic_end;
236 // The next picture to use as the previous reference picture in
238 VAAPIEncodePicture *next_prev;
240 // Next input order index (display order).
242 // Number of frames that output is behind input.
243 int64_t output_delay;
244 // Next encode order index.
245 int64_t encode_order;
246 // Number of frames decode output will need to be delayed.
247 int64_t decode_delay;
248 // Next output order index (in encode order).
249 int64_t output_order;
251 // Timestamp handling.
253 int64_t dts_pts_diff;
254 int64_t ts_ring[MAX_REORDER_DELAY * 3];
257 int slice_block_rows;
258 int slice_block_cols;
262 // Frame type decision.
273 } VAAPIEncodeContext;
276 // Codec supports controlling the subdivision of pictures into slices.
277 FLAG_SLICE_CONTROL = 1 << 0,
278 // Codec only supports constant quality (no rate control).
279 FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
280 // Codec is intra-only.
281 FLAG_INTRA_ONLY = 1 << 2,
282 // Codec supports B-pictures.
283 FLAG_B_PICTURES = 1 << 3,
284 // Codec supports referencing B-pictures.
285 FLAG_B_PICTURE_REFERENCES = 1 << 4,
286 // Codec supports non-IDR key pictures (that is, key pictures do
287 // not necessarily empty the DPB).
288 FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
291 typedef struct VAAPIEncodeType {
292 // List of supported profiles and corresponding VAAPI profiles.
293 // (Must end with FF_PROFILE_UNKNOWN.)
294 const VAAPIEncodeProfile *profiles;
296 // Codec feature flags.
299 // Perform any extra codec-specific configuration after the
300 // codec context is initialised (set up the private data and
301 // add any necessary global parameters).
302 int (*configure)(AVCodecContext *avctx);
304 // The size of any private data structure associated with each
305 // picture (can be zero if not required).
306 size_t picture_priv_data_size;
308 // The size of the parameter structures:
309 // sizeof(VAEnc{type}ParameterBuffer{codec}).
310 size_t sequence_params_size;
311 size_t picture_params_size;
312 size_t slice_params_size;
314 // Fill the parameter structures.
315 int (*init_sequence_params)(AVCodecContext *avctx);
316 int (*init_picture_params)(AVCodecContext *avctx,
317 VAAPIEncodePicture *pic);
318 int (*init_slice_params)(AVCodecContext *avctx,
319 VAAPIEncodePicture *pic,
320 VAAPIEncodeSlice *slice);
322 // The type used by the packed header: this should look like
323 // VAEncPackedHeader{something}.
324 int sequence_header_type;
325 int picture_header_type;
326 int slice_header_type;
328 // Write the packed header data to the provided buffer.
329 // The sequence header is also used to fill the codec extradata
330 // when the encoder is starting.
331 int (*write_sequence_header)(AVCodecContext *avctx,
332 char *data, size_t *data_len);
333 int (*write_picture_header)(AVCodecContext *avctx,
334 VAAPIEncodePicture *pic,
335 char *data, size_t *data_len);
336 int (*write_slice_header)(AVCodecContext *avctx,
337 VAAPIEncodePicture *pic,
338 VAAPIEncodeSlice *slice,
339 char *data, size_t *data_len);
341 // Fill an extra parameter structure, which will then be
342 // passed to vaRenderPicture(). Will be called repeatedly
343 // with increasing index argument until AVERROR_EOF is
345 int (*write_extra_buffer)(AVCodecContext *avctx,
346 VAAPIEncodePicture *pic,
347 int index, int *type,
348 char *data, size_t *data_len);
350 // Write an extra packed header. Will be called repeatedly
351 // with increasing index argument until AVERROR_EOF is
353 int (*write_extra_header)(AVCodecContext *avctx,
354 VAAPIEncodePicture *pic,
355 int index, int *type,
356 char *data, size_t *data_len);
360 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
361 const AVFrame *input_image, int *got_packet);
363 int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
364 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
366 int ff_vaapi_encode_init(AVCodecContext *avctx);
367 int ff_vaapi_encode_close(AVCodecContext *avctx);
370 #define VAAPI_ENCODE_COMMON_OPTIONS \
372 "Use low-power encoding mode (only available on some platforms; " \
373 "may not support all encoding features)", \
374 OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
375 { .i64 = 0 }, 0, 1, FLAGS }, \
377 "Distance (in I-frames) between IDR frames", \
378 OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
379 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
381 "Maximum B-frame reference depth", \
382 OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
383 { .i64 = 1 }, 1, INT_MAX, FLAGS }
386 #endif /* AVCODEC_VAAPI_ENCODE_H */