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.

343 lines
11KB

  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_PICTURE_REFERENCES = 2,
  34. MAX_REORDER_DELAY = 16,
  35. MAX_PARAM_BUFFER_SIZE = 1024,
  36. };
  37. enum {
  38. PICTURE_TYPE_IDR = 0,
  39. PICTURE_TYPE_I = 1,
  40. PICTURE_TYPE_P = 2,
  41. PICTURE_TYPE_B = 3,
  42. };
  43. typedef struct VAAPIEncodeSlice {
  44. int index;
  45. int row_start;
  46. int row_size;
  47. int block_start;
  48. int block_size;
  49. void *priv_data;
  50. void *codec_slice_params;
  51. } VAAPIEncodeSlice;
  52. typedef struct VAAPIEncodePicture {
  53. struct VAAPIEncodePicture *next;
  54. int64_t display_order;
  55. int64_t encode_order;
  56. int64_t pts;
  57. int type;
  58. int input_available;
  59. int encode_issued;
  60. int encode_complete;
  61. AVFrame *input_image;
  62. VASurfaceID input_surface;
  63. AVFrame *recon_image;
  64. VASurfaceID recon_surface;
  65. int nb_param_buffers;
  66. VABufferID *param_buffers;
  67. AVBufferRef *output_buffer_ref;
  68. VABufferID output_buffer;
  69. void *priv_data;
  70. void *codec_picture_params;
  71. int nb_refs;
  72. struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
  73. int nb_slices;
  74. VAAPIEncodeSlice *slices;
  75. } VAAPIEncodePicture;
  76. typedef struct VAAPIEncodeProfile {
  77. // lavc profile value (FF_PROFILE_*).
  78. int av_profile;
  79. // Supported bit depth.
  80. int depth;
  81. // Number of components.
  82. int nb_components;
  83. // Chroma subsampling in width dimension.
  84. int log2_chroma_w;
  85. // Chroma subsampling in height dimension.
  86. int log2_chroma_h;
  87. // VAAPI profile value.
  88. VAProfile va_profile;
  89. } VAAPIEncodeProfile;
  90. typedef struct VAAPIEncodeContext {
  91. const AVClass *class;
  92. // Codec-specific hooks.
  93. const struct VAAPIEncodeType *codec;
  94. // Global options.
  95. // Use low power encoding mode.
  96. int low_power;
  97. // Desired packed headers.
  98. unsigned int desired_packed_headers;
  99. // The required size of surfaces. This is probably the input
  100. // size (AVCodecContext.width|height) aligned up to whatever
  101. // block size is required by the codec.
  102. int surface_width;
  103. int surface_height;
  104. // The block size for slice calculations.
  105. int slice_block_width;
  106. int slice_block_height;
  107. // Everything above this point must be set before calling
  108. // ff_vaapi_encode_init().
  109. // Chosen encoding profile details.
  110. const VAAPIEncodeProfile *profile;
  111. // Encoding profile (VAProfile*).
  112. VAProfile va_profile;
  113. // Encoding entrypoint (VAEntryoint*).
  114. VAEntrypoint va_entrypoint;
  115. // Rate control mode.
  116. unsigned int va_rc_mode;
  117. // Bitrate for codec-specific encoder parameters.
  118. unsigned int va_bit_rate;
  119. // Packed headers which will actually be sent.
  120. unsigned int va_packed_headers;
  121. // Configuration attributes to use when creating va_config.
  122. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  123. int nb_config_attributes;
  124. VAConfigID va_config;
  125. VAContextID va_context;
  126. AVBufferRef *device_ref;
  127. AVHWDeviceContext *device;
  128. AVVAAPIDeviceContext *hwctx;
  129. // The hardware frame context containing the input frames.
  130. AVBufferRef *input_frames_ref;
  131. AVHWFramesContext *input_frames;
  132. // The hardware frame context containing the reconstructed frames.
  133. AVBufferRef *recon_frames_ref;
  134. AVHWFramesContext *recon_frames;
  135. // Pool of (reusable) bitstream output buffers.
  136. AVBufferPool *output_buffer_pool;
  137. // Global parameters which will be applied at the start of the
  138. // sequence (includes rate control parameters below).
  139. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  140. size_t global_params_size[MAX_GLOBAL_PARAMS];
  141. int nb_global_params;
  142. // Rate control parameters.
  143. struct {
  144. VAEncMiscParameterBuffer misc;
  145. VAEncMiscParameterRateControl rc;
  146. } rc_params;
  147. struct {
  148. VAEncMiscParameterBuffer misc;
  149. VAEncMiscParameterHRD hrd;
  150. } hrd_params;
  151. struct {
  152. VAEncMiscParameterBuffer misc;
  153. VAEncMiscParameterFrameRate fr;
  154. } fr_params;
  155. #if VA_CHECK_VERSION(0, 36, 0)
  156. struct {
  157. VAEncMiscParameterBuffer misc;
  158. VAEncMiscParameterBufferQualityLevel quality;
  159. } quality_params;
  160. #endif
  161. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  162. void *codec_sequence_params;
  163. // Per-sequence parameters found in the per-picture parameter
  164. // structure (VAEncPictureParameterBuffer*).
  165. void *codec_picture_params;
  166. // Current encoding window, in display (input) order.
  167. VAAPIEncodePicture *pic_start, *pic_end;
  168. // Next input order index (display order).
  169. int64_t input_order;
  170. // Number of frames that output is behind input.
  171. int64_t output_delay;
  172. // Number of frames decode output will need to be delayed.
  173. int64_t decode_delay;
  174. // Next output order index (encode order).
  175. int64_t output_order;
  176. enum {
  177. // All encode operations are done independently (synchronise
  178. // immediately after every operation).
  179. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  180. // Overlap as many operations as possible.
  181. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  182. // Overlap operations only when satisfying parallel dependencies.
  183. ISSUE_MODE_MINIMISE_LATENCY,
  184. } issue_mode;
  185. // Timestamp handling.
  186. int64_t first_pts;
  187. int64_t dts_pts_diff;
  188. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  189. // Slice structure.
  190. int slice_block_rows;
  191. int slice_block_cols;
  192. int nb_slices;
  193. int slice_size;
  194. // Frame type decision.
  195. int gop_size;
  196. int p_per_i;
  197. int b_per_p;
  198. int force_idr;
  199. int gop_counter;
  200. int p_counter;
  201. int end_of_stream;
  202. } VAAPIEncodeContext;
  203. enum {
  204. // Codec supports controlling the subdivision of pictures into slices.
  205. FLAG_SLICE_CONTROL = 1 << 0,
  206. // Codec only supports constant quality (no rate control).
  207. FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
  208. };
  209. typedef struct VAAPIEncodeType {
  210. // List of supported profiles and corresponding VAAPI profiles.
  211. // (Must end with FF_PROFILE_UNKNOWN.)
  212. const VAAPIEncodeProfile *profiles;
  213. // Codec feature flags.
  214. int flags;
  215. // Perform any extra codec-specific configuration after the
  216. // codec context is initialised (set up the private data and
  217. // add any necessary global parameters).
  218. int (*configure)(AVCodecContext *avctx);
  219. // The size of any private data structure associated with each
  220. // picture (can be zero if not required).
  221. size_t picture_priv_data_size;
  222. // The size of the parameter structures:
  223. // sizeof(VAEnc{type}ParameterBuffer{codec}).
  224. size_t sequence_params_size;
  225. size_t picture_params_size;
  226. size_t slice_params_size;
  227. // Fill the parameter structures.
  228. int (*init_sequence_params)(AVCodecContext *avctx);
  229. int (*init_picture_params)(AVCodecContext *avctx,
  230. VAAPIEncodePicture *pic);
  231. int (*init_slice_params)(AVCodecContext *avctx,
  232. VAAPIEncodePicture *pic,
  233. VAAPIEncodeSlice *slice);
  234. // The type used by the packed header: this should look like
  235. // VAEncPackedHeader{something}.
  236. int sequence_header_type;
  237. int picture_header_type;
  238. int slice_header_type;
  239. // Write the packed header data to the provided buffer.
  240. // The sequence header is also used to fill the codec extradata
  241. // when the encoder is starting.
  242. int (*write_sequence_header)(AVCodecContext *avctx,
  243. char *data, size_t *data_len);
  244. int (*write_picture_header)(AVCodecContext *avctx,
  245. VAAPIEncodePicture *pic,
  246. char *data, size_t *data_len);
  247. int (*write_slice_header)(AVCodecContext *avctx,
  248. VAAPIEncodePicture *pic,
  249. VAAPIEncodeSlice *slice,
  250. char *data, size_t *data_len);
  251. // Fill an extra parameter structure, which will then be
  252. // passed to vaRenderPicture(). Will be called repeatedly
  253. // with increasing index argument until AVERROR_EOF is
  254. // returned.
  255. int (*write_extra_buffer)(AVCodecContext *avctx,
  256. VAAPIEncodePicture *pic,
  257. int index, int *type,
  258. char *data, size_t *data_len);
  259. // Write an extra packed header. Will be called repeatedly
  260. // with increasing index argument until AVERROR_EOF is
  261. // returned.
  262. int (*write_extra_header)(AVCodecContext *avctx,
  263. VAAPIEncodePicture *pic,
  264. int index, int *type,
  265. char *data, size_t *data_len);
  266. } VAAPIEncodeType;
  267. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  268. const AVFrame *input_image, int *got_packet);
  269. int ff_vaapi_encode_init(AVCodecContext *avctx);
  270. int ff_vaapi_encode_close(AVCodecContext *avctx);
  271. #define VAAPI_ENCODE_COMMON_OPTIONS \
  272. { "low_power", \
  273. "Use low-power encoding mode (only available on some platforms; " \
  274. "may not support all encoding features)", \
  275. OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
  276. { .i64 = 0 }, 0, 1, FLAGS }
  277. #endif /* AVCODEC_VAAPI_ENCODE_H */