71f608751b4996e6bcb5c990f9ca5d54b042ca4f
[libav.git] / libavcodec / vaapi_encode.h
1 /*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #ifndef AVCODEC_VAAPI_ENCODE_H
20 #define AVCODEC_VAAPI_ENCODE_H
21
22 #include <stdint.h>
23
24 #include <va/va.h>
25
26 #include "libavutil/hwcontext.h"
27 #include "libavutil/hwcontext_vaapi.h"
28
29 #include "avcodec.h"
30
31 struct VAAPIEncodeType;
32 struct VAAPIEncodePicture;
33
34 enum {
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,
42 };
43
44 enum {
45 PICTURE_TYPE_IDR = 0,
46 PICTURE_TYPE_I = 1,
47 PICTURE_TYPE_P = 2,
48 PICTURE_TYPE_B = 3,
49 };
50
51 typedef struct VAAPIEncodeSlice {
52 void *priv_data;
53 void *codec_slice_params;
54 } VAAPIEncodeSlice;
55
56 typedef struct VAAPIEncodePicture {
57 struct VAAPIEncodePicture *next;
58
59 int64_t display_order;
60 int64_t encode_order;
61 int64_t pts;
62
63 int type;
64 int input_available;
65 int encode_issued;
66 int encode_complete;
67
68 AVFrame *input_image;
69 VASurfaceID input_surface;
70
71 AVFrame *recon_image;
72 VASurfaceID recon_surface;
73
74 int nb_param_buffers;
75 VABufferID param_buffers[MAX_PARAM_BUFFERS];
76
77 AVBufferRef *output_buffer_ref;
78 VABufferID output_buffer;
79
80 void *priv_data;
81 void *codec_picture_params;
82
83 int nb_refs;
84 struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
85
86 int nb_slices;
87 VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
88 } VAAPIEncodePicture;
89
90 typedef struct VAAPIEncodeContext {
91 const AVClass *class;
92
93 // Codec-specific hooks.
94 const struct VAAPIEncodeType *codec;
95
96 // Encoding profile (VAProfileXXX).
97 VAProfile va_profile;
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
105 // The required size of surfaces. This is probably the input
106 // size (AVCodecContext.width|height) aligned up to whatever
107 // block size is required by the codec.
108 int surface_width;
109 int surface_height;
110
111 // Everything above this point must be set before calling
112 // ff_vaapi_encode_init().
113
114 // Codec-specific state.
115 void *priv_data;
116
117 // Configuration attributes to use when creating va_config.
118 VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
119 int nb_config_attributes;
120
121 VAConfigID va_config;
122 VAContextID va_context;
123
124 AVBufferRef *device_ref;
125 AVHWDeviceContext *device;
126 AVVAAPIDeviceContext *hwctx;
127
128 // The hardware frame context containing the input frames.
129 AVBufferRef *input_frames_ref;
130 AVHWFramesContext *input_frames;
131
132 // The hardware frame context containing the reconstructed frames.
133 AVBufferRef *recon_frames_ref;
134 AVHWFramesContext *recon_frames;
135
136 // Pool of (reusable) bitstream output buffers.
137 AVBufferPool *output_buffer_pool;
138
139 // Global parameters which will be applied at the start of the
140 // sequence (includes rate control parameters below).
141 VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
142 size_t global_params_size[MAX_GLOBAL_PARAMS];
143 int nb_global_params;
144
145 // Rate control parameters.
146 struct {
147 VAEncMiscParameterBuffer misc;
148 VAEncMiscParameterRateControl rc;
149 } rc_params;
150 struct {
151 VAEncMiscParameterBuffer misc;
152 VAEncMiscParameterHRD hrd;
153 } hrd_params;
154
155 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
156 void *codec_sequence_params;
157
158 // Per-sequence parameters found in the per-picture parameter
159 // structure (VAEncPictureParameterBuffer*).
160 void *codec_picture_params;
161
162 // Current encoding window, in display (input) order.
163 VAAPIEncodePicture *pic_start, *pic_end;
164
165 // Next input order index (display order).
166 int64_t input_order;
167 // Number of frames that output is behind input.
168 int64_t output_delay;
169 // Number of frames decode output will need to be delayed.
170 int64_t decode_delay;
171 // Next output order index (encode order).
172 int64_t output_order;
173
174 enum {
175 // All encode operations are done independently (synchronise
176 // immediately after every operation).
177 ISSUE_MODE_SERIALISE_EVERYTHING = 0,
178 // Overlap as many operations as possible.
179 ISSUE_MODE_MAXIMISE_THROUGHPUT,
180 // Overlap operations only when satisfying parallel dependencies.
181 ISSUE_MODE_MINIMISE_LATENCY,
182 } issue_mode;
183
184 // Timestamp handling.
185 int64_t first_pts;
186 int64_t dts_pts_diff;
187 int64_t ts_ring[MAX_REORDER_DELAY * 3];
188
189 // Frame type decision.
190 int i_per_idr;
191 int p_per_i;
192 int b_per_p;
193 int idr_counter;
194 int i_counter;
195 int p_counter;
196 int end_of_stream;
197
198 // Codec-local options are allocated to follow this structure in
199 // memory (in the AVCodec definition, set priv_data_size to
200 // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
201 void *codec_options;
202 char codec_options_data[0];
203 } VAAPIEncodeContext;
204
205
206 typedef struct VAAPIEncodeType {
207 size_t priv_data_size;
208
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);
213
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;
219
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);
227
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;
233
234 // Write the packed header data to the provided buffer.
235 int (*write_sequence_header)(AVCodecContext *avctx,
236 char *data, size_t *data_len);
237 int (*write_picture_header)(AVCodecContext *avctx,
238 VAAPIEncodePicture *pic,
239 char *data, size_t *data_len);
240 int (*write_slice_header)(AVCodecContext *avctx,
241 VAAPIEncodePicture *pic,
242 VAAPIEncodeSlice *slice,
243 char *data, size_t *data_len);
244
245 // Fill an extra parameter structure, which will then be
246 // passed to vaRenderPicture(). Will be called repeatedly
247 // with increasing index argument until AVERROR_EOF is
248 // returned.
249 int (*write_extra_buffer)(AVCodecContext *avctx,
250 VAAPIEncodePicture *pic,
251 int index, int *type,
252 char *data, size_t *data_len);
253
254 // Write an extra packed header. Will be called repeatedly
255 // with increasing index argument until AVERROR_EOF is
256 // returned.
257 int (*write_extra_header)(AVCodecContext *avctx,
258 VAAPIEncodePicture *pic,
259 int index, int *type,
260 char *data, size_t *data_len);
261 } VAAPIEncodeType;
262
263
264 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
265 const AVFrame *input_image, int *got_packet);
266
267 int ff_vaapi_encode_init(AVCodecContext *avctx);
268 int ff_vaapi_encode_close(AVCodecContext *avctx);
269
270 #endif /* AVCODEC_VAAPI_ENCODE_H */