]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode.h
5d6fdcf336a788d171a799a12a40c5f494cf3df2
[ffmpeg] / libavcodec / vaapi_encode.h
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
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 #if VA_CHECK_VERSION(1, 0, 0)
27 #include <va/va_str.h>
28 #endif
29
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_vaapi.h"
32
33 #include "avcodec.h"
34
35 struct VAAPIEncodeType;
36 struct VAAPIEncodePicture;
37
38 enum {
39     MAX_CONFIG_ATTRIBUTES  = 4,
40     MAX_GLOBAL_PARAMS      = 4,
41     MAX_PICTURE_REFERENCES = 2,
42     MAX_REORDER_DELAY      = 16,
43     MAX_PARAM_BUFFER_SIZE  = 1024,
44 };
45
46 enum {
47     PICTURE_TYPE_IDR = 0,
48     PICTURE_TYPE_I   = 1,
49     PICTURE_TYPE_P   = 2,
50     PICTURE_TYPE_B   = 3,
51 };
52
53 typedef struct VAAPIEncodeSlice {
54     int             index;
55     void           *priv_data;
56     void           *codec_slice_params;
57 } VAAPIEncodeSlice;
58
59 typedef struct VAAPIEncodePicture {
60     struct VAAPIEncodePicture *next;
61
62     int64_t         display_order;
63     int64_t         encode_order;
64     int64_t         pts;
65
66     int             type;
67     int             input_available;
68     int             encode_issued;
69     int             encode_complete;
70
71     AVFrame        *input_image;
72     VASurfaceID     input_surface;
73
74     AVFrame        *recon_image;
75     VASurfaceID     recon_surface;
76
77     int          nb_param_buffers;
78     VABufferID     *param_buffers;
79
80     AVBufferRef    *output_buffer_ref;
81     VABufferID      output_buffer;
82
83     void           *priv_data;
84     void           *codec_picture_params;
85
86     int          nb_refs;
87     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
88
89     int          nb_slices;
90     VAAPIEncodeSlice *slices;
91 } VAAPIEncodePicture;
92
93 typedef struct VAAPIEncodeProfile {
94     // lavc profile value (FF_PROFILE_*).
95     int       av_profile;
96     // Supported bit depth.
97     int       depth;
98     // Number of components.
99     int       nb_components;
100     // Chroma subsampling in width dimension.
101     int       log2_chroma_w;
102     // Chroma subsampling in height dimension.
103     int       log2_chroma_h;
104     // VAAPI profile value.
105     VAProfile va_profile;
106 } VAAPIEncodeProfile;
107
108 typedef struct VAAPIEncodeContext {
109     const AVClass *class;
110
111     // Codec-specific hooks.
112     const struct VAAPIEncodeType *codec;
113
114     // Global options.
115
116     // Use low power encoding mode.
117     int             low_power;
118
119     // Supported packed headers (initially the desired set, modified
120     // later to what is actually supported).
121     unsigned int    va_packed_headers;
122
123     // The required size of surfaces.  This is probably the input
124     // size (AVCodecContext.width|height) aligned up to whatever
125     // block size is required by the codec.
126     int             surface_width;
127     int             surface_height;
128
129     // Everything above this point must be set before calling
130     // ff_vaapi_encode_init().
131
132     // Chosen encoding profile details.
133     const VAAPIEncodeProfile *profile;
134
135     // Encoding profile (VAProfile*).
136     VAProfile       va_profile;
137     // Encoding entrypoint (VAEntryoint*).
138     VAEntrypoint    va_entrypoint;
139     // Rate control mode.
140     unsigned int    va_rc_mode;
141     // Bitrate for codec-specific encoder parameters.
142     unsigned int    va_bit_rate;
143
144     // Configuration attributes to use when creating va_config.
145     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
146     int          nb_config_attributes;
147
148     VAConfigID      va_config;
149     VAContextID     va_context;
150
151     AVBufferRef    *device_ref;
152     AVHWDeviceContext *device;
153     AVVAAPIDeviceContext *hwctx;
154
155     // The hardware frame context containing the input frames.
156     AVBufferRef    *input_frames_ref;
157     AVHWFramesContext *input_frames;
158
159     // The hardware frame context containing the reconstructed frames.
160     AVBufferRef    *recon_frames_ref;
161     AVHWFramesContext *recon_frames;
162
163     // Pool of (reusable) bitstream output buffers.
164     AVBufferPool   *output_buffer_pool;
165
166     // Global parameters which will be applied at the start of the
167     // sequence (includes rate control parameters below).
168     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
169     size_t          global_params_size[MAX_GLOBAL_PARAMS];
170     int          nb_global_params;
171
172     // Rate control parameters.
173     struct {
174         VAEncMiscParameterBuffer misc;
175         VAEncMiscParameterRateControl rc;
176     } rc_params;
177     struct {
178         VAEncMiscParameterBuffer misc;
179         VAEncMiscParameterHRD hrd;
180     } hrd_params;
181     struct {
182         VAEncMiscParameterBuffer misc;
183         VAEncMiscParameterFrameRate fr;
184     } fr_params;
185 #if VA_CHECK_VERSION(0, 36, 0)
186     struct {
187         VAEncMiscParameterBuffer misc;
188         VAEncMiscParameterBufferQualityLevel quality;
189     } quality_params;
190 #endif
191
192     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
193     void           *codec_sequence_params;
194
195     // Per-sequence parameters found in the per-picture parameter
196     // structure (VAEncPictureParameterBuffer*).
197     void           *codec_picture_params;
198
199     // Current encoding window, in display (input) order.
200     VAAPIEncodePicture *pic_start, *pic_end;
201
202     // Next input order index (display order).
203     int64_t         input_order;
204     // Number of frames that output is behind input.
205     int64_t         output_delay;
206     // Number of frames decode output will need to be delayed.
207     int64_t         decode_delay;
208     // Next output order index (encode order).
209     int64_t         output_order;
210
211     enum {
212         // All encode operations are done independently (synchronise
213         // immediately after every operation).
214         ISSUE_MODE_SERIALISE_EVERYTHING = 0,
215         // Overlap as many operations as possible.
216         ISSUE_MODE_MAXIMISE_THROUGHPUT,
217         // Overlap operations only when satisfying parallel dependencies.
218         ISSUE_MODE_MINIMISE_LATENCY,
219     } issue_mode;
220
221     // Timestamp handling.
222     int64_t         first_pts;
223     int64_t         dts_pts_diff;
224     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
225
226     // Frame type decision.
227     int gop_size;
228     int p_per_i;
229     int b_per_p;
230     int force_idr;
231     int gop_counter;
232     int p_counter;
233     int end_of_stream;
234 } VAAPIEncodeContext;
235
236 typedef struct VAAPIEncodeType {
237     // List of supported profiles and corresponding VAAPI profiles.
238     // (Must end with FF_PROFILE_UNKNOWN.)
239     const VAAPIEncodeProfile *profiles;
240
241     // Perform any extra codec-specific configuration after the
242     // codec context is initialised (set up the private data and
243     // add any necessary global parameters).
244     int (*configure)(AVCodecContext *avctx);
245
246     // The size of the parameter structures:
247     // sizeof(VAEnc{type}ParameterBuffer{codec}).
248     size_t sequence_params_size;
249     size_t picture_params_size;
250     size_t slice_params_size;
251
252     // Fill the parameter structures.
253     int  (*init_sequence_params)(AVCodecContext *avctx);
254     int   (*init_picture_params)(AVCodecContext *avctx,
255                                  VAAPIEncodePicture *pic);
256     int     (*init_slice_params)(AVCodecContext *avctx,
257                                  VAAPIEncodePicture *pic,
258                                  VAAPIEncodeSlice *slice);
259
260     // The type used by the packed header: this should look like
261     // VAEncPackedHeader{something}.
262     int sequence_header_type;
263     int picture_header_type;
264     int slice_header_type;
265
266     // Write the packed header data to the provided buffer.
267     // The sequence header is also used to fill the codec extradata
268     // when the encoder is starting.
269     int (*write_sequence_header)(AVCodecContext *avctx,
270                                  char *data, size_t *data_len);
271     int  (*write_picture_header)(AVCodecContext *avctx,
272                                  VAAPIEncodePicture *pic,
273                                  char *data, size_t *data_len);
274     int    (*write_slice_header)(AVCodecContext *avctx,
275                                  VAAPIEncodePicture *pic,
276                                  VAAPIEncodeSlice *slice,
277                                  char *data, size_t *data_len);
278
279     // Fill an extra parameter structure, which will then be
280     // passed to vaRenderPicture().  Will be called repeatedly
281     // with increasing index argument until AVERROR_EOF is
282     // returned.
283     int    (*write_extra_buffer)(AVCodecContext *avctx,
284                                  VAAPIEncodePicture *pic,
285                                  int index, int *type,
286                                  char *data, size_t *data_len);
287
288     // Write an extra packed header.  Will be called repeatedly
289     // with increasing index argument until AVERROR_EOF is
290     // returned.
291     int    (*write_extra_header)(AVCodecContext *avctx,
292                                  VAAPIEncodePicture *pic,
293                                  int index, int *type,
294                                  char *data, size_t *data_len);
295 } VAAPIEncodeType;
296
297
298 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
299                      const AVFrame *input_image, int *got_packet);
300
301 int ff_vaapi_encode_init(AVCodecContext *avctx);
302 int ff_vaapi_encode_close(AVCodecContext *avctx);
303
304
305 #define VAAPI_ENCODE_COMMON_OPTIONS \
306     { "low_power", \
307       "Use low-power encoding mode (only available on some platforms; " \
308       "may not support all encoding features)", \
309       OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
310       { .i64 = 0 }, 0, 1, FLAGS }
311
312
313 #endif /* AVCODEC_VAAPI_ENCODE_H */