2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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_PICTURE_SLICES = 1,
39 MAX_PARAM_BUFFERS = 16,
40 MAX_REORDER_DELAY = 16,
41 MAX_PARAM_BUFFER_SIZE = 1024,
51 typedef struct VAAPIEncodeSlice {
53 void *codec_slice_params;
56 typedef struct VAAPIEncodePicture {
57 struct VAAPIEncodePicture *next;
59 int64_t display_order;
69 VASurfaceID input_surface;
72 VASurfaceID recon_surface;
75 VABufferID param_buffers[MAX_PARAM_BUFFERS];
77 AVBufferRef *output_buffer_ref;
78 VABufferID output_buffer;
81 void *codec_picture_params;
84 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
87 VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
90 typedef struct VAAPIEncodeContext {
93 // Codec-specific hooks.
94 const struct VAAPIEncodeType *codec;
96 // Encoding profile (VAProfileXXX).
98 // Encoding entrypoint (usually VAEntryointEncSlice).
99 VAEntrypoint va_entrypoint;
100 // Surface colour/sampling format (usually VA_RT_FORMAT_YUV420).
101 unsigned int va_rt_format;
102 // Rate control mode.
103 unsigned int va_rc_mode;
104 // Supported packed headers (initially the desired set, modified
105 // later to what is actually supported).
106 unsigned int va_packed_headers;
108 // The required size of surfaces. This is probably the input
109 // size (AVCodecContext.width|height) aligned up to whatever
110 // block size is required by the codec.
114 // Everything above this point must be set before calling
115 // ff_vaapi_encode_init().
117 // Codec-specific state.
120 // Configuration attributes to use when creating va_config.
121 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
122 int nb_config_attributes;
124 VAConfigID va_config;
125 VAContextID va_context;
127 AVBufferRef *device_ref;
128 AVHWDeviceContext *device;
129 AVVAAPIDeviceContext *hwctx;
131 // The hardware frame context containing the input frames.
132 AVBufferRef *input_frames_ref;
133 AVHWFramesContext *input_frames;
135 // The hardware frame context containing the reconstructed frames.
136 AVBufferRef *recon_frames_ref;
137 AVHWFramesContext *recon_frames;
139 // Pool of (reusable) bitstream output buffers.
140 AVBufferPool *output_buffer_pool;
142 // Global parameters which will be applied at the start of the
143 // sequence (includes rate control parameters below).
144 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
145 size_t global_params_size[MAX_GLOBAL_PARAMS];
146 int nb_global_params;
148 // Rate control parameters.
150 VAEncMiscParameterBuffer misc;
151 VAEncMiscParameterRateControl rc;
154 VAEncMiscParameterBuffer misc;
155 VAEncMiscParameterHRD hrd;
158 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
159 void *codec_sequence_params;
161 // Per-sequence parameters found in the per-picture parameter
162 // structure (VAEncPictureParameterBuffer*).
163 void *codec_picture_params;
165 // Current encoding window, in display (input) order.
166 VAAPIEncodePicture *pic_start, *pic_end;
168 // Next input order index (display order).
170 // Number of frames that output is behind input.
171 int64_t output_delay;
172 // Number of frames decode output will need to be delayed.
173 int64_t decode_delay;
174 // Next output order index (encode order).
175 int64_t output_order;
178 // All encode operations are done independently (synchronise
179 // immediately after every operation).
180 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
181 // Overlap as many operations as possible.
182 ISSUE_MODE_MAXIMISE_THROUGHPUT,
183 // Overlap operations only when satisfying parallel dependencies.
184 ISSUE_MODE_MINIMISE_LATENCY,
187 // Timestamp handling.
189 int64_t dts_pts_diff;
190 int64_t ts_ring[MAX_REORDER_DELAY * 3];
192 // Frame type decision.
201 // Codec-local options are allocated to follow this structure in
202 // memory (in the AVCodec definition, set priv_data_size to
203 // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
205 char codec_options_data[0];
206 } VAAPIEncodeContext;
209 typedef struct VAAPIEncodeType {
210 size_t priv_data_size;
212 // Perform any extra codec-specific configuration after the
213 // codec context is initialised (set up the private data and
214 // add any necessary global parameters).
215 int (*configure)(AVCodecContext *avctx);
217 // The size of the parameter structures:
218 // sizeof(VAEnc{type}ParameterBuffer{codec}).
219 size_t sequence_params_size;
220 size_t picture_params_size;
221 size_t slice_params_size;
223 // Fill the parameter structures.
224 int (*init_sequence_params)(AVCodecContext *avctx);
225 int (*init_picture_params)(AVCodecContext *avctx,
226 VAAPIEncodePicture *pic);
227 int (*init_slice_params)(AVCodecContext *avctx,
228 VAAPIEncodePicture *pic,
229 VAAPIEncodeSlice *slice);
231 // The type used by the packed header: this should look like
232 // VAEncPackedHeader{something}.
233 int sequence_header_type;
234 int picture_header_type;
235 int slice_header_type;
237 // Write the packed header data to the provided buffer.
238 // The sequence header is also used to fill the codec extradata
239 // when the encoder is starting.
240 int (*write_sequence_header)(AVCodecContext *avctx,
241 char *data, size_t *data_len);
242 int (*write_picture_header)(AVCodecContext *avctx,
243 VAAPIEncodePicture *pic,
244 char *data, size_t *data_len);
245 int (*write_slice_header)(AVCodecContext *avctx,
246 VAAPIEncodePicture *pic,
247 VAAPIEncodeSlice *slice,
248 char *data, size_t *data_len);
250 // Fill an extra parameter structure, which will then be
251 // passed to vaRenderPicture(). Will be called repeatedly
252 // with increasing index argument until AVERROR_EOF is
254 int (*write_extra_buffer)(AVCodecContext *avctx,
255 VAAPIEncodePicture *pic,
256 int index, int *type,
257 char *data, size_t *data_len);
259 // Write an extra packed header. Will be called repeatedly
260 // with increasing index argument until AVERROR_EOF is
262 int (*write_extra_header)(AVCodecContext *avctx,
263 VAAPIEncodePicture *pic,
264 int index, int *type,
265 char *data, size_t *data_len);
269 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
270 const AVFrame *input_image, int *got_packet);
272 int ff_vaapi_encode_init(AVCodecContext *avctx);
273 int ff_vaapi_encode_close(AVCodecContext *avctx);
275 #endif /* AVCODEC_VAAPI_ENCODE_H */