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.

311 lines
9.7KB

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