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