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