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