Commit | Line | Data |
---|---|---|
104c804b MT |
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 { | |
f6b85523 MT |
35 | MAX_CONFIG_ATTRIBUTES = 4, |
36 | MAX_GLOBAL_PARAMS = 4, | |
104c804b MT |
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, | |
104c804b MT |
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 | ||
104c804b MT |
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 | ||
8a62d2c2 | 77 | AVBufferRef *output_buffer_ref; |
104c804b MT |
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 | ||
80a5d051 MT |
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; | |
892bbbcd MT |
104 | // Supported packed headers (initially the desired set, modified |
105 | // later to what is actually supported). | |
106 | unsigned int va_packed_headers; | |
80a5d051 MT |
107 | |
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. | |
111 | int surface_width; | |
112 | int surface_height; | |
113 | ||
114 | // Everything above this point must be set before calling | |
115 | // ff_vaapi_encode_init(). | |
116 | ||
104c804b MT |
117 | // Codec-specific state. |
118 | void *priv_data; | |
119 | ||
80a5d051 MT |
120 | // Configuration attributes to use when creating va_config. |
121 | VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES]; | |
122 | int nb_config_attributes; | |
123 | ||
104c804b MT |
124 | VAConfigID va_config; |
125 | VAContextID va_context; | |
126 | ||
104c804b MT |
127 | AVBufferRef *device_ref; |
128 | AVHWDeviceContext *device; | |
129 | AVVAAPIDeviceContext *hwctx; | |
130 | ||
80a5d051 | 131 | // The hardware frame context containing the input frames. |
104c804b MT |
132 | AVBufferRef *input_frames_ref; |
133 | AVHWFramesContext *input_frames; | |
134 | ||
80a5d051 | 135 | // The hardware frame context containing the reconstructed frames. |
104c804b MT |
136 | AVBufferRef *recon_frames_ref; |
137 | AVHWFramesContext *recon_frames; | |
138 | ||
80a5d051 | 139 | // Pool of (reusable) bitstream output buffers. |
8a62d2c2 MT |
140 | AVBufferPool *output_buffer_pool; |
141 | ||
80a5d051 MT |
142 | // Global parameters which will be applied at the start of the |
143 | // sequence (includes rate control parameters below). | |
f6b85523 MT |
144 | VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS]; |
145 | size_t global_params_size[MAX_GLOBAL_PARAMS]; | |
146 | int nb_global_params; | |
147 | ||
80a5d051 MT |
148 | // Rate control parameters. |
149 | struct { | |
150 | VAEncMiscParameterBuffer misc; | |
151 | VAEncMiscParameterRateControl rc; | |
152 | } rc_params; | |
153 | struct { | |
154 | VAEncMiscParameterBuffer misc; | |
155 | VAEncMiscParameterHRD hrd; | |
156 | } hrd_params; | |
157 | ||
104c804b MT |
158 | // Per-sequence parameter structure (VAEncSequenceParameterBuffer*). |
159 | void *codec_sequence_params; | |
160 | ||
161 | // Per-sequence parameters found in the per-picture parameter | |
162 | // structure (VAEncPictureParameterBuffer*). | |
f6b85523 | 163 | void *codec_picture_params; |
104c804b MT |
164 | |
165 | // Current encoding window, in display (input) order. | |
166 | VAAPIEncodePicture *pic_start, *pic_end; | |
167 | ||
168 | // Next input order index (display order). | |
169 | int64_t input_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; | |
176 | ||
80a5d051 MT |
177 | enum { |
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, | |
185 | } issue_mode; | |
104c804b MT |
186 | |
187 | // Timestamp handling. | |
188 | int64_t first_pts; | |
189 | int64_t dts_pts_diff; | |
190 | int64_t ts_ring[MAX_REORDER_DELAY * 3]; | |
191 | ||
192 | // Frame type decision. | |
193 | int i_per_idr; | |
194 | int p_per_i; | |
195 | int b_per_p; | |
196 | int idr_counter; | |
197 | int i_counter; | |
198 | int p_counter; | |
199 | int end_of_stream; | |
200 | ||
69b06ed4 MT |
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)). | |
204 | void *codec_options; | |
205 | char codec_options_data[0]; | |
104c804b MT |
206 | } VAAPIEncodeContext; |
207 | ||
208 | ||
209 | typedef struct VAAPIEncodeType { | |
80a5d051 | 210 | size_t priv_data_size; |
104c804b | 211 | |
80a5d051 MT |
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); | |
104c804b | 216 | |
80a5d051 MT |
217 | // The size of the parameter structures: |
218 | // sizeof(VAEnc{type}ParameterBuffer{codec}). | |
104c804b MT |
219 | size_t sequence_params_size; |
220 | size_t picture_params_size; | |
221 | size_t slice_params_size; | |
222 | ||
80a5d051 | 223 | // Fill the parameter structures. |
104c804b MT |
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); | |
230 | ||
80a5d051 MT |
231 | // The type used by the packed header: this should look like |
232 | // VAEncPackedHeader{something}. | |
104c804b MT |
233 | int sequence_header_type; |
234 | int picture_header_type; | |
235 | int slice_header_type; | |
236 | ||
80a5d051 | 237 | // Write the packed header data to the provided buffer. |
104c804b MT |
238 | int (*write_sequence_header)(AVCodecContext *avctx, |
239 | char *data, size_t *data_len); | |
240 | int (*write_picture_header)(AVCodecContext *avctx, | |
241 | VAAPIEncodePicture *pic, | |
242 | char *data, size_t *data_len); | |
243 | int (*write_slice_header)(AVCodecContext *avctx, | |
244 | VAAPIEncodePicture *pic, | |
245 | VAAPIEncodeSlice *slice, | |
246 | char *data, size_t *data_len); | |
247 | ||
80a5d051 MT |
248 | // Fill an extra parameter structure, which will then be |
249 | // passed to vaRenderPicture(). Will be called repeatedly | |
250 | // with increasing index argument until AVERROR_EOF is | |
251 | // returned. | |
104c804b MT |
252 | int (*write_extra_buffer)(AVCodecContext *avctx, |
253 | VAAPIEncodePicture *pic, | |
254 | int index, int *type, | |
255 | char *data, size_t *data_len); | |
80a5d051 MT |
256 | |
257 | // Write an extra packed header. Will be called repeatedly | |
258 | // with increasing index argument until AVERROR_EOF is | |
259 | // returned. | |
19d7667a MT |
260 | int (*write_extra_header)(AVCodecContext *avctx, |
261 | VAAPIEncodePicture *pic, | |
262 | int index, int *type, | |
263 | char *data, size_t *data_len); | |
104c804b MT |
264 | } VAAPIEncodeType; |
265 | ||
266 | ||
267 | int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, | |
268 | const AVFrame *input_image, int *got_packet); | |
269 | ||
80a5d051 | 270 | int ff_vaapi_encode_init(AVCodecContext *avctx); |
104c804b MT |
271 | int ff_vaapi_encode_close(AVCodecContext *avctx); |
272 | ||
273 | #endif /* AVCODEC_VAAPI_ENCODE_H */ |