]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode.h
Merge commit '2edaafe5b93832715781851dfe2663da228a05ad'
[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     // Configuration attributes to use when creating va_config.
117     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
118     int          nb_config_attributes;
119
120     VAConfigID      va_config;
121     VAContextID     va_context;
122
123     AVBufferRef    *device_ref;
124     AVHWDeviceContext *device;
125     AVVAAPIDeviceContext *hwctx;
126
127     // The hardware frame context containing the input frames.
128     AVBufferRef    *input_frames_ref;
129     AVHWFramesContext *input_frames;
130
131     // The hardware frame context containing the reconstructed frames.
132     AVBufferRef    *recon_frames_ref;
133     AVHWFramesContext *recon_frames;
134
135     // Pool of (reusable) bitstream output buffers.
136     AVBufferPool   *output_buffer_pool;
137
138     // Global parameters which will be applied at the start of the
139     // sequence (includes rate control parameters below).
140     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
141     size_t          global_params_size[MAX_GLOBAL_PARAMS];
142     int          nb_global_params;
143
144     // Rate control parameters.
145     struct {
146         VAEncMiscParameterBuffer misc;
147         VAEncMiscParameterRateControl rc;
148     } rc_params;
149     struct {
150         VAEncMiscParameterBuffer misc;
151         VAEncMiscParameterHRD hrd;
152     } hrd_params;
153     struct {
154         VAEncMiscParameterBuffer misc;
155         VAEncMiscParameterFrameRate fr;
156     } fr_params;
157 #if VA_CHECK_VERSION(0, 36, 0)
158     struct {
159         VAEncMiscParameterBuffer misc;
160         VAEncMiscParameterBufferQualityLevel quality;
161     } quality_params;
162 #endif
163
164     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
165     void           *codec_sequence_params;
166
167     // Per-sequence parameters found in the per-picture parameter
168     // structure (VAEncPictureParameterBuffer*).
169     void           *codec_picture_params;
170
171     // Current encoding window, in display (input) order.
172     VAAPIEncodePicture *pic_start, *pic_end;
173
174     // Next input order index (display order).
175     int64_t         input_order;
176     // Number of frames that output is behind input.
177     int64_t         output_delay;
178     // Number of frames decode output will need to be delayed.
179     int64_t         decode_delay;
180     // Next output order index (encode order).
181     int64_t         output_order;
182
183     enum {
184         // All encode operations are done independently (synchronise
185         // immediately after every operation).
186         ISSUE_MODE_SERIALISE_EVERYTHING = 0,
187         // Overlap as many operations as possible.
188         ISSUE_MODE_MAXIMISE_THROUGHPUT,
189         // Overlap operations only when satisfying parallel dependencies.
190         ISSUE_MODE_MINIMISE_LATENCY,
191     } issue_mode;
192
193     // Timestamp handling.
194     int64_t         first_pts;
195     int64_t         dts_pts_diff;
196     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
197
198     // Frame type decision.
199     int p_per_i;
200     int b_per_p;
201     int force_idr;
202     int gop_counter;
203     int p_counter;
204     int end_of_stream;
205 } VAAPIEncodeContext;
206
207
208 typedef struct VAAPIEncodeType {
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     // The sequence header is also used to fill the codec extradata
236     // when the encoder is starting.
237     int (*write_sequence_header)(AVCodecContext *avctx,
238                                  char *data, size_t *data_len);
239     int  (*write_picture_header)(AVCodecContext *avctx,
240                                  VAAPIEncodePicture *pic,
241                                  char *data, size_t *data_len);
242     int    (*write_slice_header)(AVCodecContext *avctx,
243                                  VAAPIEncodePicture *pic,
244                                  VAAPIEncodeSlice *slice,
245                                  char *data, size_t *data_len);
246
247     // Fill an extra parameter structure, which will then be
248     // passed to vaRenderPicture().  Will be called repeatedly
249     // with increasing index argument until AVERROR_EOF is
250     // returned.
251     int    (*write_extra_buffer)(AVCodecContext *avctx,
252                                  VAAPIEncodePicture *pic,
253                                  int index, int *type,
254                                  char *data, size_t *data_len);
255
256     // Write an extra packed header.  Will be called repeatedly
257     // with increasing index argument until AVERROR_EOF is
258     // returned.
259     int    (*write_extra_header)(AVCodecContext *avctx,
260                                  VAAPIEncodePicture *pic,
261                                  int index, int *type,
262                                  char *data, size_t *data_len);
263 } VAAPIEncodeType;
264
265
266 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
267                      const AVFrame *input_image, int *got_packet);
268
269 int ff_vaapi_encode_init(AVCodecContext *avctx);
270 int ff_vaapi_encode_close(AVCodecContext *avctx);
271
272 #endif /* AVCODEC_VAAPI_ENCODE_H */