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.

387 lines
12KB

  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. typedef struct VAAPIEncodeContext {
  110. const AVClass *class;
  111. // Codec-specific hooks.
  112. const struct VAAPIEncodeType *codec;
  113. // Global options.
  114. // Use low power encoding mode.
  115. int low_power;
  116. // Number of I frames between IDR frames.
  117. int idr_interval;
  118. // Desired B frame reference depth.
  119. int desired_b_depth;
  120. // Desired packed headers.
  121. unsigned int desired_packed_headers;
  122. // The required size of surfaces. This is probably the input
  123. // size (AVCodecContext.width|height) aligned up to whatever
  124. // block size is required by the codec.
  125. int surface_width;
  126. int surface_height;
  127. // The block size for slice calculations.
  128. int slice_block_width;
  129. int slice_block_height;
  130. // Everything above this point must be set before calling
  131. // ff_vaapi_encode_init().
  132. // Chosen encoding profile details.
  133. const VAAPIEncodeProfile *profile;
  134. // Encoding profile (VAProfile*).
  135. VAProfile va_profile;
  136. // Encoding entrypoint (VAEntryoint*).
  137. VAEntrypoint va_entrypoint;
  138. // Rate control mode.
  139. unsigned int va_rc_mode;
  140. // Bitrate for codec-specific encoder parameters.
  141. unsigned int va_bit_rate;
  142. // Packed headers which will actually be sent.
  143. unsigned int va_packed_headers;
  144. // Configuration attributes to use when creating va_config.
  145. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  146. int nb_config_attributes;
  147. VAConfigID va_config;
  148. VAContextID va_context;
  149. AVBufferRef *device_ref;
  150. AVHWDeviceContext *device;
  151. AVVAAPIDeviceContext *hwctx;
  152. // The hardware frame context containing the input frames.
  153. AVBufferRef *input_frames_ref;
  154. AVHWFramesContext *input_frames;
  155. // The hardware frame context containing the reconstructed frames.
  156. AVBufferRef *recon_frames_ref;
  157. AVHWFramesContext *recon_frames;
  158. // Pool of (reusable) bitstream output buffers.
  159. AVBufferPool *output_buffer_pool;
  160. // Global parameters which will be applied at the start of the
  161. // sequence (includes rate control parameters below).
  162. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  163. size_t global_params_size[MAX_GLOBAL_PARAMS];
  164. int nb_global_params;
  165. // Rate control parameters.
  166. struct {
  167. VAEncMiscParameterBuffer misc;
  168. VAEncMiscParameterRateControl rc;
  169. } rc_params;
  170. struct {
  171. VAEncMiscParameterBuffer misc;
  172. VAEncMiscParameterHRD hrd;
  173. } hrd_params;
  174. struct {
  175. VAEncMiscParameterBuffer misc;
  176. VAEncMiscParameterFrameRate fr;
  177. } fr_params;
  178. #if VA_CHECK_VERSION(0, 36, 0)
  179. struct {
  180. VAEncMiscParameterBuffer misc;
  181. VAEncMiscParameterBufferQualityLevel quality;
  182. } quality_params;
  183. #endif
  184. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  185. void *codec_sequence_params;
  186. // Per-sequence parameters found in the per-picture parameter
  187. // structure (VAEncPictureParameterBuffer*).
  188. void *codec_picture_params;
  189. // Current encoding window, in display (input) order.
  190. VAAPIEncodePicture *pic_start, *pic_end;
  191. // The next picture to use as the previous reference picture in
  192. // encoding order.
  193. VAAPIEncodePicture *next_prev;
  194. // Next input order index (display order).
  195. int64_t input_order;
  196. // Number of frames that output is behind input.
  197. int64_t output_delay;
  198. // Next encode order index.
  199. int64_t encode_order;
  200. // Number of frames decode output will need to be delayed.
  201. int64_t decode_delay;
  202. // Next output order index (in encode order).
  203. int64_t output_order;
  204. // Timestamp handling.
  205. int64_t first_pts;
  206. int64_t dts_pts_diff;
  207. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  208. // Slice structure.
  209. int slice_block_rows;
  210. int slice_block_cols;
  211. int nb_slices;
  212. int slice_size;
  213. // Frame type decision.
  214. int gop_size;
  215. int closed_gop;
  216. int gop_per_idr;
  217. int p_per_i;
  218. int max_b_depth;
  219. int b_per_p;
  220. int force_idr;
  221. int idr_counter;
  222. int gop_counter;
  223. int end_of_stream;
  224. } VAAPIEncodeContext;
  225. enum {
  226. // Codec supports controlling the subdivision of pictures into slices.
  227. FLAG_SLICE_CONTROL = 1 << 0,
  228. // Codec only supports constant quality (no rate control).
  229. FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
  230. // Codec is intra-only.
  231. FLAG_INTRA_ONLY = 1 << 2,
  232. // Codec supports B-pictures.
  233. FLAG_B_PICTURES = 1 << 3,
  234. // Codec supports referencing B-pictures.
  235. FLAG_B_PICTURE_REFERENCES = 1 << 4,
  236. // Codec supports non-IDR key pictures (that is, key pictures do
  237. // not necessarily empty the DPB).
  238. FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
  239. };
  240. typedef struct VAAPIEncodeType {
  241. // List of supported profiles and corresponding VAAPI profiles.
  242. // (Must end with FF_PROFILE_UNKNOWN.)
  243. const VAAPIEncodeProfile *profiles;
  244. // Codec feature flags.
  245. int flags;
  246. // Perform any extra codec-specific configuration after the
  247. // codec context is initialised (set up the private data and
  248. // add any necessary global parameters).
  249. int (*configure)(AVCodecContext *avctx);
  250. // The size of any private data structure associated with each
  251. // picture (can be zero if not required).
  252. size_t picture_priv_data_size;
  253. // The size of the parameter structures:
  254. // sizeof(VAEnc{type}ParameterBuffer{codec}).
  255. size_t sequence_params_size;
  256. size_t picture_params_size;
  257. size_t slice_params_size;
  258. // Fill the parameter structures.
  259. int (*init_sequence_params)(AVCodecContext *avctx);
  260. int (*init_picture_params)(AVCodecContext *avctx,
  261. VAAPIEncodePicture *pic);
  262. int (*init_slice_params)(AVCodecContext *avctx,
  263. VAAPIEncodePicture *pic,
  264. VAAPIEncodeSlice *slice);
  265. // The type used by the packed header: this should look like
  266. // VAEncPackedHeader{something}.
  267. int sequence_header_type;
  268. int picture_header_type;
  269. int slice_header_type;
  270. // Write the packed header data to the provided buffer.
  271. // The sequence header is also used to fill the codec extradata
  272. // when the encoder is starting.
  273. int (*write_sequence_header)(AVCodecContext *avctx,
  274. char *data, size_t *data_len);
  275. int (*write_picture_header)(AVCodecContext *avctx,
  276. VAAPIEncodePicture *pic,
  277. char *data, size_t *data_len);
  278. int (*write_slice_header)(AVCodecContext *avctx,
  279. VAAPIEncodePicture *pic,
  280. VAAPIEncodeSlice *slice,
  281. char *data, size_t *data_len);
  282. // Fill an extra parameter structure, which will then be
  283. // passed to vaRenderPicture(). Will be called repeatedly
  284. // with increasing index argument until AVERROR_EOF is
  285. // returned.
  286. int (*write_extra_buffer)(AVCodecContext *avctx,
  287. VAAPIEncodePicture *pic,
  288. int index, int *type,
  289. char *data, size_t *data_len);
  290. // Write an extra packed header. Will be called repeatedly
  291. // with increasing index argument until AVERROR_EOF is
  292. // returned.
  293. int (*write_extra_header)(AVCodecContext *avctx,
  294. VAAPIEncodePicture *pic,
  295. int index, int *type,
  296. char *data, size_t *data_len);
  297. } VAAPIEncodeType;
  298. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  299. const AVFrame *input_image, int *got_packet);
  300. int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
  301. int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
  302. int ff_vaapi_encode_init(AVCodecContext *avctx);
  303. int ff_vaapi_encode_close(AVCodecContext *avctx);
  304. #define VAAPI_ENCODE_COMMON_OPTIONS \
  305. { "low_power", \
  306. "Use low-power encoding mode (only available on some platforms; " \
  307. "may not support all encoding features)", \
  308. OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
  309. { .i64 = 0 }, 0, 1, FLAGS }, \
  310. { "idr_interval", \
  311. "Distance (in I-frames) between IDR frames", \
  312. OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
  313. { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
  314. { "b_depth", \
  315. "Maximum B-frame reference depth", \
  316. OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
  317. { .i64 = 1 }, 1, INT_MAX, FLAGS }
  318. #endif /* AVCODEC_VAAPI_ENCODE_H */