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