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 {
56 void *codec_slice_params;
59 typedef struct VAAPIEncodePicture {
60 struct VAAPIEncodePicture *next;
62 int64_t display_order;
72 VASurfaceID input_surface;
75 VASurfaceID recon_surface;
78 VABufferID *param_buffers;
80 AVBufferRef *output_buffer_ref;
81 VABufferID output_buffer;
84 void *codec_picture_params;
87 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
90 VAAPIEncodeSlice *slices;
93 typedef struct VAAPIEncodeProfile {
94 // lavc profile value (FF_PROFILE_*).
96 // Supported bit depth.
98 // Number of components.
100 // Chroma subsampling in width dimension.
102 // Chroma subsampling in height dimension.
104 // VAAPI profile value.
105 VAProfile va_profile;
106 } VAAPIEncodeProfile;
108 typedef struct VAAPIEncodeContext {
109 const AVClass *class;
111 // Codec-specific hooks.
112 const struct VAAPIEncodeType *codec;
116 // Use low power encoding mode.
119 // Supported packed headers (initially the desired set, modified
120 // later to what is actually supported).
121 unsigned int va_packed_headers;
123 // The required size of surfaces. This is probably the input
124 // size (AVCodecContext.width|height) aligned up to whatever
125 // block size is required by the codec.
129 // Everything above this point must be set before calling
130 // ff_vaapi_encode_init().
132 // Chosen encoding profile details.
133 const VAAPIEncodeProfile *profile;
135 // Encoding profile (VAProfile*).
136 VAProfile va_profile;
137 // Encoding entrypoint (VAEntryoint*).
138 VAEntrypoint va_entrypoint;
139 // Rate control mode.
140 unsigned int va_rc_mode;
141 // Bitrate for codec-specific encoder parameters.
142 unsigned int va_bit_rate;
144 // Configuration attributes to use when creating va_config.
145 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
146 int nb_config_attributes;
148 VAConfigID va_config;
149 VAContextID va_context;
151 AVBufferRef *device_ref;
152 AVHWDeviceContext *device;
153 AVVAAPIDeviceContext *hwctx;
155 // The hardware frame context containing the input frames.
156 AVBufferRef *input_frames_ref;
157 AVHWFramesContext *input_frames;
159 // The hardware frame context containing the reconstructed frames.
160 AVBufferRef *recon_frames_ref;
161 AVHWFramesContext *recon_frames;
163 // Pool of (reusable) bitstream output buffers.
164 AVBufferPool *output_buffer_pool;
166 // Global parameters which will be applied at the start of the
167 // sequence (includes rate control parameters below).
168 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
169 size_t global_params_size[MAX_GLOBAL_PARAMS];
170 int nb_global_params;
172 // Rate control parameters.
174 VAEncMiscParameterBuffer misc;
175 VAEncMiscParameterRateControl rc;
178 VAEncMiscParameterBuffer misc;
179 VAEncMiscParameterHRD hrd;
182 VAEncMiscParameterBuffer misc;
183 VAEncMiscParameterFrameRate fr;
185 #if VA_CHECK_VERSION(0, 36, 0)
187 VAEncMiscParameterBuffer misc;
188 VAEncMiscParameterBufferQualityLevel quality;
192 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
193 void *codec_sequence_params;
195 // Per-sequence parameters found in the per-picture parameter
196 // structure (VAEncPictureParameterBuffer*).
197 void *codec_picture_params;
199 // Current encoding window, in display (input) order.
200 VAAPIEncodePicture *pic_start, *pic_end;
202 // Next input order index (display order).
204 // Number of frames that output is behind input.
205 int64_t output_delay;
206 // Number of frames decode output will need to be delayed.
207 int64_t decode_delay;
208 // Next output order index (encode order).
209 int64_t output_order;
212 // All encode operations are done independently (synchronise
213 // immediately after every operation).
214 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
215 // Overlap as many operations as possible.
216 ISSUE_MODE_MAXIMISE_THROUGHPUT,
217 // Overlap operations only when satisfying parallel dependencies.
218 ISSUE_MODE_MINIMISE_LATENCY,
221 // Timestamp handling.
223 int64_t dts_pts_diff;
224 int64_t ts_ring[MAX_REORDER_DELAY * 3];
226 // Frame type decision.
234 } VAAPIEncodeContext;
236 typedef struct VAAPIEncodeType {
237 // List of supported profiles and corresponding VAAPI profiles.
238 // (Must end with FF_PROFILE_UNKNOWN.)
239 const VAAPIEncodeProfile *profiles;
241 // Perform any extra codec-specific configuration after the
242 // codec context is initialised (set up the private data and
243 // add any necessary global parameters).
244 int (*configure)(AVCodecContext *avctx);
246 // The size of the parameter structures:
247 // sizeof(VAEnc{type}ParameterBuffer{codec}).
248 size_t sequence_params_size;
249 size_t picture_params_size;
250 size_t slice_params_size;
252 // Fill the parameter structures.
253 int (*init_sequence_params)(AVCodecContext *avctx);
254 int (*init_picture_params)(AVCodecContext *avctx,
255 VAAPIEncodePicture *pic);
256 int (*init_slice_params)(AVCodecContext *avctx,
257 VAAPIEncodePicture *pic,
258 VAAPIEncodeSlice *slice);
260 // The type used by the packed header: this should look like
261 // VAEncPackedHeader{something}.
262 int sequence_header_type;
263 int picture_header_type;
264 int slice_header_type;
266 // Write the packed header data to the provided buffer.
267 // The sequence header is also used to fill the codec extradata
268 // when the encoder is starting.
269 int (*write_sequence_header)(AVCodecContext *avctx,
270 char *data, size_t *data_len);
271 int (*write_picture_header)(AVCodecContext *avctx,
272 VAAPIEncodePicture *pic,
273 char *data, size_t *data_len);
274 int (*write_slice_header)(AVCodecContext *avctx,
275 VAAPIEncodePicture *pic,
276 VAAPIEncodeSlice *slice,
277 char *data, size_t *data_len);
279 // Fill an extra parameter structure, which will then be
280 // passed to vaRenderPicture(). Will be called repeatedly
281 // with increasing index argument until AVERROR_EOF is
283 int (*write_extra_buffer)(AVCodecContext *avctx,
284 VAAPIEncodePicture *pic,
285 int index, int *type,
286 char *data, size_t *data_len);
288 // Write an extra packed header. Will be called repeatedly
289 // with increasing index argument until AVERROR_EOF is
291 int (*write_extra_header)(AVCodecContext *avctx,
292 VAAPIEncodePicture *pic,
293 int index, int *type,
294 char *data, size_t *data_len);
298 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
299 const AVFrame *input_image, int *got_packet);
301 int ff_vaapi_encode_init(AVCodecContext *avctx);
302 int ff_vaapi_encode_close(AVCodecContext *avctx);
305 #define VAAPI_ENCODE_COMMON_OPTIONS \
307 "Use low-power encoding mode (only available on some platforms; " \
308 "may not support all encoding features)", \
309 OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
310 { .i64 = 0 }, 0, 1, FLAGS }
313 #endif /* AVCODEC_VAAPI_ENCODE_H */