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