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 #include "libavutil/hwcontext.h"
27 #include "libavutil/hwcontext_vaapi.h"
31 struct VAAPIEncodeType;
32 struct VAAPIEncodePicture;
35 MAX_CONFIG_ATTRIBUTES = 4,
36 MAX_GLOBAL_PARAMS = 4,
37 MAX_PICTURE_REFERENCES = 2,
38 MAX_REORDER_DELAY = 16,
39 MAX_PARAM_BUFFER_SIZE = 1024,
49 typedef struct VAAPIEncodeSlice {
52 void *codec_slice_params;
55 typedef struct VAAPIEncodePicture {
56 struct VAAPIEncodePicture *next;
58 int64_t display_order;
68 VASurfaceID input_surface;
71 VASurfaceID recon_surface;
74 VABufferID *param_buffers;
76 AVBufferRef *output_buffer_ref;
77 VABufferID output_buffer;
80 void *codec_picture_params;
83 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
86 VAAPIEncodeSlice *slices;
89 typedef struct VAAPIEncodeContext {
92 // Codec-specific hooks.
93 const struct VAAPIEncodeType *codec;
95 // Encoding profile (VAProfileXXX).
97 // Encoding entrypoint (usually VAEntryointEncSlice).
98 VAEntrypoint va_entrypoint;
99 // Surface colour/sampling format (usually VA_RT_FORMAT_YUV420).
100 unsigned int va_rt_format;
101 // Rate control mode.
102 unsigned int va_rc_mode;
103 // Supported packed headers (initially the desired set, modified
104 // later to what is actually supported).
105 unsigned int va_packed_headers;
107 // The required size of surfaces. This is probably the input
108 // size (AVCodecContext.width|height) aligned up to whatever
109 // block size is required by the codec.
113 // Everything above this point must be set before calling
114 // ff_vaapi_encode_init().
116 // Codec-specific state.
119 // Configuration attributes to use when creating va_config.
120 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
121 int nb_config_attributes;
123 VAConfigID va_config;
124 VAContextID va_context;
126 AVBufferRef *device_ref;
127 AVHWDeviceContext *device;
128 AVVAAPIDeviceContext *hwctx;
130 // The hardware frame context containing the input frames.
131 AVBufferRef *input_frames_ref;
132 AVHWFramesContext *input_frames;
134 // The hardware frame context containing the reconstructed frames.
135 AVBufferRef *recon_frames_ref;
136 AVHWFramesContext *recon_frames;
138 // Pool of (reusable) bitstream output buffers.
139 AVBufferPool *output_buffer_pool;
141 // Global parameters which will be applied at the start of the
142 // sequence (includes rate control parameters below).
143 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
144 size_t global_params_size[MAX_GLOBAL_PARAMS];
145 int nb_global_params;
147 // Rate control parameters.
149 VAEncMiscParameterBuffer misc;
150 VAEncMiscParameterRateControl rc;
153 VAEncMiscParameterBuffer misc;
154 VAEncMiscParameterHRD hrd;
157 VAEncMiscParameterBuffer misc;
158 VAEncMiscParameterFrameRate fr;
160 #if VA_CHECK_VERSION(0, 36, 0)
162 VAEncMiscParameterBuffer misc;
163 VAEncMiscParameterBufferQualityLevel quality;
167 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
168 void *codec_sequence_params;
170 // Per-sequence parameters found in the per-picture parameter
171 // structure (VAEncPictureParameterBuffer*).
172 void *codec_picture_params;
174 // Current encoding window, in display (input) order.
175 VAAPIEncodePicture *pic_start, *pic_end;
177 // Next input order index (display order).
179 // Number of frames that output is behind input.
180 int64_t output_delay;
181 // Number of frames decode output will need to be delayed.
182 int64_t decode_delay;
183 // Next output order index (encode order).
184 int64_t output_order;
187 // All encode operations are done independently (synchronise
188 // immediately after every operation).
189 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
190 // Overlap as many operations as possible.
191 ISSUE_MODE_MAXIMISE_THROUGHPUT,
192 // Overlap operations only when satisfying parallel dependencies.
193 ISSUE_MODE_MINIMISE_LATENCY,
196 // Timestamp handling.
198 int64_t dts_pts_diff;
199 int64_t ts_ring[MAX_REORDER_DELAY * 3];
201 // Frame type decision.
209 // Codec-local options are allocated to follow this structure in
210 // memory (in the AVCodec definition, set priv_data_size to
211 // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
213 char codec_options_data[0];
214 } VAAPIEncodeContext;
217 typedef struct VAAPIEncodeType {
218 size_t priv_data_size;
220 // Perform any extra codec-specific configuration after the
221 // codec context is initialised (set up the private data and
222 // add any necessary global parameters).
223 int (*configure)(AVCodecContext *avctx);
225 // The size of the parameter structures:
226 // sizeof(VAEnc{type}ParameterBuffer{codec}).
227 size_t sequence_params_size;
228 size_t picture_params_size;
229 size_t slice_params_size;
231 // Fill the parameter structures.
232 int (*init_sequence_params)(AVCodecContext *avctx);
233 int (*init_picture_params)(AVCodecContext *avctx,
234 VAAPIEncodePicture *pic);
235 int (*init_slice_params)(AVCodecContext *avctx,
236 VAAPIEncodePicture *pic,
237 VAAPIEncodeSlice *slice);
239 // The type used by the packed header: this should look like
240 // VAEncPackedHeader{something}.
241 int sequence_header_type;
242 int picture_header_type;
243 int slice_header_type;
245 // Write the packed header data to the provided buffer.
246 // The sequence header is also used to fill the codec extradata
247 // when the encoder is starting.
248 int (*write_sequence_header)(AVCodecContext *avctx,
249 char *data, size_t *data_len);
250 int (*write_picture_header)(AVCodecContext *avctx,
251 VAAPIEncodePicture *pic,
252 char *data, size_t *data_len);
253 int (*write_slice_header)(AVCodecContext *avctx,
254 VAAPIEncodePicture *pic,
255 VAAPIEncodeSlice *slice,
256 char *data, size_t *data_len);
258 // Fill an extra parameter structure, which will then be
259 // passed to vaRenderPicture(). Will be called repeatedly
260 // with increasing index argument until AVERROR_EOF is
262 int (*write_extra_buffer)(AVCodecContext *avctx,
263 VAAPIEncodePicture *pic,
264 int index, int *type,
265 char *data, size_t *data_len);
267 // Write an extra packed header. Will be called repeatedly
268 // with increasing index argument until AVERROR_EOF is
270 int (*write_extra_header)(AVCodecContext *avctx,
271 VAAPIEncodePicture *pic,
272 int index, int *type,
273 char *data, size_t *data_len);
277 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
278 const AVFrame *input_image, int *got_packet);
280 int ff_vaapi_encode_init(AVCodecContext *avctx);
281 int ff_vaapi_encode_close(AVCodecContext *avctx);
283 #endif /* AVCODEC_VAAPI_ENCODE_H */