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.

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