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 // Rate control mode.
120 unsigned int va_rc_mode;
121 // Supported packed headers (initially the desired set, modified
122 // later to what is actually supported).
123 unsigned int va_packed_headers;
125 // The required size of surfaces. This is probably the input
126 // size (AVCodecContext.width|height) aligned up to whatever
127 // block size is required by the codec.
131 // Everything above this point must be set before calling
132 // ff_vaapi_encode_init().
134 // Chosen encoding profile details.
135 const VAAPIEncodeProfile *profile;
137 // Encoding profile (VAProfile*).
138 VAProfile va_profile;
139 // Encoding entrypoint (VAEntryoint*).
140 VAEntrypoint va_entrypoint;
142 // Configuration attributes to use when creating va_config.
143 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
144 int nb_config_attributes;
146 VAConfigID va_config;
147 VAContextID va_context;
149 AVBufferRef *device_ref;
150 AVHWDeviceContext *device;
151 AVVAAPIDeviceContext *hwctx;
153 // The hardware frame context containing the input frames.
154 AVBufferRef *input_frames_ref;
155 AVHWFramesContext *input_frames;
157 // The hardware frame context containing the reconstructed frames.
158 AVBufferRef *recon_frames_ref;
159 AVHWFramesContext *recon_frames;
161 // Pool of (reusable) bitstream output buffers.
162 AVBufferPool *output_buffer_pool;
164 // Global parameters which will be applied at the start of the
165 // sequence (includes rate control parameters below).
166 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
167 size_t global_params_size[MAX_GLOBAL_PARAMS];
168 int nb_global_params;
170 // Rate control parameters.
172 VAEncMiscParameterBuffer misc;
173 VAEncMiscParameterRateControl rc;
176 VAEncMiscParameterBuffer misc;
177 VAEncMiscParameterHRD hrd;
180 VAEncMiscParameterBuffer misc;
181 VAEncMiscParameterFrameRate fr;
183 #if VA_CHECK_VERSION(0, 36, 0)
185 VAEncMiscParameterBuffer misc;
186 VAEncMiscParameterBufferQualityLevel quality;
190 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
191 void *codec_sequence_params;
193 // Per-sequence parameters found in the per-picture parameter
194 // structure (VAEncPictureParameterBuffer*).
195 void *codec_picture_params;
197 // Current encoding window, in display (input) order.
198 VAAPIEncodePicture *pic_start, *pic_end;
200 // Next input order index (display order).
202 // Number of frames that output is behind input.
203 int64_t output_delay;
204 // Number of frames decode output will need to be delayed.
205 int64_t decode_delay;
206 // Next output order index (encode order).
207 int64_t output_order;
210 // All encode operations are done independently (synchronise
211 // immediately after every operation).
212 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
213 // Overlap as many operations as possible.
214 ISSUE_MODE_MAXIMISE_THROUGHPUT,
215 // Overlap operations only when satisfying parallel dependencies.
216 ISSUE_MODE_MINIMISE_LATENCY,
219 // Timestamp handling.
221 int64_t dts_pts_diff;
222 int64_t ts_ring[MAX_REORDER_DELAY * 3];
224 // Frame type decision.
231 } VAAPIEncodeContext;
233 typedef struct VAAPIEncodeType {
234 // List of supported profiles and corresponding VAAPI profiles.
235 // (Must end with FF_PROFILE_UNKNOWN.)
236 const VAAPIEncodeProfile *profiles;
238 // Perform any extra codec-specific configuration after the
239 // codec context is initialised (set up the private data and
240 // add any necessary global parameters).
241 int (*configure)(AVCodecContext *avctx);
243 // The size of the parameter structures:
244 // sizeof(VAEnc{type}ParameterBuffer{codec}).
245 size_t sequence_params_size;
246 size_t picture_params_size;
247 size_t slice_params_size;
249 // Fill the parameter structures.
250 int (*init_sequence_params)(AVCodecContext *avctx);
251 int (*init_picture_params)(AVCodecContext *avctx,
252 VAAPIEncodePicture *pic);
253 int (*init_slice_params)(AVCodecContext *avctx,
254 VAAPIEncodePicture *pic,
255 VAAPIEncodeSlice *slice);
257 // The type used by the packed header: this should look like
258 // VAEncPackedHeader{something}.
259 int sequence_header_type;
260 int picture_header_type;
261 int slice_header_type;
263 // Write the packed header data to the provided buffer.
264 // The sequence header is also used to fill the codec extradata
265 // when the encoder is starting.
266 int (*write_sequence_header)(AVCodecContext *avctx,
267 char *data, size_t *data_len);
268 int (*write_picture_header)(AVCodecContext *avctx,
269 VAAPIEncodePicture *pic,
270 char *data, size_t *data_len);
271 int (*write_slice_header)(AVCodecContext *avctx,
272 VAAPIEncodePicture *pic,
273 VAAPIEncodeSlice *slice,
274 char *data, size_t *data_len);
276 // Fill an extra parameter structure, which will then be
277 // passed to vaRenderPicture(). Will be called repeatedly
278 // with increasing index argument until AVERROR_EOF is
280 int (*write_extra_buffer)(AVCodecContext *avctx,
281 VAAPIEncodePicture *pic,
282 int index, int *type,
283 char *data, size_t *data_len);
285 // Write an extra packed header. Will be called repeatedly
286 // with increasing index argument until AVERROR_EOF is
288 int (*write_extra_header)(AVCodecContext *avctx,
289 VAAPIEncodePicture *pic,
290 int index, int *type,
291 char *data, size_t *data_len);
295 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
296 const AVFrame *input_image, int *got_packet);
298 int ff_vaapi_encode_init(AVCodecContext *avctx);
299 int ff_vaapi_encode_close(AVCodecContext *avctx);
301 #endif /* AVCODEC_VAAPI_ENCODE_H */