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,
41 MAX_PICTURE_REFERENCES = 2,
42 MAX_REORDER_DELAY = 16,
43 MAX_PARAM_BUFFER_SIZE = 1024,
53 typedef struct VAAPIEncodeSlice {
60 void *codec_slice_params;
63 typedef struct VAAPIEncodePicture {
64 struct VAAPIEncodePicture *next;
66 int64_t display_order;
76 VASurfaceID input_surface;
79 VASurfaceID recon_surface;
82 VABufferID *param_buffers;
84 AVBufferRef *output_buffer_ref;
85 VABufferID output_buffer;
88 void *codec_picture_params;
91 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
94 VAAPIEncodeSlice *slices;
97 typedef struct VAAPIEncodeProfile {
98 // lavc profile value (FF_PROFILE_*).
100 // Supported bit depth.
102 // Number of components.
104 // Chroma subsampling in width dimension.
106 // Chroma subsampling in height dimension.
108 // VAAPI profile value.
109 VAProfile va_profile;
110 } VAAPIEncodeProfile;
112 typedef struct VAAPIEncodeContext {
113 const AVClass *class;
115 // Codec-specific hooks.
116 const struct VAAPIEncodeType *codec;
120 // Use low power encoding mode.
123 // Desired packed headers.
124 unsigned int desired_packed_headers;
126 // The required size of surfaces. This is probably the input
127 // size (AVCodecContext.width|height) aligned up to whatever
128 // block size is required by the codec.
132 // The block size for slice calculations.
133 int slice_block_width;
134 int slice_block_height;
136 // Everything above this point must be set before calling
137 // ff_vaapi_encode_init().
139 // Chosen encoding profile details.
140 const VAAPIEncodeProfile *profile;
142 // Encoding profile (VAProfile*).
143 VAProfile va_profile;
144 // Encoding entrypoint (VAEntryoint*).
145 VAEntrypoint va_entrypoint;
146 // Rate control mode.
147 unsigned int va_rc_mode;
148 // Bitrate for codec-specific encoder parameters.
149 unsigned int va_bit_rate;
150 // Packed headers which will actually be sent.
151 unsigned int va_packed_headers;
153 // Configuration attributes to use when creating va_config.
154 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
155 int nb_config_attributes;
157 VAConfigID va_config;
158 VAContextID va_context;
160 AVBufferRef *device_ref;
161 AVHWDeviceContext *device;
162 AVVAAPIDeviceContext *hwctx;
164 // The hardware frame context containing the input frames.
165 AVBufferRef *input_frames_ref;
166 AVHWFramesContext *input_frames;
168 // The hardware frame context containing the reconstructed frames.
169 AVBufferRef *recon_frames_ref;
170 AVHWFramesContext *recon_frames;
172 // Pool of (reusable) bitstream output buffers.
173 AVBufferPool *output_buffer_pool;
175 // Global parameters which will be applied at the start of the
176 // sequence (includes rate control parameters below).
177 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
178 size_t global_params_size[MAX_GLOBAL_PARAMS];
179 int nb_global_params;
181 // Rate control parameters.
183 VAEncMiscParameterBuffer misc;
184 VAEncMiscParameterRateControl rc;
187 VAEncMiscParameterBuffer misc;
188 VAEncMiscParameterHRD hrd;
191 VAEncMiscParameterBuffer misc;
192 VAEncMiscParameterFrameRate fr;
194 #if VA_CHECK_VERSION(0, 36, 0)
196 VAEncMiscParameterBuffer misc;
197 VAEncMiscParameterBufferQualityLevel quality;
201 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
202 void *codec_sequence_params;
204 // Per-sequence parameters found in the per-picture parameter
205 // structure (VAEncPictureParameterBuffer*).
206 void *codec_picture_params;
208 // Current encoding window, in display (input) order.
209 VAAPIEncodePicture *pic_start, *pic_end;
211 // Next input order index (display order).
213 // Number of frames that output is behind input.
214 int64_t output_delay;
215 // Number of frames decode output will need to be delayed.
216 int64_t decode_delay;
217 // Next output order index (encode order).
218 int64_t output_order;
221 // All encode operations are done independently (synchronise
222 // immediately after every operation).
223 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
224 // Overlap as many operations as possible.
225 ISSUE_MODE_MAXIMISE_THROUGHPUT,
226 // Overlap operations only when satisfying parallel dependencies.
227 ISSUE_MODE_MINIMISE_LATENCY,
230 // Timestamp handling.
232 int64_t dts_pts_diff;
233 int64_t ts_ring[MAX_REORDER_DELAY * 3];
236 int slice_block_rows;
237 int slice_block_cols;
241 // Frame type decision.
249 } VAAPIEncodeContext;
252 // Codec supports controlling the subdivision of pictures into slices.
253 FLAG_SLICE_CONTROL = 1 << 0,
254 // Codec only supports constant quality (no rate control).
255 FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
258 typedef struct VAAPIEncodeType {
259 // List of supported profiles and corresponding VAAPI profiles.
260 // (Must end with FF_PROFILE_UNKNOWN.)
261 const VAAPIEncodeProfile *profiles;
263 // Codec feature flags.
266 // Perform any extra codec-specific configuration after the
267 // codec context is initialised (set up the private data and
268 // add any necessary global parameters).
269 int (*configure)(AVCodecContext *avctx);
271 // The size of the parameter structures:
272 // sizeof(VAEnc{type}ParameterBuffer{codec}).
273 size_t sequence_params_size;
274 size_t picture_params_size;
275 size_t slice_params_size;
277 // Fill the parameter structures.
278 int (*init_sequence_params)(AVCodecContext *avctx);
279 int (*init_picture_params)(AVCodecContext *avctx,
280 VAAPIEncodePicture *pic);
281 int (*init_slice_params)(AVCodecContext *avctx,
282 VAAPIEncodePicture *pic,
283 VAAPIEncodeSlice *slice);
285 // The type used by the packed header: this should look like
286 // VAEncPackedHeader{something}.
287 int sequence_header_type;
288 int picture_header_type;
289 int slice_header_type;
291 // Write the packed header data to the provided buffer.
292 // The sequence header is also used to fill the codec extradata
293 // when the encoder is starting.
294 int (*write_sequence_header)(AVCodecContext *avctx,
295 char *data, size_t *data_len);
296 int (*write_picture_header)(AVCodecContext *avctx,
297 VAAPIEncodePicture *pic,
298 char *data, size_t *data_len);
299 int (*write_slice_header)(AVCodecContext *avctx,
300 VAAPIEncodePicture *pic,
301 VAAPIEncodeSlice *slice,
302 char *data, size_t *data_len);
304 // Fill an extra parameter structure, which will then be
305 // passed to vaRenderPicture(). Will be called repeatedly
306 // with increasing index argument until AVERROR_EOF is
308 int (*write_extra_buffer)(AVCodecContext *avctx,
309 VAAPIEncodePicture *pic,
310 int index, int *type,
311 char *data, size_t *data_len);
313 // Write an extra packed header. Will be called repeatedly
314 // with increasing index argument until AVERROR_EOF is
316 int (*write_extra_header)(AVCodecContext *avctx,
317 VAAPIEncodePicture *pic,
318 int index, int *type,
319 char *data, size_t *data_len);
323 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
324 const AVFrame *input_image, int *got_packet);
326 int ff_vaapi_encode_init(AVCodecContext *avctx);
327 int ff_vaapi_encode_close(AVCodecContext *avctx);
330 #define VAAPI_ENCODE_COMMON_OPTIONS \
332 "Use low-power encoding mode (only available on some platforms; " \
333 "may not support all encoding features)", \
334 OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
335 { .i64 = 0 }, 0, 1, FLAGS }
338 #endif /* AVCODEC_VAAPI_ENCODE_H */