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