]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode.h
Merge commit '49f9c4272c4029b57ff300d908ba03c6332fc9c4'
[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_DPB_SIZE           = 16,
42     MAX_PICTURE_REFERENCES = 2,
43     MAX_REORDER_DELAY      = 16,
44     MAX_PARAM_BUFFER_SIZE  = 1024,
45 };
46
47 enum {
48     PICTURE_TYPE_IDR = 0,
49     PICTURE_TYPE_I   = 1,
50     PICTURE_TYPE_P   = 2,
51     PICTURE_TYPE_B   = 3,
52 };
53
54 typedef struct VAAPIEncodeSlice {
55     int             index;
56     int             row_start;
57     int             row_size;
58     int             block_start;
59     int             block_size;
60     void           *priv_data;
61     void           *codec_slice_params;
62 } VAAPIEncodeSlice;
63
64 typedef struct VAAPIEncodePicture {
65     struct VAAPIEncodePicture *next;
66
67     int64_t         display_order;
68     int64_t         encode_order;
69     int64_t         pts;
70     int             force_idr;
71
72     int             type;
73     int             b_depth;
74     int             encode_issued;
75     int             encode_complete;
76
77     AVFrame        *input_image;
78     VASurfaceID     input_surface;
79
80     AVFrame        *recon_image;
81     VASurfaceID     recon_surface;
82
83     int          nb_param_buffers;
84     VABufferID     *param_buffers;
85
86     AVBufferRef    *output_buffer_ref;
87     VABufferID      output_buffer;
88
89     void           *priv_data;
90     void           *codec_picture_params;
91
92     // Whether this picture is a reference picture.
93     int             is_reference;
94
95     // The contents of the DPB after this picture has been decoded.
96     // This will contain the picture itself if it is a reference picture,
97     // but not if it isn't.
98     int                     nb_dpb_pics;
99     struct VAAPIEncodePicture *dpb[MAX_DPB_SIZE];
100     // The reference pictures used in decoding this picture.  If they are
101     // used by later pictures they will also appear in the DPB.
102     int                     nb_refs;
103     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
104     // The previous reference picture in encode order.  Must be in at least
105     // one of the reference list and DPB list.
106     struct VAAPIEncodePicture *prev;
107     // Reference count for other pictures referring to this one through
108     // the above pointers, directly from incomplete pictures and indirectly
109     // through completed pictures.
110     int             ref_count[2];
111     int             ref_removed[2];
112
113     int          nb_slices;
114     VAAPIEncodeSlice *slices;
115 } VAAPIEncodePicture;
116
117 typedef struct VAAPIEncodeProfile {
118     // lavc profile value (FF_PROFILE_*).
119     int       av_profile;
120     // Supported bit depth.
121     int       depth;
122     // Number of components.
123     int       nb_components;
124     // Chroma subsampling in width dimension.
125     int       log2_chroma_w;
126     // Chroma subsampling in height dimension.
127     int       log2_chroma_h;
128     // VAAPI profile value.
129     VAProfile va_profile;
130 } VAAPIEncodeProfile;
131
132 enum {
133     RC_MODE_AUTO,
134     RC_MODE_CQP,
135     RC_MODE_CBR,
136     RC_MODE_VBR,
137     RC_MODE_ICQ,
138     RC_MODE_QVBR,
139     RC_MODE_AVBR,
140     RC_MODE_MAX = RC_MODE_AVBR,
141 };
142
143 typedef struct VAAPIEncodeRCMode {
144     // Mode from above enum (RC_MODE_*).
145     int mode;
146     // Name.
147     const char *name;
148     // Supported in the compile-time VAAPI version.
149     int supported;
150     // VA mode value (VA_RC_*).
151     uint32_t va_mode;
152     // Uses bitrate parameters.
153     int bitrate;
154     // Supports maxrate distinct from bitrate.
155     int maxrate;
156     // Uses quality value.
157     int quality;
158     // Supports HRD/VBV parameters.
159     int hrd;
160 } VAAPIEncodeRCMode;
161
162 typedef struct VAAPIEncodeContext {
163     const AVClass *class;
164
165     // Codec-specific hooks.
166     const struct VAAPIEncodeType *codec;
167
168     // Global options.
169
170     // Use low power encoding mode.
171     int             low_power;
172
173     // Number of I frames between IDR frames.
174     int             idr_interval;
175
176     // Desired B frame reference depth.
177     int             desired_b_depth;
178
179     // Explicitly set RC mode (otherwise attempt to pick from
180     // available modes).
181     int             explicit_rc_mode;
182
183     // Explicitly-set QP, for use with the "qp" options.
184     // (Forces CQP mode when set, overriding everything else.)
185     int             explicit_qp;
186
187     // Desired packed headers.
188     unsigned int    desired_packed_headers;
189
190     // The required size of surfaces.  This is probably the input
191     // size (AVCodecContext.width|height) aligned up to whatever
192     // block size is required by the codec.
193     int             surface_width;
194     int             surface_height;
195
196     // The block size for slice calculations.
197     int             slice_block_width;
198     int             slice_block_height;
199
200     // Everything above this point must be set before calling
201     // ff_vaapi_encode_init().
202
203     // Chosen encoding profile details.
204     const VAAPIEncodeProfile *profile;
205
206     // Chosen rate control mode details.
207     const VAAPIEncodeRCMode *rc_mode;
208     // RC quality level - meaning depends on codec and RC mode.
209     // In CQP mode this sets the fixed quantiser value.
210     int             rc_quality;
211
212     // Encoding profile (VAProfile*).
213     VAProfile       va_profile;
214     // Encoding entrypoint (VAEntryoint*).
215     VAEntrypoint    va_entrypoint;
216     // Rate control mode.
217     unsigned int    va_rc_mode;
218     // Bitrate for codec-specific encoder parameters.
219     unsigned int    va_bit_rate;
220     // Packed headers which will actually be sent.
221     unsigned int    va_packed_headers;
222
223     // Configuration attributes to use when creating va_config.
224     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
225     int          nb_config_attributes;
226
227     VAConfigID      va_config;
228     VAContextID     va_context;
229
230     AVBufferRef    *device_ref;
231     AVHWDeviceContext *device;
232     AVVAAPIDeviceContext *hwctx;
233
234     // The hardware frame context containing the input frames.
235     AVBufferRef    *input_frames_ref;
236     AVHWFramesContext *input_frames;
237
238     // The hardware frame context containing the reconstructed frames.
239     AVBufferRef    *recon_frames_ref;
240     AVHWFramesContext *recon_frames;
241
242     // Pool of (reusable) bitstream output buffers.
243     AVBufferPool   *output_buffer_pool;
244
245     // Global parameters which will be applied at the start of the
246     // sequence (includes rate control parameters below).
247     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
248     size_t          global_params_size[MAX_GLOBAL_PARAMS];
249     int          nb_global_params;
250
251     // Rate control parameters.
252     struct {
253         VAEncMiscParameterBuffer misc;
254         VAEncMiscParameterRateControl rc;
255     } rc_params;
256     struct {
257         VAEncMiscParameterBuffer misc;
258         VAEncMiscParameterHRD hrd;
259     } hrd_params;
260     struct {
261         VAEncMiscParameterBuffer misc;
262         VAEncMiscParameterFrameRate fr;
263     } fr_params;
264 #if VA_CHECK_VERSION(0, 36, 0)
265     struct {
266         VAEncMiscParameterBuffer misc;
267         VAEncMiscParameterBufferQualityLevel quality;
268     } quality_params;
269 #endif
270
271     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
272     void           *codec_sequence_params;
273
274     // Per-sequence parameters found in the per-picture parameter
275     // structure (VAEncPictureParameterBuffer*).
276     void           *codec_picture_params;
277
278     // Current encoding window, in display (input) order.
279     VAAPIEncodePicture *pic_start, *pic_end;
280     // The next picture to use as the previous reference picture in
281     // encoding order.
282     VAAPIEncodePicture *next_prev;
283
284     // Next input order index (display order).
285     int64_t         input_order;
286     // Number of frames that output is behind input.
287     int64_t         output_delay;
288     // Next encode order index.
289     int64_t         encode_order;
290     // Number of frames decode output will need to be delayed.
291     int64_t         decode_delay;
292     // Next output order index (in encode order).
293     int64_t         output_order;
294
295     // Timestamp handling.
296     int64_t         first_pts;
297     int64_t         dts_pts_diff;
298     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
299
300     // Slice structure.
301     int slice_block_rows;
302     int slice_block_cols;
303     int nb_slices;
304     int slice_size;
305
306     // Frame type decision.
307     int gop_size;
308     int closed_gop;
309     int gop_per_idr;
310     int p_per_i;
311     int max_b_depth;
312     int b_per_p;
313     int force_idr;
314     int idr_counter;
315     int gop_counter;
316     int end_of_stream;
317 } VAAPIEncodeContext;
318
319 enum {
320     // Codec supports controlling the subdivision of pictures into slices.
321     FLAG_SLICE_CONTROL         = 1 << 0,
322     // Codec only supports constant quality (no rate control).
323     FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
324     // Codec is intra-only.
325     FLAG_INTRA_ONLY            = 1 << 2,
326     // Codec supports B-pictures.
327     FLAG_B_PICTURES            = 1 << 3,
328     // Codec supports referencing B-pictures.
329     FLAG_B_PICTURE_REFERENCES  = 1 << 4,
330     // Codec supports non-IDR key pictures (that is, key pictures do
331     // not necessarily empty the DPB).
332     FLAG_NON_IDR_KEY_PICTURES  = 1 << 5,
333 };
334
335 typedef struct VAAPIEncodeType {
336     // List of supported profiles and corresponding VAAPI profiles.
337     // (Must end with FF_PROFILE_UNKNOWN.)
338     const VAAPIEncodeProfile *profiles;
339
340     // Codec feature flags.
341     int flags;
342
343     // Default quality for this codec - used as quantiser or RC quality
344     // factor depending on RC mode.
345     int default_quality;
346
347     // Perform any extra codec-specific configuration after the
348     // codec context is initialised (set up the private data and
349     // add any necessary global parameters).
350     int (*configure)(AVCodecContext *avctx);
351
352     // The size of any private data structure associated with each
353     // picture (can be zero if not required).
354     size_t picture_priv_data_size;
355
356     // The size of the parameter structures:
357     // sizeof(VAEnc{type}ParameterBuffer{codec}).
358     size_t sequence_params_size;
359     size_t picture_params_size;
360     size_t slice_params_size;
361
362     // Fill the parameter structures.
363     int  (*init_sequence_params)(AVCodecContext *avctx);
364     int   (*init_picture_params)(AVCodecContext *avctx,
365                                  VAAPIEncodePicture *pic);
366     int     (*init_slice_params)(AVCodecContext *avctx,
367                                  VAAPIEncodePicture *pic,
368                                  VAAPIEncodeSlice *slice);
369
370     // The type used by the packed header: this should look like
371     // VAEncPackedHeader{something}.
372     int sequence_header_type;
373     int picture_header_type;
374     int slice_header_type;
375
376     // Write the packed header data to the provided buffer.
377     // The sequence header is also used to fill the codec extradata
378     // when the encoder is starting.
379     int (*write_sequence_header)(AVCodecContext *avctx,
380                                  char *data, size_t *data_len);
381     int  (*write_picture_header)(AVCodecContext *avctx,
382                                  VAAPIEncodePicture *pic,
383                                  char *data, size_t *data_len);
384     int    (*write_slice_header)(AVCodecContext *avctx,
385                                  VAAPIEncodePicture *pic,
386                                  VAAPIEncodeSlice *slice,
387                                  char *data, size_t *data_len);
388
389     // Fill an extra parameter structure, which will then be
390     // passed to vaRenderPicture().  Will be called repeatedly
391     // with increasing index argument until AVERROR_EOF is
392     // returned.
393     int    (*write_extra_buffer)(AVCodecContext *avctx,
394                                  VAAPIEncodePicture *pic,
395                                  int index, int *type,
396                                  char *data, size_t *data_len);
397
398     // Write an extra packed header.  Will be called repeatedly
399     // with increasing index argument until AVERROR_EOF is
400     // returned.
401     int    (*write_extra_header)(AVCodecContext *avctx,
402                                  VAAPIEncodePicture *pic,
403                                  int index, int *type,
404                                  char *data, size_t *data_len);
405 } VAAPIEncodeType;
406
407
408 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
409                      const AVFrame *input_image, int *got_packet);
410
411 int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
412 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
413
414 int ff_vaapi_encode_init(AVCodecContext *avctx);
415 int ff_vaapi_encode_close(AVCodecContext *avctx);
416
417
418 #define VAAPI_ENCODE_COMMON_OPTIONS \
419     { "low_power", \
420       "Use low-power encoding mode (only available on some platforms; " \
421       "may not support all encoding features)", \
422       OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
423       { .i64 = 0 }, 0, 1, FLAGS }, \
424     { "idr_interval", \
425       "Distance (in I-frames) between IDR frames", \
426       OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
427       { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
428     { "b_depth", \
429       "Maximum B-frame reference depth", \
430       OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
431       { .i64 = 1 }, 1, INT_MAX, FLAGS }
432
433 #define VAAPI_ENCODE_RC_MODE(name, desc) \
434     { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
435       0, 0, FLAGS, "rc_mode" }
436 #define VAAPI_ENCODE_RC_OPTIONS \
437     { "rc_mode",\
438       "Set rate control mode", \
439       OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \
440       { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \
441     { "auto", "Choose mode automatically based on other parameters", \
442       0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \
443     VAAPI_ENCODE_RC_MODE(CQP,  "Constant-quality"), \
444     VAAPI_ENCODE_RC_MODE(CBR,  "Constant-bitrate"), \
445     VAAPI_ENCODE_RC_MODE(VBR,  "Variable-bitrate"), \
446     VAAPI_ENCODE_RC_MODE(ICQ,  "Intelligent constant-quality"), \
447     VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
448     VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
449
450
451 #endif /* AVCODEC_VAAPI_ENCODE_H */