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.

453 lines
15KB

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