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_PICTURE_SLICES = 1,
39 MAX_PARAM_BUFFERS = 16,
40 MAX_REORDER_DELAY = 16,
41 MAX_PARAM_BUFFER_SIZE = 1024,
52 // All encode operations are done independently.
53 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
54 // Overlap as many operations as possible.
55 ISSUE_MODE_MAXIMISE_THROUGHPUT,
56 // Overlap operations only when satisfying parallel dependencies.
57 ISSUE_MODE_MINIMISE_LATENCY,
60 typedef struct VAAPIEncodeSlice {
62 void *codec_slice_params;
65 typedef struct VAAPIEncodePicture {
66 struct VAAPIEncodePicture *next;
68 int64_t display_order;
78 VASurfaceID input_surface;
81 VASurfaceID recon_surface;
84 VABufferID param_buffers[MAX_PARAM_BUFFERS];
86 AVBufferRef *output_buffer_ref;
87 VABufferID output_buffer;
90 void *codec_picture_params;
93 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
96 VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
99 typedef struct VAAPIEncodeContext {
100 const AVClass *class;
102 // Codec-specific hooks.
103 const struct VAAPIEncodeType *codec;
105 // Codec-specific state.
108 VAProfile va_profile;
109 VAEntrypoint va_entrypoint;
110 VAConfigID va_config;
111 VAContextID va_context;
115 AVBufferRef *device_ref;
116 AVHWDeviceContext *device;
117 AVVAAPIDeviceContext *hwctx;
119 AVBufferRef *input_frames_ref;
120 AVHWFramesContext *input_frames;
122 // Input size, set from input frames.
125 // Aligned size, set by codec init, becomes hwframe size.
130 AVBufferRef *recon_frames_ref;
131 AVHWFramesContext *recon_frames;
133 AVBufferPool *output_buffer_pool;
135 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
136 int nb_config_attributes;
138 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
139 size_t global_params_size[MAX_GLOBAL_PARAMS];
140 int nb_global_params;
142 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
143 void *codec_sequence_params;
145 // Per-sequence parameters found in the per-picture parameter
146 // structure (VAEncPictureParameterBuffer*).
147 void *codec_picture_params;
149 // Current encoding window, in display (input) order.
150 VAAPIEncodePicture *pic_start, *pic_end;
152 // Next input order index (display order).
154 // Number of frames that output is behind input.
155 int64_t output_delay;
156 // Number of frames decode output will need to be delayed.
157 int64_t decode_delay;
158 // Next output order index (encode order).
159 int64_t output_order;
163 // Timestamp handling.
165 int64_t dts_pts_diff;
166 int64_t ts_ring[MAX_REORDER_DELAY * 3];
168 // Frame type decision.
177 // Codec-local options are allocated to follow this structure in
178 // memory (in the AVCodec definition, set priv_data_size to
179 // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
181 char codec_options_data[0];
182 } VAAPIEncodeContext;
185 typedef struct VAAPIEncodeType {
186 size_t priv_data_size;
188 int (*init)(AVCodecContext *avctx);
189 int (*close)(AVCodecContext *avctx);
191 size_t sequence_params_size;
192 size_t picture_params_size;
193 size_t slice_params_size;
195 int (*init_sequence_params)(AVCodecContext *avctx);
196 int (*init_picture_params)(AVCodecContext *avctx,
197 VAAPIEncodePicture *pic);
198 int (*init_slice_params)(AVCodecContext *avctx,
199 VAAPIEncodePicture *pic,
200 VAAPIEncodeSlice *slice);
202 int sequence_header_type;
203 int picture_header_type;
204 int slice_header_type;
206 int (*write_sequence_header)(AVCodecContext *avctx,
207 char *data, size_t *data_len);
208 int (*write_picture_header)(AVCodecContext *avctx,
209 VAAPIEncodePicture *pic,
210 char *data, size_t *data_len);
211 int (*write_slice_header)(AVCodecContext *avctx,
212 VAAPIEncodePicture *pic,
213 VAAPIEncodeSlice *slice,
214 char *data, size_t *data_len);
216 int (*write_extra_buffer)(AVCodecContext *avctx,
217 VAAPIEncodePicture *pic,
218 int index, int *type,
219 char *data, size_t *data_len);
220 int (*write_extra_header)(AVCodecContext *avctx,
221 VAAPIEncodePicture *pic,
222 int index, int *type,
223 char *data, size_t *data_len);
227 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
228 const AVFrame *input_image, int *got_packet);
230 int ff_vaapi_encode_init(AVCodecContext *avctx,
231 const VAAPIEncodeType *type);
232 int ff_vaapi_encode_close(AVCodecContext *avctx);
234 #endif /* AVCODEC_VAAPI_ENCODE_H */