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.

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