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 // Configuration attributes to use when creating va_config.
117 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
118 int nb_config_attributes;
120 VAConfigID va_config;
121 VAContextID va_context;
123 AVBufferRef *device_ref;
124 AVHWDeviceContext *device;
125 AVVAAPIDeviceContext *hwctx;
127 // The hardware frame context containing the input frames.
128 AVBufferRef *input_frames_ref;
129 AVHWFramesContext *input_frames;
131 // The hardware frame context containing the reconstructed frames.
132 AVBufferRef *recon_frames_ref;
133 AVHWFramesContext *recon_frames;
135 // Pool of (reusable) bitstream output buffers.
136 AVBufferPool *output_buffer_pool;
138 // Global parameters which will be applied at the start of the
139 // sequence (includes rate control parameters below).
140 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
141 size_t global_params_size[MAX_GLOBAL_PARAMS];
142 int nb_global_params;
144 // Rate control parameters.
146 VAEncMiscParameterBuffer misc;
147 VAEncMiscParameterRateControl rc;
150 VAEncMiscParameterBuffer misc;
151 VAEncMiscParameterHRD hrd;
154 VAEncMiscParameterBuffer misc;
155 VAEncMiscParameterFrameRate fr;
157 #if VA_CHECK_VERSION(0, 36, 0)
159 VAEncMiscParameterBuffer misc;
160 VAEncMiscParameterBufferQualityLevel quality;
164 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
165 void *codec_sequence_params;
167 // Per-sequence parameters found in the per-picture parameter
168 // structure (VAEncPictureParameterBuffer*).
169 void *codec_picture_params;
171 // Current encoding window, in display (input) order.
172 VAAPIEncodePicture *pic_start, *pic_end;
174 // Next input order index (display order).
176 // Number of frames that output is behind input.
177 int64_t output_delay;
178 // Number of frames decode output will need to be delayed.
179 int64_t decode_delay;
180 // Next output order index (encode order).
181 int64_t output_order;
184 // All encode operations are done independently (synchronise
185 // immediately after every operation).
186 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
187 // Overlap as many operations as possible.
188 ISSUE_MODE_MAXIMISE_THROUGHPUT,
189 // Overlap operations only when satisfying parallel dependencies.
190 ISSUE_MODE_MINIMISE_LATENCY,
193 // Timestamp handling.
195 int64_t dts_pts_diff;
196 int64_t ts_ring[MAX_REORDER_DELAY * 3];
198 // Frame type decision.
205 } VAAPIEncodeContext;
208 typedef struct VAAPIEncodeType {
209 // Perform any extra codec-specific configuration after the
210 // codec context is initialised (set up the private data and
211 // add any necessary global parameters).
212 int (*configure)(AVCodecContext *avctx);
214 // The size of the parameter structures:
215 // sizeof(VAEnc{type}ParameterBuffer{codec}).
216 size_t sequence_params_size;
217 size_t picture_params_size;
218 size_t slice_params_size;
220 // Fill the parameter structures.
221 int (*init_sequence_params)(AVCodecContext *avctx);
222 int (*init_picture_params)(AVCodecContext *avctx,
223 VAAPIEncodePicture *pic);
224 int (*init_slice_params)(AVCodecContext *avctx,
225 VAAPIEncodePicture *pic,
226 VAAPIEncodeSlice *slice);
228 // The type used by the packed header: this should look like
229 // VAEncPackedHeader{something}.
230 int sequence_header_type;
231 int picture_header_type;
232 int slice_header_type;
234 // Write the packed header data to the provided buffer.
235 // The sequence header is also used to fill the codec extradata
236 // when the encoder is starting.
237 int (*write_sequence_header)(AVCodecContext *avctx,
238 char *data, size_t *data_len);
239 int (*write_picture_header)(AVCodecContext *avctx,
240 VAAPIEncodePicture *pic,
241 char *data, size_t *data_len);
242 int (*write_slice_header)(AVCodecContext *avctx,
243 VAAPIEncodePicture *pic,
244 VAAPIEncodeSlice *slice,
245 char *data, size_t *data_len);
247 // Fill an extra parameter structure, which will then be
248 // passed to vaRenderPicture(). Will be called repeatedly
249 // with increasing index argument until AVERROR_EOF is
251 int (*write_extra_buffer)(AVCodecContext *avctx,
252 VAAPIEncodePicture *pic,
253 int index, int *type,
254 char *data, size_t *data_len);
256 // Write an extra packed header. Will be called repeatedly
257 // with increasing index argument until AVERROR_EOF is
259 int (*write_extra_header)(AVCodecContext *avctx,
260 VAAPIEncodePicture *pic,
261 int index, int *type,
262 char *data, size_t *data_len);
266 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
267 const AVFrame *input_image, int *got_packet);
269 int ff_vaapi_encode_init(AVCodecContext *avctx);
270 int ff_vaapi_encode_close(AVCodecContext *avctx);
272 #endif /* AVCODEC_VAAPI_ENCODE_H */