vaapi_encode.h 16 KB

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