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