You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

480 lines
16KB

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