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.

314 lines
9.8KB

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